Skip to content

Commit

Permalink
Update templates to get chip-tool working with the operation credenti…
Browse files Browse the repository at this point in the history
…als cluster
  • Loading branch information
vivien-apple authored and woody-apple committed May 18, 2021
1 parent 6bfdd05 commit dea5d6e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 23 deletions.
32 changes: 19 additions & 13 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,19 @@ static void OnInt64sAttributeResponse(void * context, int64_t value)

static void OnStringAttributeResponse(void * context, const chip::ByteSpan value)
{
char * str = (char *)malloc(value.size() * sizeof(char));
memmove(str, value.data(), value.size());
str[value.size()] = '\0';
free(str);

ChipLogProgress(chipTool, "String attribute Response: %s (%" PRIu16 ")", str, strlen(str));
ChipLogProgress(chipTool, "String attribute Response: %zu", value.size());

ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
command->SetCommandExitStatus(true);
}

{{#all_user_clusters}}
{{#if (isClient side) }}
{{#chip_client_clusters}}
{{#if (user_cluster_has_enabled_command name side)}}
{{#all_user_cluster_commands}}
{{#if (isStrEqual clusterName parent.name)}}
{{#if (isCommandAvailable parent.side incoming outgoing commandSource name)}}
{{#if (isStrEndsWith name "Response")}}
static void On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}(void * context{{#zcl_command_arguments}}{{#unless (isStrEqual label "status")}}, {{asUnderlyingZclType type}} {{asSymbol label}}{{/unless}}{{/zcl_command_arguments}})
static void On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}(void * context{{#zcl_command_arguments}}{{#unless (isStrEqual label "status")}}, {{#if (isOctetString type)}}chip::ByteSpan{{else}}{{asUnderlyingZclType type}}{{/if}} {{asSymbol label}}{{/unless}}{{/zcl_command_arguments}})
{
ChipLogProgress(chipTool, "{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}");

Expand All @@ -130,8 +124,7 @@ static void On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false
{{/if}}
{{/all_user_cluster_commands}}
{{/if}}
{{/if}}
{{/all_user_clusters}}
{{/chip_client_clusters}}

{{#chip_client_clusters}}
{{#chip_server_cluster_attributes}}
Expand All @@ -145,11 +138,24 @@ static void On{{asCamelCased parent.name false}}{{asCamelCased name false}}ListA
{{#if isStruct}}
ChipLogProgress(chipTool, "{{type}}[%lu]:", i);
{{#chip_attribute_list_entryTypes}}
{{#if (isOctetString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %zu", entries[i].{{name}}.size());
{{else if (isCharString type)}}
// Currently the generated code emits `uint8_t *` for CHAR_STRING, it needs to emits chip::ByteSpan
// ChipLogProgress(Zcl, " {{asSymbol label}}: %.*s", entries[i].{{name}}.size(), entries[i].{{name}}.data());
{{else}}
ChipLogProgress(chipTool, " {{name}}: {{asPrintFormat type}}", entries[i].{{name}});
{{/if}}
{{/chip_attribute_list_entryTypes}}
{{else}}
{{#if (isOctetString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %zu", entries[i].size());
{{else if (isCharString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %.*s", entries[i].size(), entries[i].data());
{{else}}
ChipLogProgress(chipTool, "{{type}}[%lu]: {{asPrintFormat type}}", i, entries[i]);
{{/if}}
{{/if}}
}

ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
Expand Down Expand Up @@ -203,8 +209,8 @@ public:
}

private:
{{#if (hasSpecificResponse name)}}
chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallback>(On{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}Response, this);
{{#if hasSpecificResponse}}
chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased responseName false}}Callback> * onSuccessCallback = new chip::Callback::Callback<{{asCamelCased parent.name false}}Cluster{{asCamelCased responseName false}}Callback>(On{{asCamelCased parent.name false}}Cluster{{asCamelCased responseName false}}, this);
{{else}}
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
{{/if}}
Expand Down
27 changes: 27 additions & 0 deletions src/app/zap-templates/common/ClustersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,33 @@ function enhancedCommands(commands, types)
});
});

// This filter uses the assumption that a response to a command has a well defined name, such as
// (response name) == (command name + 'Response') or s/Request/Response. This is very often the case,
// but this is not always true since some clusters use the same response to answer different commands, such as the
// operational cluster.
commands.forEach(command => {
const automaticFilter = response => (response.name == (command.name + 'Response')
|| (command.name.includes('Request') && response.name == (command.name.replace('Request', 'Response'))));
const manualFilter = response => {
switch (command.name) {
case 'AddOpCert':
case 'UpdateOpCert':
case 'UpdateFabricLabel':
case 'RemoveFabric':
return response.name == 'OpCertResponse';
default:
return false;
}
};
const filter = response => automaticFilter(response) || manualFilter(response);

const response = commands.find(filter);
if (response) {
command.hasSpecificResponse = true;
command.responseName = response.name;
}
});

// At this stage, 'command.arguments' may contains 'struct'. But controllers does not know (yet) how
// to handle them. So those needs to be inlined.
commands.forEach(command => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,11 @@ bool emberAf{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}
{{#zcl_command_arguments}}
{{#if (isStrEqual label "status")}}
LogStatus(status);
{{else if (isOctetString type)}}
ChipLogProgress(Zcl, " {{asSymbol label}}: %zu", {{asSymbol label}}.size());
{{else if (isCharString type)}}
// Currently the generated code emits `uint8_t *` for CHAR_STRING, it needs to emits chip::ByteSpan
// ChipLogProgress(Zcl, " {{asSymbol label}}: %.*s", {{asSymbol label}}.size(), {{asSymbol label}}.data());
{{else}}
ChipLogProgress(Zcl, " {{asSymbol label}}: {{asPrintFormat type}}", {{asSymbol label}});
{{/if}}
Expand Down
8 changes: 0 additions & 8 deletions src/app/zap-templates/templates/chip/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,6 @@ function isManufacturerSpecificCommand()
return !!this.mfgCode;
}

function hasSpecificResponse(commandName)
{
const { clusterName, clusterSide } = checkIsInsideClusterBlock(this.parent, 'has_specific_response');
const filter = response => response.name == (commandName + 'Response');
return asPromise.call(this, Clusters.getServerResponses(clusterName).then(responses => responses.find(filter)));
}

function asCallbackAttributeType(attributeType)
{
switch (parseInt(attributeType)) {
Expand Down Expand Up @@ -432,4 +425,3 @@ exports.isWritableAttribute = isWritableAttribute;
exports.isReportableAttribute = isReportableAttribute;
exports.isManufacturerSpecificCommand = isManufacturerSpecificCommand;
exports.asCallbackAttributeType = asCallbackAttributeType;
exports.hasSpecificResponse = hasSpecificResponse;
4 changes: 2 additions & 2 deletions src/darwin/Framework/CHIP/templates/CHIPClustersObjc-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,8 @@ private:
- (void){{asCamelCased name}}:(ResponseHandler)responseHandler
{{/if}}
{
{{#if (hasSpecificResponse name)}}
CHIP{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallbackBridge * onSuccess = new CHIP{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ResponseCallbackBridge(responseHandler, [self callbackQueue]);
{{#if hasSpecificResponse}}
CHIP{{asCamelCased parent.name false}}Cluster{{asCamelCased responseName false}}CallbackBridge * onSuccess = new CHIP{{asCamelCased parent.name false}}Cluster{{asCamelCased responseName false}}CallbackBridge(responseHandler, [self callbackQueue]);
{{else}}
CHIPDefaultSuccessCallbackBridge * onSuccess = new CHIPDefaultSuccessCallbackBridge(responseHandler, [self callbackQueue]);
{{/if}}
Expand Down

0 comments on commit dea5d6e

Please sign in to comment.