Skip to content

Commit

Permalink
Replacing stateful helpers with stateless helpers (#25850)
Browse files Browse the repository at this point in the history
* Replacing stateful helpers with stateless helpers
- Replacing chip_client_clusters, chip_cluster_commands, chip_cluster_responses and chip_cluster_command_arguments with all_user_clusters, zcl_commands, zcl_command_responses and zcl_command_arguments respectively in templates
- Updating the required zap version in sdk.json
- updating minimum version of zap
- Github: ZAP#971

* Using ../ when the partial with arguments is inside an if case which is under a block helper does not make it retrieve the parent context. However using parent. within the same if case retrieves the parent block helper's context. It is weird that ../ and parent. are not behaving the same way inside an inline partial with arguments.
Therefore moving the inline outside the if case and switching back to ../manufacturerCode and ../code instead of ../../manufacturerCode and ../../code
Github: ZAP#971
  • Loading branch information
brdandu authored and pull[bot] committed Feb 14, 2024
1 parent 6818701 commit 3263785
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 126 deletions.
16 changes: 8 additions & 8 deletions examples/chip-tool/templates/logging/DataModelLogger-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ CHIP_ERROR DataModelLogger::LogValue(const char * label, size_t indent, const {{
{{/zcl_events}}
{{/zcl_clusters}}

{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#zcl_commands_source_server}}
CHIP_ERROR DataModelLogger::LogValue(const char * label, size_t indent, const {{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & value)
{
Expand All @@ -58,7 +58,7 @@ CHIP_ERROR DataModelLogger::LogValue(const char * label, size_t indent, const {{
return CHIP_NO_ERROR;
}
{{/zcl_commands_source_server}}
{{/chip_client_clusters}}
{{/all_user_clusters}}

CHIP_ERROR DataModelLogger::LogAttribute(const chip::app::ConcreteDataAttributePath & path, chip::TLV::TLVReader * data)
{
Expand All @@ -67,7 +67,7 @@ CHIP_ERROR DataModelLogger::LogAttribute(const chip::app::ConcreteDataAttributeP

switch (path.mClusterId)
{
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#zcl_attributes_server}}
{{#first}}
case {{asUpperCamelCase parent.name}}::Id:
Expand All @@ -87,7 +87,7 @@ CHIP_ERROR DataModelLogger::LogAttribute(const chip::app::ConcreteDataAttributeP
}
{{/last}}
{{/zcl_attributes_server}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
default:
break;
}
Expand All @@ -102,7 +102,7 @@ CHIP_ERROR DataModelLogger::LogCommand(const chip::app::ConcreteCommandPath & pa

switch (path.mClusterId)
{
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#zcl_commands_source_server}}
{{#first}}
case {{asUpperCamelCase parent.name}}::Id:
Expand All @@ -122,7 +122,7 @@ CHIP_ERROR DataModelLogger::LogCommand(const chip::app::ConcreteCommandPath & pa
}
{{/last}}
{{/zcl_commands_source_server}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
default:
break;
}
Expand Down Expand Up @@ -158,7 +158,7 @@ CHIP_ERROR DataModelLogger::LogEvent(const chip::app::EventHeader & header, chip

switch (header.mPath.mClusterId)
{
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#chip_server_cluster_events}}
{{#first}}
case {{asUpperCamelCase parent.name}}::Id:
Expand All @@ -178,7 +178,7 @@ CHIP_ERROR DataModelLogger::LogEvent(const chip::app::EventHeader & header, chip
}
{{/last}}
{{/chip_server_cluster_events}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
default:
break;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/templates/logging/DataModelLogger.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::C
{{/zcl_events}}
{{/zcl_clusters}}

{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#zcl_commands_source_server}}
static CHIP_ERROR LogValue(const char * label, size_t indent, const chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::DecodableType & value);
{{/zcl_commands_source_server}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
58 changes: 31 additions & 27 deletions examples/darwin-framework-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,30 @@

{{> clusters_header}}

{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#unless (wasRemoved (asUpperCamelCase name preserveAcronyms=true))}}
{{> cluster_header}}

{{#chip_cluster_commands}}
{{#unless (wasRemoved (asUpperCamelCase clusterName preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
{{#unless (wasRemoved (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
/*
* Command {{asUpperCamelCase name}}
*/
class {{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}: public ClusterCommand
class {{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}: public ClusterCommand
{
public:
{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}(): ClusterCommand("{{cleanse_label_as_kebab_case name}}"){{#zcl_command_arguments}}{{#if_chip_complex}}, mComplex_{{asUpperCamelCase label}}(&mRequest.{{asLowerCamelCase label}}){{/if_chip_complex}}{{/zcl_command_arguments}}
{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}(): ClusterCommand("{{cleanse_label_as_kebab_case name}}"){{#zcl_command_arguments}}{{#if_chip_complex}}, mComplex_{{asUpperCamelCase label}}(&mRequest.{{asLowerCamelCase label}}){{/if_chip_complex}}{{/zcl_command_arguments}}
{
{{#chip_cluster_command_arguments}}
{{#zcl_command_arguments}}
{{#if_chip_complex}}
AddArgument("{{asUpperCamelCase label}}", &mComplex_{{asUpperCamelCase label}});
{{else if (isString type)}}
AddArgument("{{asUpperCamelCase label}}", &mRequest.{{asLowerCamelCase label}});
{{else}}
AddArgument("{{asUpperCamelCase label}}", {{as_type_min_value type language='c++'}}, {{as_type_max_value type language='c++'}}, &mRequest.{{asLowerCamelCase label}});
{{/if_chip_complex}}
{{/chip_cluster_command_arguments}}
{{/zcl_command_arguments}}
ClusterCommand::AddArguments();
}

Expand All @@ -48,19 +49,19 @@ public:
ChipLogProgress(chipTool, "Sending cluster ({{asHex parent.code 8}}) command ({{asHex code 8}}) on endpoint %u", endpointId);

dispatch_queue_t callbackQueue = dispatch_queue_create("com.chip.command", DISPATCH_QUEUE_SERIAL);
__auto_type * cluster = [[MTRBaseCluster{{asUpperCamelCase clusterName preserveAcronyms=true}} alloc] initWithDevice:device endpointID:@(endpointId) queue:callbackQueue];
__auto_type * params = [[MTR{{asUpperCamelCase clusterName preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Params alloc] init];
__auto_type * cluster = [[MTRBaseCluster{{asUpperCamelCase parent.name preserveAcronyms=true}} alloc] initWithDevice:device endpointID:@(endpointId) queue:callbackQueue];
__auto_type * params = [[MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase name preserveAcronyms=true}}Params alloc] init];
params.timedInvokeTimeoutMs = mTimedInteractionTimeoutMs.HasValue() ? [NSNumber numberWithUnsignedShort:mTimedInteractionTimeoutMs.Value()] : nil;
{{#chip_cluster_command_arguments}}
{{>decodable_value target=(concat "params." (asStructPropertyName label)) source=(concat "mRequest." (asLowerCamelCase label)) cluster=parent.clusterName type=type depth=0}}
{{/chip_cluster_command_arguments}}
{{#zcl_command_arguments}}
{{>decodable_value target=(concat "params." (asStructPropertyName label)) source=(concat "mRequest." (asLowerCamelCase label)) cluster=parent.parent.name type=type depth=0}}
{{/zcl_command_arguments}}
uint16_t repeatCount = mRepeatCount.ValueOr(1);
uint16_t __block responsesNeeded = repeatCount;
while (repeatCount--)
{
[cluster {{asLowerCamelCase name}}WithParams:params completion:
{{#if hasSpecificResponse}}
^(MTR{{asUpperCamelCase clusterName preserveAcronyms=true}}Cluster{{asUpperCamelCase responseName}}Params * _Nullable values, NSError * _Nullable error) {
^(MTR{{asUpperCamelCase parent.name preserveAcronyms=true}}Cluster{{asUpperCamelCase responseName}}Params * _Nullable values, NSError * _Nullable error) {
NSLog(@"Values: %@", values);
{{else}}
^(NSError * _Nullable error) {
Expand All @@ -79,18 +80,19 @@ public:
}

private:
{{#if (hasArguments)}}
chip::app::Clusters::{{asUpperCamelCase clusterName}}::Commands::{{asUpperCamelCase name}}::Type mRequest;
{{#if hasArguments}}
chip::app::Clusters::{{asUpperCamelCase parent.name}}::Commands::{{asUpperCamelCase name}}::Type mRequest;
{{/if}}
{{#chip_cluster_command_arguments}}
{{#zcl_command_arguments}}
{{#if_chip_complex}}
TypedComplexArgument<{{zapTypeToEncodableClusterObjectType type ns=parent.parent.name}}> mComplex_{{asUpperCamelCase label}};
{{/if_chip_complex}}
{{/chip_cluster_command_arguments}}
{{/zcl_command_arguments}}
};

{{/unless}}
{{/chip_cluster_commands}}
{{/if}}
{{/zcl_commands}}

{{#zcl_attributes_server removeKeys='isOptional'}}
{{#unless (wasRemoved (asUpperCamelCase parent.name preserveAcronyms=true) attribute=(asUpperCamelCase name preserveAcronyms=true))}}
Expand Down Expand Up @@ -248,12 +250,12 @@ public:
{{/unless}}
{{/zcl_attributes_server}}
{{/unless}}
{{/chip_client_clusters}}
{{/all_user_clusters}}

/*----------------------------------------------------------------------------*\
| Register all Clusters commands |
\*----------------------------------------------------------------------------*/
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#unless (wasRemoved (asUpperCamelCase name preserveAcronyms=true))}}
void registerCluster{{asUpperCamelCase name}}(Commands & commands)
{
Expand All @@ -263,11 +265,13 @@ void registerCluster{{asUpperCamelCase name}}(Commands & commands)

commands_list clusterCommands = {
make_unique<ClusterCommand>(Id), //
{{#chip_cluster_commands}}
{{#unless (wasRemoved (asUpperCamelCase clusterName preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
make_unique<{{asUpperCamelCase clusterName}}{{asUpperCamelCase name}}>(), //
{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
{{#unless (wasRemoved (asUpperCamelCase parent.name preserveAcronyms=true) command=(asUpperCamelCase name preserveAcronyms=true))}}
make_unique<{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}>(), //
{{/unless}}
{{/chip_cluster_commands}}
{{/if}}
{{/zcl_commands}}
{{#zcl_attributes_server removeKeys='isOptional'}}
{{#first}}
make_unique<ReadAttribute>(Id), //
Expand Down Expand Up @@ -303,7 +307,7 @@ void registerCluster{{asUpperCamelCase name}}(Commands & commands)
commands.Register(clusterName, clusterCommands);
}
{{/unless}}
{{/chip_client_clusters}}
{{/all_user_clusters}}

void registerClusterAny(Commands & commands)
{
Expand All @@ -325,9 +329,9 @@ void registerClusterAny(Commands & commands)
void registerClusters(Commands & commands)
{
registerClusterAny(commands);
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#unless (wasRemoved (asUpperCamelCase name preserveAcronyms=true))}}
registerCluster{{asUpperCamelCase name}}(commands);
{{/unless}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
}
2 changes: 1 addition & 1 deletion scripts/tools/sdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"meta": {
"sdkRoot": "../..",
"description": "Matter SDK",
"requiredFeatureLevel": 77
"requiredFeatureLevel": 95
},
"zcl": {
"main": "src/app/zap-templates/zcl/zcl.json",
Expand Down
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/CHIPClientCallbacks.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#include <lib/support/Span.h>

// List specific responses
{{#chip_client_clusters}}
{{#all_user_clusters side='client'}}
{{#zcl_attributes_server removeKeys='isOptional'}}
{{#if isArray}}
typedef void (*{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}ListAttributeCallback)(void * context, {{zapTypeToDecodableClusterObjectType type ns=parent.name isArgument=true}} data);
{{/if}}
{{/zcl_attributes_server}}
{{/chip_client_clusters}}
{{/all_user_clusters}}

{{/if}}
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/CHIPClusters.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
namespace chip {
namespace Controller {

{{#chip_client_clusters}}
{{#all_user_clusters side='client'}}
class DLL_EXPORT {{asUpperCamelCase name}}Cluster : public ClusterBase
{
public:
{{asUpperCamelCase name}}Cluster(Messaging::ExchangeManager & exchangeManager, const SessionHandle & session, EndpointId endpoint) : ClusterBase(exchangeManager, session, endpoint) {}
~{{asUpperCamelCase name}}Cluster() {}
};

{{/chip_client_clusters}}
{{/all_user_clusters}}
} // namespace Controller
} // namespace chip
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ id MTRDecodeAttributeValue(const ConcreteAttributePath & aPath, TLV::TLVReader &
{
switch (aPath.mClusterId)
{
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#unless (wasRemoved (asUpperCamelCase name preserveAcronyms=true))}}
case Clusters::{{asUpperCamelCase name}}::Id: {
using namespace Clusters::{{asUpperCamelCase name}};
Expand Down Expand Up @@ -47,7 +47,7 @@ id MTRDecodeAttributeValue(const ConcreteAttributePath & aPath, TLV::TLVReader &
break;
}
{{/unless}}
{{/chip_client_clusters}}
{{/all_user_clusters}}
default: {
*aError = CHIP_ERROR_IM_MALFORMED_ATTRIBUTE_PATH_IB;
break;
Expand Down
32 changes: 18 additions & 14 deletions src/darwin/Framework/CHIP/templates/MTRBaseClusters-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using chip::System::Clock::Timeout;
using chip::System::Clock::Seconds16;

// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks): Linter is unable to locate the delete on these objects.
{{#chip_client_clusters includeAll=true}}
{{#all_user_clusters side='client'}}
{{#unless (wasRemoved (asUpperCamelCase name preserveAcronyms=true))}}
@implementation MTRBaseCluster{{asUpperCamelCase name preserveAcronyms=true}}

Expand All @@ -45,7 +45,8 @@ using chip::System::Clock::Seconds16;
return self;
}

{{#chip_cluster_commands}}
{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
{{! Takes two arguments: cluster name and command name, plus the ambient state where the command is "this" }}
{{#*inline "commandImpl"}}
{{! This is used as the implementation for both the new-name and old-name bits, so check for both here. }}
Expand All @@ -59,13 +60,13 @@ MTR{{compatClusterNameRemapping parent.name}}Cluster{{compatCommandNameRemapping
MTR{{cluster}}Cluster{{command}}Params
{{/if}}
{{/inline}}
{{#unless (hasArguments)}}
{{#unless hasArguments}}
- (void){{asLowerCamelCase name}}WithCompletion:({{>command_completion_type command=.}})completion
{
[self {{asLowerCamelCase name}}WithParams:nil completion:completion];
}
{{/unless}}
- (void){{asLowerCamelCase name}}WithParams: ({{> paramsType}} * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completion:({{>command_completion_type command=.}})completion
- (void){{asLowerCamelCase name}}WithParams: ({{> paramsType}} * {{#unless commandHasRequiredField}}_Nullable{{/unless}})params completion:({{>command_completion_type command=.}})completion
{
// Make a copy of params before we go async.
params = [params copy];
Expand Down Expand Up @@ -105,19 +106,19 @@ MTR{{cluster}}Cluster{{command}}Params
timedInvokeTimeoutMs.SetValue(10000);
}
{{/if}}
{{#chip_cluster_command_arguments}}
{{#zcl_command_arguments}}
{{#first}}
{{#unless (commandHasRequiredField parent)}}
{{#unless parent.commandHasRequiredField}}
if (params != nil) {
{{/unless}}
{{/first}}
{{>encode_value target=(concat "request." (asLowerCamelCase label)) source=(concat "params." (asStructPropertyName label)) cluster=parent.parent.name errorCode="return CHIP_ERROR_INVALID_ARGUMENT;" depth=0}}
{{#last}}
{{#unless (commandHasRequiredField parent)}}
{{#unless parent.commandHasRequiredField}}
}
{{/unless}}
{{/last}}
{{/chip_cluster_command_arguments}}
{{/zcl_command_arguments}}

return MTRStartInvokeInteraction(typedBridge, request, exchangeManager, session, successCb, failureCb, self->_endpoint, timedInvokeTimeoutMs, invokeTimeout);
});
Expand All @@ -127,7 +128,8 @@ MTR{{cluster}}Cluster{{command}}Params
{{/inline}}
{{> commandImpl cluster=(asUpperCamelCase parent.name preserveAcronyms=true)
command=(asUpperCamelCase name preserveAcronyms=true)}}
{{/chip_cluster_commands}}
{{/if}}
{{/zcl_commands}}

{{#zcl_attributes_server removeKeys='isOptional'}}
{{! This is used as the implementation for both the new-name and old-name bits, so check for both here. }}
Expand Down Expand Up @@ -248,11 +250,12 @@ reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value
(not (wasRemoved (compatClusterNameRemapping name))))}}
@implementation MTRBaseCluster{{compatClusterNameRemapping name}} (Deprecated)

{{#chip_cluster_commands}}
{{#zcl_commands}}
{{#if (is_str_equal source 'client')}}
{{! Takes two arguments: cluster name and command name, plus the ambient state where the command is "this" }}
{{#*inline "commandImpl"}}
{{#unless (wasRemoved cluster command=command)}}
- (void){{asLowerCamelCase command}}WithParams:(MTR{{cluster}}Cluster{{command}}Params * {{#unless (commandHasRequiredField .)}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=. compatRemapNames=true}})completionHandler
- (void){{asLowerCamelCase command}}WithParams:(MTR{{cluster}}Cluster{{command}}Params * {{#unless commandHasRequiredField}}_Nullable{{/unless}})params completionHandler:({{>command_completion_type command=. compatRemapNames=true}})completionHandler
{
[self {{asLowerCamelCase name}}WithParams:params completion:
{{#if hasSpecificResponse}}
Expand All @@ -265,7 +268,7 @@ reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value
{{/if}}
];
}
{{#unless (hasArguments)}}
{{#unless hasArguments}}
- (void){{asLowerCamelCase command}}WithCompletionHandler:({{>command_completion_type command=. compatRemapNames=true}})completionHandler
{
[self {{asLowerCamelCase command}}WithParams:nil completionHandler:completionHandler];
Expand All @@ -275,7 +278,8 @@ reportHandler:(void (^)({{asObjectiveCClass type parent.name}} * _Nullable value
{{/inline}}
{{> commandImpl cluster=(compatClusterNameRemapping parent.name)
command=(compatCommandNameRemapping parent.name name)}}
{{/chip_cluster_commands}}
{{/if}}
{{/zcl_commands}}

{{#zcl_attributes_server removeKeys='isOptional'}}
{{#if (and (wasIntroducedBeforeRelease "First major API revamp" (compatClusterNameRemapping parent.name) attribute=(compatAttributeNameRemapping parent.name name))
Expand Down Expand Up @@ -345,6 +349,6 @@ subscriptionEstablished:(MTRSubscriptionEstablishedHandler _Nullable)subscriptio
@end
{{/if}}

{{/chip_client_clusters}}
{{/all_user_clusters}}

// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
Loading

0 comments on commit 3263785

Please sign in to comment.