2007-03-27 12:39:30 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, 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-11-13 22:09:14 +01:00
|
|
|
* A program that collects statistics from a network of Tmote Sky nodes
|
2007-03-27 12:39:30 +02:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2012-03-28 21:51:19 +02:00
|
|
|
#include "net/netstack.h"
|
2007-03-27 12:39:30 +02:00
|
|
|
#include "net/rime.h"
|
2007-11-28 17:52:22 +01:00
|
|
|
#include "net/rime/collect.h"
|
2010-03-24 00:04:21 +01:00
|
|
|
#include "net/rime/collect-neighbor.h"
|
|
|
|
#include "net/rime/timesynch.h"
|
2007-03-27 12:39:30 +02:00
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/button-sensor.h"
|
2010-01-15 11:32:36 +01:00
|
|
|
#include "dev/light-sensor.h"
|
|
|
|
#include "dev/sht11-sensor.h"
|
2007-03-27 12:39:30 +02:00
|
|
|
|
2008-07-01 23:02:51 +02:00
|
|
|
#include "dev/cc2420.h"
|
2007-03-27 12:39:30 +02:00
|
|
|
#include <stdio.h>
|
2007-05-22 23:05:09 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include "contiki-net.h"
|
2007-03-27 12:39:30 +02:00
|
|
|
|
2007-11-28 17:52:22 +01:00
|
|
|
static struct collect_conn tc;
|
2007-03-27 12:39:30 +02:00
|
|
|
|
2007-11-28 17:52:22 +01:00
|
|
|
struct sky_collect_msg {
|
2007-11-13 22:09:14 +01:00
|
|
|
uint16_t light1;
|
|
|
|
uint16_t light2;
|
|
|
|
uint16_t temperature;
|
|
|
|
uint16_t humidity;
|
2008-01-08 09:05:34 +01:00
|
|
|
uint16_t rssi;
|
2008-11-30 19:36:55 +01:00
|
|
|
rimeaddr_t best_neighbor;
|
2007-11-13 22:09:14 +01:00
|
|
|
uint16_t best_neighbor_etx;
|
|
|
|
uint16_t best_neighbor_rtmetric;
|
|
|
|
uint32_t energy_lpm;
|
|
|
|
uint32_t energy_cpu;
|
|
|
|
uint32_t energy_rx;
|
|
|
|
uint32_t energy_tx;
|
|
|
|
uint32_t energy_rled;
|
|
|
|
|
|
|
|
uint16_t tx, rx;
|
|
|
|
uint16_t reliabletx, reliablerx,
|
|
|
|
rexmit, acktx, noacktx, ackrx, timedout, badackrx;
|
|
|
|
/* Reasons for dropping incoming packets: */
|
|
|
|
uint16_t toolong, tooshort, badsynch, badcrc;
|
|
|
|
uint16_t contentiondrop, /* Packet dropped due to contention */
|
|
|
|
sendingdrop; /* Packet dropped when we were sending a packet */
|
|
|
|
uint16_t lltx, llrx;
|
|
|
|
|
2008-01-08 09:05:34 +01:00
|
|
|
rtimer_clock_t timestamp;
|
2007-05-22 23:05:09 +02:00
|
|
|
};
|
|
|
|
|
2007-11-13 22:09:14 +01:00
|
|
|
#define REXMITS 4
|
|
|
|
|
2007-03-27 12:39:30 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-28 17:52:22 +01:00
|
|
|
PROCESS(test_collect_process, "Test collect process");
|
2007-03-27 12:39:30 +02:00
|
|
|
PROCESS(depth_blink_process, "Depth indicator");
|
2007-11-28 17:52:22 +01:00
|
|
|
AUTOSTART_PROCESSES(&test_collect_process, &depth_blink_process);
|
2007-03-27 12:39:30 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(depth_blink_process, ev, data)
|
|
|
|
{
|
|
|
|
static struct etimer et;
|
|
|
|
static int count;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
2007-11-13 22:09:14 +01:00
|
|
|
etimer_set(&et, CLOCK_SECOND * 10);
|
2007-03-27 12:39:30 +02:00
|
|
|
PROCESS_WAIT_UNTIL(etimer_expired(&et));
|
2007-11-28 17:52:22 +01:00
|
|
|
count = collect_depth(&tc);
|
|
|
|
if(count == COLLECT_MAX_DEPTH) {
|
2007-11-13 22:09:14 +01:00
|
|
|
leds_on(LEDS_BLUE);
|
2007-03-27 12:39:30 +02:00
|
|
|
} else {
|
2007-11-13 22:09:14 +01:00
|
|
|
leds_off(LEDS_BLUE);
|
2010-09-14 08:47:08 +02:00
|
|
|
count /= COLLECT_LINK_ESTIMATE_UNIT;
|
2007-03-27 12:39:30 +02:00
|
|
|
while(count > 0) {
|
|
|
|
leds_on(LEDS_RED);
|
2007-11-13 22:09:14 +01:00
|
|
|
etimer_set(&et, CLOCK_SECOND / 32);
|
2007-03-27 12:39:30 +02:00
|
|
|
PROCESS_WAIT_UNTIL(etimer_expired(&et));
|
|
|
|
leds_off(LEDS_RED);
|
2007-11-13 22:09:14 +01:00
|
|
|
etimer_set(&et, CLOCK_SECOND / 8);
|
2007-03-27 12:39:30 +02:00
|
|
|
PROCESS_WAIT_UNTIL(etimer_expired(&et));
|
|
|
|
--count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-08 09:05:34 +01:00
|
|
|
#define MAX(a, b) ((a) > (b)? (a): (b))
|
|
|
|
#define MIN(a, b) ((a) < (b)? (a): (b))
|
|
|
|
struct spectrum {
|
|
|
|
int channel[16];
|
|
|
|
};
|
|
|
|
#define NUM_SAMPLES 4
|
|
|
|
static struct spectrum rssi_samples[NUM_SAMPLES];
|
|
|
|
static int
|
|
|
|
do_rssi(void)
|
|
|
|
{
|
|
|
|
static int sample;
|
2008-02-24 23:27:41 +01:00
|
|
|
int channel;
|
2008-01-08 09:05:34 +01:00
|
|
|
|
2012-03-28 21:51:19 +02:00
|
|
|
NETSTACK_MAC.off(0);
|
2008-01-08 09:05:34 +01:00
|
|
|
|
2008-07-02 11:05:40 +02:00
|
|
|
cc2420_on();
|
2008-01-08 09:05:34 +01:00
|
|
|
for(channel = 11; channel <= 26; ++channel) {
|
2008-07-02 11:05:40 +02:00
|
|
|
cc2420_set_channel(channel);
|
|
|
|
rssi_samples[sample].channel[channel - 11] = cc2420_rssi() + 53;
|
2008-01-08 09:05:34 +01:00
|
|
|
}
|
|
|
|
|
2012-03-28 21:51:19 +02:00
|
|
|
NETSTACK_MAC.on();
|
2008-01-08 09:05:34 +01:00
|
|
|
|
|
|
|
sample = (sample + 1) % NUM_SAMPLES;
|
|
|
|
|
|
|
|
{
|
|
|
|
int channel, tot;
|
|
|
|
tot = 0;
|
|
|
|
for(channel = 0; channel < 16; ++channel) {
|
|
|
|
int max = -256;
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < NUM_SAMPLES; ++i) {
|
|
|
|
max = MAX(max, rssi_samples[i].channel[channel]);
|
|
|
|
}
|
|
|
|
tot += max / 20;
|
|
|
|
}
|
|
|
|
return tot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-27 12:39:30 +02:00
|
|
|
static void
|
2008-07-02 11:05:40 +02:00
|
|
|
recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
|
2007-03-27 12:39:30 +02:00
|
|
|
{
|
2007-11-28 17:52:22 +01:00
|
|
|
struct sky_collect_msg *msg;
|
2007-05-22 23:05:09 +02:00
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
msg = packetbuf_dataptr();
|
2007-11-13 22:09:14 +01:00
|
|
|
printf("%u %u %u %u %u %u %u %u %u %u %u %lu %lu %lu %lu %lu ",
|
2008-12-02 09:09:46 +01:00
|
|
|
(originator->u8[1] << 8) + originator->u8[0],
|
2008-11-30 19:36:55 +01:00
|
|
|
seqno, hops,
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->light1, msg->light2, msg->temperature, msg->humidity,
|
2008-01-08 09:05:34 +01:00
|
|
|
msg->rssi,
|
2007-11-13 22:09:14 +01:00
|
|
|
|
2008-11-30 19:36:55 +01:00
|
|
|
(msg->best_neighbor.u8[0] << 8) + msg->best_neighbor.u8[1],
|
|
|
|
msg->best_neighbor_etx, msg->best_neighbor_rtmetric,
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->energy_lpm, msg->energy_cpu, msg->energy_rx, msg->energy_tx, msg->energy_rled
|
|
|
|
);
|
2008-01-08 09:05:34 +01:00
|
|
|
printf("%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u %u ",
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->tx, msg->rx, msg->reliabletx, msg->reliablerx, msg->rexmit,
|
|
|
|
msg->acktx, msg->noacktx, msg->ackrx, msg->timedout, msg->badackrx,
|
|
|
|
msg->toolong, msg->tooshort, msg->badsynch, msg->badcrc,
|
|
|
|
msg->contentiondrop, msg->sendingdrop, msg->lltx, msg->llrx);
|
2010-03-24 00:04:21 +01:00
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
|
|
|
printf("%u", timesynch_time() - msg->timestamp);
|
|
|
|
#else
|
|
|
|
printf("%u", RTIMER_NOW() - msg->timestamp);
|
|
|
|
#endif /* TIMESYNCH_CONF_ENABLED */
|
|
|
|
printf("\n");
|
2008-07-02 11:05:40 +02:00
|
|
|
|
2007-03-27 12:39:30 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-28 17:52:22 +01:00
|
|
|
static const struct collect_callbacks callbacks = { recv };
|
2007-03-27 12:39:30 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-11-28 17:52:22 +01:00
|
|
|
PROCESS_THREAD(test_collect_process, ev, data)
|
2007-03-27 12:39:30 +02:00
|
|
|
{
|
|
|
|
PROCESS_EXITHANDLER(goto exit;)
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2010-01-15 11:32:36 +01:00
|
|
|
SENSORS_ACTIVATE(button_sensor);
|
2007-03-27 12:39:30 +02:00
|
|
|
|
2010-03-29 11:33:20 +02:00
|
|
|
collect_open(&tc, 128, COLLECT_ROUTER, &callbacks);
|
2007-03-27 12:39:30 +02:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
static struct etimer et;
|
|
|
|
|
2007-05-22 23:05:09 +02:00
|
|
|
etimer_set(&et, CLOCK_SECOND * 20);
|
2007-03-27 12:39:30 +02:00
|
|
|
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
|
|
|
|
if(ev == sensors_event) {
|
|
|
|
if(data == &button_sensor) {
|
2007-11-28 17:52:22 +01:00
|
|
|
collect_set_sink(&tc, 1);
|
2007-03-27 12:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(etimer_expired(&et)) {
|
2007-11-28 17:52:22 +01:00
|
|
|
struct sky_collect_msg *msg;
|
2010-03-24 00:04:21 +01:00
|
|
|
struct collect_neighbor *n;
|
2007-05-22 23:05:09 +02:00
|
|
|
/* leds_toggle(LEDS_BLUE);*/
|
2010-01-15 11:32:36 +01:00
|
|
|
|
|
|
|
SENSORS_ACTIVATE(light_sensor);
|
|
|
|
SENSORS_ACTIVATE(sht11_sensor);
|
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
|
|
|
msg = (struct sky_collect_msg *)packetbuf_dataptr();
|
|
|
|
packetbuf_set_datalen(sizeof(struct sky_collect_msg));
|
2010-01-15 11:32:36 +01:00
|
|
|
msg->light1 = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
|
|
|
|
msg->light2 = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);
|
|
|
|
msg->temperature = sht11_sensor.value(SHT11_SENSOR_TEMP);
|
|
|
|
msg->humidity = sht11_sensor.value(SHT11_SENSOR_HUMIDITY);
|
2008-01-08 09:05:34 +01:00
|
|
|
msg->rssi = do_rssi();
|
2010-01-15 11:32:36 +01:00
|
|
|
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->energy_lpm = energest_type_time(ENERGEST_TYPE_LPM);
|
|
|
|
msg->energy_cpu = energest_type_time(ENERGEST_TYPE_CPU);
|
|
|
|
msg->energy_rx = energest_type_time(ENERGEST_TYPE_LISTEN);
|
|
|
|
msg->energy_tx = energest_type_time(ENERGEST_TYPE_TRANSMIT);
|
|
|
|
msg->energy_rled = energest_type_time(ENERGEST_TYPE_LED_RED);
|
2008-11-30 19:36:55 +01:00
|
|
|
rimeaddr_copy(&msg->best_neighbor, &rimeaddr_null);
|
|
|
|
msg->best_neighbor_etx =
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->best_neighbor_rtmetric = 0;
|
2010-09-14 08:47:08 +02:00
|
|
|
n = collect_neighbor_list_best(&tc.neighbor_list);
|
2007-11-13 22:09:14 +01:00
|
|
|
if(n != NULL) {
|
2008-11-30 19:36:55 +01:00
|
|
|
rimeaddr_copy(&msg->best_neighbor, &n->addr);
|
2010-09-14 08:47:08 +02:00
|
|
|
msg->best_neighbor_etx = collect_neighbor_link_estimate(n);
|
2007-11-13 22:09:14 +01:00
|
|
|
msg->best_neighbor_rtmetric = n->rtmetric;
|
|
|
|
}
|
|
|
|
|
2013-03-20 22:08:47 +01:00
|
|
|
msg->tx = RIMESTATS_GET(tx);
|
|
|
|
msg->rx = RIMESTATS_GET(rx);
|
|
|
|
msg->reliabletx = RIMESTATS_GET(reliabletx);
|
|
|
|
msg->reliablerx = RIMESTATS_GET(reliablerx);
|
|
|
|
msg->rexmit = RIMESTATS_GET(rexmit);
|
|
|
|
msg->acktx = RIMESTATS_GET(acktx);
|
|
|
|
msg->noacktx = RIMESTATS_GET(noacktx);
|
|
|
|
msg->ackrx = RIMESTATS_GET(ackrx);
|
|
|
|
msg->timedout = RIMESTATS_GET(timedout);
|
|
|
|
msg->badackrx = RIMESTATS_GET(badackrx);
|
|
|
|
msg->toolong = RIMESTATS_GET(toolong);
|
|
|
|
msg->tooshort = RIMESTATS_GET(tooshort);
|
|
|
|
msg->badsynch = RIMESTATS_GET(badsynch);
|
|
|
|
msg->badcrc = RIMESTATS_GET(badcrc);
|
|
|
|
msg->contentiondrop = RIMESTATS_GET(contentiondrop);
|
|
|
|
msg->sendingdrop = RIMESTATS_GET(sendingdrop);
|
|
|
|
msg->lltx = RIMESTATS_GET(lltx);
|
|
|
|
msg->llrx = RIMESTATS_GET(llrx);
|
2010-03-24 00:04:21 +01:00
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
2008-01-08 09:05:34 +01:00
|
|
|
msg->timestamp = timesynch_time();
|
2010-03-24 00:04:21 +01:00
|
|
|
#else
|
|
|
|
msg->timestamp = RTIMER_NOW();
|
|
|
|
#endif /* TIMESYNCH_CONF_ENABLED */
|
2010-01-15 11:32:36 +01:00
|
|
|
|
|
|
|
SENSORS_DEACTIVATE(light_sensor);
|
|
|
|
SENSORS_DEACTIVATE(sht11_sensor);
|
|
|
|
|
2007-11-28 17:52:22 +01:00
|
|
|
collect_send(&tc, REXMITS);
|
2007-03-27 12:39:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exit:
|
2007-11-28 17:52:22 +01:00
|
|
|
collect_close(&tc);
|
2007-03-27 12:39:30 +02:00
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|