frame.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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #if defined( __GNUC__ )
00066 #include <avr/io.h>
00067 #include <util/delay.h>
00068 #else
00069 #include <iom1284.h>
00070 #endif
00071
00072 #include <stdlib.h>
00073 #include <string.h>
00074 #include <stdint.h>
00075 #include <stdbool.h>
00076 #include "at86rf230_registermap.h"
00077 #include "radio.h"
00078 #include "frame.h"
00079
00080 #include "zmac.h"
00081 #include "process.h"
00082 #include "sicslowmac.h"
00083
00084
00085
00086
00087
00088
00089 #ifndef AUTO_CRC_PADDING
00090 #define AUTO_CRC_PADDING 0
00091 #endif
00092
00093
00094
00095
00096
00097
00098
00099
00100 static uint8_t tx_frame_buffer[130];
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 void
00120 frame_tx_create(frame_create_params_t *p,frame_result_t *frame_result)
00121 {
00122 field_length_t flen;
00123 uint8_t index=0;
00124
00125
00126 memset(&flen, 0, sizeof(field_length_t));
00127
00128
00129 if (p->fcf.destAddrMode){
00130 flen.dest_pid_len = 2;
00131 }
00132 if (p->fcf.srcAddrMode){
00133 flen.src_pid_len = 2;
00134 }
00135
00136 if(p->fcf.destAddrMode == p->fcf.srcAddrMode){
00137 p->fcf.panIdCompression = 1;
00138 }
00139 if (p->fcf.panIdCompression){
00140
00141 flen.src_pid_len = 0;
00142 }
00143
00144 switch (p->fcf.destAddrMode){
00145 case 2:
00146 flen.dest_addr_len = 2;
00147 break;
00148 case 3:
00149 flen.dest_addr_len = 8;
00150 break;
00151 default:
00152 break;
00153 }
00154 switch (p->fcf.srcAddrMode){
00155 case 2:
00156 flen.src_addr_len = 2;
00157 break;
00158 case 3:
00159 flen.src_addr_len = 8;
00160 break;
00161 default:
00162 break;
00163 }
00164
00165 if (p->fcf.securityEnabled){
00166 switch (p->aux_hdr.security_control.key_id_mode){
00167 case 0:
00168 flen.aux_sec_len = 5;
00169 break;
00170 case 1:
00171 flen.aux_sec_len = 6;
00172 break;
00173 case 2:
00174 flen.aux_sec_len = 10;
00175 break;
00176 case 3:
00177 flen.aux_sec_len = 14;
00178 break;
00179 default:
00180 break;
00181 }
00182 }
00183
00184
00185
00186 *(uint16_t *)tx_frame_buffer = p->fcf.word_val;
00187 index = 2;
00188 tx_frame_buffer[index++] = p->seq;
00189
00190 if (flen.dest_pid_len == 2){
00191 *(uint16_t *)&tx_frame_buffer[index] = p->dest_pid;
00192 index += 2;
00193 }
00194
00195 switch (flen.dest_addr_len){
00196 case 2:
00197 *(uint16_t *)&tx_frame_buffer[index] = p->dest_addr.addr16;
00198 index += 2;
00199 break;
00200 case 8:
00201 *(uint64_t *)&tx_frame_buffer[index] = p->dest_addr.addr64;
00202 index += 8;
00203 break;
00204 case 0:
00205 default:
00206 break;
00207 }
00208
00209 if (flen.src_pid_len == 2){
00210 *(uint16_t *)&tx_frame_buffer[index] = p->src_pid;
00211 index += 2;
00212 }
00213
00214 switch (flen.src_addr_len){
00215 case 2:
00216 *(uint16_t *)&tx_frame_buffer[index] = p->src_addr.addr16;
00217 index += 2;
00218 break;
00219 case 8:
00220 *(uint64_t *)&tx_frame_buffer[index] = p->src_addr.addr64;
00221 index += 8;
00222 break;
00223 case 0:
00224 default:
00225 break;
00226 }
00227
00228 if (flen.aux_sec_len){
00229 memcpy((char *)&tx_frame_buffer[index],
00230 (char *)&p->aux_hdr,
00231 flen.aux_sec_len);
00232 index += flen.aux_sec_len;
00233 }
00234
00235 memcpy((char *)&tx_frame_buffer[index],
00236 (char *)p->payload,
00237 p->payload_len);
00238 index += p->payload_len;
00239
00240
00241 frame_result->length = index + AUTO_CRC_PADDING;
00242 frame_result->frame = tx_frame_buffer;
00243 return;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 void rx_frame_parse(hal_rx_frame_t *rx_frame, parsed_frame_t *pf)
00257 {
00258
00259 uint8_t *p = rx_frame->data;
00260 fcf_t *fcf = (fcf_t *)&rx_frame->data;
00261 static uint8_t frame_dropped = 0;
00262
00263
00264 if (pf->in_use) {
00265
00266
00267 if (!frame_dropped) {
00268 event_object_t event;
00269 event.event = MAC_EVENT_DROPPED;
00270 event.data = NULL;
00271 mac_put_event(&event);
00272 process_post(&mac_process, event.event, event.data);
00273 }
00274 frame_dropped = 1;
00275 return;
00276 }
00277
00278
00279 pf->fcf = (fcf_t *)p;
00280 pf->seqNum = p+2;
00281 p += 3;
00282
00283 if (fcf->frameType == ACKFRAME) {
00284
00285 pf->payload_length = 0;
00286
00287 } else {
00288
00289
00290 if (fcf->frameType != BEACONFRAME){
00291 pf->dest_pid = (uint16_t *)p;
00292 p += 2;
00293
00294 pf->dest_addr = 0;
00295 if (fcf->destAddrMode == SHORTADDRMODE ||
00296 fcf->destAddrMode == LONGADDRMODE){
00297 pf->dest_addr = (addr_t *)p;
00298
00299 if (fcf->destAddrMode == SHORTADDRMODE){
00300 p += 2;
00301 }
00302 if (fcf->destAddrMode == LONGADDRMODE){
00303 p += 8;
00304 }
00305 }
00306 }
00307
00308 pf->src_pid = 0;
00309 if (!fcf->panIdCompression){
00310 pf->src_pid = (uint16_t *)p;
00311 p += 2;
00312 } else {
00313 pf->src_pid = pf->dest_pid;
00314 }
00315
00316 pf->src_addr = (addr_t *)p;
00317 if (fcf->srcAddrMode == SHORTADDRMODE){
00318 p += 2;
00319 }
00320 if (fcf->srcAddrMode == LONGADDRMODE){
00321 p += 8;
00322 }
00323
00324 pf->aux_sec_hdr = 0;
00325
00326 pf->payload_length = rx_frame->length - (p - (uint8_t*)&rx_frame->data) - 2;
00327
00328 pf->payload = p;
00329 }
00330
00331 pf->lqi = rx_frame->lqi;
00332 pf->fcs = rx_frame->crc;
00333
00334
00335 event_object_t event;
00336 event.event = MAC_EVENT_RX;
00337 event.data = (uint8_t*)pf;
00338 pf->in_use = 1;
00339 mac_put_event(&event);
00340 process_poll(&mac_process);
00341 }
00342
00343
00344