Skip to content

Commit 2368252

Browse files
vivien-applepull[bot]
authored andcommitted
[YAML] Add identity keyword to YAML (#12409)
* [YAML] Add identity keyword to YAML * Update generated code
1 parent cd23f32 commit 2368252

File tree

7 files changed

+1946
-1386
lines changed

7 files changed

+1946
-1386
lines changed

examples/chip-tool/commands/common/CHIPCommand.cpp

+32-23
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@
2929

3030
using DeviceControllerFactory = chip::Controller::DeviceControllerFactory;
3131

32-
constexpr const char kCommissionerAlpha[] = "alpha";
33-
constexpr const char kCommissionerBeta[] = "beta";
34-
constexpr const char kCommissionerGamma[] = "gamma";
35-
36-
constexpr chip::FabricId kCommissionerAlphaFabricId = 1;
37-
constexpr chip::FabricId kCommissionerBetaFabricId = 2;
38-
constexpr chip::FabricId kCommissionerGammaFabricId = 3;
32+
constexpr chip::FabricId kIdentityAlphaFabricId = 1;
33+
constexpr chip::FabricId kIdentityBetaFabricId = 2;
34+
constexpr chip::FabricId kIdentityGammaFabricId = 3;
3935

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

55-
ReturnLogErrorOnFailure(InitializeCommissioner(CurrentCommissionerName(), CurrentCommissionerIndex()));
51+
ReturnLogErrorOnFailure(InitializeCommissioner(GetIdentity(), CurrentCommissionerIndex()));
5652

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

6965
return CHIP_NO_ERROR;
7066
}
7167

72-
std::string CHIPCommand::CurrentCommissionerName()
68+
void CHIPCommand::SetIdentity(const char * identity)
69+
{
70+
std::string name = std::string(identity);
71+
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0)
72+
{
73+
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
74+
kIdentityBeta, kIdentityGamma);
75+
chipDie();
76+
}
77+
78+
mCommissionerName.SetValue(const_cast<char *>(identity));
79+
}
80+
81+
std::string CHIPCommand::GetIdentity()
7382
{
74-
std::string name = mCommissionerName.HasValue() ? mCommissionerName.Value() : kCommissionerAlpha;
75-
if (name.compare(kCommissionerAlpha) != 0 && name.compare(kCommissionerBeta) != 0 && name.compare(kCommissionerGamma) != 0)
83+
std::string name = mCommissionerName.HasValue() ? mCommissionerName.Value() : kIdentityAlpha;
84+
if (name.compare(kIdentityAlpha) != 0 && name.compare(kIdentityBeta) != 0 && name.compare(kIdentityGamma) != 0)
7685
{
77-
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kCommissionerAlpha,
78-
kCommissionerBeta, kCommissionerGamma);
86+
ChipLogError(chipTool, "Unknown commissioner name: %s. Supported names are [%s, %s, %s]", name.c_str(), kIdentityAlpha,
87+
kIdentityBeta, kIdentityGamma);
7988
chipDie();
8089
}
8190

@@ -86,28 +95,28 @@ uint16_t CHIPCommand::CurrentCommissionerIndex()
8695
{
8796
uint16_t index = 0;
8897

89-
std::string name = CurrentCommissionerName();
90-
if (name.compare(kCommissionerAlpha) == 0)
98+
std::string name = GetIdentity();
99+
if (name.compare(kIdentityAlpha) == 0)
91100
{
92-
index = kCommissionerAlphaFabricId;
101+
index = kIdentityAlphaFabricId;
93102
}
94-
else if (name.compare(kCommissionerBeta) == 0)
103+
else if (name.compare(kIdentityBeta) == 0)
95104
{
96-
index = kCommissionerBetaFabricId;
105+
index = kIdentityBetaFabricId;
97106
}
98-
else if (name.compare(kCommissionerGamma) == 0)
107+
else if (name.compare(kIdentityGamma) == 0)
99108
{
100-
index = kCommissionerGammaFabricId;
109+
index = kIdentityGammaFabricId;
101110
}
102111

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

108117
chip::Controller::DeviceCommissioner & CHIPCommand::CurrentCommissioner()
109118
{
110-
auto item = mCommissioners.find(CurrentCommissionerName());
119+
auto item = mCommissioners.find(GetIdentity());
111120
return *item->second.get();
112121
}
113122

examples/chip-tool/commands/common/CHIPCommand.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
class PersistentStorage;
2727

28+
constexpr const char kIdentityAlpha[] = "alpha";
29+
constexpr const char kIdentityBeta[] = "beta";
30+
constexpr const char kIdentityGamma[] = "gamma";
31+
2832
class CHIPCommand : public Command
2933
{
3034
public:
@@ -66,6 +70,9 @@ class CHIPCommand : public Command
6670
PersistentStorage mCommissionerStorage;
6771
chip::SimpleFabricStorage mFabricStorage;
6872

73+
std::string GetIdentity();
74+
void SetIdentity(const char * name);
75+
6976
// This method returns the commissioner instance to be used for running the command.
7077
// The default commissioner instance name is "alpha", but it can be overriden by passing
7178
// --identity "instance name" when running a command.
@@ -74,7 +81,6 @@ class CHIPCommand : public Command
7481
private:
7582
CHIP_ERROR InitializeCommissioner(std::string key, chip::FabricId fabricId);
7683
CHIP_ERROR ShutdownCommissioner(std::string key);
77-
std::string CurrentCommissionerName();
7884
uint16_t CurrentCommissionerIndex();
7985
std::map<std::string, std::unique_ptr<ChipDeviceCommissioner>> mCommissioners;
8086
chip::Optional<char *> mCommissionerName;

examples/chip-tool/commands/tests/TestCommand.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void TestCommand::OnDeviceConnectedFn(void * context, chip::OperationalDevicePro
4040
ChipLogProgress(chipTool, " **** Test Setup: Device Connected\n");
4141
auto * command = static_cast<TestCommand *>(context);
4242
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Device connected, but cannot run the test, as the context is null"));
43-
command->mDevice = device;
43+
command->mDevices[command->GetIdentity()] = device;
4444

4545
command->NextTest();
4646
}

examples/chip-tool/commands/tests/TestCommand.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TestCommand : public CHIPCommand
6363
CHIP_ERROR UserPrompt(const char * message);
6464

6565
protected:
66-
ChipDevice * mDevice;
66+
std::map<std::string, ChipDevice *> mDevices;
6767
chip::NodeId mNodeId;
6868

6969
static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device);

examples/chip-tool/templates/partials/test_cluster.zapt

+5-3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class {{filename}}: public TestCommand
149149
{{#if (isTestOnlyCluster cluster)}}
150150
CHIP_ERROR {{>testCommand}}()
151151
{
152+
SetIdentity(kIdentity{{asUpperCamelCase identity}});
152153
return {{command}}({{#chip_tests_item_parameters}}{{#not_first}}, {{/not_first}}{{#if (isString type)}}"{{/if}}{{definedValue}}{{#if (isString type)}}"{{/if}}{{/chip_tests_item_parameters}});
153154
}
154155
{{else if isWait}}
@@ -171,6 +172,7 @@ class {{filename}}: public TestCommand
171172
{{#*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}}
172173
{{#*inline "doneArguments"}}{{/inline}}
173174

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

197-
ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}(mDevice, this, success, failure, {{#if isGroupCommand}}groupId{{else}}endpoint{{/if}}, request));
199+
ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}({{>device}}, this, success, failure, {{#if isGroupCommand}}groupId{{else}}endpoint{{/if}}, request));
198200
{{#unless async}}return CHIP_NO_ERROR;{{/unless}}
199201
{{else}}
200202
chip::Controller::{{asUpperCamelCase cluster}}ClusterTest cluster;
201203
{{#if isGroupCommand}}
202-
cluster.AssociateWithGroup(mDevice, groupId);
204+
cluster.AssociateWithGroup({{>device}}, groupId);
203205
{{else}}
204-
cluster.Associate(mDevice, endpoint);
206+
cluster.Associate({{>device}}, endpoint);
205207
{{/if}}
206208

207209
{{#chip_tests_item_parameters}}

src/app/zap-templates/common/ClusterTestGeneration.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const templateUtil = require(zapPath + 'dist/src-electron/generator/template-uti
2929
const { getClusters, getCommands, getAttributes, isTestOnlyCluster } = require('./simulated-clusters/SimulatedClusters.js');
3030
const { asBlocks } = require('./ClustersHelper.js');
3131

32+
const kIdentityName = 'identity';
3233
const kClusterName = 'cluster';
3334
const kEndpointName = 'endpoint';
3435
const kGroupId = 'groupId';
@@ -275,11 +276,13 @@ function setDefaultResponse(test)
275276

276277
function setDefaults(test, defaultConfig)
277278
{
278-
const defaultClusterName = defaultConfig[kClusterName] || null;
279-
const defaultEndpointId = kEndpointName in defaultConfig ? defaultConfig[kEndpointName] : null;
280-
const defaultDisabled = false;
279+
const defaultIdentityName = kIdentityName in defaultConfig ? defaultConfig[kIdentityName] : "alpha";
280+
const defaultClusterName = defaultConfig[kClusterName] || null;
281+
const defaultEndpointId = kEndpointName in defaultConfig ? defaultConfig[kEndpointName] : null;
282+
const defaultDisabled = false;
281283

282284
setDefaultType(test);
285+
setDefault(test, kIdentityName, defaultIdentityName);
283286
setDefault(test, kClusterName, defaultClusterName);
284287
setDefault(test, kEndpointName, defaultEndpointId);
285288
setDefault(test, kDisabledName, defaultDisabled);

0 commit comments

Comments
 (0)