Skip to content

Commit

Permalink
Update Darwin framework API to get connected device (project-chip#8143)
Browse files Browse the repository at this point in the history
* Update Darwin framework API to get connected device

* fix test

* Update src/darwin/Framework/CHIP/CHIPDeviceController.mm

Co-authored-by: Boris Zbarsky <[email protected]>

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and Nikita committed Sep 23, 2021
1 parent 3fc581c commit c47f4f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/darwin/CHIPTool/CHIPTool/Framework Helpers/DefaultsUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ BOOL CHIPGetConnectedDevice(CHIPDeviceConnectionCallback completionHandler)
// Let's use the last device that was paired
deviceId--;
NSError * error;
return [controller getConnectedDevice:deviceId
completionHandler:completionHandler
queue:dispatch_get_main_queue()
error:&error];
return [controller getConnectedDevice:deviceId queue:dispatch_get_main_queue() completionHandler:completionHandler];
}

return NO;
Expand All @@ -99,10 +96,7 @@ BOOL CHIPGetConnectedDeviceWithID(uint64_t deviceId, CHIPDeviceConnectionCallbac
CHIPDeviceController * controller = InitializeCHIP();

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

BOOL CHIPIsDevicePaired(uint64_t deviceId)
Expand Down
3 changes: 1 addition & 2 deletions src/darwin/Framework/CHIP/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ typedef void (^CHIPDeviceConnectionCallback)(CHIPDevice * _Nullable device, NSEr

- (BOOL)isDevicePaired:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;
- (BOOL)getConnectedDevice:(uint64_t)deviceID
completionHandler:(CHIPDeviceConnectionCallback)completionHandler
queue:(dispatch_queue_t)queue
error:(NSError * __autoreleasing *)error;
completionHandler:(CHIPDeviceConnectionCallback)completionHandler;
- (nullable CHIPDevice *)getPairedDevice:(uint64_t)deviceID error:(NSError * __autoreleasing *)error;

- (instancetype)init NS_UNAVAILABLE;
Expand Down
17 changes: 10 additions & 7 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -374,22 +374,25 @@ - (BOOL)isDevicePaired:(uint64_t)deviceID error:(NSError * __autoreleasing *)err
}

- (BOOL)getConnectedDevice:(uint64_t)deviceID
completionHandler:(CHIPDeviceConnectionCallback)completionHandler
queue:(dispatch_queue_t)queue
error:(NSError * __autoreleasing *)error
completionHandler:(CHIPDeviceConnectionCallback)completionHandler
{
__block CHIP_ERROR errorCode = CHIP_ERROR_INCORRECT_STATE;
if (![self isRunning]) {
[self checkForError:errorCode logMsg:kErrorNotRunning error:error];
NSError * error;
[self checkForError:CHIP_ERROR_INCORRECT_STATE logMsg:kErrorNotRunning error:&error];
dispatch_async(queue, ^{
completionHandler(nil, error);
});
return NO;
}

dispatch_async(_chipWorkQueue, ^{
CHIPDeviceConnectionBridge * connectionBridge = new CHIPDeviceConnectionBridge(completionHandler, queue);
errorCode = connectionBridge->connect(self->_cppCommissioner, deviceID);
CHIP_ERROR errorCode = connectionBridge->connect(self->_cppCommissioner, deviceID);

if ([self checkForError:errorCode logMsg:kErrorGetPairedDevice error:error]) {
// Errors are propagated to the caller thru completionHandler.
NSError * error;
if ([self checkForError:errorCode logMsg:kErrorGetPairedDevice error:&error]) {
// Errors are propagated to the caller through completionHandler.
// No extra error handling is needed here.
return;
}
Expand Down
7 changes: 3 additions & 4 deletions src/darwin/Framework/CHIPTests/CHIPControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ - (void)testControllerInvalidAccess
NSError * error;
XCTAssertFalse([controller isRunning]);
XCTAssertFalse([controller getConnectedDevice:1234
completionHandler:^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
}
queue:dispatch_get_main_queue()
error:&error]);
XCTAssertEqual(error.code, CHIPErrorCodeInvalidState);
completionHandler:^(CHIPDevice * _Nullable chipDevice, NSError * _Nullable error) {
XCTAssertEqual(error.code, CHIPErrorCodeInvalidState);
}]);
XCTAssertFalse([controller unpairDevice:1 error:&error]);
XCTAssertEqual(error.code, CHIPErrorCodeInvalidState);
}
Expand Down

0 comments on commit c47f4f6

Please sign in to comment.