BLE ADV Gateway, converts advertisement to proper JSON serial output
Dependencies: BLE_API mbed mbedtls nRF51822
main.cpp@11:d5edb6e3edab, 2017-07-29 (annotated)
- Committer:
- electronichamsters
- Date:
- Sat Jul 29 21:49:32 2017 +0000
- Revision:
- 11:d5edb6e3edab
- Parent:
- 10:9db2ac7e1eea
- Child:
- 12:30c6e83f0fe5
Added spoof checking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
electronichamsters | 11:d5edb6e3edab | 1 | /* |
electronichamsters | 9:1ea51b2048a8 | 2 | Eric Tsai |
electronichamsters | 11:d5edb6e3edab | 3 | |
electronichamsters | 11:d5edb6e3edab | 4 | 7/29/2017: Added spoof checking using clock algorithm. |
electronichamsters | 9:1ea51b2048a8 | 5 | |
electronichamsters | 11:d5edb6e3edab | 6 | */ |
electronichamsters | 8:46c5e0bfab05 | 7 | |
electronichamsters | 9:1ea51b2048a8 | 8 | #include "mbed.h" |
electronichamsters | 9:1ea51b2048a8 | 9 | #include "ble/BLE.h" |
electronichamsters | 9:1ea51b2048a8 | 10 | #include "mbedtls/aes.h" |
electronichamsters | 8:46c5e0bfab05 | 11 | |
electronichamsters | 8:46c5e0bfab05 | 12 | |
electronichamsters | 9:1ea51b2048a8 | 13 | //comment out when done with debug uart, else eats batteries |
electronichamsters | 9:1ea51b2048a8 | 14 | #define MyDebugEnb 0 |
sunsmile2015 | 0:3dc6e424dba0 | 15 | |
electronichamsters | 8:46c5e0bfab05 | 16 | |
electronichamsters | 11:d5edb6e3edab | 17 | |
electronichamsters | 9:1ea51b2048a8 | 18 | //DigitalOut led1(LED1, 1); |
electronichamsters | 9:1ea51b2048a8 | 19 | Ticker ticker; |
electronichamsters | 8:46c5e0bfab05 | 20 | |
electronichamsters | 11:d5edb6e3edab | 21 | const uint16_t Periodicity = 180; //clock periodicity used for spoof checking, should be 1800 seconds for 30minutes |
electronichamsters | 11:d5edb6e3edab | 22 | |
electronichamsters | 9:1ea51b2048a8 | 23 | //aes stuff |
electronichamsters | 9:1ea51b2048a8 | 24 | uint8_t src_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 9:1ea51b2048a8 | 25 | uint8_t des_buf[16] = {0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4,0x1,0x2,0x3,0x4}; |
electronichamsters | 9:1ea51b2048a8 | 26 | mbedtls_aes_context aes; |
electronichamsters | 9:1ea51b2048a8 | 27 | 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 | 28 | size_t input_len = 16; |
electronichamsters | 9:1ea51b2048a8 | 29 | size_t output_len = 0; |
electronichamsters | 9:1ea51b2048a8 | 30 | |
electronichamsters | 9:1ea51b2048a8 | 31 | Serial device(p9, p11); //nRF51822 uart : TX=p9. RX=p11 |
electronichamsters | 9:1ea51b2048a8 | 32 | |
electronichamsters | 11:d5edb6e3edab | 33 | //data structures for tracking wake time of BLE end nodes, for to prevent spoofing |
electronichamsters | 11:d5edb6e3edab | 34 | struct MAC_Birth_Record |
electronichamsters | 11:d5edb6e3edab | 35 | { |
electronichamsters | 11:d5edb6e3edab | 36 | uint8_t MAC_Addr[6]; //mac[0] is low order byte in mac address |
electronichamsters | 11:d5edb6e3edab | 37 | uint16_t MAC_Time; //The time of birth for this end node |
electronichamsters | 11:d5edb6e3edab | 38 | uint16_t Attempt_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 39 | uint8_t Attempt_Cnt; |
electronichamsters | 11:d5edb6e3edab | 40 | uint8_t Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 41 | }; |
electronichamsters | 11:d5edb6e3edab | 42 | const uint8_t sizeOfSpoof = 50; |
electronichamsters | 11:d5edb6e3edab | 43 | MAC_Birth_Record Spoof_Check[sizeOfSpoof]; //array tracking end node birth times |
electronichamsters | 11:d5edb6e3edab | 44 | uint8_t Spoof_Ray_Tail = 0; //array index for next record |
electronichamsters | 11:d5edb6e3edab | 45 | uint8_t Received_MAC_Addr[6]; //mac[0] is low order byte in mac address |
electronichamsters | 11:d5edb6e3edab | 46 | uint16_t Received_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 47 | static Timer Gateway_Time; |
electronichamsters | 11:d5edb6e3edab | 48 | static uint16_t Expected_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 49 | uint8_t Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 50 | |
electronichamsters | 11:d5edb6e3edab | 51 | |
electronichamsters | 11:d5edb6e3edab | 52 | const uint8_t ADVisUnknown = 0; |
electronichamsters | 11:d5edb6e3edab | 53 | const uint8_t ADVisNew = 1; |
electronichamsters | 11:d5edb6e3edab | 54 | const uint8_t ADVisDuplicate = 2; |
electronichamsters | 11:d5edb6e3edab | 55 | const uint8_t ADVisSpoof = 3; |
electronichamsters | 11:d5edb6e3edab | 56 | |
electronichamsters | 11:d5edb6e3edab | 57 | //******************** |
electronichamsters | 11:d5edb6e3edab | 58 | // Takes into account 30-minute clock, wrap arounds and edge cases |
electronichamsters | 11:d5edb6e3edab | 59 | // returns: |
electronichamsters | 11:d5edb6e3edab | 60 | // TRUE: if sensor's reported time lines up to gateway's recorded time |
electronichamsters | 11:d5edb6e3edab | 61 | // Expected_MAC_Time |
electronichamsters | 11:d5edb6e3edab | 62 | //******************* |
electronichamsters | 11:d5edb6e3edab | 63 | bool Is_Birth_Time_Correct (uint16_t gateway_mac_time, uint16_t sensor_mac_time, uint8_t margin) |
electronichamsters | 11:d5edb6e3edab | 64 | { |
electronichamsters | 11:d5edb6e3edab | 65 | bool return_val = 0; |
electronichamsters | 11:d5edb6e3edab | 66 | uint16_t current_time = (uint16_t)(Gateway_Time.read_ms()/1000); |
electronichamsters | 11:d5edb6e3edab | 67 | int16_t gateway_time_zone = current_time - sensor_mac_time; |
electronichamsters | 11:d5edb6e3edab | 68 | |
electronichamsters | 11:d5edb6e3edab | 69 | if (current_time >= sensor_mac_time) |
electronichamsters | 11:d5edb6e3edab | 70 | { |
electronichamsters | 11:d5edb6e3edab | 71 | Expected_MAC_Time = current_time - sensor_mac_time; |
electronichamsters | 11:d5edb6e3edab | 72 | } |
electronichamsters | 11:d5edb6e3edab | 73 | else |
electronichamsters | 11:d5edb6e3edab | 74 | { |
electronichamsters | 11:d5edb6e3edab | 75 | Expected_MAC_Time = (Periodicity - sensor_mac_time + current_time); |
electronichamsters | 11:d5edb6e3edab | 76 | } |
electronichamsters | 11:d5edb6e3edab | 77 | |
electronichamsters | 11:d5edb6e3edab | 78 | if (1) //check for count==0 situations |
electronichamsters | 11:d5edb6e3edab | 79 | { |
electronichamsters | 11:d5edb6e3edab | 80 | if ( (gateway_mac_time < (Expected_MAC_Time + margin)) && (gateway_mac_time > (Expected_MAC_Time - margin)) ) |
electronichamsters | 11:d5edb6e3edab | 81 | { |
electronichamsters | 11:d5edb6e3edab | 82 | return_val = 1; |
electronichamsters | 11:d5edb6e3edab | 83 | } |
electronichamsters | 11:d5edb6e3edab | 84 | else |
electronichamsters | 11:d5edb6e3edab | 85 | { |
electronichamsters | 11:d5edb6e3edab | 86 | return_val = 0; |
electronichamsters | 11:d5edb6e3edab | 87 | |
electronichamsters | 11:d5edb6e3edab | 88 | } |
electronichamsters | 11:d5edb6e3edab | 89 | } |
electronichamsters | 11:d5edb6e3edab | 90 | |
electronichamsters | 11:d5edb6e3edab | 91 | /* |
electronichamsters | 11:d5edb6e3edab | 92 | //if positive, not a wrap around edge case |
electronichamsters | 11:d5edb6e3edab | 93 | //todo: take into account low count, indicates reset? |
electronichamsters | 11:d5edb6e3edab | 94 | if ((gateway_time_zone >= 0) && (1)) |
electronichamsters | 11:d5edb6e3edab | 95 | { |
electronichamsters | 11:d5edb6e3edab | 96 | Expected_MAC_Time = gateway_time_zone; |
electronichamsters | 11:d5edb6e3edab | 97 | if ( (gateway_mac_time < (Expected_MAC_Time + margin)) && (gateway_mac_time > (Expected_MAC_Time - margin)) ) |
electronichamsters | 11:d5edb6e3edab | 98 | { |
electronichamsters | 11:d5edb6e3edab | 99 | return_val = 1; //valid birth time |
electronichamsters | 11:d5edb6e3edab | 100 | } |
electronichamsters | 11:d5edb6e3edab | 101 | |
electronichamsters | 11:d5edb6e3edab | 102 | } |
electronichamsters | 11:d5edb6e3edab | 103 | else //wrap around edge case, gateway_time_zone is negative |
electronichamsters | 11:d5edb6e3edab | 104 | { |
electronichamsters | 11:d5edb6e3edab | 105 | gateway_time_zone = (Periodicity - sensor_mac_time + current_time); |
electronichamsters | 11:d5edb6e3edab | 106 | Expected_MAC_Time = gateway_time_zone; //todo: 2 declarations?? |
electronichamsters | 11:d5edb6e3edab | 107 | //if ( (Periodicity-gateway_mac_time)+current_time == sensor_mac_time) //wrong, need bands |
electronichamsters | 11:d5edb6e3edab | 108 | if ( (gateway_mac_time < (Expected_MAC_Time + margin)) && (gateway_mac_time > (Expected_MAC_Time - margin)) ) |
electronichamsters | 11:d5edb6e3edab | 109 | { |
electronichamsters | 11:d5edb6e3edab | 110 | return_val = 1; |
electronichamsters | 11:d5edb6e3edab | 111 | } |
electronichamsters | 11:d5edb6e3edab | 112 | else |
electronichamsters | 11:d5edb6e3edab | 113 | { |
electronichamsters | 11:d5edb6e3edab | 114 | return_val = 0; |
electronichamsters | 11:d5edb6e3edab | 115 | |
electronichamsters | 11:d5edb6e3edab | 116 | } |
electronichamsters | 11:d5edb6e3edab | 117 | |
electronichamsters | 11:d5edb6e3edab | 118 | } |
electronichamsters | 11:d5edb6e3edab | 119 | */ |
electronichamsters | 11:d5edb6e3edab | 120 | return return_val; |
electronichamsters | 11:d5edb6e3edab | 121 | |
electronichamsters | 11:d5edb6e3edab | 122 | }//end Is_Birth_Time_Correct |
electronichamsters | 11:d5edb6e3edab | 123 | |
electronichamsters | 11:d5edb6e3edab | 124 | |
electronichamsters | 11:d5edb6e3edab | 125 | |
electronichamsters | 11:d5edb6e3edab | 126 | /* ************************************** |
electronichamsters | 11:d5edb6e3edab | 127 | check adv against birth records to detect spoofing |
electronichamsters | 11:d5edb6e3edab | 128 | add device to birth records if new |
electronichamsters | 11:d5edb6e3edab | 129 | returns |
electronichamsters | 11:d5edb6e3edab | 130 | TRUE: if device reported birth time matches real birth time |
electronichamsters | 11:d5edb6e3edab | 131 | if device is first encountered |
electronichamsters | 11:d5edb6e3edab | 132 | if X consecutive birth time matches |
electronichamsters | 11:d5edb6e3edab | 133 | FALSE: if device reported birth time does not match |
electronichamsters | 11:d5edb6e3edab | 134 | |
electronichamsters | 11:d5edb6e3edab | 135 | ToDo: add attempts |
electronichamsters | 11:d5edb6e3edab | 136 | ToDo: wrap around of gateway clock not accounted for. Like if Gateway_Time.read_ms() - Received_MAC_Time < 0 |
electronichamsters | 11:d5edb6e3edab | 137 | */ |
electronichamsters | 11:d5edb6e3edab | 138 | uint8_t Is_Not_Spoofed () |
electronichamsters | 8:46c5e0bfab05 | 139 | { |
electronichamsters | 11:d5edb6e3edab | 140 | uint8_t return_val = ADVisUnknown; |
electronichamsters | 11:d5edb6e3edab | 141 | //iterate through all of birth records looking for one that matches MAC address |
electronichamsters | 11:d5edb6e3edab | 142 | for (int i=0; i<sizeOfSpoof; i++) |
electronichamsters | 11:d5edb6e3edab | 143 | { |
electronichamsters | 11:d5edb6e3edab | 144 | //Search for matching MAC address |
electronichamsters | 11:d5edb6e3edab | 145 | if (Spoof_Check[i].MAC_Addr[0] == Received_MAC_Addr[0] && |
electronichamsters | 11:d5edb6e3edab | 146 | Spoof_Check[i].MAC_Addr[1] == Received_MAC_Addr[1] && |
electronichamsters | 11:d5edb6e3edab | 147 | Spoof_Check[i].MAC_Addr[2] == Received_MAC_Addr[2] && |
electronichamsters | 11:d5edb6e3edab | 148 | Spoof_Check[i].MAC_Addr[3] == Received_MAC_Addr[3] && |
electronichamsters | 11:d5edb6e3edab | 149 | Spoof_Check[i].MAC_Addr[4] == Received_MAC_Addr[4] && |
electronichamsters | 11:d5edb6e3edab | 150 | Spoof_Check[i].MAC_Addr[5] == Received_MAC_Addr[5]) |
electronichamsters | 11:d5edb6e3edab | 151 | { |
electronichamsters | 11:d5edb6e3edab | 152 | |
electronichamsters | 11:d5edb6e3edab | 153 | |
electronichamsters | 11:d5edb6e3edab | 154 | |
electronichamsters | 11:d5edb6e3edab | 155 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 156 | device.printf("found MAC address in array\r\n"); |
electronichamsters | 11:d5edb6e3edab | 157 | device.printf(" Index = %d \r\n", i); |
electronichamsters | 11:d5edb6e3edab | 158 | device.printf(" MAC = %02x:%02x:%02x:%02x:%02x:%02x \r\n", Received_MAC_Addr[5],Received_MAC_Addr[4],Received_MAC_Addr[3],Received_MAC_Addr[2],Received_MAC_Addr[1],Received_MAC_Addr[0]); |
electronichamsters | 11:d5edb6e3edab | 159 | device.printf(" Received MAC Time = %d \r\n", Received_MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 160 | device.printf(" Gateway time = %d \r\n", (uint16_t)(Gateway_Time.read_ms()/1000)); |
electronichamsters | 11:d5edb6e3edab | 161 | device.printf(" Array.MAC_Time = %d \r\n", Spoof_Check[i].MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 162 | device.printf(" Array.Attempt_MAC_Time = %d \r\n", Spoof_Check[i].Attempt_MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 163 | device.printf(" Array.Attempt_Cnt = %d \r\n", Spoof_Check[i].Attempt_Cnt); |
electronichamsters | 11:d5edb6e3edab | 164 | device.printf(" Array.Xmit_Cnt = %d \r\n", Spoof_Check[i].Xmit_Cnt); |
electronichamsters | 11:d5edb6e3edab | 165 | device.printf(" expected time = %d \r\n", ((uint16_t)(Gateway_Time.read_ms()/1000)) - Received_MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 166 | #endif |
electronichamsters | 11:d5edb6e3edab | 167 | |
electronichamsters | 11:d5edb6e3edab | 168 | //calcuate the time of birth of this bluetooth module @ gateway time zone relative to received MAC_Time. |
electronichamsters | 11:d5edb6e3edab | 169 | //int16_t expected_mac_time = ((uint16_t)(Gateway_Time.read_ms()/1000)) - Received_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 170 | //uint16_t expected_mac_time = 0; |
electronichamsters | 11:d5edb6e3edab | 171 | |
electronichamsters | 11:d5edb6e3edab | 172 | |
electronichamsters | 11:d5edb6e3edab | 173 | //todo: this needs to be a POSITIVE VALUE, calculated by Is_MAC_Time_Correct(); |
electronichamsters | 11:d5edb6e3edab | 174 | |
electronichamsters | 11:d5edb6e3edab | 175 | |
electronichamsters | 11:d5edb6e3edab | 176 | // ToDo: there's a bug here. Need to use count to figure out when it's a first-powered on device. Can't use time? |
electronichamsters | 11:d5edb6e3edab | 177 | |
electronichamsters | 11:d5edb6e3edab | 178 | //***** |
electronichamsters | 11:d5edb6e3edab | 179 | // check primary Mac_Time |
electronichamsters | 11:d5edb6e3edab | 180 | //**** |
electronichamsters | 11:d5edb6e3edab | 181 | if |
electronichamsters | 11:d5edb6e3edab | 182 | ( |
electronichamsters | 11:d5edb6e3edab | 183 | Is_Birth_Time_Correct(Spoof_Check[i].MAC_Time, Received_MAC_Time, 3) |
electronichamsters | 11:d5edb6e3edab | 184 | ) |
electronichamsters | 11:d5edb6e3edab | 185 | { |
electronichamsters | 11:d5edb6e3edab | 186 | if (Spoof_Check[i].Xmit_Cnt != Received_Xmit_Cnt) |
electronichamsters | 11:d5edb6e3edab | 187 | { |
electronichamsters | 11:d5edb6e3edab | 188 | return_val = ADVisNew; //MAC Time checks out and not duplicate |
electronichamsters | 11:d5edb6e3edab | 189 | Spoof_Check[i].MAC_Time = Expected_MAC_Time; //update birth time for this device. |
electronichamsters | 11:d5edb6e3edab | 190 | Spoof_Check[i].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 191 | //i = sizeOfSpoof; //exit for loop, we've found the MAC address. But can't do it this way. |
electronichamsters | 11:d5edb6e3edab | 192 | //ToDo: hey, what happens if the array holds entries w/ same MAC? Is that possible? |
electronichamsters | 11:d5edb6e3edab | 193 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 194 | device.printf("Pirmary MAC Time as expected...expected=%d...Spoof_mac_tmr=%d \r\n", Expected_MAC_Time, Spoof_Check[i].MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 195 | #endif |
electronichamsters | 11:d5edb6e3edab | 196 | } |
electronichamsters | 11:d5edb6e3edab | 197 | else |
electronichamsters | 11:d5edb6e3edab | 198 | { |
electronichamsters | 11:d5edb6e3edab | 199 | return_val = ADVisDuplicate; //MAC Time checks out and is duplicate |
electronichamsters | 11:d5edb6e3edab | 200 | } |
electronichamsters | 11:d5edb6e3edab | 201 | }//if primary MAC_time match |
electronichamsters | 11:d5edb6e3edab | 202 | else |
electronichamsters | 11:d5edb6e3edab | 203 | { |
electronichamsters | 11:d5edb6e3edab | 204 | //***** |
electronichamsters | 11:d5edb6e3edab | 205 | // check secondary Mac_Time |
electronichamsters | 11:d5edb6e3edab | 206 | //**** |
electronichamsters | 11:d5edb6e3edab | 207 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 208 | device.printf(" MAC Time No Match!!...expected=%d...Spoof_mac_tmr=%d \r\n", Expected_MAC_Time, Spoof_Check[i].MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 209 | |
electronichamsters | 11:d5edb6e3edab | 210 | #endif |
electronichamsters | 11:d5edb6e3edab | 211 | |
electronichamsters | 11:d5edb6e3edab | 212 | //increment count if matches secondary |
electronichamsters | 11:d5edb6e3edab | 213 | //if ( (Spoof_Check[i].Attempt_MAC_Time < (expected_mac_time + 5)) && (Spoof_Check[i].Attempt_MAC_Time > (expected_mac_time - 5)) ) |
electronichamsters | 11:d5edb6e3edab | 214 | if (Is_Birth_Time_Correct(Spoof_Check[i].Attempt_MAC_Time, Received_MAC_Time, 3)) |
electronichamsters | 11:d5edb6e3edab | 215 | { |
electronichamsters | 11:d5edb6e3edab | 216 | if (Spoof_Check[i].Xmit_Cnt != Received_Xmit_Cnt) |
electronichamsters | 11:d5edb6e3edab | 217 | { |
electronichamsters | 11:d5edb6e3edab | 218 | |
electronichamsters | 11:d5edb6e3edab | 219 | //Spoof_Check[i].Attempt_MAC_Time = expected_mac_time; |
electronichamsters | 11:d5edb6e3edab | 220 | Spoof_Check[i].Attempt_Cnt++; |
electronichamsters | 11:d5edb6e3edab | 221 | Spoof_Check[i].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 222 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 223 | device.printf(" Match on secondary, Attempt_Cnt= %d, Attempt_MAC_Time = %d, Expected_MAC_Time=%d \r\n", Spoof_Check[i].Attempt_Cnt, Spoof_Check[i].Attempt_MAC_Time, Expected_MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 224 | #endif |
electronichamsters | 11:d5edb6e3edab | 225 | //this takes care of usecase where a module is reset after it's already been seen by gateway |
electronichamsters | 11:d5edb6e3edab | 226 | //after 3 consecutive correlated MAC_Time attempts we assume is our long lost friend and not a spoof. |
electronichamsters | 11:d5edb6e3edab | 227 | if (Spoof_Check[i].Attempt_Cnt >= 3) |
electronichamsters | 11:d5edb6e3edab | 228 | { |
electronichamsters | 11:d5edb6e3edab | 229 | return_val = ADVisNew; |
electronichamsters | 11:d5edb6e3edab | 230 | |
electronichamsters | 11:d5edb6e3edab | 231 | //promote this MAC_Time to primary and start accepting data @ this MAC_Time |
electronichamsters | 11:d5edb6e3edab | 232 | Spoof_Check[i].MAC_Time = Expected_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 233 | Spoof_Check[i].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 234 | Spoof_Check[i].Attempt_Cnt = 0; |
electronichamsters | 11:d5edb6e3edab | 235 | } |
electronichamsters | 11:d5edb6e3edab | 236 | else //Received_MAC_Time matches Attempt, but not enough times to be considered as valid. |
electronichamsters | 11:d5edb6e3edab | 237 | { |
electronichamsters | 11:d5edb6e3edab | 238 | return_val = ADVisSpoof; |
electronichamsters | 11:d5edb6e3edab | 239 | |
electronichamsters | 11:d5edb6e3edab | 240 | } |
electronichamsters | 11:d5edb6e3edab | 241 | |
electronichamsters | 11:d5edb6e3edab | 242 | |
electronichamsters | 11:d5edb6e3edab | 243 | }//is not duplicate, and transmit count matches |
electronichamsters | 11:d5edb6e3edab | 244 | else |
electronichamsters | 11:d5edb6e3edab | 245 | { |
electronichamsters | 11:d5edb6e3edab | 246 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 247 | device.printf(" is Duplicate , but matches Attempt_MAC_Time matches\r\n"); |
electronichamsters | 11:d5edb6e3edab | 248 | #endif |
electronichamsters | 11:d5edb6e3edab | 249 | return_val = ADVisDuplicate; |
electronichamsters | 11:d5edb6e3edab | 250 | } |
electronichamsters | 11:d5edb6e3edab | 251 | } //Recevied MAC_Time matches secondary MAC_Time |
electronichamsters | 11:d5edb6e3edab | 252 | else |
electronichamsters | 11:d5edb6e3edab | 253 | { |
electronichamsters | 11:d5edb6e3edab | 254 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 255 | device.printf(" No Match on secondary either, ===setting return_val = Spoof ===\r\n"); |
electronichamsters | 11:d5edb6e3edab | 256 | #endif |
electronichamsters | 11:d5edb6e3edab | 257 | return_val = ADVisSpoof; //it should still be zero, so this is just for clarification |
electronichamsters | 11:d5edb6e3edab | 258 | //at this point: MAC matches, MAC_Time doesn't match primary nor secondary. |
electronichamsters | 11:d5edb6e3edab | 259 | Spoof_Check[i].Attempt_Cnt = 0; //reset secondary count |
electronichamsters | 11:d5edb6e3edab | 260 | |
electronichamsters | 11:d5edb6e3edab | 261 | } |
electronichamsters | 11:d5edb6e3edab | 262 | //update second backup regardless whether it matches exptected_mac_time matches secondary or not. |
electronichamsters | 11:d5edb6e3edab | 263 | Spoof_Check[i].Attempt_MAC_Time = Expected_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 264 | Spoof_Check[i].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 265 | }//if Primary MAC_Time doesn't match |
electronichamsters | 11:d5edb6e3edab | 266 | |
electronichamsters | 11:d5edb6e3edab | 267 | break; //return_val tells us if this is a valid or not. Don't need to search through remainder of array |
electronichamsters | 11:d5edb6e3edab | 268 | }//if matching MAC address |
electronichamsters | 11:d5edb6e3edab | 269 | else //MAC doesn't match |
electronichamsters | 11:d5edb6e3edab | 270 | { |
electronichamsters | 11:d5edb6e3edab | 271 | //MAC doesn't match and we've searched through entire record |
electronichamsters | 11:d5edb6e3edab | 272 | //let's add this MAC as new, an initialze records to match this sensor |
electronichamsters | 11:d5edb6e3edab | 273 | if (i >= (sizeOfSpoof - 1)) //we've searched through entire array and didn't find this MAC |
electronichamsters | 11:d5edb6e3edab | 274 | { |
electronichamsters | 11:d5edb6e3edab | 275 | //we've searched through the entire array and didn't find this MAC address |
electronichamsters | 11:d5edb6e3edab | 276 | //add this MAC to array, and report this as valid MAC_Time |
electronichamsters | 11:d5edb6e3edab | 277 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 278 | device.printf("MAC not found, creating new at %d \r\n", Spoof_Ray_Tail); |
electronichamsters | 11:d5edb6e3edab | 279 | device.printf(" sizeOfSpoof=%d ... and i=%d \r\n", sizeOfSpoof, i); |
electronichamsters | 11:d5edb6e3edab | 280 | #endif |
electronichamsters | 11:d5edb6e3edab | 281 | |
electronichamsters | 11:d5edb6e3edab | 282 | return_val = ADVisNew; |
electronichamsters | 11:d5edb6e3edab | 283 | |
electronichamsters | 11:d5edb6e3edab | 284 | //create new entry for newly seen MAC @ tail |
electronichamsters | 11:d5edb6e3edab | 285 | // why refigure?? Spoof_Check[Spoof_Ray_Tail].MAC_Time = ((uint16_t)(Gateway_Time.read_ms()/1000)) - Received_MAC_Time; |
electronichamsters | 11:d5edb6e3edab | 286 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[0] = Received_MAC_Addr[0]; |
electronichamsters | 11:d5edb6e3edab | 287 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[1] = Received_MAC_Addr[1]; |
electronichamsters | 11:d5edb6e3edab | 288 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[2] = Received_MAC_Addr[2]; |
electronichamsters | 11:d5edb6e3edab | 289 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[3] = Received_MAC_Addr[3]; |
electronichamsters | 11:d5edb6e3edab | 290 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[4] = Received_MAC_Addr[4]; |
electronichamsters | 11:d5edb6e3edab | 291 | Spoof_Check[Spoof_Ray_Tail].MAC_Addr[5] = Received_MAC_Addr[5]; |
electronichamsters | 11:d5edb6e3edab | 292 | Spoof_Check[Spoof_Ray_Tail].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 293 | |
electronichamsters | 11:d5edb6e3edab | 294 | //calculate Expected_MAC_Time |
electronichamsters | 11:d5edb6e3edab | 295 | Is_Birth_Time_Correct (0, Received_MAC_Time, 3); |
electronichamsters | 11:d5edb6e3edab | 296 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 297 | device.printf("Expected_MAC_Time should be %d \r\n", Expected_MAC_Time); |
electronichamsters | 11:d5edb6e3edab | 298 | |
electronichamsters | 11:d5edb6e3edab | 299 | #endif |
electronichamsters | 11:d5edb6e3edab | 300 | Spoof_Check[Spoof_Ray_Tail].MAC_Time = Expected_MAC_Time; //wrong!!! |
electronichamsters | 11:d5edb6e3edab | 301 | Spoof_Check[Spoof_Ray_Tail].Xmit_Cnt = Received_Xmit_Cnt; |
electronichamsters | 11:d5edb6e3edab | 302 | Spoof_Check[Spoof_Ray_Tail].Attempt_Cnt = 0; |
electronichamsters | 11:d5edb6e3edab | 303 | |
electronichamsters | 11:d5edb6e3edab | 304 | //increment tail pointer to next position or wrap around |
electronichamsters | 11:d5edb6e3edab | 305 | //todo: might not be using the last index position of this array, not sure. Doesn't matter IDK. |
electronichamsters | 11:d5edb6e3edab | 306 | if (Spoof_Ray_Tail >= (sizeOfSpoof-1) ) //FIFO at end of array, return to beginning |
electronichamsters | 11:d5edb6e3edab | 307 | { |
electronichamsters | 11:d5edb6e3edab | 308 | Spoof_Ray_Tail = 0; |
electronichamsters | 11:d5edb6e3edab | 309 | } |
electronichamsters | 11:d5edb6e3edab | 310 | else |
electronichamsters | 11:d5edb6e3edab | 311 | { |
electronichamsters | 11:d5edb6e3edab | 312 | Spoof_Ray_Tail++; |
electronichamsters | 11:d5edb6e3edab | 313 | } |
electronichamsters | 11:d5edb6e3edab | 314 | |
electronichamsters | 11:d5edb6e3edab | 315 | }//end if loop at end of array |
electronichamsters | 11:d5edb6e3edab | 316 | }//end else not maching MAC |
electronichamsters | 11:d5edb6e3edab | 317 | |
electronichamsters | 11:d5edb6e3edab | 318 | }//loop through Spoof_Check array |
electronichamsters | 11:d5edb6e3edab | 319 | |
electronichamsters | 11:d5edb6e3edab | 320 | |
electronichamsters | 11:d5edb6e3edab | 321 | return return_val; |
electronichamsters | 11:d5edb6e3edab | 322 | }//end Is_Not_Spoofed() |
electronichamsters | 11:d5edb6e3edab | 323 | |
electronichamsters | 11:d5edb6e3edab | 324 | |
electronichamsters | 11:d5edb6e3edab | 325 | |
electronichamsters | 11:d5edb6e3edab | 326 | |
electronichamsters | 11:d5edb6e3edab | 327 | void periodicCallback(void) //every 1800 seconds = 30 minutes, max for ticker. |
electronichamsters | 11:d5edb6e3edab | 328 | { |
electronichamsters | 11:d5edb6e3edab | 329 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 330 | device.printf("===== reset timer ===== \r\n"); |
electronichamsters | 11:d5edb6e3edab | 331 | #endif |
electronichamsters | 9:1ea51b2048a8 | 332 | //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
electronichamsters | 9:1ea51b2048a8 | 333 | //device.printf("periodic 5..."); |
electronichamsters | 9:1ea51b2048a8 | 334 | //device.printf("\r\n"); |
electronichamsters | 11:d5edb6e3edab | 335 | Gateway_Time.reset(); |
electronichamsters | 11:d5edb6e3edab | 336 | } |
electronichamsters | 11:d5edb6e3edab | 337 | |
electronichamsters | 11:d5edb6e3edab | 338 | |
electronichamsters | 11:d5edb6e3edab | 339 | //with the current beacon, I see 3 valid advertisements per wake period with ambient other beacons. sometimes 4. |
electronichamsters | 11:d5edb6e3edab | 340 | //even with 2 phones broadcasting at 10Hz, still see 3 adv's. |
electronichamsters | 11:d5edb6e3edab | 341 | bool is_ours(const uint8_t * adv_data, const uint8_t * adv_address) |
electronichamsters | 11:d5edb6e3edab | 342 | { |
electronichamsters | 11:d5edb6e3edab | 343 | //if ((adv_data[5] == adv_address[3]) && (adv_data[6] == adv_address[2]) && (adv_data[7] == adv_address[1]) && (adv_data[8] == adv_address[0])) |
electronichamsters | 11:d5edb6e3edab | 344 | if ((adv_data[5] == adv_address[3]) && (adv_data[6] == adv_address[2])) |
electronichamsters | 11:d5edb6e3edab | 345 | return 1; |
electronichamsters | 11:d5edb6e3edab | 346 | else |
electronichamsters | 11:d5edb6e3edab | 347 | return 0; |
electronichamsters | 8:46c5e0bfab05 | 348 | } |
electronichamsters | 8:46c5e0bfab05 | 349 | |
electronichamsters | 9:1ea51b2048a8 | 350 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
electronichamsters | 8:46c5e0bfab05 | 351 | |
electronichamsters | 8:46c5e0bfab05 | 352 | |
electronichamsters | 9:1ea51b2048a8 | 353 | //BLE MAC Address (6 bytes): |
electronichamsters | 9:1ea51b2048a8 | 354 | // params->peerAddr[5] |
electronichamsters | 9:1ea51b2048a8 | 355 | // params->peerAddr[4] |
electronichamsters | 9:1ea51b2048a8 | 356 | // ... |
electronichamsters | 9:1ea51b2048a8 | 357 | // params->peerAddr[0] |
electronichamsters | 9:1ea51b2048a8 | 358 | //RSSI = params->rssi |
electronichamsters | 9:1ea51b2048a8 | 359 | //Payload |
electronichamsters | 9:1ea51b2048a8 | 360 | // params->advertisingData[#<advertisingDataLen] |
electronichamsters | 8:46c5e0bfab05 | 361 | |
electronichamsters | 8:46c5e0bfab05 | 362 | |
electronichamsters | 8:46c5e0bfab05 | 363 | |
electronichamsters | 9:1ea51b2048a8 | 364 | // [<---MY DATA-->] |
electronichamsters | 9:1ea51b2048a8 | 365 | //0x02 0x01 0x06 0x06 0xff D E C ? ? |
electronichamsters | 9:1ea51b2048a8 | 366 | //0 1 2 3 4 5 6 7 8 9 |
electronichamsters | 8:46c5e0bfab05 | 367 | |
electronichamsters | 9:1ea51b2048a8 | 368 | if ( (params->advertisingDataLen) >= 8) |
electronichamsters | 9:1ea51b2048a8 | 369 | { |
electronichamsters | 9:1ea51b2048a8 | 370 | //if one of "ours", these UUID fields will be this value. Arbitrary " |
electronichamsters | 11:d5edb6e3edab | 371 | |
electronichamsters | 11:d5edb6e3edab | 372 | //if ( (params->advertisingData[5] == 0x44) && (params->advertisingData[6] == 0x45) && (params->advertisingData[7]==0x43) ) |
electronichamsters | 11:d5edb6e3edab | 373 | //if (is_ours(params->advertisingData[5],params->advertisingData[6],params->advertisingData[7], params->advertisingData[8], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0] )) |
electronichamsters | 11:d5edb6e3edab | 374 | if (is_ours(params->advertisingData, params->peerAddr)) |
electronichamsters | 9:1ea51b2048a8 | 375 | { |
electronichamsters | 11:d5edb6e3edab | 376 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 377 | device.printf("----------- ADV CAll Back -----------------\r\n "); |
electronichamsters | 11:d5edb6e3edab | 378 | #endif |
electronichamsters | 11:d5edb6e3edab | 379 | //************** |
electronichamsters | 11:d5edb6e3edab | 380 | // Decrypt data |
electronichamsters | 11:d5edb6e3edab | 381 | //************** |
electronichamsters | 11:d5edb6e3edab | 382 | //prep array for deciphering the encrypted portion of advertisement |
electronichamsters | 11:d5edb6e3edab | 383 | //device.printf("\r\n copy adv length: %d \r\n", params->advertisingDataLen); //always 31 |
electronichamsters | 11:d5edb6e3edab | 384 | for (int i = 0; i<16; i++) |
electronichamsters | 11:d5edb6e3edab | 385 | { |
electronichamsters | 11:d5edb6e3edab | 386 | src_buf[i]=params->advertisingData[i+15]; |
electronichamsters | 11:d5edb6e3edab | 387 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 388 | //device.printf("%x ", src_buf[i]); |
electronichamsters | 11:d5edb6e3edab | 389 | #endif |
electronichamsters | 11:d5edb6e3edab | 390 | } |
electronichamsters | 11:d5edb6e3edab | 391 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 392 | //device.printf("...\r\n "); |
electronichamsters | 11:d5edb6e3edab | 393 | #endif |
electronichamsters | 11:d5edb6e3edab | 394 | |
electronichamsters | 11:d5edb6e3edab | 395 | |
electronichamsters | 11:d5edb6e3edab | 396 | mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, src_buf, des_buf ); //execution time not an issue |
electronichamsters | 11:d5edb6e3edab | 397 | |
electronichamsters | 11:d5edb6e3edab | 398 | |
electronichamsters | 11:d5edb6e3edab | 399 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 400 | |
electronichamsters | 11:d5edb6e3edab | 401 | //device.printf("decoded first 16 bytes \r\n"); |
electronichamsters | 11:d5edb6e3edab | 402 | for (int i = 0; i<16; i++) |
electronichamsters | 11:d5edb6e3edab | 403 | { |
electronichamsters | 11:d5edb6e3edab | 404 | //device.printf("%02x", params->advertisingData[index]); |
electronichamsters | 11:d5edb6e3edab | 405 | if (i < 2) |
electronichamsters | 11:d5edb6e3edab | 406 | { |
electronichamsters | 11:d5edb6e3edab | 407 | //device.printf("%x ", des_buf[i]); |
electronichamsters | 11:d5edb6e3edab | 408 | } |
electronichamsters | 11:d5edb6e3edab | 409 | else |
electronichamsters | 11:d5edb6e3edab | 410 | { |
electronichamsters | 11:d5edb6e3edab | 411 | //device.printf("%c ", des_buf[i]); |
electronichamsters | 11:d5edb6e3edab | 412 | } |
electronichamsters | 11:d5edb6e3edab | 413 | } |
electronichamsters | 11:d5edb6e3edab | 414 | //device.printf("done----- \r\n"); |
electronichamsters | 11:d5edb6e3edab | 415 | #endif |
electronichamsters | 11:d5edb6e3edab | 416 | //save MAC address |
electronichamsters | 11:d5edb6e3edab | 417 | Received_MAC_Addr[0] = params->peerAddr[0]; |
electronichamsters | 11:d5edb6e3edab | 418 | Received_MAC_Addr[1] = params->peerAddr[1]; |
electronichamsters | 11:d5edb6e3edab | 419 | Received_MAC_Addr[2] = params->peerAddr[2]; |
electronichamsters | 11:d5edb6e3edab | 420 | Received_MAC_Addr[3] = params->peerAddr[3]; |
electronichamsters | 11:d5edb6e3edab | 421 | Received_MAC_Addr[4] = params->peerAddr[4]; |
electronichamsters | 11:d5edb6e3edab | 422 | Received_MAC_Addr[5] = params->peerAddr[5]; |
electronichamsters | 11:d5edb6e3edab | 423 | |
electronichamsters | 11:d5edb6e3edab | 424 | uint16_t beacon_timer = des_buf[0] | (des_buf[1] << 8); //it's a 2 byte uint little endian |
electronichamsters | 11:d5edb6e3edab | 425 | Received_MAC_Time = beacon_timer; |
electronichamsters | 11:d5edb6e3edab | 426 | Received_Xmit_Cnt = des_buf[2]; |
electronichamsters | 11:d5edb6e3edab | 427 | |
electronichamsters | 11:d5edb6e3edab | 428 | |
electronichamsters | 11:d5edb6e3edab | 429 | |
electronichamsters | 9:1ea51b2048a8 | 430 | /* |
electronichamsters | 9:1ea51b2048a8 | 431 | BLE Received from MAC C2F154BB0AF9 |
electronichamsters | 9:1ea51b2048a8 | 432 | volt:3.11,mag:1 |
electronichamsters | 9:1ea51b2048a8 | 433 | |
electronichamsters | 9:1ea51b2048a8 | 434 | uart-transmit: |
electronichamsters | 9:1ea51b2048a8 | 435 | mac:C2F154BB0AF9,rssi:##,volt:3.11,mag:1 |
electronichamsters | 9:1ea51b2048a8 | 436 | |
electronichamsters | 9:1ea51b2048a8 | 437 | /ble/C2F154BB0AF9/rssi |
electronichamsters | 9:1ea51b2048a8 | 438 | /ble/C2F154BB0AF9/1st_token |
electronichamsters | 9:1ea51b2048a8 | 439 | /ble/c2F153 |
electronichamsters | 9:1ea51b2048a8 | 440 | |
electronichamsters | 9:1ea51b2048a8 | 441 | */ |
electronichamsters | 9:1ea51b2048a8 | 442 | /* |
electronichamsters | 9:1ea51b2048a8 | 443 | device.printf("Adv peerAddr: [%02x%02x%02x%02x%02x%02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", |
electronichamsters | 9:1ea51b2048a8 | 444 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
electronichamsters | 9:1ea51b2048a8 | 445 | params->rssi, params->isScanResponse, params->type); |
electronichamsters | 9:1ea51b2048a8 | 446 | */ |
electronichamsters | 9:1ea51b2048a8 | 447 | |
electronichamsters | 11:d5edb6e3edab | 448 | // punch out the data as json |
electronichamsters | 11:d5edb6e3edab | 449 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 450 | // 1. MAC and RSSI |
electronichamsters | 11:d5edb6e3edab | 451 | // "mac":xxxxxxxx,rssi:xx, |
electronichamsters | 11:d5edb6e3edab | 452 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 453 | uint8_t ADV_Result = Is_Not_Spoofed(); |
electronichamsters | 11:d5edb6e3edab | 454 | #if MyDebugEnb |
electronichamsters | 11:d5edb6e3edab | 455 | device.printf("--------ADV_Result = %d", ADV_Result); |
electronichamsters | 11:d5edb6e3edab | 456 | #endif |
electronichamsters | 11:d5edb6e3edab | 457 | |
electronichamsters | 11:d5edb6e3edab | 458 | |
electronichamsters | 11:d5edb6e3edab | 459 | if (ADV_Result == ADVisNew) |
electronichamsters | 9:1ea51b2048a8 | 460 | { |
electronichamsters | 11:d5edb6e3edab | 461 | device.printf("{\"mac\":\"%02x%02x%02x%02x%02x%02x\",\"rssi\":%d,", |
electronichamsters | 11:d5edb6e3edab | 462 | params->peerAddr[5], |
electronichamsters | 11:d5edb6e3edab | 463 | params->peerAddr[4], |
electronichamsters | 11:d5edb6e3edab | 464 | params->peerAddr[3], |
electronichamsters | 11:d5edb6e3edab | 465 | params->peerAddr[2], |
electronichamsters | 11:d5edb6e3edab | 466 | params->peerAddr[1], |
electronichamsters | 11:d5edb6e3edab | 467 | params->peerAddr[0], |
electronichamsters | 11:d5edb6e3edab | 468 | params->rssi |
electronichamsters | 11:d5edb6e3edab | 469 | ); |
electronichamsters | 11:d5edb6e3edab | 470 | //mac:"c2f154bb0af9" |
electronichamsters | 11:d5edb6e3edab | 471 | |
electronichamsters | 11:d5edb6e3edab | 472 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 473 | // 2. Volt |
electronichamsters | 11:d5edb6e3edab | 474 | // "volt":3.03, |
electronichamsters | 11:d5edb6e3edab | 475 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 476 | device.printf("\"volt\":%c%c%c%c,", |
electronichamsters | 11:d5edb6e3edab | 477 | params->advertisingData[9], |
electronichamsters | 11:d5edb6e3edab | 478 | params->advertisingData[10], |
electronichamsters | 11:d5edb6e3edab | 479 | params->advertisingData[11], |
electronichamsters | 11:d5edb6e3edab | 480 | params->advertisingData[12]); |
electronichamsters | 11:d5edb6e3edab | 481 | |
electronichamsters | 11:d5edb6e3edab | 482 | |
electronichamsters | 11:d5edb6e3edab | 483 | |
electronichamsters | 11:d5edb6e3edab | 484 | |
electronichamsters | 11:d5edb6e3edab | 485 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 486 | // 3. clock |
electronichamsters | 11:d5edb6e3edab | 487 | // "tmr":xxx, |
electronichamsters | 11:d5edb6e3edab | 488 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 489 | //print clock |
electronichamsters | 11:d5edb6e3edab | 490 | // "tmr":3232, |
electronichamsters | 11:d5edb6e3edab | 491 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 492 | |
electronichamsters | 11:d5edb6e3edab | 493 | device.printf("\"tmr\":%d,", beacon_timer); |
electronichamsters | 11:d5edb6e3edab | 494 | //device.printf("\"tmr\":%d,", des_buf[0]); |
electronichamsters | 11:d5edb6e3edab | 495 | device.printf("\"xmit_cnt\":%d,", Received_Xmit_Cnt); |
electronichamsters | 11:d5edb6e3edab | 496 | |
electronichamsters | 11:d5edb6e3edab | 497 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 498 | // 4. rest of payload as json |
electronichamsters | 11:d5edb6e3edab | 499 | // "tmr":xxx, |
electronichamsters | 11:d5edb6e3edab | 500 | //--------------------------------------- |
electronichamsters | 11:d5edb6e3edab | 501 | for (int i = 3; i< 16; i++) |
electronichamsters | 9:1ea51b2048a8 | 502 | { |
electronichamsters | 11:d5edb6e3edab | 503 | //device.printf("%02x", params->advertisingData[index]); |
electronichamsters | 11:d5edb6e3edab | 504 | device.printf("%c", des_buf[i]); //print as character |
electronichamsters | 9:1ea51b2048a8 | 505 | } |
electronichamsters | 11:d5edb6e3edab | 506 | |
electronichamsters | 11:d5edb6e3edab | 507 | |
electronichamsters | 11:d5edb6e3edab | 508 | |
electronichamsters | 11:d5edb6e3edab | 509 | /* |
electronichamsters | 11:d5edb6e3edab | 510 | for (unsigned index = 8; index < params->advertisingDataLen; index++) |
electronichamsters | 9:1ea51b2048a8 | 511 | { |
electronichamsters | 9:1ea51b2048a8 | 512 | //device.printf("%02x", params->advertisingData[index]); |
electronichamsters | 9:1ea51b2048a8 | 513 | device.printf("%c", params->advertisingData[index]); |
electronichamsters | 11:d5edb6e3edab | 514 | } |
electronichamsters | 11:d5edb6e3edab | 515 | */ |
electronichamsters | 11:d5edb6e3edab | 516 | |
electronichamsters | 11:d5edb6e3edab | 517 | device.printf("}"); |
electronichamsters | 11:d5edb6e3edab | 518 | device.printf("\r\n"); |
electronichamsters | 11:d5edb6e3edab | 519 | |
electronichamsters | 11:d5edb6e3edab | 520 | /* |
electronichamsters | 11:d5edb6e3edab | 521 | mac:c2f154bb0af9,rssi:-55,volt:3.05,mag:1 |
electronichamsters | 11:d5edb6e3edab | 522 | mac:c2f154bb0af9,rssi:-67,volt:3.05,mag:1 |
electronichamsters | 11:d5edb6e3edab | 523 | mac:c2f154bb0af9,rssi:-60,volt:3.07,mag:0 |
electronichamsters | 11:d5edb6e3edab | 524 | */ |
electronichamsters | 9:1ea51b2048a8 | 525 | } |
electronichamsters | 11:d5edb6e3edab | 526 | if (ADV_Result == ADVisDuplicate) |
electronichamsters | 9:1ea51b2048a8 | 527 | { |
electronichamsters | 11:d5edb6e3edab | 528 | device.printf("{\"mac\":\"%02x%02x%02x%02x%02x%02x\",\"duplicate\": %d}\r\n", |
electronichamsters | 11:d5edb6e3edab | 529 | params->peerAddr[5], |
electronichamsters | 11:d5edb6e3edab | 530 | params->peerAddr[4], |
electronichamsters | 11:d5edb6e3edab | 531 | params->peerAddr[3], |
electronichamsters | 11:d5edb6e3edab | 532 | params->peerAddr[2], |
electronichamsters | 11:d5edb6e3edab | 533 | params->peerAddr[1], |
electronichamsters | 11:d5edb6e3edab | 534 | params->peerAddr[0], |
electronichamsters | 11:d5edb6e3edab | 535 | ADV_Result); |
electronichamsters | 9:1ea51b2048a8 | 536 | } |
electronichamsters | 11:d5edb6e3edab | 537 | if (ADV_Result == ADVisSpoof) |
electronichamsters | 11:d5edb6e3edab | 538 | { |
electronichamsters | 11:d5edb6e3edab | 539 | device.printf("{\"mac\":\"%02x%02x%02x%02x%02x%02x\",\"spoof\": %d}\r\n", |
electronichamsters | 11:d5edb6e3edab | 540 | params->peerAddr[5], |
electronichamsters | 11:d5edb6e3edab | 541 | params->peerAddr[4], |
electronichamsters | 11:d5edb6e3edab | 542 | params->peerAddr[3], |
electronichamsters | 11:d5edb6e3edab | 543 | params->peerAddr[2], |
electronichamsters | 11:d5edb6e3edab | 544 | params->peerAddr[1], |
electronichamsters | 11:d5edb6e3edab | 545 | params->peerAddr[0], |
electronichamsters | 11:d5edb6e3edab | 546 | ADV_Result); |
electronichamsters | 11:d5edb6e3edab | 547 | } |
electronichamsters | 11:d5edb6e3edab | 548 | if (ADV_Result == ADVisUnknown) |
electronichamsters | 11:d5edb6e3edab | 549 | { |
electronichamsters | 11:d5edb6e3edab | 550 | device.printf("{\"mac\":\"%02x%02x%02x%02x%02x%02x\",\"unknown\": %d}\r\n", |
electronichamsters | 11:d5edb6e3edab | 551 | params->peerAddr[5], |
electronichamsters | 11:d5edb6e3edab | 552 | params->peerAddr[4], |
electronichamsters | 11:d5edb6e3edab | 553 | params->peerAddr[3], |
electronichamsters | 11:d5edb6e3edab | 554 | params->peerAddr[2], |
electronichamsters | 11:d5edb6e3edab | 555 | params->peerAddr[1], |
electronichamsters | 11:d5edb6e3edab | 556 | params->peerAddr[0], |
electronichamsters | 11:d5edb6e3edab | 557 | ADV_Result); |
electronichamsters | 11:d5edb6e3edab | 558 | } |
electronichamsters | 9:1ea51b2048a8 | 559 | }//end if it's our adv |
electronichamsters | 9:1ea51b2048a8 | 560 | }//end if advertisingDataLen |
electronichamsters | 8:46c5e0bfab05 | 561 | |
electronichamsters | 11:d5edb6e3edab | 562 | }//end advertisementCallback |
electronichamsters | 8:46c5e0bfab05 | 563 | |
electronichamsters | 8:46c5e0bfab05 | 564 | |
electronichamsters | 8:46c5e0bfab05 | 565 | |
andresag | 7:0a8bbb6dea16 | 566 | /** |
electronichamsters | 9:1ea51b2048a8 | 567 | * This function is called when the ble initialization process has failed |
andresag | 7:0a8bbb6dea16 | 568 | */ |
andresag | 7:0a8bbb6dea16 | 569 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 7:0a8bbb6dea16 | 570 | { |
andresag | 7:0a8bbb6dea16 | 571 | /* Initialization error handling should go here */ |
electronichamsters | 9:1ea51b2048a8 | 572 | device.printf("periodic callback "); |
electronichamsters | 9:1ea51b2048a8 | 573 | device.printf("\r\n"); |
andresag | 7:0a8bbb6dea16 | 574 | } |
andresag | 7:0a8bbb6dea16 | 575 | |
andresag | 7:0a8bbb6dea16 | 576 | /** |
andresag | 7:0a8bbb6dea16 | 577 | * Callback triggered when the ble initialization process has finished |
andresag | 7:0a8bbb6dea16 | 578 | */ |
andresag | 7:0a8bbb6dea16 | 579 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
sunsmile2015 | 2:b935358da5ba | 580 | { |
andresag | 7:0a8bbb6dea16 | 581 | BLE& ble = params->ble; |
andresag | 7:0a8bbb6dea16 | 582 | ble_error_t error = params->error; |
andresag | 7:0a8bbb6dea16 | 583 | |
andresag | 7:0a8bbb6dea16 | 584 | if (error != BLE_ERROR_NONE) { |
andresag | 7:0a8bbb6dea16 | 585 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 7:0a8bbb6dea16 | 586 | onBleInitError(ble, error); |
andresag | 7:0a8bbb6dea16 | 587 | return; |
andresag | 7:0a8bbb6dea16 | 588 | } |
andresag | 7:0a8bbb6dea16 | 589 | |
andresag | 7:0a8bbb6dea16 | 590 | /* Ensure that it is the default instance of BLE */ |
andresag | 7:0a8bbb6dea16 | 591 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 7:0a8bbb6dea16 | 592 | return; |
andresag | 7:0a8bbb6dea16 | 593 | } |
electronichamsters | 9:1ea51b2048a8 | 594 | |
electronichamsters | 9:1ea51b2048a8 | 595 | // in ms. Duty cycle = (interval / window); 200ms/500ms = 40% |
electronichamsters | 9:1ea51b2048a8 | 596 | ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); |
electronichamsters | 9:1ea51b2048a8 | 597 | ble.gap().startScan(advertisementCallback); |
sunsmile2015 | 4:e5fa4c8838db | 598 | } |
sunsmile2015 | 4:e5fa4c8838db | 599 | |
sunsmile2015 | 2:b935358da5ba | 600 | int main(void) |
sunsmile2015 | 2:b935358da5ba | 601 | { |
electronichamsters | 11:d5edb6e3edab | 602 | for (int i=0; i<sizeOfSpoof; i++) |
electronichamsters | 11:d5edb6e3edab | 603 | { |
electronichamsters | 11:d5edb6e3edab | 604 | Spoof_Check[i].MAC_Addr[0]= 0; |
electronichamsters | 11:d5edb6e3edab | 605 | Spoof_Check[i].MAC_Addr[1]= 0; |
electronichamsters | 11:d5edb6e3edab | 606 | Spoof_Check[i].MAC_Addr[2]= 0; |
electronichamsters | 11:d5edb6e3edab | 607 | Spoof_Check[i].MAC_Time = 0; |
electronichamsters | 11:d5edb6e3edab | 608 | Spoof_Check[i].Attempt_MAC_Time = 0; |
electronichamsters | 11:d5edb6e3edab | 609 | Spoof_Check[i].Attempt_Cnt = 0; |
electronichamsters | 11:d5edb6e3edab | 610 | Spoof_Check[i].Attempt_Cnt = 0; |
electronichamsters | 11:d5edb6e3edab | 611 | Spoof_Check[i].Xmit_Cnt = 0; |
electronichamsters | 11:d5edb6e3edab | 612 | } |
electronichamsters | 11:d5edb6e3edab | 613 | |
electronichamsters | 11:d5edb6e3edab | 614 | Gateway_Time.start(); |
electronichamsters | 8:46c5e0bfab05 | 615 | device.baud(9600); |
electronichamsters | 11:d5edb6e3edab | 616 | device.printf("started main 06 hashed observer... "); |
electronichamsters | 8:46c5e0bfab05 | 617 | device.printf("\r\n"); |
electronichamsters | 11:d5edb6e3edab | 618 | ticker.attach(periodicCallback, Periodicity); |
electronichamsters | 8:46c5e0bfab05 | 619 | |
electronichamsters | 9:1ea51b2048a8 | 620 | //mbedtls_aes_init(&aes); |
electronichamsters | 9:1ea51b2048a8 | 621 | mbedtls_aes_setkey_dec( &aes, iv, 128 ); |
electronichamsters | 9:1ea51b2048a8 | 622 | //mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, input, output ); |
sunsmile2015 | 0:3dc6e424dba0 | 623 | |
andresag | 7:0a8bbb6dea16 | 624 | BLE &ble = BLE::Instance(); |
andresag | 7:0a8bbb6dea16 | 625 | ble.init(bleInitComplete); |
rgrover1 | 5:f4d74a8cad43 | 626 | |
sunsmile2015 | 0:3dc6e424dba0 | 627 | while (true) { |
electronichamsters | 9:1ea51b2048a8 | 628 | ble.waitForEvent(); |
electronichamsters | 9:1ea51b2048a8 | 629 | } |
electronichamsters | 9:1ea51b2048a8 | 630 | } |