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