Contiki 2.6
|
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; 00100 00101 memcpy(&msg, packetbuf_dataptr(), sizeof(struct adv_msg)); 00102 00103 PRINTF("%d.%d: adv_packet_received from %d.%d with val %d\n", 00104 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], 00105 from->u8[0], from->u8[1], msg.val); 00106 00107 /* If we receive an announcement with a lower value than ours, we 00108 cancel our own announcement. */ 00109 if(msg.val < c->val) { 00110 /* ctimer_stop(&c->send_timer);*/ 00111 } 00112 00113 if(c->u->recv) { 00114 c->u->recv(c, from, msg.val); 00115 } 00116 } 00117 /*---------------------------------------------------------------------------*/ 00118 static void 00119 adv_packet_sent(struct broadcast_conn *bc, int status, int num_tx) 00120 { 00121 } 00122 /*---------------------------------------------------------------------------*/ 00123 static void send_timer(void *ptr); 00124 00125 static void 00126 set_timers(struct neighbor_discovery_conn *c) 00127 { 00128 ctimer_set(&c->interval_timer, c->current_interval, send_timer, c); 00129 ctimer_set(&c->send_timer, c->current_interval / 2 + random_rand() % 00130 (c->current_interval / 2), 00131 send_adv, c); 00132 } 00133 /*---------------------------------------------------------------------------*/ 00134 static void 00135 send_timer(void *ptr) 00136 { 00137 struct neighbor_discovery_conn *c = ptr; 00138 clock_time_t interval; 00139 00140 interval = c->current_interval * 2; 00141 00142 if(interval > c->max_interval) { 00143 interval = c->max_interval; 00144 } 00145 00146 c->current_interval = interval; 00147 00148 /* printf("current_interval %lu\n", (long unsigned int) interval);*/ 00149 00150 PRINTF("current_interval %lu\n", (long unsigned int) interval); 00151 00152 set_timers(c); 00153 } 00154 /*---------------------------------------------------------------------------*/ 00155 static CC_CONST_FUNCTION struct broadcast_callbacks broadcast_callbacks = 00156 {adv_packet_received, adv_packet_sent }; 00157 /*---------------------------------------------------------------------------*/ 00158 void 00159 neighbor_discovery_open(struct neighbor_discovery_conn *c, uint16_t channel, 00160 clock_time_t initial, 00161 clock_time_t min, 00162 clock_time_t max, 00163 const struct neighbor_discovery_callbacks *cb) 00164 { 00165 PRINTF("%d.%d: neighbor discovery open channel %d\n", 00166 rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], 00167 channel); 00168 broadcast_open(&c->c, channel, &broadcast_callbacks); 00169 c->u = cb; 00170 c->initial_interval = initial; 00171 c->min_interval = min; 00172 c->max_interval = max; 00173 } 00174 /*---------------------------------------------------------------------------*/ 00175 void 00176 neighbor_discovery_close(struct neighbor_discovery_conn *c) 00177 { 00178 broadcast_close(&c->c); 00179 ctimer_stop(&c->send_timer); 00180 ctimer_stop(&c->interval_timer); 00181 } 00182 /*---------------------------------------------------------------------------*/ 00183 void 00184 neighbor_discovery_set_val(struct neighbor_discovery_conn *c, uint16_t val) 00185 { 00186 c->val = val; 00187 } 00188 /*---------------------------------------------------------------------------*/ 00189 void 00190 neighbor_discovery_start(struct neighbor_discovery_conn *c, uint16_t val) 00191 { 00192 c->current_interval = c->initial_interval; 00193 c->val = val; 00194 set_timers(c); 00195 } 00196 /*---------------------------------------------------------------------------*/ 00197 /** @} */