2017-07-06 17:29:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017, Inria.
|
|
|
|
* 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
|
|
|
|
* The Contiki shell commands
|
|
|
|
* \author
|
|
|
|
* Simon Duquennoy <simon.duquennoy@inria.fr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \addtogroup shell
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "shell.h"
|
|
|
|
#include "shell-commands.h"
|
2017-07-06 17:43:44 +00:00
|
|
|
#include "sys/log.h"
|
2017-07-06 17:29:42 +00:00
|
|
|
#include "net/ip/uip.h"
|
|
|
|
#include "net/ip/uiplib.h"
|
|
|
|
#include "net/ipv6/uip-icmp6.h"
|
2017-07-07 13:12:23 +00:00
|
|
|
#include "net/ipv6/uip-ds6.h"
|
2017-07-06 17:43:44 +00:00
|
|
|
#include "net/mac/tsch/tsch-log.h"
|
2017-07-07 13:12:23 +00:00
|
|
|
#if UIP_CONF_IPV6_RPL_LITE == 1
|
|
|
|
#include "net/rpl-lite/rpl.h"
|
|
|
|
#else /* UIP_CONF_IPV6_RPL_LITE == 1 */
|
|
|
|
#include "net/rpl/rpl.h"
|
|
|
|
#include "net/rpl/rpl-private.h"
|
|
|
|
#endif /* UIP_CONF_IPV6_RPL_LITE == 1 */
|
2017-07-06 17:43:44 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-07-06 17:29:42 +00:00
|
|
|
|
|
|
|
#define PING_TIMEOUT (5 * CLOCK_SECOND)
|
|
|
|
|
|
|
|
static struct uip_icmp6_echo_reply_notification echo_reply_notification;
|
|
|
|
static shell_output_func *curr_ping_output_func = NULL;
|
|
|
|
static struct process *curr_ping_process;
|
2017-07-07 13:43:31 +00:00
|
|
|
static uint8_t curr_ping_ttl;
|
|
|
|
static uint16_t curr_ping_datalen;
|
2017-07-06 17:29:42 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, uint16_t datalen)
|
|
|
|
{
|
|
|
|
if(curr_ping_output_func != NULL) {
|
|
|
|
curr_ping_output_func = NULL;
|
2017-07-07 13:43:31 +00:00
|
|
|
curr_ping_ttl = ttl;
|
|
|
|
curr_ping_datalen = datalen;
|
2017-07-06 17:29:42 +00:00
|
|
|
process_poll(curr_ping_process);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
2017-07-07 13:11:51 +00:00
|
|
|
PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, char *args))
|
2017-07-06 17:29:42 +00:00
|
|
|
{
|
|
|
|
static uip_ipaddr_t remote_addr;
|
|
|
|
static struct etimer timeout_timer;
|
2017-07-07 13:11:51 +00:00
|
|
|
char *next_args;
|
2017-07-06 17:29:42 +00:00
|
|
|
|
|
|
|
PT_BEGIN(pt);
|
|
|
|
|
2017-07-07 13:11:51 +00:00
|
|
|
SHELL_ARGS_INIT(args, next_args);
|
2017-07-06 17:29:42 +00:00
|
|
|
|
2017-07-07 13:11:51 +00:00
|
|
|
/* Get argument (remote IPv6) */
|
|
|
|
SHELL_ARGS_NEXT(args, next_args);
|
2017-07-06 17:29:42 +00:00
|
|
|
if(uiplib_ipaddrconv(args, &remote_addr) == 0) {
|
|
|
|
SHELL_OUTPUT(output, "Invalid IPv6: %s\n", args);
|
|
|
|
PT_EXIT(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
SHELL_OUTPUT(output, "Pinging ");
|
|
|
|
shell_output_6addr(output, &remote_addr);
|
|
|
|
SHELL_OUTPUT(output, "\n");
|
|
|
|
|
|
|
|
/* Send ping request */
|
|
|
|
curr_ping_process = PROCESS_CURRENT();
|
|
|
|
curr_ping_output_func = output;
|
|
|
|
etimer_set(&timeout_timer, PING_TIMEOUT);
|
|
|
|
uip_icmp6_send(&remote_addr, ICMP6_ECHO_REQUEST, 0, 4);
|
|
|
|
PT_WAIT_UNTIL(pt, curr_ping_output_func == NULL || etimer_expired(&timeout_timer));
|
|
|
|
|
|
|
|
if(curr_ping_output_func != NULL) {
|
|
|
|
SHELL_OUTPUT(output, "Timeout\n");
|
|
|
|
curr_ping_output_func = NULL;
|
2017-07-07 13:43:31 +00:00
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "Received ping reply from ");
|
|
|
|
shell_output_6addr(output, &remote_addr);
|
2017-07-07 16:05:27 +00:00
|
|
|
SHELL_OUTPUT(output, ", len %u, ttl %u, delay %lu ms\n",
|
|
|
|
curr_ping_datalen, curr_ping_ttl, (1000*(clock_time() - timeout_timer.timer.start))/CLOCK_SECOND);
|
2017-07-06 17:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
2017-07-07 13:12:23 +00:00
|
|
|
PT_THREAD(cmd_rpl_set_root(struct pt *pt, shell_output_func output, char *args))
|
2017-07-06 17:43:44 +00:00
|
|
|
{
|
2017-07-07 13:12:23 +00:00
|
|
|
static int is_on;
|
|
|
|
static uip_ipaddr_t prefix;
|
|
|
|
char *next_args;
|
2017-07-06 17:43:44 +00:00
|
|
|
|
|
|
|
PT_BEGIN(pt);
|
|
|
|
|
2017-07-07 13:12:23 +00:00
|
|
|
SHELL_ARGS_INIT(args, next_args);
|
|
|
|
|
|
|
|
/* Get first arg (0/1) */
|
|
|
|
SHELL_ARGS_NEXT(args, next_args);
|
|
|
|
|
|
|
|
if(!strcmp(args, "1")) {
|
|
|
|
is_on = 1;
|
|
|
|
} else if(!strcmp(args, "0")) {
|
|
|
|
is_on = 0;
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "Invalid argument: %s\n", args);
|
|
|
|
PT_EXIT(pt);
|
2017-07-06 17:43:44 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 13:12:23 +00:00
|
|
|
/* Get first second arg (prefix) */
|
|
|
|
SHELL_ARGS_NEXT(args, next_args);
|
|
|
|
if(args != NULL) {
|
|
|
|
if(uiplib_ipaddrconv(args, &prefix) == 0) {
|
|
|
|
SHELL_OUTPUT(output, "Invalid Prefix: %s\n", args);
|
|
|
|
PT_EXIT(pt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uip_ip6addr(&prefix, UIP_DS6_DEFAULT_PREFIX, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
}
|
2017-07-06 17:43:44 +00:00
|
|
|
|
2017-07-07 13:12:23 +00:00
|
|
|
if(is_on) {
|
|
|
|
if(!rpl_dag_root_is_root()) {
|
|
|
|
SHELL_OUTPUT(output, "Setting as DAG root with prefix ");
|
|
|
|
shell_output_6addr(output, &prefix);
|
|
|
|
SHELL_OUTPUT(output, "\n");
|
|
|
|
rpl_dag_root_init(&prefix, NULL);
|
|
|
|
rpl_dag_root_init_dag_immediately();
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "Node is already a DAG root\n");
|
2017-07-06 17:43:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-07 13:12:23 +00:00
|
|
|
if(rpl_dag_root_is_root()) {
|
|
|
|
SHELL_OUTPUT(output, "Setting as non-root node: leaving DAG\n");
|
|
|
|
rpl_dag_leave();
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "Node is not a DAG root\n");
|
|
|
|
}
|
2017-07-06 17:43:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
2017-07-07 13:11:51 +00:00
|
|
|
PT_THREAD(cmd_log(struct pt *pt, shell_output_func output, char *args))
|
2017-07-06 17:29:42 +00:00
|
|
|
{
|
2017-07-07 13:11:51 +00:00
|
|
|
static int prev_level;
|
|
|
|
static int level;
|
|
|
|
char *next_args;
|
|
|
|
char *ptr;
|
|
|
|
|
2017-07-06 17:29:42 +00:00
|
|
|
PT_BEGIN(pt);
|
|
|
|
|
2017-07-07 13:11:51 +00:00
|
|
|
SHELL_ARGS_INIT(args, next_args);
|
|
|
|
|
|
|
|
/* Get and parse argument */
|
|
|
|
SHELL_ARGS_NEXT(args, next_args);
|
|
|
|
level = (int)strtol(args, &ptr, 10);
|
|
|
|
if((level == 0 && args == ptr)
|
|
|
|
|| level < LOG_LEVEL_NONE || level > LOG_LEVEL_DBG) {
|
|
|
|
SHELL_OUTPUT(output, "Invalid argument: %s\n", args);
|
|
|
|
PT_EXIT(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set log level */
|
|
|
|
prev_level = log_get_level();
|
|
|
|
if(level != prev_level) {
|
|
|
|
log_set_level(level);
|
|
|
|
if(level >= LOG_LEVEL_DBG) {
|
|
|
|
tsch_log_init();
|
|
|
|
SHELL_OUTPUT(output, "TSCH logging started\n");
|
|
|
|
} else {
|
|
|
|
tsch_log_stop();
|
|
|
|
SHELL_OUTPUT(output, "TSCH logging stopped\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SHELL_OUTPUT(output, "Log level set to %u (%s)\n", level, log_level_to_str(level));
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(cmd_help(struct pt *pt, shell_output_func output, char *args))
|
|
|
|
{
|
|
|
|
struct shell_command_t *cmd_ptr;
|
|
|
|
|
|
|
|
PT_BEGIN(pt);
|
2017-07-06 17:29:42 +00:00
|
|
|
|
|
|
|
SHELL_OUTPUT(output, "Available commands:\n");
|
2017-07-07 13:11:51 +00:00
|
|
|
cmd_ptr = shell_commands;
|
2017-07-06 17:29:42 +00:00
|
|
|
while(cmd_ptr->name != NULL) {
|
|
|
|
SHELL_OUTPUT(output, "%s\n", cmd_ptr->help);
|
|
|
|
cmd_ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-07-07 16:05:27 +00:00
|
|
|
static
|
|
|
|
PT_THREAD(ipaddr(struct pt *pt, shell_output_func output, char *args))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
PT_BEGIN(pt);
|
|
|
|
|
|
|
|
SHELL_OUTPUT(output, "Node IPv6 addresses:\n");
|
|
|
|
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
|
|
|
|
state = uip_ds6_if.addr_list[i].state;
|
|
|
|
if(uip_ds6_if.addr_list[i].isused &&
|
|
|
|
(state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
|
|
|
|
SHELL_OUTPUT(output, "-- ");
|
|
|
|
shell_output_6addr(output, &uip_ds6_if.addr_list[i].ipaddr);
|
|
|
|
SHELL_OUTPUT(output, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static
|
|
|
|
PT_THREAD(routes(struct pt *pt, shell_output_func output, char *args))
|
|
|
|
{
|
|
|
|
uip_ds6_defrt_t *default_route;
|
|
|
|
#if RPL_WITH_NON_STORING
|
|
|
|
rpl_ns_node_t *link;
|
|
|
|
#endif /* RPL_WITH_NON_STORING */
|
|
|
|
#if RPL_WITH_STORING
|
|
|
|
uip_ds6_route_t *route;
|
|
|
|
#endif /* RPL_WITH_STORING */
|
|
|
|
|
|
|
|
PT_BEGIN(pt);
|
|
|
|
|
|
|
|
/* Our default route */
|
|
|
|
SHELL_OUTPUT(output, "Default route:\n");
|
|
|
|
default_route = uip_ds6_defrt_lookup(uip_ds6_defrt_choose());
|
|
|
|
if(default_route != NULL) {
|
|
|
|
SHELL_OUTPUT(output, "-- ");
|
|
|
|
shell_output_6addr(output, &default_route->ipaddr);
|
|
|
|
if(default_route->lifetime.interval != 0) {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: %lu seconds)\n", (unsigned long)default_route->lifetime.interval);
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: infinite)\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "-- None\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if RPL_WITH_NON_STORING
|
|
|
|
if(rpl_ns_num_nodes() > 0) {
|
|
|
|
/* Our routing links */
|
|
|
|
SHELL_OUTPUT(output, "Routing links (%u in total):\n", rpl_ns_num_nodes());
|
|
|
|
link = rpl_ns_node_head();
|
|
|
|
while(link != NULL) {
|
|
|
|
uip_ipaddr_t child_ipaddr;
|
|
|
|
uip_ipaddr_t parent_ipaddr;
|
|
|
|
rpl_ns_get_node_global_addr(&child_ipaddr, link);
|
|
|
|
rpl_ns_get_node_global_addr(&parent_ipaddr, link->parent);
|
|
|
|
SHELL_OUTPUT(output, "-- ");
|
|
|
|
shell_output_6addr(output, &child_ipaddr);
|
|
|
|
if(link->parent == NULL) {
|
|
|
|
memset(&parent_ipaddr, 0, sizeof(parent_ipaddr));
|
|
|
|
SHELL_OUTPUT(output, " (DODAG root)");
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, " to ");
|
|
|
|
shell_output_6addr(output, &parent_ipaddr);
|
|
|
|
}
|
|
|
|
if(link->lifetime != RPL_ROUTE_INFINITE_LIFETIME) {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: %lu seconds)\n", (unsigned long)link->lifetime);
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: infinite)\n");
|
|
|
|
}
|
|
|
|
link = rpl_ns_node_next(link);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "No routing links\n");
|
|
|
|
}
|
|
|
|
#endif /* RPL_WITH_NON_STORING */
|
|
|
|
|
|
|
|
#if RPL_WITH_STORING
|
|
|
|
if(uip_ds6_route_num_routes() > 0) {
|
|
|
|
/* Our routing entries */
|
|
|
|
SHELL_OUTPUT(output, "Routing entries (%u in total):\n", uip_ds6_route_num_routes());
|
|
|
|
route = uip_ds6_route_head();
|
|
|
|
while(route != NULL) {
|
|
|
|
SHELL_OUTPUT(output, "-- ");
|
|
|
|
shell_output_6addr(output, &route->ipaddr);
|
|
|
|
SHELL_OUTPUT(output, " via ");
|
|
|
|
shell_output_6addr(output, uip_ds6_route_nexthop(route));
|
|
|
|
if((unsigned long)route->state.lifetime != RPL_ROUTE_INFINITE_LIFETIME) {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: %lu seconds)\n", (unsigned long)route->state.lifetime);
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, " (lifetime: infinite)\n");
|
|
|
|
}
|
|
|
|
route = uip_ds6_route_next(route);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SHELL_OUTPUT(output, "No routing entries\n");
|
|
|
|
}
|
|
|
|
#endif /* RPL_WITH_STORING */
|
|
|
|
|
|
|
|
PT_END(pt);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-07-06 17:29:42 +00:00
|
|
|
void
|
|
|
|
shell_commands_init(void)
|
|
|
|
{
|
|
|
|
/* Set up Ping Reply callback */
|
|
|
|
uip_icmp6_echo_reply_callback_add(&echo_reply_notification,
|
|
|
|
echo_reply_handler);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct shell_command_t shell_commands[] = {
|
2017-07-07 13:12:23 +00:00
|
|
|
{ "help", cmd_help, "'> help': Shows this help" },
|
2017-07-07 16:05:27 +00:00
|
|
|
{ "ipaddr", ipaddr, "'> ipaddr': Shows all IPv6 addresses" },
|
2017-07-07 13:12:23 +00:00
|
|
|
{ "ping", cmd_ping, "'> ping addr': Pings the IPv6 address 'addr'" },
|
|
|
|
{ "rpl-set-root", cmd_rpl_set_root, "'> rpl-set-root 0/1 [prefix]': Sets node as root (on) or not (off). A /64 prefix can be optionally specified." },
|
2017-07-07 16:05:27 +00:00
|
|
|
{ "routes", routes, "'> routes': Shows the route entries" },
|
2017-07-07 13:12:23 +00:00
|
|
|
{ "log", cmd_log, "'> log level': Sets log level (0--4). Level 4 also enables TSCH per-slot logging." },
|
2017-07-06 17:29:42 +00:00
|
|
|
{ NULL, NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @} */
|