Skip to content

Commit

Permalink
Fix iOS CHIPTool commissioning (#13148)
Browse files Browse the repository at this point in the history
* Fix iOS CHIPTool commissioning

* Fix style

* Fix rebase

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Mar 3, 2022
1 parent b9fd14b commit 1051074
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CHIPPersistentStorageDelegate>

Expand Down
28 changes: 22 additions & 6 deletions src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 1051074

Please sign in to comment.