@@ -64,13 +64,13 @@ @interface BleConnection : NSObject <CBCentralManagerDelegate, CBPeripheralDeleg
64
64
@property (nonatomic , readonly , nullable ) dispatch_source_t timer;
65
65
@property (nonatomic , readonly ) BleConnectionMode currentMode;
66
66
@property (strong , nonatomic ) NSMutableDictionary <CBPeripheral *, NSDictionary *> * cachedPeripherals;
67
- @property (unsafe_unretained , nonatomic ) bool found;
68
- @property (unsafe_unretained , nonatomic ) chip::SetupDiscriminator deviceDiscriminator;
69
- @property (unsafe_unretained , nonatomic ) void * appState;
70
- @property (unsafe_unretained , nonatomic ) BleConnectionDelegate::OnConnectionCompleteFunct onConnectionComplete;
71
- @property (unsafe_unretained , nonatomic ) BleConnectionDelegate::OnConnectionErrorFunct onConnectionError;
72
- @property (unsafe_unretained , nonatomic ) chip::DeviceLayer::BleScannerDelegate * scannerDelegate;
73
- @property (unsafe_unretained , nonatomic ) chip::Ble::BleLayer * mBleLayer ;
67
+ @property (assign , nonatomic ) bool found;
68
+ @property (assign , nonatomic ) chip::SetupDiscriminator deviceDiscriminator;
69
+ @property (assign , nonatomic ) void * appState;
70
+ @property (assign , nonatomic ) BleConnectionDelegate::OnConnectionCompleteFunct onConnectionComplete;
71
+ @property (assign , nonatomic ) BleConnectionDelegate::OnConnectionErrorFunct onConnectionError;
72
+ @property (assign , nonatomic ) chip::DeviceLayer::BleScannerDelegate * scannerDelegate;
73
+ @property (assign , nonatomic ) chip::Ble::BleLayer * mBleLayer ;
74
74
75
75
- (instancetype )initWithDelegate : (chip::DeviceLayer::BleScannerDelegate *)delegate prewarm : (bool )prewarm ;
76
76
- (instancetype )initWithDiscriminator : (const chip::SetupDiscriminator &)deviceDiscriminator ;
@@ -129,7 +129,7 @@ - (void)removePeripheralsFromCache;
129
129
ChipLogProgress (Ble, " ConnectionDelegate NewConnection with conn obj: %p" , connObj);
130
130
CBPeripheral * peripheral = CBPeripheralFromBleConnObject (connObj);
131
131
132
- // The BLE_CONNECTION_OBJECT represent a CBPeripheral object. In order for it to be valid the central
132
+ // The BLE_CONNECTION_OBJECT represents a CBPeripheral object. In order for it to be valid the central
133
133
// manager needs to still be running.
134
134
if (!ble || [ble isConnecting ]) {
135
135
if (OnConnectionError) {
@@ -308,6 +308,7 @@ - (void)dispatchConnectionComplete:(CBPeripheral *)peripheral
308
308
309
309
- (void )centralManagerDidUpdateState : (CBCentralManager *)central
310
310
{
311
+ assertChipStackLockedByCurrentThread ();
311
312
MATTER_LOG_METRIC (kMetricBLECentralManagerState , static_cast <uint32_t >(central.state ));
312
313
313
314
switch (central.state ) {
@@ -340,6 +341,8 @@ - (void)centralManager:(CBCentralManager *)central
340
341
advertisementData : (NSDictionary *)advertisementData
341
342
RSSI : (NSNumber *)RSSI
342
343
{
344
+ assertChipStackLockedByCurrentThread ();
345
+
343
346
NSData * serviceData = advertisementData[CBAdvertisementDataServiceDataKey][_chipServiceUUID];
344
347
if (!serviceData) {
345
348
return ;
@@ -405,8 +408,10 @@ - (BOOL)checkDiscriminator:(uint16_t)discriminator
405
408
406
409
- (void )centralManager : (CBCentralManager *)central didConnectPeripheral : (CBPeripheral *)peripheral
407
410
{
411
+ assertChipStackLockedByCurrentThread ();
408
412
MATTER_LOG_METRIC_END (kMetricBLEConnectPeripheral );
409
413
MATTER_LOG_METRIC_BEGIN (kMetricBLEDiscoveredServices );
414
+
410
415
[peripheral setDelegate: self ];
411
416
[peripheral discoverServices: nil ];
412
417
[self stopScanning ];
@@ -418,6 +423,8 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip
418
423
419
424
- (void )peripheral : (CBPeripheral *)peripheral didDiscoverServices : (NSError *)error
420
425
{
426
+ assertChipStackLockedByCurrentThread ();
427
+
421
428
if (nil != error) {
422
429
ChipLogError (Ble, " BLE:Error finding Chip Service in the device: [%s]" , [error.localizedDescription UTF8String ]);
423
430
}
@@ -442,6 +449,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err
442
449
443
450
- (void )peripheral : (CBPeripheral *)peripheral didDiscoverCharacteristicsForService : (CBService *)service error : (NSError *)error
444
451
{
452
+ assertChipStackLockedByCurrentThread ();
445
453
MATTER_LOG_METRIC_END (kMetricBLEDiscoveredCharacteristics , CHIP_ERROR (chip::ChipError::Range::kOS , static_cast <uint32_t >(error.code )));
446
454
447
455
if (nil != error) {
@@ -457,6 +465,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
457
465
didWriteValueForCharacteristic : (CBCharacteristic *)characteristic
458
466
error : (NSError *)error
459
467
{
468
+ assertChipStackLockedByCurrentThread ();
469
+
460
470
if (nil == error) {
461
471
ChipBleUUID svcId = BleUUIDFromCBUUD (characteristic.service .UUID );
462
472
ChipBleUUID charId = BleUUIDFromCBUUD (characteristic.UUID );
@@ -473,6 +483,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
473
483
didUpdateNotificationStateForCharacteristic : (CBCharacteristic *)characteristic
474
484
error : (NSError *)error
475
485
{
486
+ assertChipStackLockedByCurrentThread ();
487
+
476
488
bool isNotifying = characteristic.isNotifying ;
477
489
478
490
if (nil == error) {
@@ -503,6 +515,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
503
515
didUpdateValueForCharacteristic : (CBCharacteristic *)characteristic
504
516
error : (NSError *)error
505
517
{
518
+ assertChipStackLockedByCurrentThread ();
519
+
506
520
if (nil == error) {
507
521
ChipBleUUID svcId = BleUUIDFromCBUUD (characteristic.service .UUID );
508
522
ChipBleUUID charId = BleUUIDFromCBUUD (characteristic.UUID );
@@ -550,28 +564,19 @@ - (void)stop
550
564
[self stopScanning ];
551
565
[self removePeripheralsFromCache ];
552
566
553
- if (!_centralManager && !_peripheral) {
554
- return ;
567
+ if (_peripheral) {
568
+ // Close all BLE connections before we release CB objects
569
+ _mBleLayer->CloseAllBleConnections ();
570
+ _peripheral = nil ;
555
571
}
556
572
557
- // Properly closing the underlying ble connections needs to happens
558
- // on the chip work queue. At the same time the SDK is trying to
559
- // properly unsubscribe and shutdown the connection, so if we nullify
560
- // the centralManager and the peripheral members too early it won't be
561
- // able to reach those.
562
- // This is why closing connections happens as 2 async steps.
563
- {
564
- if (_peripheral) {
565
- // Close all BLE connections before we release CB objects
566
- _mBleLayer->CloseAllBleConnections ();
567
- }
568
-
573
+ if (_centralManager) {
569
574
_centralManager.delegate = nil ;
570
575
_centralManager = nil ;
571
- _peripheral = nil ;
572
- if (chip::DeviceLayer::Internal::ble == self) {
573
- chip::DeviceLayer::Internal::ble = nil ;
574
- }
576
+ }
577
+
578
+ if ( chip::DeviceLayer::Internal::ble == self) {
579
+ chip::DeviceLayer::Internal::ble = nil ;
575
580
}
576
581
}
577
582
0 commit comments