UpdatedDecryp

Dependencies:   BahlDecrypModified CyaSSL mbed nRF51822

Fork of Decryptulator by Mobius IoT

Revision:
9:69a2ad0bcdb7
Parent:
8:649bd171929e
Child:
11:16f67d5752e1
diff -r 649bd171929e -r 69a2ad0bcdb7 main.cpp
--- a/main.cpp	Fri Jul 31 09:13:50 2015 +0000
+++ b/main.cpp	Wed Aug 26 12:58:23 2015 +0000
@@ -15,57 +15,57 @@
  */
 
 #include "mbed.h"
-#include "BLE.h"
+#include "toolchain.h"
+#include "ble/BLE.h"
 #include "TMP_nrf51/TMP_nrf51.h"
 
-#define APP_SPECIFIC_ID_TEST 0xFEFE
-
-#pragma pack(1)
-/* Advertising data  */
-struct AdvertisingData_t {
-    uint8_t length; /* doesn't include itself */
-    GapAdvertisingData::DataType dataType;
-    uint8_t data[1];
-};
-
-struct ApplicationData_t {
-    uint16_t applicationSpecificId;             /* An ID used to identify temperature value
-                                                   in the manufacture specific AD data field */
-    TMP_nrf51::tmpSensorValue_t tmpSensorValue; /* User defined application data */
-};
-#pragma pack()
-
 BLE        ble;
-DigitalOut led1(LED1);
+DigitalOut alivenessLED(LED1, 1);
 
 void periodicCallback(void)
 {
-    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
+    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
 }
 
+/*
+ * This function is called every time we scan an advertisement.
+ */
 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-    AdvertisingData_t *pAdvData = NULL;
-    uint8_t len = 0;
-    
-    /* Search for the manufacturer data */
-    while(len < params->advertisingDataLen) {
-        pAdvData = (AdvertisingData_t *)&params->advertisingData[len];
-        if(pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
+    struct AdvertisingData_t {
+        uint8_t                        length; /* doesn't include itself */
+        GapAdvertisingData::DataType_t dataType;
+        uint8_t                        data[0];
+    } PACKED;
+
+    struct ApplicationData_t {
+        uint16_t applicationSpecificId;             /* An ID used to identify temperature value
+                                                       in the manufacture specific AD data field */
+        TMP_nrf51::TempSensorValue_t tmpSensorValue; /* User defined application data */
+    } PACKED;
+
+    static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
+
+    /* Search for the manufacturer specific data with matching application-ID */
+    AdvertisingData_t *pAdvData;
+    size_t index = 0;
+    while (index < params->advertisingDataLen) {
+        pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
+        if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
             ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
-            if(pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
+            if (pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
+                /* dump information on the console. */
                 printf("From [%02x %02x %02x], ", params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
-                printf("Temp is %f\r\n", (float)pAppData->tmpSensorValue);
+                printf("Temp is %f\r\n", (TMP_nrf51::TempSensorValue_t)pAppData->tmpSensorValue);
                 break;
             }
         }
-        len += (pAdvData->length + 1);
+        index += (pAdvData->length + 1);
     }
 }
 
 int main(void)
 {
-    led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);