Contiki 3.x
shell-rsh.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, 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 /**
34  * \file
35  * A brief description of what this file is.
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "shell.h"
42 
43 #include "net/rime/rime.h"
44 #include "net/rime/meshconn.h"
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 static struct meshconn_conn meshconn;
50 static struct process *front_process;
51 
52 static int initiator = 0;
53 
54 /*---------------------------------------------------------------------------*/
55 PROCESS(shell_rsh_process, "rsh");
56 SHELL_COMMAND(rsh_command,
57  "rsh",
58  "rsh <node>: remote login to another node",
59  &shell_rsh_process);
60 PROCESS(shell_rsh_server_process, "rsh server");
61 /*SHELL_COMMAND(rsh_server_command,
62  "rsh server",
63  "rsh server: never invoked directly",
64  &shell_rsh_server_process);*/
65 /*---------------------------------------------------------------------------*/
66 PROCESS_THREAD(shell_rsh_process, ev, data)
67 {
68  static linkaddr_t receiver;
69  struct shell_input *input;
70  const char *nextptr;
71  char buf[40];
72 
73  PROCESS_BEGIN();
74 
75  receiver.u8[0] = shell_strtolong(data, &nextptr);
76  if(nextptr == data || *nextptr != '.') {
77  shell_output_str(&rsh_command,
78  "rsh <node>: node address must be specified", "");
79  PROCESS_EXIT();
80  }
81  ++nextptr;
82  receiver.u8[1] = shell_strtolong(nextptr, &nextptr);
83 
84  sprintf(buf, "%d.%d", receiver.u8[0], receiver.u8[1]);
85  shell_output_str(&rsh_command, "Connecting to ", buf);
86 
87  initiator = 1;
88  meshconn_connect(&meshconn, &receiver);
89 
90  while(1) {
92  if(ev == shell_event_input) {
93  input = data;
94  if(input->len1 + input->len2 == 0) {
95  meshconn_close_connection(&meshconn);
96  PROCESS_EXIT();
97  }
98  if(input->len1 + input->len2 >= PACKETBUF_SIZE) {
99  shell_output_str(&rsh_command, "rsh: input too long", "");
100  } else {
101  packetbuf_clear();
102  memcpy(packetbuf_dataptr(), input->data1, input->len1);
103  memcpy((char *)packetbuf_dataptr() + input->len1,
104  input->data2, input->len2);
105  packetbuf_set_datalen(input->len1 + input->len2);
106  meshconn_send(&meshconn);
107  }
108  }
109  }
110 
111  PROCESS_END();
112 }
113 /*---------------------------------------------------------------------------*/
114 PROCESS_THREAD(shell_rsh_server_process, ev, data)
115 {
116  struct shell_input *input;
117 
118  PROCESS_BEGIN();
119 
120  while(1) {
122  (ev == PROCESS_EVENT_EXITED &&
123  data == front_process));
124  if(ev == PROCESS_EVENT_EXITED) {
125  front_process = NULL;
126  } else if(ev == shell_event_input) {
127  input = data;
128  packetbuf_clear();
129  memcpy(packetbuf_dataptr(), input->data1, input->len1);
130  memcpy((char *)packetbuf_dataptr() + input->len1,
131  input->data2, input->len2);
132  packetbuf_set_datalen(input->len1 + input->len2);
133  /* printf("Sending meshconn with %d + %d bytes\n",
134  input->len1, input->len2);*/
135  meshconn_send(&meshconn);
136  }
137  /* PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);*/
138  }
139  PROCESS_END();
140 }
141 /*---------------------------------------------------------------------------*/
142 static void
143 connected_meshconn(struct meshconn_conn *c)
144 {
145  /* printf("connected\n");*/
146 }
147 static void
148 recv_meshconn(struct meshconn_conn *c)
149 {
150  /* int ret;
151  struct process *started_process;*/
152 
153  /* printf("got '%.*s'\n", packetbuf_datalen(), (char *)packetbuf_dataptr());*/
154  /* Echo reply */
155  if(!initiator) {
156  packetbuf_copyfrom("abcdefghijklmnopq", 18);
157  meshconn_send(c);
158  }
159 
160  return;
161 #if 0
162  if(front_process != NULL &&
163  process_is_running(front_process)) {
164  struct shell_input input;
165  input.data1 = packetbuf_dataptr();
166  input.len1 = packetbuf_datalen();
167  input.data2 = "";
168  input.len2 = 0;
169  process_post_synch(front_process, shell_event_input, &input);
170  } else {
171 
173  &rsh_server_command, &started_process);
174 
175  if(started_process != NULL &&
176  ret == SHELL_FOREGROUND &&
177  process_is_running(started_process)) {
178  front_process = started_process;
179  }
180  }
181  /* shell_input(packetbuf_dataptr(), packetbuf_datalen());*/
182  /* shell_output(&rsh_command, packetbuf_dataptr(), packetbuf_datalen(), "", 0);*/
183 #endif
184 }
185 static void
186 closed_meshconn(struct meshconn_conn *c)
187 {
188  /* printf("closed\n");*/
189 }
190 static void
191 timedout_meshconn(struct meshconn_conn *c)
192 {
193  /* printf("timedout\n");*/
194 }
195 static void
196 reset_meshconn(struct meshconn_conn *c)
197 {
198  /* printf("reset\n");*/
199 }
200 /*---------------------------------------------------------------------------*/
201 static struct meshconn_callbacks meshconn_callbacks = { connected_meshconn,
202  recv_meshconn,
203  closed_meshconn,
204  timedout_meshconn,
205  reset_meshconn };
206 /*---------------------------------------------------------------------------*/
207 void
208 shell_rsh_init(void)
209 {
210  meshconn_open(&meshconn, 23, &meshconn_callbacks);
211  shell_register_command(&rsh_command);
212 
213  process_start(&shell_rsh_server_process, NULL);
214 }
215 /*---------------------------------------------------------------------------*/
int process_is_running(struct process *p)
Check if a process is running.
Definition: process.c:383
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition: packetbuf.c:93
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
void process_post_synch(struct process *p, process_event_t ev, process_data_t data)
Post a synchronous event to a process.
Definition: process.c:362
Main header file for the Contiki shell
#define NULL
The null pointer.
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
Structure for shell input data.
Definition: shell.h:365
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_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
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
#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
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219