34 #include "radio-uip-uaodv.h"
46 #define MAX_BUFFERED_PACKETS 10
47 #define MAX_RETRANSMISSIONS_RREP 16
48 #define MAX_RETRANSMISSIONS_UNICAST 16
52 #define FWD_ID_LENGTH 4
53 #define FWD_NEXT_IP FWD_ID_LENGTH
54 #define FWD_PACKET_LENGTH (FWD_NEXT_IP + 4)
58 #define ACK_ID_LENGTH 3
59 #define ACK_CRC ACK_ID_LENGTH
60 #define ACK_PACKET_LENGTH (ACK_ID_LENGTH + 2)
61 #define ACK_TIMEOUT (CLOCK_SECOND / 50) * (random_rand() % 100)
68 struct buf_packet *
next;
79 LIST(buf_packet_list);
80 MEMB(buf_packet_mem,
struct buf_packet, MAX_BUFFERED_PACKETS);
82 PROCESS(radio_uip_process,
"radio uIP uAODV process");
90 u8_t radio_uip_uaodv_send(
void);
92 int radio_uip_handle_ack(u8_t *buf,
int len);
93 u16_t radio_uip_calc_crc(u8_t *buf,
int len);
94 int radio_uip_buffer_outgoing_packet(u8_t *buf,
int len,
uip_ipaddr_t *dest,
int max_sends);
95 int radio_uip_is_ack(u8_t *buf,
int len);
96 int radio_uip_uaodv_add_header(u8_t *buf,
int len,
uip_ipaddr_t *addr);
97 int radio_uip_uaodv_remove_header(u8_t *buf,
int len);
98 void radio_uip_uaodv_change_header(u8_t *buf,
int len,
uip_ipaddr_t *addr);
99 int radio_uip_uaodv_header_exists(u8_t *buf,
int len);
101 int radio_uip_uaodv_fwd_is_broadcast(u8_t *buf,
int len);
102 int radio_uip_uaodv_fwd_is_me(u8_t *buf,
int len);
103 int radio_uip_uaodv_dest_is_me(u8_t *buf,
int len);
104 int radio_uip_uaodv_dest_port(u8_t *buf,
int len);
110 struct buf_packet *packet;
117 if(ev == EVENT_SEND_ACK) {
120 u8_t ackPacket[ACK_PACKET_LENGTH];
121 memcpy(ackPacket, ACK_ID, ACK_ID_LENGTH);
122 ackPacket[ACK_CRC] = ((
u16_t) data >> 8);
123 ackPacket[ACK_CRC+1] = ((
u16_t) data & 0xff);
124 radio->
send(ackPacket, ACK_PACKET_LENGTH);
126 }
else if(ev == PROCESS_EVENT_TIMER) {
130 packet = packet->next) {
138 }
else if (packet->resends > 0) {
143 radio->
send(packet->data, packet->len);
149 if (packet->want_ack && !
uip_ipaddr_cmp(&packet->finaldest, &uip_broadcast_addr)) {
150 uaodv_bad_dest(&packet->finaldest);
172 if (radio_uip_is_ack(&uip_buf[UIP_LLH_LEN],
uip_len)) {
173 radio_uip_handle_ack(&uip_buf[UIP_LLH_LEN],
uip_len);
178 if (!radio_uip_uaodv_header_exists(&uip_buf[UIP_LLH_LEN],
uip_len)) {
184 if (!radio_uip_uaodv_fwd_is_me(&uip_buf[UIP_LLH_LEN],
uip_len)) {
191 crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN],
uip_len);
192 process_post(&radio_uip_process, EVENT_SEND_ACK, (
void*) (u32_t) crc);
196 uip_len = radio_uip_uaodv_remove_header(&uip_buf[UIP_LLH_LEN],
uip_len);
201 radio_uip_uaodv_send(
void)
203 struct uaodv_rt_entry *route;
206 if (radio_uip_uaodv_is_broadcast(&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr)) {
207 return radio_uip_buffer_outgoing_packet(&uip_buf[UIP_LLH_LEN],
uip_len, (
void*) &uip_broadcast_addr, 1);
211 if (((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->proto == UIP_PROTO_UDP
212 && radio_uip_uaodv_dest_port(&uip_buf[UIP_LLH_LEN],
uip_len) ==
UIP_HTONS(UAODV_UDPPORT)) {
214 memcpy(&nexthop, &((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr, 4);
216 uip_len = radio_uip_uaodv_add_header(
217 &uip_buf[UIP_LLH_LEN],
223 return radio_uip_buffer_outgoing_packet(
224 &uip_buf[UIP_LLH_LEN],
226 &((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN + FWD_PACKET_LENGTH])->destipaddr,
227 MAX_RETRANSMISSIONS_RREP);
231 route = uaodv_rt_lookup_any((&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
232 if (route ==
NULL || route->is_bad) {
235 if (tcpip_is_forwarding) {
236 uaodv_bad_dest((&((
struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
243 uip_len = radio_uip_uaodv_add_header(&uip_buf[UIP_LLH_LEN],
uip_len, &route->nexthop);
244 return radio_uip_buffer_outgoing_packet(
245 &uip_buf[UIP_LLH_LEN],
248 MAX_RETRANSMISSIONS_UNICAST);
260 radio->set_receive_function(receiver);
265 radio_uip_calc_crc(u8_t *buf,
int len)
267 u16_t crcacc = 0xffff;
271 for (counter = 0; counter < len; counter++) {
272 crcacc =
crc16_add(buf[counter], crcacc);
278 radio_uip_buffer_outgoing_packet(u8_t *buf,
int len,
uip_ipaddr_t *dest,
int max_sends)
280 struct buf_packet *packet;
285 crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN],
uip_len);
290 packet = packet->next) {
291 if (packet->crc == crc) {
297 packet = (
struct buf_packet *)
memb_alloc(&buf_packet_mem);
298 if (packet ==
NULL) {
303 memcpy(packet->data, buf, len);
305 packet->resends = max_sends;
307 if (packet->resends > 1)
308 packet->want_ack = 1;
310 packet->want_ack = 0;
311 memcpy(&packet->finaldest, dest, 4);
326 radio_uip_is_ack(u8_t *buf,
int len)
328 if (len != ACK_PACKET_LENGTH)
331 return memcmp(buf, ACK_ID, ACK_ID_LENGTH) == 0;
336 radio_uip_handle_ack(u8_t *buf,
int len)
338 struct buf_packet *packet;
341 ackCRC = (
u16_t) (buf[ACK_CRC] << 8) + (
u16_t) (0xff&buf[ACK_CRC+1]);
346 packet = packet->next) {
347 if (packet->crc == ackCRC) {
358 radio_uip_uaodv_add_header(u8_t *buf,
int len,
uip_ipaddr_t *addr)
361 memcpy(tempbuf, buf, len);
362 memcpy(&buf[FWD_PACKET_LENGTH], tempbuf, len);
363 memcpy(buf, FWD_ID, FWD_ID_LENGTH);
364 memcpy(&buf[FWD_NEXT_IP], (
char*)addr, 4);
365 return FWD_PACKET_LENGTH + len;
369 radio_uip_uaodv_remove_header(u8_t *buf,
int len)
372 memcpy(tempbuf, &buf[FWD_PACKET_LENGTH], len);
373 memcpy(buf, tempbuf, len);
374 return len - FWD_PACKET_LENGTH;
378 radio_uip_uaodv_change_header(u8_t *buf,
int len,
uip_ipaddr_t *addr)
380 memcpy(&buf[FWD_NEXT_IP], addr, 4);
384 radio_uip_uaodv_header_exists(u8_t *buf,
int len)
386 return !memcmp(buf, FWD_ID, FWD_ID_LENGTH);
396 radio_uip_uaodv_fwd_is_broadcast(u8_t *buf,
int len)
398 return radio_uip_uaodv_is_broadcast((
uip_ipaddr_t*) &buf[FWD_NEXT_IP]);
402 radio_uip_uaodv_fwd_is_me(u8_t *buf,
int len)
404 return !memcmp(&buf[FWD_NEXT_IP], &uip_hostaddr, 4);
408 radio_uip_uaodv_dest_is_me(u8_t *buf,
int len)
410 return !memcmp((&((
struct uip_udpip_hdr *)buf)->destipaddr), &uip_hostaddr, 4);
414 radio_uip_uaodv_dest_port(u8_t *buf,
int len)
416 if (len <
sizeof(
struct uip_udpip_hdr))
418 return (
int) ((
struct uip_udpip_hdr *)buf)->destport;