2008-02-05 00:42:17 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2008, 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
|
2010-09-23 00:11:20 +02:00
|
|
|
* Shell commands for Rime communication primitives
|
2008-02-05 00:42:17 +01:00
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-conf.h"
|
|
|
|
#include "shell-rime.h"
|
|
|
|
|
|
|
|
#include "dev/leds.h"
|
|
|
|
|
2008-07-08 01:22:59 +02:00
|
|
|
#include "lib/crc16.h"
|
2009-02-11 12:08:53 +01:00
|
|
|
#include "lib/random.h"
|
2008-02-05 00:42:17 +01:00
|
|
|
|
2013-12-12 20:50:07 +01:00
|
|
|
#include "net/rime/rime.h"
|
2010-04-04 14:27:31 +02:00
|
|
|
#include "net/netstack.h"
|
2008-02-05 00:42:17 +01:00
|
|
|
#include "net/rime/route.h"
|
|
|
|
|
|
|
|
#include "net/rime/timesynch.h"
|
|
|
|
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-02-05 00:42:17 +01:00
|
|
|
#include "ether.h"
|
2008-11-17 23:52:10 +01:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifndef HAVE_SNPRINTF
|
|
|
|
int snprintf(char *str, size_t size, const char *format, ...);
|
|
|
|
#endif /* HAVE_SNPRINTF */
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2010-10-12 13:36:42 +02:00
|
|
|
#define DEFAULT_COLLECT_REXMITS 15
|
2010-09-13 21:15:21 +02:00
|
|
|
|
2008-08-15 21:06:14 +02:00
|
|
|
|
2008-07-08 01:22:59 +02:00
|
|
|
#define COLLECT_MSG_HDRSIZE 4
|
2008-02-05 00:42:17 +01:00
|
|
|
struct collect_msg {
|
|
|
|
uint16_t timestamp;
|
2008-07-08 01:22:59 +02:00
|
|
|
uint16_t crc;
|
2008-02-05 00:42:17 +01:00
|
|
|
uint8_t data[1];
|
|
|
|
};
|
|
|
|
|
2010-09-13 15:29:29 +02:00
|
|
|
struct collect_conn shell_collect_conn;
|
2008-02-05 00:42:17 +01:00
|
|
|
static int waiting_for_collect = 0;
|
|
|
|
|
|
|
|
static int is_sink = 0;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 21:58:35 +01:00
|
|
|
PROCESS(shell_mac_process, "mac");
|
|
|
|
SHELL_COMMAND(mac_command,
|
|
|
|
"mac",
|
|
|
|
"mac <onoroff>: turn MAC protocol on (1) or off (0)",
|
|
|
|
&shell_mac_process);
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS(shell_send_process, "send");
|
|
|
|
SHELL_COMMAND(send_command,
|
|
|
|
"send",
|
2010-09-13 15:29:29 +02:00
|
|
|
"send <rexmits>: send data to the collector node, with rexmits hop-by-hop retransmissions",
|
2008-02-05 00:42:17 +01:00
|
|
|
&shell_send_process);
|
|
|
|
PROCESS(shell_collect_process, "collect");
|
|
|
|
SHELL_COMMAND(collect_command,
|
|
|
|
"collect",
|
|
|
|
"collect: collect data from the network",
|
|
|
|
&shell_collect_process);
|
|
|
|
#if WITH_TREEDEPTH
|
|
|
|
PROCESS(shell_treedepth_process, "treedepth");
|
|
|
|
SHELL_COMMAND(treedepth_command,
|
|
|
|
"treedepth",
|
|
|
|
"treedepth: print the collection tree depth",
|
|
|
|
&shell_treedepth_process);
|
|
|
|
#endif /* WITH_TREEDEPTH */
|
|
|
|
PROCESS(shell_routes_process, "routes");
|
|
|
|
SHELL_COMMAND(routes_command,
|
|
|
|
"routes",
|
|
|
|
"routes: dump route list in binary format",
|
|
|
|
&shell_routes_process);
|
|
|
|
PROCESS(shell_packetize_process, "packetize");
|
|
|
|
SHELL_COMMAND(packetize_command,
|
|
|
|
"packetize",
|
|
|
|
"packetize: put data into one packet",
|
|
|
|
&shell_packetize_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-24 21:58:35 +01:00
|
|
|
PROCESS_THREAD(shell_mac_process, ev, data)
|
|
|
|
{
|
|
|
|
int onoroff;
|
|
|
|
const char *next;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
onoroff = shell_strtolong((char *)data, &next);
|
|
|
|
if(next == data) {
|
2010-04-30 09:17:50 +02:00
|
|
|
shell_output_str(&mac_command, "mac: current MAC layer: ", NETSTACK_RDC.name);
|
2008-07-02 16:08:06 +02:00
|
|
|
shell_output_str(&mac_command, "mac usage: ", mac_command.description);
|
2008-02-24 21:58:35 +01:00
|
|
|
} else {
|
|
|
|
if(onoroff) {
|
2010-04-04 14:27:31 +02:00
|
|
|
NETSTACK_RDC.on();
|
|
|
|
shell_output_str(&mac_command, "mac: turned MAC on: ", NETSTACK_RDC.name);
|
2008-02-24 21:58:35 +01:00
|
|
|
} else {
|
2010-04-04 14:27:31 +02:00
|
|
|
NETSTACK_RDC.off(1);
|
2008-07-02 16:08:06 +02:00
|
|
|
shell_output_str(&mac_command, "mac: turned MAC off (keeping radio on): ",
|
2010-04-30 09:17:50 +02:00
|
|
|
NETSTACK_RDC.name);
|
2008-02-24 21:58:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_THREAD(shell_packetize_process, ev, data)
|
|
|
|
{
|
|
|
|
static struct queuebuf *q = NULL;
|
|
|
|
static char *ptr;
|
|
|
|
static int size;
|
|
|
|
int len;
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
struct shell_input *input;
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
|
|
|
|
|
|
|
|
if(q == NULL) {
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
|
|
|
q = queuebuf_new_from_packetbuf();
|
2008-02-05 00:42:17 +01:00
|
|
|
if(q == NULL) {
|
|
|
|
shell_output_str(&packetize_command, "packetize: could not allocate packet buffer", "");
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
|
|
|
ptr = queuebuf_dataptr(q);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
input = data;
|
|
|
|
|
|
|
|
len = input->len1 + input->len2;
|
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
if(len + size >= PACKETBUF_SIZE ||
|
2008-02-05 00:42:17 +01:00
|
|
|
len == 0) {
|
|
|
|
shell_output(&packetize_command,
|
|
|
|
ptr, size,
|
|
|
|
"", 0);
|
|
|
|
queuebuf_free(q);
|
|
|
|
q = NULL;
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(ptr + size, input->data1, input->len1);
|
|
|
|
size += input->len1;
|
|
|
|
memcpy(ptr + size, input->data2, input->len2);
|
|
|
|
size += input->len2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_routes_process, ev, data)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
uint16_t len;
|
|
|
|
uint16_t dest;
|
|
|
|
uint16_t nexthop;
|
|
|
|
uint16_t hop_count;
|
|
|
|
uint16_t seqno;
|
|
|
|
} msg;
|
|
|
|
int i;
|
|
|
|
struct route_entry *r;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.len = 4;
|
|
|
|
for(i = 0; i < route_num(); ++i) {
|
|
|
|
r = route_get(i);
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy((linkaddr_t *)&msg.dest, &r->dest);
|
|
|
|
linkaddr_copy((linkaddr_t *)&msg.nexthop, &r->nexthop);
|
2009-05-10 23:04:06 +02:00
|
|
|
msg.hop_count = r->cost;
|
2008-02-05 00:42:17 +01:00
|
|
|
msg.seqno = r->seqno;
|
|
|
|
shell_output(&routes_command, &msg, sizeof(msg), "", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if WITH_TREEDEPTH
|
|
|
|
PROCESS_THREAD(shell_treedepth_process, ev, data)
|
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%d", collect_depth(&collect));
|
|
|
|
|
|
|
|
shell_output_str(&treedepth_command, buf, "");
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
#endif /* WITH_TREEDEPTH */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_collect_process, ev, data)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
|
|
|
timesynch_set_authority_level(0);
|
|
|
|
#endif
|
2010-09-13 15:29:29 +02:00
|
|
|
collect_set_sink(&shell_collect_conn, 1);
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
is_sink = 1;
|
|
|
|
waiting_for_collect = 1;
|
|
|
|
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
|
|
|
|
|
|
|
|
waiting_for_collect = 0;
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_send_process, ev, data)
|
|
|
|
{
|
|
|
|
struct shell_input *input;
|
|
|
|
int len;
|
|
|
|
struct collect_msg *msg;
|
2010-09-13 15:29:29 +02:00
|
|
|
static int num_rexmits;
|
2010-09-14 09:18:14 +02:00
|
|
|
const char *next;
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
PROCESS_BEGIN();
|
2010-09-13 15:29:29 +02:00
|
|
|
|
2010-09-14 09:18:14 +02:00
|
|
|
num_rexmits = shell_strtolong((char *)data, &next);
|
2010-09-13 21:15:21 +02:00
|
|
|
|
|
|
|
if(next == data) {
|
|
|
|
/* If no argument was given, we send packets with a default number
|
|
|
|
of retransmissions. */
|
|
|
|
num_rexmits = DEFAULT_COLLECT_REXMITS;
|
|
|
|
}
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
|
|
|
|
input = data;
|
|
|
|
|
|
|
|
len = input->len1 + input->len2;
|
|
|
|
|
|
|
|
if(len == 0) {
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
if(len < PACKETBUF_SIZE) {
|
|
|
|
packetbuf_clear();
|
|
|
|
packetbuf_set_datalen(len + COLLECT_MSG_HDRSIZE);
|
|
|
|
msg = packetbuf_dataptr();
|
2008-02-05 00:42:17 +01:00
|
|
|
memcpy(msg->data, input->data1, input->len1);
|
|
|
|
memcpy(msg->data + input->len1, input->data2, input->len2);
|
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
|
|
|
msg->timestamp = timesynch_time();
|
|
|
|
#else
|
|
|
|
msg->timestamp = 0;
|
|
|
|
#endif
|
2008-07-08 01:22:59 +02:00
|
|
|
msg->crc = crc16_data(msg->data, len, 0);
|
2010-09-13 15:29:29 +02:00
|
|
|
collect_send(&shell_collect_conn, num_rexmits);
|
2008-02-05 00:42:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
recv_collect(const linkaddr_t *originator, uint8_t seqno, uint8_t hops)
|
2008-02-05 00:42:17 +01:00
|
|
|
{
|
2010-02-04 17:21:15 +01:00
|
|
|
struct collect_msg collect_msg;
|
|
|
|
char *dataptr;
|
2008-02-05 00:42:17 +01:00
|
|
|
rtimer_clock_t latency;
|
2008-07-08 01:22:59 +02:00
|
|
|
int len;
|
2010-02-04 17:21:15 +01:00
|
|
|
|
|
|
|
/* Copy the collect message header. */
|
|
|
|
memcpy(&collect_msg, packetbuf_dataptr(), sizeof(collect_msg));
|
2015-10-04 22:31:28 +02:00
|
|
|
dataptr = (char *)((struct collect_msg *)packetbuf_dataptr())->data;
|
2008-02-05 00:42:17 +01:00
|
|
|
|
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
2010-02-04 17:21:15 +01:00
|
|
|
latency = timesynch_time() - collect_msg.timestamp;
|
2008-02-05 00:42:17 +01:00
|
|
|
#else
|
|
|
|
latency = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(waiting_for_collect) {
|
|
|
|
struct {
|
|
|
|
uint16_t len;
|
|
|
|
uint16_t originator;
|
|
|
|
uint16_t seqno;
|
|
|
|
uint16_t hops;
|
|
|
|
uint16_t latency;
|
|
|
|
} msg;
|
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
if(packetbuf_datalen() >= COLLECT_MSG_HDRSIZE) {
|
|
|
|
len = packetbuf_datalen() - COLLECT_MSG_HDRSIZE;
|
2008-07-08 01:22:59 +02:00
|
|
|
|
2015-10-04 22:31:28 +02:00
|
|
|
if(collect_msg.crc == crc16_data((unsigned char *)dataptr, len, 0)) {
|
2009-03-12 22:58:20 +01:00
|
|
|
msg.len = 5 + (packetbuf_datalen() - COLLECT_MSG_HDRSIZE) / 2;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy((linkaddr_t *)&msg.originator, originator);
|
2008-07-08 01:22:59 +02:00
|
|
|
msg.seqno = seqno;
|
|
|
|
msg.hops = hops;
|
|
|
|
msg.latency = latency;
|
|
|
|
|
|
|
|
shell_output(&collect_command,
|
|
|
|
&msg, sizeof(msg),
|
2010-02-04 17:21:15 +01:00
|
|
|
dataptr, packetbuf_datalen() - COLLECT_MSG_HDRSIZE);
|
2008-07-08 01:22:59 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 00:42:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
static const struct collect_callbacks collect_callbacks = { recv_collect };
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_rime_init(void)
|
|
|
|
{
|
2010-09-13 15:29:29 +02:00
|
|
|
collect_open(&shell_collect_conn, SHELL_RIME_CHANNEL_COLLECT,
|
2010-03-25 09:52:23 +01:00
|
|
|
COLLECT_ROUTER, &collect_callbacks);
|
2010-09-23 00:11:20 +02:00
|
|
|
collect_set_keepalive(&shell_collect_conn, 10 * 60 * CLOCK_SECOND);
|
|
|
|
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_register_command(&collect_command);
|
2008-02-24 21:58:35 +01:00
|
|
|
shell_register_command(&mac_command);
|
2008-02-05 00:42:17 +01:00
|
|
|
shell_register_command(&packetize_command);
|
|
|
|
shell_register_command(&routes_command);
|
|
|
|
shell_register_command(&send_command);
|
|
|
|
|
|
|
|
#if WITH_TREEDEPTH
|
|
|
|
shell_register_command(&treedepth_command);
|
|
|
|
#endif /* WITH_TREEDEPTH */
|
|
|
|
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|