fork BLE_API to add update adv payload API

Fork of BLE_API by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
sunsmile2015
Date:
Fri Jul 31 09:09:50 2015 +0000
Parent:
757:4cd3b18607ec
Commit message:
add adv packet update API

Changed in this revision

ble/Gap.h Show annotated file Show diff for this revision Revisions of this file
ble/GapAdvertisingData.h Show annotated file Show diff for this revision Revisions of this file
--- a/ble/Gap.h	Tue Jul 21 13:25:40 2015 +0100
+++ b/ble/Gap.h	Fri Jul 31 09:09:50 2015 +0000
@@ -586,6 +586,27 @@
     }
 
     /**
+     * Update the advertising payload filed which has the same adv type as the input
+     * parameter. Total length of the new data must be the same as the old one.
+     *
+     * @param  type The type which describes the variable length data.
+     * @param  data data bytes.
+     * @param  len  length of data.
+     */
+    ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
+        if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
+            setDeviceName(data);
+        }
+
+        ble_error_t rc;
+        if ((rc = _advPayload.updateData(type, data, len)) != BLE_ERROR_NONE) {
+            return rc;
+        }
+
+        return setAdvertisingData();
+    }
+
+    /**
      * Setup a particular, user-constructed advertisement payload for the
      * underlying stack. It would be uncommon for this API to be used directly;
      * there are other APIs to build an advertisement payload (see above).
--- a/ble/GapAdvertisingData.h	Tue Jul 21 13:25:40 2015 +0100
+++ b/ble/GapAdvertisingData.h	Fri Jul 31 09:09:50 2015 +0000
@@ -234,6 +234,33 @@
     }
 
     /**
+     * update one advertising data field with the same AD type (see DataType)
+     *
+     * @param  advDataType The Advertising 'DataType' to add
+     * @param  payload     Pointer to the payload contents
+     * @param  len         Size of the payload in bytes
+     *
+     * @return BLE_ERROR_UNSPECIFIED if the specified field is not found, else
+     * BLE_ERROR_NONE.
+     */
+    ble_error_t updateData(DataType advDataType, const uint8_t *payload, uint8_t len)
+     {
+        uint8_t byteIndex = 0;
+        
+        while (byteIndex < _payloadLen) {
+            if (_payload[byteIndex + 1] == advDataType) { /* Check adv type */
+                if (_payload[byteIndex] == (len + 1)) { /* Check adv field length */
+                    memcpy(&_payload[byteIndex + 2], payload, len);
+                }
+                return BLE_ERROR_NONE;
+            }
+            byteIndex += (_payload[byteIndex] + 1);
+        }
+
+        return BLE_ERROR_UNSPECIFIED;
+    }
+
+    /**
      * Helper function to add APPEARANCE data to the advertising payload
      *
      * @param  appearance