High level Bluetooth Low Energy API and radio abstraction layer

Dependencies:   nRF51822

Dependents:   LinkNode_LIS3DH

Fork of BLE_API by Bluetooth Low Energy

Revision:
1188:039621a96054
Parent:
1187:231e4007429a
Child:
1189:740911150041
--- a/ble/Gap.h	Wed Apr 06 19:15:41 2016 +0100
+++ b/ble/Gap.h	Wed Apr 06 19:15:42 2016 +0100
@@ -948,7 +948,7 @@
      */
     void clearAdvertisingPayload(void) {
         _advPayload.clear();
-        setAdvertisingData();
+        setAdvertisingData(_advPayload, _scanResponse);
     }
 
     /**
@@ -966,12 +966,18 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
+        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = _advPayload.addFlags(flags)) != BLE_ERROR_NONE) {
+        if ((rc = advPayloadCopy.addFlags(flags)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
+        if (rc == BLE_ERROR_NONE) {
+            _advPayload = advPayloadCopy;
+        }
+
+        return rc;
     }
 
     /**
@@ -987,14 +993,18 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
-        setAppearance(app);
-
+        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = _advPayload.addAppearance(app)) != BLE_ERROR_NONE) {
+        if ((rc = advPayloadCopy.addAppearance(app)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
+        if (rc == BLE_ERROR_NONE) {
+            _advPayload = advPayloadCopy;
+        }
+
+        return rc;
     }
 
     /**
@@ -1010,12 +1020,16 @@
      *         advertising payload.
      */
     ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
+        if (power < -100 || power > 20) {
+            return BLE_ERROR_PARAM_OUT_OF_RANGE;
+        }
+
         ble_error_t rc;
         if ((rc = _advPayload.addTxPower(power)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        return setAdvertisingData(_advPayload, _scanResponse);
     }
 
     /**
@@ -1042,16 +1056,18 @@
      *       payload.
      */
     ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
-        if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
-            setDeviceName(data);
-        }
-
+        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = _advPayload.addData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = advPayloadCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
+        if (rc == BLE_ERROR_NONE) {
+            _advPayload = advPayloadCopy;
+        }
+
+        return rc;
     }
 
     /**
@@ -1071,16 +1087,18 @@
      *         matching AD type; otherwise, an appropriate error.
      */
     ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
-        if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
-            setDeviceName(data);
-        }
-
+        GapAdvertisingData advPayloadCopy = _advPayload;
         ble_error_t rc;
-        if ((rc = _advPayload.updateData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = advPayloadCopy.updateData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        rc = setAdvertisingData(advPayloadCopy, _scanResponse);
+        if (rc == BLE_ERROR_NONE) {
+            _advPayload = advPayloadCopy;
+        }
+
+        return rc;
     }
 
     /**
@@ -1097,8 +1115,13 @@
      *         set.
      */
     ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
+        ble_error_t rc = setAdvertisingData(_advPayload, _scanResponse);
+        if (rc != BLE_ERROR_NONE) {
+            /* The payload has a problem, do not store it */
+            return rc;
+        }
         _advPayload = payload;
-        return setAdvertisingData();
+        return BLE_ERROR_NONE;
     }
 
     /**
@@ -1127,12 +1150,18 @@
      *         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 = _scanResponse.addData(type, data, len)) != BLE_ERROR_NONE) {
+        if ((rc = scanResponseCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
             return rc;
         }
 
-        return setAdvertisingData();
+        rc = setAdvertisingData(_advPayload, scanResponseCopy);
+        if (rc == BLE_ERROR_NONE) {
+            _scanResponse = scanResponseCopy;
+        }
+
+        return rc;
     }
 
     /**
@@ -1144,7 +1173,7 @@
      */
     void clearScanResponse(void) {
         _scanResponse.clear();
-        setAdvertisingData();
+        setAdvertisingData(_advPayload, _scanResponse);
     }
 
     /**
@@ -1375,16 +1404,6 @@
 
 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.