Skip to content

Commit bfc4bac

Browse files
[ESP32] Backport nimble and bluedroid fixes to v1.3-branch. (#37801)
* [ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass (#37354) * [ESP32]: Fixed the bluedroid commissioning with latest chip-tool (#37786) - Added the indicate flag instead of notify as per spec in bluedroid implementation. - Fixes the commissioning failure due to "No valid C2" error.
1 parent bbe86c2 commit bfc4bac

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class BLEManagerImpl final : public BLEManager,
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. */
235235
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
236+
kBleDeinitAndMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */
236237
};
237238

238239
enum

src/platform/ESP32/bluedroid/BLEManagerImpl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0
105105
const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
106106
0x9D, 0x12 } };
107107

108-
const uint8_t CharProps_ReadNotify = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
109-
const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE;
108+
const uint8_t CharProps_ReadIndicate = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_INDICATE;
109+
const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE;
110110

111111
// Offsets into CHIPoBLEGATTAttrs for specific attributes.
112112
enum
@@ -136,7 +136,7 @@ const esp_gatts_attr_db_t CHIPoBLEGATTAttrs[] = {
136136

137137
// Characteristic declaration
138138
{ { ESP_GATT_AUTO_RSP },
139-
{ ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadNotify } },
139+
{ ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadIndicate } },
140140
// Characteristic value
141141
{ { ESP_GATT_RSP_BY_APP }, { ESP_UUID_LEN_128, (uint8_t *) UUID_CHIPoBLEChar_TX, ESP_GATT_PERM_READ, 512, 0, NULL } },
142142
// Client characteristic configuration description (CCCD) value
@@ -575,7 +575,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
575575
{
576576
gl_profile_tab[PROFILE_A_APP_ID].write_char_handle = char_elem_result[i].char_handle;
577577
}
578-
else if (char_elem_result[i].properties & CharProps_ReadNotify)
578+
else if (char_elem_result[i].properties & CharProps_ReadIndicate)
579579
{
580580
gl_profile_tab[PROFILE_A_APP_ID].notify_char_handle = char_elem_result[i].char_handle;
581581
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda,

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ CHIP_ERROR BLEManagerImpl::_Init()
246246

247247
void BLEManagerImpl::_Shutdown()
248248
{
249+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
250+
{
251+
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
252+
return;
253+
}
254+
249255
CancelBleAdvTimeoutTimer();
250256

251257
BleLayer::Shutdown();
@@ -725,13 +731,15 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
725731
return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
726732
}
727733
}
734+
728735
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
729736
{
730737
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
731738
{
732739
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
733740
}
734741
}
742+
735743
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
736744
{
737745
CancelBleAdvTimeoutTimer();
@@ -742,6 +750,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
742750
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
743751
}
744752
}
753+
745754
void BLEManagerImpl::DriveBLEState(void)
746755
{
747756
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -752,6 +761,11 @@ void BLEManagerImpl::DriveBLEState(void)
752761
mFlags.Set(Flags::kAsyncInitCompleted);
753762
}
754763

764+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
765+
{
766+
return;
767+
}
768+
755769
// Initializes the ESP BLE layer if needed.
756770
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
757771
{
@@ -857,7 +871,7 @@ void BLEManagerImpl::DriveBLEState(void)
857871
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
858872
{
859873
DeinitESPBleLayer();
860-
mFlags.ClearAll();
874+
mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased);
861875
}
862876

863877
exit:

0 commit comments

Comments
 (0)