From d56dbaf07bf8dfc0c373cfee190bb66cf77da33c Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 6 Aug 2011 16:24:50 +0200 Subject: [PATCH 1/5] Enhance serial badge stuff. --- firmware/Makefile | 4 + firmware/SERIAL_DOKU | 20 ++- firmware/applications/serial.c | 20 +++ firmware/applications/serial/serial.c | 210 +++++++++++++++++--------- tools/bootloader/autoflash-cygwin.sh | 4 + 5 files changed, 178 insertions(+), 80 deletions(-) create mode 100644 firmware/applications/serial.c diff --git a/firmware/Makefile b/firmware/Makefile index 349b4ff..40e36bc 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -10,8 +10,12 @@ VPATH += OBJS += main.o ifeq "$(wildcard table.c)" "table.c" +ifeq "$(APP)" "serial" +OBJS += +else OBJS += table.o endif +endif LIBS += lcd/liblcd.a LIBS += basic/libbasic.a diff --git a/firmware/SERIAL_DOKU b/firmware/SERIAL_DOKU index d67b459..d49daca 100644 --- a/firmware/SERIAL_DOKU +++ b/firmware/SERIAL_DOKU @@ -1,11 +1,17 @@ i: initialize -cb / cm / cr: config funk for beacon/mesh/remote -Ck: set encryptionkey -Ct: set txmac -Cc: set channel -Ct: set rxmac(0) -Cl: set rxlens -Ce: sec encrpytion on/off +pb: preset funk for beacon +pm: preset funk for mesh +pM: preset funk for pubMesh +pr: preset funk for rem0te + +t: set packet dump type (b/m/0) + +ck: set encryptionkey +cm: set rxmac(0) +ct: set txmac +cc: set channel +cl: set rxlen +ce: sec encrpytion on/off s : send packet s+ : send packet 10 times diff --git a/firmware/applications/serial.c b/firmware/applications/serial.c new file mode 100644 index 0000000..acb5cf4 --- /dev/null +++ b/firmware/applications/serial.c @@ -0,0 +1,20 @@ +#include + +#include "basic/basic.h" +#include "basic/config.h" + +#include + +/**************************************************************************/ + +#include "serial.gen" + +void main_serial(void) { + GLOBAL(daytrig)=10; + GLOBAL(lcdbacklight)=10; + handleMenu(&mainmenu); + gpioSetValue (RB_LED3, 1); + ISPandReset(); +}; + +void serial(void); diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index e780fbf..14490f9 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -23,7 +23,7 @@ #define BEACON_CHANNEL 81 #define BEACON_MAC "\x1\x2\x3\x2\1" -#include "SECRETS" +#include "SECRETS.release" char funkencrypt=0; @@ -47,9 +47,12 @@ struct NRF_CFG config = { .maclen ="\x10", }; -int process(char * input); +char type=0; + +static int process(char * input); #define INPUTLEN 99 +//# MENU serial void dwim(void){ char input[INPUTLEN+1]; int inputptr=0; @@ -58,6 +61,8 @@ void dwim(void){ usbCDCInit(); delayms(500); getInputWaitRelease(); + nrf_init(); + gpioSetValue (RB_LED1, 0); puts("D start\r\n"); while(!getInputRaw()){ @@ -97,13 +102,13 @@ void dwim(void){ }; inputptr+=l; }; - + gpioSetValue (RB_LED1, 0); puts("D exit\r\n"); } #define BUFLEN 32 #define NYB(x) ((x>'9')?(x|0x20)-'a'+10:x-'0') -uint8_t * hextobyte(char * input, int *len){ +static uint8_t * hextobyte(char * input, int *len){ static uint8_t buf[BUFLEN]; int p=0; @@ -123,7 +128,16 @@ uint8_t * hextobyte(char * input, int *len){ return buf; }; -int process(char * input){ +static uint32_t gethexval(char * input){ + int len; + uint8_t *hex=hextobyte(input,&len); + uint32_t v=hex[0]; + while(--len>0) + v=v*256+(*++hex); + return v; +}; + +static int process(char * input){ if(input == NULL || input[0]==0) return -1; @@ -135,7 +149,8 @@ int process(char * input){ if(input[0]=='i'){ nrf_init(); nrf_config_set(&config); - }else if(input[0]=='c'){ + }else if(input[0]=='p'){ + type=input[1]; if(input[1]=='m'){ config.channel=MESH_CHANNEL; memcpy(config.txmac,MESH_MAC,5); @@ -145,6 +160,18 @@ int process(char * input){ nrf_config_set(&config); memcpy(thekey,meshkey,sizeof(thekey)); funkencrypt=1; + }else if(input[1]=='M'){ + config.channel=83; + memcpy(config.txmac,MESH_MAC,5); + memcpy(config.mac0,MESH_MAC,5); + config.maclen[0]=0x20; + config.nrmacs=1; + nrf_config_set(&config); + static const uint32_t const pubmesh[4] = { + 0x00000042, 0x000005ec, 0x00000023, 0x00000005 + }; + memcpy(thekey,pubmesh,sizeof(thekey)); + funkencrypt=1; }else if(input[1]=='r'){ config.channel=REMOTE_CHANNEL; memcpy(config.txmac,REMOTE_MAC,5); @@ -163,6 +190,43 @@ int process(char * input){ nrf_config_set(&config); memcpy(thekey,openbeaconkey,sizeof(thekey)); funkencrypt=1; + }else if(input[1]=='B'){ + config.channel=BEACON_CHANNEL; + memcpy(config.txmac,BEACON_MAC,5); + memcpy(config.mac0,BEACON_MAC,5); + config.maclen[0]=0x10; + config.nrmacs=1; + nrf_config_set(&config); + static const uint32_t pubbeaconkey[4] = { + 0xB4595344, 0xD3E119B6, 0xA814D0EC, 0xEFF5A24E + }; + memcpy(thekey,pubbeaconkey,sizeof(thekey)); + funkencrypt=1; + }; + }else if(input[0]=='t'){ + type=input[1]; + }else if(input[0]=='c'){ + int len; + uint8_t *hex=hextobyte(&input[2],&len); + if(input[1]=='k'){ + thekey[0]=uint8ptouint32(hex); + thekey[1]=uint8ptouint32(hex+4); + thekey[2]=uint8ptouint32(hex+8); + thekey[3]=uint8ptouint32(hex+12); + }else if(input[1]=='m'){ + memcpy(config.mac0,hex,5); + nrf_config_set(&config); + }else if(input[1]=='t'){ + memcpy(config.txmac,hex,5); + nrf_config_set(&config); + }else if(input[1]=='c'){ + config.channel=hex[0]; + nrf_config_set(&config); + }else if(input[1]=='l'){ + config.maclen[0]=hex[0]; + nrf_config_set(&config); + }else if(input[1]=='e'){ + funkencrypt= hex[0]; }else if(input[1]=='?'){ nrf_config_get(&config); puts_plus("Ch: ");puts_plus(IntToStrX( config.channel,2 )); puts_plus("\r\n"); @@ -203,41 +267,6 @@ int process(char * input){ puts_plus(IntToStrX( funkencrypt,2 )); puts_plus("\r\n"); }; - }else if(input[0]=='C'){ - int len; - uint8_t *hex=hextobyte(&input[2],&len); - if(input[1]=='k'){ - thekey[0]=uint8ptouint32(hex); - thekey[1]=uint8ptouint32(hex+4); - thekey[2]=uint8ptouint32(hex+8); - thekey[3]=uint8ptouint32(hex+12); - }else if(input[1]=='m'){ - config.mac0[0]=uint8ptouint32(hex); - config.mac0[1]=uint8ptouint32(hex+4); - config.mac0[2]=uint8ptouint32(hex+8); - config.mac0[3]=uint8ptouint32(hex+12); - config.mac0[4]=uint8ptouint32(hex+16); - nrf_config_set(&config); - }else if(input[1]=='t'){ - config.txmac[0]=uint8ptouint32(hex); - config.txmac[1]=uint8ptouint32(hex+4); - config.txmac[2]=uint8ptouint32(hex+8); - config.txmac[3]=uint8ptouint32(hex+12); - config.txmac[4]=uint8ptouint32(hex+16); - nrf_config_set(&config); - }else if(input[1]=='c'){ - config.channel=*hex; - nrf_config_set(&config); - }else if(input[1]=='l'){ - config.maclen[0]=uint8ptouint32(hex); - config.maclen[1]=uint8ptouint32(hex+4); - config.maclen[2]=uint8ptouint32(hex+8); - config.maclen[3]=uint8ptouint32(hex+12); - config.maclen[4]=uint8ptouint32(hex+16); - nrf_config_set(&config); - }else if(input[1]=='e'){ - funkencrypt= uint8ptouint32(hex); - }; }else if (input[0]=='s'){ __attribute__ ((aligned (4))) uint8_t buf[32]; int status=0; @@ -262,14 +291,18 @@ int process(char * input){ memcpy(buf,hex,len); status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL); - puts_plus("P "); - puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); if(debug){ + puts_plus("P "); + puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); for(int i=0;i0){ @@ -277,46 +310,34 @@ int process(char * input){ memcpy(buf,hex,len); status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL); }; - }else if (input[1]=='t'){ - static int ctr=1; - int status; - - buf[0]=0x10; // Length: 16 bytes - buf[1]='1'; // Proto - buf[2]=0x00; - buf[3]=0x00; // Unused - - uint32touint8p(ctr++,buf+4); - - uint32touint8p(0x5ec,buf+8); - - buf[12]=0xff; // salt (0xffff always?) - buf[13]=0xff; - status=nrf_snd_pkt_crc_encr(16,buf,funkencrypt?thekey:NULL); }else{ + ; }; - puts_plus("S state="); - puts_plus(IntToStrX( status,2 )); puts_plus("\r\n"); }else if (input[0]=='r'){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; int pctr=5; - int t=getTimer()+5000/SYSTICKSPEED; + int t=5; if(input[1]=='+'){ if(input[2]!=0){ - uint8_t *hex=hextobyte(&input[2],&len); - t=getTimer()+hex[0]*1000/SYSTICKSPEED; + t=gethexval(&input[2]); }; pctr=-1; } if(input[1]=='-'){ - uint8_t *hex=hextobyte(&input[2],&len); - pctr=hex[0]; + pctr=gethexval(&input[2]); + t=-1; } - puts_plus("D receive ...\r\n"); + puts_plus("D rcv: "); + puts_plus(IntToStr(pctr,8,0)); + puts_plus("pkts, "); + puts_plus(IntToStr(t,8,0)); + puts_plus("secs ...\r\n"); + if(t>0) + t=getTimer()+t*1000/SYSTICKSPEED; nrf_rcv_pkt_start(); do{ len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,funkencrypt?thekey:NULL); @@ -326,14 +347,57 @@ int process(char * input){ continue; }; puts_plus("R "); - puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); if(len==-3){ puts_plus("[!crc] "); - len=16; + len=32; }; - for(int i=0;igetTimer()); + }while(t>((t==-1)?getTimer():0)); nrf_rcv_pkt_end(); }else{ diff --git a/tools/bootloader/autoflash-cygwin.sh b/tools/bootloader/autoflash-cygwin.sh index ee86308..e363880 100644 --- a/tools/bootloader/autoflash-cygwin.sh +++ b/tools/bootloader/autoflash-cygwin.sh @@ -1,4 +1,8 @@ drive=/cygdrive/f +if [ ! -z "$1" ] ; then + dirve=/cygdrive/$a +fi + file=$drive/firmware.bin while : ; do From 9f963642b337d7884c6bb711e41081b4c1bd421e Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sat, 6 Aug 2011 16:52:29 +0200 Subject: [PATCH 2/5] rudimentary filter stuff --- firmware/SERIAL_DOKU | 5 ++- firmware/applications/serial/serial.c | 63 +++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/firmware/SERIAL_DOKU b/firmware/SERIAL_DOKU index d49daca..99bb071 100644 --- a/firmware/SERIAL_DOKU +++ b/firmware/SERIAL_DOKU @@ -4,7 +4,8 @@ pm: preset funk for mesh pM: preset funk for pubMesh pr: preset funk for rem0te -t: set packet dump type (b/m/0) +t: set packet dump type (b/B/m/M/0) +f: set filter stuff ck: set encryptionkey cm: set rxmac(0) @@ -19,4 +20,4 @@ sd : send packet with debugoutput r: recv packets r+: recv packets for seconds -r-: recv packets (5 seconds max) +r-: recv packets diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index 14490f9..30ffae2 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -48,6 +48,9 @@ struct NRF_CFG config = { }; char type=0; +char filter=0; +uint8_t filterdata[10]; +char outc[2]= {0,0}; static int process(char * input); @@ -314,6 +317,26 @@ static int process(char * input){ ; }; puts_plus("\r\n"); + }else if (input[0]=='f'){ + int len; + filter=input[1]; + uint8_t *hex=hextobyte(input+2,&len); + if(len>9) + len=9; + memcpy(filterdata,hex,len); + puts_plus("F "); + if(filter){ + puts_plus("["); + outc[0]=filter; + puts_plus(outc); + puts_plus("]"); + for(int i=0;i0) t=getTimer()+t*1000/SYSTICKSPEED; nrf_rcv_pkt_start(); @@ -346,12 +375,16 @@ static int process(char * input){ delayms(10); continue; }; - puts_plus("R "); - if(len==-3){ - puts_plus("[!crc] "); - len=32; + if (!filter){ + if(len==-3){ + puts_plus("[!crc] "); + len=32; + }; }; - if(type=='m'){ + if(type=='m' || type=='M'){ + if(filter && filter!=buf[0]) + continue; + puts_plus("R "); puts_plus(IntToStrX( buf[0],2 )); puts_plus(" "); puts_plus(IntToStrX( buf[1],2 )); @@ -373,7 +406,19 @@ static int process(char * input){ puts_plus(IntToStrX( buf[30],2 )); puts_plus(IntToStrX( buf[31],2 )); puts_plus(" "); + }else if(type=='B'){ + if(filter) + if(uint8ptouint32(buf+8)!=uint8ptouint32(filterdata)) + continue; + puts_plus("RF "); + puts_plus(IntToStrX( buf[2],2 )); + puts_plus(" "); + puts_plus(IntToStrX( uint8ptouint32(buf+8),8 )); }else if(type=='b'){ + if(filter) + if(uint8ptouint32(buf+8)!=uint8ptouint32(filterdata)) + continue; + puts_plus("R "); puts_plus(IntToStrX( buf[0],2 )); puts_plus(" "); puts_plus(IntToStrX( buf[1],2 )); @@ -391,15 +436,15 @@ static int process(char * input){ puts_plus(" "); puts_plus(IntToStrX( buf[14],2 )); puts_plus(IntToStrX( buf[15],2 )); - puts_plus(" "); }else{ + puts_plus("R "); puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); for(int i=0;i0){ @@ -410,7 +455,7 @@ static int process(char * input){ puts("\r\n"); if(--pctr==0) break; - }while(t>((t==-1)?getTimer():0)); + }while((t>0)?(t>getTimer()):1); nrf_rcv_pkt_end(); }else{ From 75ec951d73b4f107f075c2cef20cfddad3cc381f Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 7 Aug 2011 01:44:25 +0200 Subject: [PATCH 3/5] First attemt at serial connection --- firmware/applications/uart.c | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 firmware/applications/uart.c diff --git a/firmware/applications/uart.c b/firmware/applications/uart.c new file mode 100644 index 0000000..8712a86 --- /dev/null +++ b/firmware/applications/uart.c @@ -0,0 +1,49 @@ +#include + +#include "basic/basic.h" + +#include "lcd/print.h" + +#include + + +#include "core/cpu/cpu.h" +#include "core/uart/uart.h" + +/**************************************************************************/ +#define BUF 5 + +void main_uart(void) { + uint8_t uartBuffer[BUF] = { 'T', 'e', 's', 't', '\n' }; +uint8_t o[2]={0,0}; + + gpioSetDir(RB_LED0, gpioDirection_Output); + gpioSetValue (RB_LED0, 1); + lcdPrintln("Hi"); + lcdRefresh(); + uartInit(9600); + lcdPrintln("Inited."); + lcdRefresh(); + + uartSend((uint8_t *)uartBuffer, BUF); + lcdPrintln("sent."); + lcdRefresh(); + + uartRxBufferWrite('x'); + + // Get a reference to the UART control block +// uart_pcb_t *pcb = uartGetPCB(); + + gpioSetValue (RB_LED0, 1-gpioGetValue(RB_LED0)); + // Read any text available in the queue + while (uartRxBufferDataPending()) + { + gpioSetValue (RB_LED0, 1-gpioGetValue(RB_LED0)); + // Read the first available character + uint8_t c = uartRxBufferRead(); + o[0]=c; + lcdPrint(o); + lcdRefresh(); + uartSend(&c, 1); + } +}; From 0f1303800d08361440c3d488777a47fd89f7389d Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Sun, 7 Aug 2011 01:52:05 +0200 Subject: [PATCH 4/5] Just a hint of options --- firmware/core/projectconfig.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/firmware/core/projectconfig.h b/firmware/core/projectconfig.h index 7d58144..41d5420 100644 --- a/firmware/core/projectconfig.h +++ b/firmware/core/projectconfig.h @@ -82,3 +82,9 @@ #define CFG_USBCDC_BUFFERSIZE (256) +/* you will need these for the UART */ +#if 0 +#define CFG_INTERFACE_UART (1) +#define CFG_UART_BAUDRATE (115200) +#define CFG_UART_BUFSIZE (512) +#endif From 20b939926f1d225bf7c8a01264687ba02dd1b476 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 9 Aug 2011 00:11:16 +0200 Subject: [PATCH 5/5] New loadable "voltage" --- firmware/l0dable/voltage.c | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 firmware/l0dable/voltage.c diff --git a/firmware/l0dable/voltage.c b/firmware/l0dable/voltage.c new file mode 100644 index 0000000..70a5266 --- /dev/null +++ b/firmware/l0dable/voltage.c @@ -0,0 +1,50 @@ +#include +#include + +#include "basic/basic.h" +#include "basic/config.h" + +#include "lcd/render.h" +#include "lcd/print.h" + +#include "usetable.h" + +/**************************************************************************/ + +void ram(void) { + int v,mv; + do{ + lcdClear(); + lcdPrintln("Battery status:"); + mv=GetVoltage(); + v=mv/1000; + + lcdNl(); + if (mv<3550){ + lcdPrintln(" Charge NOW!"); + }else if (mv<3650){ + lcdPrintln(" Charge soon"); + }else if (mv<4000){ + lcdPrintln(" OK"); + }else if(mv<4200){ + lcdPrintln(" Good"); + }else{ + lcdPrintln(" Full"); + }; + + lcdNl(); + lcdPrint(" "); + lcdPrint(IntToStr(v,2,0)); + lcdPrint("."); + lcdPrint(IntToStr(mv%1000,3,F_ZEROS)); + lcdPrintln("V"); + + lcdNl(); + if(gpioGetValue(RB_PWR_CHRG)){ + lcdPrintln("(not charging)"); + }else{ + lcdPrintln("CHARGING"); + }; + lcdRefresh(); + } while ((getInputWaitTimeout(242))==BTN_NONE); +}