51 #include <avr/eeprom.h>
52 #include <util/delay.h>
68 #define PRINTF(...) printf(__VA_ARGS__)
69 #define SICSLOW_CORRECTION_DELAY 70
72 #define SICSLOW_CORRECTION_DELAY 7
76 #include "sicslow_ethernet.h"
77 #define LOG_FRAME(x,y) mac_logTXtoEthernet(x,y)
79 #define LOG_FRAME(x,y)
84 static struct mac_driver *pmac_driver = &mac_driver_struct;
86 static parsed_frame_t *parsed_frame;
103 event_object_t event_object[MAX_EVENTS];
107 static void setinput(
void (*r)(
const struct mac_driver *d));
109 void sicslowmac_unknownIndication(
void);
112 void (*sicslowmac_snifferhook)(
const struct mac_driver *r) =
NULL;
124 return (event_queue.head != event_queue.tail);
137 if ((event_queue.head + 1) % MAX_EVENTS == event_queue.tail){
142 newhead = event_queue.head;
145 event_queue.event_object[newhead] = *object;
149 if (newhead >= MAX_EVENTS){
152 event_queue.head = newhead;
164 event_object_t *
object =
NULL;
165 volatile uint8_t newtail;
167 newtail = event_queue.tail;
169 object = &(event_queue.event_object[newtail]);
173 if (newtail >= MAX_EVENTS){
177 event_queue.tail = newtail;
182 void mac_pollhandler(
void)
196 event_object_t *event;
205 if (event->event == MAC_EVENT_RX){
207 parsed_frame = (parsed_frame_t *)event->data;
208 if (parsed_frame->fcf->frameType == DATAFRAME){
209 sicslowmac_dataIndication();
213 sicslowmac_unknownIndication();
218 parsed_frame->in_use =
false;
221 if (event->event == MAC_EVENT_DROPPED){
223 PRINTF(
"sicslowmac: Frame Dropped!\n");
230 setinput(
void (*r)(
const struct mac_driver *d))
235 static uint8_t dest_reversed[UIP_LLADDR_LEN];
236 static uint8_t src_reversed[UIP_LLADDR_LEN];
238 # define MSB(u16) (((uint8_t* )&u16)[1])
239 # define LSB(u16) (((uint8_t* )&u16)[0])
242 sicslowmac_dataIndication(
void)
247 #if UIP_LLADDR_LEN == 8
252 memcpy(dest_reversed, (uint8_t *)parsed_frame->dest_addr, UIP_LLADDR_LEN);
253 memcpy(src_reversed, (uint8_t *)parsed_frame->src_addr, UIP_LLADDR_LEN);
256 byte_reverse((uint8_t *)dest_reversed, UIP_LLADDR_LEN);
257 byte_reverse((uint8_t *)src_reversed, UIP_LLADDR_LEN);
259 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const linkaddr_t *)dest_reversed);
260 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const linkaddr_t *)src_reversed);
262 #elif UIP_CONF_USE_RUM
264 packetbuf_copyfrom(parsed_frame->payload + UIP_DATA_RUM_OFFSET, parsed_frame->payload_length - UIP_DATA_RUM_OFFSET);
267 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
268 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
269 dest_reversed[2] = 0;
270 dest_reversed[3] = 0;
271 dest_reversed[4] =
MSB(parsed_frame->payload[0]);
272 dest_reversed[5] =
LSB(parsed_frame->payload[1]);
274 src_reversed[0] =
MSB(parsed_frame->src_pid);
275 src_reversed[1] =
LSB(parsed_frame->src_pid);
278 src_reversed[4] =
MSB(parsed_frame->payload[2]);
279 src_reversed[5] =
LSB(parsed_frame->payload[3]);
286 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
287 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
288 dest_reversed[2] = 0;
289 dest_reversed[3] = 0;
290 dest_reversed[4] =
MSB(parsed_frame->dest_addr->addr16);
291 dest_reversed[5] =
LSB(parsed_frame->dest_addr->addr16);
293 src_reversed[0] =
MSB(parsed_frame->src_pid);
294 src_reversed[1] =
LSB(parsed_frame->src_pid);
297 src_reversed[4] =
MSB(parsed_frame->src_addr->addr16);
298 src_reversed[5] =
LSB(parsed_frame->src_addr->addr16);
300 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const linkaddr_t *)dest_reversed);
301 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const linkaddr_t *)src_reversed);
305 PRINTF(
"sicslowmac: hand off frame to sicslowpan \n");
310 sicslowmac_unknownIndication(
void)
312 if (sicslowmac_snifferhook) {
320 #if UIP_LLADDR_LEN == 8
321 memcpy(dest_reversed, (uint8_t *)parsed_frame->dest_addr, UIP_LLADDR_LEN);
322 memcpy(src_reversed, (uint8_t *)parsed_frame->src_addr, UIP_LLADDR_LEN);
325 byte_reverse((uint8_t *)dest_reversed, UIP_LLADDR_LEN);
326 byte_reverse((uint8_t *)src_reversed, UIP_LLADDR_LEN);
328 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const linkaddr_t *)dest_reversed);
329 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const linkaddr_t *)src_reversed);
331 #elif UIP_CONF_USE_RUM
333 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
334 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
335 dest_reversed[2] = 0;
336 dest_reversed[3] = 0;
337 dest_reversed[4] =
MSB(parsed_frame->payload[0]);
338 dest_reversed[5] =
LSB(parsed_frame->payload[1]);
340 src_reversed[0] =
MSB(parsed_frame->src_pid);
341 src_reversed[1] =
LSB(parsed_frame->src_pid);
344 src_reversed[4] =
MSB(parsed_frame->payload[2]);
345 src_reversed[5] =
LSB(parsed_frame->payload[3]);
349 dest_reversed[0] =
MSB(parsed_frame->dest_pid);
350 dest_reversed[1] =
LSB(parsed_frame->dest_pid);
351 dest_reversed[2] = 0;
352 dest_reversed[3] = 0;
353 dest_reversed[4] =
MSB(parsed_frame->dest_addr->addr16);
354 dest_reversed[5] =
LSB(parsed_frame->dest_addr->addr16);
356 src_reversed[0] =
MSB(parsed_frame->src_pid);
357 src_reversed[1] =
LSB(parsed_frame->src_pid);
360 src_reversed[4] =
MSB(parsed_frame->src_addr->addr16);
361 src_reversed[5] =
LSB(parsed_frame->src_addr->addr16);
363 packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, (
const linkaddr_t *)dest_reversed);
364 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const linkaddr_t *)src_reversed);
368 PRINTF(
"sicslowmac: hand off frame to sniffer \n");
370 sicslowmac_snifferhook(pmac_driver);
399 _delay_ms(SICSLOW_CORRECTION_DELAY);
406 msduHandle = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
442 memcpy(¶ms.
dest_addr, (uint8_t *)packetbuf_addr(PACKETBUF_ADDR_RECEIVER), LONG_ADDR_LEN);
469 LOG_FRAME(¶ms, &result);
472 uint8_t retry_count = 3;
476 PRINTF(
"sicslowmac: sending packet of length %d to radio, result:", result.
length);
484 PRINTF(
" Success\n");
495 PRINTF(
" Radio busy, retrying\n");
501 if (retry_count == 3) {
503 }
else if (retry_count == 2) {
505 }
else if (retry_count == 1) {
512 PRINTF(
"sicslowmac: Unable to send packet, dropped\n");
545 return &sicslowmac_driver;
571 eeprom_read_block ((
void *)&
macLongAddr, EEPROMMACADDRESS, 8);
580 parsed_frame_t * sicslowmac_get_frame(
void)
586 struct mac_driver * sicslowmac_get_driver(
void)
591 PROCESS(mac_process,
"802.15.4 MAC process");
608 printf(
"Radio init successful.\n");
610 printf(
"Radio init failed with return: %d\n", return_value);
614 uint8_t eeprom_channel;
615 uint8_t eeprom_check;
617 eeprom_channel = eeprom_read_byte((uint8_t *)9);
618 eeprom_check = eeprom_read_byte((uint8_t *)10);
620 if ((eeprom_channel < 11) || (eeprom_channel > 26) || ((uint8_t)eeprom_channel != (uint8_t)~eeprom_check)) {
635 pmac_driver->set_receive_function = setinput;
637 sicslowpan_init(pmac_driver);
652 void byte_reverse(uint8_t * bytes, uint8_t num)
bool framePending
True if sender has more data to send.
uint16_t macCoordShortAddress
The 16-bit short address assigned to the coordinator through which the network layer wishes to commun...
bool securityEnabled
True if security is used in this frame.
void sicslowmac_resetRequest(bool setDefaultPIB)
This is the implementation of the 15.4 MAC Reset Request primitive.
uint8_t length
Length (in bytes) of created frame.
int(* get_src_panid)(void)
Get the Source PAN_ID.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
radio_status_t radio_send_data(uint8_t data_length, uint8_t *data)
This function will download a frame to the radio transceiver's transmit buffer and send it...
int sicslowmac_dataRequest(void)
This is the implementation of the 15.4 MAC Data Request primitive.
Example glue code between the existing MAC code and the Contiki mac interface ...
#define RX_AACK_ON
Constant RX_AACK_ON for sub-register SR_TRX_STATUS.
#define PROCESS_BEGIN()
Define the beginning of a process.
uint16_t src_pid
Source PAN ID.
const linkaddr_t linkaddr_null
The null Rime address.
Header for the Contiki/uIP interface.
uint8_t seq
Sequence number.
uint8_t payload_len
Length of payload field.
Example glue code between the existing MAC code and the Contiki mac interface.
uint16_t macSrcPANId
The 16-bit identifier of the PAN on which the device is operating.
int(* get_dst_panid)(void)
Get the Destination PAN_ID.
uint8_t destAddrMode
Destination address mode, see 802.15.4.
uint64_t(* get_long_addr)(void)
Get the Long Address.
#define AVR_ENTER_CRITICAL_REGION()
This macro will protect the following code from interrupts.
#define LSB(u16)
Least significant byte of u16.
uint8_t mac_event_pending(void)
Checks for any pending events in the queue.
bool panIdCompression
Is this a compressed header?
The interface structure for the 802.15.4 quasi-MAC.
#define AVR_LEAVE_CRITICAL_REGION()
This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION so that interrupts are e...
Header file for the Rime buffer (packetbuf) management
void ieee_15_4_init(ieee_15_4_manager_t *pieee_15_4_manager)
initializes the 802.15.4 manager layer.
#define TRX_OFF
Constant TRX_OFF for sub-register SR_TRX_STATUS.
void radio_use_auto_tx_crc(bool auto_crc_on)
This function will enable or disable automatic CRC during frame transmission.
void frame_tx_create(frame_create_params_t *p, frame_result_t *frame_result)
Creates a frame for transmission over the air.
#define NULL
The null pointer.
The structure of a device driver for a radio in Contiki.
void(* send)(mac_callback_t sent_callback, void *ptr)
Send a packet from the Rime buffer.
event_object_t * mac_get_event(void)
Pulls an event from the event queue.
uint64_t macLongAddr
Our own long address.
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
uint8_t macDSN
The sequence number (0x00 - 0xff) added to the transmitted data or MAC command frame.
uint8_t * frame
Pointer to created frame.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
void mac_task(process_event_t ev, process_data_t data)
This is the main loop task for the MAC.
Strucure used to return that status of the frame create process.
addr_t src_addr
Source address.
The end-user tried to do an invalid state transition.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
uint64_t addr64
Long address.
#define PROCESS_END()
Define the end of a process.
Header file for the 6lowpan implementation (RFC4944 and draft-hui-6lowpan-hc-01) ...
uint8_t srcAddrMode
Source address mode, see 802.15.4.
uint16_t dest_pid
Destination PAN ID.
radio_status_t radio_init(bool cal_rc_osc, hal_rx_start_isr_event_handler_t rx_event, hal_trx_end_isr_event_handler_t trx_end_event, radio_rx_callback rx_callback)
Initialize the Transceiver Access Toolbox and lower layers.
uint8_t frameType
Frame type field, see 802.15.4.
ieee_15_4_manager_t ieee15_4ManagerAddress
Interface structure for this module.
radio_status_t
This enumeration defines the possible return values for the TAT API functions.
int mac_wake(void)
Stub function that will be implemented in phase 2 to cause end nodes to sleep.
uint16_t addr16
Short address.
radio_status_t radio_set_operating_channel(uint8_t channel)
This function will change the operating channel.
addr_t dest_addr
Destination address.
void mac_init(void)
Initializes the (quasi) 802.15.4 MAC.
#define PROCESS_POLLHANDLER(handler)
Specify an action when a process is polled.
The requested service was performed successfully.
Parameters used by the frame_tx_create() function.
#define PROCESS(name, strname)
Declare a process.
bool ackRequired
Is an ack frame required?
void mac_put_event(event_object_t *object)
Puts an event into the queue of events.
void packetbuf_clear(void)
Clear and reset the packetbuf.
The structure of a MAC protocol driver in Contiki.
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
#define PROCESS_YIELD()
Yield the currently running process.
The IEEE 802.15.4 (2003/2006) MAC utility functions.
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
fcf_t fcf
Frame control field.
802.15.4 frame creation and parsing functions
uint8_t * payload
Pointer to 802.15.4 frame payload.
radio_status_t radio_set_trx_state(uint8_t new_state)
This function will change the current state of the radio transceiver's internal state machine...
#define MSB(u16)
Most significant byte of u16.
uint8_t frameVersion
802.15.4 frame version
int mac_sleep(void)
Stub function that will be implemented in phase 2 to cause end nodes to sleep.
uint16_t macShortAddress
The 16-bit address that the device uses to communicate in the PAN.
uint16_t macDstPANId
The 16-bit identifier of the PAN on which the device is sending to.