Skip to content

Commit

Permalink
Add {{chip_server_global_response}} helper for the zap templates (#8117)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored and pull[bot] committed Sep 7, 2021
1 parent a4d8434 commit 2751009
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 246 deletions.
12 changes: 6 additions & 6 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,12 @@ private:
{{#if isList}}
chip::Callback::Callback<{{asCamelCased parent.name false}}{{asCamelCased name false}}ListAttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCamelCased parent.name false}}{{asCamelCased name false}}ListAttributeCallback>(On{{asCamelCased parent.name false}}{{asCamelCased name false}}ListAttributeResponse, this);
{{else}}
chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onSuccessCallback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeResponse, this);
{{/if}}
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
};

{{#if (isWritableAttribute)}}
{{#if isWritableAttribute}}
class Write{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
{
public:
Expand Down Expand Up @@ -327,7 +327,7 @@ private:
};

{{/if}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
class Report{{asCamelCased parent.name false}}{{asCamelCased name false}}: public ModelCommand
{
public:
Expand Down Expand Up @@ -368,7 +368,7 @@ public:
private:
chip::Callback::Callback<DefaultSuccessCallback> * onSuccessCallback = new chip::Callback::Callback<DefaultSuccessCallback>(OnDefaultSuccessResponse, this);
chip::Callback::Callback<DefaultFailureCallback> * onFailureCallback = new chip::Callback::Callback<DefaultFailureCallback>(OnDefaultFailureResponse, this);
chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onReportCallback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeResponse, this);
uint16_t mMinInterval;
uint16_t mMaxInterval;
{{#if isAnalog}}
Expand All @@ -395,10 +395,10 @@ void registerCluster{{asCamelCased name false}}(Commands & commands)
make_unique<Discover{{asCamelCased name false}}Attributes>(),
{{#chip_server_cluster_attributes}}
make_unique<Read{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
{{#if (isWritableAttribute)}}
{{#if isWritableAttribute}}
make_unique<Write{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
{{/if}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
make_unique<Report{{asCamelCased parent.name false}}{{asCamelCased name false}}>(),
{{/if}}
{{/chip_server_cluster_attributes}}
Expand Down
8 changes: 4 additions & 4 deletions examples/chip-tool/templates/reporting-commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public:
{
{{#chip_clusters}}
{{#chip_server_cluster_attributes}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
delete onReport{{asCamelCased parent.name false}}{{asCamelCased name false}}Callback;
{{/if}}
{{/chip_server_cluster_attributes}}
Expand All @@ -30,7 +30,7 @@ public:
chip::app::CHIPDeviceCallbacksMgr & callbacksMgr = chip::app::CHIPDeviceCallbacksMgr::GetInstance();
{{#chip_clusters}}
{{#chip_server_cluster_attributes}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
callbacksMgr.AddReportCallback(chip::kTestDeviceNodeId, endpointId, {{asHex parent.code 4}}, {{asHex code 4}}, onReport{{asCamelCased parent.name false}}{{asCamelCased name false}}Callback->Cancel());
{{/if}}
{{/chip_server_cluster_attributes}}
Expand Down Expand Up @@ -75,8 +75,8 @@ public:
private:
{{#chip_clusters}}
{{#chip_server_cluster_attributes}}
{{#if (isReportableAttribute)}}
chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback> * onReport{{asCamelCased parent.name false}}{{asCamelCased name false}}Callback = new chip::Callback::Callback<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>(On{{asCallbackAttributeType atomicTypeId}}AttributeResponse, this);
{{#if isReportableAttribute}}
chip::Callback::Callback<{{chipCallback.name}}AttributeCallback> * onReport{{asCamelCased parent.name false}}{{asCamelCased name false}}Callback = new chip::Callback::Callback<{{chipCallback.name}}AttributeCallback>(On{{chipCallback.name}}AttributeResponse, this);
{{/if}}
{{/chip_server_cluster_attributes}}
{{/chip_clusters}}
Expand Down
148 changes: 123 additions & 25 deletions src/app/zap-templates/common/ClustersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const zclQuery = require(zapPath + 'db/query-zcl.js')
const { Deferred } = require('./Deferred.js');
const ListHelper = require('./ListHelper.js');
const StringHelper = require('./StringHelper.js');
const StructHelper = require('./StructHelper.js');
const ChipTypesHelper = require('./ChipTypesHelper.js');

//
Expand Down Expand Up @@ -118,6 +119,14 @@ function loadAttributes(packageId)
//.then(attributes => Promise.all(attributes.map(attribute => types.typeSizeAttribute(db, packageId, attribute))
}

function loadGlobalAttributes(packageId)
{
const { db, sessionId } = this.global;
return zclQuery.selectAllAttributes(db, packageId)
.then(attributes => attributes.filter(attribute => attribute.clusterRef == null))
.then(attributes => attributes.map(attribute => attribute.code));
}

//
// Load step 2
//
Expand Down Expand Up @@ -166,6 +175,37 @@ function asPutCastType(zclType)
}
}

function asChipCallback(item)
{
if (StringHelper.isString(item.type)) {
return { name : 'String', type : 'const chip::ByteSpan' };
}

if (ListHelper.isList(item.type)) {
return { name : 'List', type : null };
}

if (item.type == 'boolean') {
return { name : 'Boolean', type : 'bool' };
}

const basicType = ChipTypesHelper.asBasicType(item.chipType);
switch (basicType) {
case 'int8_t':
case 'int16_t':
case 'int32_t':
case 'int64_t':
return { name : 'Int' + basicType.replace(/[^0-9]/g, '') + 's', type : basicType };
case 'uint8_t':
case 'uint16_t':
case 'uint32_t':
case 'uint64_t':
return { name : 'Int' + basicType.replace(/[^0-9]/g, '') + 'u', type : basicType };
default:
return { name : 'Unsupported', type : null };
}
}

function getAtomic(atomics, type)
{
return atomics.find(atomic => atomic.name == type.toLowerCase());
Expand Down Expand Up @@ -321,7 +361,8 @@ function enhancedCommands(commands, types)
});

commands.forEach(command => {
command.isResponse = command.name.includes('Response');
command.isResponse = command.name.includes('Response');
command.isManufacturerSpecificCommand = !!this.mfgCode;
});

commands.forEach(command => {
Expand Down Expand Up @@ -392,14 +433,25 @@ function enhancedCommands(commands, types)
return commands;
}

function enhancedAttributes(attributes, types)
function enhancedAttributes(attributes, globalAttributes, types)
{
attributes.forEach(attribute => {
enhancedItem(attribute, types);
attribute.isGlobalAttribute = globalAttributes.includes(attribute.code);
attribute.isWritableAttribute = attribute.isWritable === 1;
attribute.isReportableAttribute = attribute.includedReportable === 1;
attribute.chipCallback = asChipCallback(attribute);
});

attributes.forEach(attribute => {
const argument = { isList : attribute.isList, name : attribute.name, chipType : attribute.chipType, type : attribute.type };
const argument = {
name : attribute.name,
type : attribute.type,
size : attribute.size,
isList : attribute.isList,
chipType : attribute.chipType,
chipCallback : attribute.chipCallback
};
attribute.arguments = [ argument ];
attribute.response = { arguments : [ argument ] };
});
Expand Down Expand Up @@ -434,12 +486,13 @@ Clusters.init = function(context, packageId) {
loadClusters.call(context),
loadCommands.call(context, packageId),
loadAttributes.call(context, packageId),
loadGlobalAttributes.call(context, packageId),
];

return Promise.all(promises).then(([types, clusters, commands, attributes]) => {
return Promise.all(promises).then(([types, clusters, commands, attributes, globalAttributes]) => {
this._clusters = clusters;
this._commands = enhancedCommands(commands, types);
this._attributes = enhancedAttributes(attributes, types);
this._attributes = enhancedAttributes(attributes, globalAttributes, types);

return this.ready.resolve();
});
Expand All @@ -461,78 +514,123 @@ function asPromise(promise)
return templateUtil.ensureZclPackageId(this).then(fn).catch(err => console.log(err));
}

//
// Helpers: Get all clusters/commands/responses/attributes.
//
const kResponseFilter = (isResponse, item) => isResponse == item.isResponse;

Clusters.getClusters = function()
{
return this.ready.then(() => this._clusters);
}

Clusters.getCommands = function(name, side)
Clusters.getCommands = function()
{
const filter = command => command.clusterName.toLowerCase() == name.toLowerCase() && command.clusterSide == side && command.isResponse == false;
return this.ready.then(() => this._commands.filter(filter));
return this.ready.then(() => this._commands.filter(kResponseFilter.bind(null, false)));
}

Clusters.getResponses = function(name, side)
Clusters.getResponses = function()
{
const filter = command => command.clusterName.toLowerCase() == name.toLowerCase() && command.clusterSide == side && command.isResponse == true;
return this.ready.then(() => this._commands.filter(filter));
return this.ready.then(() => this._commands.filter(kResponseFilter.bind(null, true)));
}

Clusters.getAttributes = function(name, side)
Clusters.getAttributes = function()
{
return this.ready.then(() => this._attributes);
}

//
// Helpers: Get by Cluster Name
//
const kNameFilter = (name, item) => name.toLowerCase() == (item.clusterName || item.name).toLowerCase();

Clusters.getCommandsByClusterName = function(name)
{
return this.getCommands().then(items => items.filter(kNameFilter.bind(null, name)));
}

Clusters.getResponsesByClusterName = function(name)
{
return this.getResponses().then(items => items.filter(kNameFilter.bind(null, name)));
}

Clusters.getAttributesByClusterName = function(name)
{
return this.ready.then(() => {
const code = this._clusters.find(cluster => cluster.name.toLowerCase() == name.toLowerCase()).id;
const filter = attribute => attribute.clusterId == code && attribute.side == side;
return this._attributes.filter(filter);
const clusterId = this._clusters.find(kNameFilter.bind(null, name)).id;
const filter = attribute => attribute.clusterId == clusterId;
return this.getAttributes().then(items => items.filter(filter));
});
}

//
// Helpers: Get by Cluster Side
//
const kSideFilter = (side, item) => side == (item.clusterSide || item.side);

Clusters.getCommandsByClusterSide = function(side)
{
return this.getCommands().then(items => items.filter(kSideFilter.bind(null, side)));
}

Clusters.getResponsesByClusterSide = function(side)
{
return this.getResponses().then(items => items.filter(kSideFilter.bind(null, side)));
}

Clusters.getAttributesByClusterSide = function(side)
{
return this.getAttributes().then(items => items.filter(kSideFilter.bind(null, side)));
}

//
// Helpers: Client
//
const kClientSideFilter = kSideFilter.bind(null, 'client');

Clusters.getClientClusters = function()
{
const filter = cluster => cluster.side == 'client';
return this.ready.then(() => this._clusters.filter(filter));
return this.getClusters().then(items => items.filter(kClientSideFilter));
}

Clusters.getClientCommands = function(name)
{
return this.getCommands(name, 'client');
return this.getCommandsByClusterName(name).then(items => items.filter(kClientSideFilter));
}

Clusters.getClientResponses = function(name)
{
return this.getResponses(name, 'client');
return this.getResponsesByClusterName(name).then(items => items.filter(kClientSideFilter));
}

Clusters.getClientAttributes = function(name)
{
return this.getAttributes(name, 'client');
return this.getAttributesByClusterName(name).then(items => items.filter(kClientSideFilter));
}

//
// Helpers: Server
//
const kServerSideFilter = kSideFilter.bind(null, 'server');

Clusters.getServerClusters = function()
{
const filter = cluster => cluster.side == 'server';
return this.ready.then(() => this._clusters.filter(filter));
return this.getClusters().then(items => items.filter(kServerSideFilter));
}

Clusters.getServerCommands = function(name)
{
return this.getCommands(name, 'server');
return this.getCommandsByClusterName(name).then(items => items.filter(kServerSideFilter));
}

Clusters.getServerResponses = function(name)
{
return this.getResponses(name, 'server');
return this.getResponsesByClusterName(name).then(items => items.filter(kServerSideFilter));
}

Clusters.getServerAttributes = function(name)
{
return this.getAttributes(name, 'server');
return this.getAttributesByClusterName(name).then(items => items.filter(kServerSideFilter));
}

//
Expand Down
6 changes: 3 additions & 3 deletions src/app/zap-templates/templates/app/CHIPClusters-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ CHIP_ERROR {{asCamelCased parent.name false}}Cluster::ReadAttribute{{asCamelCase
{{#if isList}}
{{asCamelCased parent.name false}}Cluster{{asCamelCased name false}}ListAttributeFilter
{{else}}
BasicAttributeFilter<{{asCallbackAttributeType atomicTypeId}}AttributeCallback>
BasicAttributeFilter<{{chipCallback.name}}AttributeCallback>
{{/if}}
);
}

{{#if (isWritableAttribute)}}
{{#if isWritableAttribute}}
CHIP_ERROR {{asCamelCased parent.name false}}Cluster::WriteAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, {{chipType}} value)
{
uint8_t seqNum = mDevice->GetNextSequenceNumber();
Expand All @@ -98,7 +98,7 @@ CHIP_ERROR {{asCamelCased parent.name false}}Cluster::WriteAttribute{{asCamelCas
}

{{/if}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
CHIP_ERROR {{asCamelCased parent.name false}}Cluster::ConfigureAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, uint16_t minInterval, uint16_t maxInterval{{#if isAnalog}}, {{chipType}} change{{/if}})
{
uint8_t seqNum = mDevice->GetNextSequenceNumber();
Expand Down
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 @@ -35,12 +35,12 @@ public:
CHIP_ERROR ReadAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback);
{{/chip_server_cluster_attributes}}
{{#chip_server_cluster_attributes}}
{{#if (isWritableAttribute)}}
{{#if isWritableAttribute}}
CHIP_ERROR WriteAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, {{chipType}} value);
{{/if}}
{{/chip_server_cluster_attributes}}
{{#chip_server_cluster_attributes}}
{{#if (isReportableAttribute)}}
{{#if isReportableAttribute}}
CHIP_ERROR ConfigureAttribute{{asCamelCased name false}}(Callback::Cancelable * onSuccessCallback, Callback::Cancelable * onFailureCallback, uint16_t minInterval, uint16_t maxInterval{{#if isAnalog}}, {{chipType}} change{{/if}});
CHIP_ERROR ReportAttribute{{asCamelCased name false}}(Callback::Cancelable * onReportCallback);
{{/if}}
Expand Down
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/attribute-size-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ uint16_t emberAfCopyList(ClusterId clusterId, EmberAfAttributeMetadata * am, boo
switch (clusterId)
{
{{#chip_server_clusters}}
{{#if (chip_has_list_attributes)}}
{{#if (chip_server_has_list_attributes)}}
case {{asHex code 4}}: // {{name}} Cluster
{
uint16_t entryOffset = kSizeLengthInBytes;
Expand Down Expand Up @@ -152,7 +152,7 @@ uint16_t emberAfAttributeValueListSize(ClusterId clusterId, AttributeId attribut
switch (clusterId)
{
{{#chip_server_clusters}}
{{#if (chip_has_list_attributes)}}
{{#if (chip_server_has_list_attributes)}}
case {{asHex code 4}}: // {{name}} Cluster
switch (attributeId)
{
Expand Down
Loading

0 comments on commit 2751009

Please sign in to comment.