Skip to content

Commit

Permalink
Improve Darwin framework APIs to follow Objective-C conventions bette…
Browse files Browse the repository at this point in the history
…r. (#11957)

Specific changes:

1) Rename Payload structs to Params structs.
2) For commands with no fields, have the name be fooWithCompletionHandler and
   no params struct.
3) For commands with only optional fields, allow the params struct to be nil.
4) Rename "responseHandler" to "completionHandler" so that we get the
   behavior described in
   https://developer.apple.com/documentation/swift/calling_objective-c_apis_asynchronously
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Oct 16, 2023
1 parent b86d77b commit 3822483
Show file tree
Hide file tree
Showing 17 changed files with 3,758 additions and 4,773 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ - (IBAction)bind:(id)sender
if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
if (chipDevice) {
CHIPBinding * cluster = [[CHIPBinding alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
__auto_type * payload = [[CHIPBindingClusterBindPayload alloc] init];
payload.NodeId = @(nodeId);
payload.GroupId = @(groupId);
payload.EndpointId = @(endpointId);
payload.ClusterId = @(clusterId);
[cluster bind:payload
responseHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
NSString * resultString = (error == nil)
? @"Bind command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
NSLog(resultString, nil);
}];
__auto_type * params = [[CHIPBindingClusterBindParams alloc] init];
params.nodeId = @(nodeId);
params.groupId = @(groupId);
params.endpointId = @(endpointId);
params.clusterId = @(clusterId);
[cluster bindWithParams:params
completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
NSString * resultString = (error == nil)
? @"Bind command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
NSLog(resultString, nil);
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
Expand All @@ -171,18 +171,18 @@ - (IBAction)unbind:(id)sender
if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
if (chipDevice) {
CHIPBinding * cluster = [[CHIPBinding alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
__auto_type * payload = [[CHIPBindingClusterUnbindPayload alloc] init];
payload.NodeId = @(nodeId);
payload.GroupId = @(groupId);
payload.EndpointId = @(endpointId);
payload.ClusterId = @(clusterId);
[cluster unbind:payload
responseHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
NSString * resultString = (error == nil)
? @"Unbind command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
NSLog(resultString, nil);
}];
__auto_type * params = [[CHIPBindingClusterUnbindParams alloc] init];
params.nodeId = @(nodeId);
params.groupId = @(groupId);
params.endpointId = @(endpointId);
params.clusterId = @(clusterId);
[cluster unbindWithParams:params
completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
NSString * resultString = (error == nil)
? @"Unbind command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
NSLog(resultString, nil);
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,11 @@ - (IBAction)sendMessage:(id)sender
CHIPBasic * cluster = [[CHIPBasic alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
[self updateResult:@"MfgSpecificPing command sent..."];

[cluster mfgSpecificPing:[[CHIPBasicClusterMfgSpecificPingPayload alloc] init]
responseHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error == nil)
? @"MfgSpecificPing command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
[self updateResult:resultString];
}];
[cluster mfgSpecificPingWithCompletionHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error == nil) ? @"MfgSpecificPing command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
[self updateResult:resultString];
}];
} else {
[self updateResult:@"Failed to establish a connection with the device"];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,35 +348,37 @@ - (IBAction)updateFabricLabelButtonPressed:(id)sender
CHIPOperationalCredentials * cluster =
[[CHIPOperationalCredentials alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
[self updateResult:[NSString stringWithFormat:@"updateFabricLabel command sent."] isError:NO];
__auto_type * payload = [[CHIPOperationalCredentialsClusterUpdateFabricLabelPayload alloc] init];
payload.Label = label;
__auto_type * params = [[CHIPOperationalCredentialsClusterUpdateFabricLabelParams alloc] init];
params.label = label;

[cluster
updateFabricLabel:payload
responseHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"Got back error trying to updateFabricLabel %@", error);
updateFabricLabelWithParams:params
completionHandler:^(NSError * _Nullable error, NSDictionary * _Nullable values) {
dispatch_async(dispatch_get_main_queue(), ^{
self->_updateFabricLabelTextField.text = @"";
[self
updateResult:[NSString stringWithFormat:@"Command updateFabricLabel failed with error %@",
error]
isError:YES];
});
} else {
NSLog(@"Successfully updated the label: %@", values);
dispatch_async(dispatch_get_main_queue(), ^{
self->_updateFabricLabelTextField.text = @"";
[self updateResult:[NSString stringWithFormat:
if (error) {
NSLog(@"Got back error trying to updateFabricLabel %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
self->_updateFabricLabelTextField.text = @"";
[self updateResult:[NSString
stringWithFormat:
@"Command updateFabricLabel failed with error %@", error]
isError:YES];
});
} else {
NSLog(@"Successfully updated the label: %@", values);
dispatch_async(dispatch_get_main_queue(), ^{
self->_updateFabricLabelTextField.text = @"";
[self
updateResult:[NSString
stringWithFormat:
@"Command updateFabricLabel succeeded to update label to %@",
label]
isError:NO];
[self fetchFabricsList];
isError:NO];
[self fetchFabricsList];
});
}
});
}
});
}];
}];
} else {
[self updateResult:[NSString stringWithFormat:@"Failed to establish a connection with the device"] isError:YES];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,12 @@ - (IBAction)onButtonTapped:(id)sender
CHIPOnOff * onOff = [[CHIPOnOff alloc] initWithDevice:chipDevice
endpoint:endpoint
queue:dispatch_get_main_queue()];
[onOff on:[[CHIPOnOffClusterOnPayload alloc] init]
responseHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"On command success";
[self updateResult:resultString];
}];
[onOff onWithCompletionHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"On command success";
[self updateResult:resultString];
}];
} else {
[self updateResult:[NSString stringWithFormat:@"Failed to establish a connection with the device"]];
}
Expand All @@ -272,13 +271,12 @@ - (IBAction)offButtonTapped:(id)sender
CHIPOnOff * onOff = [[CHIPOnOff alloc] initWithDevice:chipDevice
endpoint:endpoint
queue:dispatch_get_main_queue()];
[onOff off:[[CHIPOnOffClusterOffPayload alloc] init]
responseHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"Off command success";
[self updateResult:resultString];
}];
[onOff offWithCompletionHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"Off command success";
[self updateResult:resultString];
}];
} else {
[self updateResult:[NSString stringWithFormat:@"Failed to establish a connection with the device"]];
}
Expand All @@ -302,13 +300,12 @@ - (IBAction)toggleButtonTapped:(id)sender
CHIPOnOff * onOff = [[CHIPOnOff alloc] initWithDevice:chipDevice
endpoint:endpoint
queue:dispatch_get_main_queue()];
[onOff toggle:[[CHIPOnOffClusterTogglePayload alloc] init]
responseHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"Toggle command success";
[self updateResult:resultString];
}];
[onOff toggleWithCompletionHandler:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error != nil)
? [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code]
: @"Toggle command success";
[self updateResult:resultString];
}];
} else {
[self updateResult:[NSString stringWithFormat:@"Failed to establish a connection with the device"]];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,17 +541,17 @@ - (void)addWiFiNetwork:(NSString *)ssid password:(NSString *)password
self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice
endpoint:0
queue:dispatch_get_main_queue()];
__auto_type * payload = [[CHIPNetworkCommissioningClusterAddWiFiNetworkPayload alloc] init];
payload.Ssid = [ssid dataUsingEncoding:NSUTF8StringEncoding];
payload.Credentials = [password dataUsingEncoding:NSUTF8StringEncoding];
payload.Breadcrumb = @(0);
payload.TimeoutMs = @(3000);
__auto_type * params = [[CHIPNetworkCommissioningClusterAddWiFiNetworkParams alloc] init];
params.ssid = [ssid dataUsingEncoding:NSUTF8StringEncoding];
params.credentials = [password dataUsingEncoding:NSUTF8StringEncoding];
params.breadcrumb = @(0);
params.timeoutMs = @(3000);

__weak typeof(self) weakSelf = self;
[self->_cluster addWiFiNetwork:payload
responseHandler:^(NSError * error, NSDictionary * values) {
[weakSelf onAddNetworkResponse:error isWiFi:YES];
}];
[self->_cluster addWiFiNetworkWithParams:params
completionHandler:^(NSError * error, NSDictionary * values) {
[weakSelf onAddNetworkResponse:error isWiFi:YES];
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
Expand All @@ -569,16 +569,16 @@ - (void)addThreadNetwork:(NSData *)threadDataSet
self.cluster = [[CHIPNetworkCommissioning alloc] initWithDevice:chipDevice
endpoint:0
queue:dispatch_get_main_queue()];
__auto_type * payload = [[CHIPNetworkCommissioningClusterAddThreadNetworkPayload alloc] init];
payload.OperationalDataset = threadDataSet;
payload.Breadcrumb = @(0);
payload.TimeoutMs = @(3000);
__auto_type * params = [[CHIPNetworkCommissioningClusterAddThreadNetworkParams alloc] init];
params.operationalDataset = threadDataSet;
params.breadcrumb = @(0);
params.timeoutMs = @(3000);

__weak typeof(self) weakSelf = self;
[self->_cluster addThreadNetwork:payload
responseHandler:^(NSError * error, NSDictionary * values) {
[weakSelf onAddNetworkResponse:error isWiFi:NO];
}];
[self->_cluster addThreadNetworkWithParams:params
completionHandler:^(NSError * error, NSDictionary * values) {
[weakSelf onAddNetworkResponse:error isWiFi:NO];
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
Expand All @@ -596,22 +596,22 @@ - (void)onAddNetworkResponse:(NSError *)error isWiFi:(BOOL)isWiFi
return;
}

__auto_type * payload = [[CHIPNetworkCommissioningClusterEnableNetworkPayload alloc] init];
__auto_type * params = [[CHIPNetworkCommissioningClusterEnableNetworkParams alloc] init];
if (isWiFi) {
NSString * ssid = CHIPGetDomainValueForKey(kCHIPToolDefaultsDomain, kNetworkSSIDDefaultsKey);
payload.NetworkID = [ssid dataUsingEncoding:NSUTF8StringEncoding];
params.networkID = [ssid dataUsingEncoding:NSUTF8StringEncoding];
} else {
uint8_t tempThreadNetworkId[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
payload.NetworkID = [NSData dataWithBytes:tempThreadNetworkId length:sizeof(tempThreadNetworkId)];
params.networkID = [NSData dataWithBytes:tempThreadNetworkId length:sizeof(tempThreadNetworkId)];
}
payload.Breadcrumb = @(0);
payload.TimeoutMs = @(3000);
params.breadcrumb = @(0);
params.timeoutMs = @(3000);

__weak typeof(self) weakSelf = self;
[_cluster enableNetwork:payload
responseHandler:^(NSError * err, NSDictionary * values) {
[weakSelf onEnableNetworkResponse:err];
}];
[_cluster enableNetworkWithParams:params
completionHandler:^(NSError * err, NSDictionary * values) {
[weakSelf onEnableNetworkResponse:err];
}];
}

- (void)onEnableNetworkResponse:(NSError *)error
Expand Down
Loading

0 comments on commit 3822483

Please sign in to comment.