@@ -77,13 +77,6 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0
77
77
const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18 , 0xEE , 0x2E , 0xF5 , 0x26 , 0x3D , 0x45 , 0x59 , 0x95 , 0x9F , 0x4F , 0x9C , 0x42 , 0x9F ,
78
78
0x9D , 0x12 } };
79
79
80
- void HandleConnectTimeout (chip::System::Layer *, void * apEndpoint)
81
- {
82
- VerifyOrDie (apEndpoint != nullptr );
83
- static_cast <BluezEndpoint *>(apEndpoint)->CancelConnect ();
84
- BLEManagerImpl::HandleConnectFailed (CHIP_ERROR_TIMEOUT);
85
- }
86
-
87
80
} // namespace
88
81
89
82
BLEManagerImpl BLEManagerImpl::sInstance ;
@@ -116,16 +109,17 @@ CHIP_ERROR BLEManagerImpl::_Init()
116
109
117
110
void BLEManagerImpl::_Shutdown ()
118
111
{
119
- // Ensure scan resources are cleared (e.g. timeout timers).
112
+ // Make sure that timers are stopped before shutting down the BLE layer.
113
+ DeviceLayer::SystemLayer ().CancelTimer (HandleScanTimer, this );
114
+ DeviceLayer::SystemLayer ().CancelTimer (HandleAdvertisingTimer, this );
115
+ DeviceLayer::SystemLayer ().CancelTimer (HandleConnectTimer, this );
116
+
120
117
mDeviceScanner .Shutdown ();
121
- // Stop advertising and free resources.
122
118
mBLEAdvertisement .Shutdown ();
123
- // Make sure that the endpoint is not used by the timer.
124
- DeviceLayer::SystemLayer ().CancelTimer (HandleConnectTimeout, &mEndpoint );
125
- // Release BLE connection resources (unregister from BlueZ).
126
119
mEndpoint .Shutdown ();
120
+
127
121
mBluezObjectManager .Shutdown ();
128
- mFlags .Clear (Flags::kBluezBLELayerInitialized );
122
+ mFlags .Clear (Flags::kBluezManagerInitialized );
129
123
}
130
124
131
125
CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled (bool val)
@@ -262,15 +256,30 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv
262
256
apEvent->Platform .BLEAdapter .mAdapterAddress );
263
257
if (apEvent->Platform .BLEAdapter .mAdapterId == mAdapterId )
264
258
{
265
- // TODO: Handle adapter added
259
+ mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled ;
260
+ DriveBLEState ();
266
261
}
267
262
break ;
268
263
case DeviceEventType::kPlatformLinuxBLEAdapterRemoved :
269
264
ChipLogDetail (DeviceLayer, " BLE adapter removed: id=%u address=%s" , apEvent->Platform .BLEAdapter .mAdapterId ,
270
265
apEvent->Platform .BLEAdapter .mAdapterAddress );
271
266
if (apEvent->Platform .BLEAdapter .mAdapterId == mAdapterId )
272
267
{
273
- // TODO: Handle adapter removed
268
+ // Shutdown all BLE operations and release resources
269
+ mDeviceScanner .Shutdown ();
270
+ mBLEAdvertisement .Shutdown ();
271
+ mEndpoint .Shutdown ();
272
+ // Drop reference to the adapter
273
+ mAdapter .reset ();
274
+ // Clear all flags related to BlueZ BLE operations
275
+ mFlags .Clear (Flags::kBluezAdapterAvailable );
276
+ mFlags .Clear (Flags::kBluezBLELayerInitialized );
277
+ mFlags .Clear (Flags::kAdvertisingConfigured );
278
+ mFlags .Clear (Flags::kAppRegistered );
279
+ mFlags .Clear (Flags::kAdvertising );
280
+ CleanScanConfig ();
281
+ // Indicate that the adapter is no longer available
282
+ err = BLE_ERROR_ADAPTER_UNAVAILABLE;
274
283
}
275
284
break ;
276
285
case DeviceEventType::kPlatformLinuxBLECentralConnected :
@@ -344,9 +353,7 @@ void BLEManagerImpl::HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * apEv
344
353
exit :
345
354
if (err != CHIP_NO_ERROR)
346
355
{
347
- ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %s" , ErrorStr (err));
348
- mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
349
- DeviceLayer::SystemLayer ().CancelTimer (HandleAdvertisingTimer, this );
356
+ DisableBLEService (err);
350
357
mFlags .Clear (Flags::kControlOpInProgress );
351
358
}
352
359
}
@@ -680,9 +687,23 @@ void BLEManagerImpl::DriveBLEState()
680
687
exit :
681
688
if (err != CHIP_NO_ERROR)
682
689
{
683
- ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %s" , ErrorStr (err));
690
+ DisableBLEService (err);
691
+ }
692
+ }
693
+
694
+ void BLEManagerImpl::DisableBLEService (CHIP_ERROR err)
695
+ {
696
+ ChipLogError (DeviceLayer, " Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format ());
697
+ mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled ;
698
+ // Stop all timers if the error is other than BLE adapter unavailable. In case of BLE adapter
699
+ // beeing unavailable, we will keep timers running, as the adapter might become available in
700
+ // the nearest future (e.g. BlueZ restart due to crash). By doing that we will ensure that BLE
701
+ // adapter reappearance will not extend timeouts for the ongoing operations.
702
+ if (err != BLE_ERROR_ADAPTER_UNAVAILABLE)
703
+ {
704
+ DeviceLayer::SystemLayer ().CancelTimer (HandleScanTimer, this );
684
705
DeviceLayer::SystemLayer ().CancelTimer (HandleAdvertisingTimer, this );
685
- mServiceMode = ConnectivityManager:: kCHIPoBLEServiceMode_Disabled ;
706
+ DeviceLayer::SystemLayer (). CancelTimer (HandleConnectTimer, this ) ;
686
707
}
687
708
}
688
709
@@ -768,7 +789,7 @@ void BLEManagerImpl::InitiateScan(BleScanState scanType)
768
789
ChipLogError (Ble, " Failed to start BLE scan: %" CHIP_ERROR_FORMAT, err.Format ());
769
790
});
770
791
771
- err = DeviceLayer::SystemLayer ().StartTimer (kNewConnectionScanTimeout , HandleScannerTimer , this );
792
+ err = DeviceLayer::SystemLayer ().StartTimer (kNewConnectionScanTimeout , HandleScanTimer , this );
772
793
VerifyOrExit (err == CHIP_NO_ERROR, {
773
794
mBLEScanConfig .mBleScanState = BleScanState::kNotScanning ;
774
795
mDeviceScanner .StopScan ();
@@ -782,7 +803,7 @@ void BLEManagerImpl::InitiateScan(BleScanState scanType)
782
803
}
783
804
}
784
805
785
- void BLEManagerImpl::HandleScannerTimer (chip::System::Layer *, void * appState)
806
+ void BLEManagerImpl::HandleScanTimer (chip::System::Layer *, void * appState)
786
807
{
787
808
auto * manager = static_cast <BLEManagerImpl *>(appState);
788
809
manager->OnScanError (CHIP_ERROR_TIMEOUT);
@@ -792,7 +813,7 @@ void BLEManagerImpl::HandleScannerTimer(chip::System::Layer *, void * appState)
792
813
void BLEManagerImpl::CleanScanConfig ()
793
814
{
794
815
if (mBLEScanConfig .mBleScanState == BleScanState::kConnecting )
795
- DeviceLayer::SystemLayer ().CancelTimer (HandleConnectTimeout, & mEndpoint );
816
+ DeviceLayer::SystemLayer ().CancelTimer (HandleConnectTimer, this );
796
817
797
818
mBLEScanConfig .mBleScanState = BleScanState::kNotScanning ;
798
819
}
@@ -813,7 +834,7 @@ CHIP_ERROR BLEManagerImpl::CancelConnection()
813
834
// If in discovery mode, stop scan.
814
835
else if (mBLEScanConfig .mBleScanState != BleScanState::kNotScanning )
815
836
{
816
- DeviceLayer::SystemLayer ().CancelTimer (HandleScannerTimer , this );
837
+ DeviceLayer::SystemLayer ().CancelTimer (HandleScanTimer , this );
817
838
mDeviceScanner .StopScan ();
818
839
}
819
840
return CHIP_NO_ERROR;
@@ -904,10 +925,10 @@ void BLEManagerImpl::OnDeviceScanned(BluezDevice1 & device, const chip::Ble::Chi
904
925
// We StartScan in the ChipStack thread.
905
926
// StopScan should also be performed in the ChipStack thread.
906
927
// At the same time, the scan timer also needs to be canceled in the ChipStack thread.
907
- DeviceLayer::SystemLayer ().CancelTimer (HandleScannerTimer , this );
928
+ DeviceLayer::SystemLayer ().CancelTimer (HandleScanTimer , this );
908
929
mDeviceScanner .StopScan ();
909
930
// Stop scanning and then start connecting timer
910
- DeviceLayer::SystemLayer ().StartTimer (kConnectTimeout , HandleConnectTimeout, & mEndpoint );
931
+ DeviceLayer::SystemLayer ().StartTimer (kConnectTimeout , HandleConnectTimer, this );
911
932
chip::DeviceLayer::PlatformMgr ().UnlockChipStack ();
912
933
913
934
CHIP_ERROR err = mEndpoint .ConnectDevice (device);
@@ -916,6 +937,13 @@ void BLEManagerImpl::OnDeviceScanned(BluezDevice1 & device, const chip::Ble::Chi
916
937
ChipLogProgress (Ble, " New device connected: %s" , address);
917
938
}
918
939
940
+ void BLEManagerImpl::HandleConnectTimer (chip::System::Layer *, void * appState)
941
+ {
942
+ auto * manager = static_cast <BLEManagerImpl *>(appState);
943
+ manager->mEndpoint .CancelConnect ();
944
+ BLEManagerImpl::HandleConnectFailed (CHIP_ERROR_TIMEOUT);
945
+ }
946
+
919
947
void BLEManagerImpl::OnScanComplete ()
920
948
{
921
949
switch (mBLEScanConfig .mBleScanState )
0 commit comments