Skip to content

Commit 7cdf4e8

Browse files
shubhamdpsayondeeprestyled-commits
authored
[v1.3] [ESP32] Cherry-picking extended advertisement support and ble deinit crash fix (#33159)
* [ESP32] Enable extended ble announcement for esp32 platform (#32389) * enable extended ble advertisement for esp32 platform * changed extended advertisement to extended announcement * restlyed * made discoverty timeout range and default dependent on extended announcement. * help section for extended ble announcement * fixed eliding of extended data during extended announcement. * fixed setting of Additional data flag * [ESP32] Implement BLE Manager Shutdown for nimble host (#33109) * [ESP32] Implement BLE Manager Shutdown for nimble host - Replace ble deinit imple in Esp32AppServer with BLEMgr().Shutdown() - Replace few ESP_LOG with ChipLog in Esp32AppServer - Move ble deinit kCommissioningComplete switch case - Make USE_BLE_ONLY_FOR_COMMISSIONING depends on BT_ENABLED * Restyled by clang-format * address reviews * Add checks for timer handler --------- Co-authored-by: Restyled.io <commits@restyled.io> --------- Co-authored-by: SAYON DEEP <sayondeep0710@rediffmail.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 5b0afc7 commit 7cdf4e8

File tree

6 files changed

+199
-73
lines changed

6 files changed

+199
-73
lines changed

config/esp32/components/chip/Kconfig

+21-8
Original file line numberDiff line numberDiff line change
@@ -726,12 +726,13 @@ menu "CHIP Device Layer"
726726
should not start advertising automatically after power-up.
727727

728728
config USE_BLE_ONLY_FOR_COMMISSIONING
729-
bool "Use BLE only for commissioning"
730-
default y
731-
help
732-
Disable this flag if BLE is used for any other purpose than commissioning.
733-
When enabled, it deinitialized the BLE on successful commissioning, and on
734-
bootup do not initialize the BLE if device is already provisioned with Wi-Fi/Thread credentials.
729+
depends on BT_ENABLED
730+
bool "Use BLE only for commissioning"
731+
default y
732+
help
733+
Disable this flag if BLE is used for any other purpose than commissioning.
734+
When enabled, it deinitialized the BLE on successful commissioning, and on
735+
bootup do not initialize the BLE if device is already provisioned with Wi-Fi/Thread credentials.
735736

736737
endmenu
737738

@@ -1192,8 +1193,11 @@ menu "CHIP Device Layer"
11921193
menu "Commissioning Window Options"
11931194
config CHIP_DISCOVERY_TIMEOUT_SECS
11941195
int "Commissioning Window Timeout in seconds"
1195-
range 180 900
1196-
default 900
1196+
range 180 900 if !ENABLE_BLE_EXT_ANNOUNCEMENT
1197+
range 901 172800 if ENABLE_BLE_EXT_ANNOUNCEMENT
1198+
default 900 if !ENABLE_BLE_EXT_ANNOUNCEMENT
1199+
default 172800 if ENABLE_BLE_EXT_ANNOUNCEMENT
1200+
11971201
help
11981202
The amount of time (in seconds) after which the CHIP platform will close the Commissioning Window
11991203
endmenu
@@ -1216,4 +1220,13 @@ menu "CHIP Device Layer"
12161220

12171221
endmenu
12181222

1223+
menu "Enable BLE Extended Announcement"
1224+
config ENABLE_BLE_EXT_ANNOUNCEMENT
1225+
bool "Enable BLE Extended Announcement"
1226+
default n
1227+
help
1228+
Enable BLE Extended Announcement.To be used with CHIP_DISCOVERY_TIMEOUT_SECS for extended announcement duration.
1229+
1230+
endmenu
1231+
12191232
endmenu

examples/platform/esp32/common/CommonDeviceCallbacks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
5151

5252
case DeviceEventType::kCHIPoBLEConnectionClosed:
5353
ESP_LOGI(TAG, "CHIPoBLE disconnected");
54-
Esp32AppServer::DeInitBLEIfCommissioned();
5554
break;
5655

5756
case DeviceEventType::kDnssdInitialized:
@@ -67,6 +66,7 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
6766

6867
case DeviceEventType::kCommissioningComplete: {
6968
ESP_LOGI(TAG, "Commissioning complete");
69+
Esp32AppServer::DeInitBLEIfCommissioned();
7070
}
7171
break;
7272

examples/platform/esp32/common/Esp32AppServer.cpp

+5-40
Original file line numberDiff line numberDiff line change
@@ -116,47 +116,12 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_
116116

117117
void Esp32AppServer::DeInitBLEIfCommissioned(void)
118118
{
119-
#if CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
119+
#ifdef CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
120120
if (chip::Server::GetInstance().GetFabricTable().FabricCount() > 0)
121121
{
122-
esp_err_t err = ESP_OK;
123-
124-
#if CONFIG_BT_NIMBLE_ENABLED
125-
if (!ble_hs_is_enabled())
126-
{
127-
ESP_LOGI(TAG, "BLE already deinited");
128-
return;
129-
}
130-
if (nimble_port_stop() != 0)
131-
{
132-
ESP_LOGE(TAG, "nimble_port_stop() failed");
133-
return;
134-
}
135-
vTaskDelay(100);
136-
nimble_port_deinit();
137-
138-
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
139-
err = esp_nimble_hci_and_controller_deinit();
140-
#endif
141-
#endif /* CONFIG_BT_NIMBLE_ENABLED */
142-
143-
#if CONFIG_IDF_TARGET_ESP32
144-
err |= esp_bt_mem_release(ESP_BT_MODE_BTDM);
145-
#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || \
146-
CONFIG_IDF_TARGET_ESP32C6
147-
err |= esp_bt_mem_release(ESP_BT_MODE_BLE);
148-
#endif
149-
150-
if (err != ESP_OK)
151-
{
152-
ESP_LOGE(TAG, "BLE deinit failed");
153-
}
154-
else
155-
{
156-
ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed");
157-
}
122+
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
158123
}
159-
#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
124+
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
160125
}
161126

162127
void Esp32AppServer::Init(AppDelegate * sAppDelegate)
@@ -167,7 +132,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
167132
if (hex_string_to_binary(CONFIG_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey,
168133
sizeof(sTestEventTriggerEnableKey)) == 0)
169134
{
170-
ESP_LOGE(TAG, "Failed to convert the EnableKey string to octstr type value");
135+
ChipLogError(DeviceLayer, "Failed to convert the EnableKey string to octstr type value");
171136
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
172137
}
173138
static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{};
@@ -194,7 +159,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
194159
if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() &&
195160
(chip::Server::GetInstance().GetFabricTable().FabricCount() != 0))
196161
{
197-
ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now");
162+
ChipLogProgress(DeviceLayer, "Thread has been provisioned, publish the dns service now");
198163
chip::app::DnssdServer::Instance().StartServer();
199164
}
200165
#endif

src/platform/ESP32/BLEManagerImpl.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class BLEManagerImpl final : public BLEManager,
156156
// ===== Members that implement the BLEManager internal interface.
157157

158158
CHIP_ERROR _Init(void);
159-
void _Shutdown() {}
159+
void _Shutdown();
160160
bool _IsAdvertisingEnabled(void);
161161
CHIP_ERROR _SetAdvertisingEnabled(bool val);
162162
bool _IsAdvertising(void);
@@ -232,6 +232,7 @@ class BLEManagerImpl final : public BLEManager,
232232
kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */
233233
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
234234
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
235+
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
235236
};
236237

237238
enum
@@ -296,15 +297,12 @@ class BLEManagerImpl final : public BLEManager,
296297

297298
void DriveBLEState(void);
298299
CHIP_ERROR InitESPBleLayer(void);
300+
void DeinitESPBleLayer(void);
299301
CHIP_ERROR ConfigureAdvertisingData(void);
300302
CHIP_ERROR StartAdvertising(void);
301-
302-
static constexpr System::Clock::Timeout kFastAdvertiseTimeout =
303-
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
304-
System::Clock::Timestamp mAdvertiseStartTime;
305-
306-
static void HandleFastAdvertisementTimer(System::Layer * systemLayer, void * context);
307-
void HandleFastAdvertisementTimer();
303+
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
304+
void CancelBleAdvTimeoutTimer(void);
305+
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
308306

309307
#if CONFIG_BT_BLUEDROID_ENABLED
310308
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
@@ -330,6 +328,9 @@ class BLEManagerImpl final : public BLEManager,
330328
static void HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);
331329

332330
#elif CONFIG_BT_NIMBLE_ENABLED
331+
CHIP_ERROR DeinitBLE();
332+
static void ClaimBLEMemory(System::Layer *, void *);
333+
333334
void HandleRXCharRead(struct ble_gatt_char_context * param);
334335
void HandleRXCharWrite(struct ble_gatt_char_context * param);
335336
void HandleTXCharWrite(struct ble_gatt_char_context * param);

src/platform/ESP32/CHIPDevicePlatformConfig.h

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DISCOVERY_TIMEOUT_SECS
107107
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE CONFIG_ENABLE_ESP32_BLE_CONTROLLER
108108
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART
109+
#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING CONFIG_ENABLE_BLE_EXT_ANNOUNCEMENT
109110

110111
// Options for background chip task
111112
#define CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING CONFIG_ENABLE_BG_EVENT_PROCESSING

0 commit comments

Comments
 (0)