
A scanner framework that receives advertisements, filters them by MAC, and prints out some data as CSV over the serial port
Dependencies: BLE_API mbed nRF51822
Fork of BasicScanner by
Diff: main.cpp
- Revision:
- 13:8999c8b2e18e
- Parent:
- 12:dbbf0ddc9b12
- Child:
- 14:d8f4a49c9fb3
--- a/main.cpp Sat May 28 22:07:19 2016 +0000 +++ b/main.cpp Wed Jun 01 20:36:08 2016 +0000 @@ -1,19 +1,23 @@ #include "mbed.h" -//#include "toolchain.h" #include "ble/BLE.h" #include "TMP_nrf51/TMP_nrf51.h" #include "UARTService.h" -#include "ctc_aes.h" #define UART_TX p9 #define UART_RX p11 #define LOG(...) { pc.printf(__VA_ARGS__); } +#define SCAN_INTERVAL 500 +#define SCAN_WINDOW 500 + + //Baud rate: for the TinyBLE use 115200 for term or 4000000 for internal energy monitoring +#define BAUD_RATE 115200 + DigitalOut alivenessLED(LED1, 1); Ticker ticker; @@ -21,103 +25,37 @@ UARTService *uartServicePtr; -//stuff for encryption -uint8_t payload[31]; -#define BLOCK_SIZE 16 -#define KEYLEN 256 //128, 192, 256 -int j; -unsigned char nonce_counter[BLOCK_SIZE]; -unsigned char plain[BLOCK_SIZE]; -unsigned char cipher[BLOCK_SIZE]; -unsigned char* counter_bytes = nonce_counter+BLOCK_SIZE/2; -size_t counter_tx_len = 3; -unsigned char key[KEYLEN/8]; -unsigned char iv[BLOCK_SIZE];//not used for ctr mode but required by setKey -Aes ctx; void periodicCallback(void) { alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */ } -void initAES(void) -{ - for(int i=0;i<KEYLEN/8;i++) - key[i] = i; - for(int i=0; i<BLOCK_SIZE-3; i++) - nonce_counter[i]=i<8?i:0; - for(int i=0; i<BLOCK_SIZE;i++) - iv[i]=0; - AesSetKey(&ctx, key, KEYLEN/8, iv, AES_ENCRYPTION); -} - -void decrypt(const Gap::AdvertisementCallbackParams_t *params) -{ - //puts decrypted data into GLOBAL plain variable. - - - //get coutner - for(int i=(params->advertisingDataLen)-counter_tx_len; i < params->advertisingDataLen; i++) - nonce_counter[BLOCK_SIZE-3+(i-((params->advertisingDataLen)-counter_tx_len))] = params->advertisingData[i]; - - //print nonce_counter - LOG("\nNonceCtr: "); - for(int i=0;i<BLOCK_SIZE;i++) - LOG("%02x ", nonce_counter[i]); - - //get cipher text - for(int i=0; i < (params->advertisingDataLen) - (counter_tx_len + 2); i++) - cipher[i] = params->advertisingData[i+2]; - - //print cipher - LOG("\nCiphertxt: "); - for(int i=0; i < BLOCK_SIZE; i++) - LOG("%02x ", cipher[i]); - - - //build key stream - AesEncrypt(&ctx, nonce_counter, plain); - //print key - LOG("\nKey: "); - for(int i=0; i<BLOCK_SIZE; i++) - LOG("%02x ", plain[i]); - - //decrypt into plain (destroying key) - for(int i=0;i<BLOCK_SIZE;i++) - plain[i]^=cipher[i]; -} /* * This function is called every time we scan an advertisement. */ void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { + // Here is what we do when we receive a packet /* Search for the manufacturer specific data with matching application-ID */ int addr_length = 6; - //print from addr - //if(params->peerAddr[addr_length-1] == 0xfc) - //{ + if(params->peerAddr[addr_length-1] == 0xaa) + { LOG("\nFrom: "); for(int i=0; i<addr_length; i++) LOG("%02x:", params->peerAddr[addr_length-i-1]); //print payload - LOG("\nPayload: "); + LOG(" RSSI: %d", params->rssi); + LOG("\n Payload: "); for(int i=0; i < params->advertisingDataLen; i++) LOG(" %02x", params->advertisingData[i]); - - - decrypt(params); - - //print plaintext - LOG("\nPlaintext: "); - for(int i=0; i<BLOCK_SIZE; i++) - LOG("%02x ", plain[i]); //print close of round LOG("\n\n"); - //} + } } /** @@ -149,17 +87,17 @@ } /* Setup and start scanning */ - ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */); + ble.gap().setScanParams(SCAN_INTERVAL /* scan interval */, SCAN_WINDOW /* scan window */); ble.gap().startScan(advertisementCallback); } int main(void) { - //use 115200 for term 4M for energy - pc.baud(115200); + + pc.baud(BAUD_RATE); - LOG("---- DECRYPTULATOR ACTIVIZE ----\n"); - initAES(); + LOG("---- Basic Scanner ACTIVIZE ----\n"); + ticker.attach(periodicCallback, 1); /* flash the LED because reasons */