Contiki 3.x
shell-text.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  * Text-related shell commands
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "shell.h"
42 
43 #include <ctype.h>
44 #include <stdio.h>
45 #ifndef HAVE_SNPRINTF
46 int snprintf(char *str, size_t size, const char *format, ...);
47 #endif /* HAVE_SNPRINTF */
48 
49 #include <string.h>
50 
51 /*---------------------------------------------------------------------------*/
52 PROCESS(shell_echo_process, "echo");
53 SHELL_COMMAND(echo_command,
54  "echo",
55  "echo <text>: print <text>",
56  &shell_echo_process);
57 PROCESS(shell_binprint_process, "binprint");
58 SHELL_COMMAND(binprint_command,
59  "binprint",
60  "binprint: print binary data in decimal format",
61  &shell_binprint_process);
62 PROCESS(shell_hd_process, "hd");
63 SHELL_COMMAND(hd_command,
64  "hd",
65  "hd: print binary data in hexadecimal format",
66  &shell_hd_process);
67 PROCESS(shell_size_process, "size");
68 SHELL_COMMAND(size_command,
69  "size",
70  "size: print the size of the input",
71  &shell_size_process);
72 /*---------------------------------------------------------------------------*/
73 PROCESS_THREAD(shell_echo_process, ev, data)
74 {
75  PROCESS_BEGIN();
76 
77  shell_output(&echo_command, data, (int)strlen(data), "", 0);
78 
79  PROCESS_END();
80 }
81 /*---------------------------------------------------------------------------*/
82 PROCESS_THREAD(shell_hd_process, ev, data)
83 {
84  struct shell_input *input;
85  uint16_t *ptr;
86  int i;
87  char buf[57], *bufptr;
88 
89  PROCESS_BEGIN();
90 
91  while(1) {
93  input = data;
94 
95  if(input->len1 + input->len2 == 0) {
96  PROCESS_EXIT();
97  }
98 
99  bufptr = buf;
100  ptr = (uint16_t *)input->data1;
101  for(i = 0; i < input->len1 && i < input->len1 - 1; i += 2) {
102  bufptr += sprintf(bufptr, "0x%04x ", *ptr);
103  if(bufptr - buf >= sizeof(buf) - 7) {
104  shell_output_str(&hd_command, buf, "");
105  bufptr = buf;
106  }
107  ptr++;
108  }
109 
110  ptr = (uint16_t *)input->data2;
111  for(i = 0; i < input->len2 && i < input->len2 - 1; i += 2) {
112  bufptr += sprintf(bufptr, "0x%04x ", *ptr);
113  if(bufptr - buf >= sizeof(buf) - 7) {
114  shell_output_str(&hd_command, buf, "");
115  bufptr = buf;
116  }
117  ptr++;
118  }
119  if(bufptr != buf) {
120  shell_output_str(&hd_command, buf, "");
121  }
122 
123  }
124  PROCESS_END();
125 }
126 /*---------------------------------------------------------------------------*/
127 PROCESS_THREAD(shell_binprint_process, ev, data)
128 {
129  struct shell_input *input;
130  uint16_t *ptr;
131  int i;
132  char buf[2*64], *bufptr;
133  uint16_t val;
134 
135  PROCESS_BEGIN();
136 
137  while(1) {
139  input = data;
140 
141  if(input->len1 + input->len2 == 0) {
142  PROCESS_EXIT();
143  }
144 
145  bufptr = buf;
146  ptr = (uint16_t *)input->data1;
147  for(i = 0; i < input->len1 && i < input->len1 - 1; i += 2) {
148  memcpy(&val, ptr, sizeof(val));
149  bufptr += sprintf(bufptr, "%u ", val);
150  if(bufptr - buf >= sizeof(buf) - 6) {
151  shell_output_str(&binprint_command, buf, "");
152  bufptr = buf;
153  }
154  ptr++;
155  }
156 
157  /* XXX need to check if input->len1 == 1 here, and then shift this
158  byte into the sequence of 16-bitters below. */
159 
160  ptr = (uint16_t *)input->data2;
161  for(i = 0; i < input->len2 && i < input->len2 - 1; i += 2) {
162  memcpy(&val, ptr, sizeof(val));
163  bufptr += sprintf(bufptr, "%u ", val);
164  if(bufptr - buf >= sizeof(buf) - 6) {
165  shell_output_str(&binprint_command, buf, "");
166  bufptr = buf;
167  }
168  ptr++;
169  }
170 
171  if(bufptr != buf) {
172  shell_output_str(&binprint_command, buf, "");
173  }
174 
175  }
176  PROCESS_END();
177 }
178 /*---------------------------------------------------------------------------*/
179 PROCESS_THREAD(shell_size_process, ev, data)
180 {
181  static unsigned long size;
182  struct shell_input *input;
183  char buf[10];
184 
185  PROCESS_BEGIN();
186  size = 0;
187 
188  while(1) {
190  input = data;
191 
192  size += input->len1 + input->len2;
193 
194  if(input->len1 + input->len2 == 0) {
195  snprintf(buf, sizeof(buf), "%lu", size);
196  shell_output_str(&size_command, buf, "");
197  PROCESS_EXIT();
198  }
199  }
200  PROCESS_END();
201 }
202 /*---------------------------------------------------------------------------*/
203 void
204 shell_text_init(void)
205 {
206  shell_register_command(&binprint_command);
207  shell_register_command(&hd_command);
208  shell_register_command(&echo_command);
209  shell_register_command(&size_command);
210 }
211 /*---------------------------------------------------------------------------*/
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#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
Main header file for the Contiki shell
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
#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
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(name, strname)
Declare a process.
Definition: process.h:307
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219