2008-02-05 20:35:21 +00: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
|
|
|
|
* A brief description of what this file is.
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "contiki-conf.h"
|
|
|
|
#include "shell-rime.h"
|
|
|
|
|
|
|
|
#include "dev/leds.h"
|
|
|
|
|
2008-07-07 23:22:38 +00:00
|
|
|
#include "lib/crc16.h"
|
2008-02-05 20:35:21 +00:00
|
|
|
#include "lib/random.h"
|
|
|
|
|
2013-12-12 19:50:07 +00:00
|
|
|
#include "net/rime/rime.h"
|
2008-02-05 20:35:21 +00:00
|
|
|
#include "net/rime/route.h"
|
|
|
|
#include "net/rime/trickle.h"
|
|
|
|
|
|
|
|
#include "net/rime/timesynch.h"
|
|
|
|
|
2008-11-17 22:52:10 +00:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-02-05 20:35:21 +00:00
|
|
|
#include "ether.h"
|
2008-11-17 22:52:10 +00:00
|
|
|
#endif /* CONTIKI_TARGET_NETSIM */
|
2008-02-05 20:35:21 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#ifndef HAVE_SNPRINTF
|
|
|
|
int snprintf(char *str, size_t size, const char *format, ...);
|
|
|
|
#endif /* HAVE_SNPRINTF */
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define COLLECT_REXMITS 4
|
|
|
|
|
2008-07-07 23:22:38 +00:00
|
|
|
#define TRICKLEMSG_HDR_SIZE 2
|
|
|
|
|
2008-02-05 20:35:21 +00:00
|
|
|
struct trickle_msg {
|
2008-07-07 23:22:38 +00:00
|
|
|
uint16_t crc;
|
2008-02-05 20:35:21 +00:00
|
|
|
char netcmd[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct trickle_conn trickle;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(shell_netcmd_process, "netcmd");
|
|
|
|
PROCESS(shell_netcmd_server_process, "netcmd server");
|
|
|
|
SHELL_COMMAND(netcmd_command,
|
|
|
|
"netcmd",
|
|
|
|
"netcmd <command>: run a command on all nodes in the network",
|
|
|
|
&shell_netcmd_process);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_netcmd_server_process, ev, data)
|
|
|
|
{
|
|
|
|
static struct process *child_command;
|
|
|
|
int err;
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
/* XXX: direct output to null. */
|
2010-03-19 13:20:40 +00:00
|
|
|
printf("netcmd server got command string '%s'\n", (char *)data);
|
2008-02-05 20:35:21 +00:00
|
|
|
err = shell_start_command(data, strlen((char * )data), NULL, &child_command);
|
|
|
|
if(err == SHELL_FOREGROUND && process_is_running(child_command)) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_EXIT ||
|
|
|
|
(ev == PROCESS_EVENT_EXITED &&
|
|
|
|
data == child_command));
|
|
|
|
if(ev == PROCESS_EVENT_EXIT) {
|
|
|
|
process_exit(child_command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(shell_netcmd_process, ev, data)
|
|
|
|
{
|
|
|
|
struct trickle_msg *msg;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2009-04-06 21:17:34 +00:00
|
|
|
/* Get the length of the command line, excluding a terminating NUL character. */
|
2008-02-05 20:35:21 +00:00
|
|
|
len = strlen((char *)data);
|
2009-04-06 21:17:34 +00:00
|
|
|
|
|
|
|
/* Check the length of the command line to see that it is small
|
|
|
|
enough to fit in a packet. We count with 32 bytes of header,
|
|
|
|
which may be a little too much, but at least we are on the safe
|
|
|
|
side. */
|
|
|
|
if(len > PACKETBUF_SIZE - 32) {
|
2008-02-05 20:35:21 +00:00
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "%d", len);
|
|
|
|
shell_output_str(&netcmd_command, "command line too large: ", buf);
|
|
|
|
} else {
|
|
|
|
|
2009-03-12 21:58:20 +00:00
|
|
|
packetbuf_clear();
|
|
|
|
msg = packetbuf_dataptr();
|
2009-04-06 21:17:34 +00:00
|
|
|
packetbuf_set_datalen(len + 1 + TRICKLEMSG_HDR_SIZE);
|
2008-02-05 20:35:21 +00:00
|
|
|
strcpy(msg->netcmd, data);
|
2009-04-06 21:17:34 +00:00
|
|
|
|
|
|
|
/* Terminate the string with a NUL character. */
|
|
|
|
msg->netcmd[len] = 0;
|
2008-07-07 23:22:38 +00:00
|
|
|
msg->crc = crc16_data(msg->netcmd, len, 0);
|
2010-03-19 13:20:40 +00:00
|
|
|
printf("netcmd sending '%s'\n", msg->netcmd);
|
2008-02-05 20:35:21 +00:00
|
|
|
trickle_send(&trickle);
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
recv_trickle(struct trickle_conn *c)
|
|
|
|
{
|
|
|
|
struct trickle_msg *msg;
|
2009-04-06 23:29:31 +00:00
|
|
|
uint16_t crc;
|
2008-07-07 23:22:38 +00:00
|
|
|
int len;
|
2008-02-05 20:35:21 +00:00
|
|
|
|
2009-03-12 21:58:20 +00:00
|
|
|
msg = packetbuf_dataptr();
|
2008-02-05 20:35:21 +00:00
|
|
|
|
2009-04-06 21:17:34 +00:00
|
|
|
if(packetbuf_datalen() > 1 + TRICKLEMSG_HDR_SIZE) {
|
2008-07-07 23:22:38 +00:00
|
|
|
|
|
|
|
/* First ensure that the old process is killed. */
|
|
|
|
process_exit(&shell_netcmd_server_process);
|
2008-07-03 09:51:21 +00:00
|
|
|
|
2009-04-06 21:17:34 +00:00
|
|
|
len = packetbuf_datalen() - 1 - TRICKLEMSG_HDR_SIZE;
|
2008-07-07 23:22:38 +00:00
|
|
|
|
2009-04-06 21:17:34 +00:00
|
|
|
/* Make sure that the incoming command is null-terminated. */
|
2008-07-07 23:22:38 +00:00
|
|
|
msg->netcmd[len] = 0;
|
2009-04-06 23:29:31 +00:00
|
|
|
memcpy(&crc, &msg->crc, sizeof(crc));
|
|
|
|
|
|
|
|
if(crc == crc16_data(msg->netcmd, len, 0)) {
|
2008-07-07 23:22:38 +00:00
|
|
|
/* Start the server process with the incoming command. */
|
2014-06-12 11:40:28 +00:00
|
|
|
process_start(&shell_netcmd_server_process, (void *)msg->netcmd);
|
2008-07-07 23:22:38 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-05 20:35:21 +00:00
|
|
|
}
|
|
|
|
const static struct trickle_callbacks trickle_callbacks = { recv_trickle };
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
shell_rime_netcmd_init(void)
|
|
|
|
{
|
2008-08-15 18:58:42 +00:00
|
|
|
trickle_open(&trickle, CLOCK_SECOND * 4, SHELL_RIME_CHANNEL_NETCMD,
|
|
|
|
&trickle_callbacks);
|
2008-02-05 20:35:21 +00:00
|
|
|
shell_register_command(&netcmd_command);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|