osd-contiki/platform/cooja/net/radio-uip-uaodv.c

419 lines
13 KiB
C
Raw Normal View History

2007-05-19 16:25:43 +02:00
/*
* Copyright (c) 2007, Swedish Institute of Computer Science
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
#include "radio-uip-uaodv.h"
#include "net/ip/uip.h"
2007-05-19 16:25:43 +02:00
#include "net/uaodv.h"
#include "net/ipv4/uaodv-rt.h"
#include "net/ipv4/uaodv-def.h"
2007-07-18 01:02:21 +02:00
#include "lib/crc16.h"
#include "list.h"
2007-05-19 16:25:43 +02:00
#include <string.h>
#include <stdio.h>
2007-07-18 01:02:21 +02:00
/* Packet buffer size and retransmission settings */
#define MAX_BUFFERED_PACKETS 10
#define MAX_RETRANSMISSIONS_RREP 16
#define MAX_RETRANSMISSIONS_UNICAST 16
/* Forward packet (header) */
#define FWD_ID "fWd:"
#define FWD_ID_LENGTH 4
#define FWD_NEXT_IP FWD_ID_LENGTH
#define FWD_PACKET_LENGTH (FWD_NEXT_IP + 4)
/* Acknowledgement packet */
#define ACK_ID "aCk"
#define ACK_ID_LENGTH 3
#define ACK_CRC ACK_ID_LENGTH
#define ACK_PACKET_LENGTH (ACK_ID_LENGTH + 2)
2007-07-26 15:22:01 +02:00
#define ACK_TIMEOUT (CLOCK_SECOND / 50) * (random_rand() % 100)
2007-07-11 17:23:42 +02:00
enum {
EVENT_SEND_ACK
};
2007-07-18 01:02:21 +02:00
struct buf_packet {
struct buf_packet *next;
uint8_t data[UIP_BUFSIZE];
2007-07-11 17:23:42 +02:00
int len;
uint8_t resends;
uint8_t acked;
uint8_t want_ack;
uint16_t crc;
uip_ipaddr_t finaldest;
2007-07-11 17:23:42 +02:00
struct etimer etimer;
};
2007-07-18 01:02:21 +02:00
LIST(buf_packet_list);
MEMB(buf_packet_mem, struct buf_packet, MAX_BUFFERED_PACKETS);
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
PROCESS(radio_uip_process, "radio uIP uAODV process");
2007-07-11 17:23:42 +02:00
static const struct radio_driver *radio;
2007-10-22 15:08:13 +02:00
2007-05-19 16:25:43 +02:00
/*---------------------------------------------------------------------------*/
2007-10-22 15:08:13 +02:00
static void receiver(const struct radio_driver *d);
uint8_t radio_uip_uaodv_send(void);
2007-10-22 15:08:13 +02:00
void radio_uip_uaodv_init(const struct radio_driver *d);
int radio_uip_handle_ack(uint8_t *buf, int len);
uint16_t radio_uip_calc_crc(uint8_t *buf, int len);
int radio_uip_buffer_outgoing_packet(uint8_t *buf, int len, uip_ipaddr_t *dest, int max_sends);
int radio_uip_is_ack(uint8_t *buf, int len);
int radio_uip_uaodv_add_header(uint8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_remove_header(uint8_t *buf, int len);
void radio_uip_uaodv_change_header(uint8_t *buf, int len, uip_ipaddr_t *addr);
int radio_uip_uaodv_header_exists(uint8_t *buf, int len);
2007-05-19 16:25:43 +02:00
int radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr);
int radio_uip_uaodv_fwd_is_broadcast(uint8_t *buf, int len);
int radio_uip_uaodv_fwd_is_me(uint8_t *buf, int len);
int radio_uip_uaodv_dest_is_me(uint8_t *buf, int len);
int radio_uip_uaodv_dest_port(uint8_t *buf, int len);
2007-05-19 16:25:43 +02:00
/*---------------------------------------------------------------------------*/
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
/* Main process - handles (re)transmissions and acks */
PROCESS_THREAD(radio_uip_process, ev, data)
2007-07-11 17:23:42 +02:00
{
2007-07-18 01:02:21 +02:00
struct buf_packet *packet;
2007-07-11 17:23:42 +02:00
PROCESS_BEGIN();
while(1) {
PROCESS_YIELD();
if(ev == EVENT_SEND_ACK) {
2007-07-18 01:02:21 +02:00
/* Prepare and send ack for given 16-bit CRC */
uint8_t ackPacket[ACK_PACKET_LENGTH];
2007-07-18 01:02:21 +02:00
memcpy(ackPacket, ACK_ID, ACK_ID_LENGTH);
ackPacket[ACK_CRC] = ((uint16_t) data >> 8);
ackPacket[ACK_CRC+1] = ((uint16_t) data & 0xff);
2007-07-18 01:02:21 +02:00
radio->send(ackPacket, ACK_PACKET_LENGTH);
2007-07-11 17:23:42 +02:00
} else if(ev == PROCESS_EVENT_TIMER) {
/* Locate which packet acknowledgement timed out */
2007-07-18 01:02:21 +02:00
for(packet = list_head(buf_packet_list);
packet != NULL;
packet = packet->next) {
if (etimer_expired(&packet->etimer)) {
if (packet->acked) {
/* Already acked packet, remove silently */
list_remove(buf_packet_list, packet);
memb_free(&buf_packet_mem, packet);
} else if (packet->resends > 0) {
/* Resend packet */
packet->resends--;
2007-07-26 15:22:01 +02:00
etimer_set(&packet->etimer, ACK_TIMEOUT);
2007-07-18 01:02:21 +02:00
radio->send(packet->data, packet->len);
2007-07-11 17:23:42 +02:00
} else {
2007-07-18 01:02:21 +02:00
/* Packet was resent maximum number of times */
/* If an ack was expected, flag destination to bad */
if (packet->want_ack && !uip_ipaddr_cmp(&packet->finaldest, &uip_broadcast_addr)) {
uaodv_bad_dest(&packet->finaldest);
}
list_remove(buf_packet_list, packet);
memb_free(&buf_packet_mem, packet);
}
2007-07-11 17:23:42 +02:00
}
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
2007-05-19 16:25:43 +02:00
static void
receiver(const struct radio_driver *d)
{
uip_len = d->read(&uip_buf[UIP_LLH_LEN], UIP_BUFSIZE - UIP_LLH_LEN);
if (uip_len <= 0) {
return;
}
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
/* Detect and handle acknowledgements */
2007-07-11 17:23:42 +02:00
if (radio_uip_is_ack(&uip_buf[UIP_LLH_LEN], uip_len)) {
radio_uip_handle_ack(&uip_buf[UIP_LLH_LEN], uip_len);
return;
}
2007-07-18 01:02:21 +02:00
/* If no uAODV header, receive as usual */
2007-05-19 16:25:43 +02:00
if (!radio_uip_uaodv_header_exists(&uip_buf[UIP_LLH_LEN], uip_len)) {
tcpip_input();
return;
}
/* Drop packet unless we are the uAODV forwarder */
if (!radio_uip_uaodv_fwd_is_me(&uip_buf[UIP_LLH_LEN], uip_len)) {
return;
}
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
{
/* Send ack as soon as possible */
uint16_t crc;
2007-07-18 01:02:21 +02:00
crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN], uip_len);
process_post(&radio_uip_process, EVENT_SEND_ACK, (void*) (uint32_t) crc);
2007-07-18 01:02:21 +02:00
}
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
/* Strip header and receive packet */
uip_len = radio_uip_uaodv_remove_header(&uip_buf[UIP_LLH_LEN], uip_len);
tcpip_input();
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
uint8_t
2007-05-19 16:25:43 +02:00
radio_uip_uaodv_send(void)
{
2007-07-18 01:02:21 +02:00
struct uaodv_rt_entry *route;
/* Transmit broadcast packets without header */
if (radio_uip_uaodv_is_broadcast(&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr)) {
2007-11-25 23:45:04 +01:00
return radio_uip_buffer_outgoing_packet(&uip_buf[UIP_LLH_LEN], uip_len, (void*) &uip_broadcast_addr, 1);
2007-07-18 01:02:21 +02:00
}
2007-07-11 17:23:42 +02:00
2007-07-18 01:02:21 +02:00
/* Transmit uAODV packets with headers but without using route table */
if (((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->proto == UIP_PROTO_UDP
&& radio_uip_uaodv_dest_port(&uip_buf[UIP_LLH_LEN], uip_len) == UIP_HTONS(UAODV_UDPPORT)) {
2007-07-18 01:02:21 +02:00
uip_ipaddr_t nexthop;
memcpy(&nexthop, &((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr, 4);
uip_len = radio_uip_uaodv_add_header(
&uip_buf[UIP_LLH_LEN],
uip_len,
&nexthop
);
/* Buffer packet for persistent transmission */
return radio_uip_buffer_outgoing_packet(
&uip_buf[UIP_LLH_LEN],
uip_len,
&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN + FWD_PACKET_LENGTH])->destipaddr,
MAX_RETRANSMISSIONS_RREP);
2007-05-19 16:25:43 +02:00
}
2007-07-18 01:02:21 +02:00
/* Fetch already prepared uAODV route */
route = uaodv_rt_lookup_any((&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
if (route == NULL || route->is_bad) {
/* If we are forwarding, notify origin of this bad route */
if (tcpip_is_forwarding) {
uaodv_bad_dest((&((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr));
}
return UIP_FW_DROPPED;
}
/* Add header and buffer packet for persistent transmission */
uip_len = radio_uip_uaodv_add_header(&uip_buf[UIP_LLH_LEN], uip_len, uip_ds6_route_nexthop(route)); /* TODO Correct? */
2007-07-18 01:02:21 +02:00
return radio_uip_buffer_outgoing_packet(
&uip_buf[UIP_LLH_LEN],
uip_len,
&route->dest,
MAX_RETRANSMISSIONS_UNICAST);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
void
radio_uip_uaodv_init(const struct radio_driver *d)
{
2007-07-18 01:02:21 +02:00
/* Prepare buffers and start main process */
memb_init(&buf_packet_mem);
list_init(buf_packet_list);
process_start(&radio_uip_process, NULL);
2007-07-11 17:23:42 +02:00
2007-05-19 16:25:43 +02:00
radio = d;
radio->set_receive_function(receiver);
2007-07-11 17:23:42 +02:00
radio->on();
}
/*---------------------------------------------------------------------------*/
uint16_t
radio_uip_calc_crc(uint8_t *buf, int len)
2007-07-11 17:23:42 +02:00
{
uint16_t crcacc = 0xffff;
2007-07-11 17:23:42 +02:00
int counter;
2007-07-18 01:02:21 +02:00
/* TODO Not effective */
2007-07-11 17:23:42 +02:00
for (counter = 0; counter < len; counter++) {
crcacc = crc16_add(buf[counter], crcacc);
}
return crcacc;
}
/*---------------------------------------------------------------------------*/
int
radio_uip_buffer_outgoing_packet(uint8_t *buf, int len, uip_ipaddr_t *dest, int max_sends)
2007-07-11 17:23:42 +02:00
{
2007-07-18 01:02:21 +02:00
struct buf_packet *packet;
uint16_t crc;
2007-07-18 01:02:21 +02:00
/* Calculate packet's unique CRC */
crc = radio_uip_calc_crc(&uip_buf[UIP_LLH_LEN], uip_len);
/* Check if this packet is already being transmitted */
for(packet = list_head(buf_packet_list);
packet != NULL;
packet = packet->next) {
if (packet->crc == crc) {
return UIP_FW_DROPPED;
}
}
2007-07-11 17:23:42 +02:00
/* Allocate storage memory */
2007-07-18 01:02:21 +02:00
packet = (struct buf_packet *)memb_alloc(&buf_packet_mem);
if (packet == NULL) {
return UIP_FW_DROPPED;
2007-07-11 17:23:42 +02:00
}
2007-07-18 01:02:21 +02:00
/* Prepare packet buffer */
memcpy(packet->data, buf, len);
packet->len = len;
packet->resends = max_sends;
packet->acked = 0;
if (packet->resends > 1)
packet->want_ack = 1;
else
packet->want_ack = 0;
memcpy(&packet->finaldest, dest, 4);
packet->crc = crc;
/* Set first transmission to as soon as possible */
PROCESS_CONTEXT_BEGIN(&radio_uip_process);
etimer_set(&packet->etimer, 0);
PROCESS_CONTEXT_END(&radio_uip_process);
/* Add to buffered packets list */
list_add(buf_packet_list, packet);
return UIP_FW_OK;
2007-07-11 17:23:42 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_is_ack(uint8_t *buf, int len)
2007-07-11 17:23:42 +02:00
{
2007-12-13 17:57:31 +01:00
if (len != ACK_PACKET_LENGTH)
2007-07-11 17:23:42 +02:00
return 0;
2007-12-13 17:57:31 +01:00
return memcmp(buf, ACK_ID, ACK_ID_LENGTH) == 0;
2007-07-11 17:23:42 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_handle_ack(uint8_t *buf, int len)
2007-07-11 17:23:42 +02:00
{
2007-07-18 01:02:21 +02:00
struct buf_packet *packet;
uint16_t ackCRC;
2007-07-11 17:23:42 +02:00
ackCRC = (uint16_t) (buf[ACK_CRC] << 8) + (uint16_t) (0xff&buf[ACK_CRC+1]);
2007-07-11 17:23:42 +02:00
/* Locate which packet was acknowledged */
2007-07-18 01:02:21 +02:00
for(packet = list_head(buf_packet_list);
packet != NULL;
packet = packet->next) {
if (packet->crc == ackCRC) {
2007-07-11 17:23:42 +02:00
/* Signal packet has been acknowledged */
2007-07-18 01:02:21 +02:00
packet->acked = 1;
2007-07-11 17:23:42 +02:00
return 0;
}
}
2007-07-18 01:02:21 +02:00
2007-07-11 17:23:42 +02:00
return 1;
}
2007-05-19 16:25:43 +02:00
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_add_header(uint8_t *buf, int len, uip_ipaddr_t *addr)
2007-05-19 16:25:43 +02:00
{
uint8_t tempbuf[len];
2007-10-23 14:16:13 +02:00
memcpy(tempbuf, buf, len);
memcpy(&buf[FWD_PACKET_LENGTH], tempbuf, len);
2007-07-18 01:02:21 +02:00
memcpy(buf, FWD_ID, FWD_ID_LENGTH);
memcpy(&buf[FWD_NEXT_IP], (char*)addr, 4);
return FWD_PACKET_LENGTH + len;
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_remove_header(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
uint8_t tempbuf[len];
2007-10-23 14:16:13 +02:00
memcpy(tempbuf, &buf[FWD_PACKET_LENGTH], len);
memcpy(buf, tempbuf, len);
return len - FWD_PACKET_LENGTH;
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
void
radio_uip_uaodv_change_header(uint8_t *buf, int len, uip_ipaddr_t *addr)
2007-05-19 16:25:43 +02:00
{
2007-07-18 01:02:21 +02:00
memcpy(&buf[FWD_NEXT_IP], addr, 4);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_header_exists(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
2007-07-18 01:02:21 +02:00
return !memcmp(buf, FWD_ID, FWD_ID_LENGTH);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_is_broadcast(uip_ipaddr_t *addr)
{
2007-07-18 01:02:21 +02:00
return uip_ipaddr_cmp(addr, &uip_broadcast_addr);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_fwd_is_broadcast(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
2007-07-18 01:02:21 +02:00
return radio_uip_uaodv_is_broadcast((uip_ipaddr_t*) &buf[FWD_NEXT_IP]);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_fwd_is_me(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
2007-07-18 01:02:21 +02:00
return !memcmp(&buf[FWD_NEXT_IP], &uip_hostaddr, 4);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_dest_is_me(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
2007-07-11 17:23:42 +02:00
return !memcmp((&((struct uip_udpip_hdr *)buf)->destipaddr), &uip_hostaddr, 4);
2007-05-19 16:25:43 +02:00
}
/*---------------------------------------------------------------------------*/
int
radio_uip_uaodv_dest_port(uint8_t *buf, int len)
2007-05-19 16:25:43 +02:00
{
2007-07-11 17:23:42 +02:00
if (len < sizeof(struct uip_udpip_hdr))
return -1;
2007-05-19 16:25:43 +02:00
return (int) ((struct uip_udpip_hdr *)buf)->destport;
}
/*---------------------------------------------------------------------------*/