framer-802154.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "net/mac/framer-802154.h"
00041 #include "net/mac/frame802154.h"
00042 #include "net/packetbuf.h"
00043 #include "lib/random.h"
00044 #include <string.h>
00045
00046 #define DEBUG 0
00047
00048 #if DEBUG
00049 #include <stdio.h>
00050 #define PRINTF(...) printf(__VA_ARGS__)
00051 #define PRINTADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7])
00052 #else
00053 #define PRINTF(...)
00054 #define PRINTADDR(addr)
00055 #endif
00056
00057 static uint8_t mac_dsn;
00058 static uint8_t initialized = 0;
00059 static const uint16_t mac_dst_pan_id = IEEE802154_PANID;
00060 static const uint16_t mac_src_pan_id = IEEE802154_PANID;
00061
00062
00063 static int
00064 is_broadcast_addr(uint8_t mode, uint8_t *addr)
00065 {
00066 int i = mode == FRAME802154_SHORTADDRMODE ? 2 : 8;
00067 while(i-- > 0) {
00068 if(addr[i] != 0xff) {
00069 return 0;
00070 }
00071 }
00072 return 1;
00073 }
00074
00075 static int
00076 create(void)
00077 {
00078 frame802154_t params;
00079 uint8_t len;
00080
00081
00082 memset(¶ms, 0, sizeof(params));
00083
00084 if(!initialized) {
00085 initialized = 1;
00086 mac_dsn = random_rand() & 0xff;
00087 }
00088
00089
00090 params.fcf.frame_type = FRAME802154_DATAFRAME;
00091 params.fcf.security_enabled = 0;
00092 params.fcf.frame_pending = packetbuf_attr(PACKETBUF_ATTR_PENDING);
00093 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
00094 params.fcf.ack_required = 0;
00095 } else {
00096 params.fcf.ack_required = packetbuf_attr(PACKETBUF_ATTR_MAC_ACK);
00097 }
00098 params.fcf.panid_compression = 0;
00099
00100
00101 params.fcf.frame_version = FRAME802154_IEEE802154_2003;
00102
00103
00104 if(packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO)) {
00105 params.seq = packetbuf_attr(PACKETBUF_ATTR_MAC_SEQNO);
00106 } else {
00107 params.seq = mac_dsn++;
00108 packetbuf_set_attr(PACKETBUF_ATTR_MAC_SEQNO, params.seq);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117 if(sizeof(rimeaddr_t) == 2) {
00118
00119 params.fcf.src_addr_mode = FRAME802154_SHORTADDRMODE;
00120 } else {
00121 params.fcf.src_addr_mode = FRAME802154_LONGADDRMODE;
00122 }
00123 params.dest_pid = mac_dst_pan_id;
00124
00125
00126
00127
00128
00129 if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_RECEIVER), &rimeaddr_null)) {
00130
00131 params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
00132 params.dest_addr[0] = 0xFF;
00133 params.dest_addr[1] = 0xFF;
00134
00135 } else {
00136 rimeaddr_copy((rimeaddr_t *)¶ms.dest_addr,
00137 packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
00138
00139 if(sizeof(rimeaddr_t) == 2) {
00140 params.fcf.dest_addr_mode = FRAME802154_SHORTADDRMODE;
00141 } else {
00142 params.fcf.dest_addr_mode = FRAME802154_LONGADDRMODE;
00143 }
00144 }
00145
00146
00147 params.src_pid = mac_src_pan_id;
00148
00149
00150
00151
00152
00153 rimeaddr_copy((rimeaddr_t *)¶ms.src_addr, &rimeaddr_node_addr);
00154
00155 params.payload = packetbuf_dataptr();
00156 params.payload_len = packetbuf_datalen();
00157 len = frame802154_hdrlen(¶ms);
00158 if(packetbuf_hdralloc(len)) {
00159 frame802154_create(¶ms, packetbuf_hdrptr(), len);
00160
00161 PRINTF("15.4-OUT: %2X", params.fcf.frame_type);
00162 PRINTADDR(params.dest_addr.u8);
00163 PRINTF("%u %u (%u)\n", len, packetbuf_datalen(), packetbuf_totlen());
00164
00165 return len;
00166 } else {
00167 PRINTF("15.4-OUT: too large header: %u\n", len);
00168 return 0;
00169 }
00170 }
00171
00172 static int
00173 parse(void)
00174 {
00175 frame802154_t frame;
00176 int len;
00177 len = packetbuf_datalen();
00178 if(frame802154_parse(packetbuf_dataptr(), len, &frame) &&
00179 packetbuf_hdrreduce(len - frame.payload_len)) {
00180 if(frame.fcf.dest_addr_mode) {
00181 if(frame.dest_pid != mac_src_pan_id &&
00182 frame.dest_pid != FRAME802154_BROADCASTPANDID) {
00183
00184 PRINTF("15.4: for another pan %u\n", frame.dest_pid);
00185 return 0;
00186 }
00187 if(!is_broadcast_addr(frame.fcf.dest_addr_mode, frame.dest_addr)) {
00188 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (rimeaddr_t *)&frame.dest_addr);
00189 }
00190 }
00191 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (rimeaddr_t *)&frame.src_addr);
00192 packetbuf_set_attr(PACKETBUF_ATTR_PENDING, frame.fcf.frame_pending);
00193
00194 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, frame.seq);
00195
00196 PRINTF("15.4-IN: %2X", frame.fcf.frame_type);
00197 PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_SENDER));
00198 PRINTADDR(packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
00199 PRINTF("%u (%u)\n", packetbuf_datalen(), len);
00200
00201 return len - frame.payload_len;
00202 }
00203 return 0;
00204 }
00205
00206 const struct framer framer_802154 = {
00207 create, parse
00208 };