Skip to content

Commit 4d96e83

Browse files
[ESP32] Backport nimble and bluedroid fixes to v1.4-branch. (#37800)
* [ESP32]: Added the missing ble_store_config_init api to fix ble_bonding (#37705) * [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 2b4717e commit 4d96e83

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class BLEManagerImpl final : public BLEManager,
227227
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
228228
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
229229
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
230+
kBleDeinitAndMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */
230231
};
231232

232233
enum

src/platform/ESP32/bluedroid/BLEManagerImpl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ const uint8_t UUID_CHIPoBLEChar_TX[] = { 0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F
100100
const uint8_t ShortUUID_CHIPoBLE_CharTx_Desc[] = { 0x02, 0x29 };
101101
#endif
102102

103-
const uint8_t CharProps_ReadNotify = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY;
104-
const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE;
103+
const uint8_t CharProps_ReadIndicate = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_INDICATE;
104+
const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE;
105105

106106
// Offsets into CHIPoBLEGATTAttrs for specific attributes.
107107
enum
@@ -131,7 +131,7 @@ const esp_gatts_attr_db_t CHIPoBLEGATTAttrs[] = {
131131

132132
// Characteristic declaration
133133
{ { ESP_GATT_AUTO_RSP },
134-
{ ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadNotify } },
134+
{ ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadIndicate } },
135135
// Characteristic value
136136
{ { ESP_GATT_RSP_BY_APP }, { ESP_UUID_LEN_128, (uint8_t *) UUID_CHIPoBLEChar_TX, ESP_GATT_PERM_READ, 512, 0, NULL } },
137137
// Client characteristic configuration description (CCCD) value
@@ -568,7 +568,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp
568568
{
569569
gl_profile_tab[PROFILE_A_APP_ID].write_char_handle = char_elem_result[i].char_handle;
570570
}
571-
else if (char_elem_result[i].properties & CharProps_ReadNotify)
571+
else if (char_elem_result[i].properties & CharProps_ReadIndicate)
572572
{
573573
gl_profile_tab[PROFILE_A_APP_ID].notify_char_handle = char_elem_result[i].char_handle;
574574
esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda,

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
#include "services/gap/ble_svc_gap.h"
7171
#include "services/gatt/ble_svc_gatt.h"
7272

73+
// Not declared in any header file, hence requires a forward declaration.
74+
extern "C" void ble_store_config_init(void);
75+
7376
#define MAX_ADV_DATA_LEN 31
7477
#define CHIP_ADV_DATA_TYPE_FLAGS 0x01
7578
#define CHIP_ADV_DATA_FLAGS 0x06
@@ -244,6 +247,12 @@ CHIP_ERROR BLEManagerImpl::_Init()
244247

245248
void BLEManagerImpl::_Shutdown()
246249
{
250+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
251+
{
252+
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
253+
return;
254+
}
255+
247256
CancelBleAdvTimeoutTimer();
248257

249258
BleLayer::Shutdown();
@@ -712,13 +721,15 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
712721
return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
713722
}
714723
}
724+
715725
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
716726
{
717727
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
718728
{
719729
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
720730
}
721731
}
732+
722733
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
723734
{
724735
CancelBleAdvTimeoutTimer();
@@ -729,6 +740,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
729740
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
730741
}
731742
}
743+
732744
void BLEManagerImpl::DriveBLEState(void)
733745
{
734746
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -739,6 +751,11 @@ void BLEManagerImpl::DriveBLEState(void)
739751
mFlags.Set(Flags::kAsyncInitCompleted);
740752
}
741753

754+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
755+
{
756+
return;
757+
}
758+
742759
// Initializes the ESP BLE layer if needed.
743760
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
744761
{
@@ -844,7 +861,7 @@ void BLEManagerImpl::DriveBLEState(void)
844861
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
845862
{
846863
DeinitESPBleLayer();
847-
mFlags.ClearAll();
864+
mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased);
848865
}
849866

850867
exit:
@@ -956,6 +973,7 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)
956973
}
957974
}
958975

976+
ble_store_config_init();
959977
nimble_port_freertos_init(bleprph_host_task);
960978

961979
xSemaphoreTake(semaphoreHandle, portMAX_DELAY);

0 commit comments

Comments
 (0)