Send data from server to node using serial forwarder
Send data from server to node using serial forwarder
Note: this wiki page is only for TinyOS 2.x serial packet standard
1. To open the serial forwarder on testbed, run the command
forwarder.py -2 -a telosb
2. We can get the packet format used by the serial forwarder in tospacket.py:
byte 0: Delimiter (0x7E)
byte 1: Protocol: ACK (0x43), PACKET_ACK (0x44) or PACKET_NOACK (0x45)
byte 2: Sequence number (omitted if protocol is PACKET_NOACK)
byte 3: Packet type (0x00, omitted if protocol is ACK)
byte 4: Destination high byte (omitted if protocol is ACK)
byte 5: Destination low byte (omitted if protocol is ACK)
byte 6: Source high byte (omitted if protocol is ACK)
byte 7: Source low byte (omitted if protocol is ACK)
byte 8: Payload length (omitted if protocol is ACK)
byte 9: Destination PAN group (omitted if protocol is ACK)
byte 10: Packet AM type (omitted if protocol is ACK)
byte 11 .. n-4: Payload (omitted if protocol is ACK)
byte n-3: CRC-16 low byte
byte n-2: CRC-16 high byte
byte n-1: Delimiter (0x7E)
3. In tinyos standard, the delimiter character is 0x7E, the escape character is 0x7D
4. But we don't need the whole Tos packet as above to use the serial forwarder, the serial forwarder will do a lot of things for us.
5. what we don't need to send to the serial forwarder:
byte 0 and byte n-1: Delimiter(added by serial forwarder server)
byte 1 : Protocol Type(this byte should be given by the serial forwarder server)
byte 2 : Sequence Number(this byte should be given by the serial forwarder server)
byte n-2 and n-3 : the two-byte-CRC is calculated by the serial forwarder
6. Next we will name the packet we sent to the serial forwarder server as local packet, and the packet sent by serial forwarder server to the mote as forward packet
7. The process of using the serial forwarder
7.1 First create a socket, and connect the socket to the server at port 9002
7.2 Send [0x55, 0x20] to the server. This is a handshake signal used by the serial forwarder. The handshake signal for Tos2 is 'U ', it is uppercase U followed by a whitespace.
7.3 Calculate the length of the local packet, send the length to the server(should be one byte, so the maximum length of the serial packet won't exceed 256 byte)
7.4 Send the local packet to server
Note: The handshake signal, the length of the local packet, and the local packet itself don't need to be escaped, the serial forwarder will do the escaping itself
Example:
Step 1 Suppose you want to send some information to node 155, then according to the tospacket.py, you need to set up the local packet as bellow:
byte 0: Delimiter, no need, given by the serial forwarder server
byte 1: Protocol, no need, given by the serial forwarder server
byte 2: Sequence Number, no need, given by the serial forwarder server
byte 3: Packet type, 0x00
byte 4: Destination high byte, 0x00
byte 5: Destination low byte, 0x9B
byte 6: Source high byte, 0x00
byte 7: Source low byte, 0x00
byte 8: Payload length, 0x08
byte 9: Destination PAN group, 0x00
byte 10: Packet AM type, 0x67
byte 11 .. n-4: Payload, give some example payload here:
0x00, 0x95, 0x00,0x9B, 0x00, 0x03, 0x00, 0x0C
byte n-3: CRC-16 low byte, no need, calculated by server
byte n-2: CRC-16 high byte, no need, calculated by server
byte n-1: Delimiter (0x7E), no need, given by server
The array view of local packet:
[0x00, 0x00, 0x9B, 0x00, 0x00, 0x08, 0x00, 0x67, 0x00, 0x95, 0x00, 0x9B, 0x00, 0x03, 0x00, 0x0C]
Step 2
We can get the length of the local packet, it is 16
Step 3
Create the socket, connect the socket to server at port 9002 using TCP, send [0x55, 0x20] first as hand shake signal
Step 4
Still using that connected socket, send the length of the local packet 16
Step 5
Use the socket to send the local packet
So the final data sent to the serial forwarder is:
[0x55, 0x20, 0x10, 0x00, 0x00, 0x9B, 0x00, 0x00, 0x08, 0x00, 0x67, 0x00, 0x95, 0x00, 0x9B, 0x00, 0x03, 0x00, 0x0C]
Step 6
When the serial forwarder server received all the stuff, it will do the calculation, and add essential information for the packet, calculate the CRC
Suppose we get:
protocol type as PACKET_ACK(0x44)
sequence number as 48(0x30)
the CRC as 246 130(0xF6 0x82)
The final forward packet that received by the mote would be
[0x7E, 0x44, 0x30, 0x00, 0x00, 0x9B, 0x00, 0x00, 0x08, 0x00, 0x67, 0x00, 0x95, 0x00, 0x9B, 0x00, 0x03, 0x00, 0x0C, 0xF6, 0x82, 0x7E]
Notes about serial forwarder server code:
The most important code is in sfserver.py:
1. In the class BaseTosHandler, the function handle() define what to do when received data
2. handle() will first invoke handle_handshake() to process the handshake signal
3. then handle() read the byte for length of packet
4. handle() read the local packet
5. handle() invokes self.server.send(bytes_) to send packet to mote
6. all the work such as escaping the data, giving delimiter, protocol type and sequence number, and CRC calculation is done in the send() function