52 #include <avr/eeprom.h>
53 #include <util/delay.h>
69 #define PRINTF(...) printf(__VA_ARGS__)
70 #define SICSLOW_CORRECTION_DELAY 70
73 #define SICSLOW_CORRECTION_DELAY 7
77 #include "sicslow_ethernet.h"
78 #define LOG_FRAME(x,y) mac_logTXtoEthernet(x,y)
80 #define LOG_FRAME(x,y)
85 static struct mac_driver *pmac_driver = &mac_driver_struct;
87 static parsed_frame_t *parsed_frame;
104 event_object_t event_object[MAX_EVENTS];
108 static void setinput(
void (*r)(
const struct mac_driver *d));
110 void sicslowmac_unknownIndication(
void);
113 void (*sicslowmac_snifferhook)(
const struct mac_driver *r) =
NULL;
125 return (event_queue.head != event_queue.tail);
138 if ((event_queue.head + 1) % MAX_EVENTS == event_queue.tail){
143 newhead = event_queue.head;
146 event_queue.event_object[newhead] = *object;
150 if (newhead >= MAX_EVENTS){
153 event_queue.head = newhead;
165 event_object_t *
object =
NULL;
166 volatile uint8_t newtail;
168 newtail = event_queue.tail;
170 object = &(event_queue.event_object[newtail]);
174 if (newtail >= MAX_EVENTS){
178 event_queue.tail = newtail;
183 void mac_pollhandler(
void)
197 event_object_t *event;
206 if (event->event == MAC_EVENT_RX){
208 parsed_frame = (parsed_frame_t *)event->data;
209 if (parsed_frame->fcf->frameType == DATAFRAME){
210 sicslowmac_dataIndication();
214 sicslowmac_unknownIndication();
219 parsed_frame->in_use =
false;
222 if (event->event == MAC_EVENT_DROPPED){
224 PRINTF(
"sicslowmac: Frame Dropped!\n");
231 setinput(
void (*r)(
const struct mac_driver *d))
236 static uint8_t dest_reversed[UIP_LLADDR_LEN];
237 static uint8_t src_reversed[UIP_LLADDR_LEN];
239 # define MSB(u16) (((uint8_t* )&u16)[1])
240 # define LSB(u16) (((uint8_t* )&u16)[0])
243 sicslowmac_dataIndication(
void)
248 #if UIP_LLADDR_LEN == 8
253 memcpy(dest_reversed, (uint8_t *)parsed_frame->dest_addr, UIP_LLADDR_LEN);
254 memcpy(src_reversed, (uint8_t *)parsed_frame->src_addr, UIP_LLADDR_LEN);
257 byte_reverse((uint8_t *)dest_reversed, UIP_LLADDR_LEN);
258 byte_reverse((uint8_t *)src_reversed, UIP_LLADDR_LEN);
260 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const rimeaddr_t *)dest_reversed);
261 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const rimeaddr_t *)src_reversed);
263 #elif UIP_CONF_USE_RUM
265 packetbuf_copyfrom(parsed_frame->payload + UIP_DATA_RUM_OFFSET, parsed_frame->payload_length - UIP_DATA_RUM_OFFSET);
268 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
269 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
270 dest_reversed[2] = 0;
271 dest_reversed[3] = 0;
272 dest_reversed[4] =
MSB(parsed_frame->payload[0]);
273 dest_reversed[5] =
LSB(parsed_frame->payload[1]);
275 src_reversed[0] =
MSB(parsed_frame->src_pid);
276 src_reversed[1] =
LSB(parsed_frame->src_pid);
279 src_reversed[4] =
MSB(parsed_frame->payload[2]);
280 src_reversed[5] =
LSB(parsed_frame->payload[3]);
287 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
288 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
289 dest_reversed[2] = 0;
290 dest_reversed[3] = 0;
291 dest_reversed[4] =
MSB(parsed_frame->dest_addr->addr16);
292 dest_reversed[5] =
LSB(parsed_frame->dest_addr->addr16);
294 src_reversed[0] =
MSB(parsed_frame->src_pid);
295 src_reversed[1] =
LSB(parsed_frame->src_pid);
298 src_reversed[4] =
MSB(parsed_frame->src_addr->addr16);
299 src_reversed[5] =
LSB(parsed_frame->src_addr->addr16);
301 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const rimeaddr_t *)dest_reversed);
302 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const rimeaddr_t *)src_reversed);
306 PRINTF(
"sicslowmac: hand off frame to sicslowpan \n");
311 sicslowmac_unknownIndication(
void)
313 if (sicslowmac_snifferhook) {
321 #if UIP_LLADDR_LEN == 8
322 memcpy(dest_reversed, (uint8_t *)parsed_frame->dest_addr, UIP_LLADDR_LEN);
323 memcpy(src_reversed, (uint8_t *)parsed_frame->src_addr, UIP_LLADDR_LEN);
326 byte_reverse((uint8_t *)dest_reversed, UIP_LLADDR_LEN);
327 byte_reverse((uint8_t *)src_reversed, UIP_LLADDR_LEN);
329 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const rimeaddr_t *)dest_reversed);
330 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const rimeaddr_t *)src_reversed);
332 #elif UIP_CONF_USE_RUM
334 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
335 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
336 dest_reversed[2] = 0;
337 dest_reversed[3] = 0;
338 dest_reversed[4] =
MSB(parsed_frame->payload[0]);
339 dest_reversed[5] =
LSB(parsed_frame->payload[1]);
341 src_reversed[0] =
MSB(parsed_frame->src_pid);
342 src_reversed[1] =
LSB(parsed_frame->src_pid);
345 src_reversed[4] =
MSB(parsed_frame->payload[2]);
346 src_reversed[5] =
LSB(parsed_frame->payload[3]);
350 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
351 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
352 dest_reversed[2] = 0;
353 dest_reversed[3] = 0;
354 dest_reversed[4] =
MSB(parsed_frame->dest_addr->addr16);
355 dest_reversed[5] =
LSB(parsed_frame->dest_addr->addr16);
357 src_reversed[0] =
MSB(parsed_frame->src_pid);
358 src_reversed[1] =
LSB(parsed_frame->src_pid);
361 src_reversed[4] =
MSB(parsed_frame->src_addr->addr16);
362 src_reversed[5] =
LSB(parsed_frame->src_addr->addr16);
364 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const rimeaddr_t *)dest_reversed);
365 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const rimeaddr_t *)src_reversed);
369 PRINTF(
"sicslowmac: hand off frame to sniffer \n");
371 sicslowmac_snifferhook(pmac_driver);
400 _delay_ms(SICSLOW_CORRECTION_DELAY);
407 msduHandle = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
443 memcpy(¶ms.
dest_addr, (uint8_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER), LONG_ADDR_LEN);
470 LOG_FRAME(¶ms, &result);
473 uint8_t retry_count = 3;
477 PRINTF(
"sicslowmac: sending packet of length %d to radio, result:", result.
length);
485 PRINTF(
" Success\n");
496 PRINTF(
" Radio busy, retrying\n");
502 if (retry_count == 3) {
504 }
else if (retry_count == 2) {
506 }
else if (retry_count == 1) {
513 PRINTF(
"sicslowmac: Unable to send packet, dropped\n");
546 return &sicslowmac_driver;
572 eeprom_read_block ((
void *)&
macLongAddr, EEPROMMACADDRESS, 8);
581 parsed_frame_t * sicslowmac_get_frame(
void)
587 struct mac_driver * sicslowmac_get_driver(
void)
592 PROCESS(mac_process,
"802.15.4 MAC process");
609 printf(
"Radio init successful.\n");
611 printf(
"Radio init failed with return: %d\n", return_value);
615 uint8_t eeprom_channel;
616 uint8_t eeprom_check;
618 eeprom_channel = eeprom_read_byte((uint8_t *)9);
619 eeprom_check = eeprom_read_byte((uint8_t *)10);
621 if ((eeprom_channel < 11) || (eeprom_channel > 26) || ((uint8_t)eeprom_channel != (uint8_t)~eeprom_check)) {
636 pmac_driver->set_receive_function = setinput;
638 sicslowpan_init(pmac_driver);
653 void byte_reverse(uint8_t * bytes, uint8_t num)