52 #ifdef CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES
53 #define CHAMELEON_WITH_MAC_LINK_ADDRESSES CHAMELEON_CONF_WITH_MAC_LINK_ADDRESSES
55 #define CHAMELEON_WITH_MAC_LINK_ADDRESSES 0
62 static const uint8_t bitmask[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0,
63 0xf8, 0xfc, 0xfe, 0xff };
68 #define PRINTF(...) printf(__VA_ARGS__)
75 get_bits_in_byte(uint8_t *from,
int bitpos,
int vallen)
79 shifted_val = (from[0] << 8) | from[1];
87 return (((shifted_val << bitpos) >> 8) & bitmask[vallen]) >> (8 - vallen);
91 get_bits(uint8_t *to, uint8_t *from,
int bitpos,
int vallen)
97 *to = get_bits_in_byte(from, bitpos, vallen);
100 for(i = 0; i < vallen / 8; ++i) {
107 to[i] = get_bits_in_byte(&from[i], 0, bits);
110 for(i = 0; i < vallen / 8; ++i) {
113 to[i] = get_bits_in_byte(&from[i], bitpos, 8);
117 to[i] = get_bits_in_byte(&from[i], bitpos, bits);
124 header_size(
const struct packetbuf_attrlist *a)
132 for(; a->type != PACKETBUF_ATTR_NONE; ++a) {
133 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
134 if(a->type == PACKETBUF_ADDR_SENDER ||
135 a->type == PACKETBUF_ADDR_RECEIVER) {
154 set_bits_in_byte(uint8_t *target,
int bitpos, uint8_t val,
int vallen)
156 unsigned short shifted_val;
157 shifted_val = val << (8 - bitpos + 8 - vallen);
160 target[0] |= shifted_val >> 8;
161 target[1] |= shifted_val & 0xff;
165 set_bits(uint8_t *ptr,
int bitpos, uint8_t *val,
int vallen)
173 set_bits_in_byte(ptr, bitpos, *val , vallen);
176 for(i = 0; i < vallen / 8; ++i) {
183 set_bits_in_byte(&ptr[i], 0, val[i] >> (8 - bits), bits);
186 for(i = 0; i < vallen / 8; ++i) {
189 set_bits_in_byte(&ptr[i], bitpos, val[i], 8);
193 set_bits_in_byte(&ptr[i], 0, val[i] >> (8 - bits + bitpos), bits);
201 printbin(
int n,
int digits)
206 for(i = 0; i < digits; ++i) {
207 output[digits - i - 1] = (n & 1) +
'0';
216 printhdr(uint8_t *hdr,
int len)
221 for(i = 0; i < len; ++i) {
238 pack_header(
struct channel *c)
240 const struct packetbuf_attrlist *a;
242 int byteptr, bitptr, len;
244 struct bitopt_hdr *hdr;
249 hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1);
251 PRINTF(
"chameleon-bitopt: insufficient space for headers\n");
255 hdr->channel[0] = c->channelno & 0xff;
256 hdr->channel[1] = (c->channelno >> 8) & 0xff;
259 memset(hdrptr, 0, hdrbytesize);
261 byteptr = bitptr = 0;
263 for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) {
264 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
265 if(a->type == PACKETBUF_ADDR_SENDER ||
266 a->type == PACKETBUF_ADDR_RECEIVER) {
268 PRINTF(
"%d.%d: pack_header leaving sender/receiver to link layer\n");
272 PRINTF(
"%d.%d: pack_header type %s, len %d, bitptr %d, ",
274 packetbuf_attr_strings[a->type], a->len, bitptr);
277 byteptr = bitptr / 8;
278 if(PACKETBUF_IS_ADDR(a->type)) {
279 set_bits(&hdrptr[byteptr], bitptr & 7,
280 (uint8_t *)packetbuf_addr(a->type), len);
281 PRINTF(
"address %d.%d\n",
283 ((uint8_t *)packetbuf_addr(a->type))[0],
284 ((uint8_t *)packetbuf_addr(a->type))[1]);
286 packetbuf_attr_t val;
287 val = packetbuf_attr(a->type);
288 set_bits(&hdrptr[byteptr], bitptr & 7,
289 (uint8_t *)&val, len);
302 static struct channel *
305 const struct packetbuf_attrlist *a;
306 int byteptr, bitptr, len;
309 struct bitopt_hdr *hdr;
317 PRINTF(
"chameleon-bitopt: too short packet\n");
320 c = channel_lookup((hdr->channel[1] << 8) + hdr->channel[0]);
322 PRINTF(
"chameleon-bitopt: input: channel %u not found\n",
323 (hdr->channel[1] << 8) + hdr->channel[0]);
328 hdrbytesize = c->hdrsize / 8 + ((c->hdrsize & 7) == 0? 0: 1);
330 PRINTF(
"chameleon-bitopt: too short packet\n");
333 byteptr = bitptr = 0;
334 for(a = c->attrlist; a->type != PACKETBUF_ATTR_NONE; ++a) {
335 #if CHAMELEON_WITH_MAC_LINK_ADDRESSES
336 if(a->type == PACKETBUF_ADDR_SENDER ||
337 a->type == PACKETBUF_ADDR_RECEIVER) {
342 PRINTF(
"%d.%d: unpack_header type %s, len %d, bitptr %d\n",
344 packetbuf_attr_strings[a->type], a->len, bitptr);
347 byteptr = bitptr / 8;
348 if(PACKETBUF_IS_ADDR(a->type)) {
350 get_bits((uint8_t *)&addr, &hdrptr[byteptr], bitptr & 7, len);
351 PRINTF(
"%d.%d: unpack_header type %s, addr %d.%d\n",
353 packetbuf_attr_strings[a->type],
354 addr.u8[0], addr.u8[1]);
355 packetbuf_set_addr(a->type, &addr);
357 packetbuf_attr_t val = 0;
358 get_bits((uint8_t *)&val, &hdrptr[byteptr], bitptr & 7, len);
360 packetbuf_set_attr(a->type, val);
361 PRINTF(
"%d.%d: unpack_header type %s, val %d\n",
363 packetbuf_attr_strings[a->type],