58 #define PRINTF(...) printf(__VA_ARGS__)
65 PROCESS(shell_bin2hex_process,
"bin2hex");
68 "bin2hex: binary to hexadecimal",
69 &shell_bin2hex_process);
70 PROCESS(shell_hex2bin_process,
"hex2bin");
73 "hex2bin: hexadecimal to binary",
74 &shell_hex2bin_process);
75 PROCESS(shell_crc_process,
"crc");
78 "crc: append per-block crc",
81 PROCESS(shell_crcvalidate_process,
"crc-v");
84 "crc-v: verify crc and output if valid",
85 &shell_crcvalidate_process);
89 fromhexchar(
unsigned char c)
92 if(c >=
'0' && c <=
'9') {
94 }
else if(c >=
'a' && c <=
'f') {
96 }
else if(c >=
'A' && c <=
'F') {
99 PRINTF(
"Bad hex input: %c", c);
106 fromhex(
unsigned char c1,
unsigned char c2)
108 return (fromhexchar(c1)<<4) + fromhexchar(c2);
124 if(input->len1 + input->len2 == 0) {
128 buf = alloca((input->len1 + input->len2)*2);
131 for(i = 0; i < input->len1; i++) {
132 bufptr += sprintf(bufptr,
"%02x", 0xff&((
char*)input->data1)[i]);
134 for(i = 0; i < input->len2; i++) {
135 bufptr += sprintf(bufptr,
"%02x", 0xff&((
char*)input->data2)[i]);
140 buf, ((input->len1 + input->len2)*2),
"", 0);
160 if(input->len1 + input->len2 == 0) {
164 if(input->len1 % 2 != 0) {
165 PRINTF(
"Bad input length 1: %d\n", input->len1);
168 if(input->len2 % 2 != 0) {
169 PRINTF(
"Bad input length 2: %d\n", input->len2);
173 buf = alloca((input->len1 + input->len2)/2+1);
176 for(i = 0; i < input->len1; i += 2) {
177 buf[cnt++] = fromhex(
178 ((
char*)input->data1)[i],
179 ((
char*)input->data1)[i+1]);
181 for(i = 0; i < input->len2; i += 2) {
182 buf[cnt++] = fromhex(
183 ((
char*)input->data2)[i],
184 ((
char*)input->data2)[i+1]);
208 if(input->len1 + input->len2 == 0) {
214 for(i = 0; i < input->len1; i++) {
215 crc =
crc16_add(((
char*)(input->data1))[i], crc);
217 for(i = 0; i < input->len2; i++) {
218 crc =
crc16_add(((
char*)(input->data2))[i], crc);
222 buf = alloca(input->len2+2);
224 memcpy(buf, input->data2, input->len2);
225 buf[input->len2] = crc&0xff;
226 buf[input->len2+1] = (crc>>8)&0xff;
228 shell_output(&crc_command, input->data1, input->len1, buf, input->len2+2);
240 uint16_t crc, crc_footer;
251 if(input->len1 + input->len2 == 0) {
255 if(input->len1 + input->len2 < 2) {
257 PRINTF(
"Too short input: %d+%d\n", input->len1, input->len2);
261 if(input->len2 == 1) {
262 crc1 = ((
char*)input->data1)[input->len1-1];
263 crc2 = ((
char*)input->data2)[input->len2-1];
266 }
else if(input->len2 >= 2) {
267 crc1 = ((
char*)input->data2)[input->len2-2];
268 crc2 = ((
char*)input->data2)[input->len2-1];
271 crc1 = ((
char*)input->data1)[input->len1-2];
272 crc2 = ((
char*)input->data1)[input->len1-1];
278 for(i = 0; i < input->len1; i++) {
279 crc =
crc16_add(((
char*)(input->data1))[i], crc);
281 for(i = 0; i < input->len2; i++) {
282 crc =
crc16_add(((
char*)(input->data2))[i], crc);
286 crc_footer = ((0xff&crc2)<<8) | (0xff&crc1);
289 if(crc_footer == crc) {
291 &crcvalidate_command,
292 input->data1, input->len1, input->data2, input->len2);
#define PROCESS_EXIT()
Exit the currently running process.
#define PROCESS_BEGIN()
Define the beginning of a process.
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.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
Structure for shell input data.
void shell_register_command(struct shell_command *c)
Register a command with the shell.
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
#define PROCESS(name, strname)
Declare a process.
int shell_event_input
The event number for shell input data.
unsigned short crc16_add(unsigned char b, unsigned short acc)
Update an accumulated CRC16 checksum with one byte.
Header file for the CRC16 calculcation
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.