Commit ad18b4d58488dc0818c4c3e27843ec70acb8e96d

kanoi 2013-08-23T17:08:14

Merge pull request #484 from kanoi/master BTB show C/MHz/mV for device + api.c remove unused reply string

diff --git a/MCast.class b/MCast.class
new file mode 100644
index 0000000..09dde32
Binary files /dev/null and b/MCast.class differ
diff --git a/MCast.java b/MCast.java
new file mode 100644
index 0000000..4ba237e
--- /dev/null
+++ b/MCast.java
@@ -0,0 +1,333 @@
+/*
+ *
+ * Copyright (C) Andrew Smith 2013
+ *
+ * Usage: java MCast [-v] code toaddr port replyport wait
+ *
+ * If any are missing or blank they use the defaults:
+ *
+ *	-v means report how long the last reply took
+ *
+ *	code		= 'FTW'
+ *	toaddr		= '224.0.0.75'
+ *	port		= '4028'
+ *	replyport	= '4027'
+ *	wait		= '1000'
+ *
+ */
+
+import java.net.*;
+import java.io.*;
+import java.util.*;
+
+class MCast implements Runnable
+{
+	static private final String MCAST_CODE = "FTW";
+	static private final String MCAST_ADDR = "224.0.0.75";
+	static private final int MCAST_PORT = 4028;
+	static private final int MCAST_REPORT = 4027;
+	static private final int MCAST_WAIT4 = 1000;
+
+	static private String code = MCAST_CODE;
+	static private String addr = MCAST_ADDR;
+	static private int port = MCAST_PORT;
+	static private int report = MCAST_REPORT;
+	static private int wait4 = MCAST_WAIT4;
+
+	private InetAddress mcast_addr = null;
+
+	static private final Integer lock = new Integer(666);
+
+	static private boolean ready = false;
+
+	static private Thread listen = null;
+
+	static public boolean verbose = false;
+
+	static private Date start = null;
+	static private Date last = null;
+	static boolean got_last = false;
+
+	static public void usAge()
+	{
+		System.err.println("usAge: java MCast [-v] [code [toaddr [port [replyport [wait]]]]]");
+		System.err.println("       -v=report elapsed ms to last reply");
+		System.err.println("     Anything below missing or blank will use it's default");
+		System.err.println("       code=X in cgminer-X-Port default="+MCAST_CODE);
+		System.err.println("       toaddr=multicast address default="+MCAST_ADDR);
+		System.err.println("       port=multicast port default="+MCAST_PORT);
+		System.err.println("       replyport=local post to listen for replies default="+MCAST_REPORT);
+		System.err.println("       wait=how long to wait for replies default="+MCAST_WAIT4+"ms");
+		System.exit(1);
+	}
+
+	private int port(String _port, String name)
+	{
+		int tmp = 0;
+
+		try
+		{
+			tmp = Integer.parseInt(_port);
+		}
+		catch (NumberFormatException nfe)
+		{
+			System.err.println("Invalid " + name + " - must be a number between 1 and 65535");
+			usAge();
+			System.exit(1);
+		}
+
+		if (tmp < 1 || tmp > 65535)
+		{
+			System.err.println("Invalid " + name + " - must be a number between 1 and 65535");
+			usAge();
+			System.exit(1);
+		}
+
+		return tmp;
+	}
+
+	public void set_code(String _code)
+	{
+		if (_code.length() > 0)
+			code = _code;
+	}
+
+	public void set_addr(String _addr)
+	{
+		if (_addr.length() > 0)
+		{
+			addr = _addr;
+
+			try
+			{
+				mcast_addr = InetAddress.getByName(addr);
+			}
+			catch (Exception e)
+			{
+				System.err.println("ERR: Invalid multicast address");
+				usAge();
+				System.exit(1);
+			}
+		}
+	}
+
+	public void set_port(String _port)
+	{
+		if (_port.length() > 0)
+			port = port(_port, "port");
+	}
+
+	public void set_report(String _report)
+	{
+		if (_report.length() > 0)
+			report = port(_report, "reply port");
+	}
+
+	public void set_wait(String _wait4)
+	{
+		if (_wait4.length() > 0)
+		{
+			try
+			{
+				wait4 = Integer.parseInt(_wait4);
+			}
+			catch (NumberFormatException nfe)
+			{
+				System.err.println("Invalid wait - must be a number between 0ms and 60000ms");
+				usAge();
+				System.exit(1);
+			}
+
+			if (wait4 < 0 || wait4 > 60000)
+			{
+				System.err.println("Invalid wait - must be a number between 0ms and 60000ms");
+				usAge();
+				System.exit(1);
+			}
+		}
+	}
+
+	public void run() // listen
+	{
+		byte[] message = new byte[1024];
+		DatagramSocket socket = null;
+		DatagramPacket packet = null;
+
+		try
+		{
+			socket = new DatagramSocket(report);
+			packet = new DatagramPacket(message, message.length);
+
+			synchronized (lock)
+			{
+				ready = true;
+			}
+
+			while (true)
+			{
+				socket.receive(packet);
+
+				synchronized (lock)
+				{
+					last = new Date();
+				}
+
+				int off = packet.getOffset();
+				int len = packet.getLength();
+
+				System.out.println("Got: '" + new String(message, off, len) + "' from" + packet.getSocketAddress());
+			}
+		}
+		catch (Exception e)
+		{
+			socket.close();
+		}
+	}
+
+	public void sendMCast()
+	{
+		try
+		{
+			String message = new String("cgminer-" + code + "-" + report);
+			MulticastSocket socket = null;
+			DatagramPacket packet = null;
+
+			socket = new MulticastSocket();
+			packet = new DatagramPacket(message.getBytes(), message.length(), mcast_addr, port);
+
+			System.out.println("About to send " + message + " to " + mcast_addr + ":" + port);
+
+			start = new Date();
+
+			socket.send(packet);
+
+			socket.close();
+		}
+		catch (Exception e)
+		{
+			e.printStackTrace();
+		}
+	}
+
+	public void init()
+	{
+		MCast lis = new MCast();
+		listen = new Thread(lis);
+		listen.start();
+
+		while (true)
+		{
+			synchronized (lock)
+			{
+				if (ready)
+					break;
+			}
+
+			try
+			{
+				Thread.sleep(100);
+			}
+			catch (Exception sl1)
+			{
+			}
+		}
+
+		try
+		{
+			Thread.sleep(500);
+		}
+		catch (Exception sl2)
+		{
+		}
+
+		sendMCast();
+
+		try
+		{
+			Thread.sleep(wait4);
+		}
+		catch (Exception sl3)
+		{
+		}
+
+		listen.interrupt();
+
+		if (verbose)
+		{
+			try
+			{
+				Thread.sleep(100);
+			}
+			catch (Exception sl4)
+			{
+			}
+
+			synchronized (lock)
+			{
+				if (last == null)
+					System.out.println("No replies received");
+				else
+				{
+					long diff = last.getTime() - start.getTime();
+					System.out.println("Last reply took " + diff + "ms");
+				}
+			}
+		}
+
+		System.exit(0);
+	}
+
+	public MCast()
+	{
+	}
+
+	public static void main(String[] params) throws Exception
+	{
+		int p = 0;
+
+		MCast mcast = new MCast();
+
+		mcast.set_addr(MCAST_ADDR);
+
+		if (params.length > p)
+		{
+			if (params[p].equals("-?")
+			||  params[p].equalsIgnoreCase("-h")
+			||  params[p].equalsIgnoreCase("-help")
+			||  params[p].equalsIgnoreCase("--help"))
+				MCast.usAge();
+			else
+			{
+				if (params[p].equals("-v"))
+				{
+					mcast.verbose = true;
+					p++;
+				}
+
+				if (params.length > p)
+				{
+					mcast.set_code(params[p++]);
+
+					if (params.length > p)
+					{
+						mcast.set_addr(params[p++]);
+
+						if (params.length > p)
+						{
+							mcast.set_port(params[p++]);
+
+							if (params.length > p)
+							{
+								mcast.set_report(params[p++]);
+								if (params.length > p)
+									mcast.set_wait(params[p]);
+							}
+						}
+					}
+				}
+			}
+		}
+
+		mcast.init();
+	}
+}
diff --git a/api.c b/api.c
index 9b4ba65..2ea9ebd 100644
--- a/api.c
+++ b/api.c
@@ -4311,7 +4311,6 @@ static void mcast()
 	char *expect_code;
 	size_t expect_code_len;
 	char buf[1024];
-	char reply[] = "cgm-" API_MCAST_CODE "-"; // constant
 	char replybuf[1024];
 
 	memset(&grp, 0, sizeof(grp));
diff --git a/driver-avalon.c b/driver-avalon.c
index a863873..83cff74 100644
--- a/driver-avalon.c
+++ b/driver-avalon.c
@@ -1370,7 +1370,14 @@ static void get_avalon_statline_before(char *buf, size_t bufsiz, struct cgpu_inf
 	int lowfan = 10000;
 
 	if (usb_ident(avalon) == IDENT_BTB) {
-		tailsprintf(buf, bufsiz, "%2d/%3dC %4dmV | ", info->temp0, info->temp2, info->core_voltage);
+		int temp = info->temp0;
+		if (info->temp2 > temp)
+			temp = info->temp2;
+		if (temp > 99)
+			temp = 99;
+		if (temp < 0)
+			temp = 0;
+		tailsprintf(buf, bufsiz, "%2dC %3d %4dmV | ", temp, info->frequency, info->core_voltage);
 	} else {
 		/* Find the lowest fan speed of the ASIC cooling fans. */
 		if (info->fan1 >= 0 && info->fan1 < lowfan)