diff --git a/src/controller/python/chip/ble/darwin/Scanning.mm b/src/controller/python/chip/ble/darwin/Scanning.mm index 564a984e094369..404008fbddd0cf 100644 --- a/src/controller/python/chip/ble/darwin/Scanning.mm +++ b/src/controller/python/chip/ble/darwin/Scanning.mm @@ -1,7 +1,7 @@ #include #include #include -#include +#include #import @@ -45,7 +45,7 @@ - (id)initWithContext:(PyObject *)context { self = [super init]; if (self) { - self.shortServiceUUID = [MTRUUIDHelper GetShortestServiceUUID:&chip::Ble::CHIP_BLE_SVC_ID]; + self.shortServiceUUID = chip::DeviceLayer::Internal::CBUUIDFromBleUUID(chip::Ble::CHIP_BLE_SVC_ID); _workQueue = dispatch_queue_create("com.chip.python.ble.work_queue", DISPATCH_QUEUE_SERIAL); _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _workQueue); @@ -78,7 +78,7 @@ - (void)centralManager:(CBCentralManager *)central NSDictionary * servicesData = [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; for (CBUUID * serviceUUID in servicesData) { - if (![serviceUUID.data isEqualToData:_shortServiceUUID.data]) { + if (![serviceUUID isEqualTo:_shortServiceUUID]) { continue; } NSData * serviceData = [servicesData objectForKey:serviceUUID]; diff --git a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj index 53f5f54282114d..4c170b3940f0dc 100644 --- a/src/darwin/Framework/Matter.xcodeproj/project.pbxproj +++ b/src/darwin/Framework/Matter.xcodeproj/project.pbxproj @@ -652,6 +652,8 @@ 3DA1A3532ABAB3B4004F0BB9 /* MTRAsyncWorkQueue.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRAsyncWorkQueue.mm; sourceTree = ""; }; 3DA1A3572ABABF69004F0BB9 /* MTRAsyncWorkQueueTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MTRAsyncWorkQueueTests.m; sourceTree = ""; }; 3DB9DAE42D67EE5A00704FAB /* MTRBleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MTRBleTests.m; sourceTree = ""; }; + 3DB9DAE92D754C7200704FAB /* BleUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BleUtils.h; sourceTree = ""; }; + 3DB9DAEA2D754C7200704FAB /* BleUtils.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = BleUtils.mm; sourceTree = ""; }; 3DECCB6D29347D2C00585AEC /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/Security.framework; sourceTree = DEVELOPER_DIR; }; 3DECCB6F2934AC1C00585AEC /* MTRLogging.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRLogging.h; sourceTree = ""; }; 3DECCB712934AFE200585AEC /* MTRLogging.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRLogging.mm; sourceTree = ""; }; @@ -694,8 +696,6 @@ 3DF5218B2D5E90BE008F8E52 /* Logging.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Logging.mm; sourceTree = ""; }; 3DF5218C2D5E90BE008F8E52 /* MdnsError.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MdnsError.h; sourceTree = ""; }; 3DF5218D2D5E90BE008F8E52 /* MdnsError.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = MdnsError.cpp; sourceTree = ""; }; - 3DF5218E2D5E90BE008F8E52 /* MTRUUIDHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MTRUUIDHelper.h; sourceTree = ""; }; - 3DF5218F2D5E90BE008F8E52 /* MTRUUIDHelperImpl.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MTRUUIDHelperImpl.mm; sourceTree = ""; }; 3DF521902D5E90BE008F8E52 /* NetworkCommissioningDriver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NetworkCommissioningDriver.h; sourceTree = ""; }; 3DF521912D5E90BE008F8E52 /* PlatformManagerImpl.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PlatformManagerImpl.h; sourceTree = ""; }; 3DF521922D5E90BE008F8E52 /* PlatformManagerImpl.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = PlatformManagerImpl.cpp; sourceTree = ""; }; @@ -1409,6 +1409,8 @@ 3DF5216F2D5E90BE008F8E52 /* BlePlatformDelegateImpl.h */, 3DF521702D5E90BE008F8E52 /* BlePlatformDelegateImpl.mm */, 3DF521712D5E90BE008F8E52 /* BleScannerDelegate.h */, + 3DB9DAE92D754C7200704FAB /* BleUtils.h */, + 3DB9DAEA2D754C7200704FAB /* BleUtils.mm */, 3DF521732D5E90BE008F8E52 /* CHIPDevicePlatformConfig.h */, 3DF521742D5E90BE008F8E52 /* CHIPDevicePlatformEvent.h */, 3DF521752D5E90BE008F8E52 /* CHIPPlatformConfig.h */, @@ -1436,8 +1438,6 @@ 3DF5218B2D5E90BE008F8E52 /* Logging.mm */, 3DF5218C2D5E90BE008F8E52 /* MdnsError.h */, 3DF5218D2D5E90BE008F8E52 /* MdnsError.cpp */, - 3DF5218E2D5E90BE008F8E52 /* MTRUUIDHelper.h */, - 3DF5218F2D5E90BE008F8E52 /* MTRUUIDHelperImpl.mm */, 3DF521902D5E90BE008F8E52 /* NetworkCommissioningDriver.h */, 3DF521912D5E90BE008F8E52 /* PlatformManagerImpl.h */, 3DF521922D5E90BE008F8E52 /* PlatformManagerImpl.cpp */, diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index 2fb18f31bbcecc..57ad9c7821db33 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -131,8 +131,7 @@ static_library("Darwin") { "BlePlatformDelegateImpl.h", "BlePlatformDelegateImpl.mm", "BleUtils.h", - "MTRUUIDHelper.h", - "MTRUUIDHelperImpl.mm", + "BleUtils.mm", ] } } diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index 56a594224acf21..0f9399d5ec3482 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -35,7 +35,8 @@ #include #include -#import "MTRUUIDHelper.h" +#import + #import "PlatformMetricKeys.h" using namespace chip::Ble; @@ -60,7 +61,6 @@ @interface BleConnection : NSObject * cachedPeripherals; @@ -212,13 +212,15 @@ @interface BleConnection () @property (nonatomic, readonly) int32_t totalDevicesRemoved; @end -@implementation BleConnection +@implementation BleConnection { + CBUUID * _chipServiceUUID; +} - (instancetype)init { self = [super init]; if (self) { - self.shortServiceUUID = [MTRUUIDHelper GetShortestServiceUUID:&chip::Ble::CHIP_BLE_SVC_ID]; + _chipServiceUUID = CBUUIDFromBleUUID(chip::Ble::CHIP_BLE_SVC_ID); _workQueue = chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue(); _centralManager = [CBCentralManager alloc]; _found = false; @@ -338,15 +340,7 @@ - (void)centralManager:(CBCentralManager *)central advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { - NSDictionary * servicesData = [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; - NSData * serviceData; - for (CBUUID * serviceUUID in servicesData) { - if ([serviceUUID.data isEqualToData:_shortServiceUUID.data]) { - serviceData = [servicesData objectForKey:serviceUUID]; - break; - } - } - + NSData * serviceData = advertisementData[CBAdvertisementDataServiceDataKey][_chipServiceUUID]; if (!serviceData) { return; } @@ -431,7 +425,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err MATTER_LOG_METRIC_END(kMetricBLEDiscoveredServices, CHIP_ERROR(chip::ChipError::Range::kOS, static_cast(error.code))); for (CBService * service in peripheral.services) { - if ([service.UUID.data isEqualToData:_shortServiceUUID.data] && !self.found) { + if ([service.UUID isEqual:_chipServiceUUID] && !self.found) { MATTER_LOG_METRIC_BEGIN(kMetricBLEDiscoveredCharacteristics); [peripheral discoverCharacteristics:nil forService:service]; self.found = true; @@ -464,9 +458,8 @@ - (void)peripheral:(CBPeripheral *)peripheral error:(NSError *)error { if (nil == error) { - chip::Ble::ChipBleUUID svcId; - chip::Ble::ChipBleUUID charId; - [BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId]; + ChipBleUUID svcId = BleUUIDFromCBUUD(characteristic.service.UUID); + ChipBleUUID charId = BleUUIDFromCBUUD(characteristic.UUID); _mBleLayer->HandleWriteConfirmation(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId); } else { ChipLogError( @@ -483,10 +476,8 @@ - (void)peripheral:(CBPeripheral *)peripheral bool isNotifying = characteristic.isNotifying; if (nil == error) { - chip::Ble::ChipBleUUID svcId; - chip::Ble::ChipBleUUID charId; - [BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId]; - + ChipBleUUID svcId = BleUUIDFromCBUUD(characteristic.service.UUID); + ChipBleUUID charId = BleUUIDFromCBUUD(characteristic.UUID); if (isNotifying) { _mBleLayer->HandleSubscribeComplete(BleConnObjectFromCBPeripheral(peripheral), &svcId, &charId); } else { @@ -513,9 +504,8 @@ - (void)peripheral:(CBPeripheral *)peripheral error:(NSError *)error { if (nil == error) { - chip::Ble::ChipBleUUID svcId; - chip::Ble::ChipBleUUID charId; - [BleConnection fillServiceWithCharacteristicUuids:characteristic svcId:&svcId charId:&charId]; + ChipBleUUID svcId = BleUUIDFromCBUUD(characteristic.service.UUID); + ChipBleUUID charId = BleUUIDFromCBUUD(characteristic.UUID); auto * value = characteristic.value; // read immediately before dispatching // build a inet buffer from the rxEv and send to blelayer. @@ -603,7 +593,7 @@ - (void)startScanning [self _resetCounters]; auto scanOptions = @{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }; - [_centralManager scanForPeripheralsWithServices:@[ _shortServiceUUID ] options:scanOptions]; + [_centralManager scanForPeripheralsWithServices:@[ _chipServiceUUID ] options:scanOptions]; } - (void)stopScanning @@ -782,55 +772,6 @@ - (void)removePeripheralsFromCache } } -/** - * private static method to copy service and characteristic UUIDs from CBCharacteristic to a pair of ChipBleUUID objects. - * this is used in calls into Chip layer to decouple it from CoreBluetooth - * - * @param[in] characteristic the source characteristic - * @param[in] svcId the destination service UUID - * @param[in] charId the destination characteristic UUID - * - */ -+ (void)fillServiceWithCharacteristicUuids:(CBCharacteristic *)characteristic - svcId:(chip::Ble::ChipBleUUID *)svcId - charId:(chip::Ble::ChipBleUUID *)charId -{ - static const size_t FullUUIDLength = 16; - if ((FullUUIDLength != sizeof(charId->bytes)) || (FullUUIDLength != sizeof(svcId->bytes)) - || (FullUUIDLength != characteristic.UUID.data.length)) { - // we're dead. we expect the data length to be the same (16-byte) across the board - ChipLogError(Ble, "UUID of characteristic is incompatible"); - return; - } - - memcpy(charId->bytes, characteristic.UUID.data.bytes, sizeof(charId->bytes)); - memset(svcId->bytes, 0, sizeof(svcId->bytes)); - - // Expand service UUID back to 16-byte long as that's what the BLE Layer expects - // this is a buffer pre-filled with BLE service UUID Base - // byte 0 to 3 are reserved for shorter versions of BLE service UUIDs - // For 4-byte service UUIDs, all bytes from 0 to 3 are used - // For 2-byte service UUIDs, byte 0 and 1 shall be 0 - uint8_t serviceFullUUID[FullUUIDLength] - = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; - - switch (characteristic.service.UUID.data.length) { - case 2: - // copy the 2-byte service UUID onto the right offset - memcpy(serviceFullUUID + 2, characteristic.service.UUID.data.bytes, 2); - break; - case 4: - // flow through - case 16: - memcpy(serviceFullUUID, characteristic.service.UUID.data.bytes, characteristic.service.UUID.data.length); - break; - default: - // we're dead. we expect the data length to be the same (16-byte) across the board - ChipLogError(Ble, "Service UUIDs are incompatible"); - } - memcpy(svcId->bytes, serviceFullUUID, sizeof(svcId->bytes)); -} - - (void)setBleLayer:(chip::Ble::BleLayer *)bleLayer { _mBleLayer = bleLayer; diff --git a/src/platform/Darwin/BlePlatformDelegateImpl.mm b/src/platform/Darwin/BlePlatformDelegateImpl.mm index 1fcd1585b274c7..1b5fe31c58f3bf 100644 --- a/src/platform/Darwin/BlePlatformDelegateImpl.mm +++ b/src/platform/Darwin/BlePlatformDelegateImpl.mm @@ -30,7 +30,7 @@ #include #include -#import "MTRUUIDHelper.h" +#import using namespace chip::Ble; using chip::System::PacketBufferHandle; @@ -38,60 +38,45 @@ namespace chip { namespace DeviceLayer { namespace Internal { - CHIP_ERROR BlePlatformDelegateImpl::SubscribeCharacteristic( - BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) - { - CHIP_ERROR err = BLE_ERROR_GATT_SUBSCRIBE_FAILED; - - if (nullptr == svcId || nullptr == charId) { - return err; - } - - CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId]; - CBUUID * characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes length:sizeof(charId->bytes)]]; - CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); - for (CBService * service in peripheral.services) { - if ([service.UUID.data isEqualToData:serviceId.data]) { - for (CBCharacteristic * characteristic in service.characteristics) { - if ([characteristic.UUID.data isEqualToData:characteristicId.data]) { - err = CHIP_NO_ERROR; - [peripheral setNotifyValue:true forCharacteristic:characteristic]; - break; + namespace { + CBCharacteristic * FindCharacteristic(CBPeripheral * peripheral, const ChipBleUUID * svcId, const ChipBleUUID * charId) + { + VerifyOrReturnValue(svcId != nullptr && charId != nullptr, nil); + CBUUID * cbSvcId = CBUUIDFromBleUUID(*svcId); + for (CBService * service in peripheral.services) { + if ([service.UUID isEqual:cbSvcId]) { + CBUUID * cbCharId = CBUUIDFromBleUUID(*charId); + for (CBCharacteristic * characteristic in service.characteristics) { + if ([characteristic.UUID isEqual:cbCharId]) { + return characteristic; + } } + break; } } + return nil; } + } - return err; + CHIP_ERROR BlePlatformDelegateImpl::SubscribeCharacteristic( + BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) + { + CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); + CBCharacteristic * characteristic = FindCharacteristic(peripheral, svcId, charId); + VerifyOrReturnError(characteristic != nil, BLE_ERROR_GATT_SUBSCRIBE_FAILED); + [peripheral setNotifyValue:YES forCharacteristic:characteristic]; + return CHIP_NO_ERROR; } CHIP_ERROR BlePlatformDelegateImpl::UnsubscribeCharacteristic( BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId) { - CHIP_ERROR err = BLE_ERROR_GATT_UNSUBSCRIBE_FAILED; - if (nullptr == svcId || nullptr == charId) { - return err; - } - - CBUUID * serviceId = [MTRUUIDHelper GetShortestServiceUUID:svcId]; - CBUUID * characteristicId = characteristicId = [CBUUID UUIDWithData:[NSData dataWithBytes:charId->bytes - length:sizeof(charId->bytes)]]; CBPeripheral * peripheral = CBPeripheralFromBleConnObject(connObj); - - for (CBService * service in peripheral.services) { - if ([service.UUID.data isEqualToData:serviceId.data]) { - for (CBCharacteristic * characteristic in service.characteristics) { - if ([characteristic.UUID.data isEqualToData:characteristicId.data]) { - err = CHIP_NO_ERROR; - [peripheral setNotifyValue:false forCharacteristic:characteristic]; - break; - } - } - } - } - - return err; + CBCharacteristic * characteristic = FindCharacteristic(peripheral, svcId, charId); + VerifyOrReturnError(characteristic != nil, BLE_ERROR_GATT_UNSUBSCRIBE_FAILED); + [peripheral setNotifyValue:NO forCharacteristic:characteristic]; + return CHIP_NO_ERROR; } CHIP_ERROR BlePlatformDelegateImpl::CloseConnection(BLE_CONNECTION_OBJECT connObj) @@ -126,32 +111,12 @@ CHIP_ERROR BlePlatformDelegateImpl::SendWriteRequest( BLE_CONNECTION_OBJECT connObj, const ChipBleUUID * svcId, const ChipBleUUID * charId, PacketBufferHandle pBuf) { - CHIP_ERROR err = BLE_ERROR_GATT_WRITE_FAILED; - if (nullptr == svcId || nullptr == charId || pBuf.IsNull()) { - return err; - } - - 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 = CBPeripheralFromBleConnObject(connObj); - - for (CBService * service in peripheral.services) { - if ([service.UUID.data isEqualToData:serviceId.data]) { - for (CBCharacteristic * characteristic in service.characteristics) { - if ([characteristic.UUID.data isEqualToData:characteristicId.data]) { - err = CHIP_NO_ERROR; - [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; - break; - } - } - } - } - - // Going out of scope releases delegate's reference to pBuf. pBuf will be freed when both platform delegate and Chip - // stack free their references to it. We release pBuf's reference here since its payload bytes were copied into a new - // NSData object - return err; + CBCharacteristic * characteristic = FindCharacteristic(peripheral, svcId, charId); + VerifyOrReturnError(characteristic != nil && !pBuf.IsNull(), BLE_ERROR_GATT_WRITE_FAILED); + NSData * data = [NSData dataWithBytes:pBuf->Start() length:pBuf->DataLength()]; // copies data, pBuf can be freed + [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; + return CHIP_NO_ERROR; } } // namespace Internal diff --git a/src/platform/Darwin/BleUtils.h b/src/platform/Darwin/BleUtils.h index bca0d2a746724d..13d7b4e88aecfb 100644 --- a/src/platform/Darwin/BleUtils.h +++ b/src/platform/Darwin/BleUtils.h @@ -14,9 +14,12 @@ * limitations under the License. */ +#pragma once + #include @class CBPeripheral; +@class CBUUID; namespace chip { namespace DeviceLayer { @@ -32,6 +35,12 @@ namespace DeviceLayer { return (__bridge void *) peripheral; } + // Creates a CBUUID from a ChipBleUUID + CBUUID * CBUUIDFromBleUUID(Ble::ChipBleUUID const & uuid); + + // Creates a ChipBleUUID from a CBUUID, expanding 16 or 32 bit UUIDs if necessary. + Ble::ChipBleUUID BleUUIDFromCBUUD(CBUUID * uuid); + } } } diff --git a/src/platform/Darwin/BleUtils.mm b/src/platform/Darwin/BleUtils.mm new file mode 100644 index 00000000000000..a8b4791aa95c3b --- /dev/null +++ b/src/platform/Darwin/BleUtils.mm @@ -0,0 +1,65 @@ +/** + * 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 "BleUtils.h" + +#import +#import + +using namespace chip::Ble; + +namespace chip { +namespace DeviceLayer { + namespace Internal { + + CBUUID * CBUUIDFromBleUUID(ChipBleUUID const & uuid) + { + return [CBUUID UUIDWithData:[NSData dataWithBytes:uuid.bytes length:sizeof(uuid.bytes)]]; + } + + ChipBleUUID BleUUIDFromCBUUD(CBUUID * uuid) + { + // CBUUID handles the expansion to 128 bit automatically internally, + // but doesn't expose the expanded UUID in the `data` property, so + // we have to re-implement the expansion here. + constexpr ChipBleUUID baseUuid = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB } }; + NSData * uuidData = uuid.data; + switch (uuidData.length) { + case 2: { + ChipBleUUID outUuid = baseUuid; + memcpy(outUuid.bytes + 2, uuidData.bytes, 2); + return outUuid; + } + case 4: { + ChipBleUUID outUuid = baseUuid; + memcpy(outUuid.bytes, uuidData.bytes, 4); + return outUuid; + } + case 16: { + ChipBleUUID outUuid; + memcpy(outUuid.bytes, uuidData.bytes, 16); + return outUuid; + } + default: { + NSCAssert(NO, @"Invalid CBUUID.data: %@", uuidData); + return ChipBleUUID {}; + } + } + } + + } +} +} diff --git a/src/platform/Darwin/MTRUUIDHelper.h b/src/platform/Darwin/MTRUUIDHelper.h deleted file mode 100644 index faa0af95809f88..00000000000000 --- a/src/platform/Darwin/MTRUUIDHelper.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * - * Copyright (c) 2020 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. - */ - -#pragma once - -#include - -#import - -@interface MTRUUIDHelper : NSObject -+ (CBUUID *)GetShortestServiceUUID:(const chip::Ble::ChipBleUUID *)svcId; -@end diff --git a/src/platform/Darwin/MTRUUIDHelperImpl.mm b/src/platform/Darwin/MTRUUIDHelperImpl.mm deleted file mode 100644 index 8f1b863a760cf8..00000000000000 --- a/src/platform/Darwin/MTRUUIDHelperImpl.mm +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2015-2017 Nest Labs, Inc. - * - * 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. - */ - -#if !__has_feature(objc_arc) -#error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC). -#endif - -#import "MTRUUIDHelper.h" - -@implementation MTRUUIDHelper - -+ (CBUUID *)GetShortestServiceUUID:(const chip::Ble::ChipBleUUID *)svcId -{ - // shorten the 16-byte UUID reported by BLE Layer to shortest possible, 2 or 4 bytes - // this is the BLE Service UUID Base. If a 16-byte service UUID partially matches with these 12 bytes, - // it can be shortened to 2 or 4 bytes. - static const uint8_t bleBaseUUID[12] = { 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; - if (0 == memcmp(svcId->bytes + 4, bleBaseUUID, sizeof(bleBaseUUID))) { - // okay, let's try to shorten it - if ((0 == svcId->bytes[0]) && (0 == svcId->bytes[1])) { - // the highest 2 bytes are both 0, so we just need 2 bytes - return [CBUUID UUIDWithData:[NSData dataWithBytes:(svcId->bytes + 2) length:2]]; - } // we need to use 4 bytes - return [CBUUID UUIDWithData:[NSData dataWithBytes:svcId->bytes length:4]]; - } - // it cannot be shortened as it doesn't match with the BLE Service UUID Base - return [CBUUID UUIDWithData:[NSData dataWithBytes:svcId->bytes length:16]]; -} -@end