From 3661ff242eac64ce8ba5d887eb3a6a3b7c30ece2 Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Tue, 13 Dec 2011 17:34:31 +0100 Subject: [PATCH 1/7] io, signal and IAR-GCC checks have been moved into contiki include file --- platform/z1/dev/adxl345.h | 18 +----------------- platform/z1/dev/i2cmaster.h | 18 +----------------- platform/z1/msp430.c | 7 +------ platform/z1/platform-conf.h | 26 +++++++++++++++++++++++--- platform/z1sp/platform-conf.h | 22 ++++++++++++++++++++++ 5 files changed, 48 insertions(+), 43 deletions(-) diff --git a/platform/z1/dev/adxl345.h b/platform/z1/dev/adxl345.h index c7b23be21..0eb80ccff 100644 --- a/platform/z1/dev/adxl345.h +++ b/platform/z1/dev/adxl345.h @@ -41,7 +41,7 @@ #ifndef __ADXL345_H__ #define __ADXL345_H__ #include -#include "i2cmaster.h" +#include "dev/i2cmaster.h" #define DEBUGLEDS 0 #if DEBUGLEDS @@ -62,21 +62,6 @@ #define L_ON(x) (LEDS_PxOUT &= ~x) #define L_OFF(x) (LEDS_PxOUT |= x) -//XXX Temporary place for defines that are lacking in mspgcc4's gpio.h -#ifdef __GNUC__ -#ifndef P1SEL2_ - #define P1SEL2_ 0x0041 /* Port 1 Selection 2*/ - sfrb(P1SEL2, P1SEL2_); -#endif -#endif -#ifdef __IAR_SYSTEMS_ICC__ -#ifndef P1SEL2_ -#define P1SEL2_ (0x0041u) /* Port 1 Selection 2*/ -DEFC( P1SEL2 , P1SEL2_) -#endif -#endif - - /* Used in accm_read_axis(), eg accm_read_axis(X_AXIS);*/ enum ADXL345_AXIS { X_AXIS = 0, @@ -306,4 +291,3 @@ process_event_t int1_event, int2_event; // static ? /* -------------------------------------------------------------------------- */ #endif /* ifndef __ADXL345_H__ */ - diff --git a/platform/z1/dev/i2cmaster.h b/platform/z1/dev/i2cmaster.h index 2f11e1067..beccf3f5f 100644 --- a/platform/z1/dev/i2cmaster.h +++ b/platform/z1/dev/i2cmaster.h @@ -41,12 +41,7 @@ #ifndef __I2CMASTER_H__ #define __I2CMASTER_H__ -#include #include "contiki.h" -#include -#include - - void i2c_enable(void); @@ -58,18 +53,6 @@ void i2c_transmit_n(u8_t byte_ctr, u8_t *tx_buf); u8_t i2c_busy(void); -//XXX Temporary place for defines that are lacking in mspgcc4's gpio.h -#ifdef __GNUC__ -#ifndef P5SEL2_ - #define P5SEL2_ 0x0045 /* Port 5 Selection 2*/ - sfrb(P5SEL2, P5SEL2_); -#endif -#endif -#ifdef __IAR_SYSTEMS_ICC__ -#define P5SEL2_ (0x0045u) /* Port 5 Selection 2*/ -DEFC( P5SEL2 , P5SEL2_) -#endif - //XXX Should these defines be in the contiki-conf.h to make it more platform-independent? #define I2C_PxDIR P5DIR #define I2C_PxIN P5IN @@ -97,6 +80,7 @@ DEFC( P5SEL2 , P5SEL2_) #if 0 +#include #define PRINTFDEBUG(...) printf(__VA_ARGS__) #else #define PRINTFDEBUG(...) diff --git a/platform/z1/msp430.c b/platform/z1/msp430.c index a46902743..920783940 100644 --- a/platform/z1/msp430.c +++ b/platform/z1/msp430.c @@ -33,12 +33,7 @@ #include "contiki.h" -#ifdef __IAR_SYSTEMS_ICC__ -#include -#else -#include -#include -#include +#ifndef __IAR_SYSTEMS_ICC__ #define asmv(arg) __asm__ __volatile__(arg) #endif diff --git a/platform/z1/platform-conf.h b/platform/z1/platform-conf.h index d91e49f33..496f49295 100644 --- a/platform/z1/platform-conf.h +++ b/platform/z1/platform-conf.h @@ -60,10 +60,8 @@ #define CC_CONF_INLINE inline #endif - /* CPU target speed in Hz */ -/* CPU target speed in Hz */ -#define F_CPU 8000000uL // 8MHz by default +#define F_CPU 8000000uL /* 8MHz by default */ //Enric #define F_CPU 3900000uL /*2457600uL*/ /* Our clock resolution, this is the same as Unix HZ. */ @@ -78,6 +76,28 @@ #define MSP430_MEMCPY_WORKAROUND 1 #include "msp430def.h" +/* XXX Temporary place for defines that are lacking in mspgcc4's gpio.h */ +#ifdef __IAR_SYSTEMS_ICC__ +#ifndef P1SEL2_ +#define P1SEL2_ (0x0041u) /* Port 1 Selection 2*/ +DEFC( P1SEL2 , P1SEL2_) +#endif +#ifndef P5SEL2_ +#define P5SEL2_ (0x0045u) /* Port 5 Selection 2*/ +DEFC( P5SEL2 , P5SEL2_) +#endif +#else /* __IAR_SYSTEMS_ICC__ */ +#ifdef __GNUC__ +#ifndef P1SEL2_ + #define P1SEL2_ 0x0041 /* Port 1 Selection 2*/ + sfrb(P1SEL2, P1SEL2_); +#endif +#ifndef P5SEL2_ + #define P5SEL2_ 0x0045 /* Port 5 Selection 2*/ + sfrb(P5SEL2, P5SEL2_); +#endif +#endif /* __GNUC__ */ +#endif /* __IAR_SYSTEMS_ICC__ */ /* Types for clocks and uip_stats */ typedef unsigned short uip_stats_t; diff --git a/platform/z1sp/platform-conf.h b/platform/z1sp/platform-conf.h index a532d5994..063811eff 100644 --- a/platform/z1sp/platform-conf.h +++ b/platform/z1sp/platform-conf.h @@ -65,6 +65,28 @@ #define MSP430_MEMCPY_WORKAROUND 1 #include "msp430def.h" +/* XXX Temporary place for defines that are lacking in mspgcc4's gpio.h */ +#ifdef __IAR_SYSTEMS_ICC__ +#ifndef P1SEL2_ +#define P1SEL2_ (0x0041u) /* Port 1 Selection 2*/ +DEFC( P1SEL2 , P1SEL2_) +#endif +#ifndef P5SEL2_ +#define P5SEL2_ (0x0045u) /* Port 5 Selection 2*/ +DEFC( P5SEL2 , P5SEL2_) +#endif +#else /* __IAR_SYSTEMS_ICC__ */ +#ifdef __GNUC__ +#ifndef P1SEL2_ + #define P1SEL2_ 0x0041 /* Port 1 Selection 2*/ + sfrb(P1SEL2, P1SEL2_); +#endif +#ifndef P5SEL2_ + #define P5SEL2_ 0x0045 /* Port 5 Selection 2*/ + sfrb(P5SEL2, P5SEL2_); +#endif +#endif /* __GNUC__ */ +#endif /* __IAR_SYSTEMS_ICC__ */ /* Types for clocks and uip_stats */ typedef unsigned short uip_stats_t; From c0cf69ecbd1ac1c0fd1469925a924fe5b94e3815 Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Tue, 13 Dec 2011 17:35:04 +0100 Subject: [PATCH 2/7] fixed compiler warnings --- examples/z1/test-adxl345.c | 5 ++++- platform/z1/contiki-z1-main.c | 1 + platform/z1/dev/xmem.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/z1/test-adxl345.c b/examples/z1/test-adxl345.c index b2b95d10d..32c29c786 100644 --- a/examples/z1/test-adxl345.c +++ b/examples/z1/test-adxl345.c @@ -43,7 +43,10 @@ #include #include "contiki.h" #include "serial-shell.h" -#include "adxl345.h" +#include "shell-ps.h" +#include "shell-file.h" +#include "shell-text.h" +#include "dev/adxl345.h" #define LED_INT_ONTIME CLOCK_SECOND/2 #define ACCM_READ_INTERVAL CLOCK_SECOND diff --git a/platform/z1/contiki-z1-main.c b/platform/z1/contiki-z1-main.c index a8bcc0aa1..40731f5e3 100644 --- a/platform/z1/contiki-z1-main.c +++ b/platform/z1/contiki-z1-main.c @@ -45,6 +45,7 @@ #include "net/netstack.h" #include "net/mac/frame802154.h" #include "dev/button-sensor.h" +#include "dev/adxl345.h" #if WITH_UIP6 #include "net/uip-ds6.h" diff --git a/platform/z1/dev/xmem.c b/platform/z1/dev/xmem.c index f2bd1632b..888648046 100644 --- a/platform/z1/dev/xmem.c +++ b/platform/z1/dev/xmem.c @@ -189,7 +189,7 @@ xmem_pread(void *_p, int size, unsigned long offset) return size; } /*---------------------------------------------------------------------------*/ -static const char * +static const unsigned char * program_page(unsigned long offset, const unsigned char *p, int nbytes) { const unsigned char *end = p + nbytes; From d14d90ea680a0b76f71c6718251f6db56b0fcfa4 Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Tue, 20 Dec 2011 04:59:08 +0100 Subject: [PATCH 3/7] Moved variable definitions from header file --- examples/z1/test-adxl345.c | 4 ++-- platform/z1/dev/adxl345.c | 6 ++++-- platform/z1/dev/adxl345.h | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/z1/test-adxl345.c b/examples/z1/test-adxl345.c index 32c29c786..005c8a7b4 100644 --- a/examples/z1/test-adxl345.c +++ b/examples/z1/test-adxl345.c @@ -179,8 +179,8 @@ PROCESS_THREAD(accel_process, ev, data) { accm_init(); /* Register the callback functions for each interrupt */ - ACCM_REGISTER_INT1_CB((void *)accm_ff_cb); - ACCM_REGISTER_INT2_CB((void *)accm_tap_cb); + ACCM_REGISTER_INT1_CB(accm_ff_cb); + ACCM_REGISTER_INT2_CB(accm_tap_cb); /* Set what strikes the corresponding interrupts. Several interrupts per pin is possible. For the eight possible interrupts, see adxl345.h and adxl345 datasheet. */ diff --git a/platform/z1/dev/adxl345.c b/platform/z1/dev/adxl345.c index 6ddbcbc10..3cd86f6e3 100644 --- a/platform/z1/dev/adxl345.c +++ b/platform/z1/dev/adxl345.c @@ -46,8 +46,10 @@ #include "i2cmaster.h" /* Callback pointers when interrupt occurs */ -extern void (*accm_int1_cb)(u8_t reg); -extern void (*accm_int2_cb)(u8_t reg); +void (*accm_int1_cb)(u8_t reg); +void (*accm_int2_cb)(u8_t reg); + +process_event_t int1_event, int2_event; /* Bitmasks for the interrupts */ static uint16_t int1_mask = 0, int2_mask = 0; diff --git a/platform/z1/dev/adxl345.h b/platform/z1/dev/adxl345.h index 0eb80ccff..8da72cf5c 100644 --- a/platform/z1/dev/adxl345.h +++ b/platform/z1/dev/adxl345.h @@ -278,12 +278,12 @@ void accm_set_irq(uint8_t int1, uint8_t int2); #define ADXL345_SRATE_0_10 0x00 // 0.10 Hz, when I2C data rate >= 100 kHz /* Callback pointers for the interrupts */ -void (*accm_int1_cb)(u8_t reg); -void (*accm_int2_cb)(u8_t reg); +extern void (*accm_int1_cb)(u8_t reg); +extern void (*accm_int2_cb)(u8_t reg); /* Interrupt 1 and 2 events; ADXL345 signals interrupt on INT1 or INT2 pins, ISR is invoked and polls the accelerometer process which invokes the callbacks. */ -process_event_t int1_event, int2_event; // static ? +extern process_event_t int1_event, int2_event; // static ? #define ACCM_INT1 0x01 #define ACCM_INT2 0x02 From 28e5cac1ea3e978078d9f5aed63bfd3b38999d0a Mon Sep 17 00:00:00 2001 From: Niclas Finne Date: Tue, 20 Dec 2011 04:53:52 +0100 Subject: [PATCH 4/7] Sensors already included by platform --- examples/z1/Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/z1/Makefile b/examples/z1/Makefile index 941441db1..6cbbaa9c4 100644 --- a/examples/z1/Makefile +++ b/examples/z1/Makefile @@ -3,8 +3,7 @@ TARGET=z1 endif CONTIKI_PROJECT = test-phidgets blink test-adxl345 tmp102-test test-battery test-sht11 #test-potent -CONTIKI_SOURCEFILES += cc2420-arch.c sensors.c sht11.c potentiometer-sensor.c -PROJECT_SOURCEFILES = i2cmaster.c tmp102.c adxl345.c battery-sensor.c sky-sensors.c #potentiometer-sensor.c +CONTIKI_SOURCEFILES += sht11.c potentiometer-sensor.c APPS=serial-shell From e7430392612133bac81b4c3f5ba8a3b661447358 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Wed, 21 Dec 2011 10:51:23 +0100 Subject: [PATCH 5/7] no newline added by echo command --- apps/shell/shell-text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/shell/shell-text.c b/apps/shell/shell-text.c index d80d3cb4e..67c816adc 100644 --- a/apps/shell/shell-text.c +++ b/apps/shell/shell-text.c @@ -75,7 +75,7 @@ PROCESS_THREAD(shell_echo_process, ev, data) { PROCESS_BEGIN(); - shell_output(&echo_command, data, (int)strlen(data), "\n", 1); + shell_output(&echo_command, data, (int)strlen(data), "", 0); PROCESS_END(); } From 2644057fb2cd3d4028b7d10dd8468bbca6ebe96e Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Wed, 21 Dec 2011 10:53:18 +0100 Subject: [PATCH 6/7] using dma by default --- cpu/msp430/dev/uart1.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/cpu/msp430/dev/uart1.c b/cpu/msp430/dev/uart1.c index 5bcf6f694..5d8faaa81 100644 --- a/cpu/msp430/dev/uart1.c +++ b/cpu/msp430/dev/uart1.c @@ -55,13 +55,9 @@ static volatile uint8_t transmitting; #ifdef UART1_CONF_RX_WITH_DMA #define RX_WITH_DMA UART1_CONF_RX_WITH_DMA #else /* UART1_CONF_RX_WITH_DMA */ -#define RX_WITH_DMA 0 +#define RX_WITH_DMA 1 #endif /* UART1_CONF_RX_WITH_DMA */ -#if RX_WITH_DMA -#warning RX_WITH_DMA ENABLED - WILL NOT WORK WITH MSPSIM / COOJA! -#endif /* RX_WITH_DMA */ - #if TX_WITH_INTERRUPT #define TXBUFSIZE 128 From abc4cee94d42546ff03b1a98df094b7eefe986b8 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Wed, 21 Dec 2011 10:54:39 +0100 Subject: [PATCH 7/7] hex conversion and crc shell commands --- apps/shell/Makefile.shell | 2 +- apps/shell/shell-crc.c | 298 ++++++++++++++++++++++++++++++++++++++ apps/shell/shell-crc.h | 39 +++++ 3 files changed, 338 insertions(+), 1 deletion(-) create mode 100644 apps/shell/shell-crc.c create mode 100644 apps/shell/shell-crc.h diff --git a/apps/shell/Makefile.shell b/apps/shell/Makefile.shell index f873dbabc..5b552a180 100644 --- a/apps/shell/Makefile.shell +++ b/apps/shell/Makefile.shell @@ -11,7 +11,7 @@ shell_src = shell.c shell-reboot.c \ shell-rime-unicast.c \ shell-tweet.c shell-base64.c \ shell-netperf.c shell-memdebug.c \ - shell-powertrace.c shell-collect-view.c + shell-powertrace.c shell-collect-view.c shell-crc.c shell_dsc = shell-dsc.c APPS += webserver diff --git a/apps/shell/shell-crc.c b/apps/shell/shell-crc.c new file mode 100644 index 000000000..a60043253 --- /dev/null +++ b/apps/shell/shell-crc.c @@ -0,0 +1,298 @@ +/* + * 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. + * + * $Id: shell-text.c,v 1.7 2010/09/13 13:29:47 adamdunkels Exp $ + */ + +/** + * \file + * Block-wise hexadecimal conversion and CRC commands + * \author + * Fredrik Osterlind + */ + +#include "contiki.h" +#include "shell.h" +#include "lib/crc16.h" + +#include +#include +#include +#include + +#define DEBUG 0 +#if DEBUG +#include +#define PRINTF(...) printf(__VA_ARGS__) +#else +#define PRINTF(...) +#endif + +/*---------------------------------------------------------------------------*/ +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); +PROCESS(shell_crcvalidate_process, "crc-v"); +SHELL_COMMAND(crcvalidate_command, + "crc-v", + "crc-v: verify crc and output if valid", + &shell_crcvalidate_process); +/*---------------------------------------------------------------------------*/ +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(); +} +/*---------------------------------------------------------------------------*/ +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) +{ + shell_register_command(&bin2hex_command); + shell_register_command(&hex2bin_command); + shell_register_command(&crc_command); + shell_register_command(&crcvalidate_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-crc.h b/apps/shell/shell-crc.h new file mode 100644 index 000000000..c7fd1574e --- /dev/null +++ b/apps/shell/shell-crc.h @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#ifndef __SHELL_CRC_H__ +#define __SHELL_CRC_H__ + +#include "shell.h" + +void shell_crc_init(void); + +#endif /* __SHELL_CRC_H__ */