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

Update Darwin framework to use GetConnectedDevice #8011

Merged
merged 2 commits into from
Jul 2, 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 @@ -31,8 +31,9 @@ void CHIPSetDomainValueForKey(NSString * domain, NSString * key, id value);
void CHIPRemoveDomainValueForKey(NSString * domain, NSString * key);
uint64_t CHIPGetNextAvailableDeviceID(void);
void CHIPSetNextAvailableDeviceID(uint64_t id);
CHIPDevice * CHIPGetPairedDevice(void);
CHIPDevice * CHIPGetPairedDeviceWithID(uint64_t id);
BOOL CHIPIsDevicePaired(uint64_t id);
BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler);
BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallback completionHandler);
void CHIPUnpairDeviceWithID(uint64_t deviceId);

@interface CHIPToolPersistentStorageDelegate : NSObject <CHIPPersistentStorageDelegate>
Expand Down
31 changes: 22 additions & 9 deletions src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,46 @@ void CHIPSetNextAvailableDeviceID(uint64_t id)
return controller;
}

CHIPDevice * CHIPGetPairedDevice(void)
BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler)
{
CHIPDeviceController * controller = InitializeCHIP();

CHIPDevice * device = nil;
uint64_t deviceId = CHIPGetNextAvailableDeviceID();
if (deviceId > 1) {
// Let's use the last device that was paired
deviceId--;
NSError * error;
device = [controller getPairedDevice:deviceId error:&error];
return [controller getConnectedDevice:deviceId
completionHandler:completionHandler
queue:dispatch_get_main_queue()
error:&error];
}

return device;
return NO;
}

CHIPDevice * CHIPGetPairedDeviceWithID(uint64_t deviceId)
BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallback completionHandler)
{
CHIPDeviceController * controller = InitializeCHIP();

NSError * error;
CHIPDevice * device = [controller getPairedDevice:deviceId error:&error];
return [controller getConnectedDevice:deviceId
completionHandler:completionHandler
queue:dispatch_get_main_queue()
error:&error];
}

BOOL CHIPIsDevicePaired(uint64_t deviceId)
{
CHIPDeviceController * controller = InitializeCHIP();

NSError * error;
bool paired = [controller isDevicePaired:deviceId error:&error];
if (error.code != CHIPSuccess) {
NSLog(@"Got back error retrieve device with deviceId %llu", deviceId);
return nil;
NSLog(@"Error retrieving device info for deviceId %llu", deviceId);
paired = NO;
}
return device;
return paired;
pan-apple marked this conversation as resolved.
Show resolved Hide resolved
}

void CHIPUnpairDeviceWithID(uint64_t deviceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ @interface BindingsViewController ()
@property (nonatomic, strong) UITextField * groupIDTextField;
@property (nonatomic, strong) UITextField * endpointIDTextField;
@property (nonatomic, strong) UITextField * clusterIDTextField;

@property (nonatomic, strong) CHIPBinding * cluster;
@end

@implementation BindingsViewController
Expand All @@ -37,8 +35,6 @@ - (void)viewDidLoad

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];

self.cluster = [[CHIPBinding alloc] initWithDevice:CHIPGetPairedDevice() endpoint:0 queue:dispatch_get_main_queue()];
}

- (void)dismissKeyboard
Expand Down Expand Up @@ -140,15 +136,27 @@ - (IBAction)bind:(id)sender
int groupId = [_groupIDTextField.text intValue];
int clusterId = [_clusterIDTextField.text intValue];

[self.cluster bind:nodeId
groupId:groupId
endpointId:endpointId
clusterId:clusterId
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);
}];
if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
if (chipDevice) {
CHIPBinding * cluster = [[CHIPBinding alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
[cluster bind:nodeId
groupId:groupId
endpointId:endpointId
clusterId:clusterId
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);
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
})) {
NSLog(@"Status: Waiting for connection with the device");
} else {
NSLog(@"Status: Failed to trigger the connection with the device");
}
}

- (IBAction)unbind:(id)sender
Expand All @@ -158,15 +166,27 @@ - (IBAction)unbind:(id)sender
int groupId = [_groupIDTextField.text intValue];
int clusterId = [_clusterIDTextField.text intValue];

[self.cluster unbind:nodeId
groupId:groupId
endpointId:endpointId
clusterId:clusterId
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);
}];
if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
if (chipDevice) {
CHIPBinding * cluster = [[CHIPBinding alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
[cluster unbind:nodeId
groupId:groupId
endpointId:endpointId
clusterId:clusterId
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);
}];
} else {
NSLog(@"Status: Failed to establish a connection with the device");
}
})) {
NSLog(@"Status: Waiting for connection with the device");
} else {
NSLog(@"Status: Failed to trigger the connection with the device");
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (void)refreshDeviceList
uint64_t nextDeviceID = CHIPGetNextAvailableDeviceID();
_deviceList = [NSMutableArray new];
for (uint64_t i = 0; i < nextDeviceID; i++) {
if (CHIPGetPairedDeviceWithID(i) != nil) {
if (CHIPIsDevicePaired(i)) {
[_deviceList addObject:[@(i) stringValue]];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ @interface EchoViewController ()
@property (nonatomic, strong) UILabel * resultLabel;
@property (nonatomic, strong) UIStackView * stackView;

@property (readwrite) CHIPBasic * cluster;

@end

@implementation EchoViewController
Expand All @@ -44,8 +42,6 @@ - (void)viewDidLoad
// listen for taps to dismiss the keyboard
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];

self.cluster = [[CHIPBasic alloc] initWithDevice:CHIPGetPairedDevice() endpoint:0 queue:dispatch_get_main_queue()];
}

- (void)dismissKeyboard
Expand Down Expand Up @@ -110,22 +106,29 @@ - (void)updateResult:(NSString *)result

- (IBAction)sendMessage:(id)sender
{
if (!self.cluster) {
[self updateResult:@"Something went wrong. Cluster is not initialized."];
}

NSString * msg = [self.messageTextField text];
if (msg.length == 0) {
msg = [self.messageTextField placeholder];
}

[self updateResult:@"MfgSpecificPing command sent..."];

[self.cluster mfgSpecificPing:^(NSError * error, NSDictionary * values) {
NSString * resultString = (error == nil) ? @"MfgSpecificPing command: success!"
: [NSString stringWithFormat:@"An error occured: 0x%02lx", error.code];
[self updateResult:resultString];
}];
if (CHIPGetConnectedDevice(^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
if (chipDevice) {
CHIPBasic * cluster = [[CHIPBasic alloc] initWithDevice:chipDevice endpoint:0 queue:dispatch_get_main_queue()];
[self updateResult:@"MfgSpecificPing command sent..."];

[cluster mfgSpecificPing:^(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"];
}
})) {
[self updateResult:@"Waiting for connection with the device"];
} else {
[self updateResult:@"Failed to trigger the connection with the device"];
}
}

@end
Loading