From 10510740f63748d7c081be1fb9da9185e146e275 Mon Sep 17 00:00:00 2001 From: Sagar Dhawan Date: Mon, 20 Dec 2021 11:32:23 -0800 Subject: [PATCH] Fix iOS CHIPTool commissioning (#13148) * Fix iOS CHIPTool commissioning * Fix style * Fix rebase * Restyled by clang-format Co-authored-by: Restyled.io --- .../Framework Helpers/DefaultsUtils.h | 1 + .../Framework Helpers/DefaultsUtils.m | 28 ++++-- .../QRCode/QRCodeViewController.m | 90 ++++++++----------- 3 files changed, 60 insertions(+), 59 deletions(-) diff --git a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.h b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.h index 70c3431627eecf..44bcf732832763 100644 --- a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.h +++ b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.h @@ -35,6 +35,7 @@ BOOL CHIPIsDevicePaired(uint64_t id); BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler); BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallback completionHandler); void CHIPUnpairDeviceWithID(uint64_t deviceId); +CHIPDevice * CHIPGetDeviceBeingCommissioned(void); @interface CHIPToolPersistentStorageDelegate : NSObject diff --git a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m index e9e964dc57874b..9b4f09ce5fe64a 100644 --- a/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m +++ b/src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m @@ -76,18 +76,34 @@ void CHIPSetNextAvailableDeviceID(uint64_t id) return controller; } -BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler) +uint64_t CHIPGetLastPairedDeviceId(void) { - CHIPDeviceController * controller = InitializeCHIP(); - uint64_t deviceId = CHIPGetNextAvailableDeviceID(); if (deviceId > 1) { - // Let's use the last device that was paired deviceId--; - return [controller getConnectedDevice:deviceId queue:dispatch_get_main_queue() completionHandler:completionHandler]; } + return deviceId; +} + +BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler) +{ + CHIPDeviceController * controller = InitializeCHIP(); - return NO; + // Let's use the last device that was paired + uint64_t deviceId = CHIPGetLastPairedDeviceId(); + return [controller getConnectedDevice:deviceId queue:dispatch_get_main_queue() completionHandler:completionHandler]; +} + +CHIPDevice * CHIPGetDeviceBeingCommissioned(void) +{ + NSError * error; + CHIPDeviceController * controller = InitializeCHIP(); + CHIPDevice * device = [controller getDeviceBeingCommissioned:CHIPGetLastPairedDeviceId() error:&error]; + if (error) { + NSLog(@"Error retrieving device being commissioned for deviceId %llu", CHIPGetLastPairedDeviceId()); + return nil; + } + return device; } BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallback completionHandler) diff --git a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m index f0894ce787cf16..f38b88c5271553 100644 --- a/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m +++ b/src/darwin/CHIPTool/CHIPTool/View Controllers/QRCode/QRCodeViewController.m @@ -536,66 +536,50 @@ - (void)retrieveAndSendWifiCredentials - (void)addOrUpdateWiFiNetwork:(NSString *)ssid password:(NSString *)password { - if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) { - if (chipDevice) { - self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice - endpoint:0 - queue:dispatch_get_main_queue()]; - __auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateWiFiNetworkParams alloc] init]; - params.ssid = [ssid dataUsingEncoding:NSUTF8StringEncoding]; - params.credentials = [password dataUsingEncoding:NSUTF8StringEncoding]; - params.breadcrumb = @(0); - - __weak typeof(self) weakSelf = self; - [self->_cluster - addOrUpdateWiFiNetworkWithParams:params - completionHandler:^( - CHIPNetworkCommissioningClusterAddOrUpdateWiFiNetworkResponseParams * _Nullable response, - NSError * _Nullable error) { - // TODO: addOrUpdateWiFiNetworkWithParams - // returns status in its response, - // not via the NSError! - [weakSelf onAddNetworkResponse:error isWiFi:YES]; - }]; - } else { - NSLog(@"Status: Failed to establish a connection with the device"); - } - })) { - NSLog(@"Status: Waiting for connection with the device"); + CHIPDevice * chipDevice = CHIPGetDeviceBeingCommissioned(); + if (chipDevice) { + self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()]; + __auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateWiFiNetworkParams alloc] init]; + params.ssid = [ssid dataUsingEncoding:NSUTF8StringEncoding]; + params.credentials = [password dataUsingEncoding:NSUTF8StringEncoding]; + params.breadcrumb = @(0); + + __weak typeof(self) weakSelf = self; + [self->_cluster + addOrUpdateWiFiNetworkWithParams:params + completionHandler:^(CHIPNetworkCommissioningClusterNetworkConfigResponseParams * _Nullable response, + NSError * _Nullable error) { + // TODO: addWiFiNetworkWithParams + // returns status in its response, + // not via the NSError! + [weakSelf onAddNetworkResponse:error isWiFi:YES]; + }]; } else { - NSLog(@"Status: Failed to trigger the connection with the device"); + NSLog(@"Status: Failed to find a device being commissioned"); } } - (void)addOrUpdateThreadNetwork:(NSData *)threadDataSet { - if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) { - if (chipDevice) { - self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice - endpoint:0 - queue:dispatch_get_main_queue()]; - __auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateThreadNetworkParams alloc] init]; - params.operationalDataset = threadDataSet; - params.breadcrumb = @(0); - - __weak typeof(self) weakSelf = self; - [self->_cluster - addOrUpdateThreadNetworkWithParams:params - completionHandler:^( - CHIPNetworkCommissioningClusterAddOrUpdateThreadNetworkResponseParams * _Nullable response, - NSError * _Nullable error) { - // TODO: addOrUpdateThreadNetworkWithParams - // returns status in its response, - // not via the NSError! - [weakSelf onAddNetworkResponse:error isWiFi:NO]; - }]; - } else { - NSLog(@"Status: Failed to establish a connection with the device"); - } - })) { - NSLog(@"Status: Waiting for connection with the device"); + CHIPDevice * chipDevice = CHIPGetDeviceBeingCommissioned(); + if (chipDevice) { + self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()]; + __auto_type * params = [[CHIPNetworkCommissioningClusterAddOrUpdateThreadNetworkParams alloc] init]; + params.operationalDataset = threadDataSet; + params.breadcrumb = @(0); + + __weak typeof(self) weakSelf = self; + [self->_cluster + addOrUpdateThreadNetworkWithParams:params + completionHandler:^(CHIPNetworkCommissioningClusterNetworkConfigResponseParams * _Nullable response, + NSError * _Nullable error) { + // TODO: addThreadNetworkWithParams + // returns status in its response, + // not via the NSError! + [weakSelf onAddNetworkResponse:error isWiFi:NO]; + }]; } else { - NSLog(@"Status: Failed to trigger the connection with the device"); + NSLog(@"Status: Failed to find a device being commissioned"); } }