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