diff --git a/core/net/rime/chameleon-bitopt.c b/core/net/rime/chameleon-bitopt.c index 063014f7d..4c5c0ad70 100644 --- a/core/net/rime/chameleon-bitopt.c +++ b/core/net/rime/chameleon-bitopt.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: chameleon-bitopt.c,v 1.8 2009/09/09 21:09:42 adamdunkels Exp $ + * $Id: chameleon-bitopt.c,v 1.9 2010/05/28 06:18:39 nifi Exp $ */ /** @@ -44,6 +44,17 @@ #include +/* This option enables an optimization where the link addresses are + left to the MAC RDC and not encoded in the Chameleon header. + Note: this requires that the underlying MAC layer to add link + addresses and will not work together with for example nullrdc. + */ +#ifdef CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES +#define CHAMELEON_WITH_MAC_LINK_ADDRESSES CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES +#else /* !CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES */ +#define CHAMELEON_WITH_MAC_LINK_ADDRESSES 0 +#endif /* !CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES */ + struct bitopt_hdr { uint8_t channel[2]; }; @@ -119,6 +130,13 @@ header_size(const struct packetbuf_attrlist *a) size = 0; for(; a->type != PACKETBUF_ATTR_NONE; ++a) { +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the link layer handle sender and receiver */ + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ /* PRINTF("chameleon header_size: header type %s (%d) len %d\n", packetbuf_attr_strings[a->type], a->type, @@ -229,23 +247,35 @@ pack_header(struct channel *c) all attributes that are used on this channel. */ hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1); - packetbuf_hdralloc(hdrbytesize); - hdrptr = packetbuf_hdrptr(); + if(packetbuf_hdralloc(hdrbytesize + sizeof(struct bitopt_hdr)) == 0) { + PRINTF("chameleon-bitopt: insufficient space for headers\n"); + return 0; + } + hdr = (struct bitopt_hdr *)packetbuf_hdrptr(); + hdr->channel[0] = c->channelno & 0xff; + hdr->channel[1] = (c->channelno >> 8) & 0xff; + + hdrptr = ((uint8_t *)packetbuf_hdrptr()) + sizeof(struct bitopt_hdr); memset(hdrptr, 0, hdrbytesize); byteptr = bitptr = 0; for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) { +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the link layer handle sender and receiver */ + PRINTF("%d.%d: pack_header leaving sender/receiver to link layer\n"); + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ PRINTF("%d.%d: pack_header type %s, len %d, bitptr %d, ", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], packetbuf_attr_strings[a->type], a->len, bitptr); /* len = (a->len & 0xf8) + ((a->len & 7) ? 8: 0);*/ len = a->len; byteptr = bitptr / 8; - if(a->type == PACKETBUF_ADDR_SENDER || - a->type == PACKETBUF_ADDR_RECEIVER || - a->type == PACKETBUF_ADDR_ESENDER || - a->type == PACKETBUF_ADDR_ERECEIVER) { + if(PACKETBUF_IS_ADDR(a->type)) { set_bits(&hdrptr[byteptr], bitptr & 7, (uint8_t *)packetbuf_addr(a->type), len); PRINTF("address %d.%d\n", @@ -266,11 +296,6 @@ pack_header(struct channel *c) } /* printhdr(hdrptr, hdrbytesize);*/ - packetbuf_hdralloc(sizeof(struct bitopt_hdr)); - hdr = (struct bitopt_hdr *)packetbuf_hdrptr(); - hdr->channel[0] = c->channelno & 0xff; - hdr->channel[1] = (c->channelno >> 8) & 0xff; - return 1; /* Send out packet */ } /*---------------------------------------------------------------------------*/ @@ -288,28 +313,39 @@ unpack_header(void) /* The packet has a header that tells us what channel the packet is for. */ hdr = (struct bitopt_hdr *)packetbuf_dataptr(); - packetbuf_hdrreduce(sizeof(struct bitopt_hdr)); + if(packetbuf_hdrreduce(sizeof(struct bitopt_hdr)) == 0) { + PRINTF("chameleon-bitopt: too short packet\n"); + return NULL; + } c = channel_lookup((hdr->channel[1] << 8) + hdr->channel[0]); if(c == NULL) { - PRINTF("chameleon-bitopt: input: channel %d not found\n", hdr->channel); + PRINTF("chameleon-bitopt: input: channel %u not found\n", + (hdr->channel[1] << 8) + hdr->channel[0]); return NULL; } hdrptr = packetbuf_dataptr(); hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1); - packetbuf_hdrreduce(hdrbytesize); + if(packetbuf_hdrreduce(hdrbytesize) == 0) { + PRINTF("chameleon-bitopt: too short packet\n"); + return NULL; + } byteptr = bitptr = 0; for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) { +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the link layer handle sender and receiver */ + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ PRINTF("%d.%d: unpack_header type %s, len %d, bitptr %d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], packetbuf_attr_strings[a->type], a->len, bitptr); /* len = (a->len & 0xf8) + ((a->len & 7) ? 8: 0);*/ len = a->len; byteptr = bitptr / 8; - if(a->type == PACKETBUF_ADDR_SENDER || - a->type == PACKETBUF_ADDR_RECEIVER || - a->type == PACKETBUF_ADDR_ESENDER || - a->type == PACKETBUF_ADDR_ERECEIVER) { + if(PACKETBUF_IS_ADDR(a->type)) { rimeaddr_t addr; get_bits((uint8_t *)&addr, &hdrptr[byteptr], bitptr & 7, len); PRINTF("%d.%d: unpack_header type %s, addr %d.%d\n", @@ -331,18 +367,11 @@ unpack_header(void) bitptr += len; } return c; -} -/*---------------------------------------------------------------------------*/ -static void -init(void) -{ - } /*---------------------------------------------------------------------------*/ CC_CONST_FUNCTION struct chameleon_module chameleon_bitopt = { unpack_header, pack_header, - header_size, - init + header_size }; /*---------------------------------------------------------------------------*/ diff --git a/core/net/rime/chameleon-raw.c b/core/net/rime/chameleon-raw.c index 02b2d0a7a..b44e7eace 100644 --- a/core/net/rime/chameleon-raw.c +++ b/core/net/rime/chameleon-raw.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: chameleon-raw.c,v 1.7 2009/09/09 21:09:42 adamdunkels Exp $ + * $Id: chameleon-raw.c,v 1.8 2010/05/28 06:18:39 nifi Exp $ */ /** @@ -43,6 +43,17 @@ #include "net/rime/chameleon.h" #include "net/rime.h" +/* This option enables an optimization where the link addresses are + left to the MAC RDC and not encoded in the Chameleon header. + Note: this requires that the underlying MAC layer to add link + addresses and will not work together with for example nullrdc. + */ +#ifdef CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES +#define CHAMELEON_WITH_MAC_LINK_ADDRESSES CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES +#else /* !CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES */ +#define CHAMELEON_WITH_MAC_LINK_ADDRESSES 0 +#endif /* !CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES */ + #define DEBUG 0 #if DEBUG #include @@ -52,7 +63,7 @@ #endif struct raw_hdr { - uint16_t channel; + uint8_t channel[2]; }; /*---------------------------------------------------------------------------*/ @@ -68,25 +79,36 @@ input(void) /* The packet has a header that tells us what channel the packet is for. */ hdr = (struct raw_hdr *)packetbuf_dataptr(); - packetbuf_hdrreduce(sizeof(struct raw_hdr)); - c = channel_lookup(hdr->channel); + if(packetbuf_hdrreduce(sizeof(struct raw_hdr)) == 0) { + PRINTF("chameleon-raw: too short packet\n"); + return NULL; + } + c = channel_lookup((hdr->channel[1] << 8) + hdr->channel[0]); if(c == NULL) { - PRINTF("chameleon-raw: input: channel %d not found\n", hdr->channel); + PRINTF("chameleon-raw: input: channel %u not found\n", + (hdr->channel[1] << 8) + hdr->channel[0]); return NULL; } hdrptr = packetbuf_dataptr(); - packetbuf_hdrreduce(c->hdrsize); + if(packetbuf_hdrreduce(c->hdrsize) == 0) { + PRINTF("chameleon-raw: too short packet\n"); + return NULL; + } byteptr = bitptr = 0; for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) { +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the link layer handle sender and receiver */ + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ PRINTF("%d.%d: unpack_header type %s, len %d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], packetbuf_attr_strings[a->type], a->len); len = (a->len & 0xf8) + ((a->len & 7) ? 8: 0); - if(a->type == PACKETBUF_ADDR_SENDER || - a->type == PACKETBUF_ADDR_RECEIVER || - a->type == PACKETBUF_ADDR_ESENDER || - a->type == PACKETBUF_ADDR_ERECEIVER) { + if(PACKETBUF_IS_ADDR(a->type)) { const rimeaddr_t addr; memcpy((uint8_t *)&addr, &hdrptr[byteptr], len / 8); PRINTF("%d.%d: unpack_header type %s, addr %d.%d\n", @@ -119,18 +141,30 @@ output(struct channel *c) /* Compute the total size of the final header by summing the size of all attributes that are used on this channel. */ - packetbuf_hdralloc(c->hdrsize); - hdrptr = packetbuf_hdrptr(); + if(packetbuf_hdralloc(c->hdrsize + sizeof(struct raw_hdr)) == 0) { + PRINTF("chameleon-raw: insufficient space for headers\n"); + return 0; + } + hdr = (struct raw_hdr *)packetbuf_hdrptr(); + hdr->channel[0] = c->channelno & 0xff; + hdr->channel[1] = (c->channelno >> 8) & 0xff; + + hdrptr = ((uint8_t *)packetbuf_hdrptr()) + sizeof(struct raw_hdr); byteptr = 0; for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) { +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the link layer handle sender and receiver */ + PRINTF("%d.%d: pack_header leaving sender/receiver to link layer\n"); + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ PRINTF("%d.%d: pack_header type %s, len %d\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], packetbuf_attr_strings[a->type], a->len); len = (a->len & 0xf8) + ((a->len & 7) ? 8: 0); - if(a->type == PACKETBUF_ADDR_SENDER || - a->type == PACKETBUF_ADDR_RECEIVER || - a->type == PACKETBUF_ADDR_ESENDER || - a->type == PACKETBUF_ADDR_ERECEIVER) { + if(PACKETBUF_IS_ADDR(a->type)) { const rimeaddr_t *rimeaddr; /* memcpy(&hdrptr[byteptr], (uint8_t *)packetbuf_attr_aget(a->type), len / 8);*/ rimeaddr = packetbuf_addr(a->type); @@ -152,10 +186,6 @@ output(struct channel *c) byteptr += len / 8; } - packetbuf_hdralloc(sizeof(struct raw_hdr)); - hdr = (struct raw_hdr *)packetbuf_hdrptr(); - hdr->channel = c->channelno; - return 1; /* Send out packet */ } /*---------------------------------------------------------------------------*/ @@ -173,6 +203,13 @@ hdrsize(const struct packetbuf_attrlist *a) packetbuf_attr_strings[a->type], a->type, a->len);*/ +#if CHAMELEON_WITH_MAC_LINK_ADDRESSES + if(a->type == PACKETBUF_ADDR_SENDER || + a->type == PACKETBUF_ADDR_RECEIVER) { + /* Let the mac layer handle the sender and receiver */ + continue; + } +#endif /* CHAMELEON_WITH_MAC_LINK_ADDRESSES */ len = a->len; if(len < 8) { len = 8; @@ -180,13 +217,7 @@ hdrsize(const struct packetbuf_attrlist *a) size += len; } return size / 8; -} -/*---------------------------------------------------------------------------*/ -static void -init(void) -{ - } /*---------------------------------------------------------------------------*/ CC_CONST_FUNCTION struct chameleon_module chameleon_raw = { input, output, - hdrsize, init }; + hdrsize }; diff --git a/core/net/rime/chameleon.c b/core/net/rime/chameleon.c index 79697d97a..eb790b0c7 100644 --- a/core/net/rime/chameleon.c +++ b/core/net/rime/chameleon.c @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: chameleon.c,v 1.9 2010/02/23 18:29:53 adamdunkels Exp $ + * $Id: chameleon.c,v 1.10 2010/05/28 06:18:39 nifi Exp $ */ /** @@ -45,7 +45,15 @@ #include -static const struct chameleon_module *header_module; +#ifndef CHAMELEON_MODULE +#ifdef CHAMELEON_CONF_MODULE +#define CHAMELEON_MODULE CHAMELEON_CONF_MODULE +#else /* CHAMELEON_CONF_MODULE */ +#define CHAMELEON_MODULE chameleon_bitopt +#endif /* CHAMELEON_CONF_MODULE */ +#endif /* CHAMELEON_MODULE */ + +extern const struct chameleon_module CHAMELEON_MODULE; #define DEBUG 0 #if DEBUG @@ -57,9 +65,8 @@ static const struct chameleon_module *header_module; /*---------------------------------------------------------------------------*/ void -chameleon_init(const struct chameleon_module *m) +chameleon_init(void) { - header_module = m; channel_init(); } /*---------------------------------------------------------------------------*/ @@ -110,17 +117,15 @@ chameleon_parse(void) #if DEBUG printhdr(packetbuf_dataptr(), packetbuf_datalen()); #endif /* DEBUG */ - if(header_module) { - c = header_module->input(); - if(c != NULL) { - PRINTF("%d.%d: chameleon_input channel %d\n", - rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], - c->channelno); - packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno); - } else { - PRINTF("%d.%d: chameleon_input channel not found for incoming packet\n", - rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); - } + c = CHAMELEON_MODULE.input(); + if(c != NULL) { + PRINTF("%d.%d: chameleon_input channel %d\n", + rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], + c->channelno); + packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno); + } else { + PRINTF("%d.%d: chameleon_input channel not found for incoming packet\n", + rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]); } return c; } @@ -134,15 +139,13 @@ chameleon_create(struct channel *c) rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], c->channelno); - if(header_module) { - ret = header_module->output(c); - packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno); + ret = CHAMELEON_MODULE.output(c); + packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno); #if DEBUG - printhdr(packetbuf_hdrptr(), packetbuf_hdrlen()); + printhdr(packetbuf_hdrptr(), packetbuf_hdrlen()); #endif /* DEBUG */ - if(ret) { - return 1; - } + if(ret) { + return 1; } return 0; } @@ -150,11 +153,6 @@ chameleon_create(struct channel *c) int chameleon_hdrsize(const struct packetbuf_attrlist attrlist[]) { - if(header_module != NULL && - header_module->hdrsize != NULL) { - return header_module->hdrsize(attrlist); - } else { - return 0; - } + return CHAMELEON_MODULE.hdrsize(attrlist); } /*---------------------------------------------------------------------------*/ diff --git a/core/net/rime/chameleon.h b/core/net/rime/chameleon.h index 24d19e43d..739903783 100644 --- a/core/net/rime/chameleon.h +++ b/core/net/rime/chameleon.h @@ -28,7 +28,7 @@ * * This file is part of the Contiki operating system. * - * $Id: chameleon.h,v 1.3 2010/02/23 18:29:53 adamdunkels Exp $ + * $Id: chameleon.h,v 1.4 2010/05/28 06:18:39 nifi Exp $ */ /** @@ -41,19 +41,15 @@ #ifndef __CHAMELEON_H__ #define __CHAMELEON_H__ - #include "net/rime/channel.h" -#include "net/rime/chameleon-bitopt.h" -#include "net/rime/chameleon-raw.h" struct chameleon_module { struct channel *(* input)(void); int (* output)(struct channel *); int (* hdrsize)(const struct packetbuf_attrlist *); - void (* init)(void); }; -void chameleon_init(const struct chameleon_module *header_processing_module); +void chameleon_init(void); int chameleon_hdrsize(const struct packetbuf_attrlist attrlist[]); struct channel *chameleon_parse(void); diff --git a/core/net/rime/rime.c b/core/net/rime/rime.c index 6605af33c..ab06f1fa3 100644 --- a/core/net/rime/rime.c +++ b/core/net/rime/rime.c @@ -33,7 +33,7 @@ * * This file is part of the Contiki operating system. * - * $Id: rime.c,v 1.28 2010/03/19 13:17:55 adamdunkels Exp $ + * $Id: rime.c,v 1.29 2010/05/28 06:18:39 nifi Exp $ */ /** @@ -152,7 +152,7 @@ init(void) announcement_init(); rime_mac = &NETSTACK_MAC; - chameleon_init(&chameleon_bitopt); + chameleon_init(); #if ! RIME_CONF_NO_POLITE_ANNOUCEMENTS /* XXX This is initializes the transmission of announcements but it * is not currently certain where this initialization is supposed to