Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iOS CHIPTool commissioning #13148

Merged
merged 4 commits into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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