@@ -76,8 +76,7 @@ namespace Internal {
76
76
77
77
namespace {
78
78
79
- TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
80
- #if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
79
+ #ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
81
80
static constexpr uint16_t kNewConnectionScanTimeout = 60 ;
82
81
static constexpr uint16_t kConnectTimeout = 20 ;
83
82
#endif
@@ -212,16 +211,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
212
211
{
213
212
CHIP_ERROR err;
214
213
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
-
225
214
// Initialize the Chip BleLayer.
226
215
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
227
216
err = BleLayer::Init (this , this , this , &DeviceLayer::SystemLayer ());
@@ -257,9 +246,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
257
246
258
247
void BLEManagerImpl::_Shutdown ()
259
248
{
260
- VerifyOrReturn (sbleAdvTimeoutTimer != nullptr );
261
- xTimerDelete (sbleAdvTimeoutTimer, portMAX_DELAY);
262
- sbleAdvTimeoutTimer = nullptr ;
249
+ CancelBleAdvTimeoutTimer ();
263
250
264
251
BleLayer::Shutdown ();
265
252
@@ -294,7 +281,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
294
281
return err;
295
282
}
296
283
297
- void BLEManagerImpl::BleAdvTimeoutHandler (TimerHandle_t xTimer )
284
+ void BLEManagerImpl::BleAdvTimeoutHandler (System::Layer *, void * )
298
285
{
299
286
if (BLEMgrImpl ().mFlags .Has (Flags::kFastAdvertisingEnabled ))
300
287
{
@@ -306,7 +293,6 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
306
293
BLEMgrImpl ().mFlags .Clear (Flags::kExtAdvertisingEnabled );
307
294
BLEMgrImpl ().StartBleAdvTimeoutTimer (CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
308
295
#endif
309
- PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
310
296
}
311
297
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
312
298
else
@@ -316,9 +302,9 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
316
302
BLEMgrImpl ().mFlags .Set (Flags::kExtAdvertisingEnabled );
317
303
BLEMgr ().SetAdvertisingMode (BLEAdvertisingMode::kSlowAdvertising );
318
304
BLEMgrImpl ().mFlags .Set (Flags::kAdvertisingRefreshNeeded , 1 );
319
- PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
320
305
}
321
306
#endif
307
+ PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
322
308
}
323
309
324
310
CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode (BLEAdvertisingMode mode)
@@ -513,6 +499,10 @@ bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const
513
499
uint8_t value[2 ];
514
500
int rc;
515
501
struct peer * peer = peer_find (conId);
502
+ if (peer == nullptr )
503
+ {
504
+ return false ;
505
+ }
516
506
517
507
dsc = peer_dsc_find_uuid (peer, (ble_uuid_t *) (&ShortUUID_CHIPoBLEService), (ble_uuid_t *) (&UUID_CHIPoBLEChar_TX),
518
508
(ble_uuid_t *) (&ShortUUID_CHIPoBLE_CharTx_Desc));
@@ -547,6 +537,10 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons
547
537
uint8_t value[2 ];
548
538
int rc;
549
539
struct peer * peer = peer_find (conId);
540
+ if (peer == nullptr )
541
+ {
542
+ return false ;
543
+ }
550
544
551
545
dsc = peer_dsc_find_uuid (peer, (ble_uuid_t *) (&ShortUUID_CHIPoBLEService), (ble_uuid_t *) (&UUID_CHIPoBLEChar_TX),
552
546
(ble_uuid_t *) (&ShortUUID_CHIPoBLE_CharTx_Desc));
@@ -696,7 +690,11 @@ bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQU
696
690
return false ;
697
691
}
698
692
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
+ }
700
698
701
699
CHIP_ERROR BLEManagerImpl::MapBLEError (int bleErr)
702
700
{
@@ -729,26 +727,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
729
727
}
730
728
void BLEManagerImpl::CancelBleAdvTimeoutTimer (void )
731
729
{
732
- VerifyOrReturn (sbleAdvTimeoutTimer != nullptr );
733
-
734
- if (xTimerStop (sbleAdvTimeoutTimer, pdMS_TO_TICKS (0 )) == pdFAIL)
730
+ if (SystemLayer ().IsTimerActive (BleAdvTimeoutHandler, nullptr ))
735
731
{
736
- ChipLogError (DeviceLayer, " Failed to stop BledAdv timeout timer " );
732
+ SystemLayer (). CancelTimer (BleAdvTimeoutHandler, nullptr );
737
733
}
738
734
}
739
735
void BLEManagerImpl::StartBleAdvTimeoutTimer (uint32_t aTimeoutInMs)
740
736
{
741
- VerifyOrReturn (sbleAdvTimeoutTimer != nullptr );
742
-
743
- if (xTimerIsTimerActive (sbleAdvTimeoutTimer))
744
- {
745
- CancelBleAdvTimeoutTimer ();
746
- }
737
+ CancelBleAdvTimeoutTimer ();
747
738
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))
752
741
{
753
742
ChipLogError (DeviceLayer, " Failed to start BledAdv timeout timer" );
754
743
}
@@ -843,23 +832,21 @@ void BLEManagerImpl::DriveBLEState(void)
843
832
ExitNow ();
844
833
}
845
834
}
846
- // mFlags.Clear(Flags::kAdvertisingRefreshNeeded);
847
835
848
836
// 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 );
853
839
854
- ChipLogProgress (DeviceLayer, " CHIPoBLE advertising stopped" );
840
+ ChipLogProgress (DeviceLayer, " CHIPoBLE advertising stopped" );
855
841
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);
863
850
}
864
851
865
852
ExitNow ();
0 commit comments