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