From 83f3eb33eb18054f8ddbfe970c6f1e15c3ed45d2 Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Thu, 25 May 2023 20:50:38 +0200 Subject: [PATCH] [darwin] Add more logging when a device with a matching Matter UUID is discovered over BLE but some parts of the service data does not match (#26832) --- .../Darwin/BleConnectionDelegateImpl.mm | 44 ++++++++++++++----- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index 5819e0fc35d95e..70b6605eb2fd5c 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -350,11 +350,6 @@ - (void)centralManager:(CBCentralManager *)central advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { - NSNumber * isConnectable = [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; - if ([isConnectable boolValue] == NO) { - return; - } - NSDictionary * servicesData = [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; NSData * serviceData; for (CBUUID * serviceUUID in servicesData) { @@ -364,26 +359,53 @@ - (void)centralManager:(CBCentralManager *)central } } - if (!serviceData || [serviceData length] != 8) { + if (!serviceData) { + return; + } + + NSNumber * isConnectable = [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; + if ([isConnectable boolValue] == NO) { + ChipLogError(Ble, "A device (%p) with a matching Matter UUID has been discovered but it is not connectable.", peripheral); return; } const uint8_t * bytes = (const uint8_t *) [serviceData bytes]; + if ([serviceData length] != 8) { + NSMutableString * hexString = [NSMutableString stringWithCapacity:([serviceData length] * 2)]; + for (NSUInteger i = 0; i < [serviceData length]; i++) { + [hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long) bytes[i]]]; + } + ChipLogError(Ble, + "A device (%p) with a matching Matter UUID has been discovered but the service data len does not match our expectation " + "(serviceData = %s)", + peripheral, [hexString UTF8String]); + return; + } + uint8_t opCode = bytes[0]; if (opCode != 0 && opCode != 1) { + ChipLogError(Ble, + "A device (%p) with a matching Matter UUID has been discovered but the service data opCode not match our expectation " + "(opCode = %u).", + peripheral, opCode); return; } uint16_t discriminator = (bytes[1] | (bytes[2] << 8)) & 0xfff; - if ([self isConnecting] and [self checkDiscriminator:discriminator]) { + if ([self isConnecting]) { + if (![self checkDiscriminator:discriminator]) { + ChipLogError(Ble, + "A device (%p) with a matching Matter UUID has been discovered but the service data discriminator not match our " + "expectation (discriminator = %u).", + peripheral, discriminator); + return; + } + ChipLogProgress(Ble, "Connecting to device %p with discriminator: %d", peripheral, discriminator); [self connect:peripheral]; [self stopScanning]; - return; - } - - if (![self isConnecting]) { + } else { [self addPeripheralToCache:peripheral data:serviceData]; } }