Contiki 2.6

chameleon.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * $Id: chameleon.c,v 1.10 2010/05/28 06:18:39 nifi Exp $
00032  */
00033 
00034 /**
00035  * \file
00036  *         Chameleon, Rime's header processing module
00037  * \author
00038  *         Adam Dunkels <adam@sics.se>
00039  */
00040 
00041 #include "net/rime/chameleon.h"
00042 #include "net/rime/channel.h"
00043 #include "net/rime.h"
00044 #include "lib/list.h"
00045 
00046 #include <stdio.h>
00047 
00048 #ifndef CHAMELEON_MODULE
00049 #ifdef CHAMELEON_CONF_MODULE
00050 #define CHAMELEON_MODULE CHAMELEON_CONF_MODULE
00051 #else /* CHAMELEON_CONF_MODULE */
00052 #define CHAMELEON_MODULE chameleon_bitopt
00053 #endif /* CHAMELEON_CONF_MODULE */
00054 #endif /* CHAMELEON_MODULE */
00055 
00056 extern const struct chameleon_module CHAMELEON_MODULE;
00057 
00058 #define DEBUG 0
00059 #if DEBUG
00060 #include <stdio.h>
00061 #define PRINTF(...) printf(__VA_ARGS__)
00062 #else
00063 #define PRINTF(...)
00064 #endif
00065 
00066 /*---------------------------------------------------------------------------*/
00067 void
00068 chameleon_init(void)
00069 {
00070   channel_init();
00071 }
00072 /*---------------------------------------------------------------------------*/
00073 #if DEBUG
00074 static void
00075 printbin(int n, int digits)
00076 {
00077   int i;
00078   char output[128];
00079 
00080   for(i = 0; i < digits; ++i) {
00081     output[digits - i - 1] = (n & 1) + '0';
00082     n >>= 1;
00083   }
00084   output[i] = 0;
00085 
00086   printf(output);
00087 }
00088 
00089 static void
00090 printhdr(uint8_t *hdr, int len)
00091 {
00092   int i, j;
00093 
00094   j = 0;
00095   for(i = 0; i < len; ++i) {
00096     printbin(hdr[i], 8);
00097     printf(" (0x%0x), ", hdr[i]);
00098     ++j;
00099     if(j == 10) {
00100       printf("\n");
00101       j = 0;
00102     }
00103   }
00104 
00105   if(j != 0) {
00106     printf("\n");
00107   }
00108 }
00109 #endif /* DEBUG */
00110 /*---------------------------------------------------------------------------*/
00111 struct channel *
00112 chameleon_parse(void)
00113 {
00114   struct channel *c = NULL;
00115   PRINTF("%d.%d: chameleon_input\n",
00116          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00117 #if DEBUG
00118   printhdr(packetbuf_dataptr(), packetbuf_datalen());
00119 #endif /* DEBUG */
00120   c = CHAMELEON_MODULE.input();
00121   if(c != NULL) {
00122     PRINTF("%d.%d: chameleon_input channel %d\n",
00123            rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00124            c->channelno);
00125     packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
00126   } else {
00127     PRINTF("%d.%d: chameleon_input channel not found for incoming packet\n",
00128            rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
00129   }
00130   return c;
00131 }
00132 /*---------------------------------------------------------------------------*/
00133 int
00134 chameleon_create(struct channel *c)
00135 {
00136   int ret;
00137 
00138   PRINTF("%d.%d: chameleon_output channel %d\n",
00139          rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
00140          c->channelno);
00141 
00142   ret = CHAMELEON_MODULE.output(c);
00143   packetbuf_set_attr(PACKETBUF_ATTR_CHANNEL, c->channelno);
00144 #if DEBUG
00145   printhdr(packetbuf_hdrptr(), packetbuf_hdrlen());
00146 #endif /* DEBUG */
00147   if(ret) {
00148     return 1;
00149   }
00150   return 0;
00151 }
00152 /*---------------------------------------------------------------------------*/
00153 int
00154 chameleon_hdrsize(const struct packetbuf_attrlist attrlist[])
00155 {
00156   return CHAMELEON_MODULE.hdrsize(attrlist);
00157 }
00158 /*---------------------------------------------------------------------------*/