High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Revision:
1192:718787de23b7
Parent:
1191:c8635748feec
Child:
1193:c8bbf3a6712d
--- a/ble/Gap.h	Wed Apr 06 19:15:45 2016 +0100
+++ b/ble/Gap.h	Wed Apr 06 19:15:46 2016 +0100
@@ -848,6 +848,7 @@
         return INIT_POLICY_IGNORE_WHITELIST;
     }
 
+
 protected:
     /* Override the following in the underlying adaptation layer to provide the functionality of scanning. */
 
@@ -939,6 +940,7 @@
      */
     ble_error_t startAdvertising(void) {
         ble_error_t rc;
+        setAdvertisingData(); /* Update the underlying stack. */
         if ((rc = startAdvertising(_advParams)) == BLE_ERROR_NONE) {
             state.advertising = 1;
         }
@@ -952,7 +954,7 @@
      */
     void clearAdvertisingPayload(void) {
         _advPayload.clear();
-        setAdvertisingData(_advPayload, _scanResponse);
+        setAdvertisingData();
     }
 
     /**
@@ -970,18 +972,12 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
-        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = advPayloadCopy.addFlags(flags)) != BLE_ERROR_NONE) {
+        if ((rc = _advPayload.addFlags(flags)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = advPayloadCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -997,18 +993,14 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
-        GapAdvertisingData advPayloadCopy = _advPayload;
+        setAppearance(app);
+
         ble_error_t rc;
-        if ((rc = advPayloadCopy.addAppearance(app)) != BLE_ERROR_NONE) {
+        if ((rc = _advPayload.addAppearance(app)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = advPayloadCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -1024,18 +1016,12 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
-        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = advPayloadCopy.addTxPower(power)) != BLE_ERROR_NONE) {
+        if ((rc = _advPayload.addTxPower(power)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = advPayloadCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -1062,18 +1048,16 @@
      *       payload.
      */
     ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
-        GapAdvertisingData advPayloadCopy = _advPayload;
+        if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
+            setDeviceName(data);
+        }
+
         ble_error_t rc;
-        if ((rc = advPayloadCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = _advPayload.addData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = advPayloadCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -1093,18 +1077,16 @@
      *         matching AD type; otherwise, an appropriate error.
      */
     ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
-        GapAdvertisingData advPayloadCopy = _advPayload;
+        if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
+            setDeviceName(data);
+        }
+
         ble_error_t rc;
-        if ((rc = advPayloadCopy.updateData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = _advPayload.updateData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = advPayloadCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -1121,12 +1103,8 @@
      *         set.
      */
     ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
-        ble_error_t rc = setAdvertisingData(payload, _scanResponse);
-        if (rc == BLE_ERROR_NONE) {
-            _advPayload = payload;
-        }
-
-        return rc;
+        _advPayload = payload;
+        return setAdvertisingData();
     }
 
     /**
@@ -1155,18 +1133,12 @@
      *         response payload.
      */
     ble_error_t accumulateScanResponse(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
-        GapAdvertisingData scanResponseCopy = _scanResponse;
         ble_error_t rc;
-        if ((rc = scanResponseCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = _scanResponse.addData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        rc = setAdvertisingData(_advPayload, scanResponseCopy);
-        if (rc == BLE_ERROR_NONE) {
-            _scanResponse = scanResponseCopy;
-        }
-
-        return rc;
+        return setAdvertisingData();
     }
 
     /**
@@ -1178,7 +1150,7 @@
      */
     void clearScanResponse(void) {
         _scanResponse.clear();
-        setAdvertisingData(_advPayload, _scanResponse);
+        setAdvertisingData();
     }
 
     /**
@@ -1409,6 +1381,16 @@
 
 private:
     /**
+     * Helper function used to set the advertising data in the underlying BLE stack.
+     *
+     * @return BLE_ERROR_NONE if the advertising data was successfully set.
+     */
+    ble_error_t setAdvertisingData(void) {
+        return setAdvertisingData(_advPayload, _scanResponse);
+    }
+
+private:
+    /**
      * Functionality that is BLE stack-dependent and must be implemented by the
      * ported. This is a helper function to set the advertising data in the
      * BLE stack.
@@ -1653,7 +1635,7 @@
      *              invoked in response to a shutdown event.
      */
     template <typename T>
-    void onShutdown(T *objPtr, void (T::*memberPtr)(void)) {
+    void onShutdown(T *objPtr, void (T::*memberPtr)(const Gap *)) {
         shutdownCallChain.add(objPtr, memberPtr);
     }