From 3498e8057ddf7c2dc9d33f78fe35c89f2fbf7fea Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 1 Jul 2022 22:18:41 -0400 Subject: [PATCH] Make sure to do our BLE callbacks on the right dispatch queue. (#20235) We were calling back into the Matter core while running on the BLE queue. https://github.com/project-chip/connectedhomeip/issues/20230 --- .../Darwin/BleConnectionDelegateImpl.mm | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index d2bff08e99a953..579fab4abf6294 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -108,7 +108,7 @@ - (id)initWithDiscriminator:(uint16_t)deviceDiscriminator dispatch_source_set_event_handler(_timer, ^{ [self stop]; - _onConnectionError(_appState, BLE_ERROR_APP_CLOSED_CONNECTION); + [self dispatchConnectionError:BLE_ERROR_APP_CLOSED_CONNECTION]; }); dispatch_source_set_timer( _timer, dispatch_walltime(nullptr, kScanningTimeoutInSeconds * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 5 * NSEC_PER_SEC); @@ -117,6 +117,21 @@ - (id)initWithDiscriminator:(uint16_t)deviceDiscriminator return self; } +// All our callback dispatch must happen on _chipWorkQueue +- (void)dispatchConnectionError:(CHIP_ERROR)error +{ + dispatch_async(_chipWorkQueue, ^{ + self.onConnectionError(self.appState, error); + }); +} + +- (void)dispatchConnectionComplete:(CBPeripheral *)peripheral +{ + dispatch_async(_chipWorkQueue, ^{ + self.onConnectionComplete(self.appState, (__bridge void *) peripheral); + }); +} + // Start CBCentralManagerDelegate - (void)centralManagerDidUpdateState:(CBCentralManager *)central @@ -129,7 +144,7 @@ - (void)centralManagerDidUpdateState:(CBCentralManager *)central case CBManagerStatePoweredOff: ChipLogDetail(Ble, "CBManagerState: OFF"); [self stop]; - _onConnectionError(_appState, BLE_ERROR_APP_CLOSED_CONNECTION); + [self dispatchConnectionError:BLE_ERROR_APP_CLOSED_CONNECTION]; break; case CBManagerStateUnauthorized: ChipLogDetail(Ble, "CBManagerState: Unauthorized"); @@ -216,7 +231,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err if (!self.found || error != nil) { ChipLogError(Ble, "Service not found on the device."); - _onConnectionError(_appState, CHIP_ERROR_INCORRECT_STATE); + [self dispatchConnectionError:CHIP_ERROR_INCORRECT_STATE]; } } @@ -228,7 +243,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi } // XXX error ? - _onConnectionComplete(_appState, (__bridge void *) peripheral); + [self dispatchConnectionComplete:peripheral]; } - (void)peripheral:(CBPeripheral *)peripheral