neighbor-discovery.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup rimeneighbordiscovery
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: neighbor-discovery.c,v 1.19 2010/03/19 13:21:48 adamdunkels Exp $
00037  */
00038 
00039 /**
00040  * \file
00041  *         Neighbor discovery
00042  * \author
00043  *         Adam Dunkels <adam@sics.se>
00044  */
00045 
00046 #include "contiki.h"
00047 
00048 #include "net/rime.h"
00049 #include "net/rime/neighbor-discovery.h"
00050 
00051 #include "dev/radio-sensor.h"
00052 
00053 #include "lib/random.h"
00054 
00055 #if CONTIKI_TARGET_NETSIM
00056 #include "ether.h"
00057 #endif
00058 
00059 #include <string.h>
00060 #include <stdio.h>
00061 #include <stddef.h>
00062 
00063 struct adv_msg {
00064   uint16_t val;
00065 };
00066 
00067 #define DEBUG 0
00068 #if DEBUG
00069 #include <stdio.h>
00070 #define PRINTF(...) printf(__VA_ARGS__)
00071 #else
00072 #define PRINTF(...)
00073 #endif
00074 
00075 /*---------------------------------------------------------------------------*/
00076 static void
00077 send_adv(void *ptr)
00078 {
00079   struct neighbor_discovery_conn *c = ptr;
00080   struct adv_msg *hdr;
00081 
00082   packetbuf_clear();
00083   packetbuf_set_datalen(sizeof(struct adv_msg));
00084   hdr = packetbuf_dataptr();
00085   hdr->val = c->val;
00086   broadcast_send(&c->c);
00087   if(c->u->sent) {
00088     c->u->sent(c);
00089   }
00090   PRINTF("%d.%d: sending neighbor advertisement with val %d\n",
00091          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00092          c->val);
00093 }
00094 /*---------------------------------------------------------------------------*/
00095 static void
00096 adv_packet_received(struct broadcast_conn *ibc, const rimeaddr_t *from)
00097 {
00098   struct neighbor_discovery_conn *c = (struct neighbor_discovery_conn *)ibc;
00099   struct adv_msg *msg = packetbuf_dataptr();
00100   uint16_t val;
00101 
00102   memcpy(&val, &msg->val, sizeof(val));
00103 
00104   PRINTF("%d.%d: adv_packet_received from %d.%d with val %d\n",
00105          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00106          from->u8[0], from->u8[1], val);
00107   
00108   /* If we receive an announcement with a lower value than ours, we
00109      cancel our own announcement. */
00110   if(val < c->val) {
00111     /*    ctimer_stop(&c->send_timer);*/
00112   }
00113 
00114   if(c->u->recv) {
00115     c->u->recv(c, from, val);
00116   }
00117 }
00118 /*---------------------------------------------------------------------------*/
00119 static void
00120 adv_packet_sent(struct broadcast_conn *bc, int status, int num_tx)
00121 {
00122 }
00123 /*---------------------------------------------------------------------------*/
00124 static void send_timer(void *ptr);
00125 
00126 static void
00127 set_timers(struct neighbor_discovery_conn *c)
00128 {
00129   ctimer_set(&c->interval_timer, c->current_interval, send_timer, c);
00130   ctimer_set(&c->send_timer, c->current_interval / 2 + random_rand() %
00131              (c->current_interval / 2),
00132              send_adv, c);
00133 }
00134 /*---------------------------------------------------------------------------*/
00135 static void
00136 send_timer(void *ptr)
00137 {
00138   struct neighbor_discovery_conn *c = ptr;
00139   clock_time_t interval;
00140 
00141   interval = c->current_interval * 2;
00142 
00143   if(interval > c->max_interval) {
00144     interval = c->max_interval;
00145   }
00146 
00147   c->current_interval = interval;
00148 
00149   /*  printf("current_interval %lu\n", (long unsigned int) interval);*/
00150 
00151   PRINTF("current_interval %lu\n", (long unsigned int) interval);
00152 
00153   set_timers(c);
00154 }
00155 /*---------------------------------------------------------------------------*/
00156 static CC_CONST_FUNCTION struct broadcast_callbacks broadcast_callbacks =
00157   {adv_packet_received, adv_packet_sent };
00158 /*---------------------------------------------------------------------------*/
00159 void
00160 neighbor_discovery_open(struct neighbor_discovery_conn *c, uint16_t channel,
00161                         clock_time_t initial,
00162                         clock_time_t min,
00163                         clock_time_t max,
00164                         const struct neighbor_discovery_callbacks *cb)
00165 {
00166   PRINTF("%d.%d: neighbor discovery open channel %d\n",
00167          rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
00168          channel);
00169   broadcast_open(&c->c, channel, &broadcast_callbacks);
00170   c->u = cb;
00171   c->initial_interval = initial;
00172   c->min_interval = min;
00173   c->max_interval = max;
00174 }
00175 /*---------------------------------------------------------------------------*/
00176 void
00177 neighbor_discovery_close(struct neighbor_discovery_conn *c)
00178 {
00179   broadcast_close(&c->c);
00180   ctimer_stop(&c->send_timer);
00181   ctimer_stop(&c->interval_timer);
00182 }
00183 /*---------------------------------------------------------------------------*/
00184 void
00185 neighbor_discovery_set_val(struct neighbor_discovery_conn *c, uint16_t val)
00186 {
00187   c->val = val;
00188 }
00189 /*---------------------------------------------------------------------------*/
00190 void
00191 neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val)
00192 {
00193   c->current_interval = c->initial_interval;
00194   c->val = val;
00195   set_timers(c);
00196 }
00197 /*---------------------------------------------------------------------------*/
00198 /** @} */

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