rime.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rime
00003  * @{
00004  */
00005 
00006 /*
00007  * Copyright (c) 2006, Swedish Institute of Computer Science.
00008  * All rights reserved.
00009  *
00010  * Redistribution and use in source and binary forms, with or without
00011  * modification, are permitted provided that the following conditions
00012  * are met:
00013  * 1. Redistributions of source code must retain the above copyright
00014  *    notice, this list of conditions and the following disclaimer.
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in the
00017  *    documentation and/or other materials provided with the distribution.
00018  * 3. Neither the name of the Institute nor the names of its contributors
00019  *    may be used to endorse or promote products derived from this software
00020  *    without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034  * This file is part of the Contiki operating system.
00035  *
00036  * $Id: rime.c,v 1.31 2010/10/03 20:10:22 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Rime initialization and common code
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #define DEBUG 0
00047 #if DEBUG
00048 #include <stdio.h>
00049 #define PRINTF(...) printf(__VA_ARGS__)
00050 #else
00051 #define PRINTF(...)
00052 #endif
00053 
00054 #include "net/netstack.h"
00055 #include "net/rime.h"
00056 #include "net/rime/chameleon.h"
00057 #include "net/rime/route.h"
00058 #include "net/rime/announcement.h"
00059 #include "net/rime/broadcast-announcement.h"
00060 #include "net/mac/mac.h"
00061 
00062 #include "lib/list.h"
00063 
00064 const struct mac_driver *rime_mac;
00065 
00066 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
00067 #define BROADCAST_ANNOUNCEMENT_CHANNEL RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL
00068 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
00069 #define BROADCAST_ANNOUNCEMENT_CHANNEL 2
00070 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_CHANNEL */
00071 
00072 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
00073 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME
00074 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
00075 #define BROADCAST_ANNOUNCEMENT_BUMP_TIME CLOCK_SECOND * 8
00076 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_BUMP_TIME */
00077 
00078 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
00079 #define BROADCAST_ANNOUNCEMENT_MIN_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME
00080 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
00081 #define BROADCAST_ANNOUNCEMENT_MIN_TIME CLOCK_SECOND * 60
00082 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MIN_TIME */
00083 
00084 #ifdef RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
00085 #define BROADCAST_ANNOUNCEMENT_MAX_TIME RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
00086 #else /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
00087 #define BROADCAST_ANNOUNCEMENT_MAX_TIME CLOCK_SECOND * 3600UL
00088 #endif /* RIME_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME */
00089 
00090 
00091 LIST(sniffers);
00092 
00093 /*---------------------------------------------------------------------------*/
00094 void
00095 rime_sniffer_add(struct rime_sniffer *s)
00096 {
00097   list_add(sniffers, s);
00098 }
00099 /*---------------------------------------------------------------------------*/
00100 void
00101 rime_sniffer_remove(struct rime_sniffer *s)
00102 {
00103   list_remove(sniffers, s);
00104 }
00105 /*---------------------------------------------------------------------------*/
00106 static void
00107 input(void)
00108 {
00109   struct rime_sniffer *s;
00110   struct channel *c;
00111 
00112   RIMESTATS_ADD(rx);
00113   c = chameleon_parse();
00114   
00115   for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
00116     if(s->input_callback != NULL) {
00117       s->input_callback();
00118     }
00119   }
00120   
00121   if(c != NULL) {
00122     abc_input(c);
00123   }
00124 }
00125 /*---------------------------------------------------------------------------*/
00126 static void
00127 init(void)
00128 {
00129   queuebuf_init();
00130   packetbuf_clear();
00131   announcement_init();
00132 
00133   rime_mac = &NETSTACK_MAC;
00134   chameleon_init();
00135   
00136   /* XXX This is initializes the transmission of announcements but it
00137    * is not currently certain where this initialization is supposed to
00138    * be. Also, the times are arbitrarily set for now. They should
00139    * either be configurable, or derived from some MAC layer property
00140    * (duty cycle, sleep time, or something similar). But this is OK
00141    * for now, and should at least get us started with experimenting
00142    * with announcements.
00143    */
00144   broadcast_announcement_init(BROADCAST_ANNOUNCEMENT_CHANNEL,
00145                               BROADCAST_ANNOUNCEMENT_BUMP_TIME,
00146                               BROADCAST_ANNOUNCEMENT_MIN_TIME,
00147                               BROADCAST_ANNOUNCEMENT_MAX_TIME);
00148 }
00149 /*---------------------------------------------------------------------------*/
00150 static void
00151 packet_sent(void *ptr, int status, int num_tx)
00152 {
00153   struct channel *c = ptr;
00154 
00155   
00156   switch(status) {
00157   case MAC_TX_COLLISION:
00158     PRINTF("rime: collision after %d tx\n", num_tx);
00159     break; 
00160   case MAC_TX_NOACK:
00161     PRINTF("rime: noack after %d tx\n", num_tx);
00162     break;
00163   case MAC_TX_OK:
00164     PRINTF("rime: sent after %d tx\n", num_tx);
00165     break;
00166   default:
00167     PRINTF("rime: error %d after %d tx\n", status, num_tx);
00168   }
00169   
00170   if(status == MAC_TX_OK) {
00171     struct rime_sniffer *s;
00172     /* Call sniffers, but only if the packet was sent. */
00173     for(s = list_head(sniffers); s != NULL; s = list_item_next(s)) {
00174       if(s->output_callback != NULL) {
00175         s->output_callback();
00176       }
00177     }
00178   }
00179   
00180   abc_sent(c, status, num_tx);
00181 }
00182 /*---------------------------------------------------------------------------*/
00183 int
00184 rime_output(struct channel *c)
00185 {
00186   RIMESTATS_ADD(tx);
00187   if(chameleon_create(c)) {
00188     packetbuf_compact();
00189 
00190     NETSTACK_MAC.send(packet_sent, c);
00191     return 1;
00192   }
00193   return 0;
00194 }
00195 /*---------------------------------------------------------------------------*/
00196 const struct network_driver rime_driver = {
00197   "Rime",
00198   init,
00199   input
00200 };
00201 /** @} */

Generated on Mon Apr 11 14:23:30 2011 for Contiki 2.5 by  doxygen 1.6.1