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

Darwin: Bug fixes in BleConnectionDelegateImpl #29059

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/platform/Darwin/BleConnectionDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class BleConnectionDelegateImpl : public Ble::BleConnectionDelegate
virtual void NewConnection(Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator);
virtual void NewConnection(Ble::BleLayer * bleLayer, void * appState, BLE_CONNECTION_OBJECT connObj);
virtual CHIP_ERROR CancelConnection();

private:
CHIP_ERROR DoCancel();
};

} // namespace Internal
Expand Down
31 changes: 20 additions & 11 deletions src/platform/Darwin/BleConnectionDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ - (void)removePeripheralsFromCache;
// Make a copy of the device discriminator for the block to capture.
SetupDiscriminator deviceDiscriminator = inDeviceDiscriminator;

ChipLogProgress(Ble, "%s", __FUNCTION__);
ChipLogProgress(Ble, "ConnectionDelegate NewConnection with discriminator");
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
if (!bleWorkQueue) {
bleWorkQueue = dispatch_queue_create(kBleWorkQueueName, DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
}
Expand Down Expand Up @@ -134,12 +134,13 @@ - (void)removePeripheralsFromCache;
{
assertChipStackLockedByCurrentThread();

ChipLogProgress(Ble, "%s", __FUNCTION__);
ChipLogProgress(Ble, "ConnectionDelegate NewConnection with conn obj: %p", connObj);

if (!bleWorkQueue) {
bleWorkQueue = dispatch_queue_create(kBleWorkQueueName, DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
}

CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; // bridge (and retain) before dispatching
dispatch_async(bleWorkQueue, ^{
// The BLE_CONNECTION_OBJECT represent a CBPeripheral object. In order for it to be valid the central
// manager needs to still be running.
Expand All @@ -157,15 +158,15 @@ - (void)removePeripheralsFromCache;
ble.appState = appState;
ble.onConnectionComplete = OnConnectionComplete;
ble.onConnectionError = OnConnectionError;
[ble updateWithPeripheral:(__bridge CBPeripheral *) connObj];
[ble updateWithPeripheral:peripheral];
});
}

void BleConnectionDelegateImpl::StartScan(BleScannerDelegate * delegate)
{
assertChipStackLockedByCurrentThread();

ChipLogProgress(Ble, "%s", __FUNCTION__);
ChipLogProgress(Ble, "ConnectionDelegate StartScan%s", (delegate ? " with delegate" : ""));

if (!bleWorkQueue) {
bleWorkQueue = dispatch_queue_create(kBleWorkQueueName, DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
Expand All @@ -191,15 +192,19 @@ - (void)removePeripheralsFromCache;

void BleConnectionDelegateImpl::StopScan()
{
assertChipStackLockedByCurrentThread();
CancelConnection();
ChipLogProgress(Ble, "ConnectionDelegate StopScan");
DoCancel();
}

CHIP_ERROR BleConnectionDelegateImpl::CancelConnection()
{
assertChipStackLockedByCurrentThread();
ChipLogProgress(Ble, "ConnectionDelegate CancelConnection");
return DoCancel();
}

ChipLogProgress(Ble, "%s", __FUNCTION__);
CHIP_ERROR BleConnectionDelegateImpl::DoCancel()
{
assertChipStackLockedByCurrentThread();
if (bleWorkQueue == nil) {
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -284,6 +289,7 @@ - (void)setupTimer:(uint64_t)timeout

_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _workQueue);
dispatch_source_set_event_handler(_timer, ^{
ChipLogProgress(Ble, "ConnectionDelegate timeout");
[self stop];
[self dispatchConnectionError:BLE_ERROR_APP_CLOSED_CONNECTION];
});
Expand Down Expand Up @@ -374,7 +380,7 @@ - (void)centralManager:(CBCentralManager *)central
}

const uint8_t * bytes = (const uint8_t *) [serviceData bytes];
if ([serviceData length] != 8) {
if ([serviceData length] != sizeof(ChipBLEDeviceIdentificationInfo)) {
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
NSMutableString * hexString = [NSMutableString stringWithCapacity:([serviceData length] * 2)];
for (NSUInteger i = 0; i < [serviceData length]; i++) {
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long) bytes[i]]];
Expand Down Expand Up @@ -521,10 +527,11 @@ - (void)peripheral:(CBPeripheral *)peripheral
chip::Ble::ChipBleUUID svcId;
chip::Ble::ChipBleUUID charId;
[BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId];
auto * value = characteristic.value; // read immediately before dispatching

dispatch_async(_chipWorkQueue, ^{
// build a inet buffer from the rxEv and send to blelayer.
auto msgBuf = chip::System::PacketBufferHandle::NewWithData(characteristic.value.bytes, characteristic.value.length);
auto msgBuf = chip::System::PacketBufferHandle::NewWithData(value.bytes, value.length);

if (msgBuf.IsNull()) {
ChipLogError(Ble, "Failed at allocating buffer for incoming BLE data");
Expand Down Expand Up @@ -583,7 +590,9 @@ - (void)stop
_centralManager.delegate = nil;
_centralManager = nil;
_peripheral = nil;
chip::DeviceLayer::Internal::ble = nil;
if (chip::DeviceLayer::Internal::ble == self) {
ksperling-apple marked this conversation as resolved.
Show resolved Hide resolved
chip::DeviceLayer::Internal::ble = nil;
}
});
});
}
Expand Down