Skip to content

Commit

Permalink
[MatterYamlTests][darwin-framework-tool] Ensure that WaitForCommissio…
Browse files Browse the repository at this point in the history
…nee works after a reboot (#29252)
  • Loading branch information
vivien-apple authored Oct 19, 2023
1 parent dbc8e49 commit 2e5c2a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,15 @@
#include <app/OperationalSessionSetup.h>
#include <lib/core/CHIPCallback.h>

NS_ASSUME_NONNULL_BEGIN
@interface MTRDeviceTestDelegate : NSObject <MTRDeviceDelegate>
@property CHIPCommandBridge * commandBridge;
- (instancetype)initWithCommandBridge:(CHIPCommandBridge *)commandBridge;
- (instancetype)init NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

class WaitForCommissioneeCommand : public CHIPCommandBridge {
class WaitForCommissioneeCommand : public CHIPCommandBridge
{
public:
WaitForCommissioneeCommand()
: CHIPCommandBridge("wait-for-commissionee")
, mDeviceDelegate([[MTRDeviceTestDelegate alloc] initWithCommandBridge:this])
WaitForCommissioneeCommand() : CHIPCommandBridge("wait-for-commissionee")
{
AddArgument("nodeId", 0, UINT64_MAX, &mNodeId);
AddArgument("expire-existing-session", 0, 1, &mExpireExistingSession);
AddArgument(
"timeout", 0, UINT64_MAX, &mTimeoutSecs, "Time, in seconds, before this command is considered to have timed out.");
AddArgument("timeout", 0, UINT64_MAX, &mTimeoutSecs,
"Time, in seconds, before this command is considered to have timed out.");
}

/////////// CHIPCommandBridge Interface /////////
Expand All @@ -53,5 +44,4 @@ class WaitForCommissioneeCommand : public CHIPCommandBridge {
chip::NodeId mNodeId;
chip::Optional<uint16_t> mTimeoutSecs;
chip::Optional<bool> mExpireExistingSession;
id<MTRDeviceDelegate> _Nullable mDeviceDelegate;
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,18 @@

#import "MTRDevice_Externs.h"

@implementation MTRDeviceTestDelegate
- (instancetype)initWithCommandBridge:(CHIPCommandBridge *)commandBridge
{
if (!(self = [super init])) {
return nil;
}

_commandBridge = commandBridge;
return self;
}

- (void)device:(MTRDevice *)device stateChanged:(MTRDeviceState)state
{
if (state == MTRDeviceStateReachable) {
_commandBridge->SetCommandExitStatus(CHIP_NO_ERROR);
} else if (state == MTRDeviceStateUnreachable) {
_commandBridge->SetCommandExitStatus(CHIP_ERROR_NOT_FOUND);
} else if (state == MTRDeviceStateUnknown) {
_commandBridge->SetCommandExitStatus(CHIP_ERROR_NOT_FOUND);
} else {
// This should not happens.
chipDie();
}
}

- (void)device:(MTRDevice *)device receivedAttributeReport:(NSArray<NSDictionary<NSString *, id> *> *)attributeReport
{
}

- (void)device:(MTRDevice *)device receivedEventReport:(NSArray<NSDictionary<NSString *, id> *> *)eventReport
{
}
@end

CHIP_ERROR WaitForCommissioneeCommand::RunCommand()
{
MTRDeviceController * commissioner = CurrentCommissioner();
VerifyOrReturnError(nil != commissioner, CHIP_ERROR_INCORRECT_STATE);

auto * base_device = [MTRBaseDevice deviceWithNodeID:@(mNodeId) controller:commissioner];
VerifyOrReturnError(base_device != nil, CHIP_ERROR_INCORRECT_STATE);

if (mExpireExistingSession.ValueOr(true)) {
[base_device invalidateCASESession];
}
base_device = nil;

auto * device = [MTRDevice deviceWithNodeID:@(mNodeId) controller:commissioner];
VerifyOrReturnError(device != nil, CHIP_ERROR_INCORRECT_STATE);

auto queue = dispatch_queue_create("com.chip.wait_for_commissionee", DISPATCH_QUEUE_SERIAL);
[device setDelegate:mDeviceDelegate queue:queue];

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

0 comments on commit 2e5c2a7

Please sign in to comment.