BLE ADV Gateway, converts advertisement to proper JSON serial output

Dependencies:   BLE_API mbed mbedtls nRF51822

Committer:
electronichamsters
Date:
Mon Jul 10 04:13:45 2017 +0000
Revision:
9:1ea51b2048a8
Parent:
8:46c5e0bfab05
Child:
10:9db2ac7e1eea
working gateway

Who changed what in which revision?

UserRevisionLine numberNew contents of line
electronichamsters 9:1ea51b2048a8 1 /*
electronichamsters 8:46c5e0bfab05 2
electronichamsters 9:1ea51b2048a8 3
electronichamsters 9:1ea51b2048a8 4 Eric Tsai
electronichamsters 9:1ea51b2048a8 5
electronichamsters 9:1ea51b2048a8 6 Scans for advertisements and captures the ones identified as mine
electronichamsters 9:1ea51b2048a8 7 Takes UUID fields and injects the MAC and RSSI data. Assumes UUID JSON data.
electronichamsters 8:46c5e0bfab05 8
electronichamsters 8:46c5e0bfab05 9
electronichamsters 8:46c5e0bfab05 10
electronichamsters 9:1ea51b2048a8 11 ----------------------------------------------
electronichamsters 9:1ea51b2048a8 12
electronichamsters 9:1ea51b2048a8 13
electronichamsters 9:1ea51b2048a8 14 Example output:
electronichamsters 9:1ea51b2048a8 15 {"mac":"c56e806d7cfb","rssi":-54,"volt":3.01,"mag/p":2}
electronichamsters 9:1ea51b2048a8 16 added timer
electronichamsters 9:1ea51b2048a8 17 {"mac":"c56e806d7cfb","rssi":-54,"tmr":3232,"volt":3.01,"mag/p":2}
electronichamsters 9:1ea51b2048a8 18 {"mac":"c2f154bb0af9","rssi":-76,"tmr":250,"volt":3.14,"mag/p":1}
electronichamsters 8:46c5e0bfab05 19
electronichamsters 9:1ea51b2048a8 20 todo: hash MAC to create unique identifiers for sensors seen as "mine"
electronichamsters 9:1ea51b2048a8 21 todo: create search for ensure gateway only accepts tmr values that match, else
electronichamsters 9:1ea51b2048a8 22 are consecutive transmits in case we miss power up transmit.
electronichamsters 9:1ea51b2048a8 23 */
electronichamsters 8:46c5e0bfab05 24
electronichamsters 9:1ea51b2048a8 25 #include "mbed.h"
electronichamsters 9:1ea51b2048a8 26 #include "ble/BLE.h"
electronichamsters 9:1ea51b2048a8 27 #include "mbedtls/aes.h"
electronichamsters 8:46c5e0bfab05 28
electronichamsters 8:46c5e0bfab05 29
electronichamsters 9:1ea51b2048a8 30 //comment out when done with debug uart, else eats batteries
electronichamsters 9:1ea51b2048a8 31 #define MyDebugEnb 0
sunsmile2015 0:3dc6e424dba0 32
electronichamsters 8:46c5e0bfab05 33
electronichamsters 9:1ea51b2048a8 34 //DigitalOut led1(LED1, 1);
electronichamsters 9:1ea51b2048a8 35 Ticker ticker;
electronichamsters 8:46c5e0bfab05 36
electronichamsters 9:1ea51b2048a8 37 //aes stuff
electronichamsters 9:1ea51b2048a8 38 uint8_t src_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4};
electronichamsters 9:1ea51b2048a8 39 uint8_t des_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4};
electronichamsters 9:1ea51b2048a8 40
electronichamsters 9:1ea51b2048a8 41 mbedtls_aes_context aes;
electronichamsters 9:1ea51b2048a8 42
electronichamsters 9:1ea51b2048a8 43 unsigned char iv[16] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x1, 0x2}; //16-byte key
electronichamsters 9:1ea51b2048a8 44 size_t input_len = 16;
electronichamsters 9:1ea51b2048a8 45 size_t output_len = 0;
electronichamsters 9:1ea51b2048a8 46
electronichamsters 9:1ea51b2048a8 47 Serial device(p9, p11); //nRF51822 uart : TX=p9. RX=p11
electronichamsters 9:1ea51b2048a8 48
electronichamsters 9:1ea51b2048a8 49 void periodicCallback(void)
electronichamsters 8:46c5e0bfab05 50 {
electronichamsters 9:1ea51b2048a8 51 //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
electronichamsters 9:1ea51b2048a8 52 //device.printf("periodic 5...");
electronichamsters 9:1ea51b2048a8 53 //device.printf("\r\n");
electronichamsters 8:46c5e0bfab05 54 }
electronichamsters 8:46c5e0bfab05 55
electronichamsters 9:1ea51b2048a8 56 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
electronichamsters 8:46c5e0bfab05 57
electronichamsters 8:46c5e0bfab05 58
electronichamsters 9:1ea51b2048a8 59 //BLE MAC Address (6 bytes):
electronichamsters 9:1ea51b2048a8 60 // params->peerAddr[5]
electronichamsters 9:1ea51b2048a8 61 // params->peerAddr[4]
electronichamsters 9:1ea51b2048a8 62 // ...
electronichamsters 9:1ea51b2048a8 63 // params->peerAddr[0]
electronichamsters 9:1ea51b2048a8 64 //RSSI = params->rssi
electronichamsters 9:1ea51b2048a8 65 //Payload
electronichamsters 9:1ea51b2048a8 66 // params->advertisingData[#<advertisingDataLen]
electronichamsters 8:46c5e0bfab05 67
electronichamsters 8:46c5e0bfab05 68
electronichamsters 8:46c5e0bfab05 69
electronichamsters 9:1ea51b2048a8 70 // [<---MY DATA-->]
electronichamsters 9:1ea51b2048a8 71 //0x02 0x01 0x06 0x06 0xff D E C ? ?
electronichamsters 9:1ea51b2048a8 72 //0 1 2 3 4 5 6 7 8 9
electronichamsters 8:46c5e0bfab05 73
electronichamsters 9:1ea51b2048a8 74 if ( (params->advertisingDataLen) >= 8)
electronichamsters 9:1ea51b2048a8 75 {
electronichamsters 9:1ea51b2048a8 76 //if one of "ours", these UUID fields will be this value. Arbitrary "
electronichamsters 9:1ea51b2048a8 77 //todo: use MAC to make a hash of what these 3 bytes should be.
electronichamsters 9:1ea51b2048a8 78 if ( (params->advertisingData[5] == 0x44) && (params->advertisingData[6] == 0x45) && (params->advertisingData[7]==0x43) )
electronichamsters 9:1ea51b2048a8 79 {
electronichamsters 9:1ea51b2048a8 80 /*
electronichamsters 9:1ea51b2048a8 81 BLE Received from MAC C2F154BB0AF9
electronichamsters 9:1ea51b2048a8 82 volt:3.11,mag:1
electronichamsters 9:1ea51b2048a8 83
electronichamsters 9:1ea51b2048a8 84 uart-transmit:
electronichamsters 9:1ea51b2048a8 85 mac:C2F154BB0AF9,rssi:##,volt:3.11,mag:1
electronichamsters 9:1ea51b2048a8 86
electronichamsters 9:1ea51b2048a8 87 /ble/C2F154BB0AF9/rssi
electronichamsters 9:1ea51b2048a8 88 /ble/C2F154BB0AF9/1st_token
electronichamsters 9:1ea51b2048a8 89 /ble/c2F153
electronichamsters 9:1ea51b2048a8 90
electronichamsters 9:1ea51b2048a8 91 */
electronichamsters 9:1ea51b2048a8 92 /*
electronichamsters 9:1ea51b2048a8 93 device.printf("Adv peerAddr: [%02x%02x%02x%02x%02x%02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
electronichamsters 9:1ea51b2048a8 94 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
electronichamsters 9:1ea51b2048a8 95 params->rssi, params->isScanResponse, params->type);
electronichamsters 9:1ea51b2048a8 96 */
electronichamsters 9:1ea51b2048a8 97 device.printf("{\"mac\":\"%02x%02x%02x%02x%02x%02x\",\"rssi\":%d,",
electronichamsters 9:1ea51b2048a8 98 params->peerAddr[5],
electronichamsters 9:1ea51b2048a8 99 params->peerAddr[4],
electronichamsters 9:1ea51b2048a8 100 params->peerAddr[3],
electronichamsters 9:1ea51b2048a8 101 params->peerAddr[2],
electronichamsters 9:1ea51b2048a8 102 params->peerAddr[1],
electronichamsters 9:1ea51b2048a8 103 params->peerAddr[0],
electronichamsters 9:1ea51b2048a8 104 params->rssi
electronichamsters 9:1ea51b2048a8 105 );
electronichamsters 9:1ea51b2048a8 106 //mac:"c2f154bb0af9"
electronichamsters 9:1ea51b2048a8 107
electronichamsters 9:1ea51b2048a8 108 //device.printf("\r\n copy adv length: %d \r\n", params->advertisingDataLen);
electronichamsters 9:1ea51b2048a8 109 for (int i = 0; i<16; i++)
electronichamsters 9:1ea51b2048a8 110 {
electronichamsters 9:1ea51b2048a8 111
electronichamsters 9:1ea51b2048a8 112 src_buf[i]=params->advertisingData[i+8];
electronichamsters 9:1ea51b2048a8 113 #if MyDebugEnb
electronichamsters 9:1ea51b2048a8 114 device.printf("%x ", src_buf[i]);
electronichamsters 9:1ea51b2048a8 115 #endif
electronichamsters 9:1ea51b2048a8 116 }
electronichamsters 9:1ea51b2048a8 117 #if MyDebugEnb
electronichamsters 9:1ea51b2048a8 118 device.printf("...\r\n ");
electronichamsters 9:1ea51b2048a8 119 #endif
electronichamsters 9:1ea51b2048a8 120 mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, src_buf, des_buf );
electronichamsters 9:1ea51b2048a8 121 #if MyDebugEnb
electronichamsters 9:1ea51b2048a8 122
electronichamsters 9:1ea51b2048a8 123 device.printf("decoded first 16 bytes \r\n");
electronichamsters 9:1ea51b2048a8 124 for (int i = 0; i<16; i++)
electronichamsters 9:1ea51b2048a8 125 {
electronichamsters 9:1ea51b2048a8 126 //device.printf("%02x", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 127 if (i < 2)
electronichamsters 9:1ea51b2048a8 128 {
electronichamsters 9:1ea51b2048a8 129 device.printf("%x ", des_buf[i]);
electronichamsters 9:1ea51b2048a8 130 }
electronichamsters 9:1ea51b2048a8 131 else
electronichamsters 9:1ea51b2048a8 132 {
electronichamsters 9:1ea51b2048a8 133 device.printf("%c ", des_buf[i]);
electronichamsters 9:1ea51b2048a8 134 }
electronichamsters 9:1ea51b2048a8 135 }
electronichamsters 9:1ea51b2048a8 136 device.printf("done----- \r\n");
electronichamsters 9:1ea51b2048a8 137 #endif
electronichamsters 8:46c5e0bfab05 138
electronichamsters 9:1ea51b2048a8 139 //print clock
electronichamsters 9:1ea51b2048a8 140 // "tmr":3232,
electronichamsters 9:1ea51b2048a8 141 //des_buf[0] = lower byte, des_buf[1] = higher byte
electronichamsters 9:1ea51b2048a8 142 uint16_t beacon_timer = des_buf[0] | (des_buf[1] << 8);
electronichamsters 9:1ea51b2048a8 143 device.printf("\"tmr\":%d,", beacon_timer);
electronichamsters 9:1ea51b2048a8 144 //device.printf("\"tmr\":%d,", des_buf[0]);
electronichamsters 9:1ea51b2048a8 145
electronichamsters 9:1ea51b2048a8 146 for (int i = 2; i< 16; i++)
electronichamsters 9:1ea51b2048a8 147 {
electronichamsters 9:1ea51b2048a8 148 //device.printf("%02x", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 149 device.printf("%c", des_buf[i]);
electronichamsters 9:1ea51b2048a8 150 }
electronichamsters 8:46c5e0bfab05 151
electronichamsters 9:1ea51b2048a8 152 if (params->advertisingDataLen > 24)
electronichamsters 9:1ea51b2048a8 153 {
electronichamsters 9:1ea51b2048a8 154 for (unsigned index = 24; index < params->advertisingDataLen; index++)
electronichamsters 9:1ea51b2048a8 155 {
electronichamsters 9:1ea51b2048a8 156 //device.printf("%02x", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 157 device.printf("%c", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 158 }
electronichamsters 9:1ea51b2048a8 159 }
electronichamsters 9:1ea51b2048a8 160
electronichamsters 9:1ea51b2048a8 161 /*
electronichamsters 9:1ea51b2048a8 162 for (unsigned index = 8; index < params->advertisingDataLen; index++)
electronichamsters 9:1ea51b2048a8 163 {
electronichamsters 9:1ea51b2048a8 164 //device.printf("%02x", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 165 device.printf("%c", params->advertisingData[index]);
electronichamsters 9:1ea51b2048a8 166 }
electronichamsters 9:1ea51b2048a8 167 */
electronichamsters 9:1ea51b2048a8 168
electronichamsters 9:1ea51b2048a8 169 device.printf("}");
electronichamsters 9:1ea51b2048a8 170 device.printf("\r\n");
electronichamsters 9:1ea51b2048a8 171
electronichamsters 9:1ea51b2048a8 172 /*
electronichamsters 9:1ea51b2048a8 173 mac:c2f154bb0af9,rssi:-55,volt:3.05,mag:1
electronichamsters 9:1ea51b2048a8 174 mac:c2f154bb0af9,rssi:-67,volt:3.05,mag:1
electronichamsters 9:1ea51b2048a8 175 mac:c2f154bb0af9,rssi:-60,volt:3.07,mag:0
electronichamsters 9:1ea51b2048a8 176 */
electronichamsters 9:1ea51b2048a8 177
electronichamsters 9:1ea51b2048a8 178 }//end if it's our adv
electronichamsters 9:1ea51b2048a8 179 }//end if advertisingDataLen
electronichamsters 8:46c5e0bfab05 180
electronichamsters 8:46c5e0bfab05 181
electronichamsters 8:46c5e0bfab05 182
electronichamsters 8:46c5e0bfab05 183
electronichamsters 9:1ea51b2048a8 184 }//end advertisementCallback
electronichamsters 8:46c5e0bfab05 185
andresag 7:0a8bbb6dea16 186 /**
electronichamsters 9:1ea51b2048a8 187 * This function is called when the ble initialization process has failed
andresag 7:0a8bbb6dea16 188 */
andresag 7:0a8bbb6dea16 189 void onBleInitError(BLE &ble, ble_error_t error)
andresag 7:0a8bbb6dea16 190 {
andresag 7:0a8bbb6dea16 191 /* Initialization error handling should go here */
electronichamsters 9:1ea51b2048a8 192 device.printf("periodic callback ");
electronichamsters 9:1ea51b2048a8 193 device.printf("\r\n");
andresag 7:0a8bbb6dea16 194 }
andresag 7:0a8bbb6dea16 195
andresag 7:0a8bbb6dea16 196 /**
andresag 7:0a8bbb6dea16 197 * Callback triggered when the ble initialization process has finished
andresag 7:0a8bbb6dea16 198 */
andresag 7:0a8bbb6dea16 199 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
sunsmile2015 2:b935358da5ba 200 {
andresag 7:0a8bbb6dea16 201 BLE& ble = params->ble;
andresag 7:0a8bbb6dea16 202 ble_error_t error = params->error;
andresag 7:0a8bbb6dea16 203
andresag 7:0a8bbb6dea16 204 if (error != BLE_ERROR_NONE) {
andresag 7:0a8bbb6dea16 205 /* In case of error, forward the error handling to onBleInitError */
andresag 7:0a8bbb6dea16 206 onBleInitError(ble, error);
andresag 7:0a8bbb6dea16 207 return;
andresag 7:0a8bbb6dea16 208 }
andresag 7:0a8bbb6dea16 209
andresag 7:0a8bbb6dea16 210 /* Ensure that it is the default instance of BLE */
andresag 7:0a8bbb6dea16 211 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 7:0a8bbb6dea16 212 return;
andresag 7:0a8bbb6dea16 213 }
electronichamsters 9:1ea51b2048a8 214
electronichamsters 9:1ea51b2048a8 215 // in ms. Duty cycle = (interval / window); 200ms/500ms = 40%
electronichamsters 9:1ea51b2048a8 216 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
electronichamsters 9:1ea51b2048a8 217 ble.gap().startScan(advertisementCallback);
sunsmile2015 4:e5fa4c8838db 218 }
sunsmile2015 4:e5fa4c8838db 219
sunsmile2015 2:b935358da5ba 220 int main(void)
sunsmile2015 2:b935358da5ba 221 {
electronichamsters 8:46c5e0bfab05 222 device.baud(9600);
electronichamsters 9:1ea51b2048a8 223 device.printf("started main 04... ");
electronichamsters 8:46c5e0bfab05 224 device.printf("\r\n");
electronichamsters 9:1ea51b2048a8 225 ticker.attach(periodicCallback, 5);
electronichamsters 8:46c5e0bfab05 226
electronichamsters 9:1ea51b2048a8 227 //mbedtls_aes_init(&aes);
electronichamsters 9:1ea51b2048a8 228 mbedtls_aes_setkey_dec( &aes, iv, 128 );
electronichamsters 9:1ea51b2048a8 229 //mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, input, output );
sunsmile2015 0:3dc6e424dba0 230
andresag 7:0a8bbb6dea16 231 BLE &ble = BLE::Instance();
andresag 7:0a8bbb6dea16 232 ble.init(bleInitComplete);
rgrover1 5:f4d74a8cad43 233
sunsmile2015 0:3dc6e424dba0 234 while (true) {
electronichamsters 9:1ea51b2048a8 235 ble.waitForEvent();
electronichamsters 9:1ea51b2048a8 236 }
electronichamsters 9:1ea51b2048a8 237 }