Skip to content

Commit 9363820

Browse files
esp32: Add API to add extra GATT service and support for nimble ext_adv (#38067)
* esp32: Add API to add extra GATT service and support for nimble ext_adv * Restyled by clang-format * Restyled by gn * Add documents and replace all ErrorStr(err) with err.Format() * Restyled by prettier-markdown --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent a35ca78 commit 9363820

File tree

8 files changed

+541
-204
lines changed

8 files changed

+541
-204
lines changed

docs/platforms/esp32/ble_settings.md

+134
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,137 @@ advertising packets.
3333
```
3434

3535
Note: Scan response should be configure before `InitServer`.
36+
37+
## Nimble: additional custom GATT services
38+
39+
The `ConfigureExtraServices` API is used to configure additional services
40+
alongside the Matter services. This API allows users to add their own custom
41+
services for provisioning or other purposes.
42+
43+
### Usage
44+
45+
```
46+
/* Service access callback */
47+
static int gatt_svc_access(uint16_t conn_handle, uint16_t attr_handle,
48+
struct ble_gatt_access_ctxt *ctxt, void *arg);
49+
50+
/* Service UUID */
51+
static const ble_uuid128_t gatt_svr_svc_uuid =
52+
BLE_UUID128_INIT(0x2d, 0x71, 0xa2, 0x59, 0xb4, 0x58, 0xc8, 0x12,
53+
0x99, 0x99, 0x43, 0x95, 0x12, 0x2f, 0x46, 0x59);
54+
55+
/* A characteristic that can be subscribed to */
56+
static uint16_t gatt_svr_chr_val_handle;
57+
static const ble_uuid128_t gatt_svr_chr_uuid =
58+
BLE_UUID128_INIT(0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,
59+
0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33);
60+
61+
/* A custom descriptor */
62+
static const ble_uuid128_t gatt_svr_dsc_uuid =
63+
BLE_UUID128_INIT(0x01, 0x01, 0x01, 0x01, 0x12, 0x12, 0x12, 0x12,
64+
0x23, 0x23, 0x23, 0x23, 0x34, 0x34, 0x34, 0x34);
65+
66+
{
67+
std::vector<struct ble_gatt_svc_def> gatt_svr_svcs = {
68+
{
69+
/*** Service ***/
70+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
71+
.uuid = &gatt_svr_svc_uuid.u,
72+
.characteristics = (struct ble_gatt_chr_def[])
73+
{ {
74+
/*** This characteristic can be subscribed to by writing 0x00 and 0x01 to the CCCD ***/
75+
.uuid = &gatt_svr_chr_uuid.u,
76+
.access_cb = gatt_svc_access,
77+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY | BLE_GATT_CHR_F_INDICATE,
78+
.val_handle = &gatt_svr_chr_val_handle,
79+
.descriptors = (struct ble_gatt_dsc_def[]) {
80+
{
81+
.uuid = &gatt_svr_dsc_uuid.u,
82+
.att_flags = BLE_ATT_F_READ,
83+
.access_cb = gatt_svc_access,
84+
}, {
85+
0, /* No more descriptors in this characteristic */
86+
}
87+
},
88+
}, {
89+
0, /* No more characteristics in this service. */
90+
}
91+
},
92+
},
93+
};
94+
95+
/* Add Extra service after Matter service */
96+
CHIP_ERROR err = chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureExtraServices(gatt_svr_svcs, ture);
97+
}
98+
```
99+
100+
Note: Extra service should be configure before `InitServer`.
101+
102+
## Nimble: multiple BLE advertisement
103+
104+
The chips that support BLE 5.0 features can advertise their custom BLE GAP
105+
advertisement alongside Matter's BLE advertisement if `CONFIG_BT_NIMBLE_EXT_ADV`
106+
is enabled and `CONFIG_BT_NIMBLE_MAX_EXT_ADV_INSTANCES` is a value greater
107+
than 1.
108+
109+
### Usage
110+
111+
```
112+
static uint8_t connectable_adv_pattern[] = {
113+
0x02, 0x01, 0x06,
114+
0x03, 0x03, 0xab, 0xcd,
115+
0x03, 0x03, 0x18, 0x11,
116+
0x12, 0X09, 'n', 'i', 'm', 'b', 'l', 'e', '-', 'c', 'o', 'n', 'n', 'e', 't', 'a', 'b', 'l', 'e'
117+
};
118+
119+
/* GAP event handler */
120+
static int ble_multi_adv_gap_event(struct ble_gap_event *event, void *arg);
121+
122+
{
123+
/* Use instance except 0 as Matter advertisement uses instance 0 */
124+
uint8_t instance = 1;
125+
struct ble_gap_ext_adv_params params;
126+
int size_pattern = sizeof(connectable_adv_pattern) / sizeof(connectable_adv_pattern[0]);
127+
128+
memset (&params, 0, sizeof(params));
129+
130+
params.connectable = 1;
131+
params.scannable = 1;
132+
params.own_addr_type = BLE_OWN_ADDR_RANDOM;
133+
params.sid = 1;
134+
params.primary_phy = BLE_HCI_LE_PHY_1M;
135+
params.secondary_phy = BLE_HCI_LE_PHY_1M;
136+
params.tx_power = 127;
137+
138+
int rc;
139+
struct os_mbuf *data;
140+
int size_pattern = sizeof(legacy_dur_adv_pattern) / sizeof(legacy_dur_adv_pattern[0]);
141+
142+
if (ble_gap_ext_adv_active(instance)) {
143+
ESP_LOGI(tag, "Instance already advertising");
144+
return;
145+
}
146+
147+
rc = ble_gap_ext_adv_configure(instance, params, NULL,
148+
ble_multi_adv_gap_event, NULL);
149+
assert (rc == 0);
150+
151+
/* get mbuf for adv data */
152+
data = os_msys_get_pkthdr(size_pattern, 0);
153+
assert(data);
154+
155+
/* fill mbuf with adv data */
156+
rc = os_mbuf_append(data, legacy_dur_adv_pattern, size_pattern);
157+
assert(rc == 0);
158+
159+
rc = ble_gap_ext_adv_set_data(instance, data);
160+
assert (rc == 0);
161+
162+
/* start advertising */
163+
rc = ble_gap_ext_adv_start(instance, 500, 0);
164+
assert (rc == 0);
165+
}
166+
```
167+
168+
Note: The custom additional advertisement should be configured after BLE stack
169+
is started.

src/platform/BUILD.gn

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
149149
chip_device_platform == "tizen" || chip_device_platform == "android" ||
150150
chip_device_platform == "webos" || chip_device_platform == "bl602" ||
151151
chip_device_platform == "bl702" || chip_device_platform == "bl702l" ||
152-
chip_device_platform == "bl616") {
152+
chip_device_platform == "bl616" || chip_device_platform == "esp32") {
153153
defines += [ "CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE=${chip_enable_ble}" ]
154154
}
155155

src/platform/ESP32/BLEManagerImpl.h

+15-12
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
*/
2525

2626
#pragma once
27-
#include <string>
28-
29-
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
30-
31-
#include "sdkconfig.h"
27+
#include <sdkconfig.h>
3228

3329
#include <lib/core/Optional.h>
3430

@@ -117,19 +113,18 @@ struct BLEScanConfig
117113
};
118114

119115
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
116+
120117
/**
121118
* Concrete implementation of the BLEManager singleton object for the ESP32 platform.
122119
*/
123120
class BLEManagerImpl final : public BLEManager,
124121
private Ble::BleLayer,
125122
private Ble::BlePlatformDelegate,
126123
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
127-
private Ble::BleApplicationDelegate,
128124
private Ble::BleConnectionDelegate,
129-
private ChipDeviceScannerDelegate
130-
#else
131-
private Ble::BleApplicationDelegate
125+
private ChipDeviceScannerDelegate,
132126
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
127+
private Ble::BleApplicationDelegate
133128
{
134129
public:
135130
BLEManagerImpl() {}
@@ -142,6 +137,9 @@ class BLEManagerImpl final : public BLEManager,
142137

143138
CHIP_ERROR ConfigureScanResponseData(ByteSpan data);
144139
void ClearScanResponseData(void);
140+
#ifdef CONFIG_BT_NIMBLE_ENABLED
141+
CHIP_ERROR ConfigureExtraServices(std::vector<struct ble_gatt_svc_def> & extGattSvcs, bool afterMatterSvc);
142+
#endif
145143

146144
private:
147145
chip::Optional<chip::ByteSpan> mScanResponse;
@@ -240,6 +238,12 @@ class BLEManagerImpl final : public BLEManager,
240238
BLEAdvConfig mBLEAdvConfig;
241239
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
242240
#ifdef CONFIG_BT_NIMBLE_ENABLED
241+
#ifdef CONFIG_BT_NIMBLE_EXT_ADV
242+
static constexpr size_t kMaxMatterAdvDataLen = 31;
243+
static constexpr uint8_t kMatterAdvInstance = 0;
244+
uint8_t mMatterAdvData[kMaxMatterAdvDataLen];
245+
uint16_t mMatterAdvDataLen = 0;
246+
#endif
243247
uint16_t mSubscribedConIds[kMaxConnections];
244248
#endif // CONFIG_BT_NIMBLE_ENABLED
245249

@@ -278,6 +282,7 @@ class BLEManagerImpl final : public BLEManager,
278282
esp_gatt_if_t mAppIf;
279283
#elif defined(CONFIG_BT_NIMBLE_ENABLED)
280284
uint16_t mNumGAPCons;
285+
std::vector<struct ble_gatt_svc_def> mGattSvcs;
281286
#endif // CONFIG_BT_BLUEDROID_ENABLED
282287
uint16_t mServiceAttrHandle;
283288
uint16_t mRXCharAttrHandle;
@@ -347,7 +352,7 @@ class BLEManagerImpl final : public BLEManager,
347352
static void bleprph_host_task(void * param);
348353
static void bleprph_on_sync(void);
349354
static void bleprph_on_reset(int);
350-
static const struct ble_gatt_svc_def CHIPoBLEGATTAttrs[];
355+
static const struct ble_gatt_svc_def CHIPoBLEGATTSvc;
351356
static int ble_svr_gap_event(struct ble_gap_event * event, void * arg);
352357

353358
static int gatt_svr_chr_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt * ctxt, void * arg);
@@ -423,5 +428,3 @@ inline bool BLEManagerImpl::_IsAdvertising(void)
423428
} // namespace Internal
424429
} // namespace DeviceLayer
425430
} // namespace chip
426-
427-
#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

src/platform/ESP32/BUILD.gn

+20-24
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,29 @@ static_library("ESP32") {
100100
]
101101
}
102102

103-
if (chip_enable_chipoble) {
104-
sources += [
105-
"BLEManagerImpl.h",
106-
"ChipDeviceScanner.h",
107-
]
108-
}
109-
110-
if (chip_enable_ble_controller) {
111-
sources += [ "ChipDeviceScanner.h" ]
112-
}
113-
114-
if (chip_bt_nimble_enabled) {
115-
sources += [ "nimble/BLEManagerImpl.cpp" ]
103+
if (chip_enable_ble) {
104+
sources += [ "BLEManagerImpl.h" ]
116105
if (chip_enable_ble_controller) {
117-
sources += [
118-
"nimble/ChipDeviceScanner.cpp",
119-
"nimble/blecent.h",
120-
"nimble/misc.c",
121-
"nimble/peer.c",
122-
]
106+
sources += [ "ChipDeviceScanner.h" ]
123107
}
124-
}
125108

126-
if (chip_bt_bluedroid_enabled) {
127-
sources += [ "bluedroid/BLEManagerImpl.cpp" ]
128-
if (chip_enable_ble_controller) {
129-
sources += [ "bluedroid/ChipDeviceScanner.cpp" ]
109+
if (chip_bt_nimble_enabled) {
110+
sources += [ "nimble/BLEManagerImpl.cpp" ]
111+
if (chip_enable_ble_controller) {
112+
sources += [
113+
"nimble/ChipDeviceScanner.cpp",
114+
"nimble/blecent.h",
115+
"nimble/misc.c",
116+
"nimble/peer.c",
117+
]
118+
}
119+
}
120+
121+
if (chip_bt_bluedroid_enabled) {
122+
sources += [ "bluedroid/BLEManagerImpl.cpp" ]
123+
if (chip_enable_ble_controller) {
124+
sources += [ "bluedroid/ChipDeviceScanner.cpp" ]
125+
}
130126
}
131127
}
132128

src/platform/ESP32/CHIPDevicePlatformConfig.h

-6
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,6 @@
9696
#define CHIP_DEVICE_CONFIG_ICD_FAST_POLL_INTERVAL chip::System::Clock::Milliseconds32(CONFIG_ICD_FAST_POLL_INTERVAL_MS)
9797
#endif // CHIP_CONFIG_ENABLE_ICD_SERVER
9898

99-
#ifdef CONFIG_ENABLE_CHIPOBLE
100-
#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE CONFIG_ENABLE_CHIPOBLE
101-
#else
102-
#define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE 0
103-
#endif // CONFIG_ENABLE_CHIPOBLE
104-
10599
#ifdef CONFIG_ENABLE_EXTENDED_DISCOVERY
106100
#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY CONFIG_ENABLE_EXTENDED_DISCOVERY
107101
#else

src/platform/ESP32/ConfigurationManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <platform/internal/GenericConnectivityManagerImpl_NoBLE.h>
3636
#endif
3737

38+
#include <freertos/timers.h>
3839
#include <platform/ESP32/ESP32Config.h>
3940

4041
namespace chip {

src/platform/ESP32/bluedroid/BLEManagerImpl.cpp

+1-10
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,12 @@
2323
* for the ESP32 platform.
2424
*/
2525
/* this file behaves like a config.h, comes first */
26-
#include <platform/internal/CHIPDeviceLayerInternal.h>
27-
28-
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
29-
3026
#include "sdkconfig.h"
27+
#include <platform/internal/CHIPDeviceLayerInternal.h>
3128
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
3229
#include <lib/support/CodeUtils.h>
3330
#endif
3431

35-
#ifdef CONFIG_BT_BLUEDROID_ENABLED
36-
3732
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
3833
#include <ble/Ble.h>
3934
#endif
@@ -2158,7 +2153,3 @@ void BLEManagerImpl::DriveBLEState(intptr_t arg)
21582153
} // namespace Internal
21592154
} // namespace DeviceLayer
21602155
} // namespace chip
2161-
2162-
#endif // CONFIG_BT_BLUEDROID_ENABLED
2163-
2164-
#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

0 commit comments

Comments
 (0)