Skip to content

Commit

Permalink
[ESP32]: Add way to configure scan response.
Browse files Browse the repository at this point in the history
  • Loading branch information
jadhavrohit924 committed Aug 8, 2023
1 parent 367a0c6 commit 501c690
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/guides/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +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)
21 changes: 21 additions & 0 deletions docs/guides/esp32/ble.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Bluetooth Low Energy (BLE)

# Nimble: scan response

The `BLEManagerImpl::ConfigureScanResponseData` API is used to configure the
scan response data for advertising in a Bluetooth Low Energy (BLE) application
based on the NimBLE BLE stack. Scan response data is additional data that a BLE
peripheral device 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

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); }

Note: ScanResponse should be configure before device starts advertisement.
4 changes: 4 additions & 0 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class BLEManagerImpl final : public BLEManager,
#endif
#endif

void ConfigureScanResponseData(ByteSpan data);

private:
chip::Optional<chip::ByteSpan> scanResponse;

// Allow the BLEManager interface class to delegate method calls to
// the implementation methods provided by this class.
friend BLEManager;
Expand Down
19 changes: 19 additions & 0 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
return err;
}

void BLEManagerImpl::ConfigureScanResponseData(ByteSpan data)
{
if (data.size() > MAX_ADV_DATA_LEN)
{
ChipLogError(DeviceLayer, "scan response data is out of length");
return;
}
scanResponse = chip::Optional(data);
}

void BLEManagerImpl::HandleRXCharWrite(struct ble_gatt_char_context * param)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -1584,6 +1594,15 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
}
}
#endif
if (scanResponse.HasValue())
{
err = MapBLEError(ble_gap_adv_rsp_set_data(scanResponse.Value().data(), scanResponse.Value().size()));
if (err != CHIP_NO_ERROR)
{
ChipLogError(DeviceLayer, "ble_gap_adv_rsp_set_data failed: %s", ErrorStr(err));
return err;
}
}
err = MapBLEError(ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, &adv_params, ble_svr_gap_event, NULL));
if (err == CHIP_NO_ERROR)
{
Expand Down

0 comments on commit 501c690

Please sign in to comment.