From 186faa502dc34e384672f54608e1bcb9265ad72b Mon Sep 17 00:00:00 2001 From: Rohit Jadhav Date: Tue, 8 Aug 2023 19:34:28 +0530 Subject: [PATCH] Address review comments --- docs/guides/esp32/README.md | 2 +- docs/guides/esp32/{ble.md => ble_settings.md} | 17 +++++++------ src/platform/ESP32/BLEManagerImpl.h | 8 +++++-- src/platform/ESP32/nimble/BLEManagerImpl.cpp | 24 +++++++++++++------ 4 files changed, 34 insertions(+), 17 deletions(-) rename docs/guides/esp32/{ble.md => ble_settings.md} (59%) diff --git a/docs/guides/esp32/README.md b/docs/guides/esp32/README.md index 711f8348c9e2b4..e487a58f318591 100644 --- a/docs/guides/esp32/README.md +++ b/docs/guides/esp32/README.md @@ -17,4 +17,4 @@ example on ESP32 series of SoCs - [RPC Console and Device Tracing](rpc_console.md) - [Matter OTA](ota.md) - [Generating and Using ESP Secure Cert Partition](secure_cert_partition.md) -- [BLE](ble.md) +- [BLE Settings](ble_settings.md) diff --git a/docs/guides/esp32/ble.md b/docs/guides/esp32/ble_settings.md similarity index 59% rename from docs/guides/esp32/ble.md rename to docs/guides/esp32/ble_settings.md index dee583984b0ed5..36178ebece5d71 100644 --- a/docs/guides/esp32/ble.md +++ b/docs/guides/esp32/ble_settings.md @@ -1,6 +1,6 @@ # Bluetooth Low Energy (BLE) -# Nimble: scan response +## Nimble: scan response The `ConfigureScanResponseData` API is used to configure the scan response data for advertising in a Bluetooth Low Energy (BLE) application based on the NimBLE @@ -9,21 +9,24 @@ can include in its advertising packets to provide more information about itself. This API allows you to set the scan response data that will be included in the advertising packets. -## Usage +### Usage ``` -uint8_t scanResponse[31]; // 0x05, 0x09, a, b, c, d - { + uint8_t scanResponse[31]; // 0x05, 0x09, a, b, c, d scanResponse[0] = 0x05; scanResponse[1] = 0x09; scanResponse[2] = 0x61; scanResponse[3] = 0x62; scanResponse[4] = 0x63; scanResponse[5] = 0x64; - chip::ByteSpan data(); - chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureScanResponseData(data); + chip::ByteSpan data(scanResponse); + CHIP_ERROR err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureScanResponseData(data); + if (err != CHIP_NO_ERROR) + { + ESP_LOGE(TAG, "Failed to configure scan response, err:%" CHIP_ERROR_FORMAT, err.Format()); + } } ``` -Note: ScanResponse should be configure before device starts advertisement. +Note: Scan response should be configure before `InitServer`. diff --git a/src/platform/ESP32/BLEManagerImpl.h b/src/platform/ESP32/BLEManagerImpl.h index 68aa4bbbec0f0a..5cb1421750786a 100644 --- a/src/platform/ESP32/BLEManagerImpl.h +++ b/src/platform/ESP32/BLEManagerImpl.h @@ -71,6 +71,8 @@ struct ble_gatt_char_context #include #endif +#define MAX_SCAN_RSP_DATA_LEN 31 + namespace chip { namespace DeviceLayer { namespace Internal { @@ -132,6 +134,7 @@ class BLEManagerImpl final : public BLEManager, #endif { public: + uint8_t scanResponseBuffer[MAX_SCAN_RSP_DATA_LEN]; BLEManagerImpl() {} #if CONFIG_ENABLE_ESP32_BLE_CONTROLLER CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral); @@ -140,10 +143,11 @@ class BLEManagerImpl final : public BLEManager, #endif #endif - void ConfigureScanResponseData(ByteSpan data); + CHIP_ERROR ConfigureScanResponseData(ByteSpan data); + void ClearScanResponseData(void); private: - chip::Optional scanResponse; + chip::Optional mScanResponse; // Allow the BLEManager interface class to delegate method calls to // the implementation methods provided by this class. diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index c09f26197ff0dd..016b8c57047100 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -1012,14 +1012,24 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) return err; } -void BLEManagerImpl::ConfigureScanResponseData(ByteSpan data) +CHIP_ERROR BLEManagerImpl::ConfigureScanResponseData(ByteSpan data) { - if (data.size() > MAX_ADV_DATA_LEN) + if (!IsSpanUsable(data) || data.size() > MAX_SCAN_RSP_DATA_LEN) { - ChipLogError(DeviceLayer, "scan response data is out of length"); - return; + ChipLogError(DeviceLayer, "scan response data is invalid"); + return CHIP_ERROR_INVALID_ARGUMENT; } - scanResponse = chip::Optional(data); + memcpy(scanResponseBuffer, data.data(), data.size()); + ByteSpan scanResponseSpan(scanResponseBuffer); + mScanResponse = chip::Optional(scanResponseSpan); + return CHIP_NO_ERROR; +} + +void BLEManagerImpl::ClearScanResponseData(void) +{ + ByteSpan scanResponseSpan; + mScanResponse = chip::Optional(scanResponseSpan); + ChipLogDetail(DeviceLayer, "scan response data is cleared"); } void BLEManagerImpl::HandleRXCharWrite(struct ble_gatt_char_context * param) @@ -1594,9 +1604,9 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) } } #endif - if (scanResponse.HasValue()) + if (mScanResponse.HasValue()) { - err = MapBLEError(ble_gap_adv_rsp_set_data(scanResponse.Value().data(), scanResponse.Value().size())); + err = MapBLEError(ble_gap_adv_rsp_set_data(mScanResponse.Value().data(), mScanResponse.Value().size())); if (err != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "ble_gap_adv_rsp_set_data failed: %s", ErrorStr(err));