From ce6b63046f1f337b742624ecf651323fad07210c Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 17:33:55 +0200 Subject: [PATCH 01/10] Whoops. (Cygwin-)Symlinks and git don't mix --- firmware/applications/serial/config.c | 2 +- firmware/applications/serial/util.c | 2 +- firmware/applications/serial/uuid.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) mode change 120000 => 100644 firmware/applications/serial/config.c mode change 120000 => 100644 firmware/applications/serial/util.c mode change 120000 => 100644 firmware/applications/serial/uuid.c diff --git a/firmware/applications/serial/config.c b/firmware/applications/serial/config.c deleted file mode 120000 index 0dc83e2..0000000 --- a/firmware/applications/serial/config.c +++ /dev/null @@ -1 +0,0 @@ -../tester/config.c \ No newline at end of file diff --git a/firmware/applications/serial/config.c b/firmware/applications/serial/config.c new file mode 100644 index 0000000..ecc4cc5 --- /dev/null +++ b/firmware/applications/serial/config.c @@ -0,0 +1 @@ +#include "../tester/config.c" diff --git a/firmware/applications/serial/util.c b/firmware/applications/serial/util.c deleted file mode 120000 index 2a06902..0000000 --- a/firmware/applications/serial/util.c +++ /dev/null @@ -1 +0,0 @@ -../tester/util.c \ No newline at end of file diff --git a/firmware/applications/serial/util.c b/firmware/applications/serial/util.c new file mode 100644 index 0000000..24faf5c --- /dev/null +++ b/firmware/applications/serial/util.c @@ -0,0 +1 @@ +#include "../tester/util.c" diff --git a/firmware/applications/serial/uuid.c b/firmware/applications/serial/uuid.c deleted file mode 120000 index a3edae7..0000000 --- a/firmware/applications/serial/uuid.c +++ /dev/null @@ -1 +0,0 @@ -../tester/uuid.c \ No newline at end of file diff --git a/firmware/applications/serial/uuid.c b/firmware/applications/serial/uuid.c new file mode 100644 index 0000000..b2d0d85 --- /dev/null +++ b/firmware/applications/serial/uuid.c @@ -0,0 +1 @@ +#include "../tester/uuid.c" From 9e4f27ad12dcb378976f385ce07827fad0ae3e72 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 18:22:53 +0200 Subject: [PATCH 02/10] More usbserial utility functions --- firmware/applications/serial/serial.c | 61 ++++---------------------- firmware/usbcdc/Makefile | 1 + firmware/usbcdc/util.c | 62 +++++++++++++++++++++++++++ firmware/usbcdc/util.h | 3 ++ 4 files changed, 75 insertions(+), 52 deletions(-) create mode 100644 firmware/usbcdc/util.c create mode 100644 firmware/usbcdc/util.h diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index d8e1788..ca37a40 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -7,73 +7,31 @@ #include "funk/nrf24l01p.h" -#include "core/usbcdc/usb.h" -#include "core/usbcdc/usbcore.h" -#include "core/usbcdc/usbhw.h" -#include "core/usbcdc/cdcuser.h" -#include "core/usbcdc/cdc_buf.h" +#include "usbcdc/usb.h" +#include "usbcdc/usbcore.h" +#include "usbcdc/usbhw.h" +#include "usbcdc/cdcuser.h" +#include "usbcdc/cdc_buf.h" #include #if CFG_USBMSC -#error "MSC is defined +#error "MSC is defined" #endif #if !CFG_USBCDC -#error "CDC is not defined +#error "CDC is not defined" #endif /**************************************************************************/ -volatile unsigned int lastTick; -int puts(const char * str) -{ - // There must be at least 1ms between USB frames (of up to 64 bytes) - // This buffers all data and writes it out from the buffer one frame - // and one millisecond at a time - if (USB_Configuration) - { - while(*str) - cdcBufferWrite(*str++); - // Check if we can flush the buffer now or if we need to wait - unsigned int currentTick = systickGetTicks(); - if (currentTick != lastTick) - { - uint8_t frame[64]; - uint32_t bytesRead = 0; - while (cdcBufferDataPending()) - { - // Read up to 64 bytes as long as possible - bytesRead = cdcBufferReadLen(frame, 64); - USB_WriteEP (CDC_DEP_IN, frame, bytesRead); - systickDelay(1); - } - lastTick = currentTick; - } - } - return 0; -} - void f_ser(void) { - //lastTick = systickGetTicks(); // Used to control output/printf timing - CDC_Init(); // Initialise VCOM - USB_Init(); // USB Initialization - lcdPrintln("preconnect"); - USB_Connect(TRUE); // USB Connect - lcdPrintln("postconnect"); - // Wait until USB is configured or timeout occurs - uint32_t usbTimeout = 0; -// while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) { -// if (USB_Configuration) break; -// delayms(10); // Wait 10ms -// usbTimeout++; -// } - lcdPrintln("fini"); + usbCDCInit(); }; void f_disconnect(void) { - USB_Connect(FALSE); + usbCDCOff(); }; #define LEN 10 @@ -113,4 +71,3 @@ void f_echo(){ void f_say(){ puts("hello world\r\n"); }; - diff --git a/firmware/usbcdc/Makefile b/firmware/usbcdc/Makefile index a319efc..3a9ea98 100644 --- a/firmware/usbcdc/Makefile +++ b/firmware/usbcdc/Makefile @@ -9,6 +9,7 @@ OBJS += usbcore.o OBJS += usbdesc.o OBJS += usbhw.o OBJS += usbuser.o +OBJS += util.o LIBNAME=usbcdc diff --git a/firmware/usbcdc/util.c b/firmware/usbcdc/util.c new file mode 100644 index 0000000..d1d8753 --- /dev/null +++ b/firmware/usbcdc/util.c @@ -0,0 +1,62 @@ +#include + +#include "usbcdc/usb.h" +#include "usbcdc/usbcore.h" +#include "usbcdc/cdc_buf.h" +#include "usbcdc/usbhw.h" +#include "usbcdc/cdcuser.h" + +#include "basic/basic.h" + +volatile unsigned int lastTick; + +// There must be at least 1ms between USB frames (of up to 64 bytes) +// This buffers all data and writes it out from the buffer one frame +// and one millisecond at a time +int puts(const char * str){ + if(!USB_Configuration) + return -1; + + while(*str) + cdcBufferWrite(*str++); + + //XXX: This assumes systick is 1ms, which it isn't for us. + // this makes usbserial unnecessary slow. Ah well.... + + // Check if we can flush the buffer now or if we need to wait + unsigned int currentTick = systickGetTicks(); + if (currentTick != lastTick){ + uint8_t frame[64]; + uint32_t bytesRead = 0; + while (cdcBufferDataPending()){ + // Read up to 64 bytes as long as possible + bytesRead = cdcBufferReadLen(frame, 64); + USB_WriteEP (CDC_DEP_IN, frame, bytesRead); + systickDelay(1); + } + lastTick = currentTick; + } + return 0; +} + +void usbCDCInit(){ + lastTick = systickGetTicks(); // Used to control output/printf timing + CDC_Init(); // Initialise VCOM + USB_Init(); // USB Initialization + USB_Connect(TRUE); // USB Connect + + /* You could wait until serial is connected. */ +#if 0 // We don't + uint32_t usbTimeout = 0; + + while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) { + if (USB_Configuration) break; + delayms(10); // Wait 10ms + usbTimeout++; + } +#endif +} + +usbCDCOff(void){ + USB_Connect(FALSE); +} diff --git a/firmware/usbcdc/util.h b/firmware/usbcdc/util.h new file mode 100644 index 0000000..d743b2f --- /dev/null +++ b/firmware/usbcdc/util.h @@ -0,0 +1,3 @@ +int puts(const char * str); +void usbCDCInit(void); +void usbCDCOff(void); From 196ac680e93e9d0387ca74fdb6c2a54b11914ea4 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 19:03:25 +0200 Subject: [PATCH 03/10] First version of an serial openbeaconsniffer. Yay. --- firmware/applications/serial/config.c | 39 +++++++- firmware/applications/serial/serial.c | 125 +++++++++++++++++++++----- firmware/applications/serial/util.c | 84 ++++++++++++++++- firmware/applications/serial/uuid.c | 27 +++++- 4 files changed, 250 insertions(+), 25 deletions(-) diff --git a/firmware/applications/serial/config.c b/firmware/applications/serial/config.c index ecc4cc5..0d8df7c 100644 --- a/firmware/applications/serial/config.c +++ b/firmware/applications/serial/config.c @@ -1 +1,38 @@ -#include "../tester/config.c" +#include + +#include "basic/basic.h" + +#include "lcd/print.h" +#include "lcd/display.h" + +#include "filesystem/ff.h" + +#include + +/**************************************************************************/ + +void readcfg(void) { + readConfig(); +}; + +void savecfg(void){ + saveConfig(); +}; + +void applycfg(void){ + applyConfig(); +}; + +void show(void){ + lcdClear(); + lcdPrint("time:"); lcdPrintInt(globalconfig.time); lcdNl(); + lcdPrint("btrig:"); lcdPrintInt(globalconfig.backlighttrigger); lcdNl(); + lcdPrint("bval:"); lcdPrintInt(globalconfig.backlightvalue); lcdNl(); + lcdPrint("lcd:"); lcdPrintInt(globalconfig.lcdstate); lcdNl(); + lcdPrint("priv:"); lcdPrintInt(globalconfig.privacy); lcdNl(); + lcdRefresh(); +}; + +void lcdmirror(void){ + lcdToggleFlag(LCD_MIRRORX); +}; diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index ca37a40..2c7ee49 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -12,9 +12,17 @@ #include "usbcdc/usbhw.h" #include "usbcdc/cdcuser.h" #include "usbcdc/cdc_buf.h" +#include "usbcdc/util.h" #include +#define BEACON_CHANNEL 81 +#define BEACON_MAC "\x1\x2\x3\x2\1" + +uint32_t const testkey[4] = { + 0xB4595344,0xD3E119B6,0xA814D0EC,0xEFF5A24E +}; + #if CFG_USBMSC #error "MSC is defined" #endif @@ -26,18 +34,18 @@ /**************************************************************************/ -void f_ser(void) { +void ser_enable(void) { usbCDCInit(); }; -void f_disconnect(void) { +void ser_disable(void) { usbCDCOff(); }; -#define LEN 10 -void f_sread(){ - uint8_t buf[LEN+1]; - int l=LEN; +#define myLEN 10 +void ser_read(){ + uint8_t buf[myLEN+1]; + int l=myLEN; lcdPrint("Bytes:"); CDC_OutBufAvailChar (&l); @@ -53,21 +61,94 @@ void f_sread(){ lcdPrintln(buf); }; -void f_echo(){ - uint8_t buf[2] = {0,0}; - int l; - while(1){ - CDC_OutBufAvailChar(&l); - if( l ){ - l = 1; - CDC_RdOutBuf (buf, &l); - puts(buf); - } - //puts("hello world\r\n"); - //delayms(1); - } -}; - -void f_say(){ +void ser_say(){ puts("hello world\r\n"); }; + +void f_init(){ + nrf_init(); + struct NRF_CFG config = { + .channel= BEACON_CHANNEL, + .txmac= BEACON_MAC, + .nrmacs=1, + .mac0= BEACON_MAC, + .maclen ="\x10", + }; + + nrf_config_set(&config); +}; + +void f_beacon(void){ + struct NRF_CFG config = { + .channel= BEACON_CHANNEL, + .txmac= BEACON_MAC, + .nrmacs=1, + .mac0= BEACON_MAC, + .maclen ="\x10", + }; + + nrf_config_set(&config); +}; + +int enctoggle=0; + +void f_enctog(){ + enctoggle=1-enctoggle; + lcdClear(); + lcdPrint("Encryption:"); + if(enctoggle) + lcdPrintln("ON"); + else + lcdPrintln("Off"); +}; + +const char* IntToStrX(unsigned int num, unsigned int mxlen){ +#define LEN 32 + static char s[LEN+1]; + char * o=s; + int len; + s[LEN]=0; + for (len=(LEN-1);len>=(LEN-mxlen);len--){ + s[len]=(num%16)+'0'; + if(s[len]>'9') + s[len]+='A'-'9'-1; + num/=16; + }; + return &s[len+1]; +}; + +void f_recser(void){ + __attribute__ ((aligned (4))) uint8_t buf[32]; + int len; + + getInputWaitRelease(); + + do{ + len=nrf_rcv_pkt_time_encr(1000,sizeof(buf),buf,enctoggle?testkey:NULL); + + if(len==0){ + puts("(Timeout)\r\n"); + }; + puts("pkt: "); + puts("[");puts(IntToStrX(len,2));puts("] "); + puts(IntToStrX( *(int*)(buf+ 0),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 1),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 2),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 3),2 )); + puts("."); + puts(IntToStrX( *(int*)(buf+ 4),8 )); + puts("."); + puts(IntToStrX( *(int*)(buf+ 8),8 )); + puts("."); + puts(IntToStrX( *(int*)(buf+ 12),4 )); + puts(" ["); + + len=crc16(buf,14); + puts(IntToStrX(len,4)); puts("]\r\n"); + delayms(10); + }while ((getInputRaw())==BTN_NONE); + +}; diff --git a/firmware/applications/serial/util.c b/firmware/applications/serial/util.c index 24faf5c..8747264 100644 --- a/firmware/applications/serial/util.c +++ b/firmware/applications/serial/util.c @@ -1 +1,83 @@ -#include "../tester/util.c" +#include + +#include "basic/basic.h" + +#include "lcd/lcd.h" +#include "lcd/print.h" +#include "lcd/allfonts.h" + +#include "filesystem/ff.h" +#include "filesystem/select.h" +#include "funk/nrf24l01p.h" +#include "usb/usbmsc.h" + +#include + +/**************************************************************************/ + +void show_ticks(void) { + int dx=0; + int dy=8; + lcdClear(); + dx=DoString(0,dy,"Ticks:"); + while ((getInputRaw())==BTN_NONE){ + DoInt(0,dy+8,_timectr); + lcdDisplay(); + }; + dy+=16; + dx=DoString(0,dy,"Done."); +}; + + +void adc_light(void) { + int dx=0; + int dy=8; + dx=DoString(0,dy,"Light:"); + DoString(0,dy+16,"Night:"); + while ((getInputRaw())==BTN_NONE){ + DoInt(dx,dy,GetLight()); + DoInt(dx,dy+16,isNight()); + DoInt(dx,dy+8,globalconfig.backlighttrigger); + lcdDisplay(); + }; + dy+=8; + dx=DoString(0,dy,"Done."); +}; + +void gotoISP(void) { + DoString(0,0,"Enter ISP!"); + lcdDisplay(); + ISPandReset(); +} + +void lcd_mirror(void) { + lcdToggleFlag(LCD_MIRRORX); +}; + +void lcd_invert(void) { + lcdToggleFlag(LCD_INVERTED); +}; + +void adc_check(void) { + int dx=0; + int dy=8; + // Print Voltage + dx=DoString(0,dy,"Voltage:"); + while ((getInputRaw())==BTN_NONE){ + DoInt(dx,dy,GetVoltage()); + lcdDisplay(); + }; + dy+=8; + dx=DoString(0,dy,"Done."); +}; + +void msc_menu(void){ + DoString(0,8,"MSC Enabled."); + lcdDisplay(); + usbMSCInit(); + getInputWaitRelease(); + getInputWait(); + DoString(0,16,"MSC Disabled."); + usbMSCOff(); +}; + diff --git a/firmware/applications/serial/uuid.c b/firmware/applications/serial/uuid.c index b2d0d85..601a5f7 100644 --- a/firmware/applications/serial/uuid.c +++ b/firmware/applications/serial/uuid.c @@ -1 +1,26 @@ -#include "../tester/uuid.c" +#include + +#include "basic/basic.h" + +#include "lcd/lcd.h" +#include "lcd/print.h" + +#include "funk/nrf24l01p.h" + +#include + +#include "funk/rftransfer.h" +#include "funk/openbeacon.h" + +#include "core/iap/iap.h" + +/**************************************************************************/ + +void f_uuid(void) { + IAP_return_t iap_return; + iap_return = iapReadSerialNumber(); + lcdPrintIntHex(iap_return.Result[0]); lcdNl(); + lcdPrintIntHex(iap_return.Result[1]); lcdNl(); + lcdPrintIntHex(iap_return.Result[2]); lcdNl(); + lcdPrintIntHex(iap_return.Result[3]); lcdNl(); +} From 625af67f3ffffe88ad6e26f0299f9c8401cef510 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 23:20:55 +0200 Subject: [PATCH 04/10] New polling-based funk receive functions. --- firmware/funk/nrf24l01p.c | 80 +++++++++++++++++++++++++++++++++++++++ firmware/funk/nrf24l01p.h | 6 +++ 2 files changed, 86 insertions(+) diff --git a/firmware/funk/nrf24l01p.c b/firmware/funk/nrf24l01p.c index 63ee4cf..bffaa34 100644 --- a/firmware/funk/nrf24l01p.c +++ b/firmware/funk/nrf24l01p.c @@ -91,6 +91,85 @@ void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data){ #define nrf_write_reg_long(reg, len, data) \ nrf_write_long(C_W_REGISTER|(reg), len, data) +// High-Level: +void nrf_rcv_pkt_start(void){ + + nrf_write_reg(R_CONFIG, + R_CONFIG_PRIM_RX| // Receive mode + R_CONFIG_PWR_UP| // Power on + R_CONFIG_EN_CRC // CRC on, single byte + ); + + nrf_cmd(C_FLUSH_RX); + nrf_write_reg(R_STATUS,0); + + CE_HIGH(); +}; + +int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt){ + uint8_t len; + uint8_t status=0; + + for(int i=0;i32 || len==0){ + return -2; // no packet error + }; + + if(len>maxsize){ + return -1; // packet too large + }; + + nrf_read_pkt(len,pkt); + + return len; +}; + +int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]){ + int len; + uint16_t cmpcrc; + + len=nrf_rcv_pkt_poll(maxsize,pkt); + + if(len <=0) + return len; + + if(key==NULL) + return len; + + cmpcrc=crc16(pkt,len-2); + if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { + xxtea_decode_words((uint32_t*)pkt,len/4,key); + + cmpcrc=crc16(pkt,len-2); + if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { + return -3; // CRC failed + }; + }; + return len; +}; + +void nrf_rcv_pkt_end(void){ + CE_LOW(); + nrf_cmd(C_FLUSH_RX); + nrf_write_reg(R_STATUS,R_STATUS_RX_DR); +}; + // High-Level: int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t const key[4]){ uint8_t len; @@ -156,6 +235,7 @@ int nrf_rcv_pkt_time_encr(int maxtime, int maxsize, uint8_t * pkt, uint32_t cons return len; }; + char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ if(size > MAX_PKT) diff --git a/firmware/funk/nrf24l01p.h b/firmware/funk/nrf24l01p.h index 4e84106..ace6122 100644 --- a/firmware/funk/nrf24l01p.h +++ b/firmware/funk/nrf24l01p.h @@ -157,6 +157,12 @@ void nrf_config_get(nrfconfig config); void nrf_set_strength(unsigned char strength); +// new receive IF +void nrf_rcv_pkt_start(void); +int nrf_rcv_pkt_poll(int maxsize, uint8_t * pkt); +int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]); +void nrf_rcv_pkt_end(void); + /* END */ #endif /* _NRF24L01P_H */ From 28cc6e63ca5858c58d684191cff399ede72f0a4f Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 23:39:07 +0200 Subject: [PATCH 05/10] OOps: Remove debugging-#error --- firmware/basic/idle.c | 1 - 1 file changed, 1 deletion(-) diff --git a/firmware/basic/idle.c b/firmware/basic/idle.c index 3d01408..623154b 100644 --- a/firmware/basic/idle.c +++ b/firmware/basic/idle.c @@ -7,7 +7,6 @@ QUEUE the_queue; #ifdef __arm__ volatile uint32_t _timectr=0; #else -#error "foo" extern uint32_t simTimeCounter(); #define _timectr (simTimeCounter()) #endif From 932d5502d2f963d5ac1ae36f2f0d48fcbcdc7e20 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 23:40:14 +0200 Subject: [PATCH 06/10] Add #include for time_t definition --- firmware/basic/basic.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index b311f81..c10a31a 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -1,6 +1,7 @@ #ifndef __BASIC_H_ #define __BASIC_H_ +#include #include "core/gpio/gpio.h" #include "core/adc/adc.h" @@ -203,3 +204,4 @@ int applyConfig(void); #define SYSTICKSPEED 10 #endif + From 5e1ac514ea99f1b73aa815c392869c2a09c0a663 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 23:41:09 +0200 Subject: [PATCH 07/10] Add simple itoa-style function --- firmware/basic/Makefile | 1 + firmware/basic/basic.h | 4 +++- firmware/basic/itoa.c | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 firmware/basic/itoa.c diff --git a/firmware/basic/Makefile b/firmware/basic/Makefile index e831d6d..eb3b501 100644 --- a/firmware/basic/Makefile +++ b/firmware/basic/Makefile @@ -19,6 +19,7 @@ OBJS += byteorder.o OBJS += random.o OBJS += idle.o OBJS += config.o +OBJS += itoa.o LIBNAME=basic diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index c10a31a..d218cd6 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -203,5 +203,7 @@ int applyConfig(void); #define SYSTICKSPEED 10 -#endif +// itoa.c +const char* IntToStrX(unsigned int num, unsigned int mxlen); +#endif diff --git a/firmware/basic/itoa.c b/firmware/basic/itoa.c new file mode 100644 index 0000000..9cc8de5 --- /dev/null +++ b/firmware/basic/itoa.c @@ -0,0 +1,15 @@ +#define LEN 32 + +const char* IntToStrX(unsigned int num, unsigned int mxlen){ + static char s[LEN+1]; + char * o=s; + int len; + s[LEN]=0; + for (len=(LEN-1);len>=(LEN-mxlen);len--){ + s[len]=(num%16)+'0'; + if(s[len]>'9') + s[len]+='A'-'9'-1; + num/=16; + }; + return &s[len+1]; +}; From 03e95c9681c9634513dbfbf3cda42ae90e12f933 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 27 Jul 2011 23:43:14 +0200 Subject: [PATCH 08/10] More funk test code (rem0te/game support) --- firmware/applications/serial/remote.c | 223 ++++++++++++++++++++++++++ firmware/applications/serial/serial.c | 15 -- 2 files changed, 223 insertions(+), 15 deletions(-) create mode 100644 firmware/applications/serial/remote.c diff --git a/firmware/applications/serial/remote.c b/firmware/applications/serial/remote.c new file mode 100644 index 0000000..7a479ba --- /dev/null +++ b/firmware/applications/serial/remote.c @@ -0,0 +1,223 @@ +#include + +#include "basic/basic.h" +#include "basic/byteorder.h" + +#include "lcd/lcd.h" +#include "lcd/print.h" + +#include "funk/nrf24l01p.h" +#include "usbcdc/usb.h" +#include "usbcdc/usbcore.h" +#include "usbcdc/usbhw.h" +#include "usbcdc/cdcuser.h" +#include "usbcdc/cdc_buf.h" +#include "usbcdc/util.h" + +#include + +#define REMOTE_CHANNEL 91 +#define REMOTE_MAC "REM0T" + +#if CFG_USBMSC +#error "MSC is defined" +#endif + +#if !CFG_USBCDC +#error "CDC is not defined" +#endif + +/**************************************************************************/ + +uint32_t const remotekey[4] = { + 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff +}; + +void r_init(void){ + nrf_init(); + + struct NRF_CFG config = { + .channel= REMOTE_CHANNEL, + .txmac= REMOTE_MAC, + .nrmacs=1, + .mac0= REMOTE_MAC, + .maclen ="\x10", + }; + + nrf_config_set(&config); +}; + +void s_init(void){ + usbCDCInit(); + nrf_init(); + + struct NRF_CFG config = { + .channel= REMOTE_CHANNEL, + .txmac= REMOTE_MAC, + .nrmacs=1, + .mac0= REMOTE_MAC, + .maclen ="\x10", + }; + + nrf_config_set(&config); +}; + +void r_recv(void){ + __attribute__ ((aligned (4))) uint8_t buf[32]; + int len; + + nrf_rcv_pkt_start(); + + getInputWaitRelease(); + + while(!getInputRaw()){ + delayms(100); + len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,remotekey); + + if(len<=0) + continue; + + if(buf[1]=='C'){ // Cursor + puts("C "); + puts(IntToStrX( buf[2],2 )); + puts(" "); + puts(IntToStrX( uint8ptouint32(buf+4), 8 )); + puts(" "); + puts(IntToStrX( uint8ptouint32(buf+8), 8 )); + }else{ + puts("U "); +// puts("[");puts(IntToStrX(len,2));puts("] "); + puts(IntToStrX( *(int*)(buf+ 0),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 1),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 2),2 )); + puts(" "); + puts(IntToStrX( *(int*)(buf+ 3),2 )); + puts(" "); + puts(IntToStrX( uint8ptouint32(buf+4),8 )); + puts("."); + puts(IntToStrX( uint8ptouint32(buf+8),8 )); + puts(" "); + puts(IntToStrX( uint8ptouint32(buf+10),4 )); + }; + puts("\r\n"); + + }; + + nrf_rcv_pkt_end(); + +} + + + +void r_s1(void){ + static int ctr=1; + __attribute__ ((aligned (4))) uint8_t buf[32]; + 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,remotekey); + + lcdClear(); + lcdPrint("F-St:"); lcdPrintInt(status); + lcdDisplay(); + +}; +void r_s2(void){ + static int ctr=1; + __attribute__ ((aligned (4))) uint8_t buf[32]; + int status; + + buf[0]=0x10; // Length: 16 bytes + buf[1]='C'; // 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,remotekey); + + buf[0]=0x10; // Length: 16 bytes + buf[1]='I'; // 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,remotekey); + + lcdClear(); + lcdPrint("F-St:"); lcdPrintInt(status); + lcdDisplay(); + +}; + +void r_send(void){ + int ctr=1; + __attribute__ ((aligned (4))) uint8_t buf[32]; + int len; + int status; + + while(1){ + + buf[0]=0x10; // Length: 16 bytes + buf[1]='C'; // Proto + buf[2]=getInputRaw(); + buf[3]=0x00; // Unused + + ctr++; + *(int*)(buf+4)=ctr; + + /* + buf[4]=0x00; // ctr + buf[5]=0x00; // ctr + buf[6]=0x00; // ctr + buf[7]=ctr++; // ctr + */ + + buf[8]=0x0; // Object id + buf[9]=0x0; + buf[10]=0x05; + buf[11]=0xec; + + buf[12]=0xff; // salt (0xffff always?) + buf[13]=0xff; + + lcdClear(); + lcdPrint("Key:"); lcdPrintInt(buf[2]); lcdNl(); + if(buf[2]==BTN_ENTER) + break; + + status=nrf_snd_pkt_crc_encr(16,buf,remotekey); + + lcdPrint("F-St:"); lcdPrintInt(status); + lcdDisplay(); + + len=nrf_rcv_pkt_time_encr(10,sizeof(buf),buf,remotekey); + if(len>0){ + lcdPrint("Got!"); + lcdDisplay(); + break; + }; + }; +}; + diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index 2c7ee49..94d41b3 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -102,21 +102,6 @@ void f_enctog(){ lcdPrintln("Off"); }; -const char* IntToStrX(unsigned int num, unsigned int mxlen){ -#define LEN 32 - static char s[LEN+1]; - char * o=s; - int len; - s[LEN]=0; - for (len=(LEN-1);len>=(LEN-mxlen);len--){ - s[len]=(num%16)+'0'; - if(s[len]>'9') - s[len]+='A'-'9'-1; - num/=16; - }; - return &s[len+1]; -}; - void f_recser(void){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; From 7993bc3f0d74516aa2d7c07dd19b7afb1049d0c3 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Thu, 28 Jul 2011 01:32:13 +0200 Subject: [PATCH 09/10] library dependency fix --- firmware/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/firmware/Makefile b/firmware/Makefile index f92d179..985af2e 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -41,6 +41,7 @@ LDLIBS += -Lbasic -lbasic LDLIBS += -Llcd -llcd LDLIBS += -Lcore -lcore LDLIBS += -Lusb -lusb +LDLIBS += -lbasic OCFLAGS = --strip-unneeded SUBDIRS?= $(foreach lib,$(LIBS),$(dir $(lib))) From ad623567348889a401c159637245d9f05a3e3b11 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Thu, 28 Jul 2011 01:32:45 +0200 Subject: [PATCH 10/10] Semi-functional remo0te with 1st attemt at message-back --- firmware/applications/serial/remote.c | 59 +++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/firmware/applications/serial/remote.c b/firmware/applications/serial/remote.c index 7a479ba..be96720 100644 --- a/firmware/applications/serial/remote.c +++ b/firmware/applications/serial/remote.c @@ -62,20 +62,71 @@ void s_init(void){ nrf_config_set(&config); }; + void process(uint8_t * input){ + __attribute__ ((aligned (4))) uint8_t buf[32]; + puts("process: "); + puts(input); + puts("\r\n"); + if(input[0]=='M'){ + buf[0]=0x10; // Length: 16 bytes + buf[1]='M'; // Proto + buf[2]=0x01; + buf[3]=0x01; // Unused + + uint32touint8p(0,buf+4); + + uint32touint8p(0x41424344,buf+8); + + buf[12]=0xff; // salt (0xffff always?) + buf[13]=0xff; + nrf_snd_pkt_crc_encr(16,buf,remotekey); + nrf_rcv_pkt_start(); + }; + +}; + +#define INPUTLEN 99 void r_recv(void){ __attribute__ ((aligned (4))) uint8_t buf[32]; int len; + uint8_t input[INPUTLEN+1]; + int inputptr=0; + nrf_rcv_pkt_start(); + puts("D start"); getInputWaitRelease(); while(!getInputRaw()){ delayms(100); + + // Input + int l=INPUTLEN-inputptr; + CDC_OutBufAvailChar (&l); + + if(l>0){ + CDC_RdOutBuf (input+inputptr, &l); + input[inputptr+l+1]=0; + for(int i=0;i0){ lcdPrint("Got!"); lcdDisplay(); break; }; + delayms(10); }; };