Contiki 3.x
shell-crc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 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  * Block-wise hexadecimal conversion and CRC commands
36  * \author
37  * Fredrik Osterlind <fros@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "shell.h"
42 #include "lib/crc16.h"
43 
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #ifdef __CC65__
50 #define HAVE_ALLOCA 0
51 #else
52 #define HAVE_ALLOCA 1
53 #include <alloca.h>
54 #endif
55 
56 #define DEBUG 0
57 #if DEBUG
58 #include <stdio.h>
59 #define PRINTF(...) printf(__VA_ARGS__)
60 #else
61 #define PRINTF(...)
62 #endif
63 
64 /*---------------------------------------------------------------------------*/
65 #if HAVE_ALLOCA
66 PROCESS(shell_bin2hex_process, "bin2hex");
67 SHELL_COMMAND(bin2hex_command,
68  "bin2hex",
69  "bin2hex: binary to hexadecimal",
70  &shell_bin2hex_process);
71 PROCESS(shell_hex2bin_process, "hex2bin");
72 SHELL_COMMAND(hex2bin_command,
73  "hex2bin",
74  "hex2bin: hexadecimal to binary",
75  &shell_hex2bin_process);
76 PROCESS(shell_crc_process, "crc");
77 SHELL_COMMAND(crc_command,
78  "crc",
79  "crc: append per-block crc",
80  &shell_crc_process);
81 #endif /* HAVE_ALLOCA */
82 PROCESS(shell_crcvalidate_process, "crc-v");
83 SHELL_COMMAND(crcvalidate_command,
84  "crc-v",
85  "crc-v: verify crc and output if valid",
86  &shell_crcvalidate_process);
87 /*---------------------------------------------------------------------------*/
88 #if HAVE_ALLOCA
89 static unsigned char
90 fromhexchar(unsigned char c)
91 {
92  unsigned char h;
93  if(c >= '0' && c <= '9') {
94  h = c-'0';
95  } else if(c >= 'a' && c <= 'f') {
96  h = c-'a'+10;
97  } else if(c >= 'A' && c <= 'F') {
98  h = c-'A'+10;
99  } else {
100  PRINTF("Bad hex input: %c", c);
101  h = 0;
102  }
103  return h;
104 }
105 /*---------------------------------------------------------------------------*/
106 static unsigned char
107 fromhex(unsigned char c1, unsigned char c2)
108 {
109  return (fromhexchar(c1)<<4) + fromhexchar(c2);
110 }
111 /*---------------------------------------------------------------------------*/
112 PROCESS_THREAD(shell_bin2hex_process, ev, data)
113 {
114  struct shell_input *input;
115  int i;
116  char *bufptr;
117  char *buf;
118 
119  PROCESS_BEGIN();
120 
121  while(1) {
123  input = data;
124 
125  if(input->len1 + input->len2 == 0) {
126  PROCESS_EXIT();
127  }
128 
129  buf = alloca((input->len1 + input->len2)*2);
130 
131  bufptr = buf;
132  for(i = 0; i < input->len1; i++) {
133  bufptr += sprintf(bufptr, "%02x", 0xff&((char*)input->data1)[i]);
134  }
135  for(i = 0; i < input->len2; i++) {
136  bufptr += sprintf(bufptr, "%02x", 0xff&((char*)input->data2)[i]);
137  }
138 
139  shell_output(
140  &bin2hex_command,
141  buf, ((input->len1 + input->len2)*2), "", 0);
142  }
143 
144  PROCESS_END();
145 }
146 /*---------------------------------------------------------------------------*/
147 PROCESS_THREAD(shell_hex2bin_process, ev, data)
148 {
149  struct shell_input *input;
150  int i, cnt;
151  char* buf;
152 
153  PROCESS_BEGIN();
154 
155  /* Reads data in hexadecimal format and prints in binary */
156 
157  while(1) {
159  input = data;
160 
161  if(input->len1 + input->len2 == 0) {
162  PROCESS_EXIT();
163  }
164 
165  if(input->len1 % 2 != 0) {
166  PRINTF("Bad input length 1: %d\n", input->len1);
167  continue;
168  }
169  if(input->len2 % 2 != 0) {
170  PRINTF("Bad input length 2: %d\n", input->len2);
171  continue;
172  }
173 
174  buf = alloca((input->len1 + input->len2)/2+1);
175 
176  cnt = 0;
177  for(i = 0; i < input->len1; i += 2) {
178  buf[cnt++] = fromhex(
179  ((char*)input->data1)[i],
180  ((char*)input->data1)[i+1]);
181  }
182  for(i = 0; i < input->len2; i += 2) {
183  buf[cnt++] = fromhex(
184  ((char*)input->data2)[i],
185  ((char*)input->data2)[i+1]);
186  }
187 
188  shell_output(&hex2bin_command, buf, cnt, "", 0);
189  }
190 
191  PROCESS_END();
192 }
193 /*---------------------------------------------------------------------------*/
194 PROCESS_THREAD(shell_crc_process, ev, data)
195 {
196  struct shell_input *input;
197  int i;
198  uint16_t crc;
199  char *buf;
200 
201  PROCESS_BEGIN();
202 
203  /* Append per-block 16-bit CRC */
204 
205  while(1) {
207  input = data;
208 
209  if(input->len1 + input->len2 == 0) {
210  PROCESS_EXIT();
211  }
212 
213  /* calculate crc */
214  crc = 0;
215  for(i = 0; i < input->len1; i++) {
216  crc = crc16_add(((char*)(input->data1))[i], crc);
217  }
218  for(i = 0; i < input->len2; i++) {
219  crc = crc16_add(((char*)(input->data2))[i], crc);
220  }
221 
222  /* input + 16-bit CRC */
223  buf = alloca(input->len2+2);
224 
225  memcpy(buf, input->data2, input->len2);
226  buf[input->len2] = crc&0xff;
227  buf[input->len2+1] = (crc>>8)&0xff;
228 
229  shell_output(&crc_command, input->data1, input->len1, buf, input->len2+2);
230  }
231 
232  PROCESS_END();
233 }
234 #endif /* HAVE_ALLOCA */
235 /*---------------------------------------------------------------------------*/
236 PROCESS_THREAD(shell_crcvalidate_process, ev, data)
237 {
238  struct shell_input *input;
239  int i;
240  char crc1, crc2;
241  uint16_t crc, crc_footer;
242 
243  PROCESS_BEGIN();
244 
245  /* Per-block 16-bit CRC verification:
246  * outputs data without CRCs matches, otherwise nothing */
247 
248  while(1) {
250  input = data;
251 
252  if(input->len1 + input->len2 == 0) {
253  PROCESS_EXIT();
254  }
255 
256  if(input->len1 + input->len2 < 2) {
257  /* too short - no output */
258  PRINTF("Too short input: %d+%d\n", input->len1, input->len2);
259  continue;
260  }
261 
262  if(input->len2 == 1) {
263  crc1 = ((char*)input->data1)[input->len1-1];
264  crc2 = ((char*)input->data2)[input->len2-1];
265  input->len1 -= 1;
266  input->len2 -= 1;
267  } else if(input->len2 >= 2) {
268  crc1 = ((char*)input->data2)[input->len2-2];
269  crc2 = ((char*)input->data2)[input->len2-1];
270  input->len2 -= 2;
271  } else {
272  crc1 = ((char*)input->data1)[input->len1-2];
273  crc2 = ((char*)input->data1)[input->len1-1];
274  input->len1 -= 2;
275  }
276 
277  /* recalculate crc */
278  crc = 0;
279  for(i = 0; i < input->len1; i++) {
280  crc = crc16_add(((char*)(input->data1))[i], crc);
281  }
282  for(i = 0; i < input->len2; i++) {
283  crc = crc16_add(((char*)(input->data2))[i], crc);
284  }
285 
286  /* compare with input crc */
287  crc_footer = ((0xff&crc2)<<8) | (0xff&crc1);
288 
289  /* output if matching crcs */
290  if(crc_footer == crc) {
291  shell_output(
292  &crcvalidate_command,
293  input->data1, input->len1, input->data2, input->len2);
294  }
295  }
296 
297  PROCESS_END();
298 }
299 /*---------------------------------------------------------------------------*/
300 void
301 shell_crc_init(void)
302 {
303 #if HAVE_ALLOCA
304  shell_register_command(&bin2hex_command);
305  shell_register_command(&hex2bin_command);
306  shell_register_command(&crc_command);
307 #endif /* HAVE_ALLOCA */
308  shell_register_command(&crcvalidate_command);
309 }
310 /*---------------------------------------------------------------------------*/
Main header file for the Contiki shell
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
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 SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
Structure for shell input data.
Definition: shell.h:365
unsigned short crc16_add(unsigned char b, unsigned short acc)
Update an accumulated CRC16 checksum with one byte.
Definition: crc16.c:47
Header file for the CRC16 calculcation
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120