Skip to content

Commit

Permalink
[darwin] Add more logging when a device with a matching Matter UUID i…
Browse files Browse the repository at this point in the history
…s discovered over BLE but some parts of the service data does not match (#26832)
  • Loading branch information
vivien-apple authored and pull[bot] committed Oct 23, 2023
1 parent daa5f29 commit e937cf7
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/platform/Darwin/BleConnectionDelegateImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
}
}
Expand Down

0 comments on commit e937cf7

Please sign in to comment.