1)消息缓冲区 message_t
在TinyOS 2.x 中,标准的消息缓冲区 message_t,该缓冲区定义在 tos/types/message.h中。比之前Tinyos 1.x 优化了许多。如下:
typedef nx_struct message_t {
nx_uint8_t header[sizeof(message_header_t)];
nx_uint8_t data[TOSH_DATA_LENGTH];
nx_uint8_t footer[sizeof(message_footer_t)];
nx_uint8_t metadata[sizeof(message_metadata_t)];
} message_t;
TinyOS 2.x的组件把数据包看成抽象数据类型(ADT),而不是C中的数据结构,好处在于:数据包层将不依赖于特定区域字段或位置,从而可以实现可选择数据包格式,并做出各种优化。下面一一分析message_t中的各个定义:
1 header
typedef union message_header {
cc2420_header_t cc2420;
serial_header_t serial;
} message_header_t;
typedef nx_struct cc2420_header_t {
nxle_uint8_t length;
nxle_uint16_t fcf;
nxle_uint8_t dsn;
nxle_uint16_t destpan;
nxle_uint16_t dest;
nxle_uint16_t src;
/** CC2420 802.15.4 header ends here */
#ifdef CC2420_HW_SECURITY
security_header_t secHdr;
#endif
#ifndef TFRAMES_ENABLED
/** I-Frame 6LowPAN interoperability byte */
nxle_uint8_t network;
#endif
nxle_uint8_t type;
} cc2420_header_t;
nxle_uint8_t length;
nxle_uint16_t fcf;
nxle_uint8_t dsn;
nxle_uint16_t destpan;
nxle_uint16_t dest;
nxle_uint16_t src;
/** CC2420 802.15.4 header ends here */
#ifdef CC2420_HW_SECURITY
security_header_t secHdr;
#endif
#ifndef TFRAMES_ENABLED
/** I-Frame 6LowPAN interoperability byte */
nxle_uint8_t network;
#endif
nxle_uint8_t type;
} cc2420_header_t;
2 data
message_t的数据字段存储单跳数据包有效载荷。TOSH_DATA_LENGTH,默认大小28字节。可以修改。
3 footer
message_footer_t字段确保message_t中有足够的空间来存储所有底层链路层的MTU大小。在tinyos2.x中并未有该实际字段。
/**
* CC2420 Packet Footer
*/
typedef nx_struct cc2420_footer_t {
} cc2420_footer_t;
* CC2420 Packet Footer
*/
typedef nx_struct cc2420_footer_t {
} cc2420_footer_t;
4 metadata
存储收集到的信息但不用于发送。如RSSI或者时间戳。
typedef nx_struct cc2420_metadata_t {
nx_uint8_t tx_power;
nx_uint8_t rssi;
nx_uint8_t lqi;
nx_bool crc;
nx_bool ack;
nx_uint16_t time;
} cc2420_metadata_t;
/**
* CC2420 Packet metadata. Contains extra information about the message
* that will not be transmitted.
*
* Note that the first two bytes automatically take in the values of the
* FCS when the payload is full. Do not modify the first two bytes of metadata.
*/
typedef nx_struct cc2420_metadata_t {
nx_uint8_t rssi;
nx_uint8_t lqi;
nx_uint8_t tx_power;
nx_bool crc;
nx_bool ack;
nx_bool timesync;
nx_uint32_t timestamp;
nx_uint16_t rxInterval;
/** Packet Link Metadata */
#ifdef PACKET_LINK
nx_uint16_t maxRetries;
nx_uint16_t retryDelay;
#endif
} cc2420_metadata_t;
* CC2420 Packet metadata. Contains extra information about the message
* that will not be transmitted.
*
* Note that the first two bytes automatically take in the values of the
* FCS when the payload is full. Do not modify the first two bytes of metadata.
*/
typedef nx_struct cc2420_metadata_t {
nx_uint8_t rssi;
nx_uint8_t lqi;
nx_uint8_t tx_power;
nx_bool crc;
nx_bool ack;
nx_bool timesync;
nx_uint32_t timestamp;
nx_uint16_t rxInterval;
/** Packet Link Metadata */
#ifdef PACKET_LINK
nx_uint16_t maxRetries;
nx_uint16_t retryDelay;
#endif
} cc2420_metadata_t;
2)CTP数据包格式
1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|P|C| reserved | THL |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ETX |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| origin |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| seqno | collect_id |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- P: Routing pull. P位允许节点从其它节点请求路由信息。如果节点收到一个P位置位的包,它应当传输一个路由帧。
- C: Congestion notification. 拥塞标志位。如果节点丢弃了一个CTP数据帧,它必须在下一个传输的数据帧中置C位。
- THL: Time Has Lived. 已存活时间。当节点产生一个CTP数据帧时,它必须设THL为0。当节点接收到一个CTP数据帧时,它必须增加THL值。如果节点接收到的数据包THL为255,则将它回绕为0
- ETX: The ETX routing metric of the single-hop sender. 单跳发送者的ETX值。当节点发送一个CTP数据帧时,它必须将到单跳目的地的路由ETX值填入ETX字段。如果节点接收到的路由梯度比自己的小,则它必须准备发送一个路由帧。
- origin: The originating address of the packet. 包的源地址。转发的节点不可修改这个字段
- seqno: Origin sequence number. 源顺序号。源节点设置了这个字段,转发节点不可修改它。
- collect_id: Higher-level protocol identifier. 高层协议标识。源节点设置了这个字段,转发节点不可修改它。
- data: the data payload数据负载。0个或多个字节。转发节点不可修改这个字段。
Tinyos 2.x实现在net/ctp中。
typedef nx_struct {nx_ctp_options_t options;
nx_uint8_t thl;
nx_uint16_t etx;
nx_am_addr_t origin;
nx_uint8_t originSeqNo;
nx_collection_id_t type;
nx_uint8_t (COUNT(0) data)[0]; // Deputy place-holder, field will probably be removed when we Deputize Ctp
} ctp_data_header_t;