Skip to content

Commit 7726d3b

Browse files
shubhamdppimpalemaheshDejinChen
authored
[v1.3][ESP32] Cherry-pick few BLEManager fixes (#34123)
* [ESP32] Replaced FreeRTOS timer with CHIP timers in nimble BLEManagerImpl. (#34050) * Replace free RTOS timer with ChipTimer * Replaced FreeRTOS timer with CHIP timer * ESP32: Fixes in BLEManager (#34082) - Fix the ble re-advertisement if invalid bytes are written on the characteristic. - cancel the ble advertisement timer on ble connection - remove the unnecessary if check * Fix crash for eps32 commissioner if ble disconnect during commissioning (#33332) --------- Co-authored-by: Mahesh <92411857+pimpalemahesh@users.noreply.github.com> Co-authored-by: cdj <45139296+DejinChen@users.noreply.github.com>
1 parent f4363a0 commit 7726d3b

File tree

3 files changed

+35
-47
lines changed

3 files changed

+35
-47
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class BLEManagerImpl final : public BLEManager,
302302
CHIP_ERROR StartAdvertising(void);
303303
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
304304
void CancelBleAdvTimeoutTimer(void);
305-
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
305+
static void BleAdvTimeoutHandler(System::Layer *, void *);
306306

307307
#if CONFIG_BT_BLUEDROID_ENABLED
308308
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);

src/platform/ESP32/bluedroid/BLEManagerImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQU
916916
void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)
917917
{
918918
ChipLogProgress(Ble, "Got notification regarding chip connection closure");
919+
CloseConnection(conId);
919920
}
920921

921922
CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+33-46
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ namespace Internal {
7676

7777
namespace {
7878

79-
TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
80-
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
79+
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
8180
static constexpr uint16_t kNewConnectionScanTimeout = 60;
8281
static constexpr uint16_t kConnectTimeout = 20;
8382
#endif
@@ -212,16 +211,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
212211
{
213212
CHIP_ERROR err;
214213

215-
// Create FreeRTOS sw timer for BLE timeouts and interval change.
216-
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // Just a text name, not used by the RTOS kernel
217-
1, // == default timer period
218-
false, // no timer reload (==one-shot)
219-
(void *) this, // init timer id = ble obj context
220-
BleAdvTimeoutHandler // timer callback handler
221-
);
222-
223-
VerifyOrReturnError(sbleAdvTimeoutTimer != nullptr, CHIP_ERROR_NO_MEMORY);
224-
225214
// Initialize the Chip BleLayer.
226215
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
227216
err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
@@ -257,9 +246,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
257246

258247
void BLEManagerImpl::_Shutdown()
259248
{
260-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
261-
xTimerDelete(sbleAdvTimeoutTimer, portMAX_DELAY);
262-
sbleAdvTimeoutTimer = nullptr;
249+
CancelBleAdvTimeoutTimer();
263250

264251
BleLayer::Shutdown();
265252

@@ -294,7 +281,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
294281
return err;
295282
}
296283

297-
void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
284+
void BLEManagerImpl::BleAdvTimeoutHandler(System::Layer *, void *)
298285
{
299286
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
300287
{
@@ -306,7 +293,6 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
306293
BLEMgrImpl().mFlags.Clear(Flags::kExtAdvertisingEnabled);
307294
BLEMgrImpl().StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
308295
#endif
309-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
310296
}
311297
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
312298
else
@@ -316,9 +302,9 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
316302
BLEMgrImpl().mFlags.Set(Flags::kExtAdvertisingEnabled);
317303
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
318304
BLEMgrImpl().mFlags.Set(Flags::kAdvertisingRefreshNeeded, 1);
319-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
320305
}
321306
#endif
307+
PlatformMgr().ScheduleWork(DriveBLEState, 0);
322308
}
323309

324310
CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
@@ -513,6 +499,10 @@ bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const
513499
uint8_t value[2];
514500
int rc;
515501
struct peer * peer = peer_find(conId);
502+
if (peer == nullptr)
503+
{
504+
return false;
505+
}
516506

517507
dsc = peer_dsc_find_uuid(peer, (ble_uuid_t *) (&ShortUUID_CHIPoBLEService), (ble_uuid_t *) (&UUID_CHIPoBLEChar_TX),
518508
(ble_uuid_t *) (&ShortUUID_CHIPoBLE_CharTx_Desc));
@@ -547,6 +537,10 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons
547537
uint8_t value[2];
548538
int rc;
549539
struct peer * peer = peer_find(conId);
540+
if (peer == nullptr)
541+
{
542+
return false;
543+
}
550544

551545
dsc = peer_dsc_find_uuid(peer, (ble_uuid_t *) (&ShortUUID_CHIPoBLEService), (ble_uuid_t *) (&UUID_CHIPoBLEChar_TX),
552546
(ble_uuid_t *) (&ShortUUID_CHIPoBLE_CharTx_Desc));
@@ -696,7 +690,11 @@ bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQU
696690
return false;
697691
}
698692

699-
void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) {}
693+
void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId)
694+
{
695+
ChipLogDetail(Ble, "Received notification of closed CHIPoBLE connection (con %u)", conId);
696+
CloseConnection(conId);
697+
}
700698

701699
CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
702700
{
@@ -729,26 +727,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
729727
}
730728
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
731729
{
732-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
733-
734-
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
730+
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
735731
{
736-
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
732+
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
737733
}
738734
}
739735
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
740736
{
741-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
742-
743-
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
744-
{
745-
CancelBleAdvTimeoutTimer();
746-
}
737+
CancelBleAdvTimeoutTimer();
747738

748-
// timer is not active, change its period to required value (== restart).
749-
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
750-
// cannot immediately be sent to the timer command queue.
751-
if (xTimerChangePeriod(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
739+
CHIP_ERROR err = SystemLayer().StartTimer(System::Clock::Milliseconds32(aTimeoutInMs), BleAdvTimeoutHandler, nullptr);
740+
if ((err != CHIP_NO_ERROR))
752741
{
753742
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
754743
}
@@ -843,23 +832,21 @@ void BLEManagerImpl::DriveBLEState(void)
843832
ExitNow();
844833
}
845834
}
846-
// mFlags.Clear(Flags::kAdvertisingRefreshNeeded);
847835

848836
// Transition to the not Advertising state...
849-
if (mFlags.Has(Flags::kAdvertising))
850-
{
851-
mFlags.Clear(Flags::kAdvertising);
852-
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
837+
mFlags.Clear(Flags::kAdvertising);
838+
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
853839

854-
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising stopped");
840+
ChipLogProgress(DeviceLayer, "CHIPoBLE advertising stopped");
855841

856-
// Post a CHIPoBLEAdvertisingChange(Stopped) event.
857-
{
858-
ChipDeviceEvent advChange;
859-
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
860-
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped;
861-
err = PlatformMgr().PostEvent(&advChange);
862-
}
842+
CancelBleAdvTimeoutTimer();
843+
844+
// Post a CHIPoBLEAdvertisingChange(Stopped) event.
845+
{
846+
ChipDeviceEvent advChange;
847+
advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
848+
advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped;
849+
err = PlatformMgr().PostEvent(&advChange);
863850
}
864851

865852
ExitNow();

0 commit comments

Comments
 (0)