35
35
#include < setup_payload/SetupPayload.h>
36
36
#include < tracing/metric_event.h>
37
37
38
- #import " MTRUUIDHelper.h"
38
+ #import < CoreBluetooth/CoreBluetooth.h>
39
+
39
40
#import " PlatformMetricKeys.h"
40
41
41
42
using namespace chip ::Ble;
@@ -60,7 +61,6 @@ @interface BleConnection : NSObject <CBCentralManagerDelegate, CBPeripheralDeleg
60
61
@property (strong , nonatomic ) dispatch_queue_t workQueue; // the CHIP work queue
61
62
@property (strong , nonatomic ) CBCentralManager * centralManager;
62
63
@property (strong , nonatomic ) CBPeripheral * peripheral;
63
- @property (strong , nonatomic ) CBUUID * shortServiceUUID;
64
64
@property (nonatomic , readonly , nullable ) dispatch_source_t timer;
65
65
@property (nonatomic , readonly ) BleConnectionMode currentMode;
66
66
@property (strong , nonatomic ) NSMutableDictionary <CBPeripheral *, NSDictionary *> * cachedPeripherals;
@@ -212,13 +212,15 @@ @interface BleConnection ()
212
212
@property (nonatomic , readonly ) int32_t totalDevicesRemoved;
213
213
@end
214
214
215
- @implementation BleConnection
215
+ @implementation BleConnection {
216
+ CBUUID * _chipServiceUUID;
217
+ }
216
218
217
219
- (instancetype )init
218
220
{
219
221
self = [super init ];
220
222
if (self) {
221
- self. shortServiceUUID = [MTRUUIDHelper GetShortestServiceUUID: & chip: :Ble: :CHIP_BLE_SVC_ID] ;
223
+ _chipServiceUUID = CBUUIDFromBleUUID ( chip::Ble::CHIP_BLE_SVC_ID) ;
222
224
_workQueue = chip::DeviceLayer::PlatformMgrImpl ().GetWorkQueue ();
223
225
_centralManager = [CBCentralManager alloc ];
224
226
_found = false ;
@@ -338,15 +340,7 @@ - (void)centralManager:(CBCentralManager *)central
338
340
advertisementData : (NSDictionary *)advertisementData
339
341
RSSI : (NSNumber *)RSSI
340
342
{
341
- NSDictionary * servicesData = [advertisementData objectForKey: CBAdvertisementDataServiceDataKey];
342
- NSData * serviceData;
343
- for (CBUUID * serviceUUID in servicesData) {
344
- if ([serviceUUID.data isEqualToData: _shortServiceUUID.data]) {
345
- serviceData = [servicesData objectForKey: serviceUUID];
346
- break ;
347
- }
348
- }
349
-
343
+ NSData * serviceData = advertisementData[CBAdvertisementDataServiceDataKey][_chipServiceUUID];
350
344
if (!serviceData) {
351
345
return ;
352
346
}
@@ -431,7 +425,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err
431
425
MATTER_LOG_METRIC_END (kMetricBLEDiscoveredServices , CHIP_ERROR (chip::ChipError::Range::kOS , static_cast <uint32_t >(error.code )));
432
426
433
427
for (CBService * service in peripheral.services ) {
434
- if ([service.UUID.data isEqualToData: _shortServiceUUID.data ] && !self.found ) {
428
+ if ([service.UUID isEqual: _chipServiceUUID ] && !self.found ) {
435
429
MATTER_LOG_METRIC_BEGIN (kMetricBLEDiscoveredCharacteristics );
436
430
[peripheral discoverCharacteristics: nil forService: service];
437
431
self.found = true ;
@@ -464,9 +458,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
464
458
error : (NSError *)error
465
459
{
466
460
if (nil == error) {
467
- chip::Ble::ChipBleUUID svcId;
468
- chip::Ble::ChipBleUUID charId;
469
- [BleConnection fillServiceWithCharacteristicUuids: characteristic svcId: &svcId charId: &charId];
461
+ ChipBleUUID svcId = BleUUIDFromCBUUD (characteristic.service .UUID );
462
+ ChipBleUUID charId = BleUUIDFromCBUUD (characteristic.UUID );
470
463
_mBleLayer->HandleWriteConfirmation (BleConnObjectFromCBPeripheral (peripheral), &svcId, &charId);
471
464
} else {
472
465
ChipLogError (
@@ -483,10 +476,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
483
476
bool isNotifying = characteristic.isNotifying ;
484
477
485
478
if (nil == error) {
486
- chip::Ble::ChipBleUUID svcId;
487
- chip::Ble::ChipBleUUID charId;
488
- [BleConnection fillServiceWithCharacteristicUuids: characteristic svcId: &svcId charId: &charId];
489
-
479
+ ChipBleUUID svcId = BleUUIDFromCBUUD (characteristic.service .UUID );
480
+ ChipBleUUID charId = BleUUIDFromCBUUD (characteristic.UUID );
490
481
if (isNotifying) {
491
482
_mBleLayer->HandleSubscribeComplete (BleConnObjectFromCBPeripheral (peripheral), &svcId, &charId);
492
483
} else {
@@ -513,9 +504,8 @@ - (void)peripheral:(CBPeripheral *)peripheral
513
504
error : (NSError *)error
514
505
{
515
506
if (nil == error) {
516
- chip::Ble::ChipBleUUID svcId;
517
- chip::Ble::ChipBleUUID charId;
518
- [BleConnection fillServiceWithCharacteristicUuids: characteristic svcId: &svcId charId: &charId];
507
+ ChipBleUUID svcId = BleUUIDFromCBUUD (characteristic.service .UUID );
508
+ ChipBleUUID charId = BleUUIDFromCBUUD (characteristic.UUID );
519
509
auto * value = characteristic.value ; // read immediately before dispatching
520
510
521
511
// build a inet buffer from the rxEv and send to blelayer.
@@ -603,7 +593,7 @@ - (void)startScanning
603
593
[self _resetCounters ];
604
594
605
595
auto scanOptions = @{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES };
606
- [_centralManager scanForPeripheralsWithServices: @[ _shortServiceUUID ] options: scanOptions];
596
+ [_centralManager scanForPeripheralsWithServices: @[ _chipServiceUUID ] options: scanOptions];
607
597
}
608
598
609
599
- (void )stopScanning
@@ -782,55 +772,6 @@ - (void)removePeripheralsFromCache
782
772
}
783
773
}
784
774
785
- /* *
786
- * private static method to copy service and characteristic UUIDs from CBCharacteristic to a pair of ChipBleUUID objects.
787
- * this is used in calls into Chip layer to decouple it from CoreBluetooth
788
- *
789
- * @param[in] characteristic the source characteristic
790
- * @param[in] svcId the destination service UUID
791
- * @param[in] charId the destination characteristic UUID
792
- *
793
- */
794
- + (void )fillServiceWithCharacteristicUuids : (CBCharacteristic *)characteristic
795
- svcId : (chip::Ble::ChipBleUUID *)svcId
796
- charId : (chip::Ble::ChipBleUUID *)charId
797
- {
798
- static const size_t FullUUIDLength = 16 ;
799
- if ((FullUUIDLength != sizeof (charId->bytes )) || (FullUUIDLength != sizeof (svcId->bytes ))
800
- || (FullUUIDLength != characteristic.UUID .data .length )) {
801
- // we're dead. we expect the data length to be the same (16-byte) across the board
802
- ChipLogError (Ble, " UUID of characteristic is incompatible" );
803
- return ;
804
- }
805
-
806
- memcpy (charId->bytes , characteristic.UUID .data .bytes , sizeof (charId->bytes ));
807
- memset (svcId->bytes , 0 , sizeof (svcId->bytes ));
808
-
809
- // Expand service UUID back to 16-byte long as that's what the BLE Layer expects
810
- // this is a buffer pre-filled with BLE service UUID Base
811
- // byte 0 to 3 are reserved for shorter versions of BLE service UUIDs
812
- // For 4-byte service UUIDs, all bytes from 0 to 3 are used
813
- // For 2-byte service UUIDs, byte 0 and 1 shall be 0
814
- uint8_t serviceFullUUID[FullUUIDLength]
815
- = { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x10 , 0x00 , 0x80 , 0x00 , 0x00 , 0x80 , 0x5F , 0x9B , 0x34 , 0xFB };
816
-
817
- switch (characteristic.service .UUID .data .length ) {
818
- case 2 :
819
- // copy the 2-byte service UUID onto the right offset
820
- memcpy (serviceFullUUID + 2 , characteristic.service .UUID .data .bytes , 2 );
821
- break ;
822
- case 4 :
823
- // flow through
824
- case 16 :
825
- memcpy (serviceFullUUID, characteristic.service .UUID .data .bytes , characteristic.service .UUID .data .length );
826
- break ;
827
- default :
828
- // we're dead. we expect the data length to be the same (16-byte) across the board
829
- ChipLogError (Ble, " Service UUIDs are incompatible" );
830
- }
831
- memcpy (svcId->bytes , serviceFullUUID, sizeof (svcId->bytes ));
832
- }
833
-
834
775
- (void )setBleLayer : (chip::Ble::BleLayer *)bleLayer
835
776
{
836
777
_mBleLayer = bleLayer;
0 commit comments