Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to do our BLE callbacks on the right dispatch queue. #20235

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/platform/Darwin/BleConnectionDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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");
Expand Down Expand Up @@ -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];
}
}

Expand All @@ -228,7 +243,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi
}

// XXX error ?
_onConnectionComplete(_appState, (__bridge void *) peripheral);
[self dispatchConnectionComplete:peripheral];
}

- (void)peripheral:(CBPeripheral *)peripheral
Expand Down