Skip to content

Commit

Permalink
Add GetCommissionerNodeId command supports for yaml tests, chip-tool …
Browse files Browse the repository at this point in the history
…and darwin-framework-tool (project-chip#24737)
  • Loading branch information
vivien-apple authored and David Lechner committed Mar 22, 2023
1 parent 5e2b998 commit a54447e
Show file tree
Hide file tree
Showing 25 changed files with 1,131 additions and 12,961 deletions.
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void CHIPCommand::MaybeTearDownStack()
CHIP_ERROR CHIPCommand::EnsureCommissionerForIdentity(std::string identity)
{
chip::NodeId nodeId;
ReturnErrorOnFailure(GetCommissionerNodeId(identity, &nodeId));
ReturnErrorOnFailure(GetIdentityNodeId(identity, &nodeId));
CommissionerIdentity lookupKey{ identity, nodeId };
if (mCommissioners.find(lookupKey) != mCommissioners.end())
{
Expand Down Expand Up @@ -310,7 +310,7 @@ std::string CHIPCommand::GetIdentity()
return name;
}

CHIP_ERROR CHIPCommand::GetCommissionerNodeId(std::string identity, chip::NodeId * nodeId)
CHIP_ERROR CHIPCommand::GetIdentityNodeId(std::string identity, chip::NodeId * nodeId)
{
if (mCommissionerNodeId.HasValue())
{
Expand Down Expand Up @@ -374,7 +374,7 @@ chip::Controller::DeviceCommissioner & CHIPCommand::GetCommissioner(std::string
VerifyOrDie(EnsureCommissionerForIdentity(identity) == CHIP_NO_ERROR);

chip::NodeId nodeId;
VerifyOrDie(GetCommissionerNodeId(identity, &nodeId) == CHIP_NO_ERROR);
VerifyOrDie(GetIdentityNodeId(identity, &nodeId) == CHIP_NO_ERROR);
CommissionerIdentity lookupKey{ identity, nodeId };
auto item = mCommissioners.find(lookupKey);
VerifyOrDie(item != mCommissioners.end());
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class CHIPCommand : public Command
CredentialIssuerCommands * mCredIssuerCmds;

std::string GetIdentity();
CHIP_ERROR GetCommissionerNodeId(std::string identity, chip::NodeId * nodeId);
CHIP_ERROR GetIdentityNodeId(std::string identity, chip::NodeId * nodeId);
void SetIdentity(const char * name);

// This method returns the commissioner instance to be used for running the command.
Expand Down
14 changes: 14 additions & 0 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ constexpr const char * kEventIdKey = "eventId";
constexpr const char * kCommandIdKey = "commandId";
constexpr const char * kErrorIdKey = "error";
constexpr const char * kClusterErrorIdKey = "clusterError";
constexpr const char * kValueKey = "value";
constexpr const char * kNodeIdKey = "nodeId";

namespace {
RemoteDataModelLoggerDelegate * gDelegate;
Expand Down Expand Up @@ -148,6 +150,18 @@ CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error)
return LogError(value, status);
}

CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value)
{
VerifyOrReturnError(gDelegate != nullptr, CHIP_NO_ERROR);

Json::Value rootValue;
rootValue[kValueKey] = Json::Value();
rootValue[kValueKey][kNodeIdKey] = value;

auto valueStr = chip::JsonToString(rootValue);
return gDelegate->LogJSON(valueStr.c_str());
}

void SetDelegate(RemoteDataModelLoggerDelegate * delegate)
{
gDelegate = delegate;
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/commands/common/RemoteDataModelLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ CHIP_ERROR LogErrorAsJSON(const chip::app::ConcreteCommandPath & path, const chi
CHIP_ERROR LogEventAsJSON(const chip::app::EventHeader & header, chip::TLV::TLVReader * data);
CHIP_ERROR LogErrorAsJSON(const chip::app::EventHeader & header, const chip::app::StatusIB & status);
CHIP_ERROR LogErrorAsJSON(const CHIP_ERROR & error);
CHIP_ERROR LogGetCommissionerNodeId(chip::NodeId value);
void SetDelegate(RemoteDataModelLoggerDelegate * delegate);
}; // namespace RemoteDataModelLogger
2 changes: 2 additions & 0 deletions examples/chip-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "commands/common/Commands.h"
#include "commands/pairing/CloseSessionCommand.h"
#include "commands/pairing/CommissionedListCommand.h"
#include "commands/pairing/GetCommissionerNodeIdCommand.h"
#include "commands/pairing/OpenCommissioningWindowCommand.h"
#include "commands/pairing/PairingCommand.h"

Expand Down Expand Up @@ -220,6 +221,7 @@ void registerCommandsPairing(Commands & commands, CredentialIssuerCommands * cre
make_unique<StartUdcServerCommand>(credsIssuerConfig),
make_unique<OpenCommissioningWindowCommand>(credsIssuerConfig),
make_unique<CloseSessionCommand>(credsIssuerConfig),
make_unique<GetCommissionerNodeIdCommand>(credsIssuerConfig),
};

commands.Register(clusterName, clusterCommands);
Expand Down
43 changes: 43 additions & 0 deletions examples/chip-tool/commands/pairing/GetCommissionerNodeIdCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include "../common/CHIPCommand.h"

class GetCommissionerNodeIdCommand : public CHIPCommand
{
public:
GetCommissionerNodeIdCommand(CredentialIssuerCommands * credIssuerCommands) :
CHIPCommand("get-commissioner-node-id", credIssuerCommands)
{}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override
{
chip::NodeId id;
ReturnErrorOnFailure(GetIdentityNodeId(GetIdentity(), &id));
ChipLogProgress(chipTool, "Commissioner Node Id 0x:" ChipLogFormatX64, ChipLogValueX64(id));

ReturnErrorOnFailure(RemoteDataModelLogger::LogGetCommissionerNodeId(id));
SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}

chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); }
};
7 changes: 0 additions & 7 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ class TestCommand : public TestRunner,

chip::Controller::DeviceCommissioner & GetCommissioner(const char * identity) override
{
// Best effort to get NodeId - if this fails, GetCommissioner will also fail
chip::NodeId id;
if (GetCommissionerNodeId(identity, &id) == CHIP_NO_ERROR)
{
mCommissionerNodeId.SetValue(id);
}
return CHIPCommand::GetCommissioner(identity);
};

Expand All @@ -101,7 +95,6 @@ class TestCommand : public TestRunner,

chip::Optional<char *> mPICSFilePath;
chip::Optional<uint16_t> mTimeout;
chip::Optional<chip::NodeId> mCommissionerNodeId;
std::map<std::string, std::unique_ptr<chip::OperationalDeviceProxy>> mDevices;

// When set to false, prevents interaction model events from affecting the current test status.
Expand Down
14 changes: 0 additions & 14 deletions examples/chip-tool/templates/tests/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ private:

void OnResponse(const chip::app::StatusIB & status, chip::TLV::TLVReader * data) override
{

// Allow yaml to access the current commissioner node id.
// Default to 0 (undefined node id) so we know if this isn't
// set correctly.
// Reset on every step in case it changed.
chip::NodeId commissionerNodeId = mCommissionerNodeId.ValueOr(0);
(void) commissionerNodeId;

bool shouldContinue = false;

switch (mTestIndex - 1)
Expand All @@ -80,12 +72,6 @@ private:
CHIP_ERROR DoTestStep(uint16_t testIndex) override
{
using namespace chip::app::Clusters;
// Allow yaml to access the current commissioner node id.
// Default to 0 (undefined node id) so we know if this isn't
// set correctly.
// Reset on every step in case it changed.
chip::NodeId commissionerNodeId = mCommissionerNodeId.ValueOr(0);
(void) commissionerNodeId;
switch (testIndex)
{
{{#chip_tests_items}}
Expand Down
2 changes: 2 additions & 0 deletions examples/darwin-framework-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ executable("darwin-framework-tool") {
"commands/common/MTRLogging.h",
"commands/pairing/Commands.h",
"commands/pairing/DeviceControllerDelegateBridge.mm",
"commands/pairing/GetCommissionerNodeIdCommand.h",
"commands/pairing/GetCommissionerNodeIdCommand.mm",
"commands/pairing/OpenCommissioningWindowCommand.h",
"commands/pairing/OpenCommissioningWindowCommand.mm",
"commands/pairing/PairingCommandBridge.mm",
Expand Down
2 changes: 2 additions & 0 deletions examples/darwin-framework-tool/commands/pairing/Commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#import <Matter/Matter.h>

#include "GetCommissionerNodeIdCommand.h"
#include "OpenCommissioningWindowCommand.h"
#include "PairingCommandBridge.h"
#include "PreWarmCommissioningCommand.h"
Expand Down Expand Up @@ -73,6 +74,7 @@ void registerCommandsPairing(Commands & commands)
make_unique<Unpair>(),
make_unique<OpenCommissioningWindowCommand>(),
make_unique<PreWarmCommissioningCommand>(),
make_unique<GetCommissionerNodeIdCommand>(),
};

commands.Register(clusterName, clusterCommands);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#pragma once

#include "../common/CHIPCommandBridge.h"

class GetCommissionerNodeIdCommand : public CHIPCommandBridge
{
public:
GetCommissionerNodeIdCommand() : CHIPCommandBridge("get-commissioner-node-id") {}

/////////// CHIPCommand Interface /////////
CHIP_ERROR RunCommand() override;

chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(0); }
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#import <Matter/Matter.h>

#include "GetCommissionerNodeIdCommand.h"

CHIP_ERROR GetCommissionerNodeIdCommand::RunCommand()
{
auto * controller = CurrentCommissioner();
VerifyOrReturnError(nil != controller, CHIP_ERROR_INCORRECT_STATE);

auto id = [controller.controllerNodeId unsignedLongLongValue];
ChipLogProgress(chipTool, "Commissioner Node Id 0x:" ChipLogFormatX64, ChipLogValueX64(id));

SetCommandExitStatus(CHIP_NO_ERROR);
return CHIP_NO_ERROR;
}
31 changes: 20 additions & 11 deletions examples/darwin-framework-tool/commands/tests/TestCommandBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ class TestCommandBridge : public CHIPCommandBridge,
VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE);

SetIdentity(identity);
if (controller.controllerNodeId != nil) {
mCommissionerNodeId.SetValue([controller.controllerNodeId unsignedLongLongValue]);
}

// Invalidate our existing CASE session; otherwise trying to work with
// our device will just reuse it without establishing a new CASE
Expand Down Expand Up @@ -184,10 +181,6 @@ class TestCommandBridge : public CHIPCommandBridge,
VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE);

SetIdentity(identity);
if (controller.controllerNodeId != nil) {
mCommissionerNodeId.SetValue([controller.controllerNodeId unsignedLongLongValue]);
}

[controller setDeviceControllerDelegate:mDeviceControllerDelegate queue:mCallbackQueue];
[mDeviceControllerDelegate setDeviceId:value.nodeId];
[mDeviceControllerDelegate setActive:YES];
Expand All @@ -208,6 +201,26 @@ class TestCommandBridge : public CHIPCommandBridge,
return MTRErrorToCHIPErrorCode(err);
}

CHIP_ERROR GetCommissionerNodeId(const char * _Nullable identity,
const chip::app::Clusters::CommissionerCommands::Commands::GetCommissionerNodeId::Type & value,
void (^_Nonnull OnResponse)(const chip::GetCommissionerNodeIdResponse &))
{
auto * controller = GetCommissioner(identity);
VerifyOrReturnError(controller != nil, CHIP_ERROR_INCORRECT_STATE);

auto id = [controller.controllerNodeId unsignedLongLongValue];
ChipLogProgress(chipTool, "Commissioner Node Id: %llu", id);

chip::GetCommissionerNodeIdResponse outValue;
outValue.nodeId = id;

dispatch_async(mCallbackQueue, ^{
OnResponse(outValue);
});

return CHIP_NO_ERROR;
}

/////////// SystemCommands Interface /////////
CHIP_ERROR ContinueOnChipMainThread(CHIP_ERROR err) override
{
Expand All @@ -226,9 +239,6 @@ class TestCommandBridge : public CHIPCommandBridge,
MTRDeviceController * controller = GetCommissioner(identity);

SetIdentity(identity);
if (controller != nil && controller.controllerNodeId != nil) {
mCommissionerNodeId.SetValue([controller.controllerNodeId unsignedLongLongValue]);
}
return mConnectedDevices[identity];
}

Expand Down Expand Up @@ -270,7 +280,6 @@ class TestCommandBridge : public CHIPCommandBridge,
chip::Optional<char *> mPICSFilePath;
chip::Optional<chip::EndpointId> mEndpointId;
chip::Optional<uint16_t> mTimeout;
chip::Optional<chip::NodeId> mCommissionerNodeId;

bool CheckConstraintStartsWith(
const char * _Nonnull itemName, const NSString * _Nonnull current, const char * _Nonnull expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,10 @@ class {{filename}}: public TestCommandBridge
{
}

// Allow yaml to access the current commissioner node id.
// Default to 0 (undefined node id) so we know if this isn't
// set correctly.
// Reset on every step in case it changed.
chip::NodeId commissionerNodeId = mCommissionerNodeId.ValueOr(0);

/////////// TestCommand Interface /////////
void NextTest() override
{
CHIP_ERROR err = CHIP_NO_ERROR;
commissionerNodeId = mCommissionerNodeId.ValueOr(0);

if (0 == mTestIndex)
{
Expand Down Expand Up @@ -140,10 +133,31 @@ class {{filename}}: public TestCommandBridge
{{#chip_tests_item_parameters}}
{{>commandValue ns=parent.cluster container=(asPropertyValue dontUnwrapValue=true) definedValue=definedValue depth=0}}
{{/chip_tests_item_parameters}}
{{#if (and (isStrEqual cluster "CommissionerCommands") (isStrEqual command "GetCommissionerNodeId"))}}
return {{command}}("{{identity}}", value, ^(const chip::{{command}}Response & values) {
{{#chip_tests_item_responses}}
{{#chip_tests_item_response_parameters}}
{{#if hasExpectedValue}}
{
id actualValue = values.{{asStructPropertyName name}}
{{>check_test_value actual="actualValue" expected=expectedValue cluster=../cluster}}
}
{{/if}}
{{>maybeCheckExpectedConstraints}}
{{#if saveAs}}
{
{{saveAs}} = [[NSNumber alloc] initWithUnsignedLongLong:values.{{asStructPropertyName name}}];
}
{{/if}}
{{/chip_tests_item_response_parameters}}
{{/chip_tests_item_responses}}
NextTest();
});
{{else}}
return {{command}}("{{identity}}", value);
{{/if}}
{{else}}
MTRBaseDevice * device = GetDevice("{{identity}}");
commissionerNodeId = mCommissionerNodeId.ValueOr(0);
__auto_type * cluster = [[MTRBaseCluster{{>cluster}} alloc] initWithDevice:device endpointID:@({{endpoint}}) queue:mCallbackQueue];
VerifyOrReturnError(cluster != nil, CHIP_ERROR_INCORRECT_STATE);

Expand Down
Loading

0 comments on commit a54447e

Please sign in to comment.