2007-03-31 20:33:04 +02:00
|
|
|
/**
|
2007-11-28 17:04:41 +01:00
|
|
|
* \addtogroup rimecollect
|
2007-03-31 20:33:04 +02:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
*
|
2010-04-01 13:08:36 +02:00
|
|
|
* $Id: collect.c,v 1.46 2010/04/01 11:08:36 fros4943 Exp $
|
2007-03-15 21:04:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
|
|
|
#include "net/rime.h"
|
2010-03-19 14:17:00 +01:00
|
|
|
#include "net/rime/collect-neighbor.h"
|
2007-11-28 17:04:41 +01:00
|
|
|
#include "net/rime/collect.h"
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
#include "net/rime/packetqueue.h"
|
|
|
|
|
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-02-23 19:35:23 +01:00
|
|
|
#define NUM_RECENT_PACKETS 8
|
2007-11-17 11:32:54 +01:00
|
|
|
|
|
|
|
struct recent_packet {
|
|
|
|
rimeaddr_t originator;
|
2010-02-23 19:35:23 +01:00
|
|
|
rimeaddr_t sent_to;
|
2007-11-17 11:32:54 +01:00
|
|
|
uint8_t seqno;
|
|
|
|
};
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
struct ack_msg {
|
|
|
|
uint8_t flags, dummy;
|
|
|
|
uint16_t rtmetric;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ACK_FLAGS_CONGESTED 0x80
|
|
|
|
#define ACK_FLAGS_DROPPED 0x40
|
|
|
|
#define ACK_FLAGS_LIFETIME_EXCEEDED 0x20
|
|
|
|
|
2007-11-17 11:32:54 +01:00
|
|
|
static struct recent_packet recent_packets[NUM_RECENT_PACKETS];
|
|
|
|
static uint8_t recent_packet_ptr;
|
|
|
|
|
2010-04-01 12:03:19 +02:00
|
|
|
#define MAX_MAC_REXMITS 2
|
|
|
|
#define MAX_ACK_MAC_REXMITS 3
|
2010-03-29 23:52:25 +02:00
|
|
|
#define REXMIT_TIME CLOCK_SECOND * 2
|
|
|
|
#define FORWARD_PACKET_LIFETIME (6 * (REXMIT_TIME) << 3)
|
|
|
|
#define MAX_SENDING_QUEUE 6
|
2010-02-28 10:18:01 +01:00
|
|
|
PACKETQUEUE(sending_queue, MAX_SENDING_QUEUE);
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2007-03-15 21:04:30 +01:00
|
|
|
#define SINK 0
|
2007-11-28 17:04:41 +01:00
|
|
|
#define RTMETRIC_MAX COLLECT_MAX_DEPTH
|
2007-03-15 21:04:30 +01:00
|
|
|
|
|
|
|
#define MAX_HOPLIM 10
|
|
|
|
|
2009-05-30 21:54:05 +02:00
|
|
|
#ifndef COLLECT_CONF_ANNOUNCEMENTS
|
2010-02-23 19:35:23 +01:00
|
|
|
#define COLLECT_ANNOUNCEMENTS 0
|
2009-05-30 21:54:05 +02:00
|
|
|
#else
|
|
|
|
#define COLLECT_ANNOUNCEMENTS COLLECT_CONF_ANNOUNCEMENTS
|
|
|
|
#endif /* COLLECT_CONF_ANNOUNCEMENTS */
|
|
|
|
|
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
|
|
|
|
|
|
|
#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
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2010-02-28 10:18:01 +01:00
|
|
|
#include "ether.h"
|
2009-04-29 22:48:57 +02:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2010-02-28 10:18:01 +01:00
|
|
|
|
|
|
|
static void send_queued_packet(void);
|
|
|
|
static void retransmit_callback(void *ptr);
|
2007-03-31 20:33:04 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2007-11-28 17:04:41 +01:00
|
|
|
update_rtmetric(struct collect_conn *tc)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2007-11-13 21:39:29 +01:00
|
|
|
|
2010-03-09 14:21:28 +01:00
|
|
|
PRINTF("update_rtmetric: tc->rtmetric %d\n", tc->rtmetric);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* We should only update the rtmetric if we are not the sink. */
|
2007-12-09 16:43:09 +01:00
|
|
|
if(tc->rtmetric != SINK) {
|
2010-04-01 12:03:19 +02:00
|
|
|
struct collect_neighbor *best;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-04-01 12:03:19 +02:00
|
|
|
/* Pick the neighbor to use as a parent. We normally use
|
|
|
|
the parent in the n->parent. */
|
|
|
|
n = collect_neighbor_find(&tc->parent);
|
2007-11-13 21:39:29 +01:00
|
|
|
/* Find the neighbor with the lowest rtmetric. */
|
2010-04-01 12:03:19 +02:00
|
|
|
best = collect_neighbor_best();
|
|
|
|
|
|
|
|
/* If we do not have a parent in n->parent, we use the best
|
|
|
|
neighbor that we have as a new parent. Also, if the best
|
|
|
|
neighbor is better than our parent (which is defined as having
|
|
|
|
an ETX that is 1 ETX lower than the current parent), we
|
|
|
|
choose that neighbor as the new parent. */
|
|
|
|
best = collect_neighbor_best();
|
|
|
|
|
|
|
|
if(best != NULL && (n == NULL ||
|
|
|
|
collect_neighbor_etx(best) <
|
|
|
|
collect_neighbor_etx(n) - COLLECT_NEIGHBOR_ETX_SCALE)) {
|
|
|
|
rimeaddr_copy(&tc->parent, &best->addr);
|
2010-04-01 13:08:36 +02:00
|
|
|
PRINTF("Switched parent to %d.%d\n",
|
2010-04-01 12:03:19 +02:00
|
|
|
tc->parent.u8[0], tc->parent.u8[1]);
|
|
|
|
}
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
/* If n is NULL, we have no best neighbor. */
|
2007-03-15 21:04:30 +01:00
|
|
|
if(n == NULL) {
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
/* If we have don't have any neighbors, we set our rtmetric to
|
|
|
|
the maximum value to indicate that we do not have a route. */
|
2010-03-16 19:12:34 +01:00
|
|
|
|
2007-12-09 16:43:09 +01:00
|
|
|
if(tc->rtmetric != RTMETRIC_MAX) {
|
2007-11-13 21:39:29 +01:00
|
|
|
PRINTF("%d.%d: didn't find a best neighbor, setting rtmetric to max\n",
|
2007-05-15 10:09:21 +02:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
}
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric = RTMETRIC_MAX;
|
2010-02-23 19:35:23 +01:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2010-03-25 09:51:07 +01:00
|
|
|
#if ! COLLECT_CONF_WITH_LISTEN
|
2009-04-29 22:48:57 +02:00
|
|
|
announcement_set_value(&tc->announcement, tc->rtmetric);
|
2010-03-25 09:51:07 +01:00
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2010-02-23 19:35:23 +01:00
|
|
|
#else /* COLLECT_ANNOUNCEMENTS */
|
|
|
|
neighbor_discovery_set_val(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2007-03-15 21:04:30 +01:00
|
|
|
} else {
|
2007-11-13 21:39:29 +01:00
|
|
|
/* We set our rtmetric to the rtmetric of our best neighbor plus
|
2010-02-28 10:18:01 +01:00
|
|
|
the expected transmissions to reach that neighbor. */
|
2010-03-19 14:17:00 +01:00
|
|
|
if(n->rtmetric + collect_neighbor_etx(n) != tc->rtmetric) {
|
2009-04-29 22:48:57 +02:00
|
|
|
uint16_t old_rtmetric = tc->rtmetric;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-03-19 14:17:00 +01:00
|
|
|
tc->rtmetric = n->rtmetric + collect_neighbor_etx(n);
|
2009-05-30 21:54:05 +02:00
|
|
|
|
2010-02-23 19:35:23 +01:00
|
|
|
#if ! COLLECT_ANNOUNCEMENTS
|
|
|
|
|
|
|
|
/* If we get a significantly better rtmetric than we had
|
|
|
|
before, we call neighbor_discovery_start to start a new
|
|
|
|
period. */
|
2010-03-19 14:17:00 +01:00
|
|
|
if(old_rtmetric >= tc->rtmetric + COLLECT_NEIGHBOR_ETX_SCALE + COLLECT_NEIGHBOR_ETX_SCALE / 2 ||
|
2010-03-25 09:51:07 +01:00
|
|
|
old_rtmetric == RTMETRIC_MAX) {
|
2010-02-23 19:35:23 +01:00
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
} else {
|
|
|
|
neighbor_discovery_set_val(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
}
|
|
|
|
#else /* ! COLLECT_ANNOUNCEMENTS */
|
2010-03-25 09:51:07 +01:00
|
|
|
if(tc->is_router) {
|
|
|
|
announcement_set_value(&tc->announcement, tc->rtmetric);
|
|
|
|
} else {
|
|
|
|
announcement_remove_value(&tc->announcement);
|
|
|
|
}
|
2010-03-19 14:17:00 +01:00
|
|
|
if(old_rtmetric >= tc->rtmetric + COLLECT_NEIGHBOR_ETX_SCALE + COLLECT_NEIGHBOR_ETX_SCALE / 2 ||
|
2010-03-25 09:51:07 +01:00
|
|
|
old_rtmetric == RTMETRIC_MAX) {
|
2010-03-19 14:17:00 +01:00
|
|
|
announcement_bump(&tc->announcement);
|
|
|
|
}
|
2010-02-23 19:35:23 +01:00
|
|
|
#endif /* ! COLLECT_ANNOUNCEMENTS */
|
2009-05-30 21:54:05 +02:00
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
PRINTF("%d.%d: new rtmetric %d\n",
|
2007-05-15 10:09:21 +02:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric);
|
2010-03-25 09:51:07 +01:00
|
|
|
#if ! COLLECT_CONF_WITH_LISTEN
|
2009-04-29 22:48:57 +02:00
|
|
|
/* We got a new, working, route we send any queued packets we may have. */
|
|
|
|
if(old_rtmetric == RTMETRIC_MAX) {
|
|
|
|
send_queued_packet();
|
|
|
|
}
|
2010-03-25 09:51:07 +01:00
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* DEBUG_PRINTF("%d: new rtmetric %d\n", node_id, rtmetric);*/
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
|
|
|
char buf[8];
|
2007-12-09 16:43:09 +01:00
|
|
|
if(tc->rtmetric == RTMETRIC_MAX) {
|
2007-03-15 21:04:30 +01:00
|
|
|
strcpy(buf, " ");
|
|
|
|
} else {
|
2010-03-19 14:17:00 +01:00
|
|
|
PRINTF(buf, "%.1f", (float)tc->rtmetric / COLLECT_NEIGHBOR_ETX_SCALE);
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
ether_set_text(buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2007-03-22 19:54:22 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-28 00:32:34 +01:00
|
|
|
static void
|
2010-02-28 10:18:01 +01:00
|
|
|
send_queued_packet(void)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2010-02-28 10:18:01 +01:00
|
|
|
struct queuebuf *q;
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-02-28 10:18:01 +01:00
|
|
|
struct packetqueue_item *i;
|
|
|
|
struct collect_conn *c;
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: send_queued_packet queue len %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
packetqueue_len(&sending_queue));
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
i = packetqueue_first(&sending_queue);
|
|
|
|
if(i == NULL) {
|
|
|
|
PRINTF("%d.%d: nothing on queue\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
/* No packet on the queue, so there is nothing for us to send. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c = packetqueue_ptr(i);
|
|
|
|
if(c == NULL) {
|
|
|
|
/* c should not be NULL, but we check it just to be sure. */
|
|
|
|
PRINTF("%d.%d: queue, c == NULL!\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
return;
|
|
|
|
}
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(c->sending) {
|
|
|
|
/* If we are currently sending a packet, we wait until the
|
|
|
|
packet is forwarded and try again then. */
|
|
|
|
PRINTF("%d.%d: queue, c is sending\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = packetqueue_queuebuf(i);
|
|
|
|
if(q != NULL) {
|
2010-04-01 12:03:19 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: queue, q is on queue\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
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. */
|
|
|
|
n = collect_neighbor_find(&c->parent);
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(n != NULL) {
|
|
|
|
clock_time_t time;
|
|
|
|
uint8_t rexmit_time_scaling;
|
|
|
|
#if CONTIKI_TARGET_NETSIM
|
|
|
|
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
|
|
|
PRINTF("%d.%d: sending packet to %d.%d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
n->addr.u8[0], n->addr.u8[1]);
|
|
|
|
|
|
|
|
c->sending = 1;
|
|
|
|
c->transmissions = 0;
|
2010-03-09 14:21:28 +01:00
|
|
|
c->max_rexmits = packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT);
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("max_rexmits %d\n", c->max_rexmits);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
|
2010-03-29 23:52:25 +02:00
|
|
|
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);
|
|
|
|
unicast_send(&c->unicast_conn, &n->addr);
|
|
|
|
rexmit_time_scaling = c->transmissions;
|
|
|
|
if(rexmit_time_scaling > 3) {
|
|
|
|
rexmit_time_scaling = 3;
|
2010-02-23 19:35:23 +01:00
|
|
|
}
|
2010-02-28 10:18:01 +01:00
|
|
|
time = REXMIT_TIME << rexmit_time_scaling;
|
|
|
|
time = time / 2 + random_rand() % (time / 2);
|
|
|
|
PRINTF("retransmission time %lu\n", time);
|
|
|
|
ctimer_set(&c->retransmission_timer, time,
|
|
|
|
retransmit_callback, c);
|
|
|
|
} 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,
|
|
|
|
send_queued_packet, NULL);
|
|
|
|
#else /* COLLECT_CONF_WITH_LISTEN */
|
|
|
|
announcement_set_value(&c->announcement, RTMETRIC_MAX);
|
|
|
|
announcement_bump(&c->announcement);
|
|
|
|
#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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
send_next_packet(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
/* Cancel retransmission timer. */
|
|
|
|
ctimer_stop(&tc->retransmission_timer);
|
2007-03-22 19:54:22 +01:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* Remove the first packet on the queue, the packet that was just sent. */
|
|
|
|
packetqueue_dequeue(&sending_queue);
|
|
|
|
tc->seqno = (tc->seqno + 1) % (1 << COLLECT_PACKET_ID_BITS);
|
|
|
|
tc->sending = 0;
|
|
|
|
tc->transmissions = 0;
|
2008-06-30 11:15:22 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* Send the next packet in the queue, if any. */
|
|
|
|
send_queued_packet();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
handle_ack(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
struct ack_msg *msg;
|
|
|
|
uint16_t rtmetric;
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2010-02-28 10:18:01 +01:00
|
|
|
|
|
|
|
if(rimeaddr_cmp(packetbuf_addr(PACKETBUF_ADDR_SENDER),
|
2010-04-01 12:03:19 +02:00
|
|
|
&tc->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
|
|
|
|
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
msg = packetbuf_dataptr();
|
|
|
|
memcpy(&rtmetric, &msg->rtmetric, sizeof(uint16_t));
|
2010-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_find(packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
2010-02-28 10:18:01 +01:00
|
|
|
if(n != NULL) {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_update(n, rtmetric);
|
2010-02-28 10:18:01 +01:00
|
|
|
update_rtmetric(tc);
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: ACK from %d.%d after %d transmissions, flags %02x\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2010-04-01 12:03:19 +02:00
|
|
|
tc->parent.u8[0], tc->parent.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->transmissions,
|
|
|
|
msg->flags);
|
|
|
|
|
|
|
|
if(!(msg->flags & ACK_FLAGS_DROPPED)) {
|
|
|
|
send_next_packet(tc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
send_ack(struct collect_conn *tc, const rimeaddr_t *to, int congestion, int dropped, int ttl)
|
|
|
|
{
|
|
|
|
struct ack_msg *ack;
|
|
|
|
struct queuebuf *q;
|
|
|
|
uint16_t packet_seqno;
|
2007-11-17 11:32:54 +01:00
|
|
|
|
2007-03-31 20:33:04 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("send_ack\n");
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
q = queuebuf_new_from_packetbuf();
|
|
|
|
if(q != NULL) {
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01: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 = (congestion? ACK_FLAGS_CONGESTED: 0) |
|
|
|
|
(dropped? ACK_FLAGS_DROPPED: 0) |
|
|
|
|
(ttl? ACK_FLAGS_LIFETIME_EXCEEDED: 0);
|
|
|
|
/* XXX: send explicit congestion notification in ACK queue full; add rtmetric to ACK. */
|
|
|
|
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);
|
2010-04-01 12:03:19 +02:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_MAX_MAC_TRANSMISSIONS, MAX_ACK_MAC_REXMITS);
|
2010-02-28 10:18:01 +01:00
|
|
|
unicast_send(&tc->unicast_conn, to);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: collect: Sending ACK to %d.%d for %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
|
|
|
|
to->u8[0],
|
|
|
|
to->u8[1],
|
|
|
|
packet_seqno);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
RIMESTATS_ADD(acktx);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
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",
|
|
|
|
rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1],
|
|
|
|
to->u8[0], to->u8[1],
|
|
|
|
packet_seqno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
node_packet_received(struct unicast_conn *c, const rimeaddr_t *from)
|
|
|
|
{
|
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
|
|
|
((char *)c - offsetof(struct collect_conn, unicast_conn));
|
|
|
|
int i;
|
2010-03-19 14:17:00 +01:00
|
|
|
struct collect_neighbor *n;
|
2007-03-31 20:33:04 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* To protect against sending duplicate packets, we keep a list
|
|
|
|
of recently forwarded packet seqnos. If the seqno of the current
|
2010-03-16 19:12:34 +01:00
|
|
|
packet exists in the list, we increase the
|
2010-02-28 10:18:01 +01:00
|
|
|
ETX of the neighbor we sent it to in the first place. */
|
|
|
|
if(packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) ==
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_DATA) {
|
|
|
|
rimeaddr_t ack_to;
|
|
|
|
uint8_t packet_seqno;
|
|
|
|
|
|
|
|
rimeaddr_copy(&ack_to, packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
packet_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
|
|
|
|
|
|
|
if(rimeaddr_cmp(&tc->last_received_addr, packetbuf_addr(PACKETBUF_ADDR_SENDER)) &&
|
|
|
|
tc->last_received_seqno == packetbuf_attr(PACKETBUF_ATTR_PACKET_ID)) {
|
|
|
|
/* This is a duplicate of the packet we last received, so we just send an ACK. */
|
2010-03-16 19:12:34 +01:00
|
|
|
PRINTF("%d.%d: received same packet again from %d.%d with seqno %d, via %d.%d, acking\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
ack_to.u8[0], ack_to.u8[1],
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID),
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_SENDER)->u8[1]);
|
2010-02-28 10:18:01 +01:00
|
|
|
send_ack(tc, &ack_to, 0, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rimeaddr_copy(&tc->last_received_addr, packetbuf_addr(PACKETBUF_ADDR_SENDER));
|
|
|
|
tc->last_received_seqno = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID);
|
|
|
|
|
|
|
|
for(i = 0; i < NUM_RECENT_PACKETS; i++) {
|
|
|
|
if(recent_packets[i].seqno == packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID) &&
|
|
|
|
rimeaddr_cmp(&recent_packets[i].originator,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER))) {
|
|
|
|
PRINTF("%d.%d: found duplicate packet from %d.%d with seqno %d, via %d.%d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
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-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_find(&recent_packets[i].sent_to);
|
2010-02-28 10:18:01 +01:00
|
|
|
if(n != NULL) {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_update_etx(n, collect_neighbor_etx(n) / COLLECT_NEIGHBOR_ETX_SCALE + 4);
|
2010-02-28 10:18:01 +01:00
|
|
|
update_rtmetric(tc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recent_packets[recent_packet_ptr].seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
|
|
|
|
rimeaddr_copy(&recent_packets[recent_packet_ptr].originator,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER));
|
2010-03-19 14:17:00 +01:00
|
|
|
/* n = collect_neighbor_best();*/
|
2010-02-28 10:18:01 +01:00
|
|
|
|
|
|
|
if(tc->rtmetric != SINK) {
|
2010-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_best();
|
2010-02-28 10:18:01 +01:00
|
|
|
rimeaddr_copy(&recent_packets[recent_packet_ptr].sent_to,
|
|
|
|
&n->addr);
|
2010-02-08 22:59:49 +01:00
|
|
|
} else {
|
2010-02-28 10:18:01 +01:00
|
|
|
rimeaddr_copy(&recent_packets[recent_packet_ptr].sent_to,
|
|
|
|
&rimeaddr_null);
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(tc->rtmetric == SINK) {
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* If we are the sink, we call the receive function. */
|
|
|
|
|
|
|
|
send_ack(tc, &ack_to, 0, 0, 0);
|
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",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
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-02-28 10:18:01 +01:00
|
|
|
if(tc->cb->recv != NULL) {
|
|
|
|
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-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* If we are not the sink, we forward the packet to the best
|
|
|
|
neighbor. */
|
|
|
|
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-03-16 19:12:34 +01:00
|
|
|
PRINTF("%d.%d: packet received from %d.%d via %d.%d, sending %d, max_rexmits %d\n",
|
2010-02-28 10:18:01 +01:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
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-02-28 10:18:01 +01:00
|
|
|
if(packetqueue_enqueue_packetbuf(&sending_queue, FORWARD_PACKET_LIFETIME,
|
|
|
|
tc)) {
|
|
|
|
send_ack(tc, &ack_to, 0, 0, 0);
|
|
|
|
send_queued_packet();
|
|
|
|
} else {
|
|
|
|
send_ack(tc, &ack_to, 0, 1, 0);
|
|
|
|
PRINTF("%d.%d: packet dropped: no queue buffer available\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
}
|
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_TTL) <= 1) {
|
2010-02-23 19:35:23 +01:00
|
|
|
PRINTF("%d.%d: packet dropped: ttl %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_TTL));
|
2010-02-28 10:18:01 +01:00
|
|
|
send_ack(tc, &ack_to, 0, 1, 1);
|
|
|
|
}
|
|
|
|
} 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-04-01 12:03:19 +02:00
|
|
|
tc->parent.u8[0],
|
|
|
|
tc->parent.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_PACKET_ID),
|
|
|
|
tc->seqno);
|
|
|
|
handle_ack(tc);
|
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-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
|
|
|
{
|
2010-03-16 19:12:34 +01:00
|
|
|
uint16_t tx;
|
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-03-16 19:12:34 +01:00
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_DATA && transmissions > 0) {
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("%d.%d: sent to %d.%d after %d transmissions\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2010-04-01 12:03:19 +02:00
|
|
|
tc->parent.u8[0], tc->parent.u8[1],
|
2010-02-28 10:18:01 +01:00
|
|
|
transmissions);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
/* neighbor_update_etx(neighbor_find(to), transmissions);
|
|
|
|
update_rtmetric(tc);*/
|
|
|
|
tc->transmissions += transmissions;
|
|
|
|
|
2010-03-16 19:12:34 +01:00
|
|
|
tx = tc->transmissions;
|
2010-03-29 23:52:25 +02:00
|
|
|
|
2010-03-16 19:12:34 +01:00
|
|
|
#if 0
|
|
|
|
/* Punish neighbors that don't hear us by increasing the expeted
|
|
|
|
transmissions by four times the actual amount of transmissions
|
|
|
|
that we tried. This is due to the ETX calculation is done with
|
|
|
|
a moving average. */
|
|
|
|
if(status == MAC_TX_NOACK) {
|
|
|
|
tx *= 8;
|
|
|
|
}
|
|
|
|
/* Punish collisions too, but not as much. */
|
|
|
|
if(status == MAC_TX_COLLISION) {
|
|
|
|
// tx *= 2;
|
|
|
|
}
|
|
|
|
#endif /* 0 */
|
2010-02-28 10:18:01 +01:00
|
|
|
/* Update ETX with the number of transmissions. */
|
2010-03-16 19:12:34 +01:00
|
|
|
PRINTF("Updating ETX with %d transmissions (punished %d)\n", tc->transmissions,
|
|
|
|
tx);
|
2010-04-01 12:03:19 +02:00
|
|
|
collect_neighbor_update_etx(collect_neighbor_find(&tc->parent), tx);
|
2010-02-28 10:18:01 +01:00
|
|
|
update_rtmetric(tc);
|
|
|
|
}
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:09:21 +02:00
|
|
|
static void
|
2010-02-28 10:18:01 +01:00
|
|
|
timedout(struct collect_conn *tc)
|
2007-05-15 10:09:21 +02:00
|
|
|
{
|
2010-03-16 19:12:34 +01:00
|
|
|
PRINTF("%d.%d: timedout after %d retransmissions (max retransmissions %d): packet dropped\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], tc->transmissions,
|
|
|
|
tc->max_rexmits);
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
tc->sending = 0;
|
2010-04-01 12:03:19 +02:00
|
|
|
collect_neighbor_timedout_etx(collect_neighbor_find(&tc->parent), tc->transmissions);
|
2007-11-17 11:32:54 +01:00
|
|
|
update_rtmetric(tc);
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
send_next_packet(tc);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
retransmit_callback(void *ptr)
|
|
|
|
{
|
|
|
|
struct collect_conn *c = ptr;
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("retransmit\n");
|
|
|
|
if(c->transmissions >= c->max_rexmits) {
|
|
|
|
timedout(c);
|
|
|
|
} else {
|
|
|
|
c->sending = 0;
|
|
|
|
send_queued_packet();
|
|
|
|
}
|
2007-05-15 10:09:21 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
static void
|
2009-11-08 20:40:16 +01:00
|
|
|
adv_received(struct neighbor_discovery_conn *c, const rimeaddr_t *from,
|
|
|
|
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-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_find(from);
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
if(n == NULL) {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_add(from, rtmetric, 1);
|
2007-11-13 21:39:29 +01:00
|
|
|
} else {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_update(n, rtmetric);
|
2009-02-16 02:18:53 +01:00
|
|
|
PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
|
2007-11-13 21:39:29 +01:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_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
|
2009-11-08 20:40:16 +01:00
|
|
|
received_announcement(struct announcement *a, const rimeaddr_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-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_find(from);
|
2009-02-09 21:58:25 +01:00
|
|
|
|
|
|
|
if(n == NULL) {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_add(from, value, 1);
|
2009-04-29 22:48:57 +02:00
|
|
|
PRINTF("%d.%d: new neighbor %d.%d, etx %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
from->u8[0], from->u8[1], value);
|
2009-02-09 21:58:25 +01:00
|
|
|
} else {
|
2010-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_update(n, value);
|
2009-02-16 02:18:53 +01:00
|
|
|
PRINTF("%d.%d: updating neighbor %d.%d, etx %d\n",
|
2009-02-09 21:58:25 +01:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_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) {
|
|
|
|
announcement_bump(&tc->announcement);
|
|
|
|
}
|
|
|
|
#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-03-19 14:17:00 +01:00
|
|
|
collect_neighbor_init();
|
2010-03-09 14:21:28 +01:00
|
|
|
packetqueue_init(&sending_queue);
|
|
|
|
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_open(&tc->neighbor_discovery_conn, channels,
|
2010-02-23 19:35:23 +01:00
|
|
|
CLOCK_SECOND * 8,
|
|
|
|
CLOCK_SECOND * 32,
|
2010-03-16 19:12:34 +01:00
|
|
|
CLOCK_SECOND * 300UL,
|
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
|
|
|
|
announcement_set_value(&tc->announcement, RTMETRIC_MAX);
|
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2009-05-30 21:54:05 +02:00
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
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);
|
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;
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric = SINK;
|
2010-03-09 14:21:28 +01:00
|
|
|
PRINTF("collect_set_sink: tc->rtmetric %d\n", tc->rtmetric);
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
2010-03-19 14:17:00 +01:00
|
|
|
#else
|
|
|
|
announcement_bump(&tc->announcement);
|
2009-05-30 21:54:05 +02:00
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
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);
|
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;
|
2010-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, tc->eseqno++);
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_set_addr(PACKETBUF_ADDR_ESENDER, &rimeaddr_node_addr);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_HOPS, 1);
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_TTL, MAX_HOPLIM);
|
|
|
|
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",
|
2010-02-28 10:18:01 +01:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_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-04-01 13:08:36 +02:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
PRINTF("rexmit %d\n", rexmits);
|
2008-06-30 11:15:22 +02:00
|
|
|
|
2009-04-30 22:21:06 +02:00
|
|
|
if(tc->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-02-28 10:18:01 +01:00
|
|
|
update_rtmetric(tc);
|
2010-03-19 14:17:00 +01:00
|
|
|
n = collect_neighbor_best();
|
2008-06-30 11:15:22 +02:00
|
|
|
if(n != NULL) {
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-06-30 11:15:22 +02:00
|
|
|
ether_set_line(n->addr.u8[0], n->addr.u8[1]);
|
2008-11-17 23:52:10 +01:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2008-08-15 21:00:38 +02:00
|
|
|
PRINTF("%d.%d: sending to %d.%d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
n->addr.u8[0], n->addr.u8[1]);
|
2010-02-28 10:18:01 +01:00
|
|
|
if(packetqueue_enqueue_packetbuf(&sending_queue, FORWARD_PACKET_LIFETIME,
|
2009-04-29 22:48:57 +02:00
|
|
|
tc)) {
|
|
|
|
send_queued_packet();
|
|
|
|
return 1;
|
2010-02-08 22:59:49 +01:00
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
2009-04-29 22:48:57 +02:00
|
|
|
}
|
2008-06-30 11:15:22 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: did not find any neighbor to send to\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_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,
|
|
|
|
send_queued_packet, NULL);
|
|
|
|
#else /* COLLECT_CONF_WITH_LISTEN */
|
2010-04-01 13:08:36 +02:00
|
|
|
PRINTF("bump neighbor value\n");
|
2010-03-25 09:51:07 +01:00
|
|
|
announcement_set_value(&tc->announcement, RTMETRIC_MAX);
|
|
|
|
announcement_bump(&tc->announcement);
|
|
|
|
#endif /* COLLECT_CONF_WITH_LISTEN */
|
2010-02-23 19:35:23 +01:00
|
|
|
#endif /* COLLECT_ANNOUNCEMENTS */
|
2010-03-25 09:51:07 +01:00
|
|
|
|
2010-02-28 10:18:01 +01:00
|
|
|
if(packetqueue_enqueue_packetbuf(&sending_queue, FORWARD_PACKET_LIFETIME,
|
2009-04-29 22:48:57 +02:00
|
|
|
tc)) {
|
|
|
|
return 1;
|
2010-02-08 22:59:49 +01:00
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: drop originated packet: no queuebuf\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
2009-04-29 22:48:57 +02:00
|
|
|
}
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
2008-06-26 13:38:59 +02:00
|
|
|
return 0;
|
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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-03-19 14:17:00 +01:00
|
|
|
void
|
|
|
|
collect_purge(struct collect_conn *tc)
|
|
|
|
{
|
|
|
|
collect_neighbor_purge();
|
|
|
|
update_rtmetric(tc);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:33:04 +02:00
|
|
|
/** @} */
|