Contiki 3.x
shell-rime-sendcmd.c
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include "contiki.h"
34 #include "contiki-conf.h"
35 #include "shell-rime.h"
36 
37 #include "dev/leds.h"
38 
39 #include "lib/crc16.h"
40 #include "lib/random.h"
41 
42 #include "net/rime/rime.h"
43 #include "net/rime/unicast.h"
44 
45 #include "net/rime/timesynch.h"
46 
47 #if CONTIKI_TARGET_NETSIM
48 #include "ether.h"
49 #endif /* CONTIKI_TARGET_NETSIM */
50 
51 #include <stdio.h>
52 #ifndef HAVE_SNPRINTF
53 int snprintf(char *str, size_t size, const char *format, ...);
54 #endif /* HAVE_SNPRINTF */
55 #include <string.h>
56 
57 #define CMDMSG_HDR_SIZE 2
58 
59 struct cmd_msg {
60  uint16_t crc;
61  char sendcmd[1];
62 };
63 
64 static struct unicast_conn uc;
65 /*---------------------------------------------------------------------------*/
66 PROCESS(shell_sendcmd_process, "sendcmd");
67 PROCESS(shell_sendcmd_server_process, "sendcmd server");
68 SHELL_COMMAND(sendcmd_command,
69  "sendcmd",
70  "sendcmd <node addr> <command>: send a command to the specified one-hop node",
71  &shell_sendcmd_process);
72 /*---------------------------------------------------------------------------*/
73 PROCESS_THREAD(shell_sendcmd_server_process, ev, data)
74 {
75  static struct process *child_command;
76  int err;
77  PROCESS_BEGIN();
78 
79  /* XXX: direct output to null. */
80  /* printf("sendcmd server got command string '%s'\n", (char *)data);*/
81  err = shell_start_command(data, strlen((char * )data), NULL, &child_command);
82  if(err == SHELL_FOREGROUND && process_is_running(child_command)) {
83  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_EXIT ||
84  (ev == PROCESS_EVENT_EXITED &&
85  data == child_command));
86  if(ev == PROCESS_EVENT_EXIT) {
87  process_exit(child_command);
88  }
89  }
90 
91  PROCESS_END();
92 }
93 /*---------------------------------------------------------------------------*/
94 PROCESS_THREAD(shell_sendcmd_process, ev, data)
95 {
96  struct cmd_msg *msg;
97  int len;
98  linkaddr_t addr;
99  const char *nextptr;
100  char buf[32];
101 
102  PROCESS_BEGIN();
103 
104  addr.u8[0] = shell_strtolong(data, &nextptr);
105  if(nextptr == data || *nextptr != '.') {
106  shell_output_str(&sendcmd_command,
107  "sendcmd <node addr>: receiver must be specified", "");
108  PROCESS_EXIT();
109  }
110  ++nextptr;
111  addr.u8[1] = shell_strtolong(nextptr, &nextptr);
112 
113  snprintf(buf, sizeof(buf), "%d.%d", addr.u8[0], addr.u8[1]);
114  shell_output_str(&sendcmd_command, "Sending command to ", buf);
115 
116  /* Get the length of the command line, excluding a terminating NUL character. */
117  len = strlen((char *)nextptr);
118 
119  /* Check the length of the command line to see that it is small
120  enough to fit in a packet. We count with 32 bytes of header,
121  which may be a little too much, but at least we are on the safe
122  side. */
123  if(len > PACKETBUF_SIZE - 32) {
124  snprintf(buf, sizeof(buf), "%d", len);
125  shell_output_str(&sendcmd_command, "command line too large: ", buf);
126  PROCESS_EXIT();
127  }
128 
129  packetbuf_clear();
130  msg = packetbuf_dataptr();
131  packetbuf_set_datalen(len + 1 + CMDMSG_HDR_SIZE);
132  strcpy(msg->sendcmd, nextptr);
133 
134  /* Terminate the string with a NUL character. */
135  msg->sendcmd[len] = 0;
136  msg->crc = crc16_data(msg->sendcmd, len, 0);
137  /* printf("sendcmd sending '%s'\n", msg->sendcmd);*/
138  unicast_send(&uc, &addr);
139 
140  PROCESS_END();
141 }
142 /*---------------------------------------------------------------------------*/
143 static void
144 recv_uc(struct unicast_conn *c, const linkaddr_t *from)
145 {
146  struct cmd_msg *msg;
147  uint16_t crc;
148  int len;
149 
150  msg = packetbuf_dataptr();
151 
152  if(packetbuf_datalen() > 1 + CMDMSG_HDR_SIZE) {
153 
154  /* First ensure that the old process is killed. */
155  process_exit(&shell_sendcmd_server_process);
156 
157  len = packetbuf_datalen() - 1 - CMDMSG_HDR_SIZE;
158 
159  /* Make sure that the incoming command is null-terminated. */
160  msg->sendcmd[len] = 0;
161  memcpy(&crc, &msg->crc, sizeof(crc));
162 
163  if(crc == crc16_data(msg->sendcmd, len, 0)) {
164  /* Start the server process with the incoming command. */
165  process_start(&shell_sendcmd_server_process, (void *)msg->sendcmd);
166  }
167  }
168 }
169 static const struct unicast_callbacks unicast_callbacks = { recv_uc };
170 /*---------------------------------------------------------------------------*/
171 void
172 shell_rime_sendcmd_init(void)
173 {
174  unicast_open(&uc, SHELL_RIME_CHANNEL_SENDCMD, &unicast_callbacks);
175  shell_register_command(&sendcmd_command);
176 }
177 /*---------------------------------------------------------------------------*/
int process_is_running(struct process *p)
Check if a process is running.
Definition: process.c:383
Header file for Rime&#39;s single-hop unicast
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
int shell_start_command(char *commandline, int commandline_len, struct shell_command *child, struct process **started_process)
Start a shell command from another shell command.
Definition: shell.c:308
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define NULL
The null pointer.
Header file for a simple time synchronization mechanism
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
Header file for the Rime stack
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
A brief description of what this file is.
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:65
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
Header file for the CRC16 calculcation
unsigned short crc16_data(const unsigned char *data, int len, unsigned short acc)
Calculate the CRC16 over a data area.
Definition: crc16.c:66
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219