3 * Copyright (c) 2021 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 // Import helpers from zap core
19 const zapPath = '../../../third_party/zap/repo/src-electron/';
20 const queryImpexp = require(zapPath + 'db/query-impexp.js')
21 const templateUtil = require(zapPath + 'generator/template-util.js')
22 const zclHelper = require(zapPath + 'generator/helper-zcl.js')
23 const zclQuery = require(zapPath + 'db/query-zcl.js')
24 const cHelper = require(zapPath + 'generator/helper-c.js')
26 const StringHelper = require('../../../src/app/zap-templates/common/StringHelper.js');
27 const ChipTypesHelper = require('../../../src/app/zap-templates/common/ChipTypesHelper.js');
29 function hasSpecificResponse(commandName)
31 // Retrieve the clusterName and the clusterSide. If any if not available, an error will be thrown.
32 const clusterName = this.parent.name;
33 const clusterSide = this.parent.side;
34 if (clusterName == undefined || clusterSide == undefined) {
35 const error = 'chip_server_cluster_commands: Could not find relevant parent cluster.';
40 function filterCommand(cmd)
42 return cmd.clusterName == clusterName && cmd.name == (commandName + "Response");
47 const db = this.global.db;
48 return queryImpexp.exportendPointTypeIds(db, this.global.sessionId)
49 .then(endpointTypes => zclQuery.exportClustersAndEndpointDetailsFromEndpointTypes(db, endpointTypes))
50 .then(endpointsAndClusters => zclQuery.exportCommandDetailsFromAllEndpointTypesAndClusters(db, endpointsAndClusters))
51 .then(endpointCommands => endpointCommands.filter(filterCommand).length)
54 const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
55 return templateUtil.templatePromise(this.global, promise);
58 function asDelimitedCommand(name)
60 return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
63 function asTypeMinValue(type)
67 const options = { 'hash' : {} };
69 return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
70 const basicType = ChipTypesHelper.asBasicType(zclType);
76 return 'INT' + parseInt(basicType.slice(3)) + '_MIN';
83 error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
89 const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
90 return templateUtil.templatePromise(this.global, promise);
93 function asTypeMaxValue(type)
97 const options = { 'hash' : {} };
98 return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
99 const basicType = ChipTypesHelper.asBasicType(zclType);
105 return 'INT' + parseInt(basicType.slice(3)) + '_MAX';
110 return 'UINT' + parseInt(basicType.slice(4)) + '_MAX';
113 error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
119 const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
120 return templateUtil.templatePromise(this.global, promise);
123 function isStrEndsWith(str, substr)
125 return str.endsWith(substr);
131 exports.hasSpecificResponse = hasSpecificResponse;
132 exports.asDelimitedCommand = asDelimitedCommand;
133 exports.asTypeMinValue = asTypeMinValue;
134 exports.asTypeMaxValue = asTypeMaxValue;
135 exports.isStrEndsWith = isStrEndsWith;