- /* Protocol inspired by, but not identical to, RFC 1663.
- * There is currently no protocol establishment phase, and a single
- * byte ("packet type") to identify the kind/target/etc of each packet.
- *
- * The protocol is really, really not aiming for high performance.
- *
- * There is however a hook for future extensions: implementations are
- * required to answer all unknown packet types with a P_UNKNOWN packet.
- *
- * To summarise the protocol:
- * - the two sides (A & B) are connected by a (potentially unreliable)
- * byte stream
- * - the two sides exchange packets framed by 0x7e (SYNC_BYTE) bytes
- * - each packet has the form
- * <packet type> <data bytes 1..n> <16-bit crc>
- * where the crc (see net.tinyos.util.Crc) covers the packet type
- * and bytes 1..n
- * - bytes can be escaped by preceding them with 0x7d and their
- * value xored with 0x20; 0x7d and 0x7e bytes must be escaped,
- * 0x00 - 0x1f and 0x80-0x9f may be optionally escaped
- * - There are currently 5 packet types:
- * P_PACKET_NO_ACK: A user-packet, with no ack required
- * P_PACKET_ACK: A user-packet with a prefix byte, ack required.
- * The receiver must send a P_ACK packet with the prefix byte
- * as its contents.
- * P_ACK: ack for a previous P_PACKET_ACK packet
- * P_UNKNOWN: unknown packet type received. On reception of an
- * unknown packet type, the receicer must send a P_UNKNOWN packet,
- * the first byte must be the unknown packet type.
- * - Packets that are greater than a (private) MTU are silently dropped.
- */
- final static boolean DEBUG = false;
-
- final static int SYNC_BYTE = Serial.HDLC_FLAG_BYTE;
- final static int ESCAPE_BYTE = Serial.HDLC_CTLESC_BYTE;
- final static int MTU = 256;
- final static int ACK_TIMEOUT = 1000; // in milliseconds
-
- final static int P_ACK = Serial.SERIAL_PROTO_ACK;
- final static int P_PACKET_ACK = Serial.SERIAL_PROTO_PACKET_ACK;
- final static int P_PACKET_NO_ACK = Serial.SERIAL_PROTO_PACKET_NOACK;
- final static int P_UNKNOWN = Serial.SERIAL_PROTO_PACKET_UNKNOWN;
-
- private ByteSource io;
- private boolean inSync;
- private byte[] receiveBuffer = new byte[MTU];
- private int seqNo;
-
- // Packets are received by a separate thread and placed in a
- // per-packet-type queue. If received[x] is null, then x is an
- // unknown protocol (but P_UNKNOWN and P_PACKET_ACK are handled
- // specially)
- private Thread reader;
- private LinkedList[] received;
-
- /**
- * Packetizers are built using the makeXXX methods in BuildSource
- */
- Packetizer(String name, ByteSource io) {
- super(name);
- this.io = io;
- inSync = false;
- seqNo = 13;
- reader = new Thread(this);
- received = new LinkedList[256];
- received[P_ACK] = new LinkedList();
- received[P_PACKET_NO_ACK] = new LinkedList();
- }
+ /*
+ * Protocol inspired by, but not identical to, RFC 1663. There is currently no
+ * protocol establishment phase, and a single byte ("packet type") to identify
+ * the kind/target/etc of each packet.
+ *
+ * The protocol is really, really not aiming for high performance.
+ *
+ * There is however a hook for future extensions: implementations are required
+ * to answer all unknown packet types with a P_UNKNOWN packet.
+ *
+ * To summarise the protocol: - the two sides (A & B) are connected by a
+ * (potentially unreliable) byte stream - the two sides exchange packets
+ * framed by 0x7e (SYNC_BYTE) bytes - each packet has the form <packet type>
+ * <data bytes 1..n> <16-bit crc> where the crc (see net.tinyos.util.Crc)
+ * covers the packet type and bytes 1..n - bytes can be escaped by preceding
+ * them with 0x7d and their value xored with 0x20; 0x7d and 0x7e bytes must be
+ * escaped, 0x00 - 0x1f and 0x80-0x9f may be optionally escaped - There are
+ * currently 5 packet types: P_PACKET_NO_ACK: A user-packet, with no ack
+ * required P_PACKET_ACK: A user-packet with a prefix byte, ack required. The
+ * receiver must send a P_ACK packet with the prefix byte as its contents.
+ * P_ACK: ack for a previous P_PACKET_ACK packet P_UNKNOWN: unknown packet
+ * type received. On reception of an unknown packet type, the receicer must
+ * send a P_UNKNOWN packet, the first byte must be the unknown packet type. -
+ * Packets that are greater than a (private) MTU are silently dropped.
+ */
+ final static boolean DEBUG = false;
+
+ final static int SYNC_BYTE = Serial.HDLC_FLAG_BYTE;
+
+ final static int ESCAPE_BYTE = Serial.HDLC_CTLESC_BYTE;
+
+ final static int MTU = 256;
+
+ final static int ACK_TIMEOUT = 1000; // in milliseconds
+
+ final static int P_ACK = Serial.SERIAL_PROTO_ACK;
+
+ final static int P_PACKET_ACK = Serial.SERIAL_PROTO_PACKET_ACK;
+
+ final static int P_PACKET_NO_ACK = Serial.SERIAL_PROTO_PACKET_NOACK;
+
+ final static int P_UNKNOWN = Serial.SERIAL_PROTO_PACKET_UNKNOWN;
+
+ private ByteSource io;
+
+ private boolean inSync;
+
+ private byte[] receiveBuffer = new byte[MTU];
+
+ private int seqNo;
+
+ // Packets are received by a separate thread and placed in a
+ // per-packet-type queue. If received[x] is null, then x is an
+ // unknown protocol (but P_UNKNOWN and P_PACKET_ACK are handled
+ // specially)
+ private Thread reader;
+
+ private LinkedList[] received;
+
+ /**
+ * Packetizers are built using the makeXXX methods in BuildSource
+ */
+ Packetizer(String name, ByteSource io) {
+ super(name);
+ this.io = io;
+ inSync = false;
+ seqNo = 13;
+ reader = new Thread(this);
+ received = new LinkedList[256];
+ received[P_ACK] = new LinkedList();
+ received[P_PACKET_NO_ACK] = new LinkedList();
+ }