33 #include "radio-uip-uaodv.h"
35 #include "net/uaodv.h"
44 #define MAX_BUFFERED_PACKETS 10
45 #define MAX_RETRANSMISSIONS_RREP 16
46 #define MAX_RETRANSMISSIONS_UNICAST 16
50 #define FWD_ID_LENGTH 4
51 #define FWD_NEXT_IP FWD_ID_LENGTH
52 #define FWD_PACKET_LENGTH (FWD_NEXT_IP + 4)
56 #define ACK_ID_LENGTH 3
57 #define ACK_CRC ACK_ID_LENGTH
58 #define ACK_PACKET_LENGTH (ACK_ID_LENGTH + 2)
59 #define ACK_TIMEOUT (CLOCK_SECOND / 50) * (random_rand() % 100)
66 struct buf_packet *next;
73 uip_ipaddr_t finaldest;
77 LIST(buf_packet_list);
78 MEMB(buf_packet_mem,
struct buf_packet, MAX_BUFFERED_PACKETS);
80 PROCESS(radio_uip_process,
"radio uIP uAODV process");
88 uint8_t radio_uip_uaodv_send(
void);
90 int radio_uip_handle_ack(uint8_t *buf,
int len);
91 uint16_t radio_uip_calc_crc(uint8_t *buf,
int len);
92 int radio_uip_buffer_outgoing_packet(uint8_t *buf,
int len, uip_ipaddr_t *dest,
int max_sends);
93 int radio_uip_is_ack(uint8_t *buf,
int len);
94 int radio_uip_uaodv_add_header(uint8_t *buf,
int len, uip_ipaddr_t *addr);
95 int radio_uip_uaodv_remove_header(uint8_t *buf,
int len);
96 void radio_uip_uaodv_change_header(uint8_t *buf,
int len, uip_ipaddr_t *addr);
97 int radio_uip_uaodv_header_exists(uint8_t *buf,
int len);
98 int radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr);
99 int radio_uip_uaodv_fwd_is_broadcast(uint8_t *buf,
int len);
100 int radio_uip_uaodv_fwd_is_me(uint8_t *buf,
int len);
101 int radio_uip_uaodv_dest_is_me(uint8_t *buf,
int len);
102 int radio_uip_uaodv_dest_port(uint8_t *buf,
int len);
108 struct buf_packet *packet;
115 if(ev == EVENT_SEND_ACK) {
118 uint8_t ackPacket[ACK_PACKET_LENGTH];
119 memcpy(ackPacket, ACK_ID, ACK_ID_LENGTH);
120 ackPacket[ACK_CRC] = ((uint16_t) data >> 8);
121 ackPacket[ACK_CRC+1] = ((uint16_t) data & 0xff);
122 radio->
send(ackPacket, ACK_PACKET_LENGTH);
124 }
else if(ev == PROCESS_EVENT_TIMER) {
128 packet = packet->next) {
136 }
else if (packet->resends > 0) {
141 radio->
send(packet->data, packet->len);
147 if (packet->want_ack && !uip_ipaddr_cmp(&packet->finaldest, &uip_broadcast_addr)) {
148 uaodv_bad_dest(&packet->finaldest);
170 if (radio_uip_is_ack(&uip_buf[UIP_LLH_LEN],
uip_len)) {
171 radio_uip_handle_ack(&uip_buf[UIP_LLH_LEN],
uip_len);
176 if (!radio_uip_uaodv_header_exists(&uip_buf[UIP_LLH_LEN],
uip_len)) {
182 if (!radio_uip_uaodv_fwd_is_me(&uip_buf[UIP_LLH_LEN],
uip_len)) {
189 crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN],
uip_len);
190 process_post(&radio_uip_process, EVENT_SEND_ACK, (
void*) (uint32_t) crc);
194 uip_len = radio_uip_uaodv_remove_header(&uip_buf[UIP_LLH_LEN],
uip_len);
199 radio_uip_uaodv_send(
void)
201 struct uaodv_rt_entry *route;
204 if (radio_uip_uaodv_is_broadcast(&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr)) {
205 return radio_uip_buffer_outgoing_packet(&uip_buf[UIP_LLH_LEN],
uip_len, (
void*) &uip_broadcast_addr, 1);
209 if (((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->proto == UIP_PROTO_UDP
210 && radio_uip_uaodv_dest_port(&uip_buf[UIP_LLH_LEN],
uip_len) ==
UIP_HTONS(UAODV_UDPPORT)) {
211 uip_ipaddr_t nexthop;
212 memcpy(&nexthop, &((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr, 4);
214 uip_len = radio_uip_uaodv_add_header(
215 &uip_buf[UIP_LLH_LEN],
221 return radio_uip_buffer_outgoing_packet(
222 &uip_buf[UIP_LLH_LEN],
224 &((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN + FWD_PACKET_LENGTH])->destipaddr,
225 MAX_RETRANSMISSIONS_RREP);
229 route = uaodv_rt_lookup_any((&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
230 if (route ==
NULL || route->is_bad) {
233 if (tcpip_is_forwarding) {
234 uaodv_bad_dest((&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
241 uip_len = radio_uip_uaodv_add_header(&uip_buf[UIP_LLH_LEN],
uip_len, uip_ds6_route_nexthop(route));
242 return radio_uip_buffer_outgoing_packet(
243 &uip_buf[UIP_LLH_LEN],
246 MAX_RETRANSMISSIONS_UNICAST);
258 radio->set_receive_function(receiver);
263 radio_uip_calc_crc(uint8_t *buf,
int len)
265 uint16_t crcacc = 0xffff;
269 for (counter = 0; counter < len; counter++) {
270 crcacc =
crc16_add(buf[counter], crcacc);
276 radio_uip_buffer_outgoing_packet(uint8_t *buf,
int len, uip_ipaddr_t *dest,
int max_sends)
278 struct buf_packet *packet;
283 crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN],
uip_len);
288 packet = packet->next) {
289 if (packet->crc == crc) {
295 packet = (
struct buf_packet *)
memb_alloc(&buf_packet_mem);
296 if (packet ==
NULL) {
301 memcpy(packet->data, buf, len);
303 packet->resends = max_sends;
305 if (packet->resends > 1)
306 packet->want_ack = 1;
308 packet->want_ack = 0;
309 memcpy(&packet->finaldest, dest, 4);
324 radio_uip_is_ack(uint8_t *buf,
int len)
326 if (len != ACK_PACKET_LENGTH)
329 return memcmp(buf, ACK_ID, ACK_ID_LENGTH) == 0;
334 radio_uip_handle_ack(uint8_t *buf,
int len)
336 struct buf_packet *packet;
339 ackCRC = (uint16_t) (buf[ACK_CRC] << 8) + (uint16_t) (0xff&buf[ACK_CRC+1]);
344 packet = packet->next) {
345 if (packet->crc == ackCRC) {
356 radio_uip_uaodv_add_header(uint8_t *buf,
int len, uip_ipaddr_t *addr)
358 uint8_t tempbuf[len];
359 memcpy(tempbuf, buf, len);
360 memcpy(&buf[FWD_PACKET_LENGTH], tempbuf, len);
361 memcpy(buf, FWD_ID, FWD_ID_LENGTH);
362 memcpy(&buf[FWD_NEXT_IP], (
char*)addr, 4);
363 return FWD_PACKET_LENGTH + len;
367 radio_uip_uaodv_remove_header(uint8_t *buf,
int len)
369 uint8_t tempbuf[len];
370 memcpy(tempbuf, &buf[FWD_PACKET_LENGTH], len);
371 memcpy(buf, tempbuf, len);
372 return len - FWD_PACKET_LENGTH;
376 radio_uip_uaodv_change_header(uint8_t *buf,
int len, uip_ipaddr_t *addr)
378 memcpy(&buf[FWD_NEXT_IP], addr, 4);
382 radio_uip_uaodv_header_exists(uint8_t *buf,
int len)
384 return !memcmp(buf, FWD_ID, FWD_ID_LENGTH);
388 radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr)
390 return uip_ipaddr_cmp(addr, &uip_broadcast_addr);
394 radio_uip_uaodv_fwd_is_broadcast(uint8_t *buf,
int len)
396 return radio_uip_uaodv_is_broadcast((uip_ipaddr_t*) &buf[FWD_NEXT_IP]);
400 radio_uip_uaodv_fwd_is_me(uint8_t *buf,
int len)
402 return !memcmp(&buf[FWD_NEXT_IP], &uip_hostaddr, 4);
406 radio_uip_uaodv_dest_is_me(uint8_t *buf,
int len)
408 return !memcmp((&((
struct uip_udpip_hdr *)buf)->destipaddr), &uip_hostaddr, 4);
412 radio_uip_uaodv_dest_port(uint8_t *buf,
int len)
414 if (len <
sizeof(
struct uip_udpip_hdr))
416 return (
int) ((
struct uip_udpip_hdr *)buf)->destport;
#define UIP_FW_DROPPED
An error message that indicates that a packet that should be forwarded or output was dropped...
Linked list manipulation routines.
uip_len
The length of the packet in the uip_buf buffer.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
#define PROCESS_BEGIN()
Define the beginning of a process.
Header file for the uIP TCP/IP stack.
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
#define NULL
The null pointer.
The structure of a device driver for a radio in Contiki.
#define UIP_BUFSIZE
The size of the uIP packet buffer.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
#define UIP_LLH_LEN
The link level header length.
void list_init(list_t list)
Initialize a list.
void * list_head(list_t list)
Get a pointer to the first element of a list.
#define MEMB(name, structure, num)
Declare a memory block.
int(* on)(void)
Turn the radio on.
void list_add(list_t list, void *item)
Add an item at the end of a list.
#define PROCESS(name, strname)
Declare a process.
#define LIST(name)
Declare a linked list.
void process_start(struct process *p, process_data_t data)
Start a process.
Routing tables for the micro implementation of the AODV ad hoc routing protocol ...
#define PROCESS_YIELD()
Yield the currently running process.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definitions for the micro implementation of the AODV ad hoc routing protocol ...
unsigned short crc16_add(unsigned char b, unsigned short acc)
Update an accumulated CRC16 checksum with one byte.
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
#define UIP_FW_OK
A non-error message that indicates that something went OK.
Header file for the CRC16 calculcation
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
#define PROCESS_CONTEXT_END(p)
End a context switch.