2007-12-09 16:43:09 +01:00
|
|
|
/* XXX: send explicit congestion notification if already forwarding
|
2009-04-30 22:21:06 +02:00
|
|
|
packet. */
|
2007-12-09 16:43:09 +01:00
|
|
|
|
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-02-02 22:44:05 +01:00
|
|
|
* $Id: collect.c,v 1.30 2010/02/02 21:44:05 adamdunkels 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"
|
|
|
|
#include "net/rime/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"
|
|
|
|
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2007-03-15 21:04:30 +01:00
|
|
|
#include "ether.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
2009-02-10 01:44:50 +01:00
|
|
|
#define NUM_RECENT_PACKETS 2
|
2007-11-17 11:32:54 +01:00
|
|
|
|
|
|
|
struct recent_packet {
|
|
|
|
rimeaddr_t originator;
|
|
|
|
uint8_t seqno;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct recent_packet recent_packets[NUM_RECENT_PACKETS];
|
|
|
|
static uint8_t recent_packet_ptr;
|
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
#define FORWARD_PACKET_LIFETIME (CLOCK_SECOND * 16)
|
2010-02-02 22:44:05 +01:00
|
|
|
#define MAX_FORWARDING_QUEUE 6
|
2009-04-29 22:48:57 +02:00
|
|
|
PACKETQUEUE(forwarding_queue, MAX_FORWARDING_QUEUE);
|
|
|
|
|
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
|
|
|
|
#define COLLECT_ANNOUNCEMENTS 1
|
|
|
|
#else
|
|
|
|
#define COLLECT_ANNOUNCEMENTS COLLECT_CONF_ANNOUNCEMENTS
|
|
|
|
#endif /* COLLECT_CONF_ANNOUNCEMENTS */
|
|
|
|
|
2007-03-31 20:33:04 +02:00
|
|
|
#define DEBUG 0
|
|
|
|
#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
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
send_queued_packet(void)
|
|
|
|
{
|
|
|
|
struct queuebuf *q;
|
|
|
|
struct neighbor *n;
|
|
|
|
struct packetqueue_item *i;
|
|
|
|
struct collect_conn *c;
|
|
|
|
|
|
|
|
i = packetqueue_first(&forwarding_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c->forwarding) {
|
|
|
|
/* If we are currently forwarding a packet, we wait until the
|
|
|
|
packet is forwarded and try again then. */
|
|
|
|
PRINTF("%d.%d: queue, c is forwarding\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
q = packetqueue_queuebuf(i);
|
|
|
|
if(q != NULL) {
|
|
|
|
PRINTF("%d.%d: queue, q is on queue\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
queuebuf_to_packetbuf(q);
|
|
|
|
|
|
|
|
n = neighbor_best();
|
|
|
|
|
|
|
|
/* Don't send to the neighbor if it is the same neighbor that sent
|
|
|
|
us the packet. */
|
|
|
|
if(n != NULL && !rimeaddr_cmp(&n->addr, packetbuf_addr(PACKETBUF_ADDR_SENDER))) {
|
|
|
|
#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->forwarding = 1;
|
|
|
|
runicast_send(&c->runicast_conn, &n->addr, packetbuf_attr(PACKETBUF_ATTR_MAX_REXMIT));
|
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: did not find any neighbor to forward to\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
struct neighbor *n;
|
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) {
|
2007-11-13 21:39:29 +01:00
|
|
|
|
|
|
|
/* Find the neighbor with the lowest rtmetric. */
|
2007-03-15 21:04:30 +01:00
|
|
|
n = neighbor_best();
|
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. */
|
|
|
|
|
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;
|
2009-04-29 22:48:57 +02:00
|
|
|
announcement_set_value(&tc->announcement, tc->rtmetric);
|
2007-03-15 21:04:30 +01:00
|
|
|
} else {
|
2009-02-09 21:58:25 +01:00
|
|
|
|
2007-11-13 21:39:29 +01:00
|
|
|
/* We set our rtmetric to the rtmetric of our best neighbor plus
|
|
|
|
the expected transmissions to reach that neighbor. */
|
2007-12-09 16:43:09 +01:00
|
|
|
if(n->rtmetric + neighbor_etx(n) != tc->rtmetric) {
|
2009-04-29 22:48:57 +02:00
|
|
|
uint16_t old_rtmetric = tc->rtmetric;
|
|
|
|
|
2009-02-09 21:58:25 +01:00
|
|
|
tc->rtmetric = n->rtmetric + neighbor_etx(n);
|
2009-05-30 21:54:05 +02:00
|
|
|
|
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#else
|
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
|
|
|
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);
|
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();
|
|
|
|
}
|
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 {
|
2008-01-24 22:11:40 +01:00
|
|
|
sprintf(buf, "%.1f", (float)tc->rtmetric / 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
|
2009-11-08 20:40:16 +01:00
|
|
|
node_packet_received(struct runicast_conn *c, const rimeaddr_t *from,
|
|
|
|
uint8_t seqno)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2007-11-28 17:04:41 +01:00
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
2008-07-03 23:52:25 +02:00
|
|
|
((char *)c - offsetof(struct collect_conn, runicast_conn));
|
2007-11-17 11:32:54 +01:00
|
|
|
int i;
|
2007-03-15 21:04:30 +01:00
|
|
|
|
2007-11-17 11:32:54 +01:00
|
|
|
/* To protect against forwarding duplicate packets, we keep a list
|
|
|
|
of recently forwarded packet seqnos. If the seqno of the current
|
|
|
|
packet exists in the list, we drop the packet. */
|
|
|
|
|
|
|
|
for(i = 0; i < NUM_RECENT_PACKETS; i++) {
|
2009-03-12 22:58:20 +01:00
|
|
|
if(recent_packets[i].seqno == packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID) &&
|
2009-04-29 22:48:57 +02:00
|
|
|
rimeaddr_cmp(&recent_packets[i].originator,
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER))) {
|
|
|
|
PRINTF("%d.%d: dropping duplicate packet from %d.%d with seqno %d\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
recent_packets[i].originator.u8[0], recent_packets[i].originator.u8[1],
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID));
|
2007-11-17 11:32:54 +01:00
|
|
|
/* Drop the packet. */
|
2007-11-28 00:32:34 +01:00
|
|
|
return;
|
2007-11-17 11:32:54 +01:00
|
|
|
}
|
|
|
|
}
|
2009-03-12 22:58:20 +01:00
|
|
|
recent_packets[recent_packet_ptr].seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
|
2008-06-30 11:15:22 +02:00
|
|
|
rimeaddr_copy(&recent_packets[recent_packet_ptr].originator,
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER));
|
2007-11-17 11:32:54 +01:00
|
|
|
recent_packet_ptr = (recent_packet_ptr + 1) % NUM_RECENT_PACKETS;
|
|
|
|
|
2007-12-09 16:43:09 +01:00
|
|
|
if(tc->rtmetric == SINK) {
|
2007-03-22 19:54:22 +01:00
|
|
|
|
2007-11-17 11:32:54 +01:00
|
|
|
/* If we are the sink, we call the receive function. */
|
|
|
|
|
2008-06-30 11:15:22 +02:00
|
|
|
PRINTF("%d.%d: sink received packet from %d.%d via %d.%d\n",
|
2007-03-31 20:33:04 +02:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
|
2008-06-30 11:15:22 +02:00
|
|
|
from->u8[0], from->u8[1]);
|
|
|
|
|
2007-03-22 19:54:22 +01: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));
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
2007-11-28 00:32:34 +01:00
|
|
|
return;
|
2009-03-12 22:58:20 +01:00
|
|
|
} else if(packetbuf_attr(PACKETBUF_ATTR_TTL) > 1 &&
|
2008-06-30 11:15:22 +02:00
|
|
|
tc->rtmetric != RTMETRIC_MAX) {
|
2007-11-17 11:32:54 +01:00
|
|
|
|
|
|
|
/* If we are not the sink, we forward the packet to the best
|
|
|
|
neighbor. */
|
2009-03-12 22:58:20 +01: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);
|
2007-03-31 20:33:04 +02:00
|
|
|
|
|
|
|
|
2009-02-16 02:18:53 +01:00
|
|
|
PRINTF("%d.%d: packet received from %d.%d via %d.%d, forwarding %d\n",
|
2007-03-31 20:33:04 +02:00
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[0],
|
|
|
|
packetbuf_addr(PACKETBUF_ADDR_ESENDER)->u8[1],
|
2009-02-16 02:18:53 +01:00
|
|
|
from->u8[0], from->u8[1], tc->forwarding);
|
2007-03-31 20:33:04 +02:00
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
if(packetqueue_enqueue_packetbuf(&forwarding_queue, FORWARD_PACKET_LIFETIME,
|
|
|
|
tc)) {
|
|
|
|
send_queued_packet();
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
2009-04-29 22:48:57 +02:00
|
|
|
|
2007-11-28 00:32:34 +01:00
|
|
|
return;
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2009-11-08 20:40:16 +01:00
|
|
|
node_packet_sent(struct runicast_conn *c, const rimeaddr_t *to,
|
|
|
|
uint8_t transmissions)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2007-11-28 17:04:41 +01:00
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
2008-07-03 23:52:25 +02:00
|
|
|
((char *)c - offsetof(struct collect_conn, runicast_conn));
|
2007-03-22 19:54:22 +01:00
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
PRINTF("%d.%d: sent to %d.%d after %d retransmissions\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
|
|
|
|
to->u8[0], to->u8[1],
|
|
|
|
transmissions);
|
|
|
|
|
|
|
|
|
2007-03-22 19:54:22 +01:00
|
|
|
tc->forwarding = 0;
|
2009-04-29 22:48:57 +02:00
|
|
|
neighbor_update_etx(neighbor_find(to), transmissions);
|
2007-11-17 11:32:54 +01:00
|
|
|
update_rtmetric(tc);
|
2009-04-29 22:48:57 +02:00
|
|
|
|
|
|
|
/* Remove the first packet on the queue, the packet that was just sent. */
|
|
|
|
packetqueue_dequeue(&forwarding_queue);
|
|
|
|
|
|
|
|
/* Send the next packet in the queue, if any. */
|
|
|
|
send_queued_packet();
|
2007-03-15 21:04:30 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-05-15 10:09:21 +02:00
|
|
|
static void
|
2009-11-08 20:40:16 +01:00
|
|
|
node_packet_timedout(struct runicast_conn *c, const rimeaddr_t *to,
|
|
|
|
uint8_t transmissions)
|
2007-05-15 10:09:21 +02:00
|
|
|
{
|
2007-11-28 17:04:41 +01:00
|
|
|
struct collect_conn *tc = (struct collect_conn *)
|
2008-07-03 23:52:25 +02:00
|
|
|
((char *)c - offsetof(struct collect_conn, runicast_conn));
|
2007-05-15 10:09:21 +02:00
|
|
|
|
2009-04-29 22:48:57 +02:00
|
|
|
PRINTF("%d.%d: timedout after %d retransmissions\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], transmissions);
|
|
|
|
|
2007-05-15 10:09:21 +02:00
|
|
|
tc->forwarding = 0;
|
2009-04-29 22:48:57 +02:00
|
|
|
neighbor_timedout_etx(neighbor_find(to), transmissions);
|
2007-11-17 11:32:54 +01:00
|
|
|
update_rtmetric(tc);
|
2009-04-29 22:48:57 +02:00
|
|
|
|
|
|
|
/* Remove the first packet on the queue, the packet that just timed out. */
|
|
|
|
packetqueue_dequeue(&forwarding_queue);
|
|
|
|
|
|
|
|
/* Send the next packet in the queue, if any. */
|
|
|
|
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));
|
2007-11-13 21:39:29 +01:00
|
|
|
struct neighbor *n;
|
|
|
|
|
|
|
|
n = neighbor_find(from);
|
|
|
|
|
|
|
|
if(n == NULL) {
|
2007-11-27 00:31:44 +01:00
|
|
|
neighbor_add(from, rtmetric, 1);
|
2007-11-13 21:39:29 +01:00
|
|
|
} else {
|
2007-11-17 11:32:54 +01:00
|
|
|
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));
|
|
|
|
struct neighbor *n;
|
|
|
|
|
|
|
|
n = neighbor_find(from);
|
|
|
|
|
|
|
|
if(n == NULL) {
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
update_rtmetric(tc);
|
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
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-07-03 23:52:25 +02:00
|
|
|
static const struct runicast_callbacks runicast_callbacks = {node_packet_received,
|
2009-04-29 22:48:57 +02:00
|
|
|
node_packet_sent,
|
|
|
|
node_packet_timedout};
|
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,
|
2007-11-28 17:04:41 +01:00
|
|
|
const struct collect_callbacks *cb)
|
2007-03-15 21:04:30 +01:00
|
|
|
{
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_open(&tc->neighbor_discovery_conn, channels,
|
2008-06-27 18:44:58 +02:00
|
|
|
CLOCK_SECOND * 2,
|
|
|
|
CLOCK_SECOND * 10,
|
|
|
|
CLOCK_SECOND * 60,
|
2009-05-30 21:54:05 +02:00
|
|
|
&neighbor_discovery_callbacks);
|
|
|
|
#endif /* !COLLECT_ANNOUNCEMENTS */
|
2008-07-03 23:52:25 +02:00
|
|
|
runicast_open(&tc->runicast_conn, channels + 1, &runicast_callbacks);
|
2008-06-30 11:15:22 +02:00
|
|
|
channel_set_attributes(channels + 1, attributes);
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric = RTMETRIC_MAX;
|
2007-03-22 19:54:22 +01:00
|
|
|
tc->cb = cb;
|
2009-05-30 21:54:05 +02:00
|
|
|
#if COLLECT_ANNOUNCEMENTS
|
2009-02-09 21:58:25 +01:00
|
|
|
announcement_register(&tc->announcement, channels, tc->rtmetric,
|
|
|
|
received_announcement);
|
|
|
|
announcement_listen(2);
|
2009-05-30 21:54:05 +02:00
|
|
|
#else
|
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#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 */
|
2008-07-03 23:52:25 +02:00
|
|
|
runicast_close(&tc->runicast_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) {
|
2007-12-09 16:43:09 +01:00
|
|
|
tc->rtmetric = SINK;
|
2009-05-30 21:54:05 +02:00
|
|
|
#if !COLLECT_ANNOUNCEMENTS
|
|
|
|
neighbor_discovery_start(&tc->neighbor_discovery_conn, tc->rtmetric);
|
|
|
|
#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
|
|
|
{
|
|
|
|
struct neighbor *n;
|
2007-05-15 10:09:21 +02:00
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_EPACKET_ID, tc->seqno++);
|
|
|
|
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);
|
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 {
|
|
|
|
n = neighbor_best();
|
|
|
|
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]);
|
2009-04-29 22:48:57 +02:00
|
|
|
if(packetqueue_enqueue_packetbuf(&forwarding_queue, FORWARD_PACKET_LIFETIME,
|
|
|
|
tc)) {
|
|
|
|
send_queued_packet();
|
|
|
|
return 1;
|
|
|
|
}
|
2008-06-30 11:15:22 +02:00
|
|
|
} else {
|
|
|
|
/* printf("Didn't find any neighbor\n");*/
|
|
|
|
PRINTF("%d.%d: did not find any neighbor to send to\n",
|
|
|
|
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
|
2009-02-16 02:18:53 +01:00
|
|
|
announcement_listen(1);
|
2009-04-29 22:48:57 +02:00
|
|
|
if(packetqueue_enqueue_packetbuf(&forwarding_queue, FORWARD_PACKET_LIFETIME,
|
|
|
|
tc)) {
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:33:04 +02:00
|
|
|
/** @} */
|