Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Revision:
44:0e27ee7800b8
Parent:
43:e4a4f9a8766f
Child:
45:1827e4f496a7
--- a/EddystoneConfigService.h	Thu Sep 10 23:25:45 2015 +0000
+++ b/EddystoneConfigService.h	Fri Sep 11 19:38:12 2015 +0000
@@ -118,30 +118,12 @@
      * @param[in/out] paramsIn
      *                    Reference to application-visible beacon state, loaded
      *                    from persistent storage at startup.
-     * @paramsP[in]   resetToDefaultsFlag
-     *                    Applies to the state of the 'paramsIn' parameter.
-     *                    If true, it indicates that paramsIn is potentially
-     *                    un-initialized, and default values should be used
-     *                    instead. Otherwise, paramsIn overrides the defaults.
      * @param[in]     defaultAdvPowerLevelsIn
      *                    Default power-levels array; applies only if the resetToDefaultsFlag is true.
-     * @param[in]     defaultUriDataIn
-     *                    Default un-encoded URI; optional parameter, applies only if the resetToDefaultsFlag is true
-     * @param[in]     defaultUIDNamespaceIDIn
-     *                    Default 10Byte UID Namespace ID; optional parameter, applies only if the resetToDefaultsFlag is true
-     * @param[in]     defaultUIDInstanceIDIn
-     *                    Default 6byte UID Instance ID; optional parameter, applies only if the resetToDefaultsFlag is true
-     * @param[in]     defaultTLMVersionIn
-     *                    Default TLM version; defaults to 0, applies only if the resetToDefaultsFlag is true
      */
     EddystoneConfigService(BLEDevice     &bleIn,
                            Params_t      &paramsIn,
-                           bool          resetToDefaultsFlag,
-                           PowerLevels_t &defaultAdvPowerLevelsIn,
-                           const char   *defaultURIDataIn = NULL,
-                           UIDNamespaceID_t *defaultUIDNamespaceIDIn = NULL,
-                           UIDInstanceID_t  *defaultUIDInstanceIDIn = NULL,
-                           uint8_t defaultTLMVersionIn = 0) :
+                           PowerLevels_t &defaultAdvPowerLevelsIn) :
         ble(bleIn),
         params(paramsIn),       // Initialize URL Data
         defaultUriDataLength(),
@@ -150,17 +132,13 @@
         defaultUidInstanceID(),
         defaultUrlPower(params.advPowerLevels[params.txPowerMode]),
         defaultUidPower(params.advPowerLevels[params.txPowerMode]),
-        uidIsSet(true),
-        urlIsSet(true),
         defaultAdvPowerLevels(defaultAdvPowerLevelsIn),
         initSucceeded(false),
         resetFlag(),
+        tlmIsSet(false),
+        urlIsSet(false),
+        uidIsSet(false),
         lockedStateChar(UUID_LOCK_STATE_CHAR, &params.lockedState),
-        TlmVersion(defaultTLMVersionIn), // Initialize TLM Data
-        TlmBatteryVoltage(0),
-        TlmBeaconTemp(0),
-        TlmPduCount(0),
-        TlmTimeSinceBoot(0),
         lockChar(UUID_LOCK_CHAR, &params.lock),
         uriDataChar(UUID_URI_DATA_CHAR, params.uriData, 0, URI_DATA_MAX,
                     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE),
@@ -171,18 +149,20 @@
         beaconPeriodChar(UUID_BEACON_PERIOD_CHAR, &params.beaconPeriod),
         resetChar(UUID_RESET_CHAR, &resetFlag) 
         {
-        // set eddystone as not configured yet. 
+        // set eddystone as not configured yet. Used to exit config before timeout if GATT services are written to.
         params.isConfigured = false;
-        //Set UID frame
-        memcpy(defaultUidNamespaceID,defaultUIDNamespaceIDIn,UID_NAMESPACEID_SIZE);
-        memcpy(defaultUidInstanceID,defaultUIDInstanceIDIn,UID_INSTANCEID_SIZE);
-        
-        // Set URL Frame
-        encodeURI(defaultURIDataIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting
-        if (defaultUriDataLength > URI_DATA_MAX) {
-            return;
-        }
-
+    }
+    
+    /*
+    * @breif Start EddystoneConfig advertising. This function should be called after the EddystoneConfig constructor and after all the frames have been added. 
+    * @paramsP[in]   resetToDefaultsFlag
+     *                    Applies to the state of the 'paramsIn' parameter.
+     *                    If true, it indicates that paramsIn is potentially
+     *                    un-initialized, and default values should be used
+     *                    instead. Otherwise, paramsIn overrides the defaults.
+    * 
+    */
+    void start(bool resetToDefaultsFlag){
         if (!resetToDefaultsFlag && (params.uriDataLength > URI_DATA_MAX)) {
             resetToDefaultsFlag = true;
         }
@@ -222,6 +202,60 @@
     bool initSuccessfully(void) const {
         return initSucceeded;
     }
+    
+    /*
+    * @breif Function to update the default values for the TLM frame. Only applied if Reset Defaults is applied. 
+    *
+    * @param[in] tlmVersionIn     Version of the TLM frame being used
+    * @param[in] advPeriodInMin how long between TLM frames being advertised, this is measured in minutes.
+    *
+    */
+    void setDefaultTLMFrameData(uint8_t tlmVersionIn = 0, uint32_t advPeriodInMin = 1){
+        DBG("Setting Default TLM Data, version = %d, advPeriodInMind= %d",tlmVersionIn,advPeriodInMin);
+        defaultTlmVersion = tlmVersionIn;
+        TlmBatteryVoltage = 0;
+        TlmBeaconTemp = 0;
+        TlmPduCount = 0;
+        TlmTimeSinceBoot = 0;
+        defaultTlmAdvPeriod = advPeriodInMin;
+        tlmIsSet = true; // flag to add this to eddystone service when config is done
+    
+    }
+    
+    /*
+    * @breif Function to update the default values for the URI frame. Only applied if Reset Defaults is applied. 
+    *
+    * @param[in] uriIn      url to advertise
+    * @param[in] advPeriod  how long to advertise the url for, measured in number of ADV frames.
+    *
+    */
+    void setDefaultURIFrameData(const char * uriIn,uint32_t  advPeriod = 1000){
+        DBG("Setting Default URI Data");
+        // Set URL Frame
+        encodeURI(uriIn, defaultUriData, defaultUriDataLength); // encode URL to URL Formatting
+        if (defaultUriDataLength > URI_DATA_MAX) {
+            return;
+        }
+        defaultUriAdvPeriod = advPeriod;
+        urlIsSet = true; // flag to add this to eddystone service when config is done
+    }
+    
+    /*
+    * @breif Function to update the default values for the UID frame. Only applied if Reset Defaults is applied. 
+    *
+    * @param[in] namespaceID 10Byte Namespace ID
+    * @param[in] instanceID  6Byte Instance ID
+    * @param[in] advPeriod   how long to advertise the URL for, measured in the number of adv frames.
+    *
+    */
+    void setDefaultUIDFrameData(UIDNamespaceID_t *namespaceID, UIDInstanceID_t *instanceID, uint32_t advPeriod = 10){
+        //Set UID frame
+        DBG("Setting default UID Data");
+        memcpy(defaultUidNamespaceID, namespaceID, UID_NAMESPACEID_SIZE);
+        memcpy(defaultUidInstanceID,  instanceID,  UID_INSTANCEID_SIZE);
+        defaultUidAdvPeriod = advPeriod;
+        uidIsSet = true; // flag to add this to eddystone service when config is done
+    }
 
     /* Start out by advertising the configService for a limited time after
      * startup; and switch to the normal non-connectible beacon functionality
@@ -259,10 +293,12 @@
     */
     void setupEddystoneAdvertisements() {
         DBG("Switching Config -> adv");
+        // Save params to storage
         extern void saveURIBeaconConfigParams(const Params_t *paramsP); /* forward declaration; necessary to avoid a circular dependency. */
         saveURIBeaconConfigParams(&params);
+        // Setup Eddystone Service 
         static EddystoneService eddyServ(ble,params.beaconPeriod,params.txPowerMode);
-        // Set configured frames
+        // Set configured frames (TLM,UID,URI...etc)
         if(params.tlmEnabled){
             eddyServ.setTLMFrameData(params.tlmVersion,params.tlmBeaconPeriod);
         }
@@ -291,14 +327,15 @@
             memcpy(params.lock, writeParams->data, sizeof(Lock_t));
             // Set the state to be locked by the lock code (note: zeros are a valid lock)
             params.lockedState = true;
+            INFO("Device Locked");
         } else if (handle == unlockChar.getValueHandle()) {
             // Validated earlier
             params.lockedState = false;
+            INFO("Device Unlocked");
         } else if (handle == uriDataChar.getValueHandle()) {
             params.uriDataLength = writeParams->len;
             memcpy(params.uriData, writeParams->data, params.uriDataLength);
-            memcpy(defaultUriData, writeParams->data, params.uriDataLength);
-            defaultUriDataLength = params.uriDataLength;
+            params.uriEnabled = true;
             INFO("URI = %s, URILen = %d", writeParams->data, writeParams->len);
         } else if (handle == flagsChar.getValueHandle()) {
             params.flags = *(writeParams->data);
@@ -311,6 +348,7 @@
             INFO("TxPowerModeChar = %d",params.txPowerMode);
         } else if (handle == beaconPeriodChar.getValueHandle()) {
             params.beaconPeriod = *((uint16_t *)(writeParams->data));
+            INFO("BeaconPeriod = %d",params.beaconPeriod);
 
             /* Re-map beaconPeriod to within permissible bounds if necessary. */
             if (params.beaconPeriod != 0) {
@@ -330,24 +368,39 @@
             resetToDefaults();
         }
         updateCharacteristicValues();
-        params.isConfigured = true;
+        params.isConfigured = true; // some configuration data has been passed, on disconnect switch to advertising mode.
     }
 
     /*
      * Reset the default values.
      */
     void resetToDefaults(void) {
+        INFO("Resetting to defaults");
+        // General
         params.lockedState      = false;
         memset(params.lock, 0, sizeof(Lock_t));
-        memcpy(params.uriData, defaultUriData, URI_DATA_MAX);
-        params.uriDataLength    = defaultUriDataLength;
         params.flags            = 0x10;
         memcpy(params.advPowerLevels, defaultAdvPowerLevels, sizeof(PowerLevels_t));
         params.txPowerMode      = TX_POWER_MODE_LOW;
         params.beaconPeriod     = 1000;
+        
+        // TLM Frame
+        params.tlmVersion = defaultTlmVersion;
+        params.tlmBeaconPeriod = defaultTlmAdvPeriod;
+        params.tlmEnabled = tlmIsSet;
+        
+        // URL Frame
+        memcpy(params.uriData, defaultUriData, URI_DATA_MAX);
+        params.uriDataLength    = defaultUriDataLength;
+        params.uriBeaconPeriod  = defaultUriAdvPeriod;
+        params.uriEnabled = urlIsSet;
+        
+        // UID Frame
         memcpy(params.uidNamespaceID, defaultUidNamespaceID, UID_NAMESPACEID_SIZE);
         memcpy(params.uidInstanceID,  defaultUidInstanceID,  UID_INSTANCEID_SIZE);
-        params.tlmVersion = TlmVersion;
+        params.uidBeaconPeriod = defaultUidAdvPeriod;
+        params.uidEnabled = uidIsSet;
+        
         updateCharacteristicValues();
     }
 
@@ -436,28 +489,35 @@
     Ticker              timeSinceBootTick;
     Timeout             switchFrame;
 // Default value that is restored on reset
-    uint8_t             defaultUriDataLength;
-    UriData_t           defaultUriData;
-    UIDNamespaceID_t    defaultUidNamespaceID;
-    UIDInstanceID_t     defaultUidInstanceID;
-    int8_t              defaultUidPower;
-    int8_t              defaultUrlPower;
-    uint16_t            uidRFU;
-    bool                uidIsSet;
-    bool                urlIsSet;
-// Default value that is restored on reset
     PowerLevels_t       &defaultAdvPowerLevels;
     uint8_t             lockedState;
     bool                initSucceeded;
     uint8_t             resetFlag;
     bool                switchFlag;
 
-// Private Variables for Telemetry Data
-    uint8_t                      TlmVersion;
-    volatile uint16_t            TlmBatteryVoltage;
-    volatile uint16_t            TlmBeaconTemp;
-    volatile uint32_t            TlmPduCount;
-    volatile uint32_t            TlmTimeSinceBoot;
+//UID Default value that is restored on reset
+    UIDNamespaceID_t    defaultUidNamespaceID;
+    UIDInstanceID_t     defaultUidInstanceID;
+    uint32_t            defaultUidAdvPeriod;
+    int8_t              defaultUidPower;
+    uint16_t            uidRFU;
+    bool                uidIsSet;
+
+//URI  Default value that is restored on reset
+    uint8_t             defaultUriDataLength;
+    UriData_t           defaultUriData;
+    int8_t              defaultUrlPower;
+    uint32_t            defaultUriAdvPeriod;
+    bool                urlIsSet;
+
+//TLM  Default value that is restored on reset
+    uint8_t             defaultTlmVersion;
+    uint32_t            defaultTlmAdvPeriod;
+    volatile uint16_t   TlmBatteryVoltage;
+    volatile uint16_t   TlmBeaconTemp;
+    volatile uint32_t   TlmPduCount;
+    volatile uint32_t   TlmTimeSinceBoot;
+    bool                tlmIsSet;
 
     ReadOnlyGattCharacteristic<uint8_t>        lockedStateChar;
     WriteOnlyGattCharacteristic<Lock_t>        lockChar;