diff --git a/src/platform/ESP32/BLEManagerImpl.h b/src/platform/ESP32/BLEManagerImpl.h index 2baa2a04836c8b..9c4737b715d0b8 100644 --- a/src/platform/ESP32/BLEManagerImpl.h +++ b/src/platform/ESP32/BLEManagerImpl.h @@ -233,6 +233,7 @@ class BLEManagerImpl final : public BLEManager, 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. */ + kBleDeinitAndMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */ }; enum diff --git a/src/platform/ESP32/bluedroid/BLEManagerImpl.cpp b/src/platform/ESP32/bluedroid/BLEManagerImpl.cpp index 277f384ec30b70..80de70f4ed02db 100644 --- a/src/platform/ESP32/bluedroid/BLEManagerImpl.cpp +++ b/src/platform/ESP32/bluedroid/BLEManagerImpl.cpp @@ -105,8 +105,8 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0 const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, 0x12 } }; -const uint8_t CharProps_ReadNotify = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_NOTIFY; -const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE; +const uint8_t CharProps_ReadIndicate = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_INDICATE; +const uint8_t CharProps_Write = ESP_GATT_CHAR_PROP_BIT_WRITE; // Offsets into CHIPoBLEGATTAttrs for specific attributes. enum @@ -136,7 +136,7 @@ const esp_gatts_attr_db_t CHIPoBLEGATTAttrs[] = { // Characteristic declaration { { ESP_GATT_AUTO_RSP }, - { ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadNotify } }, + { ESP_UUID_LEN_16, (uint8_t *) UUID_CharDecl, ESP_GATT_PERM_READ, 1, 1, (uint8_t *) &CharProps_ReadIndicate } }, // Characteristic value { { ESP_GATT_RSP_BY_APP }, { ESP_UUID_LEN_128, (uint8_t *) UUID_CHIPoBLEChar_TX, ESP_GATT_PERM_READ, 512, 0, NULL } }, // Client characteristic configuration description (CCCD) value @@ -575,7 +575,7 @@ void BLEManagerImpl::gattc_profile_event_handler(esp_gattc_cb_event_t event, esp { gl_profile_tab[PROFILE_A_APP_ID].write_char_handle = char_elem_result[i].char_handle; } - else if (char_elem_result[i].properties & CharProps_ReadNotify) + else if (char_elem_result[i].properties & CharProps_ReadIndicate) { gl_profile_tab[PROFILE_A_APP_ID].notify_char_handle = char_elem_result[i].char_handle; esp_ble_gattc_register_for_notify(gattc_if, gl_profile_tab[PROFILE_A_APP_ID].remote_bda, diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 997e36bc53dcac..5b2c45f37c6fc5 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -246,6 +246,12 @@ CHIP_ERROR BLEManagerImpl::_Init() void BLEManagerImpl::_Shutdown() { + if (mFlags.Has(Flags::kBleDeinitAndMemReleased)) + { + ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow"); + return; + } + CancelBleAdvTimeoutTimer(); BleLayer::Shutdown(); @@ -725,6 +731,7 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr) return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr); } } + void BLEManagerImpl::CancelBleAdvTimeoutTimer(void) { if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr)) @@ -732,6 +739,7 @@ void BLEManagerImpl::CancelBleAdvTimeoutTimer(void) SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr); } } + void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs) { CancelBleAdvTimeoutTimer(); @@ -742,6 +750,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs) ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer"); } } + void BLEManagerImpl::DriveBLEState(void) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -752,6 +761,11 @@ void BLEManagerImpl::DriveBLEState(void) mFlags.Set(Flags::kAsyncInitCompleted); } + if (mFlags.Has(Flags::kBleDeinitAndMemReleased)) + { + return; + } + // Initializes the ESP BLE layer if needed. if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized)) { @@ -857,7 +871,7 @@ void BLEManagerImpl::DriveBLEState(void) if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted)) { DeinitESPBleLayer(); - mFlags.ClearAll(); + mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased); } exit: