Added new shell

This commit is contained in:
Simon Duquennoy 2017-07-06 19:29:42 +02:00
parent 801fb2373a
commit ad6ef11a0b
6 changed files with 577 additions and 0 deletions

81
apps/shell/serial-shell.c Normal file
View File

@ -0,0 +1,81 @@
/*
* 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
* A shell back-end for the serial port
* \author
* Adam Dunkels <adam@sics.se>
* Simon Duquennoy <simon.duquennoy@inria.fr>
*/
/**
* \addtogroup shell
* @{
*/
#include "contiki.h"
#include "dev/serial-line.h"
#include "sys/log.h"
#include "shell.h"
#include "serial-shell.h"
/*---------------------------------------------------------------------------*/
PROCESS(serial_shell_process, "Contiki serial shell");
/*---------------------------------------------------------------------------*/
static void
serial_shell_output(const char *str) {
printf("%s", str);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(serial_shell_process, ev, data)
{
PROCESS_BEGIN();
shell_init();
while(1) {
static struct pt shell_input_pt;
PROCESS_WAIT_EVENT_UNTIL(ev == serial_line_event_message && data != NULL);
PROCESS_PT_SPAWN(&shell_input_pt, shell_input(&shell_input_pt, serial_shell_output, data));
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
void
serial_shell_init(void)
{
process_start(&serial_shell_process, NULL);
}
/*---------------------------------------------------------------------------*/
/** @} */

49
apps/shell/serial-shell.h Normal file
View File

@ -0,0 +1,49 @@
/*
* 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
* A shell back-end for the serial port
* \author
* Adam Dunkels <adam@sics.se>
* Simon Duquennoy <simon.duquennoy@inria.fr>
*/
#ifndef SERIAL_SHELL_H_
#define SERIAL_SHELL_H_
/**
* Initializes Serial Shell module
*/
void serial_shell_init(void);
#endif /* SERIAL_SHELL_H_ */

141
apps/shell/shell-commands.c Normal file
View File

@ -0,0 +1,141 @@
/*
* 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"
#include "net/ip/uip.h"
#include "net/ip/uiplib.h"
#include "net/ipv6/uip-icmp6.h"
#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;
/*---------------------------------------------------------------------------*/
static void
echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data, uint16_t datalen)
{
if(curr_ping_output_func != NULL) {
SHELL_OUTPUT(curr_ping_output_func, "Received ping reply from ");
shell_output_6addr(curr_ping_output_func, source);
SHELL_OUTPUT(curr_ping_output_func, ", ttl %u, len %u\n", ttl, datalen);
curr_ping_output_func = NULL;
process_poll(curr_ping_process);
}
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(cmd_ping(struct pt *pt, shell_output_func output, const char *args))
{
static uip_ipaddr_t remote_addr;
static char *next_args;
static struct etimer timeout_timer;
PT_BEGIN(pt);
/* Isolate first argument */
next_args = strchr(args, ' ');
if(next_args != NULL) {
*next_args = '\0';
next_args++;
}
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;
}
PT_END(pt);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(cmd_help(struct pt *pt, shell_output_func output, const char *args))
{
PT_BEGIN(pt);
struct shell_command_t *cmd_ptr = shell_commands;
SHELL_OUTPUT(output, "Available commands:\n");
while(cmd_ptr->name != NULL) {
SHELL_OUTPUT(output, "%s\n", cmd_ptr->help);
cmd_ptr++;
}
PT_END(pt);
}
/*---------------------------------------------------------------------------*/
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[] = {
{ "help", cmd_help, "'> help': Shows this help" },
{ "ping", cmd_ping, "'> ping addr': Pings the IPv6 address 'addr'" },
{ NULL, NULL, NULL },
};
/** @} */

View File

@ -0,0 +1,66 @@
/*
* 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
* Main header file for the Contiki shell
* \author
* Simon Duquennoy <simon.duquennoy@inria.fr>
*/
/** \addtogroup apps
* @{ */
#ifndef _SHELL_COMMANDS_H_
#define _SHELL_COMMANDS_H_
/* Command handling function type */
typedef char (shell_commands_func)(struct pt *pt, shell_output_func output, const char *args);
/* Command structure */
struct shell_command_t {
const char *name;
shell_commands_func *func;
const char *help;
};
/* The set of supported commands */
extern struct shell_command_t shell_commands[];
/**
* Initializes Shell-commands module
*/
void shell_commands_init(void);
#endif /* _SHELL_COMMANDS_H_ */
/** @} */

152
apps/shell/shell.c Normal file
View File

@ -0,0 +1,152 @@
/*
* Copyright (c) 2018, 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 shell application
* \author
* Simon Duquennoy <simon.duquennoy@inria.fr>
*/
/**
* \addtogroup shell
* @{
*/
#include "contiki.h"
#include "shell.h"
#include "shell-commands.h"
#include "net/ip/uip.h"
#include "net/ip/ip64-addr.h"
/*---------------------------------------------------------------------------*/
void
shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
{
uint16_t a;
unsigned int i;
int f;
if(ipaddr == NULL) {
SHELL_OUTPUT(output, "(NULL IP addr)");
return;
}
if(ip64_addr_is_ipv4_mapped_addr(ipaddr)) {
/* Printing IPv4-mapped addresses is done according to RFC 4291 */
SHELL_OUTPUT(output, "::FFFF:%u.%u.%u.%u", ipaddr->u8[12], ipaddr->u8[13], ipaddr->u8[14], ipaddr->u8[15]);
} else {
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (ipaddr->u8[i] << 8) + ipaddr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
SHELL_OUTPUT(output, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
SHELL_OUTPUT(output, ":");
}
SHELL_OUTPUT(output, "%x", a);
}
}
}
}
/*---------------------------------------------------------------------------*/
void
shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr)
{
if(lladdr == NULL) {
SHELL_OUTPUT(output, "(NULL LL addr)");
return;
} else {
unsigned int i;
for(i = 0; i < LINKADDR_SIZE; i++) {
if(i > 0 && i % 2 == 0) {
SHELL_OUTPUT(output, ".");
}
SHELL_OUTPUT(output, "%02x", lladdr->u8[i]);
}
}
}
/*---------------------------------------------------------------------------*/
static void
output_prompt(shell_output_func output)
{
shell_output_lladdr(output, &linkaddr_node_addr);
SHELL_OUTPUT(output, "> ");
}
/*---------------------------------------------------------------------------*/
PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd))
{
static char *args;
static struct shell_command_t *cmd_ptr;
PT_BEGIN(pt);
/* Shave off any leading spaces. */
while(*cmd == ' ') {
cmd++;
}
/* Look for arguments */
args = strchr(cmd, ' ');
if(args != NULL) {
*args = '\0';
args++;
}
/* Lookup for command */
cmd_ptr = shell_commands;
while(cmd_ptr->name != NULL) {
if(strcmp(cmd, cmd_ptr->name) == 0) {
static struct pt cmd_pt;
PT_SPAWN(pt, &cmd_pt, cmd_ptr->func(&cmd_pt, output, args));
goto done;
}
cmd_ptr++;
}
SHELL_OUTPUT(output, "Command not found. Type 'help' for a list of commands\n");
done:
output_prompt(output);
PT_END(pt);
}
/*---------------------------------------------------------------------------*/
void
shell_init(void)
{
shell_commands_init();
}
/** @} */

88
apps/shell/shell.h Normal file
View File

@ -0,0 +1,88 @@
/*
* 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
* Main header file for the Contiki shell
* \author
* Simon Duquennoy <simon.duquennoy@inria.fr>
*/
/** \addtogroup apps
* @{ */
#ifndef SHELL_H_
#define SHELL_H_
#include "net/ip/uip.h"
#include "net/linkaddr.h"
#include "sys/process.h"
#include <stdio.h>
/* Printf-formatted output via a given output function */
#define SHELL_OUTPUT(output_func, format, ...) do { \
char buffer[128]; \
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
(output_func)(buffer); \
} while(0);
typedef void (shell_output_func)(const char *str);
/**
* Initializes Shell module
*/
void shell_init(void);
/**
* \brief A protothread that is spawned by a Shell driver when receiving a new line.
*/
PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd));
/**
* Prints an IPv6 address
*
* \param output The output function
* \param ipaddr The IPv6 to printed
*/
void shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr);
/**
* Prints a link-layer address
*
* \param output The output function
* \param lladdr The link-layer to be printed
*/
void shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr);
#endif /* SHELL_H_ */
/** @} */