diff --git a/ble/Gap.h b/ble/Gap.h index ea76ef0..691aac1 100644 --- a/ble/Gap.h +++ b/ble/Gap.h @@ -585,6 +585,27 @@ public: return setAdvertisingData(); } + /** + * 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; diff --git a/ble/GapAdvertisingData.h b/ble/GapAdvertisingData.h index ddd9908..1829446 100644 --- a/ble/GapAdvertisingData.h +++ b/ble/GapAdvertisingData.h @@ -233,6 +233,33 @@ public: return BLE_ERROR_NONE; } + /** + * 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 *