neighbor-discovery.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
00109
00110 if(val < c->val) {
00111
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
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