Skip to content

Commit 1063170

Browse files
jadhavrohit924restyled-commits
authored andcommitted
[ESP32]: Add way to configure BLE scan response. (#28571)
* [ESP32]: Add way to configure scan response. * Restyled by prettier-markdown * Address review comments --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 19a288d commit 1063170

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

docs/guides/esp32/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ example on ESP32 series of SoCs
1717
- [RPC Console and Device Tracing](rpc_console.md)
1818
- [Matter OTA](ota.md)
1919
- [Generating and Using ESP Secure Cert Partition](secure_cert_partition.md)
20+
- [BLE Settings](ble_settings.md)

docs/guides/esp32/ble_settings.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Bluetooth Low Energy (BLE)
2+
3+
## Nimble: scan response
4+
5+
The `ConfigureScanResponseData` API is used to configure the scan response data
6+
for advertising in a Bluetooth Low Energy (BLE) application based on the NimBLE
7+
BLE stack. Scan response data is additional data that a BLE peripheral device
8+
can include in its advertising packets to provide more information about itself.
9+
This API allows you to set the scan response data that will be included in the
10+
advertising packets.
11+
12+
### Usage
13+
14+
```
15+
{
16+
uint8_t scanResponse[31]; // 0x05, 0x09, a, b, c, d
17+
scanResponse[0] = 0x05;
18+
scanResponse[1] = 0x09;
19+
scanResponse[2] = 0x61;
20+
scanResponse[3] = 0x62;
21+
scanResponse[4] = 0x63;
22+
scanResponse[5] = 0x64;
23+
chip::ByteSpan data(scanResponse);
24+
CHIP_ERROR err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureScanResponseData(data);
25+
if (err != CHIP_NO_ERROR)
26+
{
27+
ESP_LOGE(TAG, "Failed to configure scan response, err:%" CHIP_ERROR_FORMAT, err.Format());
28+
}
29+
}
30+
```
31+
32+
Note: Scan response should be configure before `InitServer`.

src/platform/ESP32/BLEManagerImpl.h

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ struct ble_gatt_char_context
7171
#include <platform/ESP32/ChipDeviceScanner.h>
7272
#endif
7373

74+
#define MAX_SCAN_RSP_DATA_LEN 31
75+
7476
namespace chip {
7577
namespace DeviceLayer {
7678
namespace Internal {
@@ -132,6 +134,7 @@ class BLEManagerImpl final : public BLEManager,
132134
#endif
133135
{
134136
public:
137+
uint8_t scanResponseBuffer[MAX_SCAN_RSP_DATA_LEN];
135138
BLEManagerImpl() {}
136139
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
137140
CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
@@ -140,7 +143,12 @@ class BLEManagerImpl final : public BLEManager,
140143
#endif
141144
#endif
142145

146+
CHIP_ERROR ConfigureScanResponseData(ByteSpan data);
147+
void ClearScanResponseData(void);
148+
143149
private:
150+
chip::Optional<chip::ByteSpan> mScanResponse;
151+
144152
// Allow the BLEManager interface class to delegate method calls to
145153
// the implementation methods provided by this class.
146154
friend BLEManager;

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,25 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
10121012
return err;
10131013
}
10141014

1015+
CHIP_ERROR BLEManagerImpl::ConfigureScanResponseData(ByteSpan data)
1016+
{
1017+
if (!IsSpanUsable(data) || data.size() > MAX_SCAN_RSP_DATA_LEN)
1018+
{
1019+
ChipLogError(DeviceLayer, "scan response data is invalid");
1020+
return CHIP_ERROR_INVALID_ARGUMENT;
1021+
}
1022+
memcpy(scanResponseBuffer, data.data(), data.size());
1023+
ByteSpan scanResponseSpan(scanResponseBuffer);
1024+
mScanResponse = chip::Optional(scanResponseSpan);
1025+
return CHIP_NO_ERROR;
1026+
}
1027+
1028+
void BLEManagerImpl::ClearScanResponseData(void)
1029+
{
1030+
mScanResponse.ClearValue();
1031+
ChipLogDetail(DeviceLayer, "scan response data is cleared");
1032+
}
1033+
10151034
void BLEManagerImpl::HandleRXCharWrite(struct ble_gatt_char_context * param)
10161035
{
10171036
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -1584,6 +1603,15 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
15841603
}
15851604
}
15861605
#endif
1606+
if (mScanResponse.HasValue())
1607+
{
1608+
err = MapBLEError(ble_gap_adv_rsp_set_data(mScanResponse.Value().data(), mScanResponse.Value().size()));
1609+
if (err != CHIP_NO_ERROR)
1610+
{
1611+
ChipLogError(DeviceLayer, "ble_gap_adv_rsp_set_data failed: %s", ErrorStr(err));
1612+
return err;
1613+
}
1614+
}
15871615
err = MapBLEError(ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_svr_gap_event, NULL));
15881616
if (err == CHIP_NO_ERROR)
15891617
{

0 commit comments

Comments
 (0)