From 85d1a42e5a101b336dce608e16a8a7013d22ec9a Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Tue, 7 Mar 2023 13:40:00 +0100 Subject: [PATCH] [darwin] Race when BleConnection stop is called --- .../Darwin/BleConnectionDelegateImpl.mm | 129 +++++++++++------- 1 file changed, 80 insertions(+), 49 deletions(-) diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index d69623006d84ab..69973669318a62 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -32,6 +32,7 @@ #include #include #include +#include #include #import "UUIDHelper.h" @@ -40,11 +41,12 @@ constexpr uint64_t kScanningWithDiscriminatorTimeoutInSeconds = 60; constexpr uint64_t kScanningWithoutDiscriminatorTimeoutInSeconds = 120; +constexpr const char * kBleWorkQueueName = "org.csa-iot.matter.framework.ble.workqueue"; @interface BleConnection : NSObject -@property (strong, nonatomic) dispatch_queue_t workQueue; @property (strong, nonatomic) dispatch_queue_t chipWorkQueue; +@property (strong, nonatomic) dispatch_queue_t workQueue; @property (strong, nonatomic) CBCentralManager * centralManager; @property (strong, nonatomic) CBPeripheral * peripheral; @property (strong, nonatomic) CBUUID * shortServiceUUID; @@ -58,7 +60,8 @@ @interface BleConnection : NSObject CloseAllBleConnections(); + + dispatch_async(_workQueue, ^{ + _centralManager.delegate = nil; + _centralManager = nil; + _peripheral = nil; + }); + }); } - (void)startScanning @@ -445,16 +486,6 @@ - (void)connect:(CBPeripheral *)peripheral [_centralManager connectPeripheral:peripheral options:nil]; } -- (void)disconnect -{ - if (!_centralManager || !_peripheral) { - return; - } - - _mBleLayer->CloseAllBleConnections(); - _peripheral = nil; -} - - (void)update { [_cachedPeripherals removeAllObjects];