Contiki 2.5
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  * $Id: chameleon.c,v 1.10 2010/05/28 06:18:39 nifi Exp $
32  */
33 
34 /**
35  * \file
36  * Chameleon, Rime's header processing module
37  * \author
38  * Adam Dunkels <adam@sics.se>
39  */
40 
41 #include "net/rime/chameleon.h"
42 #include "net/rime/channel.h"
43 #include "net/rime.h"
44 #include "lib/list.h"
45 
46 #include <stdio.h>
47 
48 #ifndef CHAMELEON_MODULE
49 #ifdef CHAMELEON_CONF_MODULE
50 #define CHAMELEON_MODULE CHAMELEON_CONF_MODULE
51 #else /* CHAMELEON_CONF_MODULE */
52 #define CHAMELEON_MODULE chameleon_bitopt
53 #endif /* CHAMELEON_CONF_MODULE */
54 #endif /* CHAMELEON_MODULE */
55 
56 extern const struct chameleon_module CHAMELEON_MODULE;
57 
58 #define DEBUG 0
59 #if DEBUG
60 #include <stdio.h>
61 #define PRINTF(...) printf(__VA_ARGS__)
62 #else
63 #define PRINTF(...)
64 #endif
65 
66 /*---------------------------------------------------------------------------*/
67 void
68 chameleon_init(void)
69 {
70  channel_init();
71 }
72 /*---------------------------------------------------------------------------*/
73 #if DEBUG
74 static void
75 printbin(int n, int digits)
76 {
77  int i;
78  char output[128];
79 
80  for(i = 0; i < digits; ++i) {
81  output[digits - i - 1] = (n & 1) + '0';
82  n >>= 1;
83  }
84  output[i] = 0;
85 
86  printf(output);
87 }
88 
89 static void
90 printhdr(uint8_t *hdr, int len)
91 {
92  int i, j;
93 
94  j = 0;
95  for(i = 0; i < len; ++i) {
96  printbin(hdr[i], 8);
97  printf(" (0x%0x), ", hdr[i]);
98  ++j;
99  if(j == 10) {
100  printf("\n");
101  j = 0;
102  }
103  }
104 
105  if(j != 0) {
106  printf("\n");
107  }
108 }
109 #endif /* DEBUG */
110 /*---------------------------------------------------------------------------*/
111 struct channel *
112 chameleon_parse(void)
113 {
114  struct channel *c = NULL;
115  PRINTF("%d.%d: chameleon_input\n",
117 #if DEBUG
118  printhdr(packetbuf_dataptr(), packetbuf_datalen());
119 #endif /* DEBUG */
120  c = CHAMELEON_MODULE.input();
121  if(c != NULL) {
122  PRINTF("%d.%d: chameleon_input channel %d\n",
124  c->channelno);
125  packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
126  } else {
127  PRINTF("%d.%d: chameleon_input channel not found for incoming packet\n",
129  }
130  return c;
131 }
132 /*---------------------------------------------------------------------------*/
133 int
134 chameleon_create(struct channel *c)
135 {
136  int ret;
137 
138  PRINTF("%d.%d: chameleon_output channel %d\n",
140  c->channelno);
141 
142  ret = CHAMELEON_MODULE.output(c);
143  packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
144 #if DEBUG
145  printhdr(packetbuf_hdrptr(), packetbuf_hdrlen());
146 #endif /* DEBUG */
147  if(ret) {
148  return 1;
149  }
150  return 0;
151 }
152 /*---------------------------------------------------------------------------*/
153 int
154 chameleon_hdrsize(const struct packetbuf_attrlist attrlist[])
155 {
156  return CHAMELEON_MODULE.hdrsize(attrlist);
157 }
158 /*---------------------------------------------------------------------------*/