Skip to content

Commit 3302268

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Convert chip-tool YAML tests to the new subscription API. (#12852)
1 parent c77fc78 commit 3302268

File tree

2 files changed

+4791
-3958
lines changed

2 files changed

+4791
-3958
lines changed

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

+82-78
Original file line numberDiff line numberDiff line change
@@ -60,77 +60,69 @@ class {{filename}}: public TestCommand
6060
{{/chip_tests_item_response_parameters}}
6161
{{/chip_tests_items}}
6262

63-
{{#*inline "failureCallback"}}mOnFailureCallback_{{index}}{{/inline}}
64-
{{#*inline "successCallback"}}mOnSuccessCallback_{{index}}{{/inline}}
65-
66-
{{#if isGroupCommand}}
67-
{{#*inline "doneCallback"}}mOnDoneCallback_{{index}}{{/inline}}
63+
{{~#*inline "subscribeDataCallback"}}
64+
mTest_{{parent.filename}}_{{attribute}}_Reported
65+
{{/inline}}
66+
{{#*inline "subscribeDataCallbackType"}}
67+
Test_{{parent.filename}}_{{attribute}}_ReportCallback
68+
{{/inline}}
69+
{{#chip_tests_items}}
70+
{{#if allocateSubscribeDataCallback}}
71+
typedef void (*{{> subscribeDataCallbackType}})(void * context, {{zapTypeToDecodableClusterObjectType attributeObject.type ns=parent.cluster isArgument=true}} value);
72+
{{> subscribeDataCallbackType}} {{> subscribeDataCallback}} = nullptr;
6873
{{/if}}
74+
{{/chip_tests_items}}
6975

70-
{{#*inline "failureResponse"}}OnFailureCallback_{{index}}{{/inline}}
71-
{{#*inline "successResponse"}}OnSuccessCallback_{{index}}{{/inline}}
72-
73-
{{#*inline "doneResponse"}}OnDoneCallback_{{index}}{{/inline}}
76+
{{#*inline "failureResponse"}}OnFailureResponse_{{index}}{{/inline}}
77+
{{#*inline "successResponse"}}OnSuccessResponse_{{index}}{{/inline}}
78+
{{#*inline "subscriptionEstablished"}}OnSubscriptionEstablishedResponse_{{index}}{{/inline}}
79+
{{#*inline "doneResponse"}}OnDoneResponse_{{index}}{{/inline}}
7480

75-
{{#*inline "successArguments"}}void * context{{#chip_tests_item_response_parameters}}, {{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}}{{/inline}}
76-
{{#*inline "failureArguments"}}void * context, uint8_t status{{/inline}}
81+
{{#*inline "staticFailureResponse"}}OnFailureCallback_{{index}}{{/inline}}
82+
{{#*inline "staticSuccessResponse"}}OnSuccessCallback_{{index}}{{/inline}}
83+
{{#*inline "staticSubscriptionEstablished"}}OnSubscriptionEstablished_{{index}}{{/inline}}
84+
{{#*inline "staticDoneResponse"}}OnDoneCallback_{{index}}{{/inline}}
7785

78-
{{#if isGroupCommand}}
79-
{{#*inline "doneArguments"}}void * context{{/inline}}
80-
{{/if}}
86+
{{#*inline "successArguments"}}{{#chip_tests_item_response_parameters}}{{#first}}{{#if ../leadingComma}}, {{/if}}{{/first}} {{zapTypeToDecodableClusterObjectType type ns=parent.cluster isArgument=true}} {{asLowerCamelCase name}}{{#not_last}}, {{/not_last}}{{/chip_tests_item_response_parameters}}{{/inline}}
87+
{{#*inline "failureArguments"}}{{#if leadingComma}}, {{/if}}EmberAfStatus status{{/inline}}
88+
{{#*inline "staticSuccessArguments"}}void * context{{> successArguments leadingComma=true}}{{/inline}}
89+
{{#*inline "staticFailureArguments"}}void * context{{> failureArguments leadingComma=true}}{{/inline}}
90+
{{#*inline "staticDoneArguments"}}void * context{{/inline}}
91+
{{#*inline "doneArguments"}}{{/inline}}
8192

82-
{{#chip_tests_items}}
83-
{{#unless (isTestOnlyCluster cluster)}}
84-
{{#unless isWait}}
85-
{{#unless isCommand}}
86-
{{#unless isWriteAttribute}}
87-
{{#unless isReadAttribute}}
88-
chip::Callback::Callback<void (*) ({{>failureArguments}})> {{>failureCallback}} { {{>failureResponse}}, this };
89-
chip::Callback::Callback<void (*) ({{>successArguments}})> {{>successCallback}} { {{>successResponse}}, this };
90-
{{/unless}}
91-
{{/unless}}
92-
{{/unless}}
93-
{{/unless}}
94-
{{/unless}}
95-
{{/chip_tests_items}}
93+
{{#*inline "staticSubscriptionEstablishedArguments"}}void * context{{/inline}}
94+
{{#*inline "subscriptionEstablishedArguments"}}{{/inline}}
9695

9796
{{#chip_tests_items}}
9897
{{#unless (isTestOnlyCluster cluster)}}
9998
{{#unless isWait}}
10099
{{#unless isCommand}}
101100
{{#if isWriteAttribute}}
102-
static void {{>failureResponse}}(void * context, EmberAfStatus status)
103-
{
104-
(static_cast<{{filename}} *>(context))->OnFailureResponse_{{index}}(chip::to_underlying(status));
105-
}
106-
107-
{{#if isGroupCommand}}
108-
static void {{>doneResponse}}(void * context)
109-
{
110-
(static_cast<{{filename}} *>(context))->OnDoneResponse_{{index}}();
111-
112-
}
101+
{{#if isGroupCommand}}
102+
static void {{>staticDoneResponse}}({{>staticDoneArguments}})
103+
{
104+
(static_cast<{{filename}} *>(context))->{{> doneResponse }}();
105+
}
106+
{{/if}}
113107
{{/if}}
114108

115-
{{else if isReadAttribute}}
116-
static void {{>failureResponse}}(void * context, EmberAfStatus status)
109+
static void {{>staticFailureResponse}}({{>staticFailureArguments}})
117110
{
118-
(static_cast<{{filename}} *>(context))->OnFailureResponse_{{index}}(chip::to_underlying(status));
111+
(static_cast<{{filename}} *>(context))->{{>failureResponse}}(status);
119112
}
120-
{{else}}
121-
static void {{>failureResponse}}({{> failureArguments}})
113+
114+
static void {{>staticSuccessResponse}}({{> staticSuccessArguments}})
122115
{
123-
(static_cast<{{filename}} *>(context))->OnFailureResponse_{{index}}(status);
116+
(static_cast<{{filename}} *>(context))->{{>successResponse}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
124117
}
125-
{{/if}}
126118

127-
128-
static void {{>successResponse}}({{> successArguments}})
119+
{{#if isSubscribeAttribute}}
120+
static void {{> staticSubscriptionEstablished}}({{> staticSubscriptionEstablishedArguments}})
129121
{
130-
(static_cast<{{filename}} *>(context))->OnSuccessResponse_{{index}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
122+
(static_cast<{{filename}} *>(context))->{{> subscriptionEstablished}}();
131123
}
124+
{{/if}}
132125

133-
{{! "isWaitForReport" can be replaced by "async" if there is a mechanism to remove the report handler}}
134126
{{#if isWaitForReport}}
135127
bool mReceivedReport_{{index}} = false;
136128
{{/if}}
@@ -164,13 +156,6 @@ class {{filename}}: public TestCommand
164156
return CHIP_NO_ERROR;
165157
}
166158
{{else}}
167-
{{#*inline "failureResponse"}}OnFailureResponse_{{index}}{{/inline}}
168-
{{#*inline "successResponse"}}OnSuccessResponse_{{index}}{{/inline}}
169-
{{#*inline "doneResponse"}}OnDoneResponse_{{index}}{{/inline}}
170-
171-
{{#*inline "failureArguments"}}uint8_t status{{/inline}}
172-
{{#*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}}
173-
{{#*inline "doneArguments"}}{{/inline}}
174159

175160
{{#*inline "device"}}mDevices[kIdentity{{asUpperCamelCase identity}}]{{/inline}}
176161
CHIP_ERROR {{>testCommand}}()
@@ -212,11 +197,11 @@ class {{filename}}: public TestCommand
212197
{{/chip_tests_item_parameters}}
213198

214199
auto success = [](void * context, const typename RequestType::ResponseType & data) {
215-
(static_cast<{{filename}} *>(context))->OnSuccessResponse_{{index}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}data.{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
200+
(static_cast<{{filename}} *>(context))->{{>successResponse}}({{#chip_tests_item_response_parameters}}{{#not_first}}, {{/not_first}}data.{{asLowerCamelCase name}}{{/chip_tests_item_response_parameters}});
216201
};
217202

218203
auto failure = [](void * context, EmberAfStatus status) {
219-
(static_cast<{{filename}} *>(context))->OnFailureResponse_{{index}}(status);
204+
(static_cast<{{filename}} *>(context))->{{>failureResponse}}(status);
220205
};
221206

222207
ReturnErrorOnFailure(chip::Controller::{{#if isGroupCommand}}InvokeGroupCommand{{else}}InvokeCommand{{/if}}({{>device}}, this, success, failure,
@@ -225,7 +210,6 @@ class {{filename}}: public TestCommand
225210
{{> maybeTimedInteractionTimeout }}
226211
));
227212
{{> maybeWait }}
228-
{{#unless async}}return CHIP_NO_ERROR;{{/unless}}
229213
{{else}}
230214
chip::Controller::{{asUpperCamelCase cluster}}ClusterTest cluster;
231215
{{#if isGroupCommand}}
@@ -240,26 +224,26 @@ class {{filename}}: public TestCommand
240224
{{/chip_tests_item_parameters}}
241225

242226
{{#if isWriteAttribute}}
243-
{{#*inline "failureResponse"}}OnFailureCallback_{{index}}{{/inline}}
244-
{{#*inline "successResponse"}}OnSuccessCallback_{{index}}{{/inline}}
245-
{{#*inline "doneResponse"}}OnDoneCallback_{{index}}{{/inline}}
246-
ReturnErrorOnFailure(cluster.WriteAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>successResponse}}, {{>failureResponse}}
227+
ReturnErrorOnFailure(cluster.WriteAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}
247228
{{~> maybeTimedInteractionTimeout ~}}
248-
{{~#if isGroupCommand}}, {{>doneResponse}}{{/if~}}
229+
{{~#if isGroupCommand}}, {{>staticDoneResponse}}{{/if~}}
249230
));
250231
{{> maybeWait }}
251-
{{#unless async}}return CHIP_NO_ERROR;{{/unless}}
252232
{{else if isReadAttribute}}
253-
{{#*inline "failureResponse"}}OnFailureCallback_{{index}}{{/inline}}
254-
{{#*inline "successResponse"}}OnSuccessCallback_{{index}}{{/inline}}
255-
{{#if async}}ReturnErrorOnFailure({{else}}return {{/if}}cluster.ReadAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>successResponse}}, {{>failureResponse}}){{#if async}}){{/if}};
233+
ReturnErrorOnFailure(cluster.ReadAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>({{#chip_tests_item_parameters}}{{asLowerCamelCase name}}Argument, {{/chip_tests_item_parameters}}this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}));
234+
{{else if isSubscribeAttribute}}
235+
ReturnErrorOnFailure(cluster.SubscribeAttribute<chip::app::Clusters::{{asUpperCamelCase cluster}}::Attributes::{{asUpperCamelCase attribute}}::TypeInfo>(this, {{>staticSuccessResponse}}, {{>staticFailureResponse}}, minIntervalArgument, maxIntervalArgument, {{>staticSubscriptionEstablished}}));
236+
{{else if isWaitForReport}}
237+
{{> subscribeDataCallback}} = {{> staticSuccessResponse}};
256238
{{else}}
257-
{{~#*inline "commandName"}}{{asUpperCamelCase commandName}}{{#if isAttribute}}Attribute{{asUpperCamelCase attribute}}{{/if}}{{/inline}}
258-
{{#if async}}ReturnErrorOnFailure({{else}}return {{/if}}cluster.{{>commandName}}({{>successCallback}}.Cancel(){{#unless isWaitForReport}}, {{>failureCallback}}.Cancel(){{/unless}}{{#chip_tests_item_parameters}}, {{asLowerCamelCase name}}Argument{{/chip_tests_item_parameters}}){{#if async}}){{/if}};
239+
UNEXPECTED COMMAND: {{>commandName}}
259240
{{/if}}
260-
261241
{{/if}}
262-
{{#if async}}return WaitForMs(0);{{/if}}
242+
{{#if async}}
243+
return WaitForMs(0);
244+
{{else}}
245+
return CHIP_NO_ERROR;
246+
{{/if}}
263247
}
264248

265249
void {{>failureResponse}}({{>failureArguments}})
@@ -275,21 +259,40 @@ class {{filename}}: public TestCommand
275259
{{/if}}
276260
}
277261

262+
{{#if isSubscribeAttribute}}
263+
void {{>successResponse}}({{zapTypeToDecodableClusterObjectType attributeObject.type ns=parent.cluster isArgument=true}} value)
264+
{
265+
{{#if response.error}}
266+
ThrowSuccessResponse();
267+
{{else if response.errorWrongValue}}
268+
ThrowSuccessResponse();
269+
{{else}}
270+
if ({{> subscribeDataCallback}}) {
271+
auto callback = {{> subscribeDataCallback}};
272+
{{> subscribeDataCallback}} = nullptr;
273+
callback(this, value);
274+
}
275+
{{/if}}
276+
}
277+
278+
void {{>subscriptionEstablished}}({{> subscriptionEstablishedArguments}})
279+
{
280+
{{#if hasWaitForReport}}
281+
VerifyOrReturn(mReceivedReport_{{waitForReport.index}}, Exit("Initial report not received!"));
282+
{{/if}}
283+
{{#unless async}}NextTest();{{/unless}}
284+
}
285+
{{else}}
278286
void {{>successResponse}}({{>successArguments}})
279287
{
280288
{{~#if response.error}}
281289
ThrowSuccessResponse();
282290
{{else if response.errorWrongValue}}
283291
ThrowSuccessResponse();
284292
{{else}}
285-
{{! This block can be removed if there is a mechanism to remove the report handler}}
286293
{{#if isWaitForReport}}
287-
VerifyOrReturn(mReceivedReport_{{index}} == false, ChipLogError(chipTool, "Not Fatal: on report called more than once."));
288294
mReceivedReport_{{index}} = true;
289295
{{/if}}
290-
{{#if hasWaitForReport}}
291-
VerifyOrReturn(mReceivedReport_{{waitForReport.index}}, Exit("Initial report not received!"));
292-
{{/if}}
293296
{{#chip_tests_item_response_parameters}}
294297
{{~#*inline "item"}}{{asLowerCamelCase name}}{{#if isOptional}}.Value(){{/if}}{{/inline}}
295298
{{#if hasExpectedValue}}
@@ -318,6 +321,7 @@ class {{filename}}: public TestCommand
318321
{{#unless async}}NextTest();{{/unless}}
319322
{{/if}}
320323
}
324+
{{/if}}
321325

322326
{{#if isGroupCommand}}
323327
void {{>doneResponse}}({{>doneArguments}})

0 commit comments

Comments
 (0)