Contiki 3.x
collect.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Tree-based hop-by-hop reliable data collection
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimecollect
42  * @{
43  */
44 
45 #include "contiki.h"
46 #include "net/netstack.h"
47 #include "net/rime/rime.h"
48 #include "net/rime/collect.h"
51 #include "net/rime/packetqueue.h"
52 
53 #include "dev/radio-sensor.h"
54 
55 #include "lib/random.h"
56 
57 #include <string.h>
58 #include <stdio.h>
59 #include <stddef.h>
60 
61 static const struct packetbuf_attrlist attributes[] =
62  {
63  COLLECT_ATTRIBUTES
64  PACKETBUF_ATTR_LAST
65  };
66 
67 
68 /* The recent_packets list holds the sequence number, the originator,
69  and the connection for packets that have been recently
70  forwarded. This list is maintained to avoid forwarding duplicate
71  packets. */
72 #define NUM_RECENT_PACKETS 16
73 
74 struct recent_packet {
75  struct collect_conn *conn;
76  linkaddr_t originator;
77  uint8_t eseqno;
78 };
79 
80 static struct recent_packet recent_packets[NUM_RECENT_PACKETS];
81 static uint8_t recent_packet_ptr;
82 
83 
84 /* This is the header of data packets. The header comtains the routing
85  metric of the last hop sender. This is used to avoid routing loops:
86  if a node receives a packet with a lower routing metric than its
87  own, it drops the packet. */
88 struct data_msg_hdr {
89  uint8_t flags, dummy;
90  uint16_t rtmetric;
91 };
92 
93 
94 /* This is the header of ACK packets. It contains a flags field that
95  indicates if the node is congested (ACK_FLAGS_CONGESTED), if the
96  packet was dropped (ACK_FLAGS_DROPPED), if a packet was dropped due
97  to its lifetime was exceeded (ACK_FLAGS_LIFETIME_EXCEEDED), and if
98  an outdated rtmetric was detected
99  (ACK_FLAGS_RTMETRIC_NEEDS_UPDATE). The flags can contain any
100  combination of the flags. The ACK header also contains the routing
101  metric of the node that sends tha ACK. This is used to keep an
102  up-to-date routing state in the network. */
103 struct ack_msg {
104  uint8_t flags, dummy;
105  uint16_t rtmetric;
106 };
107 
108 #define ACK_FLAGS_CONGESTED 0x80
109 #define ACK_FLAGS_DROPPED 0x40
110 #define ACK_FLAGS_LIFETIME_EXCEEDED 0x20
111 #define ACK_FLAGS_RTMETRIC_NEEDS_UPDATE 0x10
112 
113 
114 /* These are configuration knobs that normally should not be
115  tweaked. MAX_MAC_REXMITS defines how many times the underlying CSMA
116  MAC layer should attempt to resend a data packet before giving
117  up. The MAX_ACK_MAC_REXMITS defines how many times the MAC layer
118  should resend ACK packets. The REXMIT_TIME is the lowest
119  retransmission timeout at the network layer. It is exponentially
120  increased for every new network layer retransmission. The
121  FORWARD_PACKET_LIFETIME is the maximum time a packet is held in the
122  forwarding queue before it is removed. The MAX_SENDING_QUEUE
123  specifies the maximum length of the output queue. If the queue is
124  full, incoming packets are dropped instead of being forwarded. */
125 #ifdef COLLECT_CONF_MAX_MAC_REXMITS
126 #define MAX_MAC_REXMITS COLLECT_CONF_MAX_MAC_REXMITS
127 #else /* COLLECT_CONF_MAX_MAC_REXMITS */
128 #define MAX_MAC_REXMITS 2
129 #endif /* COLLECT_CONF_MAX_MAC_REXMITS */
130 
131 #ifdef COLLECT_CONF_MAX_ACK_MAC_REXMITS
132 #define MAX_ACK_MAC_REXMITS COLLECT_CONF_MAX_ACK_MAC_REXMITS
133 #else /* COLLECT_CONF_MAX_ACK_MAC_REXMITS */
134 #define MAX_ACK_MAC_REXMITS 5
135 #endif /* COLLECT_CONF_MAX_ACK_MAC_REXMITS */
136 
137 #define REXMIT_TIME (CLOCK_SECOND * 32 / NETSTACK_RDC_CHANNEL_CHECK_RATE)
138 #define FORWARD_PACKET_LIFETIME_BASE REXMIT_TIME * 2
139 #define MAX_SENDING_QUEUE 3 * QUEUEBUF_NUM / 4
140 #define MIN_AVAILABLE_QUEUE_ENTRIES 4
141 #define KEEPALIVE_REXMITS 8
142 #define MAX_REXMITS 31
143 
144 MEMB(send_queue_memb, struct packetqueue_item, MAX_SENDING_QUEUE);
145 
146 /* These specifiy the sink's routing metric (0) and the maximum
147  routing metric. If a node has routing metric zero, it is the
148  sink. If a node has the maximum routing metric, it has no route to
149  a sink. */
150 #define RTMETRIC_SINK 0
151 #define RTMETRIC_MAX COLLECT_MAX_DEPTH
152 
153 /* Here we define what we mean with a significantly improved
154  rtmetric. This is used to determine when a new parent should be
155  chosen over an old parent and when to begin more rapidly advertise
156  a new rtmetric. */
157 #define SIGNIFICANT_RTMETRIC_PARENT_CHANGE (COLLECT_LINK_ESTIMATE_UNIT + \
158  COLLECT_LINK_ESTIMATE_UNIT / 2)
159 
160 /* This defines the maximum hops that a packet can take before it is
161  dropped. */
162 #ifdef COLLECT_CONF_MAX_HOPLIM
163 #define MAX_HOPLIM COLLECT_CONF_MAX_HOPLIM
164 #else /* COLLECT_CONF_MAX_HOPLIM */
165 #define MAX_HOPLIM 15
166 #endif /* COLLECT_CONF_MAX_HOPLIM */
167 
168 
169 /* Proactive probing: when there are no packets in the send
170  queue, the system periodically sends a dummy packet to potential
171  parents, i.e., neighbors with a lower rtmetric than we have but for
172  which we do not yet have a link quality estimate. */
173 #ifdef COLLECT_CONF_PROACTIVE_PROBING_INTERVAL
174 #define PROACTIVE_PROBING_INTERVAL (random_rand() % (2 * COLLECT_CONF_PROACTIVE_PROBING_INTERVAL))
175 #else /* COLLECT_CONF_PROACTIVE_PROBING_INTERVAL */
176 #define PROACTIVE_PROBING_INTERVAL (random_rand() % CLOCK_SECOND * 60)
177 #endif /* COLLECT_CONF_PROACTIVE_PROBING_INTERVAL */
178 #define PROACTIVE_PROBING_REXMITS 15
179 
180 /* The ANNOUNCEMENT_SCAN_TIME defines for how long the Collect
181  implementation should listen for announcements from other nodes
182  when it requires a route. */
183 #ifdef ANNOUNCEMENT_CONF_PERIOD
184 #define ANNOUNCEMENT_SCAN_TIME ANNOUNCEMENT_CONF_PERIOD
185 #else /* ANNOUNCEMENT_CONF_PERIOD */
186 #define ANNOUNCEMENT_SCAN_TIME CLOCK_SECOND
187 #endif /* ANNOUNCEMENT_CONF_PERIOD */
188 
189 
190 /* Statistics structure */
191 struct {
192  uint32_t foundroute;
193  uint32_t newparent;
194  uint32_t routelost;
195 
196  uint32_t acksent;
197  uint32_t datasent;
198 
199  uint32_t datarecv;
200  uint32_t ackrecv;
201  uint32_t badack;
202  uint32_t duprecv;
203 
204  uint32_t qdrop;
205  uint32_t rtdrop;
206  uint32_t ttldrop;
207  uint32_t ackdrop;
208  uint32_t timedout;
209 } stats;
210 
211 /* Debug definition: draw routing tree in Cooja. */
212 #define DRAW_TREE 0
213 #define DEBUG 0
214 #if DEBUG
215 #include <stdio.h>
216 #define PRINTF(...) printf(__VA_ARGS__)
217 #else
218 #define PRINTF(...)
219 #endif
220 
221 /* Forward declarations. */
222 static void send_queued_packet(struct collect_conn *c);
223 static void retransmit_callback(void *ptr);
224 static void retransmit_not_sent_callback(void *ptr);
225 static void set_keepalive_timer(struct collect_conn *c);
226 
227 /*---------------------------------------------------------------------------*/
228 /**
229  * This function computes the current rtmetric by adding the last
230  * known rtmetric from our parent with the link estimate to the
231  * parent.
232  *
233  */
234 static uint16_t
235 rtmetric_compute(struct collect_conn *tc)
236 {
237  struct collect_neighbor *n;
238  uint16_t rtmetric = RTMETRIC_MAX;
239 
240  /* This function computes the current rtmetric for this node. It
241  uses the rtmetric of the parent node in the tree and adds the
242  current link estimate from us to the parent node. */
243 
244  /* The collect connection structure stores the address of its
245  current parent. We look up the neighbor identification struct in
246  the collect-neighbor list. */
247  n = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
248 
249  /* If n is NULL, we have no best neighbor. Thus our rtmetric is
250  then COLLECT_RTMETRIC_MAX. */
251  if(n == NULL) {
252  rtmetric = RTMETRIC_MAX;
253  } else {
254  /* Our rtmetric is the rtmetric of our parent neighbor plus
255  the expected transmissions to reach that neighbor. */
256  rtmetric = collect_neighbor_rtmetric_link_estimate(n);
257  }
258 
259  return rtmetric;
260 }
261 /*---------------------------------------------------------------------------*/
262 /**
263  * This function is called when the route advertisements need to be
264  * transmitted more rapidly.
265  *
266  */
267 static void
268 bump_advertisement(struct collect_conn *c)
269 {
270 #if !COLLECT_ANNOUNCEMENTS
271  neighbor_discovery_start(&c->neighbor_discovery_conn, c->rtmetric);
272 #else
273  announcement_bump(&c->announcement);
274 #endif /* !COLLECT_ANNOUNCEMENTS */
275 }
276 /*---------------------------------------------------------------------------*/
277 /**
278  * This function is called to update the current parent node. The
279  * parent may change if new routing information has been found, for
280  * example if a new node with a lower rtmetric and link estimate has
281  * appeared.
282  *
283  */
284 static void
285 update_parent(struct collect_conn *tc)
286 {
287  struct collect_neighbor *current;
288  struct collect_neighbor *best;
289 
290  /* We grab the collect_neighbor struct of our current parent. */
291  current = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
292 
293  /* We call the collect_neighbor module to find the current best
294  parent. */
295  best = collect_neighbor_list_best(&tc->neighbor_list);
296 
297  /* We check if we need to switch parent. Switching parent is done in
298  the following situations:
299 
300  * We do not have a current parent.
301  * The best parent is significantly better than the current parent.
302 
303  If we do not have a current parent, and have found a best parent,
304  we simply use the new best parent.
305 
306  If we already have a current parent, but have found a new parent
307  that is better, we employ a heuristic to avoid switching parents
308  too often. The new parent must be significantly better than the
309  current parent. Being "significantly better" is defined as having
310  an rtmetric that is has a difference of at least 1.5 times the
311  COLLECT_LINK_ESTIMATE_UNIT. This is derived from the experience
312  by Gnawali et al (SenSys 2009). */
313 
314  if(best != NULL) {
315  linkaddr_t previous_parent;
316 
317  if(DRAW_TREE) {
318  linkaddr_copy(&previous_parent, &tc->parent);
319  }
320 
321  if(current == NULL) {
322  /* New parent. */
323  PRINTF("update_parent: new parent %d.%d\n",
324  best->addr.u8[0], best->addr.u8[1]);
325  linkaddr_copy(&tc->parent, &best->addr);
326  stats.foundroute++;
327  bump_advertisement(tc);
328  } else {
329  if(DRAW_TREE) {
330  PRINTF("#A e=%d\n", collect_neighbor_link_estimate(best));
331  }
332  if(collect_neighbor_rtmetric_link_estimate(best) +
333  SIGNIFICANT_RTMETRIC_PARENT_CHANGE <
334  collect_neighbor_rtmetric_link_estimate(current)) {
335 
336  /* We switch parent. */
337  PRINTF("update_parent: new parent %d.%d (%d) old parent %d.%d (%d)\n",
338  best->addr.u8[0], best->addr.u8[1],
339  collect_neighbor_rtmetric(best),
340  tc->parent.u8[0], tc->parent.u8[1],
341  collect_neighbor_rtmetric(current));
342  linkaddr_copy(&tc->parent, &best->addr);
343  stats.newparent++;
344  /* Since we now have a significantly better or worse rtmetric than
345  we had before, we let our neighbors know this quickly. */
346  bump_advertisement(tc);
347 
348  if(DRAW_TREE) {
349  PRINTF("#A e=%d\n", collect_neighbor_link_estimate(best));
350  /* {
351  int i;
352  int etx = 0;
353  PRINTF("#A l=");
354  for(i = 0; i < 8; i++) {
355  PRINTF("%d ", best->le.history[(best->le.historyptr - 1 - i) & 7]);
356  etx += current->le.history[i];
357  }
358  PRINTF("\n");
359  }*/
360  }
361  } else {
362  if(DRAW_TREE) {
363  PRINTF("#A e=%d\n", collect_neighbor_link_estimate(current));
364  /* {
365  int i;
366  int etx = 0;
367  PRINTF("#A l=");
368  for(i = 0; i < 8; i++) {
369  PRINTF("%d ", current->le.history[(current->le.historyptr - 1 - i) & 7]);
370  etx += current->le.history[i];
371  }
372  PRINTF("\n");
373  }*/
374  }
375  }
376  }
377  if(DRAW_TREE) {
378  if(!linkaddr_cmp(&previous_parent, &tc->parent)) {
379  if(!linkaddr_cmp(&previous_parent, &linkaddr_null)) {
380  PRINTF("#L %d 0\n", previous_parent.u8[0]);
381  }
382  PRINTF("#L %d 1\n", tc->parent.u8[0]);
383  }
384  }
385  } else {
386  /* No parent. */
387  if(!linkaddr_cmp(&tc->parent, &linkaddr_null)) {
388  if(DRAW_TREE) {
389  PRINTF("#L %d 0\n", tc->parent.u8[0]);
390  }
391  stats.routelost++;
392  }
393  linkaddr_copy(&tc->parent, &linkaddr_null);
394  }
395 }
396 /*---------------------------------------------------------------------------*/
397 /**
398  * This function is called whenever there is a chance that the routing
399  * metric has changed. The function goes through the list of neighbors
400  * to compute the new routing metric. If the metric has changed, it
401  * notifies neighbors.
402  *
403  *
404  */
405 static void
406 update_rtmetric(struct collect_conn *tc)
407 {
408  PRINTF("update_rtmetric: tc->rtmetric %d\n", tc->rtmetric);
409 
410  /* We should only update the rtmetric if we are not the sink. */
411  if(tc->rtmetric != RTMETRIC_SINK) {
412  uint16_t old_rtmetric, new_rtmetric;
413 
414  /* We remember the current (old) rtmetric for later. */
415  old_rtmetric = tc->rtmetric;
416 
417  /* We may need to update our parent node so we do that now. */
418  update_parent(tc);
419 
420  /* We compute the new rtmetric. */
421  new_rtmetric = rtmetric_compute(tc);
422 
423  /* We sanity check our new rtmetric. */
424  if(new_rtmetric == RTMETRIC_SINK) {
425  /* Defensive programming: if the new rtmetric somehow got to be
426  the rtmetric of the sink, there is a bug somewhere. To avoid
427  destroying the network, we simply will not assume this new
428  rtmetric. Instead, we set our rtmetric to maximum, to
429  indicate that we have no sane route. */
430  new_rtmetric = RTMETRIC_MAX;
431  }
432 
433  /* We set our new rtmetric in the collect conn structure. Then we
434  decide how we should announce this new rtmetric. */
435  tc->rtmetric = new_rtmetric;
436 
437  if(tc->is_router) {
438  /* If we are a router, we update our advertised rtmetric. */
439 #if COLLECT_ANNOUNCEMENTS
440  announcement_set_value(&tc->announcement, tc->rtmetric);
441 #else /* COLLECT_ANNOUNCEMENTS */
442  neighbor_discovery_set_val(&tc->neighbor_discovery_conn, tc->rtmetric);
443 #endif /* COLLECT_ANNOUNCEMENTS */
444 
445  }
446  PRINTF("%d.%d: new rtmetric %d\n",
448  tc->rtmetric);
449 
450  /* We got a new, working, route we send any queued packets we may have. */
451  if(old_rtmetric == RTMETRIC_MAX && new_rtmetric != RTMETRIC_MAX) {
452  PRINTF("Sending queued packet because rtmetric was max\n");
453  send_queued_packet(tc);
454  }
455  if(DRAW_TREE) {
456  if(old_rtmetric != new_rtmetric) {
457  PRINTF("#A rt=%d,p=%d\n", tc->rtmetric, tc->parent.u8[0]);
458  }
459  }
460  }
461 }
462 /*---------------------------------------------------------------------------*/
463 static int
464 enqueue_dummy_packet(struct collect_conn *c, int rexmits)
465 {
466  struct collect_neighbor *n;
467 
468  packetbuf_clear();
469  packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, c->eseqno - 1);
470  packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &linkaddr_node_addr);
471  packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
472  packetbuf_set_attr(PACKETBUF_ATTR_TTL, 1);
473  packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, rexmits);
474 
475  PRINTF("%d.%d: enqueueing dummy packet %d, max_rexmits %d\n",
477  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
478  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
479 
480  /* Allocate space for the header. */
481  packetbuf_hdralloc(sizeof(struct data_msg_hdr));
482 
483  n = collect_neighbor_list_find(&c->neighbor_list, &c->parent);
484  if(n != NULL) {
485  return packetqueue_enqueue_packetbuf(&c->send_queue,
486  FORWARD_PACKET_LIFETIME_BASE * rexmits,
487  c);
488  }
489  return 0;
490 }
491 /*---------------------------------------------------------------------------*/
492 static void
493 send_packet(struct collect_conn *c, struct collect_neighbor *n)
494 {
495  clock_time_t time;
496 
497  PRINTF("Sending packet to %d.%d, %d transmissions\n",
498  n->addr.u8[0], n->addr.u8[1],
499  c->transmissions);
500  /* Defensive programming: if a bug in the MAC/RDC layers will cause
501  it to not call us back, we'll set up the retransmission timer
502  with a high timeout, so that we can cancel the transmission and
503  send a new one. */
504  time = 16 * REXMIT_TIME;
505  ctimer_set(&c->retransmission_timer, time,
507  c->send_time = clock_time();
508 
509  unicast_send(&c->unicast_conn, &n->addr);
510 }
511 /*---------------------------------------------------------------------------*/
512 static void
513 proactive_probing_callback(void *ptr)
514 {
515  struct collect_conn *c = ptr;
516  struct packetqueue_item *i;
517 
518  ctimer_set(&c->proactive_probing_timer, PROACTIVE_PROBING_INTERVAL,
519  proactive_probing_callback, ptr);
520 
521  /* Only do proactive link probing if we are not the sink and if we
522  have a route. */
523  if(c->rtmetric != RTMETRIC_SINK && c->rtmetric != RTMETRIC_MAX) {
524  /* Grab the first packet on the send queue to see if the queue is
525  empty or not. */
526  i = packetqueue_first(&c->send_queue);
527  if(i == NULL) {
528  /* If there are no packets to send, we go through the list of
529  neighbors to find a potential parent for which we do not have a
530  link estimate and send a dummy packet to it. This allows us to
531  quickly gauge the link quality of neighbors that we do not
532  currently use as parents. */
533  struct collect_neighbor *n;
534 
535  /* Find the neighbor with the lowest number of estimates. */
536  for(n = list_head(collect_neighbor_list(&c->neighbor_list));
537  n != NULL; n = list_item_next(n)) {
538  if(n->rtmetric + COLLECT_LINK_ESTIMATE_UNIT < c->rtmetric &&
539  collect_link_estimate_num_estimates(&n->le) == 0) {
540  linkaddr_t current_parent;
541 
542  PRINTF("proactive_probing_callback: found neighbor with no link estimate, %d.%d\n",
543  n->addr.u8[LINKADDR_SIZE - 2], n->addr.u8[LINKADDR_SIZE - 1]);
544 
545  linkaddr_copy(&current_parent, &c->parent);
546  linkaddr_copy(&c->parent, &n->addr);
547  if(enqueue_dummy_packet(c, PROACTIVE_PROBING_REXMITS)) {
549  }
550  linkaddr_copy(&c->parent, &current_parent);
551  return;
552  }
553  }
554  }
555  PRINTF("%d.%d: nothing on queue\n",
557  return;
558  }
559 }
560 /*---------------------------------------------------------------------------*/
561 /**
562  * This function is called when a queued packet should be sent
563  * out. The function takes the first packet on the output queue, adds
564  * the necessary packet attributes, and sends the packet to the
565  * next-hop neighbor.
566  *
567  */
568 static void
569 send_queued_packet(struct collect_conn *c)
570 {
571  struct queuebuf *q;
572  struct collect_neighbor *n;
573  struct packetqueue_item *i;
574  struct data_msg_hdr hdr;
575  int max_mac_rexmits;
576 
577  /* If we are currently sending a packet, we do not attempt to send
578  another one. */
579  if(c->sending) {
580  PRINTF("%d.%d: queue, c is sending\n",
582  return;
583  }
584 
585 
586  /* Grab the first packet on the send queue. */
587  i = packetqueue_first(&c->send_queue);
588  if(i == NULL) {
589  PRINTF("%d.%d: nothing on queue\n",
591 
592  return;
593  }
594 
595  /* We should send the first packet from the queue. */
596  q = packetqueue_queuebuf(i);
597  if(q != NULL) {
598  /* Place the queued packet into the packetbuf. */
599  queuebuf_to_packetbuf(q);
600 
601  /* Pick the neighbor to which to send the packet. We use the
602  parent in the n->parent. */
603  n = collect_neighbor_list_find(&c->neighbor_list, &c->parent);
604 
605  if(n != NULL) {
606 
607  /* If the connection had a neighbor, we construct the packet
608  buffer attributes and set the appropriate flags in the
609  Collect connection structure and send the packet. */
610 
611  PRINTF("%d.%d: sending packet to %d.%d with eseqno %d\n",
613  n->addr.u8[0], n->addr.u8[1],
614  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
615 
616  /* Mark that we are currently sending a packet. */
617  c->sending = 1;
618 
619  /* Remember the parent that we sent this packet to. */
620  linkaddr_copy(&c->current_parent, &c->parent);
621 
622  /* This is the first time we transmit this packet, so set
623  transmissions to zero. */
624  c->transmissions = 0;
625 
626  /* Remember that maximum amount of retransmissions we should
627  make. This is stored inside a packet attribute in the packet
628  on the send queue. */
629  c->max_rexmits = packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT);
630 
631  /* Set the packet attributes: this packet wants an ACK, so we
632  sent the PACKETBUF_ATTR_RELIABLE flag; the MAC should retry
633  MAX_MAC_REXMITS times; and the PACKETBUF_ATTR_PACKET_ID is
634  set to the current sequence number on the connection. */
635  packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
636 
637  max_mac_rexmits = c->max_rexmits > MAX_MAC_REXMITS?
638  MAX_MAC_REXMITS : c->max_rexmits;
639  packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, max_mac_rexmits);
640  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->seqno);
641 
642  stats.datasent++;
643 
644  /* Copy our rtmetric into the packet header of the outgoing
645  packet. */
646  memset(&hdr, 0, sizeof(hdr));
647  hdr.rtmetric = c->rtmetric;
648  memcpy(packetbuf_dataptr(), &hdr, sizeof(struct data_msg_hdr));
649 
650  /* Send the packet. */
651  send_packet(c, n);
652 
653  } else {
654 #if COLLECT_ANNOUNCEMENTS
655 #if COLLECT_CONF_WITH_LISTEN
656  PRINTF("listen\n");
658  ctimer_set(&c->transmit_after_scan_timer, ANNOUNCEMENT_SCAN_TIME,
659  send_queued_packet, c);
660 #else /* COLLECT_CONF_WITH_LISTEN */
661  if(c->is_router) {
662  announcement_set_value(&c->announcement, RTMETRIC_MAX);
663  announcement_bump(&c->announcement);
664  }
665 #endif /* COLLECT_CONF_WITH_LISTEN */
666 #endif /* COLLECT_ANNOUNCEMENTS */
667  }
668  }
669 }
670 /*---------------------------------------------------------------------------*/
671 /**
672  * This function is called to retransmit the first packet on the send
673  * queue.
674  *
675  */
676 static void
677 retransmit_current_packet(struct collect_conn *c)
678 {
679  struct queuebuf *q;
680  struct collect_neighbor *n;
681  struct packetqueue_item *i;
682  struct data_msg_hdr hdr;
683  int max_mac_rexmits;
684 
685  /* Grab the first packet on the send queue, which is the one we are
686  about to retransmit. */
687  i = packetqueue_first(&c->send_queue);
688  if(i == NULL) {
689  PRINTF("%d.%d: nothing on queue\n",
691  /* No packet on the queue, so there is nothing for us to send. */
692  return;
693  }
694 
695  /* Get hold of the queuebuf. */
696  q = packetqueue_queuebuf(i);
697  if(q != NULL) {
698 
699  update_rtmetric(c);
700 
701  /* Place the queued packet into the packetbuf. */
702  queuebuf_to_packetbuf(q);
703 
704  /* Pick the neighbor to which to send the packet. If we have found
705  a better parent while we were transmitting this packet, we
706  chose that neighbor instead. If so, we need to attribute the
707  transmissions we made for the parent to that neighbor. */
708  if(!linkaddr_cmp(&c->current_parent, &c->parent)) {
709  /* struct collect_neighbor *current_neighbor;
710  current_neighbor = collect_neighbor_list_find(&c->neighbor_list,
711  &c->current_parent);
712  if(current_neighbor != NULL) {
713  collect_neighbor_tx(current_neighbor, c->max_rexmits);
714  }*/
715 
716  PRINTF("parent change from %d.%d to %d.%d after %d tx\n",
717  c->current_parent.u8[0], c->current_parent.u8[1],
718  c->parent.u8[0], c->parent.u8[1],
719  c->transmissions);
720 
721  linkaddr_copy(&c->current_parent, &c->parent);
722  c->transmissions = 0;
723  }
724  n = collect_neighbor_list_find(&c->neighbor_list, &c->current_parent);
725 
726  if(n != NULL) {
727 
728  /* If the connection had a neighbor, we construct the packet
729  buffer attributes and set the appropriate flags in the
730  Collect connection structure and send the packet. */
731 
732  PRINTF("%d.%d: sending packet to %d.%d with eseqno %d\n",
734  n->addr.u8[0], n->addr.u8[1],
735  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
736 
737  /* Mark that we are currently sending a packet. */
738  c->sending = 1;
739  packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
740  max_mac_rexmits = c->max_rexmits - c->transmissions > MAX_MAC_REXMITS?
741  MAX_MAC_REXMITS : c->max_rexmits - c->transmissions;
742  packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, max_mac_rexmits);
743  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->seqno);
744 
745  /* Copy our rtmetric into the packet header of the outgoing
746  packet. */
747  memset(&hdr, 0, sizeof(hdr));
748  hdr.rtmetric = c->rtmetric;
749  memcpy(packetbuf_dataptr(), &hdr, sizeof(struct data_msg_hdr));
750 
751  /* Send the packet. */
752  send_packet(c, n);
753  }
754  }
755 
756 }
757 /*---------------------------------------------------------------------------*/
758 static void
759 send_next_packet(struct collect_conn *tc)
760 {
761  /* Remove the first packet on the queue, the packet that was just sent. */
762  packetqueue_dequeue(&tc->send_queue);
763  tc->seqno = (tc->seqno + 1) % (1 << COLLECT_PACKET_ID_BITS);
764 
765  /* Cancel retransmission timer. */
766  ctimer_stop(&tc->retransmission_timer);
767  tc->sending = 0;
768  tc->transmissions = 0;
769 
770  PRINTF("sending next packet, seqno %d, queue len %d\n",
771  tc->seqno, packetqueue_len(&tc->send_queue));
772 
773  /* Send the next packet in the queue, if any. */
774  send_queued_packet(tc);
775 }
776 /*---------------------------------------------------------------------------*/
777 static void
778 handle_ack(struct collect_conn *tc)
779 {
780  struct ack_msg msg;
781  struct collect_neighbor *n;
782 
783  PRINTF("handle_ack: sender %d.%d current_parent %d.%d, id %d seqno %d\n",
784  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
785  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
786  tc->current_parent.u8[0], tc->current_parent.u8[1],
787  packetbuf_attr(PACKETBUF_ATTR_PACKET_ID), tc->seqno);
788  if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
789  &tc->current_parent) &&
790  packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == tc->seqno) {
791 
792  /* PRINTF("rtt %d / %d = %d.%02d\n",
793  (int)(clock_time() - tc->send_time),
794  (int)CLOCK_SECOND,
795  (int)((clock_time() - tc->send_time) / CLOCK_SECOND),
796  (int)(((100 * (clock_time() - tc->send_time)) / CLOCK_SECOND) % 100));*/
797 
798  stats.ackrecv++;
799  memcpy(&msg, packetbuf_dataptr(), sizeof(struct ack_msg));
800 
801  /* It is possible that we receive an ACK for a packet that we
802  think we have not yet sent: if our transmission was received by
803  the other node, but the link-layer ACK was lost, our
804  transmission counter may still be zero. If this is the case, we
805  play it safe by believing that we have sent MAX_MAC_REXMITS
806  transmissions. */
807  if(tc->transmissions == 0) {
808  tc->transmissions = MAX_MAC_REXMITS;
809  }
810  PRINTF("Updating link estimate with %d transmissions\n",
811  tc->transmissions);
812  n = collect_neighbor_list_find(&tc->neighbor_list,
813  packetbuf_addr(PACKETBUF_ADDR_SENDER));
814 
815  if(n != NULL) {
816  collect_neighbor_tx(n, tc->transmissions);
817  collect_neighbor_update_rtmetric(n, msg.rtmetric);
818  update_rtmetric(tc);
819  }
820 
821  PRINTF("%d.%d: ACK from %d.%d after %d transmissions, flags %02x, rtmetric %d\n",
823  tc->current_parent.u8[0], tc->current_parent.u8[1],
824  tc->transmissions,
825  msg.flags,
826  msg.rtmetric);
827 
828  /* The ack contains information about the state of the packet and
829  of the node that received it. We do different things depending
830  on whether or not the packet was dropped. First, we check if
831  the receiving node was congested. If so, we add a maximum
832  transmission number to its routing metric, which increases the
833  chance that another parent will be chosen. */
834  if(msg.flags & ACK_FLAGS_CONGESTED) {
835  PRINTF("ACK flag indicated parent was congested.\n");
836  if(n != NULL) {
837  collect_neighbor_set_congested(n);
838  collect_neighbor_tx(n, tc->max_rexmits * 2);
839  }
840  update_rtmetric(tc);
841  }
842  if((msg.flags & ACK_FLAGS_DROPPED) == 0) {
843  /* If the packet was successfully received, we send the next packet. */
844  send_next_packet(tc);
845  } else {
846  /* If the packet was lost due to its lifetime being exceeded,
847  there is not much more we can do with the packet, so we send
848  the next one instead. */
849  if((msg.flags & ACK_FLAGS_LIFETIME_EXCEEDED)) {
850  send_next_packet(tc);
851  } else {
852  /* If the packet was dropped, but without the node being
853  congested or the packets lifetime being exceeded, we
854  penalize the parent and try sending the packet again. */
855  PRINTF("ACK flag indicated packet was dropped by parent.\n");
856  collect_neighbor_tx(n, tc->max_rexmits);
857  update_rtmetric(tc);
858 
859  ctimer_set(&tc->retransmission_timer,
860  REXMIT_TIME + (random_rand() % (REXMIT_TIME)),
861  retransmit_callback, tc);
862  }
863  }
864 
865  /* Our neighbor's rtmetric needs to be updated, so we bump our
866  advertisements. */
867  if(msg.flags & ACK_FLAGS_RTMETRIC_NEEDS_UPDATE) {
868  bump_advertisement(tc);
869  }
870  set_keepalive_timer(tc);
871  } else {
872  stats.badack++;
873  }
874 }
875 /*---------------------------------------------------------------------------*/
876 static void
877 send_ack(struct collect_conn *tc, const linkaddr_t *to, int flags)
878 {
879  struct ack_msg *ack;
880  uint16_t packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
881 
882  packetbuf_clear();
883  packetbuf_set_datalen(sizeof(struct ack_msg));
884  ack = packetbuf_dataptr();
885  memset(ack, 0, sizeof(struct ack_msg));
886  ack->rtmetric = tc->rtmetric;
887  ack->flags = flags;
888 
889  packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, to);
890  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_ACK);
891  packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 0);
892  packetbuf_set_attr(PACKETBUF_ATTR_ERELIABLE, 0);
893  packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, packet_seqno);
894  packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, MAX_ACK_MAC_REXMITS);
895  unicast_send(&tc->unicast_conn, to);
896 
897  PRINTF("%d.%d: collect: Sending ACK to %d.%d for %d (epacket_id %d)\n",
899  to->u8[0], to->u8[1], packet_seqno,
900  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
901 
902  RIMESTATS_ADD(acktx);
903  stats.acksent++;
904 }
905 /*---------------------------------------------------------------------------*/
906 static void
907 add_packet_to_recent_packets(struct collect_conn *tc)
908 {
909  /* Remember that we have seen this packet for later, but only if
910  it has a length that is larger than zero. Packets with size
911  zero are keepalive or proactive link estimate probes, so we do
912  not record them in our history. */
913  if(packetbuf_datalen() > sizeof(struct data_msg_hdr)) {
914  recent_packets[recent_packet_ptr].eseqno =
915  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
916  linkaddr_copy(&recent_packets[recent_packet_ptr].originator,
917  packetbuf_addr(PACKETBUF_ADDR_ESENDER));
918  recent_packets[recent_packet_ptr].conn = tc;
919  recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
920  }
921 }
922 /*---------------------------------------------------------------------------*/
923 static void
924 node_packet_received(struct unicast_conn *c, const linkaddr_t *from)
925 {
926  struct collect_conn *tc = (struct collect_conn *)
927  ((char *)c - offsetof(struct collect_conn, unicast_conn));
928  int i;
929  struct data_msg_hdr hdr;
930  uint8_t ackflags = 0;
931  struct collect_neighbor *n;
932 
933  memcpy(&hdr, packetbuf_dataptr(), sizeof(struct data_msg_hdr));
934 
935  /* First update the neighbors rtmetric with the information in the
936  packet header. */
937  PRINTF("node_packet_received: from %d.%d rtmetric %d\n",
938  from->u8[0], from->u8[1], hdr.rtmetric);
939  n = collect_neighbor_list_find(&tc->neighbor_list,
940  packetbuf_addr(PACKETBUF_ADDR_SENDER));
941  if(n != NULL) {
942  collect_neighbor_update_rtmetric(n, hdr.rtmetric);
943  update_rtmetric(tc);
944  }
945 
946  /* To protect against sending duplicate packets, we keep a list of
947  recently forwarded packet seqnos. If the seqno of the current
948  packet exists in the list, we immediately send an ACK and drop
949  the packet. */
950  if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
951  PACKETBUF_ATTR_PACKET_TYPE_DATA) {
952  linkaddr_t ack_to;
953 #if DEBUG
954  uint8_t packet_seqno;
955 #endif
956 
957  stats.datarecv++;
958 
959  /* Remember to whom we should send the ACK, since we reuse the
960  packet buffer and its attributes when sending the ACK. */
961  linkaddr_copy(&ack_to, packetbuf_addr(PACKETBUF_ADDR_SENDER));
962 #if DEBUG
963  packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
964 #endif
965 
966  /* If the queue is more than half filled, we add the CONGESTED
967  flag to our outgoing acks. */
968  if(DRAW_TREE) {
969  PRINTF("#A s=%d\n", packetqueue_len(&tc->send_queue));
970  }
971  if(packetqueue_len(&tc->send_queue) >= MAX_SENDING_QUEUE / 2) {
972  ackflags |= ACK_FLAGS_CONGESTED;
973  }
974 
975  for(i = 0; i < NUM_RECENT_PACKETS; i++) {
976  if(recent_packets[i].conn == tc &&
977  recent_packets[i].eseqno == packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID) &&
978  linkaddr_cmp(&recent_packets[i].originator,
979  packetbuf_addr(PACKETBUF_ADDR_ESENDER))) {
980  /* This is a duplicate of a packet we recently received, so we
981  just send an ACK. */
982  PRINTF("%d.%d: found duplicate packet from %d.%d with seqno %d, via %d.%d\n",
984  recent_packets[i].originator.u8[0], recent_packets[i].originator.u8[1],
985  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
986  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
987  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
988  send_ack(tc, &ack_to, ackflags);
989  stats.duprecv++;
990  return;
991  }
992  }
993 
994  /* If we are the sink, the packet has reached its final
995  destination and we call the receive function. */
996  if(tc->rtmetric == RTMETRIC_SINK) {
997  struct queuebuf *q;
998 
999  add_packet_to_recent_packets(tc);
1000 
1001  /* We first send the ACK. We copy the data packet to a queuebuf
1002  first. */
1003  q = queuebuf_new_from_packetbuf();
1004  if(q != NULL) {
1005  send_ack(tc, &ack_to, 0);
1006  queuebuf_to_packetbuf(q);
1007  queuebuf_free(q);
1008  } else {
1009  PRINTF("%d.%d: collect: could not send ACK to %d.%d for %d: no queued buffers\n",
1011  ack_to.u8[0], ack_to.u8[1],
1012  packet_seqno);
1013  stats.ackdrop++;
1014  }
1015 
1016 
1017  PRINTF("%d.%d: sink received packet %d from %d.%d via %d.%d\n",
1019  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
1020  packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
1021  packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
1022  from->u8[0], from->u8[1]);
1023 
1024  packetbuf_hdrreduce(sizeof(struct data_msg_hdr));
1025  /* Call receive function. */
1026  if(packetbuf_datalen() > 0 && tc->cb->recv != NULL) {
1027  tc->cb->recv(packetbuf_addr(PACKETBUF_ADDR_ESENDER),
1028  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
1029  packetbuf_attr(PACKETBUF_ATTR_HOPS));
1030  }
1031  return;
1032  } else if(packetbuf_attr(PACKETBUF_ATTR_TTL) > 1 &&
1033  tc->rtmetric != RTMETRIC_MAX) {
1034  /* If we are not the sink, we forward the packet to our best
1035  neighbor. First, we make sure that the packet comes from a
1036  neighbor that has a higher rtmetric than we have. If not, we
1037  have a loop and we inform the sender that its rtmetric needs
1038  to be updated. Second, we set our rtmetric in the outgoing
1039  packet to let the next hop know what our rtmetric is. Third,
1040  we update the hop count and ttl. */
1041 
1042  if(hdr.rtmetric <= tc->rtmetric) {
1043  ackflags |= ACK_FLAGS_RTMETRIC_NEEDS_UPDATE;
1044  }
1045 
1046  packetbuf_set_attr(PACKETBUF_ATTR_HOPS,
1047  packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1);
1048  packetbuf_set_attr(PACKETBUF_ATTR_TTL,
1049  packetbuf_attr(PACKETBUF_ATTR_TTL) - 1);
1050 
1051 
1052  PRINTF("%d.%d: packet received from %d.%d via %d.%d, sending %d, max_rexmits %d\n",
1054  packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
1055  packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
1056  from->u8[0], from->u8[1], tc->sending,
1057  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
1058 
1059  /* We try to enqueue the packet on the outgoing packet queue. If
1060  we are able to enqueue the packet, we send a positive ACK. If
1061  we are unable to enqueue the packet, we send a negative ACK
1062  to inform the sender that the packet was dropped due to
1063  memory problems. We first check the size of our sending queue
1064  to ensure that we always have entries for packets that
1065  are originated by this node. */
1066  if(packetqueue_len(&tc->send_queue) <= MAX_SENDING_QUEUE - MIN_AVAILABLE_QUEUE_ENTRIES &&
1067  packetqueue_enqueue_packetbuf(&tc->send_queue,
1068  FORWARD_PACKET_LIFETIME_BASE *
1069  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
1070  tc)) {
1071  add_packet_to_recent_packets(tc);
1072  send_ack(tc, &ack_to, ackflags);
1073  send_queued_packet(tc);
1074  } else {
1075  send_ack(tc, &ack_to,
1076  ackflags | ACK_FLAGS_DROPPED | ACK_FLAGS_CONGESTED);
1077  PRINTF("%d.%d: packet dropped: no queue buffer available\n",
1078  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1079  stats.qdrop++;
1080  }
1081  } else if(packetbuf_attr(PACKETBUF_ATTR_TTL) <= 1) {
1082  PRINTF("%d.%d: packet dropped: ttl %d\n",
1084  packetbuf_attr(PACKETBUF_ATTR_TTL));
1085  send_ack(tc, &ack_to, ackflags |
1086  ACK_FLAGS_DROPPED | ACK_FLAGS_LIFETIME_EXCEEDED);
1087  stats.ttldrop++;
1088  }
1089  } else if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
1090  PACKETBUF_ATTR_PACKET_TYPE_ACK) {
1091  PRINTF("Collect: incoming ack %d from %d.%d (%d.%d) seqno %d (%d)\n",
1092  packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
1093  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
1094  packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
1095  tc->current_parent.u8[0],
1096  tc->current_parent.u8[1],
1097  packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
1098  tc->seqno);
1099  handle_ack(tc);
1100  stats.ackrecv++;
1101  }
1102  return;
1103 }
1104 /*---------------------------------------------------------------------------*/
1105 static void
1106 timedout(struct collect_conn *tc)
1107 {
1108  struct collect_neighbor *n;
1109  PRINTF("%d.%d: timedout after %d retransmissions to %d.%d (max retransmissions %d): packet dropped\n",
1110  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], tc->transmissions,
1111  tc->current_parent.u8[0], tc->current_parent.u8[1],
1112  tc->max_rexmits);
1113  PRINTF("%d.%d: timedout after %d retransmissions to %d.%d (max retransmissions %d): packet dropped\n",
1114  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], tc->transmissions,
1115  tc->current_parent.u8[0], tc->current_parent.u8[1],
1116  tc->max_rexmits);
1117 
1118  tc->sending = 0;
1119  n = collect_neighbor_list_find(&tc->neighbor_list,
1120  &tc->current_parent);
1121  if(n != NULL) {
1122  collect_neighbor_tx_fail(n, tc->max_rexmits);
1123  }
1124  update_rtmetric(tc);
1125  send_next_packet(tc);
1126  set_keepalive_timer(tc);
1127 }
1128 /*---------------------------------------------------------------------------*/
1129 static void
1130 node_packet_sent(struct unicast_conn *c, int status, int transmissions)
1131 {
1132  struct collect_conn *tc = (struct collect_conn *)
1133  ((char *)c - offsetof(struct collect_conn, unicast_conn));
1134 
1135  /* For data packets, we record the number of transmissions */
1136  if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
1137  PACKETBUF_ATTR_PACKET_TYPE_DATA) {
1138 
1139  tc->transmissions += transmissions;
1140  PRINTF("tx %d\n", tc->transmissions);
1141  PRINTF("%d.%d: MAC sent %d transmissions to %d.%d, status %d, total transmissions %d\n",
1143  transmissions,
1144  tc->current_parent.u8[0], tc->current_parent.u8[1],
1145  status, tc->transmissions);
1146  if(tc->transmissions >= tc->max_rexmits) {
1147  timedout(tc);
1148  stats.timedout++;
1149  } else {
1150  clock_time_t time = REXMIT_TIME / 2 + (random_rand() % (REXMIT_TIME / 2));
1151  PRINTF("retransmission time %lu\n", time);
1152  ctimer_set(&tc->retransmission_timer, time,
1153  retransmit_callback, tc);
1154  }
1155  }
1156 }
1157 /*---------------------------------------------------------------------------*/
1158 /**
1159  * This function is called from a ctimer that is setup when a packet
1160  * is first transmitted. If the MAC layer signals that the packet is
1161  * sent, the ctimer will be stopped before this function is called. If
1162  * this function ends up being called, we add the maximum number of
1163  * MAC layer transmissions to the transmission count, and call the
1164  * retransmit function.
1165  */
1166 static void
1168 {
1169  struct collect_conn *c = ptr;
1170 
1171  PRINTF("retransmit not sent, %d transmissions\n", c->transmissions);
1172  c->transmissions += MAX_MAC_REXMITS + 1;
1174 }
1175 /*---------------------------------------------------------------------------*/
1176 /**
1177  * This function is called from a ctimer that is setup when a packet
1178  * is sent. The purpose of this function is to either retransmit the
1179  * current packet, or timeout the packet. The descision is made
1180  * depending on how many times the packet has been transmitted. The
1181  * ctimer is set up in the function node_packet_sent().
1182  */
1183 static void
1185 {
1186  struct collect_conn *c = ptr;
1187 
1188  PRINTF("retransmit, %d transmissions\n", c->transmissions);
1189  if(c->transmissions >= c->max_rexmits) {
1190  timedout(c);
1191  stats.timedout++;
1192  } else {
1193  c->sending = 0;
1195  }
1196 }
1197 /*---------------------------------------------------------------------------*/
1198 #if !COLLECT_ANNOUNCEMENTS
1199 static void
1200 adv_received(struct neighbor_discovery_conn *c, const linkaddr_t *from,
1201  uint16_t rtmetric)
1202 {
1203  struct collect_conn *tc = (struct collect_conn *)
1204  ((char *)c - offsetof(struct collect_conn, neighbor_discovery_conn));
1205  struct collect_neighbor *n;
1206 
1207  n = collect_neighbor_list_find(&tc->neighbor_list, from);
1208 
1209  if(n == NULL) {
1210  collect_neighbor_list_add(&tc->neighbor_list, from, rtmetric);
1211  if(rtmetric == RTMETRIC_MAX) {
1212  bump_advertisement(tc);
1213  }
1214  } else {
1215  /* Check if the advertised rtmetric has changed to
1216  RTMETRIC_MAX. This may indicate that the neighbor has lost its
1217  routes or that it has rebooted. In either case, we bump our
1218  advertisement rate to allow our neighbor to receive a new
1219  rtmetric from us. If our neighbor already happens to have an
1220  rtmetric of RTMETRIC_MAX recorded, it may mean that our
1221  neighbor does not hear our advertisements. If this is the case,
1222  we should not bump our advertisement rate. */
1223  if(rtmetric == RTMETRIC_MAX &&
1224  collect_neighbor_rtmetric(n) != RTMETRIC_MAX) {
1225  bump_advertisement(tc);
1226  }
1227  collect_neighbor_update_rtmetric(n, rtmetric);
1228  PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
1230  n->addr.u8[0], n->addr.u8[1], rtmetric);
1231  }
1232 
1233  update_rtmetric(tc);
1234 }
1235 #else
1236 static void
1237 received_announcement(struct announcement *a, const linkaddr_t *from,
1238  uint16_t id, uint16_t value)
1239 {
1240  struct collect_conn *tc = (struct collect_conn *)
1241  ((char *)a - offsetof(struct collect_conn, announcement));
1242  struct collect_neighbor *n;
1243 
1244  n = collect_neighbor_list_find(&tc->neighbor_list, from);
1245 
1246  if(n == NULL) {
1247  /* Only add neighbors that have an rtmetric that is lower than
1248  ours. */
1249  if(value < tc->rtmetric) {
1250  collect_neighbor_list_add(&tc->neighbor_list, from, value);
1251  PRINTF("%d.%d: new neighbor %d.%d, rtmetric %d\n",
1253  from->u8[0], from->u8[1], value);
1254  }
1255  if(value == RTMETRIC_MAX && tc->rtmetric != RTMETRIC_MAX) {
1256  bump_advertisement(tc);
1257  }
1258  } else {
1259  /* Check if the advertised rtmetric has changed to
1260  RTMETRIC_MAX. This may indicate that the neighbor has lost its
1261  routes or that it has rebooted. In either case, we bump our
1262  advertisement rate to allow our neighbor to receive a new
1263  rtmetric from us. If our neighbor already happens to have an
1264  rtmetric of RTMETRIC_MAX recorded, it may mean that our
1265  neighbor does not hear our advertisements. If this is the case,
1266  we should not bump our advertisement rate. */
1267  if(value == RTMETRIC_MAX &&
1268  collect_neighbor_rtmetric(n) != RTMETRIC_MAX) {
1269  bump_advertisement(tc);
1270  }
1271  collect_neighbor_update_rtmetric(n, value);
1272  PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
1274  n->addr.u8[0], n->addr.u8[1], value);
1275  }
1276 
1277  update_rtmetric(tc);
1278 
1279 #if ! COLLECT_CONF_WITH_LISTEN
1280  if(value == RTMETRIC_MAX &&
1281  tc->rtmetric != RTMETRIC_MAX) {
1282  if(tc->is_router) {
1283  announcement_bump(&tc->announcement);
1284  }
1285  }
1286 #endif /* COLLECT_CONF_WITH_LISTEN */
1287 }
1288 #endif /* !COLLECT_ANNOUNCEMENTS */
1289 /*---------------------------------------------------------------------------*/
1290 static const struct unicast_callbacks unicast_callbacks = {node_packet_received,
1291  node_packet_sent};
1292 #if !COLLECT_ANNOUNCEMENTS
1293 static const struct neighbor_discovery_callbacks neighbor_discovery_callbacks =
1294  { adv_received, NULL};
1295 #endif /* !COLLECT_ANNOUNCEMENTS */
1296 /*---------------------------------------------------------------------------*/
1297 void
1298 collect_open(struct collect_conn *tc, uint16_t channels,
1299  uint8_t is_router,
1300  const struct collect_callbacks *cb)
1301 {
1302  unicast_open(&tc->unicast_conn, channels + 1, &unicast_callbacks);
1303  channel_set_attributes(channels + 1, attributes);
1304  tc->rtmetric = RTMETRIC_MAX;
1305  tc->cb = cb;
1306  tc->is_router = is_router;
1307  tc->seqno = 10;
1308  tc->eseqno = 0;
1309  LIST_STRUCT_INIT(tc, send_queue_list);
1310  collect_neighbor_list_new(&tc->neighbor_list);
1311  tc->send_queue.list = &(tc->send_queue_list);
1312  tc->send_queue.memb = &send_queue_memb;
1313  collect_neighbor_init();
1314 
1315 #if !COLLECT_ANNOUNCEMENTS
1316  neighbor_discovery_open(&tc->neighbor_discovery_conn, channels,
1317  CLOCK_SECOND * 4,
1318  CLOCK_SECOND * 60,
1319 #ifdef COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
1320  COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME,
1321 #else
1322  CLOCK_SECOND * 600UL,
1323 #endif
1324  &neighbor_discovery_callbacks);
1325  neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
1326 #else /* !COLLECT_ANNOUNCEMENTS */
1327  announcement_register(&tc->announcement, channels,
1328  received_announcement);
1329 #if ! COLLECT_CONF_WITH_LISTEN
1330  if(tc->is_router) {
1331  announcement_set_value(&tc->announcement, RTMETRIC_MAX);
1332  }
1333 #endif /* COLLECT_CONF_WITH_LISTEN */
1334 #endif /* !COLLECT_ANNOUNCEMENTS */
1335 
1336  ctimer_set(&tc->proactive_probing_timer, PROACTIVE_PROBING_INTERVAL,
1337  proactive_probing_callback, tc);
1338 
1339 }
1340 /*---------------------------------------------------------------------------*/
1341 static void
1342 send_keepalive(void *ptr)
1343 {
1344  struct collect_conn *c = ptr;
1345 
1346  set_keepalive_timer(c);
1347 
1348  /* Send keepalive message only if there are no pending transmissions. */
1349  if(c->sending == 0 && packetqueue_len(&c->send_queue) == 0) {
1350  if(enqueue_dummy_packet(c, KEEPALIVE_REXMITS)) {
1351  PRINTF("%d.%d: sending keepalive\n",
1352  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1353  send_queued_packet(c);
1354  }
1355  }
1356 }
1357 /*---------------------------------------------------------------------------*/
1358 static void
1359 set_keepalive_timer(struct collect_conn *c)
1360 {
1361  if(c->keepalive_period != 0) {
1362  ctimer_set(&c->keepalive_timer, (c->keepalive_period / 2) +
1363  (random_rand() % (c->keepalive_period / 2)),
1364  send_keepalive, c);
1365  } else {
1366  ctimer_stop(&c->keepalive_timer);
1367  }
1368 }
1369 /*---------------------------------------------------------------------------*/
1370 void
1371 collect_set_keepalive(struct collect_conn *c, clock_time_t period)
1372 {
1373  c->keepalive_period = period;
1374  set_keepalive_timer(c);
1375 }
1376 /*---------------------------------------------------------------------------*/
1377 void
1378 collect_close(struct collect_conn *tc)
1379 {
1380 #if COLLECT_ANNOUNCEMENTS
1381  announcement_remove(&tc->announcement);
1382 #else
1383  neighbor_discovery_close(&tc->neighbor_discovery_conn);
1384 #endif /* COLLECT_ANNOUNCEMENTS */
1385  unicast_close(&tc->unicast_conn);
1386  while(packetqueue_first(&tc->send_queue) != NULL) {
1387  packetqueue_dequeue(&tc->send_queue);
1388  }
1389 }
1390 /*---------------------------------------------------------------------------*/
1391 void
1392 collect_set_sink(struct collect_conn *tc, int should_be_sink)
1393 {
1394  if(should_be_sink) {
1395  tc->is_router = 1;
1396  tc->rtmetric = RTMETRIC_SINK;
1397  PRINTF("collect_set_sink: tc->rtmetric %d\n", tc->rtmetric);
1398  bump_advertisement(tc);
1399 
1400  /* Purge the outgoing packet queue. */
1401  while(packetqueue_len(&tc->send_queue) > 0) {
1402  packetqueue_dequeue(&tc->send_queue);
1403  }
1404 
1405  /* Stop the retransmission timer. */
1406  ctimer_stop(&tc->retransmission_timer);
1407  } else {
1408  tc->rtmetric = RTMETRIC_MAX;
1409  }
1410 #if COLLECT_ANNOUNCEMENTS
1411  announcement_set_value(&tc->announcement, tc->rtmetric);
1412 #endif /* COLLECT_ANNOUNCEMENTS */
1413  update_rtmetric(tc);
1414 
1415  bump_advertisement(tc);
1416 
1417  if(DRAW_TREE) {
1418  PRINTF("#A rt=0,p=0\n");
1419  }
1420 }
1421 /*---------------------------------------------------------------------------*/
1422 int
1423 collect_send(struct collect_conn *tc, int rexmits)
1424 {
1425  struct collect_neighbor *n;
1426  int ret;
1427 
1428  packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, tc->eseqno);
1429 
1430  /* Increase the sequence number for the packet we send out. We
1431  employ a trick that allows us to see that a node has been
1432  rebooted: if the sequence number wraps to 0, we set it to half of
1433  the sequence number space. This allows us to detect reboots,
1434  since if a sequence number is less than half of the sequence
1435  number space, the data comes from a node that was recently
1436  rebooted. */
1437 
1438  tc->eseqno = (tc->eseqno + 1) % (1 << COLLECT_PACKET_ID_BITS);
1439 
1440  if(tc->eseqno == 0) {
1441  tc->eseqno = ((int)(1 << COLLECT_PACKET_ID_BITS)) / 2;
1442  }
1443  packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &linkaddr_node_addr);
1444  packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
1445  packetbuf_set_attr(PACKETBUF_ATTR_TTL, MAX_HOPLIM);
1446  if(rexmits > MAX_REXMITS) {
1447  packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, MAX_REXMITS);
1448  } else {
1449  packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, rexmits);
1450  }
1451 
1452  PRINTF("%d.%d: originating packet %d, max_rexmits %d\n",
1454  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
1455  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
1456 
1457  if(tc->rtmetric == RTMETRIC_SINK) {
1458  packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 0);
1459  if(tc->cb->recv != NULL) {
1460  tc->cb->recv(packetbuf_addr(PACKETBUF_ADDR_ESENDER),
1461  packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
1462  packetbuf_attr(PACKETBUF_ATTR_HOPS));
1463  }
1464  return 1;
1465  } else {
1466 
1467  /* Allocate space for the header. */
1468  packetbuf_hdralloc(sizeof(struct data_msg_hdr));
1469 
1470  if(packetqueue_enqueue_packetbuf(&tc->send_queue,
1471  FORWARD_PACKET_LIFETIME_BASE *
1472  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
1473  tc)) {
1474  send_queued_packet(tc);
1475  ret = 1;
1476  } else {
1477  PRINTF("%d.%d: drop originated packet: no queuebuf\n",
1478  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1479  PRINTF("%d.%d: drop originated packet: no queuebuf\n",
1480  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1481  ret = 0;
1482  }
1483 
1484 
1485  n = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
1486  if(n != NULL) {
1487  PRINTF("%d.%d: sending to %d.%d\n",
1489  n->addr.u8[0], n->addr.u8[1]);
1490  } else {
1491  PRINTF("%d.%d: did not find any neighbor to send to\n",
1492  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1493 #if COLLECT_ANNOUNCEMENTS
1494 #if COLLECT_CONF_WITH_LISTEN
1495  PRINTF("listen\n");
1497  ctimer_set(&tc->transmit_after_scan_timer, ANNOUNCEMENT_SCAN_TIME,
1498  send_queued_packet, tc);
1499 #else /* COLLECT_CONF_WITH_LISTEN */
1500  if(tc->is_router) {
1501  announcement_set_value(&tc->announcement, RTMETRIC_MAX);
1502  announcement_bump(&tc->announcement);
1503  }
1504 #endif /* COLLECT_CONF_WITH_LISTEN */
1505 #endif /* COLLECT_ANNOUNCEMENTS */
1506 
1507  /* if(packetqueue_enqueue_packetbuf(&tc->send_queue,
1508  FORWARD_PACKET_LIFETIME_BASE *
1509  packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
1510  tc)) {
1511  return 1;
1512  } else {
1513  PRINTF("%d.%d: drop originated packet: no queuebuf\n",
1514  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1515  PRINTF("%d.%d: drop originated packet: no queuebuf\n",
1516  linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
1517  }*/
1518  }
1519  }
1520  return ret;
1521 }
1522 /*---------------------------------------------------------------------------*/
1523 int
1524 collect_depth(struct collect_conn *tc)
1525 {
1526  return tc->rtmetric;
1527 }
1528 /*---------------------------------------------------------------------------*/
1529 const linkaddr_t *
1530 collect_parent(struct collect_conn *tc)
1531 {
1532  return &tc->current_parent;
1533 }
1534 /*---------------------------------------------------------------------------*/
1535 void
1536 collect_purge(struct collect_conn *tc)
1537 {
1538  collect_neighbor_list_purge(&tc->neighbor_list);
1539  linkaddr_copy(&tc->parent, &linkaddr_null);
1540  update_rtmetric(tc);
1541  if(DRAW_TREE) {
1542  PRINTF("#L %d 0\n", tc->parent.u8[0]);
1543  }
1544  linkaddr_copy(&tc->parent, &linkaddr_null);
1545 }
1546 /*---------------------------------------------------------------------------*/
1547 void
1548 collect_print_stats(void)
1549 {
1550  PRINTF("collect stats foundroute %lu newparent %lu routelost %lu acksent %lu datasent %lu datarecv %lu ackrecv %lu badack %lu duprecv %lu qdrop %lu rtdrop %lu ttldrop %lu ackdrop %lu timedout %lu\n",
1551  stats.foundroute, stats.newparent, stats.routelost,
1552  stats.acksent, stats.datasent, stats.datarecv,
1553  stats.ackrecv, stats.badack, stats.duprecv,
1554  stats.qdrop, stats.rtdrop, stats.ttldrop, stats.ackdrop,
1555  stats.timedout);
1556 }
1557 /*---------------------------------------------------------------------------*/
1558 /** @} */
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:158
void announcement_bump(struct announcement *a)
Bump an announcement.
Definition: announcement.c:105
Header file for the Collect link estimate
Representation of an announcement.
Definition: announcement.h:83
static void send_queued_packet(struct collect_conn *c)
This function is called when a queued packet should be sent out.
Definition: collect.c:569
static void retransmit_not_sent_callback(void *ptr)
This function is called from a ctimer that is setup when a packet is first transmitted.
Definition: collect.c:1167
void announcement_set_value(struct announcement *a, uint16_t value)
Set the value of an announcement.
Definition: announcement.c:92
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:76
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
static void retransmit_callback(void *ptr)
This function is called from a ctimer that is setup when a packet is sent.
Definition: collect.c:1184
Header file for the packetqueue module
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
static uint16_t rtmetric_compute(struct collect_conn *tc)
This function computes the current rtmetric by adding the last known rtmetric from our parent with th...
Definition: collect.c:235
static void update_rtmetric(struct collect_conn *tc)
This function is called whenever there is a chance that the routing metric has changed.
Definition: collect.c:406
void announcement_register(struct announcement *a, uint16_t id, announcement_callback_t callback)
Register an announcement.
Definition: announcement.c:62
const linkaddr_t linkaddr_null
The null Rime address.
Definition: eth-conf.c:37
struct packetqueue_item * packetqueue_first(struct packetqueue *q)
Access the first item on the packet buffer.
Definition: packetqueue.c:107
Header file for the Contiki radio neighborhood management
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
Header file for the Rime stack
#define NULL
The null pointer.
int packetqueue_enqueue_packetbuf(struct packetqueue *q, clock_time_t lifetime, void *ptr)
Enqueue a packetbuf on a packet queue.
Definition: packetqueue.c:70
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
void packetqueue_dequeue(struct packetqueue *q)
Remove the first item on the packet buffer.
Definition: packetqueue.c:113
struct queuebuf * packetqueue_queuebuf(struct packetqueue_item *i)
Access the queuebuf in a packet queue item.
Definition: packetqueue.c:133
static void update_parent(struct collect_conn *tc)
This function is called to update the current parent node.
Definition: collect.c:285
void announcement_listen(int time)
Listen for announcements for a specific amount of announcement periods.
Definition: announcement.c:114
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Header file for hop-by-hop reliable data collection
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:151
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
Representation of an item in a packet queue.
Definition: packetqueue.h:86
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:170
void announcement_remove(struct announcement *a)
Remove a previously registered announcement.
Definition: announcement.c:76
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:122
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
static void retransmit_current_packet(struct collect_conn *c)
This function is called to retransmit the first packet on the send queue.
Definition: collect.c:677
int packetqueue_len(struct packetqueue *q)
Get the length of the packet queue.
Definition: packetqueue.c:127
#define LIST_STRUCT_INIT(struct_ptr, name)
Initialize a linked list that is part of a structure.
Definition: list.h:122
static void bump_advertisement(struct collect_conn *c)
This function is called when the route advertisements need to be transmitted more rapidly...
Definition: collect.c:268
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:139
Include file for the Contiki low-layer network stack (NETSTACK)