[Firebug-cvs] firebug/project/zaurus/net/tinyos/tostools Listener.java,NONE,1.1 RawPacket.java,NONE,
Brought to you by:
doolin
From: <do...@us...> - 2003-07-29 23:33:19
|
Update of /cvsroot/firebug/firebug/project/zaurus/net/tinyos/tostools In directory sc8-pr-cvs1:/tmp/cvs-serv5712/net/tinyos/tostools Added Files: Listener.java RawPacket.java Log Message: Added serial port listening classes to tostools. Everything compiles ok, needs to be tested on a pc with a known, working serial port. --- NEW FILE: Listener.java --- /** * The authors grant permission to redistribute this software, provided this * copyright and a copy of this license (for reference) are retained * in all distributed copies. * * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR * MODIFICATIONS. * * @author Mike Chen, Philip Levis, Max Min Chen, David M. Doolin * Last Modified: $Date: 2003/07/29 23:33:16 $ * */ package net.tinyos.tostools; import java.util.*; import java.io.*; import javax.comm.*; //import net.tinyos.util.*; public class Listener { private static String CLASS_NAME = "Listener"; // These three need to go into the PacketPrinter interface private static final int MAX_MSG_SIZE = 36; private static final int PORT_SPEED = 19200; private static final int LENGTH_OFFSET = 4; private int packetLength; // Toggle with -e flag private static boolean showEntireMessage = false; private CommPortIdentifier portId; private SerialPort port; private String portName; private InputStream in; private OutputStream out; /** Use this interface when changing the output * of the packet printing. Note that the packet * byte array will NOT be passed in from the original * head of the array, thus we might need the * packet length to completely parse it. The * packet length technique is carried forward * from the ListenRaw.java serial port code that * ListenFB was derived from. It may be possible to * completely eliminate the paremeter if the length * of the byte buffer is modified when passed. I don't * think that it will be because of pass-by-ref * semantics, but should experiment to find out. * Another way might be to just spin to the end of the * buffer, which could work if there is a terminating * byte, which I do not think there is... * * @todo Experiment with getting rid of packet length * parameter. */ public interface PacketPrinter { static final int MAX_MSG_SIZE = 36; static final int PORT_SPEED = 19200; static final int LENGTH_OFFSET = 4; /** Consider making this a static method. There * might be some trouble with threaded code though. * Also, the print method should take a byte array, * not an InputStream. */ public void print(InputStream in) throws IOException; public void print(byte [] packet); } public Listener(String portName) { this.portName = portName; } public void open() throws NoSuchPortException, PortInUseException, IOException, UnsupportedCommOperationException { System.out.println("Opening port " + portName); portId = CommPortIdentifier.getPortIdentifier(portName); port = (SerialPort)portId.open(CLASS_NAME, 0); in = port.getInputStream(); out = port.getOutputStream(); port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); port.disableReceiveFraming(); // These are the mote UART parameters port.setSerialPortParams(PORT_SPEED, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); printPortStatus(); System.out.println(); } private void printPortStatus() { System.out.println(" baud rate: " + port.getBaudRate()); System.out.println(" data bits: " + port.getDataBits()); System.out.println(" stop bits: " + port.getStopBits()); System.out.println(" parity: " + port.getParity()); } private static void printAllPorts() { Enumeration ports = CommPortIdentifier.getPortIdentifiers(); if (ports == null) { System.out.println("No comm ports found!"); return; } // print out all ports System.out.println("printing all ports..."); while (ports.hasMoreElements()) { System.out.println(" " + ((CommPortIdentifier)ports.nextElement()).getName()); } } /** FIXME: Need some changes here. First, change the * call to printer.print to take a byte array. * Second, preparse the byte array to extract the * type of message to print. The printers can go into * a hash table or something. Third, dispatch the * byte array (with offset and length?) to the appropriate * msg printer. */ public void read(Vector printers) throws IOException { //public void read(PacketPrinter printer) throws IOException { int i; int count = 0; byte[] packet = new byte[MAX_MSG_SIZE]; // Note that i is an integer, in.read() // is returning 4 bytes at a time. while ((i = in.read()) != -1) { String val = Integer.toHexString( i & 0xff); if (val.length() == 1) { val = "0" + val; } if(i == 0x7e || count != 0){ // We have to cast i from int to byte... packet[count] = (byte)i; System.out.print(val + " "); // Packet data count++; if (count >= MAX_MSG_SIZE) { System.out.println(); count = 0; packetLength = MAX_MSG_SIZE; // Might be faster to index the Vector. Enumeration enum = printers.elements(); while (enum.hasMoreElements()) { PacketPrinter printer = (PacketPrinter)enum.nextElement(); printer.print(packet); } } } else{ System.out.println("extra byte: " + val); } } } private static void printUsage() { //System.err.println("usage: java ListenFB [options] <port>"); System.err.println("usage: java ListenFB <database name> <port>"); /* System.err.println("options are:"); System.err.println(" -h, --help: usage help"); System.err.println(" -p: print available ports"); System.err.println(" -e: display entire message"); */ System.exit(-1); } /** Kinda kludgy */ static String dbname; static void handle_args(String [] args) { if (args.length != 3) { printUsage(); } /** None of the following is currently being used. */ /* for (int i = 0; i < args.length; i++) { if (args[i].equals("-h") || args[i].equals("--help")) { printUsage(); } if (args[i].equals("-p")) { printAllPorts(); } if (args[i].equals("-e")) { showEntireMessage = true; } } */ dbname = args[1]; if (args[args.length - 1].charAt(0) == '-') { System.out.println("No port specified."); System.exit(-1); } } public static void main(String args[]) { /* System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); */ //handle_args(args); Vector printers = new Vector(5); String port = args[args.length - 1]; Listener reader = new Listener(port); RawPacket print = new RawPacket(); printers.add(print); try { reader.open(); reader.read(printers); } catch (Exception e) { e.printStackTrace(); } } } --- NEW FILE: RawPacket.java --- /* * This software is copyrighted by David M. Doolin and the Regents of * the University of California. The following terms apply to all * files associated with the software unless explicitly disclaimed in * individual files. * * The authors hereby grant permission to use this software without * fee or royalty for any non-commercial purpose. The authors also * grant permission to redistribute this software, provided this * copyright and a copy of this license (for reference) are retained * in all distributed copies. * * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR * MODIFICATIONS. * * @author David M. Doolin * Last Modified: $Date: 2003/07/29 23:33:16 $ * */ package net.tinyos.tostools; import java.util.*; import java.io.*; import javax.comm.*; //import net.tinyos.util.*; public class RawPacket implements Listener.PacketPrinter { private static boolean showEntireMessage = false; private int packetLength; private static String CLASS_NAME = "RawPacket"; private static final int MAX_MSG_SIZE = 36; private static final int LENGTH_OFFSET = 5; public void print(byte [] packet) { } public void print(InputStream in) throws IOException { System.out.println("Printing from RawPacket..."); int i; int count = 0; byte[] packet = new byte[MAX_MSG_SIZE]; while ((i = in.read()) != -1) { String val = Integer.toHexString( i & 0xff); if (val.length() == 1) { val = "0" + val; } if(i == 0x7e || count != 0){ packet[count] = (byte)i; if (count == LENGTH_OFFSET) { // Figure out length of packet System.out.print(val + " "); packetLength = i + count; if (packetLength > MAX_MSG_SIZE - LENGTH_OFFSET) { System.err.print("!"); // If too long, print a ! packetLength = MAX_MSG_SIZE; } } // Don't print data after the packet else if (!showEntireMessage && (count > packetLength) && (count < MAX_MSG_SIZE)) {} else { //System.out.print("data: " + val + " "); // Packet data System.out.print(val + " "); // Packet data } count++; if (count >= MAX_MSG_SIZE) { count = 0; packetLength = MAX_MSG_SIZE; } } else{ System.out.println("extra byte: " + val); } } // end of while loop } } |