From 0b1535039399b92866bfce0ee0f8754f7d1622dd Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Mon, 3 Mar 2025 16:01:37 +1300 Subject: [PATCH] Factor out connObj bridge casts into helper functions --- src/platform/Darwin/BUILD.gn | 1 + .../Darwin/BleApplicationDelegateImpl.mm | 3 -- .../Darwin/BleConnectionDelegateImpl.mm | 30 ++++++++------- .../Darwin/BlePlatformDelegateImpl.mm | 16 ++++---- src/platform/Darwin/BleUtils.h | 37 +++++++++++++++++++ 5 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 src/platform/Darwin/BleUtils.h diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index 45bf60f364e723..2fb18f31bbcecc 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -130,6 +130,7 @@ static_library("Darwin") { "BleConnectionDelegateImpl.mm", "BlePlatformDelegateImpl.h", "BlePlatformDelegateImpl.mm", + "BleUtils.h", "MTRUUIDHelper.h", "MTRUUIDHelperImpl.mm", ] diff --git a/src/platform/Darwin/BleApplicationDelegateImpl.mm b/src/platform/Darwin/BleApplicationDelegateImpl.mm index 50397336f5155c..01fc44e46581ee 100644 --- a/src/platform/Darwin/BleApplicationDelegateImpl.mm +++ b/src/platform/Darwin/BleApplicationDelegateImpl.mm @@ -27,9 +27,6 @@ #include #include -using namespace ::chip; -using namespace ::chip::Ble; - namespace chip { namespace DeviceLayer { namespace Internal { diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index 6a2e7e1a2ef113..56a594224acf21 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,7 @@ using namespace chip::Ble; using namespace chip::DeviceLayer; +using namespace chip::DeviceLayer::Internal; using namespace chip::Tracing::DarwinPlatform; constexpr uint64_t kScanningWithDiscriminatorTimeoutInSeconds = 60; @@ -125,7 +127,7 @@ - (void)removePeripheralsFromCache; assertChipStackLockedByCurrentThread(); ChipLogProgress(Ble, "ConnectionDelegate NewConnection with conn obj: %p", connObj); - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); // The BLE_CONNECTION_OBJECT represent a CBPeripheral object. In order for it to be valid the central // manager needs to still be running. @@ -296,7 +298,7 @@ - (void)dispatchConnectionError:(CHIP_ERROR)error - (void)dispatchConnectionComplete:(CBPeripheral *)peripheral { if (self.onConnectionComplete != nil) { - self.onConnectionComplete(self.appState, (__bridge void *) peripheral); + self.onConnectionComplete(self.appState, BleConnObjectFromCBPeripheral(peripheral)); } } @@ -465,12 +467,12 @@ - (void)peripheral:(CBPeripheral *)peripheral chip::Ble::ChipBleUUID svcId; chip::Ble::ChipBleUUID charId; [BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId]; - _mBleLayer->HandleWriteConfirmation((__bridge void *) peripheral, &svcId, &charId); + _mBleLayer->HandleWriteConfirmation(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId); } else { ChipLogError( Ble, "BLE:Error writing Characteristics in Chip service on the device: [%s]", [error.localizedDescription UTF8String]); MATTER_LOG_METRIC(kMetricBLEWriteChrValueFailed, BLE_ERROR_GATT_WRITE_FAILED); - _mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_WRITE_FAILED); + _mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_WRITE_FAILED); } } @@ -486,9 +488,9 @@ - (void)peripheral:(CBPeripheral *)peripheral [BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId]; if (isNotifying) { - _mBleLayer->HandleSubscribeComplete((__bridge void *) peripheral, &svcId, &charId); + _mBleLayer->HandleSubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId); } else { - _mBleLayer->HandleUnsubscribeComplete((__bridge void *) peripheral, &svcId, &charId); + _mBleLayer->HandleUnsubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId); } } else { ChipLogError(Ble, "BLE:Error subscribing/unsubcribing some characteristic on the device: [%s]", @@ -497,11 +499,11 @@ - (void)peripheral:(CBPeripheral *)peripheral if (isNotifying) { MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_WRITE_FAILED); // we're still notifying, so we must failed the unsubscription - _mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_UNSUBSCRIBE_FAILED); + _mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_UNSUBSCRIBE_FAILED); } else { // we're not notifying, so we must failed the subscription MATTER_LOG_METRIC(kMetricBLEUpdateNotificationStateForChrFailed, BLE_ERROR_GATT_SUBSCRIBE_FAILED); - _mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_SUBSCRIBE_FAILED); + _mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_SUBSCRIBE_FAILED); } } } @@ -522,8 +524,8 @@ - (void)peripheral:(CBPeripheral *)peripheral if (msgBuf.IsNull()) { ChipLogError(Ble, "Failed at allocating buffer for incoming BLE data"); MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, CHIP_ERROR_NO_MEMORY); - _mBleLayer->HandleConnectionError((__bridge void *) peripheral, CHIP_ERROR_NO_MEMORY); - } else if (!_mBleLayer->HandleIndicationReceived((__bridge void *) peripheral, &svcId, &charId, std::move(msgBuf))) { + _mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), CHIP_ERROR_NO_MEMORY); + } else if (!_mBleLayer->HandleIndicationReceived(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId, std::move(msgBuf))) { // since this error comes from device manager core // we assume it would do the right thing, like closing the connection ChipLogError(Ble, "Failed at handling incoming BLE data"); @@ -533,7 +535,7 @@ - (void)peripheral:(CBPeripheral *)peripheral ChipLogError( Ble, "BLE:Error receiving indication of Characteristics on the device: [%s]", [error.localizedDescription UTF8String]); MATTER_LOG_METRIC(kMetricBLEUpdateValueForChrFailed, BLE_ERROR_GATT_INDICATE_FAILED); - _mBleLayer->HandleConnectionError((__bridge void *) peripheral, BLE_ERROR_GATT_INDICATE_FAILED); + _mBleLayer->HandleConnectionError(BleConnObjectFromCBPeripheral(peripheral), BLE_ERROR_GATT_INDICATE_FAILED); } } @@ -647,7 +649,7 @@ - (void)updateWithDelegate:(chip::DeviceLayer::BleScannerDelegate *)delegate pre NSData * serviceData = _cachedPeripherals[cachedPeripheral][@"data"]; ChipBLEDeviceIdentificationInfo info; memcpy(&info, [serviceData bytes], sizeof(info)); - delegate->OnBleScanAdd((__bridge void *) cachedPeripheral, info); + delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(cachedPeripheral), info); } _scannerDelegate = delegate; } @@ -721,7 +723,7 @@ - (void)addPeripheralToCache:(CBPeripheral *)peripheral data:(NSData *)data ChipBLEDeviceIdentificationInfo info; auto bytes = (const uint8_t *) [data bytes]; memcpy(&info, bytes, sizeof(info)); - delegate->OnBleScanAdd((__bridge void *) peripheral, info); + delegate->OnBleScanAdd(BleConnObjectFromCBPeripheral(peripheral), info); } timeoutTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _workQueue); @@ -768,7 +770,7 @@ - (void)removePeripheralFromCache:(CBPeripheral *)peripheral auto delegate = _scannerDelegate; if (delegate) { - delegate->OnBleScanRemove((__bridge void *) peripheral); + delegate->OnBleScanRemove(BleConnObjectFromCBPeripheral(peripheral)); } } } diff --git a/src/platform/Darwin/BlePlatformDelegateImpl.mm b/src/platform/Darwin/BlePlatformDelegateImpl.mm index aae48bbbdadc87..1fcd1585b274c7 100644 --- a/src/platform/Darwin/BlePlatformDelegateImpl.mm +++ b/src/platform/Darwin/BlePlatformDelegateImpl.mm @@ -28,12 +28,12 @@ #include #include #include +#include #import "MTRUUIDHelper.h" -using namespace ::chip; -using namespace ::chip::Ble; -using ::chip::System::PacketBufferHandle; +using namespace chip::Ble; +using chip::System::PacketBufferHandle; namespace chip { namespace DeviceLayer { @@ -49,7 +49,7 @@ CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId]; CBUUID * characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]]; - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); for (CBService * service in peripheral.services) { if ([service.UUID.data isEqualToData:serviceId.data]) { @@ -77,7 +77,7 @@ CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId]; CBUUID * characteristicId = characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]]; - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); for (CBService * service in peripheral.services) { if ([service.UUID.data isEqualToData:serviceId.data]) { @@ -96,7 +96,7 @@ CHIP_ERROR BlePlatformDelegateImpl::CloseConnection(BLE_CONNECTION_OBJECT connObj) { - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); // CoreBluetooth API requires a CBCentralManager to close a connection which is a property of the peripheral. CBCentralManager * manager = (CBCentralManager *) [peripheral valueForKey:@"manager"]; @@ -108,7 +108,7 @@ uint16_t BlePlatformDelegateImpl::GetMTU(BLE_CONNECTION_OBJECT connObj) const { - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); // The negotiated mtu length is a property of the peripheral. uint16_t mtuLength = [[peripheral valueForKey:@"mtuLength"] unsignedShortValue]; @@ -134,7 +134,7 @@ CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId]; CBUUID * characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]]; NSData * data = [NSData dataWithBytes:pBuf->Start() length:pBuf->DataLength()]; - CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); for (CBService * service in peripheral.services) { if ([service.UUID.data isEqualToData:serviceId.data]) { diff --git a/src/platform/Darwin/BleUtils.h b/src/platform/Darwin/BleUtils.h new file mode 100644 index 00000000000000..bca0d2a746724d --- /dev/null +++ b/src/platform/Darwin/BleUtils.h @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2025 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +@class CBPeripheral; + +namespace chip { +namespace DeviceLayer { + namespace Internal { + + inline CBPeripheral * CBPeripheralFromBleConnObject(BLE_CONNECTION_OBJECT connObj) + { + return (__bridge CBPeripheral *) connObj; + } + + inline BLE_CONNECTION_OBJECT BleConnObjectFromCBPeripheral(CBPeripheral * peripheral) + { + return (__bridge void *) peripheral; + } + + } +} +}