Skip to content

Commit

Permalink
[v1.3] [ESP32] Cherry-picking extended advertisement support and ble …
Browse files Browse the repository at this point in the history
…deinit crash fix (project-chip#33159)

* [ESP32] Enable extended ble announcement for esp32 platform (project-chip#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 (project-chip#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 <[email protected]>

---------

Co-authored-by: SAYON DEEP <[email protected]>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent 5b0afc7 commit 7cdf4e8
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 73 deletions.
29 changes: 21 additions & 8 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,13 @@ menu "CHIP Device Layer"
should not start advertising automatically after power-up.

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

endmenu

Expand Down Expand Up @@ -1192,8 +1193,11 @@ menu "CHIP Device Layer"
menu "Commissioning Window Options"
config CHIP_DISCOVERY_TIMEOUT_SECS
int "Commissioning Window Timeout in seconds"
range 180 900
default 900
range 180 900 if !ENABLE_BLE_EXT_ANNOUNCEMENT
range 901 172800 if ENABLE_BLE_EXT_ANNOUNCEMENT
default 900 if !ENABLE_BLE_EXT_ANNOUNCEMENT
default 172800 if ENABLE_BLE_EXT_ANNOUNCEMENT

help
The amount of time (in seconds) after which the CHIP platform will close the Commissioning Window
endmenu
Expand All @@ -1216,4 +1220,13 @@ menu "CHIP Device Layer"

endmenu

menu "Enable BLE Extended Announcement"
config ENABLE_BLE_EXT_ANNOUNCEMENT
bool "Enable BLE Extended Announcement"
default n
help
Enable BLE Extended Announcement.To be used with CHIP_DISCOVERY_TIMEOUT_SECS for extended announcement duration.

endmenu

endmenu
2 changes: 1 addition & 1 deletion examples/platform/esp32/common/CommonDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i

case DeviceEventType::kCHIPoBLEConnectionClosed:
ESP_LOGI(TAG, "CHIPoBLE disconnected");
Esp32AppServer::DeInitBLEIfCommissioned();
break;

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

case DeviceEventType::kCommissioningComplete: {
ESP_LOGI(TAG, "Commissioning complete");
Esp32AppServer::DeInitBLEIfCommissioned();
}
break;

Expand Down
45 changes: 5 additions & 40 deletions examples/platform/esp32/common/Esp32AppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,12 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_

void Esp32AppServer::DeInitBLEIfCommissioned(void)
{
#if CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
#ifdef CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
if (chip::Server::GetInstance().GetFabricTable().FabricCount() > 0)
{
esp_err_t err = ESP_OK;

#if CONFIG_BT_NIMBLE_ENABLED
if (!ble_hs_is_enabled())
{
ESP_LOGI(TAG, "BLE already deinited");
return;
}
if (nimble_port_stop() != 0)
{
ESP_LOGE(TAG, "nimble_port_stop() failed");
return;
}
vTaskDelay(100);
nimble_port_deinit();

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
err = esp_nimble_hci_and_controller_deinit();
#endif
#endif /* CONFIG_BT_NIMBLE_ENABLED */

#if CONFIG_IDF_TARGET_ESP32
err |= esp_bt_mem_release(ESP_BT_MODE_BTDM);
#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || \
CONFIG_IDF_TARGET_ESP32C6
err |= esp_bt_mem_release(ESP_BT_MODE_BLE);
#endif

if (err != ESP_OK)
{
ESP_LOGE(TAG, "BLE deinit failed");
}
else
{
ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed");
}
chip::DeviceLayer::Internal::BLEMgr().Shutdown();
}
#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
}

void Esp32AppServer::Init(AppDelegate * sAppDelegate)
Expand All @@ -167,7 +132,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
if (hex_string_to_binary(CONFIG_TEST_EVENT_TRIGGER_ENABLE_KEY, sTestEventTriggerEnableKey,
sizeof(sTestEventTriggerEnableKey)) == 0)
{
ESP_LOGE(TAG, "Failed to convert the EnableKey string to octstr type value");
ChipLogError(DeviceLayer, "Failed to convert the EnableKey string to octstr type value");
memset(sTestEventTriggerEnableKey, 0, sizeof(sTestEventTriggerEnableKey));
}
static SimpleTestEventTriggerDelegate sTestEventTriggerDelegate{};
Expand All @@ -194,7 +159,7 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
if (chip::DeviceLayer::ConnectivityMgr().IsThreadProvisioned() &&
(chip::Server::GetInstance().GetFabricTable().FabricCount() != 0))
{
ESP_LOGI(TAG, "Thread has been provisioned, publish the dns service now");
ChipLogProgress(DeviceLayer, "Thread has been provisioned, publish the dns service now");
chip::app::DnssdServer::Instance().StartServer();
}
#endif
Expand Down
17 changes: 9 additions & 8 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class BLEManagerImpl final : public BLEManager,
// ===== Members that implement the BLEManager internal interface.

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

enum
Expand Down Expand Up @@ -296,15 +297,12 @@ class BLEManagerImpl final : public BLEManager,

void DriveBLEState(void);
CHIP_ERROR InitESPBleLayer(void);
void DeinitESPBleLayer(void);
CHIP_ERROR ConfigureAdvertisingData(void);
CHIP_ERROR StartAdvertising(void);

static constexpr System::Clock::Timeout kFastAdvertiseTimeout =
System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME);
System::Clock::Timestamp mAdvertiseStartTime;

static void HandleFastAdvertisementTimer(System::Layer * systemLayer, void * context);
void HandleFastAdvertisementTimer();
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
void CancelBleAdvTimeoutTimer(void);
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);

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

#elif CONFIG_BT_NIMBLE_ENABLED
CHIP_ERROR DeinitBLE();
static void ClaimBLEMemory(System::Layer *, void *);

void HandleRXCharRead(struct ble_gatt_char_context * param);
void HandleRXCharWrite(struct ble_gatt_char_context * param);
void HandleTXCharWrite(struct ble_gatt_char_context * param);
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DISCOVERY_TIMEOUT_SECS
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART
#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING CONFIG_ENABLE_BLE_EXT_ANNOUNCEMENT

// Options for background chip task
#define CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING CONFIG_ENABLE_BG_EVENT_PROCESSING
Expand Down
Loading

0 comments on commit 7cdf4e8

Please sign in to comment.