Skip to content

Commit

Permalink
[YAML] Add identity keyword to YAML (#12409)
Browse files Browse the repository at this point in the history
* [YAML] Add identity keyword to YAML

* Update generated code
  • Loading branch information
vivien-apple authored and pull[bot] committed Dec 7, 2021
1 parent 16ef38c commit 6036734
Show file tree
Hide file tree
Showing 7 changed files with 1,946 additions and 1,386 deletions.
55 changes: 32 additions & 23 deletions examples/chip-tool/commands/common/CHIPCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@

using DeviceControllerFactory = chip::Controller::DeviceControllerFactory;

constexpr const char kCommissionerAlpha[] = "alpha";
constexpr const char kCommissionerBeta[] = "beta";
constexpr const char kCommissionerGamma[] = "gamma";

constexpr chip::FabricId kCommissionerAlphaFabricId = 1;
constexpr chip::FabricId kCommissionerBetaFabricId = 2;
constexpr chip::FabricId kCommissionerGammaFabricId = 3;
constexpr chip::FabricId kIdentityAlphaFabricId = 1;
constexpr chip::FabricId kIdentityBetaFabricId = 2;
constexpr chip::FabricId kIdentityGammaFabricId = 3;

CHIP_ERROR CHIPCommand::Run()
{
Expand All @@ -52,7 +48,7 @@ CHIP_ERROR CHIPCommand::Run()
factoryInitParams.listenPort = static_cast<uint16_t>(mDefaultStorage.GetListenPort() + CurrentCommissionerIndex());
ReturnLogErrorOnFailure(DeviceControllerFactory::GetInstance().Init(factoryInitParams));

ReturnLogErrorOnFailure(InitializeCommissioner(CurrentCommissionerName(), CurrentCommissionerIndex()));
ReturnLogErrorOnFailure(InitializeCommissioner(GetIdentity(), CurrentCommissionerIndex()));

chip::DeviceLayer::PlatformMgr().ScheduleWork(RunQueuedCommand, reinterpret_cast<intptr_t>(this));
ReturnLogErrorOnFailure(StartWaiting(GetWaitDuration()));
Expand All @@ -64,18 +60,31 @@ CHIP_ERROR CHIPCommand::Run()
// since the CHIP thread and event queue have been stopped, preventing any thread
// races.
//
ReturnLogErrorOnFailure(ShutdownCommissioner(CurrentCommissionerName()));
ReturnLogErrorOnFailure(ShutdownCommissioner(GetIdentity()));

return CHIP_NO_ERROR;
}

std::string CHIPCommand::CurrentCommissionerName()
void CHIPCommand::SetIdentity(const char * identity)
{
std::string name = std::string(identity);
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0)
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
kIdentityBeta, kIdentityGamma);
chipDie();
}

mCommissionerName.SetValue(const_cast<char *>(identity));
}

std::string CHIPCommand::GetIdentity()
{
std::string name = mCommissionerName.HasValue() ? mCommissionerName.Value() : kCommissionerAlpha;
if (name.compare(kCommissionerAlpha) != 0 && name.compare(kCommissionerBeta) != 0 && name.compare(kCommissionerGamma) != 0)
std::string name = mCommissionerName.HasValue() ? mCommissionerName.Value() : kIdentityAlpha;
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0)
{
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kCommissionerAlpha,
kCommissionerBeta, kCommissionerGamma);
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
kIdentityBeta, kIdentityGamma);
chipDie();
}

Expand All @@ -86,28 +95,28 @@ uint16_t CHIPCommand::CurrentCommissionerIndex()
{
uint16_t index = 0;

std::string name = CurrentCommissionerName();
if (name.compare(kCommissionerAlpha) == 0)
std::string name = GetIdentity();
if (name.compare(kIdentityAlpha) == 0)
{
index = kCommissionerAlphaFabricId;
index = kIdentityAlphaFabricId;
}
else if (name.compare(kCommissionerBeta) == 0)
else if (name.compare(kIdentityBeta) == 0)
{
index = kCommissionerBetaFabricId;
index = kIdentityBetaFabricId;
}
else if (name.compare(kCommissionerGamma) == 0)
else if (name.compare(kIdentityGamma) == 0)
{
index = kCommissionerGammaFabricId;
index = kIdentityGammaFabricId;
}

VerifyOrDieWithMsg(index != 0, chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(),
kCommissionerAlpha, kCommissionerBeta, kCommissionerGamma);
kIdentityAlpha, kIdentityBeta, kIdentityGamma);
return index;
}

chip::Controller::DeviceCommissioner & CHIPCommand::CurrentCommissioner()
{
auto item = mCommissioners.find(CurrentCommissionerName());
auto item = mCommissioners.find(GetIdentity());
return *item->second.get();
}

Expand Down
8 changes: 7 additions & 1 deletion examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

class PersistentStorage;

constexpr const char kIdentityAlpha[] = "alpha";
constexpr const char kIdentityBeta[] = "beta";
constexpr const char kIdentityGamma[] = "gamma";

class CHIPCommand : public Command
{
public:
Expand Down Expand Up @@ -66,6 +70,9 @@ class CHIPCommand : public Command
PersistentStorage mCommissionerStorage;
chip::SimpleFabricStorage mFabricStorage;

std::string GetIdentity();
void SetIdentity(const char * name);

// This method returns the commissioner instance to be used for running the command.
// The default commissioner instance name is "alpha", but it can be overriden by passing
// --identity "instance name" when running a command.
Expand All @@ -74,7 +81,6 @@ class CHIPCommand : public Command
private:
CHIP_ERROR InitializeCommissioner(std::string key, chip::FabricId fabricId);
CHIP_ERROR ShutdownCommissioner(std::string key);
std::string CurrentCommissionerName();
uint16_t CurrentCommissionerIndex();
std::map<std::string, std::unique_ptr<ChipDeviceCommissioner>> mCommissioners;
chip::Optional<char *> mCommissionerName;
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDevicePro
ChipLogProgress(chipTool, " **** Test Setup: Device Connected\n");
auto * command = static_cast<TestCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Device connected, but cannot run the test, as the context is null"));
command->mDevice = device;
command->mDevices[command->GetIdentity()] = device;

command->NextTest();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TestCommand : public CHIPCommand
CHIP_ERROR UserPrompt(const char * message);

protected:
ChipDevice * mDevice;
std::map<std::string, ChipDevice *> mDevices;
chip::NodeId mNodeId;

static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device);
Expand Down
8 changes: 5 additions & 3 deletions examples/chip-tool/templates/partials/test_cluster.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class {{filename}}: public TestCommand
{{#if (isTestOnlyCluster cluster)}}
CHIP_ERROR {{>testCommand}}()
{
SetIdentity(kIdentity{{asUpperCamelCase identity}});
return {{command}}({{#chip_tests_item_parameters}}{{#not_first}}, {{/not_first}}{{#if (isString type)}}"{{/if}}{{definedValue}}{{#if (isString type)}}"{{/if}}{{/chip_tests_item_parameters}});
}
{{else if isWait}}
Expand All @@ -171,6 +172,7 @@ class {{filename}}: public TestCommand
{{#*inline "successArguments"}}{{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}
{{#*inline "doneArguments"}}{{/inline}}

{{#*inline "device"}}mDevices[kIdentity{{asUpperCamelCase identity}}]{{/inline}}
CHIP_ERROR {{>testCommand}}()
{
{{#if isGroupCommand}}
Expand All @@ -194,14 +196,14 @@ class {{filename}}: public TestCommand
(static_cast<{{filename}} *>(context))->OnFailureResponse_{{index}}(status);
};

ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}(mDevice, this, success, failure, {{#if isGroupCommand}}groupId{{else}}endpoint{{/if}}, request));
ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}({{>device}}, this, success, failure, {{#if isGroupCommand}}groupId{{else}}endpoint{{/if}}, request));
{{#unless async}}return CHIP_NO_ERROR;{{/unless}}
{{else}}
chip::Controller::{{asUpperCamelCase cluster}}ClusterTest cluster;
{{#if isGroupCommand}}
cluster.AssociateWithGroup(mDevice, groupId);
cluster.AssociateWithGroup({{>device}}, groupId);
{{else}}
cluster.Associate(mDevice, endpoint);
cluster.Associate({{>device}}, endpoint);
{{/if}}

{{#chip_tests_item_parameters}}
Expand Down
9 changes: 6 additions & 3 deletions src/app/zap-templates/common/ClusterTestGeneration.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const templateUtil = require(zapPath + 'dist/src-electron/generator/template-uti
const { getClusters, getCommands, getAttributes, isTestOnlyCluster } = require('./simulated-clusters/SimulatedClusters.js');
const { asBlocks } = require('./ClustersHelper.js');

const kIdentityName = 'identity';
const kClusterName = 'cluster';
const kEndpointName = 'endpoint';
const kGroupId = 'groupId';
Expand Down Expand Up @@ -275,11 +276,13 @@ function setDefaultResponse(test)

function setDefaults(test, defaultConfig)
{
const defaultClusterName = defaultConfig[kClusterName] || null;
const defaultEndpointId = kEndpointName in defaultConfig ? defaultConfig[kEndpointName] : null;
const defaultDisabled = false;
const defaultIdentityName = kIdentityName in defaultConfig ? defaultConfig[kIdentityName] : "alpha";
const defaultClusterName = defaultConfig[kClusterName] || null;
const defaultEndpointId = kEndpointName in defaultConfig ? defaultConfig[kEndpointName] : null;
const defaultDisabled = false;

setDefaultType(test);
setDefault(test, kIdentityName, defaultIdentityName);
setDefault(test, kClusterName, defaultClusterName);
setDefault(test, kEndpointName, defaultEndpointId);
setDefault(test, kDisabledName, defaultDisabled);
Expand Down
Loading

0 comments on commit 6036734

Please sign in to comment.