Kenji Arai / Mbed 2 deprecated BLE_EddystoneBeacon_w_ACC_TY51822

Dependencies:   BLE_API LIS3DH mbed nRF51822 BMC050 nRF51_LowPwr nRF51_Vdd

Fork of BLE_EddystoneBeacon_Service by Bluetooth Low Energy

Revision:
14:5a2a104a21a8
Parent:
13:91c356fa928e
Child:
15:af8c24f34a9f
--- a/ZipBeaconConfigService.h	Fri Jul 17 21:45:03 2015 +0000
+++ b/ZipBeaconConfigService.h	Mon Jul 20 04:29:07 2015 +0000
@@ -35,7 +35,7 @@
 static const uint8_t UUID_TX_POWER_MODE_CHAR[]    = UUID_URI_BEACON(0x20, 0x87);
 static const uint8_t UUID_BEACON_PERIOD_CHAR[]    = UUID_URI_BEACON(0x20, 0x88);
 static const uint8_t UUID_RESET_CHAR[]            = UUID_URI_BEACON(0x20, 0x89);
-static const uint8_t BEACON_UUID[] = {0xAA, 0xFE};
+static const uint8_t BEACON_EDDYSTONE[] = {0xAA, 0xFE};
 
 /**
 * @class ZipBeaconConfigService
@@ -60,7 +60,9 @@
     typedef uint8_t Lock_t[16];               /* 128 bits */
     typedef int8_t PowerLevels_t[NUM_POWER_MODES];
 
-    
+    #define EDDYSTONE_MAX_FRAMETYPE 3
+    void (*frames[EDDYSTONE_MAX_FRAMETYPE])(uint8_t *, uint32_t);
+    uint8_t frameIndex;
     static const int URI_DATA_MAX = 18;
     typedef uint8_t  UriData_t[URI_DATA_MAX];
     
@@ -90,7 +92,7 @@
         UIDNamespaceID_t uidNamespaceID; // UUID type, Namespace ID, 10B
         UIDInstanceID_t  uidInstanceID;  // UUID type, Instance ID,  6B
     };
-
+    
     /**
      * @param[ref]    ble
      *                    BLEDevice object for the underlying controller.
@@ -370,12 +372,50 @@
         TlmTimeSinceBoot = timeSinceBoot;
         return;        
     }
+    
+    /*
+    *  Callback from onRadioNotification(), used to update the PDUCounter and maybe other stuff eventually?
+    *
+    *
+    */
+    void pduCountCallback(){
+        // Update Time and PDUCount
+        TlmPduCount++;
+        TlmTimeSinceBoot += 10;
+        
+        // Every 1 second switch the frame types
+        if((TlmPduCount % 10) == 1){
+            frameIndex = frameIndex % EDDYSTONE_MAX_FRAMETYPE;
+            uint8_t serviceData[SERVICE_DATA_MAX];
+            unsigned serviceDataLen = 0;
+            //switch payloads
+            serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0];
+            serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1];
+            switch(frameIndex){
+                case 0:
+                    constructTLMFrame(serviceData+serviceDataLen,20);
+                    break;
+                case 1:
+                    constructURLFrame(serviceData+serviceDataLen,20);
+                    break;
+                case 2:
+                    constructUIDFrame(serviceData+serviceDataLen,20);
+                    break;
+                }
+            ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen);
+        }
+        //TODO: add bit to keep timer / update time
+        frameIndex++;
+        return;        
+    }
 
     /* Helper function to switch to the non-connectible normal mode for ZipBeacon. This gets called after a timeout. */
     void setupZipBeaconAdvertisements()
     {
         uint8_t serviceData[SERVICE_DATA_MAX];
         unsigned serviceDataLen = 0;
+        // Initialize Frame transition
+        frameIndex = 0;
 
         /* Reinitialize the BLE stack. This will clear away the existing services and advertising state. */
         ble.shutdown();
@@ -397,16 +437,26 @@
         ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
         ble.setAdvertisingInterval(beaconPeriod);
         ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID));
+        ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_EDDYSTONE, sizeof(BEACON_EDDYSTONE));
+        
+        setTLMFrameData(0x00,22,33,0,0); // Initialize TLM Data, for testing, remove for release
+        updateTlmPduCount(0);
+        updateTlmTimeSinceBoot(0);
+        
+        // Construct TLM Frame in initial advertising. 
+        serviceData[serviceDataLen++] = BEACON_EDDYSTONE[0];
+        serviceData[serviceDataLen++] = BEACON_EDDYSTONE[1];
+        constructTLMFrame(serviceData+serviceDataLen,20);
+//        serviceData[serviceDataLen++] = FRAME_TYPE_URL | flags;
+//        serviceData[serviceDataLen++] = advPowerLevels[txPowerMode];
+//        for (unsigned j = 0; j < uriDataLength; j++) {
+//            serviceData[serviceDataLen++] = uriData[j];
+//        }
 
-        serviceData[serviceDataLen++] = BEACON_UUID[0];
-        serviceData[serviceDataLen++] = BEACON_UUID[1];
-        serviceData[serviceDataLen++] = FRAME_TYPE_URL | flags;
-        serviceData[serviceDataLen++] = advPowerLevels[txPowerMode];
-        for (unsigned j = 0; j < uriDataLength; j++) {
-            serviceData[serviceDataLen++] = uriData[j];
-        }
+        // attach callback to count number of sent packets
+       //TODO: ble.gap().onRadioNotification(pduCountCallback);
         ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, serviceData, serviceDataLen);
+        //callbackTick.attach(&pduCountCallback,2.0); // switch services every 2 seconds
     }
 
   private:
@@ -565,6 +615,7 @@
 
     BLEDevice           &ble;
     Params_t            &params;
+    Ticker callbackTick;
     // Default value that is restored on reset
     size_t              defaultUriDataLength;
     UriData_t           defaultUriData;