50 #ifdef CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES
51 #define CHAMELEON_WITH_MAC_LINK_ADDRESSES CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES
53 #define CHAMELEON_WITH_MAC_LINK_ADDRESSES 0
60 #define BITOPT_HDR_SIZE 2
62 static const uint8_t bitmask[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0,
63 0xf8, 0xfc, 0xfe, 0xff };
68 #define PRINTF(...) printf(__VA_ARGS__)
80 le16_write(
void *ptr, uint16_t v)
82 uint8_t *p = (uint8_t *)ptr;
88 static uint16_t CC_INLINE
89 le16_read(
const void *ptr)
91 const uint8_t *p = (
const uint8_t *)ptr;
92 return ((uint16_t)p[1] << 8) | p[0];
95 static uint8_t CC_INLINE
96 get_bits_in_byte(uint8_t *from,
int bitpos,
int vallen)
100 shifted_val = (from[0] << 8) | from[1];
108 return (((shifted_val << bitpos) >> 8) & bitmask[vallen]) >> (8 - vallen);
112 get_bits(uint8_t *to, uint8_t *from,
int bitpos,
int vallen)
118 *to = get_bits_in_byte(from, bitpos, vallen);
121 for(i = 0; i < vallen / 8; ++i) {
128 to[i] = get_bits_in_byte(&from[i], 0, bits);
131 for(i = 0; i < vallen / 8; ++i) {
134 to[i] = get_bits_in_byte(&from[i], bitpos, 8);
138 to[i] = get_bits_in_byte(&from[i], bitpos, bits);
145 header_size(
const struct packetbuf_attrlist *a)
153 for(; a->type != PACKETBUF_ATTR_NONE; ++a) {
154 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
155 if(a->type == PACKETBUF_ADDR_SENDER ||
156 a->type == PACKETBUF_ADDR_RECEIVER) {
173 set_bits_in_byte(uint8_t *target,
int bitpos, uint8_t val,
int vallen)
175 unsigned short shifted_val;
176 shifted_val = val << (8 - bitpos + 8 - vallen);
179 target[0] |= shifted_val >> 8;
180 target[1] |= shifted_val & 0xff;
184 set_bits(uint8_t *ptr,
int bitpos, uint8_t *val,
int vallen)
192 set_bits_in_byte(ptr, bitpos, *val , vallen);
195 for(i = 0; i < vallen / 8; ++i) {
202 set_bits_in_byte(&ptr[i], 0, val[i] >> (8 - bits), bits);
205 for(i = 0; i < vallen / 8; ++i) {
208 set_bits_in_byte(&ptr[i], bitpos, val[i], 8);
212 set_bits_in_byte(&ptr[i], 0, val[i] >> (8 - bits + bitpos), bits);
220 printbin(
int n,
int digits)
225 for(i = 0; i < digits; ++i) {
226 output[digits - i - 1] = (n & 1) +
'0';
235 printhdr(uint8_t *hdr,
int len)
240 for(i = 0; i < len; ++i) {
257 pack_header(
struct channel *c)
259 const struct packetbuf_attrlist *a;
261 int byteptr, bitptr, len;
263 struct bitopt_hdr *hdr;
268 hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1);
270 PRINTF(
"chameleon-bitopt: insufficient space for headers\n");
274 hdr->channel[0] = c->channelno & 0xff;
275 hdr->channel[1] = (c->channelno >> 8) & 0xff;
278 memset(hdrptr, 0, hdrbytesize);
280 byteptr = bitptr = 0;
282 for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) {
283 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
284 if(a->type == PACKETBUF_ADDR_SENDER ||
285 a->type == PACKETBUF_ADDR_RECEIVER) {
287 PRINTF(
"%d.%d: pack_header leaving sender/receiver to link layer\n");
291 PRINTF(
"%d.%d: pack_header type %d, len %d, bitptr %d, ",
293 a->type, a->len, bitptr);
296 byteptr = bitptr / 8;
297 if(PACKETBUF_IS_ADDR(a->type)) {
298 set_bits(&hdrptr[byteptr], bitptr & 7,
299 (uint8_t *)packetbuf_addr(a->type), len);
300 PRINTF(
"address %d.%d\n",
302 ((uint8_t *)packetbuf_addr(a->type))[0],
303 ((uint8_t *)packetbuf_addr(a->type))[1]);
306 packetbuf_attr_t val = packetbuf_attr(a->type);
307 le16_write(buffer, val);
308 set_bits(&hdrptr[byteptr], bitptr & 7, buffer, len);
321 static struct channel *
324 const struct packetbuf_attrlist *a;
325 int byteptr, bitptr, len;
328 struct bitopt_hdr *hdr;
336 PRINTF(
"chameleon-bitopt: too short packet\n");
339 c = channel_lookup((hdr->channel[1] << 8) + hdr->channel[0]);
341 PRINTF(
"chameleon-bitopt: input: channel %u not found\n",
342 (hdr->channel[1] << 8) + hdr->channel[0]);
347 hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1);
349 PRINTF(
"chameleon-bitopt: too short packet\n");
352 byteptr = bitptr = 0;
353 for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) {
354 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
355 if(a->type == PACKETBUF_ADDR_SENDER ||
356 a->type == PACKETBUF_ADDR_RECEIVER) {
361 PRINTF(
"%d.%d: unpack_header type %d, len %d, bitptr %d\n",
363 a->type, a->len, bitptr);
366 byteptr = bitptr / 8;
367 if(PACKETBUF_IS_ADDR(a->type)) {
369 get_bits((uint8_t *)&addr, &hdrptr[byteptr], bitptr & 7, len);
370 PRINTF(
"%d.%d: unpack_header type %d, addr %d.%d\n",
372 a->type, addr.u8[0], addr.u8[1]);
373 packetbuf_set_addr(a->type, &addr);
375 packetbuf_attr_t val;
376 uint8_t buffer[2] = {0};
377 get_bits(buffer, &hdrptr[byteptr], bitptr & 7, len);
378 val = le16_read(buffer);
379 packetbuf_set_attr(a->type, val);
380 PRINTF(
"%d.%d: unpack_header type %d, val %d\n",
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Header file for Chameleon, Rime's header processing module
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Header file for the Rime stack
#define NULL
The null pointer.
static uint8_t output(const uip_lladdr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
#define CC_CONST_FUNCTION
Configure if the C compiler have problems with const function pointers.