2007-03-15 21:04:30 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2007-03-31 20:33:04 +02:00
|
|
|
* Tree-based hop-by-hop reliable data collection
|
2007-03-15 21:04:30 +01:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 01:15:42 +01:00
|
|
|
/**
|
|
|
|
* \addtogroup rimecollect
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-03-15 21:04:30 +01:00
|
|
|
#include "contiki.h"
|
2012-01-09 18:06:30 +01:00
|
|
|
#include "net/netstack.h"
|
2013-12-12 20:50:07 +01:00
|
|
|
#include "net/rime/rime.h"
|
2007-11-28 17:04:41 +01:00
|
|
|
#include "net/rime/collect.h"
|
2010-09-08 21:21:45 +02:00
|
|
|
#include "net/rime/collect-neighbor.h"
|
|
|
|
#include "net/rime/collect-link-estimate.h"
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-06-14 21:19:16 +02:00
|
|
|
#include "net/packetqueue.h"
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2007-03-15 21:04:30 +01:00
|
|
|
#include "dev/radio-sensor.h"
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
#include "lib/random.h"
|
2007-03-15 21:04:30 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2007-03-22 19:54:22 +01:00
|
|
|
#include <stddef.h>
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
static const struct packetbuf_attrlist attributes[] =
|
2008-06-30 11:15:22 +02:00
|
|
|
{
|
|
|
|
COLLECT_ATTRIBUTES
|
2009-03-12 22:58:20 +01:00
|
|
|
PACKETBUF_ATTR_LAST
|
2008-06-30 11:15:22 +02:00
|
|
|
};
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* The recent_packets list holds the sequence number, the originator,
|
|
|
|
and the connection for packets that have been recently
|
|
|
|
forwarded. This list is maintained to avoid forwarding duplicate
|
|
|
|
packets. */
|
2011-01-18 17:05:53 +01:00
|
|
|
#define NUM_RECENT_PACKETS 16
|
2007-11-17 11:32:54 +01:00
|
|
|
|
|
|
|
struct recent_packet {
|
2010-09-08 21:21:45 +02:00
|
|
|
struct collect_conn *conn;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t originator;
|
2010-09-13 15:28:14 +02:00
|
|
|
uint8_t eseqno;
|
2007-11-17 11:32:54 +01:00
|
|
|
};
|
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
static struct recent_packet recent_packets[NUM_RECENT_PACKETS];
|
|
|
|
static uint8_t recent_packet_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the header of data packets. The header comtains the routing
|
|
|
|
metric of the last hop sender. This is used to avoid routing loops:
|
|
|
|
if a node receives a packet with a lower routing metric than its
|
|
|
|
own, it drops the packet. */
|
|
|
|
struct data_msg_hdr {
|
|
|
|
uint8_t flags, dummy;
|
|
|
|
uint16_t rtmetric;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the header of ACK packets. It contains a flags field that
|
|
|
|
indicates if the node is congested (ACK_FLAGS_CONGESTED), if the
|
2010-09-13 15:28:14 +02:00
|
|
|
packet was dropped (ACK_FLAGS_DROPPED), if a packet was dropped due
|
|
|
|
to its lifetime was exceeded (ACK_FLAGS_LIFETIME_EXCEEDED), and if
|
|
|
|
an outdated rtmetric was detected
|
|
|
|
(ACK_FLAGS_RTMETRIC_NEEDS_UPDATE). The flags can contain any
|
|
|
|
combination of the flags. The ACK header also contains the routing
|
|
|
|
metric of the node that sends tha ACK. This is used to keep an
|
|
|
|
up-to-date routing state in the network. */
|
2010-02-28 10:18:01 +01:00
|
|
|
struct ack_msg {
|
|
|
|
uint8_t flags, dummy;
|
|
|
|
uint16_t rtmetric;
|
|
|
|
};
|
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
#define ACK_FLAGS_CONGESTED 0x80
|
|
|
|
#define ACK_FLAGS_DROPPED 0x40
|
|
|
|
#define ACK_FLAGS_LIFETIME_EXCEEDED 0x20
|
|
|
|
#define ACK_FLAGS_RTMETRIC_NEEDS_UPDATE 0x10
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2007-11-17 11:32:54 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* These are configuration knobs that normally should not be
|
|
|
|
tweaked. MAX_MAC_REXMITS defines how many times the underlying CSMA
|
|
|
|
MAC layer should attempt to resend a data packet before giving
|
|
|
|
up. The MAX_ACK_MAC_REXMITS defines how many times the MAC layer
|
|
|
|
should resend ACK packets. The REXMIT_TIME is the lowest
|
|
|
|
retransmission timeout at the network layer. It is exponentially
|
|
|
|
increased for every new network layer retransmission. The
|
|
|
|
FORWARD_PACKET_LIFETIME is the maximum time a packet is held in the
|
|
|
|
forwarding queue before it is removed. The MAX_SENDING_QUEUE
|
|
|
|
specifies the maximum length of the output queue. If the queue is
|
|
|
|
full, incoming packets are dropped instead of being forwarded. */
|
2014-01-24 12:53:16 +01:00
|
|
|
#ifdef COLLECT_CONF_MAX_MAC_REXMITS
|
|
|
|
#define MAX_MAC_REXMITS COLLECT_CONF_MAX_MAC_REXMITS
|
|
|
|
#else /* COLLECT_CONF_MAX_MAC_REXMITS */
|
|
|
|
#define MAX_MAC_REXMITS 2
|
|
|
|
#endif /* COLLECT_CONF_MAX_MAC_REXMITS */
|
|
|
|
|
|
|
|
#ifdef COLLECT_CONF_MAX_ACK_MAC_REXMITS
|
|
|
|
#define MAX_ACK_MAC_REXMITS COLLECT_CONF_MAX_ACK_MAC_REXMITS
|
|
|
|
#else /* COLLECT_CONF_MAX_ACK_MAC_REXMITS */
|
|
|
|
#define MAX_ACK_MAC_REXMITS 5
|
|
|
|
#endif /* COLLECT_CONF_MAX_ACK_MAC_REXMITS */
|
|
|
|
|
2012-02-21 17:23:54 +01:00
|
|
|
#define REXMIT_TIME (CLOCK_SECOND * 32 / NETSTACK_RDC_CHANNEL_CHECK_RATE)
|
2011-01-10 16:10:43 +01:00
|
|
|
#define FORWARD_PACKET_LIFETIME_BASE REXMIT_TIME * 2
|
2010-10-24 23:08:19 +02:00
|
|
|
#define MAX_SENDING_QUEUE 3 * QUEUEBUF_NUM / 4
|
2011-01-18 17:05:53 +01:00
|
|
|
#define MIN_AVAILABLE_QUEUE_ENTRIES 4
|
2010-10-03 22:08:44 +02:00
|
|
|
#define KEEPALIVE_REXMITS 8
|
2010-10-19 09:34:29 +02:00
|
|
|
#define MAX_REXMITS 31
|
2010-09-23 00:08:08 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
MEMB(send_queue_memb, struct packetqueue_item, MAX_SENDING_QUEUE);
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* These specifiy the sink's routing metric (0) and the maximum
|
|
|
|
routing metric. If a node has routing metric zero, it is the
|
|
|
|
sink. If a node has the maximum routing metric, it has no route to
|
|
|
|
a sink. */
|
|
|
|
#define RTMETRIC_SINK 0
|
|
|
|
#define RTMETRIC_MAX COLLECT_MAX_DEPTH
|
|
|
|
|
|
|
|
/* Here we define what we mean with a significantly improved
|
|
|
|
rtmetric. This is used to determine when a new parent should be
|
|
|
|
chosen over an old parent and when to begin more rapidly advertise
|
|
|
|
a new rtmetric. */
|
2010-10-03 22:08:44 +02:00
|
|
|
#define SIGNIFICANT_RTMETRIC_PARENT_CHANGE (COLLECT_LINK_ESTIMATE_UNIT + \
|
|
|
|
COLLECT_LINK_ESTIMATE_UNIT / 2)
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* This defines the maximum hops that a packet can take before it is
|
|
|
|
dropped. */
|
2014-01-24 12:53:16 +01:00
|
|
|
#ifdef COLLECT_CONF_MAX_HOPLIM
|
|
|
|
#define MAX_HOPLIM COLLECT_CONF_MAX_HOPLIM
|
|
|
|
#else /* COLLECT_CONF_MAX_HOPLIM */
|
|
|
|
#define MAX_HOPLIM 15
|
|
|
|
#endif /* COLLECT_CONF_MAX_HOPLIM */
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
|
2010-10-28 17:36:02 +02:00
|
|
|
/* Proactive probing: when there are no packets in the send
|
2010-10-03 22:08:44 +02:00
|
|
|
queue, the system periodically sends a dummy packet to potential
|
|
|
|
parents, i.e., neighbors with a lower rtmetric than we have but for
|
|
|
|
which we do not yet have a link quality estimate. */
|
2013-03-17 23:52:14 +01:00
|
|
|
#ifdef COLLECT_CONF_PROACTIVE_PROBING_INTERVAL
|
|
|
|
#define PROACTIVE_PROBING_INTERVAL (random_rand() % (2 * COLLECT_CONF_PROACTIVE_PROBING_INTERVAL))
|
|
|
|
#else /* COLLECT_CONF_PROACTIVE_PROBING_INTERVAL */
|
2010-10-28 17:36:02 +02:00
|
|
|
#define PROACTIVE_PROBING_INTERVAL (random_rand() % CLOCK_SECOND * 60)
|
2013-03-17 23:52:14 +01:00
|
|
|
#endif /* COLLECT_CONF_PROACTIVE_PROBING_INTERVAL */
|
2010-10-28 17:36:02 +02:00
|
|
|
#define PROACTIVE_PROBING_REXMITS 15
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* The ANNOUNCEMENT_SCAN_TIME defines for how long the Collect
|
|
|
|
implementation should listen for announcements from other nodes
|
|
|
|
when it requires a route. */
|
2010-03-29 23:52:25 +02:00
|
|
|
#ifdef ANNOUNCEMENT_CONF_PERIOD
|
|
|
|
#define ANNOUNCEMENT_SCAN_TIME ANNOUNCEMENT_CONF_PERIOD
|
|
|
|
#else /* ANNOUNCEMENT_CONF_PERIOD */
|
2010-03-25 09:51:07 +01:00
|
|
|
#define ANNOUNCEMENT_SCAN_TIME CLOCK_SECOND
|
2010-03-29 23:52:25 +02:00
|
|
|
#endif /* ANNOUNCEMENT_CONF_PERIOD */
|
2010-03-25 09:51:07 +01:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
|
|
|
|
/* Statistics structure */
|
|
|
|
struct {
|
|
|
|
uint32_t foundroute;
|
|
|
|
uint32_t newparent;
|
|
|
|
uint32_t routelost;
|
|
|
|
|
|
|
|
uint32_t acksent;
|
|
|
|
uint32_t datasent;
|
|
|
|
|
|
|
|
uint32_t datarecv;
|
|
|
|
uint32_t ackrecv;
|
|
|
|
uint32_t badack;
|
|
|
|
uint32_t duprecv;
|
|
|
|
|
|
|
|
uint32_t qdrop;
|
|
|
|
uint32_t rtdrop;
|
|
|
|
uint32_t ttldrop;
|
|
|
|
uint32_t ackdrop;
|
|
|
|
uint32_t timedout;
|
|
|
|
} stats;
|
|
|
|
|
|
|
|
/* Debug definition: draw routing tree in Cooja. */
|
2010-10-05 00:36:47 +02:00
|
|
|
#define DRAW_TREE 0
|
2010-03-25 09:51:07 +01:00
|
|
|
#define DEBUG 0
|
2007-03-31 20:33:04 +02:00
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
2009-05-06 17:34:28 +02:00
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2007-03-31 20:33:04 +02:00
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
/* Forward declarations. */
|
|
|
|
static void send_queued_packet(struct collect_conn *c);
|
2010-02-28 10:18:01 +01:00
|
|
|
static void retransmit_callback(void *ptr);
|
2011-01-09 22:13:20 +01:00
|
|
|
static void retransmit_not_sent_callback(void *ptr);
|
2010-09-23 00:08:08 +02:00
|
|
|
static void set_keepalive_timer(struct collect_conn *c);
|
|
|
|
|
2007-03-31 20:33:04 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-08 21:21:45 +02:00
|
|
|
/**
|
|
|
|
* This function computes the current rtmetric by adding the last
|
|
|
|
* known rtmetric from our parent with the link estimate to the
|
|
|
|
* parent.
|
|
|
|
*
|
|
|
|
*/
|
2010-10-03 22:08:44 +02:00
|
|
|
static uint16_t
|
2010-09-08 21:21:45 +02:00
|
|
|
rtmetric_compute(struct collect_conn *tc)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-10-03 22:08:44 +02:00
|
|
|
uint16_t rtmetric = RTMETRIC_MAX;
|
2007-11-13 21:39:29 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* This function computes the current rtmetric for this node. It
|
|
|
|
uses the rtmetric of the parent node in the tree and adds the
|
|
|
|
current link estimate from us to the parent node. */
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* The collect connection structure stores the address of its
|
|
|
|
current parent. We look up the neighbor identification struct in
|
|
|
|
the collect-neighbor list. */
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
|
2010-06-14 08:54:47 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* If n is NULL, we have no best neighbor. Thus our rtmetric is
|
|
|
|
then COLLECT_RTMETRIC_MAX. */
|
|
|
|
if(n == NULL) {
|
|
|
|
rtmetric = RTMETRIC_MAX;
|
|
|
|
} else {
|
|
|
|
/* Our rtmetric is the rtmetric of our parent neighbor plus
|
|
|
|
the expected transmissions to reach that neighbor. */
|
2010-09-23 00:08:08 +02:00
|
|
|
rtmetric = collect_neighbor_rtmetric_link_estimate(n);
|
2010-09-08 21:21:45 +02:00
|
|
|
}
|
2007-11-13 21:39:29 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
return rtmetric;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-19 09:34:29 +02:00
|
|
|
/**
|
|
|
|
* This function is called when the route advertisements need to be
|
|
|
|
* transmitted more rapidly.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bump_advertisement(struct collect_conn *c)
|
|
|
|
{
|
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_start(&c->neighbor_discovery_conn, c->rtmetric);
|
|
|
|
#else
|
|
|
|
announcement_bump(&c->announcement);
|
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-08 21:21:45 +02:00
|
|
|
/**
|
|
|
|
* This function is called to update the current parent node. The
|
|
|
|
* parent may change if new routing information has been found, for
|
|
|
|
* example if a new node with a lower rtmetric and link estimate has
|
|
|
|
* appeared.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
update_parent(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
struct collect_neighbor *current;
|
|
|
|
struct collect_neighbor *best;
|
2007-11-13 21:39:29 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* We grab the collect_neighbor struct of our current parent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
current = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
|
2010-03-16 19:12:34 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* We call the collect_neighbor module to find the current best
|
|
|
|
parent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
best = collect_neighbor_list_best(&tc->neighbor_list);
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* We check if we need to switch parent. Switching parent is done in
|
|
|
|
the following situations:
|
|
|
|
|
|
|
|
* We do not have a current parent.
|
|
|
|
* The best parent is significantly better than the current parent.
|
|
|
|
|
|
|
|
If we do not have a current parent, and have found a best parent,
|
|
|
|
we simply use the new best parent.
|
|
|
|
|
|
|
|
If we already have a current parent, but have found a new parent
|
|
|
|
that is better, we employ a heuristic to avoid switching parents
|
|
|
|
too often. The new parent must be significantly better than the
|
|
|
|
current parent. Being "significantly better" is defined as having
|
|
|
|
an rtmetric that is has a difference of at least 1.5 times the
|
|
|
|
COLLECT_LINK_ESTIMATE_UNIT. This is derived from the experience
|
|
|
|
by Gnawali et al (SenSys 2009). */
|
|
|
|
|
|
|
|
if(best != NULL) {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t previous_parent;
|
2010-10-04 22:27:50 +02:00
|
|
|
|
|
|
|
if(DRAW_TREE) {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&previous_parent, &tc->parent);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
if(current == NULL) {
|
|
|
|
/* New parent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("update_parent: new parent %d.%d\n",
|
|
|
|
best->addr.u8[0], best->addr.u8[1]);
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&tc->parent, &best->addr);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.foundroute++;
|
2010-10-19 09:34:29 +02:00
|
|
|
bump_advertisement(tc);
|
2010-09-08 21:21:45 +02:00
|
|
|
} else {
|
2011-01-10 00:49:27 +01:00
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A e=%d\n", collect_neighbor_link_estimate(best));
|
2011-01-10 00:49:27 +01:00
|
|
|
}
|
2010-10-03 22:08:44 +02:00
|
|
|
if(collect_neighbor_rtmetric_link_estimate(best) +
|
|
|
|
SIGNIFICANT_RTMETRIC_PARENT_CHANGE <
|
2010-09-23 00:08:08 +02:00
|
|
|
collect_neighbor_rtmetric_link_estimate(current)) {
|
2010-10-24 23:08:19 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* We switch parent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("update_parent: new parent %d.%d (%d) old parent %d.%d (%d)\n",
|
2010-09-08 21:21:45 +02:00
|
|
|
best->addr.u8[0], best->addr.u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
collect_neighbor_rtmetric(best),
|
|
|
|
tc->parent.u8[0], tc->parent.u8[1],
|
|
|
|
collect_neighbor_rtmetric(current));
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&tc->parent, &best->addr);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.newparent++;
|
2010-10-19 09:34:29 +02:00
|
|
|
/* Since we now have a significantly better or worse rtmetric than
|
|
|
|
we had before, we let our neighbors know this quickly. */
|
|
|
|
bump_advertisement(tc);
|
|
|
|
|
2010-10-12 01:42:01 +02:00
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A e=%d\n", collect_neighbor_link_estimate(best));
|
2011-01-09 22:13:20 +01:00
|
|
|
/* {
|
2010-10-12 01:42:01 +02:00
|
|
|
int i;
|
|
|
|
int etx = 0;
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A l=");
|
2010-10-12 01:42:01 +02:00
|
|
|
for(i = 0; i < 8; i++) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("%d ", best->le.history[(best->le.historyptr - 1 - i) & 7]);
|
2010-10-12 01:42:01 +02:00
|
|
|
etx += current->le.history[i];
|
|
|
|
}
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("\n");
|
2011-01-09 22:13:20 +01:00
|
|
|
}*/
|
2010-10-12 01:42:01 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A e=%d\n", collect_neighbor_link_estimate(current));
|
2011-01-09 22:13:20 +01:00
|
|
|
/* {
|
2010-10-12 01:42:01 +02:00
|
|
|
int i;
|
|
|
|
int etx = 0;
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A l=");
|
2010-10-12 01:42:01 +02:00
|
|
|
for(i = 0; i < 8; i++) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("%d ", current->le.history[(current->le.historyptr - 1 - i) & 7]);
|
2010-10-12 01:42:01 +02:00
|
|
|
etx += current->le.history[i];
|
|
|
|
}
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("\n");
|
2011-01-09 22:13:20 +01:00
|
|
|
}*/
|
2010-10-12 01:42:01 +02:00
|
|
|
}
|
2007-05-15 10:09:21 +02:00
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
}
|
2010-10-04 22:27:50 +02:00
|
|
|
if(DRAW_TREE) {
|
2013-12-12 23:58:52 +01:00
|
|
|
if(!linkaddr_cmp(&previous_parent, &tc->parent)) {
|
|
|
|
if(!linkaddr_cmp(&previous_parent, &linkaddr_null)) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#L %d 0\n", previous_parent.u8[0]);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#L %d 1\n", tc->parent.u8[0]);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
} else {
|
|
|
|
/* No parent. */
|
2013-12-12 23:58:52 +01:00
|
|
|
if(!linkaddr_cmp(&tc->parent, &linkaddr_null)) {
|
2010-10-04 22:27:50 +02:00
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#L %d 0\n", tc->parent.u8[0]);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
2010-09-14 08:48:36 +02:00
|
|
|
stats.routelost++;
|
|
|
|
}
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&tc->parent, &linkaddr_null);
|
2010-09-08 21:21:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* This function is called whenever there is a chance that the routing
|
|
|
|
* metric has changed. The function goes through the list of neighbors
|
|
|
|
* to compute the new routing metric. If the metric has changed, it
|
|
|
|
* notifies neighbors.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
update_rtmetric(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
PRINTF("update_rtmetric: tc->rtmetric %d\n", tc->rtmetric);
|
|
|
|
|
|
|
|
/* We should only update the rtmetric if we are not the sink. */
|
|
|
|
if(tc->rtmetric != RTMETRIC_SINK) {
|
2010-10-03 22:08:44 +02:00
|
|
|
uint16_t old_rtmetric, new_rtmetric;
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* We remember the current (old) rtmetric for later. */
|
|
|
|
old_rtmetric = tc->rtmetric;
|
|
|
|
|
|
|
|
/* We may need to update our parent node so we do that now. */
|
|
|
|
update_parent(tc);
|
|
|
|
|
|
|
|
/* We compute the new rtmetric. */
|
|
|
|
new_rtmetric = rtmetric_compute(tc);
|
|
|
|
|
|
|
|
/* We sanity check our new rtmetric. */
|
|
|
|
if(new_rtmetric == RTMETRIC_SINK) {
|
|
|
|
/* Defensive programming: if the new rtmetric somehow got to be
|
|
|
|
the rtmetric of the sink, there is a bug somewhere. To avoid
|
|
|
|
destroying the network, we simply will not assume this new
|
|
|
|
rtmetric. Instead, we set our rtmetric to maximum, to
|
|
|
|
indicate that we have no sane route. */
|
|
|
|
new_rtmetric = RTMETRIC_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We set our new rtmetric in the collect conn structure. Then we
|
|
|
|
decide how we should announce this new rtmetric. */
|
|
|
|
tc->rtmetric = new_rtmetric;
|
|
|
|
|
|
|
|
if(tc->is_router) {
|
|
|
|
/* If we are a router, we update our advertised rtmetric. */
|
2010-02-23 19:35:23 +01:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2009-04-29 22:48:57 +02:00
|
|
|
announcement_set_value(&tc->announcement, tc->rtmetric);
|
2010-02-23 19:35:23 +01:00
|
|
|
#else /* COLLECT_ANNOUNCEMENTS */
|
|
|
|
neighbor_discovery_set_val(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2009-05-30 21:54:05 +02:00
|
|
|
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
PRINTF("%d.%d: new rtmetric %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-09-08 21:21:45 +02:00
|
|
|
tc->rtmetric);
|
2010-10-19 09:34:29 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* We got a new, working, route we send any queued packets we may have. */
|
|
|
|
if(old_rtmetric == RTMETRIC_MAX && new_rtmetric != RTMETRIC_MAX) {
|
|
|
|
PRINTF("Sending queued packet because rtmetric was max\n");
|
2010-09-13 15:28:14 +02:00
|
|
|
send_queued_packet(tc);
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2010-10-04 22:27:50 +02:00
|
|
|
if(DRAW_TREE) {
|
|
|
|
if(old_rtmetric != new_rtmetric) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A rt=%d,p=%d\n", tc->rtmetric, tc->parent.u8[0]);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
|
|
|
}
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 19:54:22 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-03 22:08:44 +02:00
|
|
|
static int
|
|
|
|
enqueue_dummy_packet(struct collect_conn *c, int rexmits)
|
|
|
|
{
|
|
|
|
struct collect_neighbor *n;
|
|
|
|
|
|
|
|
packetbuf_clear();
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, c->eseqno - 1);
|
2013-12-12 23:58:52 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &linkaddr_node_addr);
|
2010-10-03 22:08:44 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_TTL, 1);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, rexmits);
|
|
|
|
|
|
|
|
PRINTF("%d.%d: enqueueing dummy packet %d, max_rexmits %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-10-03 22:08:44 +02:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
|
|
|
|
|
|
|
|
/* Allocate space for the header. */
|
|
|
|
packetbuf_hdralloc(sizeof(struct data_msg_hdr));
|
|
|
|
|
|
|
|
n = collect_neighbor_list_find(&c->neighbor_list, &c->parent);
|
|
|
|
if(n != NULL) {
|
|
|
|
return packetqueue_enqueue_packetbuf(&c->send_queue,
|
2010-10-19 09:34:29 +02:00
|
|
|
FORWARD_PACKET_LIFETIME_BASE * rexmits,
|
2010-10-03 22:08:44 +02:00
|
|
|
c);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-24 23:08:19 +02:00
|
|
|
static void
|
|
|
|
send_packet(struct collect_conn *c, struct collect_neighbor *n)
|
|
|
|
{
|
|
|
|
clock_time_t time;
|
|
|
|
|
2011-01-09 22:13:20 +01:00
|
|
|
PRINTF("Sending packet to %d.%d, %d transmissions\n",
|
|
|
|
n->addr.u8[0], n->addr.u8[1],
|
|
|
|
c->transmissions);
|
2011-01-10 00:49:27 +01:00
|
|
|
/* Defensive programming: if a bug in the MAC/RDC layers will cause
|
|
|
|
it to not call us back, we'll set up the retransmission timer
|
|
|
|
with a high timeout, so that we can cancel the transmission and
|
|
|
|
send a new one. */
|
2011-01-09 22:13:20 +01:00
|
|
|
time = 16 * REXMIT_TIME;
|
|
|
|
ctimer_set(&c->retransmission_timer, time,
|
|
|
|
retransmit_not_sent_callback, c);
|
2010-12-16 23:45:15 +01:00
|
|
|
c->send_time = clock_time();
|
2011-01-10 00:49:27 +01:00
|
|
|
|
|
|
|
unicast_send(&c->unicast_conn, &n->addr);
|
2010-10-24 23:08:19 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-28 00:32:34 +01:00
|
|
|
static void
|
2010-10-28 17:36:02 +02:00
|
|
|
proactive_probing_callback(void *ptr)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-10-28 17:36:02 +02:00
|
|
|
struct collect_conn *c = ptr;
|
2010-02-28 10:18:01 +01:00
|
|
|
struct packetqueue_item *i;
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2010-10-28 17:36:02 +02:00
|
|
|
ctimer_set(&c->proactive_probing_timer, PROACTIVE_PROBING_INTERVAL,
|
|
|
|
proactive_probing_callback, ptr);
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2010-10-28 17:36:02 +02:00
|
|
|
/* Only do proactive link probing if we are not the sink and if we
|
|
|
|
have a route. */
|
|
|
|
if(c->rtmetric != RTMETRIC_SINK && c->rtmetric != RTMETRIC_MAX) {
|
|
|
|
/* Grab the first packet on the send queue to see if the queue is
|
|
|
|
empty or not. */
|
2010-09-13 15:28:14 +02:00
|
|
|
i = packetqueue_first(&c->send_queue);
|
2010-02-28 10:18:01 +01:00
|
|
|
if(i == NULL) {
|
2010-10-12 01:42:01 +02:00
|
|
|
/* If there are no packets to send, we go through the list of
|
|
|
|
neighbors to find a potential parent for which we do not have a
|
|
|
|
link estimate and send a dummy packet to it. This allows us to
|
2010-10-03 22:08:44 +02:00
|
|
|
quickly gauge the link quality of neighbors that we do not
|
|
|
|
currently use as parents. */
|
|
|
|
struct collect_neighbor *n;
|
|
|
|
|
2010-10-04 22:27:50 +02:00
|
|
|
/* Find the neighbor with the lowest number of estimates. */
|
2010-10-03 22:08:44 +02:00
|
|
|
for(n = list_head(collect_neighbor_list(&c->neighbor_list));
|
|
|
|
n != NULL; n = list_item_next(n)) {
|
|
|
|
if(n->rtmetric + COLLECT_LINK_ESTIMATE_UNIT < c->rtmetric &&
|
|
|
|
collect_link_estimate_num_estimates(&n->le) == 0) {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t current_parent;
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2010-10-28 17:36:02 +02:00
|
|
|
PRINTF("proactive_probing_callback: found neighbor with no link estimate, %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
n->addr.u8[LINKADDR_SIZE - 2], n->addr.u8[LINKADDR_SIZE - 1]);
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(¤t_parent, &c->parent);
|
|
|
|
linkaddr_copy(&c->parent, &n->addr);
|
2010-10-28 17:36:02 +02:00
|
|
|
if(enqueue_dummy_packet(c, PROACTIVE_PROBING_REXMITS)) {
|
2010-10-03 22:08:44 +02:00
|
|
|
send_queued_packet(c);
|
|
|
|
}
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&c->parent, ¤t_parent);
|
2010-10-04 22:27:50 +02:00
|
|
|
return;
|
2010-10-03 22:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-28 17:36:02 +02:00
|
|
|
PRINTF("%d.%d: nothing on queue\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-10-28 17:36:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* This function is called when a queued packet should be sent
|
|
|
|
* out. The function takes the first packet on the output queue, adds
|
|
|
|
* the necessary packet attributes, and sends the packet to the
|
|
|
|
* next-hop neighbor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
send_queued_packet(struct collect_conn *c)
|
|
|
|
{
|
|
|
|
struct queuebuf *q;
|
|
|
|
struct collect_neighbor *n;
|
|
|
|
struct packetqueue_item *i;
|
|
|
|
struct data_msg_hdr hdr;
|
|
|
|
int max_mac_rexmits;
|
|
|
|
|
|
|
|
/* If we are currently sending a packet, we do not attempt to send
|
|
|
|
another one. */
|
|
|
|
if(c->sending) {
|
|
|
|
PRINTF("%d.%d: queue, c is sending\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-10-28 17:36:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Grab the first packet on the send queue. */
|
|
|
|
i = packetqueue_first(&c->send_queue);
|
|
|
|
if(i == NULL) {
|
|
|
|
PRINTF("%d.%d: nothing on queue\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-10-28 17:36:02 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-30 09:33:51 +02:00
|
|
|
/* We should send the first packet from the queue. */
|
2010-02-28 10:18:01 +01:00
|
|
|
q = packetqueue_queuebuf(i);
|
|
|
|
if(q != NULL) {
|
2010-04-01 12:03:19 +02:00
|
|
|
/* Place the queued packet into the packetbuf. */
|
2010-02-28 10:18:01 +01:00
|
|
|
queuebuf_to_packetbuf(q);
|
2007-11-17 11:32:54 +01:00
|
|
|
|
2010-04-01 12:03:19 +02:00
|
|
|
/* Pick the neighbor to which to send the packet. We use the
|
|
|
|
parent in the n->parent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&c->neighbor_list, &c->parent);
|
2010-04-01 12:03:19 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(n != NULL) {
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* If the connection had a neighbor, we construct the packet
|
|
|
|
buffer attributes and set the appropriate flags in the
|
|
|
|
Collect connection structure and send the packet. */
|
2010-09-13 15:28:14 +02:00
|
|
|
|
2010-04-30 09:33:51 +02:00
|
|
|
PRINTF("%d.%d: sending packet to %d.%d with eseqno %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-04-30 09:33:51 +02:00
|
|
|
n->addr.u8[0], n->addr.u8[1],
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* Mark that we are currently sending a packet. */
|
2010-02-28 10:18:01 +01:00
|
|
|
c->sending = 1;
|
2010-09-08 21:21:45 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
/* Remember the parent that we sent this packet to. */
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&c->current_parent, &c->parent);
|
2010-09-23 00:08:08 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* This is the first time we transmit this packet, so set
|
|
|
|
transmissions to zero. */
|
2010-02-28 10:18:01 +01:00
|
|
|
c->transmissions = 0;
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* Remember that maximum amount of retransmissions we should
|
|
|
|
make. This is stored inside a packet attribute in the packet
|
|
|
|
on the send queue. */
|
2010-03-09 14:21:28 +01:00
|
|
|
c->max_rexmits = packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT);
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* Set the packet attributes: this packet wants an ACK, so we
|
|
|
|
sent the PACKETBUF_ATTR_RELIABLE flag; the MAC should retry
|
|
|
|
MAX_MAC_REXMITS times; and the PACKETBUF_ATTR_PACKET_ID is
|
|
|
|
set to the current sequence number on the connection. */
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
|
2010-09-13 15:28:14 +02:00
|
|
|
|
|
|
|
max_mac_rexmits = c->max_rexmits > MAX_MAC_REXMITS?
|
|
|
|
MAX_MAC_REXMITS : c->max_rexmits;
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, max_mac_rexmits);
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->seqno);
|
2010-09-08 21:21:45 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.datasent++;
|
|
|
|
|
|
|
|
/* Copy our rtmetric into the packet header of the outgoing
|
|
|
|
packet. */
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
hdr.rtmetric = c->rtmetric;
|
|
|
|
memcpy(packetbuf_dataptr(), &hdr, sizeof(struct data_msg_hdr));
|
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* Send the packet. */
|
2010-10-24 23:08:19 +02:00
|
|
|
send_packet(c, n);
|
2010-09-08 21:21:45 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
} else {
|
2010-03-16 19:12:34 +01:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2010-03-25 09:51:07 +01:00
|
|
|
#if COLLECT_CONF_WITH_LISTEN
|
2010-04-01 13:08:36 +02:00
|
|
|
PRINTF("listen\n");
|
2010-03-16 19:12:34 +01:00
|
|
|
announcement_listen(1);
|
2010-03-25 09:51:07 +01:00
|
|
|
ctimer_set(&c->transmit_after_scan_timer, ANNOUNCEMENT_SCAN_TIME,
|
2010-09-23 00:08:08 +02:00
|
|
|
send_queued_packet, c);
|
2010-03-25 09:51:07 +01:00
|
|
|
#else /* COLLECT_CONF_WITH_LISTEN */
|
2013-03-17 23:52:42 +01:00
|
|
|
if(c->is_router) {
|
|
|
|
announcement_set_value(&c->announcement, RTMETRIC_MAX);
|
|
|
|
announcement_bump(&c->announcement);
|
|
|
|
}
|
2010-03-25 09:51:07 +01:00
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2010-03-16 19:12:34 +01:00
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2007-11-17 11:32:54 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-08 21:21:45 +02:00
|
|
|
/**
|
2010-09-13 15:28:14 +02:00
|
|
|
* This function is called to retransmit the first packet on the send
|
|
|
|
* queue.
|
2010-09-08 21:21:45 +02:00
|
|
|
*
|
|
|
|
*/
|
2010-02-28 10:18:01 +01:00
|
|
|
static void
|
2010-09-13 15:28:14 +02:00
|
|
|
retransmit_current_packet(struct collect_conn *c)
|
|
|
|
{
|
|
|
|
struct queuebuf *q;
|
|
|
|
struct collect_neighbor *n;
|
|
|
|
struct packetqueue_item *i;
|
|
|
|
struct data_msg_hdr hdr;
|
|
|
|
int max_mac_rexmits;
|
|
|
|
|
|
|
|
/* Grab the first packet on the send queue, which is the one we are
|
|
|
|
about to retransmit. */
|
|
|
|
i = packetqueue_first(&c->send_queue);
|
|
|
|
if(i == NULL) {
|
|
|
|
PRINTF("%d.%d: nothing on queue\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-09-13 15:28:14 +02:00
|
|
|
/* No packet on the queue, so there is nothing for us to send. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get hold of the queuebuf. */
|
|
|
|
q = packetqueue_queuebuf(i);
|
|
|
|
if(q != NULL) {
|
2010-10-03 22:08:44 +02:00
|
|
|
|
|
|
|
update_rtmetric(c);
|
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
/* Place the queued packet into the packetbuf. */
|
|
|
|
queuebuf_to_packetbuf(q);
|
|
|
|
|
2010-10-03 22:08:44 +02:00
|
|
|
/* Pick the neighbor to which to send the packet. If we have found
|
|
|
|
a better parent while we were transmitting this packet, we
|
|
|
|
chose that neighbor instead. If so, we need to attribute the
|
|
|
|
transmissions we made for the parent to that neighbor. */
|
2013-12-12 23:58:52 +01:00
|
|
|
if(!linkaddr_cmp(&c->current_parent, &c->parent)) {
|
2010-10-03 22:08:44 +02:00
|
|
|
/* struct collect_neighbor *current_neighbor;
|
|
|
|
current_neighbor = collect_neighbor_list_find(&c->neighbor_list,
|
|
|
|
&c->current_parent);
|
|
|
|
if(current_neighbor != NULL) {
|
|
|
|
collect_neighbor_tx(current_neighbor, c->max_rexmits);
|
|
|
|
}*/
|
|
|
|
|
2010-10-04 22:27:50 +02:00
|
|
|
PRINTF("parent change from %d.%d to %d.%d after %d tx\n",
|
2010-10-03 22:08:44 +02:00
|
|
|
c->current_parent.u8[0], c->current_parent.u8[1],
|
|
|
|
c->parent.u8[0], c->parent.u8[1],
|
|
|
|
c->transmissions);
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&c->current_parent, &c->parent);
|
2010-10-03 22:08:44 +02:00
|
|
|
c->transmissions = 0;
|
|
|
|
}
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&c->neighbor_list, &c->current_parent);
|
|
|
|
|
|
|
|
if(n != NULL) {
|
|
|
|
|
|
|
|
/* If the connection had a neighbor, we construct the packet
|
|
|
|
buffer attributes and set the appropriate flags in the
|
|
|
|
Collect connection structure and send the packet. */
|
|
|
|
|
|
|
|
PRINTF("%d.%d: sending packet to %d.%d with eseqno %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
n->addr.u8[0], n->addr.u8[1],
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
|
|
|
|
|
|
|
|
/* Mark that we are currently sending a packet. */
|
|
|
|
c->sending = 1;
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
|
|
|
|
max_mac_rexmits = c->max_rexmits - c->transmissions > MAX_MAC_REXMITS?
|
|
|
|
MAX_MAC_REXMITS : c->max_rexmits - c->transmissions;
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, max_mac_rexmits);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, c->seqno);
|
|
|
|
|
|
|
|
/* Copy our rtmetric into the packet header of the outgoing
|
|
|
|
packet. */
|
|
|
|
memset(&hdr, 0, sizeof(hdr));
|
|
|
|
hdr.rtmetric = c->rtmetric;
|
|
|
|
memcpy(packetbuf_dataptr(), &hdr, sizeof(struct data_msg_hdr));
|
|
|
|
|
|
|
|
/* Send the packet. */
|
2010-10-24 23:08:19 +02:00
|
|
|
send_packet(c, n);
|
2010-09-13 15:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2010-02-28 10:18:01 +01:00
|
|
|
send_next_packet(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
/* Remove the first packet on the queue, the packet that was just sent. */
|
2010-09-13 15:28:14 +02:00
|
|
|
packetqueue_dequeue(&tc->send_queue);
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->seqno = (tc->seqno + 1) % (1 << COLLECT_PACKET_ID_BITS);
|
2011-01-10 16:10:43 +01:00
|
|
|
|
|
|
|
/* Cancel retransmission timer. */
|
|
|
|
ctimer_stop(&tc->retransmission_timer);
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->sending = 0;
|
|
|
|
tc->transmissions = 0;
|
2008-06-30 11:15:22 +02:00
|
|
|
|
2010-04-30 09:33:51 +02:00
|
|
|
PRINTF("sending next packet, seqno %d, queue len %d\n",
|
2010-09-13 15:28:14 +02:00
|
|
|
tc->seqno, packetqueue_len(&tc->send_queue));
|
2010-09-08 21:21:45 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* Send the next packet in the queue, if any. */
|
2010-09-13 15:28:14 +02:00
|
|
|
send_queued_packet(tc);
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_ack(struct collect_conn *tc)
|
|
|
|
{
|
2012-03-31 01:25:27 +02:00
|
|
|
struct ack_msg msg;
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("handle_ack: sender %d.%d current_parent %d.%d, id %d seqno %d\n",
|
2010-04-30 09:33:51 +02:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
tc->current_parent.u8[0], tc->current_parent.u8[1],
|
2010-04-30 09:33:51 +02:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_ID), tc->seqno);
|
2013-12-12 23:58:52 +01:00
|
|
|
if(linkaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
2010-09-13 15:28:14 +02:00
|
|
|
&tc->current_parent) &&
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_ID) == tc->seqno) {
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2013-05-15 11:12:05 +02:00
|
|
|
/* PRINTF("rtt %d / %d = %d.%02d\n",
|
2010-12-16 23:45:15 +01:00
|
|
|
(int)(clock_time() - tc->send_time),
|
|
|
|
(int)CLOCK_SECOND,
|
|
|
|
(int)((clock_time() - tc->send_time) / CLOCK_SECOND),
|
|
|
|
(int)(((100 * (clock_time() - tc->send_time)) / CLOCK_SECOND) % 100));*/
|
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.ackrecv++;
|
2012-03-31 01:25:27 +02:00
|
|
|
memcpy(&msg, packetbuf_dataptr(), sizeof(struct ack_msg));
|
2010-09-13 15:28:14 +02:00
|
|
|
|
|
|
|
/* It is possible that we receive an ACK for a packet that we
|
|
|
|
think we have not yet sent: if our transmission was received by
|
|
|
|
the other node, but the link-layer ACK was lost, our
|
|
|
|
transmission counter may still be zero. If this is the case, we
|
|
|
|
play it safe by believing that we have sent MAX_MAC_REXMITS
|
|
|
|
transmissions. */
|
|
|
|
if(tc->transmissions == 0) {
|
|
|
|
tc->transmissions = MAX_MAC_REXMITS;
|
|
|
|
}
|
|
|
|
PRINTF("Updating link estimate with %d transmissions\n",
|
|
|
|
tc->transmissions);
|
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(n != NULL) {
|
2010-09-23 00:08:08 +02:00
|
|
|
collect_neighbor_tx(n, tc->transmissions);
|
2012-03-31 01:25:27 +02:00
|
|
|
collect_neighbor_update_rtmetric(n, msg.rtmetric);
|
2010-02-28 10:18:01 +01:00
|
|
|
update_rtmetric(tc);
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2010-09-23 00:08:08 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("%d.%d: ACK from %d.%d after %d transmissions, flags %02x, rtmetric %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
tc->current_parent.u8[0], tc->current_parent.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->transmissions,
|
2012-03-31 01:25:27 +02:00
|
|
|
msg.flags,
|
|
|
|
msg.rtmetric);
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2010-10-04 22:27:50 +02:00
|
|
|
/* The ack contains information about the state of the packet and
|
|
|
|
of the node that received it. We do different things depending
|
|
|
|
on whether or not the packet was dropped. First, we check if
|
|
|
|
the receiving node was congested. If so, we add a maximum
|
|
|
|
transmission number to its routing metric, which increases the
|
|
|
|
chance that another parent will be chosen. */
|
2012-03-31 01:25:27 +02:00
|
|
|
if(msg.flags & ACK_FLAGS_CONGESTED) {
|
2011-01-10 16:10:43 +01:00
|
|
|
PRINTF("ACK flag indicated parent was congested.\n");
|
2012-11-22 11:55:01 +01:00
|
|
|
if(n != NULL) {
|
|
|
|
collect_neighbor_set_congested(n);
|
|
|
|
collect_neighbor_tx(n, tc->max_rexmits * 2);
|
|
|
|
}
|
2010-10-04 22:27:50 +02:00
|
|
|
update_rtmetric(tc);
|
|
|
|
}
|
2012-03-31 01:25:27 +02:00
|
|
|
if((msg.flags & ACK_FLAGS_DROPPED) == 0) {
|
2010-10-04 22:27:50 +02:00
|
|
|
/* If the packet was successfully received, we send the next packet. */
|
2010-02-28 10:18:01 +01:00
|
|
|
send_next_packet(tc);
|
2010-10-03 22:08:44 +02:00
|
|
|
} else {
|
2010-10-04 22:27:50 +02:00
|
|
|
/* If the packet was lost due to its lifetime being exceeded,
|
|
|
|
there is not much more we can do with the packet, so we send
|
|
|
|
the next one instead. */
|
2012-03-31 01:25:27 +02:00
|
|
|
if((msg.flags & ACK_FLAGS_LIFETIME_EXCEEDED)) {
|
2010-10-04 22:27:50 +02:00
|
|
|
send_next_packet(tc);
|
|
|
|
} else {
|
|
|
|
/* If the packet was dropped, but without the node being
|
|
|
|
congested or the packets lifetime being exceeded, we
|
|
|
|
penalize the parent and try sending the packet again. */
|
2011-01-10 16:10:43 +01:00
|
|
|
PRINTF("ACK flag indicated packet was dropped by parent.\n");
|
2010-10-04 22:27:50 +02:00
|
|
|
collect_neighbor_tx(n, tc->max_rexmits);
|
|
|
|
update_rtmetric(tc);
|
2011-01-10 16:10:43 +01:00
|
|
|
|
|
|
|
ctimer_set(&tc->retransmission_timer,
|
|
|
|
REXMIT_TIME + (random_rand() % (REXMIT_TIME)),
|
|
|
|
retransmit_callback, tc);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
2010-10-12 01:42:01 +02:00
|
|
|
|
|
|
|
/* Our neighbor's rtmetric needs to be updated, so we bump our
|
|
|
|
advertisements. */
|
2012-03-31 01:25:27 +02:00
|
|
|
if(msg.flags & ACK_FLAGS_RTMETRIC_NEEDS_UPDATE) {
|
2010-09-13 15:28:14 +02:00
|
|
|
bump_advertisement(tc);
|
|
|
|
}
|
2010-09-23 00:08:08 +02:00
|
|
|
set_keepalive_timer(tc);
|
2010-09-13 15:28:14 +02:00
|
|
|
} else {
|
|
|
|
stats.badack++;
|
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
send_ack(struct collect_conn *tc, const linkaddr_t *to, int flags)
|
2010-09-13 15:28:14 +02:00
|
|
|
{
|
|
|
|
struct ack_msg *ack;
|
2010-09-23 00:08:08 +02:00
|
|
|
uint16_t packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
2010-10-03 22:08:44 +02:00
|
|
|
|
2010-09-23 00:08:08 +02:00
|
|
|
packetbuf_clear();
|
|
|
|
packetbuf_set_datalen(sizeof(struct ack_msg));
|
|
|
|
ack = packetbuf_dataptr();
|
|
|
|
memset(ack, 0, sizeof(struct ack_msg));
|
|
|
|
ack->rtmetric = tc->rtmetric;
|
|
|
|
ack->flags = flags;
|
|
|
|
|
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_RECEIVER, to);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_ACK);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 0);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_ERELIABLE, 0);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_ID, packet_seqno);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, MAX_ACK_MAC_REXMITS);
|
|
|
|
unicast_send(&tc->unicast_conn, to);
|
|
|
|
|
|
|
|
PRINTF("%d.%d: collect: Sending ACK to %d.%d for %d (epacket_id %d)\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1],
|
2010-10-03 22:08:44 +02:00
|
|
|
to->u8[0], to->u8[1], packet_seqno,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
|
2010-09-23 00:08:08 +02:00
|
|
|
|
|
|
|
RIMESTATS_ADD(acktx);
|
|
|
|
stats.acksent++;
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2011-01-10 16:10:43 +01:00
|
|
|
add_packet_to_recent_packets(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
/* Remember that we have seen this packet for later, but only if
|
|
|
|
it has a length that is larger than zero. Packets with size
|
|
|
|
zero are keepalive or proactive link estimate probes, so we do
|
|
|
|
not record them in our history. */
|
|
|
|
if(packetbuf_datalen() > sizeof(struct data_msg_hdr)) {
|
|
|
|
recent_packets[recent_packet_ptr].eseqno =
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&recent_packets[recent_packet_ptr].originator,
|
2011-01-10 16:10:43 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER));
|
|
|
|
recent_packets[recent_packet_ptr].conn = tc;
|
|
|
|
recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
node_packet_received(struct unicast_conn *c, const linkaddr_t *from)
|
2010-02-28 10:18:01 +01:00
|
|
|
{
|
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
|
|
|
((char *)c - offsetof(struct collect_conn, unicast_conn));
|
|
|
|
int i;
|
2010-09-13 15:28:14 +02:00
|
|
|
struct data_msg_hdr hdr;
|
|
|
|
uint8_t ackflags = 0;
|
|
|
|
struct collect_neighbor *n;
|
2010-10-12 01:42:01 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
memcpy(&hdr, packetbuf_dataptr(), sizeof(struct data_msg_hdr));
|
|
|
|
|
|
|
|
/* First update the neighbors rtmetric with the information in the
|
|
|
|
packet header. */
|
|
|
|
PRINTF("node_packet_received: from %d.%d rtmetric %d\n",
|
|
|
|
from->u8[0], from->u8[1], hdr.rtmetric);
|
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
if(n != NULL) {
|
|
|
|
collect_neighbor_update_rtmetric(n, hdr.rtmetric);
|
|
|
|
update_rtmetric(tc);
|
|
|
|
}
|
2007-03-31 20:33:04 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* To protect against sending duplicate packets, we keep a list of
|
|
|
|
recently forwarded packet seqnos. If the seqno of the current
|
|
|
|
packet exists in the list, we immediately send an ACK and drop
|
|
|
|
the packet. */
|
2010-02-28 10:18:01 +01:00
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_DATA) {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t ack_to;
|
2010-02-28 10:18:01 +01:00
|
|
|
uint8_t packet_seqno;
|
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.datarecv++;
|
2011-01-10 16:10:43 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* Remember to whom we should send the ACK, since we reuse the
|
|
|
|
packet buffer and its attributes when sending the ACK. */
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&ack_to, packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
2010-02-28 10:18:01 +01:00
|
|
|
packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
|
|
|
|
2010-10-12 01:42:01 +02:00
|
|
|
/* If the queue is more than half filled, we add the CONGESTED
|
|
|
|
flag to our outgoing acks. */
|
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A s=%d\n", packetqueue_len(&tc->send_queue));
|
2010-10-12 01:42:01 +02:00
|
|
|
}
|
|
|
|
if(packetqueue_len(&tc->send_queue) >= MAX_SENDING_QUEUE / 2) {
|
|
|
|
ackflags |= ACK_FLAGS_CONGESTED;
|
|
|
|
}
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
for(i = 0; i < NUM_RECENT_PACKETS; i++) {
|
2010-09-08 21:21:45 +02:00
|
|
|
if(recent_packets[i].conn == tc &&
|
2010-09-13 15:28:14 +02:00
|
|
|
recent_packets[i].eseqno == packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID) &&
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_cmp(&recent_packets[i].originator,
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER))) {
|
2010-09-08 21:21:45 +02:00
|
|
|
/* This is a duplicate of a packet we recently received, so we
|
|
|
|
just send an ACK. */
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: found duplicate packet from %d.%d with seqno %d, via %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
recent_packets[i].originator.u8[0], recent_packets[i].originator.u8[1],
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
|
2010-10-12 01:42:01 +02:00
|
|
|
send_ack(tc, &ack_to, ackflags);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.duprecv++;
|
2010-09-08 21:21:45 +02:00
|
|
|
return;
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
|
|
|
|
/* If we are the sink, the packet has reached its final
|
|
|
|
destination and we call the receive function. */
|
|
|
|
if(tc->rtmetric == RTMETRIC_SINK) {
|
2010-09-23 00:08:08 +02:00
|
|
|
struct queuebuf *q;
|
|
|
|
|
2011-01-10 16:10:43 +01:00
|
|
|
add_packet_to_recent_packets(tc);
|
|
|
|
|
2010-09-23 00:08:08 +02:00
|
|
|
/* We first send the ACK. We copy the data packet to a queuebuf
|
|
|
|
first. */
|
|
|
|
q = queuebuf_new_from_packetbuf();
|
|
|
|
if(q != NULL) {
|
|
|
|
send_ack(tc, &ack_to, 0);
|
|
|
|
queuebuf_to_packetbuf(q);
|
|
|
|
queuebuf_free(q);
|
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: collect: could not send ACK to %d.%d for %d: no queued buffers\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1],
|
2010-09-23 00:08:08 +02:00
|
|
|
ack_to.u8[0], ack_to.u8[1],
|
|
|
|
packet_seqno);
|
|
|
|
stats.ackdrop++;
|
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: sink received packet %d from %d.%d via %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
|
|
|
|
from->u8[0], from->u8[1]);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
packetbuf_hdrreduce(sizeof(struct data_msg_hdr));
|
2010-09-08 21:21:45 +02:00
|
|
|
/* Call receive function. */
|
2010-10-07 17:52:28 +02:00
|
|
|
if(packetbuf_datalen() > 0 && tc->cb->recv != NULL) {
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->cb->recv(packetbuf_addr(PACKETBUF_ADDR_ESENDER),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_HOPS));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_TTL) > 1 &&
|
|
|
|
tc->rtmetric != RTMETRIC_MAX) {
|
2010-09-08 21:21:45 +02:00
|
|
|
/* If we are not the sink, we forward the packet to our best
|
2010-09-13 15:28:14 +02:00
|
|
|
neighbor. First, we make sure that the packet comes from a
|
|
|
|
neighbor that has a higher rtmetric than we have. If not, we
|
|
|
|
have a loop and we inform the sender that its rtmetric needs
|
|
|
|
to be updated. Second, we set our rtmetric in the outgoing
|
|
|
|
packet to let the next hop know what our rtmetric is. Third,
|
|
|
|
we update the hop count and ttl. */
|
|
|
|
|
|
|
|
if(hdr.rtmetric <= tc->rtmetric) {
|
|
|
|
ackflags |= ACK_FLAGS_RTMETRIC_NEEDS_UPDATE;
|
|
|
|
}
|
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_HOPS,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_HOPS) + 1);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_TTL,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_TTL) - 1);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
|
2010-03-16 19:12:34 +01:00
|
|
|
PRINTF("%d.%d: packet received from %d.%d via %d.%d, sending %d, max_rexmits %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
|
2010-03-16 19:12:34 +01:00
|
|
|
from->u8[0], from->u8[1], tc->sending,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
/* We try to enqueue the packet on the outgoing packet queue. If
|
|
|
|
we are able to enqueue the packet, we send a positive ACK. If
|
|
|
|
we are unable to enqueue the packet, we send a negative ACK
|
|
|
|
to inform the sender that the packet was dropped due to
|
2011-01-10 16:10:43 +01:00
|
|
|
memory problems. We first check the size of our sending queue
|
|
|
|
to ensure that we always have entries for packets that
|
|
|
|
are originated by this node. */
|
|
|
|
if(packetqueue_len(&tc->send_queue) <= MAX_SENDING_QUEUE - MIN_AVAILABLE_QUEUE_ENTRIES &&
|
|
|
|
packetqueue_enqueue_packetbuf(&tc->send_queue,
|
2010-10-19 09:34:29 +02:00
|
|
|
FORWARD_PACKET_LIFETIME_BASE *
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
|
|
|
|
tc)) {
|
2011-01-10 16:10:43 +01:00
|
|
|
add_packet_to_recent_packets(tc);
|
2010-09-13 15:28:14 +02:00
|
|
|
send_ack(tc, &ack_to, ackflags);
|
|
|
|
send_queued_packet(tc);
|
2010-02-28 10:18:01 +01:00
|
|
|
} else {
|
2010-09-23 00:08:08 +02:00
|
|
|
send_ack(tc, &ack_to,
|
|
|
|
ackflags | ACK_FLAGS_DROPPED | ACK_FLAGS_CONGESTED);
|
2010-12-16 23:45:15 +01:00
|
|
|
PRINTF("%d.%d: packet dropped: no queue buffer available\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.qdrop++;
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_TTL) <= 1) {
|
2010-02-23 19:35:23 +01:00
|
|
|
PRINTF("%d.%d: packet dropped: ttl %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-02-23 19:35:23 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_TTL));
|
2010-09-13 15:28:14 +02:00
|
|
|
send_ack(tc, &ack_to, ackflags |
|
|
|
|
ACK_FLAGS_DROPPED | ACK_FLAGS_LIFETIME_EXCEEDED);
|
|
|
|
stats.ttldrop++;
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_ACK) {
|
|
|
|
PRINTF("Collect: incoming ack %d from %d.%d (%d.%d) seqno %d (%d)\n",
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE),
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
tc->current_parent.u8[0],
|
|
|
|
tc->current_parent.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
|
|
|
|
tc->seqno);
|
|
|
|
handle_ack(tc);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.ackrecv++;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2007-11-28 00:32:34 +01:00
|
|
|
return;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2010-10-24 23:08:19 +02:00
|
|
|
timedout(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
struct collect_neighbor *n;
|
2010-12-16 23:45:15 +01:00
|
|
|
PRINTF("%d.%d: timedout after %d retransmissions to %d.%d (max retransmissions %d): packet dropped\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], tc->transmissions,
|
2010-10-24 23:08:19 +02:00
|
|
|
tc->current_parent.u8[0], tc->current_parent.u8[1],
|
2010-12-16 23:45:15 +01:00
|
|
|
tc->max_rexmits);
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("%d.%d: timedout after %d retransmissions to %d.%d (max retransmissions %d): packet dropped\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], tc->transmissions,
|
2011-01-10 16:10:43 +01:00
|
|
|
tc->current_parent.u8[0], tc->current_parent.u8[1],
|
|
|
|
tc->max_rexmits);
|
2010-10-24 23:08:19 +02:00
|
|
|
|
|
|
|
tc->sending = 0;
|
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list,
|
|
|
|
&tc->current_parent);
|
|
|
|
if(n != NULL) {
|
|
|
|
collect_neighbor_tx_fail(n, tc->max_rexmits);
|
|
|
|
}
|
|
|
|
update_rtmetric(tc);
|
|
|
|
send_next_packet(tc);
|
|
|
|
set_keepalive_timer(tc);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2010-02-28 10:18:01 +01:00
|
|
|
node_packet_sent(struct unicast_conn *c, int status, int transmissions)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2007-11-28 17:04:41 +01:00
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
2010-02-28 10:18:01 +01:00
|
|
|
((char *)c - offsetof(struct collect_conn, unicast_conn));
|
|
|
|
|
|
|
|
/* For data packets, we record the number of transmissions */
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
2010-10-03 22:08:44 +02:00
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_DATA) {
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->transmissions += transmissions;
|
2011-01-10 00:49:27 +01:00
|
|
|
PRINTF("tx %d\n", tc->transmissions);
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("%d.%d: MAC sent %d transmissions to %d.%d, status %d, total transmissions %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-09-13 15:28:14 +02:00
|
|
|
transmissions,
|
|
|
|
tc->current_parent.u8[0], tc->current_parent.u8[1],
|
|
|
|
status, tc->transmissions);
|
2010-10-24 23:08:19 +02:00
|
|
|
if(tc->transmissions >= tc->max_rexmits) {
|
|
|
|
timedout(tc);
|
|
|
|
stats.timedout++;
|
|
|
|
} else {
|
2010-12-16 23:45:15 +01:00
|
|
|
clock_time_t time = REXMIT_TIME / 2 + (random_rand() % (REXMIT_TIME / 2));
|
2011-01-10 00:49:27 +01:00
|
|
|
PRINTF("retransmission time %lu\n", time);
|
2010-10-24 23:08:19 +02:00
|
|
|
ctimer_set(&tc->retransmission_timer, time,
|
|
|
|
retransmit_callback, tc);
|
2010-09-13 15:28:14 +02:00
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-24 23:08:19 +02:00
|
|
|
/**
|
|
|
|
* This function is called from a ctimer that is setup when a packet
|
|
|
|
* is first transmitted. If the MAC layer signals that the packet is
|
|
|
|
* sent, the ctimer will be stopped before this function is called. If
|
|
|
|
* this function ends up being called, we add the maximum number of
|
|
|
|
* MAC layer transmissions to the transmission count, and call the
|
|
|
|
* retransmit function.
|
|
|
|
*/
|
2007-05-15 10:09:21 +02:00
|
|
|
static void
|
2010-10-24 23:08:19 +02:00
|
|
|
retransmit_not_sent_callback(void *ptr)
|
2007-05-15 10:09:21 +02:00
|
|
|
{
|
2010-10-24 23:08:19 +02:00
|
|
|
struct collect_conn *c = ptr;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-10-24 23:08:19 +02:00
|
|
|
PRINTF("retransmit not sent, %d transmissions\n", c->transmissions);
|
|
|
|
c->transmissions += MAX_MAC_REXMITS + 1;
|
|
|
|
retransmit_callback(c);
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-10-03 22:08:44 +02:00
|
|
|
/**
|
|
|
|
* This function is called from a ctimer that is setup when a packet
|
|
|
|
* is sent. The purpose of this function is to either retransmit the
|
|
|
|
* current packet, or timeout the packet. The descision is made
|
|
|
|
* depending on how many times the packet has been transmitted. The
|
|
|
|
* ctimer is set up in the function node_packet_sent().
|
|
|
|
*/
|
2010-02-28 10:18:01 +01:00
|
|
|
static void
|
|
|
|
retransmit_callback(void *ptr)
|
|
|
|
{
|
|
|
|
struct collect_conn *c = ptr;
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
PRINTF("retransmit, %d transmissions\n", c->transmissions);
|
2010-02-28 10:18:01 +01:00
|
|
|
if(c->transmissions >= c->max_rexmits) {
|
|
|
|
timedout(c);
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.timedout++;
|
2010-02-28 10:18:01 +01:00
|
|
|
} else {
|
|
|
|
c->sending = 0;
|
2010-09-13 15:28:14 +02:00
|
|
|
retransmit_current_packet(c);
|
2010-02-28 10:18:01 +01:00
|
|
|
}
|
2007-05-15 10:09:21 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
adv_received(struct neighbor_discovery_conn *c, const linkaddr_t *from,
|
2009-11-08 20:40:16 +01:00
|
|
|
uint16_t rtmetric)
|
2007-11-13 21:39:29 +01:00
|
|
|
{
|
2007-11-28 17:04:41 +01:00
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
2007-12-09 16:43:09 +01:00
|
|
|
((char *)c - offsetof(struct collect_conn, neighbor_discovery_conn));
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list, from);
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
if(n == NULL) {
|
2010-09-13 15:28:14 +02:00
|
|
|
collect_neighbor_list_add(&tc->neighbor_list, from, rtmetric);
|
2010-09-23 00:08:08 +02:00
|
|
|
if(rtmetric == RTMETRIC_MAX) {
|
|
|
|
bump_advertisement(tc);
|
|
|
|
}
|
2007-11-13 21:39:29 +01:00
|
|
|
} else {
|
2010-09-23 00:08:08 +02:00
|
|
|
/* Check if the advertised rtmetric has changed to
|
|
|
|
RTMETRIC_MAX. This may indicate that the neighbor has lost its
|
|
|
|
routes or that it has rebooted. In either case, we bump our
|
|
|
|
advertisement rate to allow our neighbor to receive a new
|
|
|
|
rtmetric from us. If our neighbor already happens to have an
|
|
|
|
rtmetric of RTMETRIC_MAX recorded, it may mean that our
|
|
|
|
neighbor does not hear our advertisements. If this is the case,
|
|
|
|
we should not bump our advertisement rate. */
|
|
|
|
if(rtmetric == RTMETRIC_MAX &&
|
|
|
|
collect_neighbor_rtmetric(n) != RTMETRIC_MAX) {
|
|
|
|
bump_advertisement(tc);
|
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
collect_neighbor_update_rtmetric(n, rtmetric);
|
2009-02-16 02:18:53 +01:00
|
|
|
PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2009-02-16 02:18:53 +01:00
|
|
|
n->addr.u8[0], n->addr.u8[1], rtmetric);
|
2007-11-13 21:39:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
update_rtmetric(tc);
|
2009-05-30 21:54:05 +02:00
|
|
|
}
|
|
|
|
#else
|
2009-02-09 21:58:25 +01:00
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
received_announcement(struct announcement *a, const linkaddr_t *from,
|
2009-02-09 21:58:25 +01:00
|
|
|
uint16_t id, uint16_t value)
|
|
|
|
{
|
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
|
|
|
((char *)a - offsetof(struct collect_conn, announcement));
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list, from);
|
2009-02-09 21:58:25 +01:00
|
|
|
|
|
|
|
if(n == NULL) {
|
2010-10-03 22:08:44 +02:00
|
|
|
/* Only add neighbors that have an rtmetric that is lower than
|
|
|
|
ours. */
|
|
|
|
if(value < tc->rtmetric) {
|
|
|
|
collect_neighbor_list_add(&tc->neighbor_list, from, value);
|
|
|
|
PRINTF("%d.%d: new neighbor %d.%d, rtmetric %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-10-03 22:08:44 +02:00
|
|
|
from->u8[0], from->u8[1], value);
|
|
|
|
}
|
2010-10-19 09:34:29 +02:00
|
|
|
if(value == RTMETRIC_MAX && tc->rtmetric != RTMETRIC_MAX) {
|
2010-09-23 00:08:08 +02:00
|
|
|
bump_advertisement(tc);
|
|
|
|
}
|
2009-02-09 21:58:25 +01:00
|
|
|
} else {
|
2010-09-23 00:08:08 +02:00
|
|
|
/* Check if the advertised rtmetric has changed to
|
|
|
|
RTMETRIC_MAX. This may indicate that the neighbor has lost its
|
|
|
|
routes or that it has rebooted. In either case, we bump our
|
|
|
|
advertisement rate to allow our neighbor to receive a new
|
|
|
|
rtmetric from us. If our neighbor already happens to have an
|
|
|
|
rtmetric of RTMETRIC_MAX recorded, it may mean that our
|
|
|
|
neighbor does not hear our advertisements. If this is the case,
|
|
|
|
we should not bump our advertisement rate. */
|
|
|
|
if(value == RTMETRIC_MAX &&
|
|
|
|
collect_neighbor_rtmetric(n) != RTMETRIC_MAX) {
|
|
|
|
bump_advertisement(tc);
|
2010-10-12 01:42:01 +02:00
|
|
|
}
|
2010-09-08 21:21:45 +02:00
|
|
|
collect_neighbor_update_rtmetric(n, value);
|
2009-02-16 02:18:53 +01:00
|
|
|
PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2009-02-16 02:18:53 +01:00
|
|
|
n->addr.u8[0], n->addr.u8[1], value);
|
2009-02-09 21:58:25 +01:00
|
|
|
}
|
|
|
|
|
2010-03-25 09:51:07 +01:00
|
|
|
update_rtmetric(tc);
|
|
|
|
|
|
|
|
#if ! COLLECT_CONF_WITH_LISTEN
|
|
|
|
if(value == RTMETRIC_MAX &&
|
|
|
|
tc->rtmetric != RTMETRIC_MAX) {
|
2013-03-17 23:52:42 +01:00
|
|
|
if(tc->is_router) {
|
|
|
|
announcement_bump(&tc->announcement);
|
|
|
|
}
|
2010-03-25 09:51:07 +01:00
|
|
|
}
|
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2007-11-13 21:39:29 +01:00
|
|
|
}
|
2009-05-30 21:54:05 +02:00
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
2007-11-13 21:39:29 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-02-28 10:18:01 +01:00
|
|
|
static const struct unicast_callbacks unicast_callbacks = {node_packet_received,
|
|
|
|
node_packet_sent};
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
static const struct neighbor_discovery_callbacks neighbor_discovery_callbacks =
|
|
|
|
{ adv_received, NULL};
|
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
2007-03-15 21:04:30 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2008-02-24 22:08:37 +01:00
|
|
|
collect_open(struct collect_conn *tc, uint16_t channels,
|
2010-03-25 09:51:07 +01:00
|
|
|
uint8_t is_router,
|
2007-11-28 17:04:41 +01:00
|
|
|
const struct collect_callbacks *cb)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-03-09 14:21:28 +01:00
|
|
|
unicast_open(&tc->unicast_conn, channels + 1, &unicast_callbacks);
|
|
|
|
channel_set_attributes(channels + 1, attributes);
|
|
|
|
tc->rtmetric = RTMETRIC_MAX;
|
|
|
|
tc->cb = cb;
|
2010-03-25 09:51:07 +01:00
|
|
|
tc->is_router = is_router;
|
2010-04-30 09:33:51 +02:00
|
|
|
tc->seqno = 10;
|
2010-10-07 17:52:28 +02:00
|
|
|
tc->eseqno = 0;
|
2010-09-13 15:28:14 +02:00
|
|
|
LIST_STRUCT_INIT(tc, send_queue_list);
|
|
|
|
collect_neighbor_list_new(&tc->neighbor_list);
|
|
|
|
tc->send_queue.list = &(tc->send_queue_list);
|
|
|
|
tc->send_queue.memb = &send_queue_memb;
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_init();
|
2010-03-09 14:21:28 +01:00
|
|
|
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_open(&tc->neighbor_discovery_conn, channels,
|
2010-09-08 21:21:45 +02:00
|
|
|
CLOCK_SECOND * 4,
|
|
|
|
CLOCK_SECOND * 60,
|
2010-12-14 23:14:33 +01:00
|
|
|
#ifdef COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME
|
|
|
|
COLLECT_CONF_BROADCAST_ANNOUNCEMENT_MAX_TIME,
|
|
|
|
#else
|
|
|
|
CLOCK_SECOND * 600UL,
|
|
|
|
#endif
|
2009-05-30 21:54:05 +02:00
|
|
|
&neighbor_discovery_callbacks);
|
2010-02-28 10:18:01 +01:00
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#else /* !COLLECT_ANNOUNCEMENTS */
|
2010-03-25 09:51:07 +01:00
|
|
|
announcement_register(&tc->announcement, channels,
|
2010-02-28 10:18:01 +01:00
|
|
|
received_announcement);
|
2010-03-25 09:51:07 +01:00
|
|
|
#if ! COLLECT_CONF_WITH_LISTEN
|
2013-03-17 23:52:42 +01:00
|
|
|
if(tc->is_router) {
|
|
|
|
announcement_set_value(&tc->announcement, RTMETRIC_MAX);
|
|
|
|
}
|
2010-03-25 09:51:07 +01:00
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2009-05-30 21:54:05 +02:00
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
2010-10-28 17:36:02 +02:00
|
|
|
|
|
|
|
ctimer_set(&tc->proactive_probing_timer, PROACTIVE_PROBING_INTERVAL,
|
|
|
|
proactive_probing_callback, tc);
|
2011-01-10 16:10:43 +01:00
|
|
|
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-23 00:08:08 +02:00
|
|
|
static void
|
|
|
|
send_keepalive(void *ptr)
|
|
|
|
{
|
|
|
|
struct collect_conn *c = ptr;
|
|
|
|
|
|
|
|
set_keepalive_timer(c);
|
|
|
|
|
|
|
|
/* Send keepalive message only if there are no pending transmissions. */
|
2010-10-03 22:08:44 +02:00
|
|
|
if(c->sending == 0 && packetqueue_len(&c->send_queue) == 0) {
|
|
|
|
if(enqueue_dummy_packet(c, KEEPALIVE_REXMITS)) {
|
|
|
|
PRINTF("%d.%d: sending keepalive\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-10-03 22:08:44 +02:00
|
|
|
send_queued_packet(c);
|
2010-09-23 00:08:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
set_keepalive_timer(struct collect_conn *c)
|
|
|
|
{
|
|
|
|
if(c->keepalive_period != 0) {
|
|
|
|
ctimer_set(&c->keepalive_timer, (c->keepalive_period / 2) +
|
|
|
|
(random_rand() % (c->keepalive_period / 2)),
|
|
|
|
send_keepalive, c);
|
|
|
|
} else {
|
|
|
|
ctimer_stop(&c->keepalive_timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
collect_set_keepalive(struct collect_conn *c, clock_time_t period)
|
|
|
|
{
|
|
|
|
c->keepalive_period = period;
|
|
|
|
set_keepalive_timer(c);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-19 23:10:16 +01:00
|
|
|
void
|
2007-11-28 17:04:41 +01:00
|
|
|
collect_close(struct collect_conn *tc)
|
2007-03-19 23:10:16 +01:00
|
|
|
{
|
2009-05-30 21:54:05 +02:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2009-02-09 21:58:25 +01:00
|
|
|
announcement_remove(&tc->announcement);
|
2009-05-30 21:54:05 +02:00
|
|
|
#else
|
|
|
|
neighbor_discovery_close(&tc->neighbor_discovery_conn);
|
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2010-02-28 10:18:01 +01:00
|
|
|
unicast_close(&tc->unicast_conn);
|
2010-09-13 15:28:14 +02:00
|
|
|
while(packetqueue_first(&tc->send_queue) != NULL) {
|
|
|
|
packetqueue_dequeue(&tc->send_queue);
|
|
|
|
}
|
2007-03-19 23:10:16 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-15 21:04:30 +01:00
|
|
|
void
|
2007-11-28 17:04:41 +01:00
|
|
|
collect_set_sink(struct collect_conn *tc, int should_be_sink)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
|
|
|
if(should_be_sink) {
|
2010-03-25 09:51:07 +01:00
|
|
|
tc->is_router = 1;
|
2010-09-08 21:21:45 +02:00
|
|
|
tc->rtmetric = RTMETRIC_SINK;
|
2010-03-09 14:21:28 +01:00
|
|
|
PRINTF("collect_set_sink: tc->rtmetric %d\n", tc->rtmetric);
|
2010-09-13 15:28:14 +02:00
|
|
|
bump_advertisement(tc);
|
2010-10-24 23:08:19 +02:00
|
|
|
|
|
|
|
/* Purge the outgoing packet queue. */
|
|
|
|
while(packetqueue_len(&tc->send_queue) > 0) {
|
|
|
|
packetqueue_dequeue(&tc->send_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop the retransmission timer. */
|
|
|
|
ctimer_stop(&tc->retransmission_timer);
|
2007-03-15 21:04:30 +01:00
|
|
|
} else {
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric = RTMETRIC_MAX;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2009-05-30 21:54:05 +02:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2009-02-09 21:58:25 +01:00
|
|
|
announcement_set_value(&tc->announcement, tc->rtmetric);
|
2009-05-30 21:54:05 +02:00
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2007-11-13 21:39:29 +01:00
|
|
|
update_rtmetric(tc);
|
2010-09-23 00:08:08 +02:00
|
|
|
|
|
|
|
bump_advertisement(tc);
|
2010-10-04 22:27:50 +02:00
|
|
|
|
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#A rt=0,p=0\n");
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-06-26 13:38:59 +02:00
|
|
|
int
|
2007-11-28 17:04:41 +01:00
|
|
|
collect_send(struct collect_conn *tc, int rexmits)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2011-01-10 16:10:43 +01:00
|
|
|
int ret;
|
|
|
|
|
2010-09-23 00:08:08 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, tc->eseqno);
|
|
|
|
|
|
|
|
/* Increase the sequence number for the packet we send out. We
|
|
|
|
employ a trick that allows us to see that a node has been
|
|
|
|
rebooted: if the sequence number wraps to 0, we set it to half of
|
|
|
|
the sequence number space. This allows us to detect reboots,
|
|
|
|
since if a sequence number is less than half of the sequence
|
|
|
|
number space, the data comes from a node that was recently
|
|
|
|
rebooted. */
|
|
|
|
|
|
|
|
tc->eseqno = (tc->eseqno + 1) % (1 << COLLECT_PACKET_ID_BITS);
|
|
|
|
|
|
|
|
if(tc->eseqno == 0) {
|
|
|
|
tc->eseqno = ((int)(1 << COLLECT_PACKET_ID_BITS)) / 2;
|
|
|
|
}
|
2013-12-12 23:58:52 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &linkaddr_node_addr);
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_TTL, MAX_HOPLIM);
|
2010-10-19 09:34:29 +02:00
|
|
|
if(rexmits > MAX_REXMITS) {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, MAX_REXMITS);
|
|
|
|
} else {
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_REXMIT, rexmits);
|
|
|
|
}
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-03-16 19:12:34 +01:00
|
|
|
PRINTF("%d.%d: originating packet %d, max_rexmits %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2010-03-16 19:12:34 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
|
2010-02-28 10:18:01 +01:00
|
|
|
|
2010-09-08 21:21:45 +02:00
|
|
|
if(tc->rtmetric == RTMETRIC_SINK) {
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 0);
|
2008-06-30 11:15:22 +02:00
|
|
|
if(tc->cb->recv != NULL) {
|
2009-03-12 22:58:20 +01:00
|
|
|
tc->cb->recv(packetbuf_addr(PACKETBUF_ADDR_ESENDER),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_HOPS));
|
2008-06-30 11:15:22 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} else {
|
2010-09-13 15:28:14 +02:00
|
|
|
|
|
|
|
/* Allocate space for the header. */
|
|
|
|
packetbuf_hdralloc(sizeof(struct data_msg_hdr));
|
|
|
|
|
2011-01-10 16:10:43 +01:00
|
|
|
if(packetqueue_enqueue_packetbuf(&tc->send_queue,
|
|
|
|
FORWARD_PACKET_LIFETIME_BASE *
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
|
|
|
|
tc)) {
|
|
|
|
send_queued_packet(tc);
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2011-06-07 19:45:41 +02:00
|
|
|
ret = 0;
|
2011-01-10 16:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-13 15:28:14 +02:00
|
|
|
n = collect_neighbor_list_find(&tc->neighbor_list, &tc->parent);
|
2008-06-30 11:15:22 +02:00
|
|
|
if(n != NULL) {
|
2008-08-15 21:00:38 +02:00
|
|
|
PRINTF("%d.%d: sending to %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2008-08-15 21:00:38 +02:00
|
|
|
n->addr.u8[0], n->addr.u8[1]);
|
2008-06-30 11:15:22 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: did not find any neighbor to send to\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2010-02-23 19:35:23 +01:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2010-03-25 09:51:07 +01:00
|
|
|
#if COLLECT_CONF_WITH_LISTEN
|
2010-04-01 13:08:36 +02:00
|
|
|
PRINTF("listen\n");
|
2009-02-16 02:18:53 +01:00
|
|
|
announcement_listen(1);
|
2010-03-25 09:51:07 +01:00
|
|
|
ctimer_set(&tc->transmit_after_scan_timer, ANNOUNCEMENT_SCAN_TIME,
|
2010-09-13 15:28:14 +02:00
|
|
|
send_queued_packet, tc);
|
2010-03-25 09:51:07 +01:00
|
|
|
#else /* COLLECT_CONF_WITH_LISTEN */
|
2013-03-17 23:52:42 +01:00
|
|
|
if(tc->is_router) {
|
|
|
|
announcement_set_value(&tc->announcement, RTMETRIC_MAX);
|
|
|
|
announcement_bump(&tc->announcement);
|
|
|
|
}
|
2010-03-25 09:51:07 +01:00
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2010-02-23 19:35:23 +01:00
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2010-03-25 09:51:07 +01:00
|
|
|
|
2011-01-10 16:10:43 +01:00
|
|
|
/* if(packetqueue_enqueue_packetbuf(&tc->send_queue,
|
2010-10-19 09:34:29 +02:00
|
|
|
FORWARD_PACKET_LIFETIME_BASE *
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT),
|
2010-04-30 09:33:51 +02:00
|
|
|
tc)) {
|
2009-04-29 22:48:57 +02:00
|
|
|
return 1;
|
2010-02-08 22:59:49 +01:00
|
|
|
} else {
|
2010-12-16 23:45:15 +01:00
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2011-01-10 16:10:43 +01:00
|
|
|
}*/
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-10 16:10:43 +01:00
|
|
|
return ret;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2007-11-28 17:04:41 +01:00
|
|
|
collect_depth(struct collect_conn *tc)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2007-12-09 16:43:09 +01:00
|
|
|
return tc->rtmetric;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-12-12 23:58:52 +01:00
|
|
|
const linkaddr_t *
|
2010-11-06 15:32:10 +01:00
|
|
|
collect_parent(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
return &tc->current_parent;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-19 14:17:00 +01:00
|
|
|
void
|
|
|
|
collect_purge(struct collect_conn *tc)
|
|
|
|
{
|
2010-09-13 15:28:14 +02:00
|
|
|
collect_neighbor_list_purge(&tc->neighbor_list);
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&tc->parent, &linkaddr_null);
|
2010-03-19 14:17:00 +01:00
|
|
|
update_rtmetric(tc);
|
2010-10-04 22:27:50 +02:00
|
|
|
if(DRAW_TREE) {
|
2013-05-15 11:12:05 +02:00
|
|
|
PRINTF("#L %d 0\n", tc->parent.u8[0]);
|
2010-10-04 22:27:50 +02:00
|
|
|
}
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&tc->parent, &linkaddr_null);
|
2010-03-19 14:17:00 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-09-13 15:28:14 +02:00
|
|
|
void
|
|
|
|
collect_print_stats(void)
|
|
|
|
{
|
2010-09-14 08:48:36 +02:00
|
|
|
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",
|
2010-09-13 15:28:14 +02:00
|
|
|
stats.foundroute, stats.newparent, stats.routelost,
|
|
|
|
stats.acksent, stats.datasent, stats.datarecv,
|
|
|
|
stats.ackrecv, stats.badack, stats.duprecv,
|
|
|
|
stats.qdrop, stats.rtdrop, stats.ttldrop, stats.ackdrop,
|
|
|
|
stats.timedout);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:33:04 +02:00
|
|
|
/** @} */
|