From 3557240fefac91ff21b92b6b8da53ce000c93ceb Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 3 Jun 2021 05:45:09 +0200 Subject: [PATCH] Enable arc for chip-device-ctrl (#6992) --- src/controller/python/BUILD.gn | 1 + .../python/chip/ble/darwin/AdapterListing.mm | 11 +++++++---- src/controller/python/chip/ble/darwin/Scanning.mm | 2 +- src/platform/BUILD.gn | 2 ++ src/platform/Darwin/BleApplicationDelegateImpl.mm | 4 ++++ src/platform/Darwin/BleConnectionDelegateImpl.mm | 7 +++++-- src/platform/Darwin/BlePlatformDelegateImpl.mm | 13 +++++++++---- src/platform/Darwin/KeyValueStoreManagerImpl.mm | 4 ++++ src/platform/Darwin/UUIDHelperImpl.mm | 4 ++++ 9 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/controller/python/BUILD.gn b/src/controller/python/BUILD.gn index f2e002858a2462..8614d4cc8ccad5 100644 --- a/src/controller/python/BUILD.gn +++ b/src/controller/python/BUILD.gn @@ -63,6 +63,7 @@ shared_library("ChipDeviceCtrl") { "chip/ble/darwin/AdapterListing.mm", "chip/ble/darwin/Scanning.mm", ] + cflags = [ "-fobjc-arc" ] } else { assert(false, "No BLE implementation available for the current OS.") } diff --git a/src/controller/python/chip/ble/darwin/AdapterListing.mm b/src/controller/python/chip/ble/darwin/AdapterListing.mm index 51a55b694c1b8b..81a0f0433aa149 100644 --- a/src/controller/python/chip/ble/darwin/AdapterListing.mm +++ b/src/controller/python/chip/ble/darwin/AdapterListing.mm @@ -9,7 +9,7 @@ @interface FakeBleAdapterInformation : NSObject @property (strong, nonatomic) CBCentralManager * centralManager; @property (nonatomic) bool advanced; @property (nonatomic) bool hasStatus; -@property (assign, nonatomic) dispatch_semaphore_t statusSemaphore; +@property (strong, nonatomic) dispatch_semaphore_t statusSemaphore; - (id)init; - (bool)isPoweredOn; @@ -84,9 +84,12 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip @end -extern "C" void * pychip_ble_adapter_list_new() { return static_cast([[FakeBleAdapterInformation alloc] init]); } +extern "C" void * pychip_ble_adapter_list_new() { return (__bridge_retained void *) [[FakeBleAdapterInformation alloc] init]; } -extern "C" void pychip_ble_adapter_list_delete(FakeBleAdapterInformation * adapterIterator) { [adapterIterator release]; } +extern "C" void pychip_ble_adapter_list_delete(FakeBleAdapterInformation * adapterIterator) +{ + CFRelease((CFTypeRef) adapterIterator); +} extern "C" bool pychip_ble_adapter_list_next(FakeBleAdapterInformation * adapterIterator) { @@ -115,5 +118,5 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip extern "C" void * pychip_ble_adapter_list_get_raw_adapter(FakeBleAdapterInformation * adapterIterator) { - return static_cast(adapterIterator); + return (__bridge void *) adapterIterator; } diff --git a/src/controller/python/chip/ble/darwin/Scanning.mm b/src/controller/python/chip/ble/darwin/Scanning.mm index a68f0cb345d94c..9538d7b2b666cd 100644 --- a/src/controller/python/chip/ble/darwin/Scanning.mm +++ b/src/controller/python/chip/ble/darwin/Scanning.mm @@ -135,5 +135,5 @@ - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPerip completeCallback:completeCallback timeoutMs:timeout]; - return static_cast(scanner); + return (__bridge_retained void *) (scanner); } diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index a9c7bde6d7aba7..15c32e5328fdcf 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -336,6 +336,8 @@ if (chip_device_platform != "none" && chip_device_platform != "external") { ] } } else if (chip_device_platform == "darwin") { + cflags += [ "-fobjc-arc" ] + sources += [ "Darwin/BLEManagerImpl.cpp", "Darwin/BLEManagerImpl.h", diff --git a/src/platform/Darwin/BleApplicationDelegateImpl.mm b/src/platform/Darwin/BleApplicationDelegateImpl.mm index 389608f5524900..c61f17a894d7c1 100644 --- a/src/platform/Darwin/BleApplicationDelegateImpl.mm +++ b/src/platform/Darwin/BleApplicationDelegateImpl.mm @@ -20,6 +20,10 @@ * Provides an implementation of BleApplicationDelegate for Darwin platforms. */ +#if !__has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + #include #include diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index ecb3506d67044f..8c86986b0e9dc9 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -21,6 +21,10 @@ * Provides an implementation of BleConnectionDelegate for Darwin platforms. */ +#if !__has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + #include #include #include @@ -70,7 +74,7 @@ - (void)stop; ble.appState = appState; ble.onConnectionComplete = OnConnectionComplete; ble.onConnectionError = OnConnectionError; - [ble.centralManager initWithDelegate:ble queue:ble.workQueue]; + ble.centralManager = [ble.centralManager initWithDelegate:ble queue:ble.workQueue]; } BLE_ERROR BleConnectionDelegateImpl::CancelConnection() @@ -356,7 +360,6 @@ - (void)connect:(CBPeripheral *)peripheral } _peripheral = peripheral; - [_peripheral retain]; [_centralManager connectPeripheral:peripheral options:nil]; } diff --git a/src/platform/Darwin/BlePlatformDelegateImpl.mm b/src/platform/Darwin/BlePlatformDelegateImpl.mm index a28045cc70dab2..a04531e0d5a299 100644 --- a/src/platform/Darwin/BlePlatformDelegateImpl.mm +++ b/src/platform/Darwin/BlePlatformDelegateImpl.mm @@ -21,6 +21,10 @@ * Provides an implementation of BlePlatformDelegate for Darwin platforms. */ +#if !__has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + #include #include #include @@ -43,7 +47,7 @@ bool found = false; CBUUID * serviceId = nil; CBUUID * characteristicId = nil; - CBPeripheral * peripheral = (CBPeripheral *) connObj; + CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; if (NULL != svcId) { serviceId = [UUIDHelper GetShortestServiceUUID:svcId]; @@ -74,7 +78,7 @@ bool found = false; CBUUID * serviceId = nil; CBUUID * characteristicId = nil; - CBPeripheral * peripheral = (CBPeripheral *) connObj; + CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; if (NULL != svcId) { serviceId = [UUIDHelper GetShortestServiceUUID:svcId]; @@ -100,7 +104,8 @@ bool BlePlatformDelegateImpl::CloseConnection(BLE_CONNECTION_OBJECT connObj) { - CBPeripheral * peripheral = (CBPeripheral *) connObj; + CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; + // CoreBluetooth API requires a CBCentralManager to close a connection which is a property of the peripheral. CBCentralManager * manager = (CBCentralManager *) [peripheral valueForKey:@"manager"]; if (manager != nil) @@ -124,7 +129,7 @@ CBUUID * serviceId = nil; CBUUID * characteristicId = nil; NSData * data = nil; - CBPeripheral * peripheral = (CBPeripheral *) connObj; + CBPeripheral * peripheral = (__bridge CBPeripheral *) connObj; if (NULL != svcId) { serviceId = [UUIDHelper GetShortestServiceUUID:svcId]; diff --git a/src/platform/Darwin/KeyValueStoreManagerImpl.mm b/src/platform/Darwin/KeyValueStoreManagerImpl.mm index 0ce40ec09da9e7..43bdf773824e73 100644 --- a/src/platform/Darwin/KeyValueStoreManagerImpl.mm +++ b/src/platform/Darwin/KeyValueStoreManagerImpl.mm @@ -21,6 +21,10 @@ * Platform-specific key value storage implementation for Darwin */ +#if !__has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + #include #include diff --git a/src/platform/Darwin/UUIDHelperImpl.mm b/src/platform/Darwin/UUIDHelperImpl.mm index 8ccb5000258f52..807b143f46991a 100644 --- a/src/platform/Darwin/UUIDHelperImpl.mm +++ b/src/platform/Darwin/UUIDHelperImpl.mm @@ -16,6 +16,10 @@ * limitations under the License. */ +#if !__has_feature(objc_arc) +#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). +#endif + #import "UUIDHelper.h" @implementation UUIDHelper