Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Backport nimble and bluedroid fixes to v1.3-branch. #37801

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/platform/ESP32/bluedroid/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -725,13 +731,15 @@ 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))
{
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
}
}

void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
{
CancelBleAdvTimeoutTimer();
Expand All @@ -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;
Expand All @@ -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))
{
Expand Down Expand Up @@ -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:
Expand Down
Loading