Contiki 3.x
chameleon.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Chameleon, Rime's header processing module
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "net/rime/chameleon.h"
41 #include "net/rime/channel.h"
42 #include "net/rime/rime.h"
43 #include "lib/list.h"
44 
45 #include <stdio.h>
46 
47 #ifndef CHAMELEON_MODULE
48 #ifdef CHAMELEON_CONF_MODULE
49 #define CHAMELEON_MODULE CHAMELEON_CONF_MODULE
50 #else /* CHAMELEON_CONF_MODULE */
51 #define CHAMELEON_MODULE chameleon_bitopt
52 #endif /* CHAMELEON_CONF_MODULE */
53 #endif /* CHAMELEON_MODULE */
54 
55 extern const struct chameleon_module CHAMELEON_MODULE;
56 
57 #define DEBUG 0
58 #if DEBUG
59 #include <stdio.h>
60 #define PRINTF(...) printf(__VA_ARGS__)
61 #else
62 #define PRINTF(...)
63 #endif
64 
65 /*---------------------------------------------------------------------------*/
66 void
67 chameleon_init(void)
68 {
69  channel_init();
70 }
71 /*---------------------------------------------------------------------------*/
72 #if DEBUG
73 static void
74 printbin(int n, int digits)
75 {
76  int i;
77  char output[128];
78 
79  for(i = 0; i < digits; ++i) {
80  output[digits - i - 1] = (n & 1) + '0';
81  n >>= 1;
82  }
83  output[i] = 0;
84 
85  printf(output);
86 }
87 
88 static void
89 printhdr(uint8_t *hdr, int len)
90 {
91  int i, j;
92 
93  j = 0;
94  for(i = 0; i < len; ++i) {
95  printbin(hdr[i], 8);
96  printf(" (0x%0x), ", hdr[i]);
97  ++j;
98  if(j == 10) {
99  printf("\n");
100  j = 0;
101  }
102  }
103 
104  if(j != 0) {
105  printf("\n");
106  }
107 }
108 #endif /* DEBUG */
109 /*---------------------------------------------------------------------------*/
110 struct channel *
111 chameleon_parse(void)
112 {
113  struct channel *c = NULL;
114  PRINTF("%d.%d: chameleon_input\n",
116 #if DEBUG
117  printhdr(packetbuf_dataptr(), packetbuf_datalen());
118 #endif /* DEBUG */
119  c = CHAMELEON_MODULE.input();
120  if(c != NULL) {
121  PRINTF("%d.%d: chameleon_input channel %d\n",
123  c->channelno);
124  packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
125  } else {
126  PRINTF("%d.%d: chameleon_input channel not found for incoming packet\n",
128  }
129  return c;
130 }
131 /*---------------------------------------------------------------------------*/
132 int
133 chameleon_create(struct channel *c)
134 {
135  int ret;
136 
137  PRINTF("%d.%d: chameleon_output channel %d\n",
139  c->channelno);
140 
141  ret = CHAMELEON_MODULE.output(c);
142  packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
143 #if DEBUG
144  printhdr(packetbuf_hdrptr(), packetbuf_hdrlen());
145 #endif /* DEBUG */
146  if(ret) {
147  return 1;
148  }
149  return 0;
150 }
151 /*---------------------------------------------------------------------------*/
152 int
153 chameleon_hdrsize(const struct packetbuf_attrlist attrlist[])
154 {
155  return CHAMELEON_MODULE.hdrsize(attrlist);
156 }
157 /*---------------------------------------------------------------------------*/
Linked list manipulation routines.
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
Header file for Chameleon, Rime&#39;s header processing module
#define NULL
The null pointer.
Header file for Rime&#39;s channel abstraction
uint8_t packetbuf_hdrlen(void)
Get the length of the header in the packetbuf.
Definition: packetbuf.c:245
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207