• No results found

5.4 ZigBee IoT Platform Implementation

5.4.4 Software implementation on IoT Gateway for XBee

5.4.4.3 Processing Tunnel Data

When the select function determines there is data available to be read from the tunnel file handle, the process in Figure 119 is used. The process extracts IPv6, and UDP headers from the data and creates an XBee API command from this information and the UDP payload. In order for a response message to be sent back to source of the command request the address and port of the source are stored in a lookup table.

Start Read Tunnel Data

Read Packet into buffer Is buffer length > 0

Yes

Parse IPv6 Header

from buffer Is next header UDP Yes

Parse UDP Header from buffer Is UDP destination port for Remote Command Request Add NAT entry in

lookup table using source address and port, this generates

a frame ID Yes Translate IPv6 destination address to XBee Module Address

Create an Xbee API command packet

header using the module address and

frame

Copy the Xbee API command header and UDP payload to

a buffer

Write the buffer to the serial file handle

End No

No

No

Figure 119 Flowchart of reading tunnel data in XBee IoT Gateway software

The details of each element in Figure 119 are as following, they are in the order of execution in the software.

x “Read Packet into buffer” performs the read operation from the tunnel file handle into a buffer. When reading from a tunnel the amount of data read is not the size of the buffer specified, but the amount of data available to be read from the handle. This means that the buffer used is the maximum size that an IPv6 packet can be on the interface (1500 bytes). The read function returns the actual number of bytes read, and hence the length of the IPv6 packet.

x “Is buffer length > 0” block checks that data was written into the buffer. If there was no data written an error has occurred and the process is stopped.

pg. 119 x “Parse IPv6 Header from buffer” extracts the IPv6 header into a structure from the buffer read

from the tunnel file handle. Parsing the header uses the technique described in section 5.3.2. The IPv6 header should contain the IPv6 address of the XBee module, which will be used later to create the XBee API packet.

x “Is next header UDP” checks if the next header field in the IPv6 header contains the value 17 which means the next header is a UDP header. If the next header is not a UDP header the process ends.

x “Parse UDP header from buffer” extracts the UDP header into a structure from the buffer using the technique described in section 5.3.2. The UDP header contains the source and destination port, and the length of the payload. The destination port is used to determine what type of API message to send to the XBee coordinator module. At present only the remote command request API message is supported, additional commands can be added in this process.

x “Is UDP destination port for Remote Command Request” checks the UDP destination port. If the destination port is not a predetermined port for a Remote Command Request then the process ends.

x “Translate IPv6 destination address to XBee Module Address” uses the technique in section 5.4.2 for translation the IPv6 address. This uses the destination address contained in the IPv6 structure parsed from the buffer.

x “Add NAT entry in lookup table using source address and port…” allows for the response to the command to be sent back to the source of the command. To create a response IPv6 UDP packet the destination address and destination port must be known, these are taken from the lookup table entry generated in this step. To know which entry to use the frame ID is used as a key for each entry. The frame ID is a field in the XBee API Remote Command Request header.

x “Create an XBee API command packet header…” combines information from the IPv6 header, UDP header and UDP payload in order to create a remote command request XBee API packet. A general XBee API header is created with the length of the payload and the command id of a remote command request, and is placed in a buffer. The remote command request is created using the IPv6 header and UDP payload, and then placed in the buffer after the general header. Lastly the payload of the remote command request is taken form the UDP payload. The result is a buffer that contains the raw XBee API packet for a remote command request with parameters specified using the IPv6 UDP packet.

x “Write the buffer to the serial file handle” is described in the next paragraph. Writing the buffer to the serial file involves escaping some of the characters and appending a checksum.

pg. 120

To write an XBee API packet to the serial port the process in Figure 120 is used. The process consists of creating the XBee general API header, putting the header at the beginning of buffer, appending a checksum to the buffer, and then writing the buffer to the serial port. When writing the buffer certain characters need to be escaped because the coordinator is in escaped API mode. The escaping of characters ensures that the start character 0x7e for XBee API packets only occurs at the start of a packet, and not inside the packet. This makes detection of the start of the packets more reliable.

Start Write XBee API packet to serial

Create Xbee API general header with

length of buffer, and command ID

Put general header at the beginning of the

buffer and the checksum at the end

Write 0x7E to serial handle

Does the current byte in the buffer need escaping

Write 0x7d, and write the current byte in the buffer XORed with 0x20 to

the serial handle Is there data to be written in the buffer

Write the current byte in the buffer to

the serial handle

Select the next byte in the buffer End Yes No No Yes

Figure 120 Flowchart for writing an XBee API packet to the serial port