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 asCallbackAttributeType(attributeType)
60 switch (parseInt(attributeType)) {
61 case 0x00: // nodata / No data
62 case 0x0A: // data24 / 24-bit data
63 case 0x0C: // data40 / 40-bit data
64 case 0x0D: // data48 / 48-bit data
65 case 0x0E: // data56 / 56-bit data
66 case 0x1A: // map24 / 24-bit bitmap
67 case 0x1C: // map40 / 40-bit bitmap
68 case 0x1D: // map48 / 48-bit bitmap
69 case 0x1E: // map56 / 56-bit bitmap
70 case 0x22: // uint24 / Unsigned 24-bit integer
71 case 0x24: // uint40 / Unsigned 40-bit integer
72 case 0x25: // uint48 / Unsigned 48-bit integer
73 case 0x26: // uint56 / Unsigned 56-bit integer
74 case 0x2A: // int24 / Signed 24-bit integer
75 case 0x2C: // int40 / Signed 40-bit integer
76 case 0x2D: // int48 / Signed 48-bit integer
77 case 0x2E: // int56 / Signed 56-bit integer
78 case 0x38: // semi / Semi-precision
79 case 0x39: // single / Single precision
80 case 0x3A: // double / Double precision
81 case 0x41: // octstr / Octet string
82 case 0x42: // string / Character string
83 case 0x43: // octstr16 / Long octet string
84 case 0x44: // string16 / Long character string
85 case 0x48: // array / Array
86 case 0x49: // struct / Structure
87 case 0x50: // set / Set
88 case 0x51: // bag / Bag
89 case 0xE0: // ToD / Time of day
91 case 0x08: // data8 / 8-bit data
92 case 0x18: // map8 / 8-bit bitmap
93 case 0x20: // uint8 / Unsigned 8-bit integer
94 case 0x30: // enum8 / 8-bit enumeration
96 case 0x09: // data16 / 16-bit data
97 case 0x19: // map16 / 16-bit bitmap
98 case 0x21: // uint16 / Unsigned 16-bit integer
99 case 0x31: // enum16 / 16-bit enumeration
100 case 0xE8: // clusterId / Cluster ID
101 case 0xE9: // attribId / Attribute ID
102 case 0xEA: // bacOID / BACnet OID
103 case 0xF1: // key128 / 128-bit security key
104 case 0xFF: // unk / Unknown
106 case 0x0B: // data32 / 32-bit data
107 case 0x1B: // map32 / 32-bit bitmap
108 case 0x23: // uint32 / Unsigned 32-bit integer
109 case 0xE1: // date / Date
110 case 0xE2: // UTC / UTCTime
112 case 0x0F: // data64 / 64-bit data
113 case 0x1F: // map64 / 64-bit bitmap
114 case 0x27: // uint64 / Unsigned 64-bit integer
115 case 0xF0: // EUI64 / IEEE address
117 case 0x10: // bool / Boolean
119 case 0x28: // int8 / Signed 8-bit integer
121 case 0x29: // int16 / Signed 16-bit integer
123 case 0x2B: // int32 / Signed 32-bit integer
125 case 0x2F: // int64 / Signed 64-bit integer
128 error = 'Unhandled attribute type ' + attributeType;
133 function asDelimitedCommand(name)
135 return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
138 function asTypeMinValue(type)
142 const options = { 'hash' : {} };
143 this.isArray = false;
144 return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
145 const basicType = ChipTypesHelper.asBasicType(zclType);
151 return 'INT' + parseInt(basicType.slice(3)) + '_MIN';
158 error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
164 const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
165 return templateUtil.templatePromise(this.global, promise);
168 function asTypeMaxValue(type)
172 const options = { 'hash' : {} };
173 return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
174 const basicType = ChipTypesHelper.asBasicType(zclType);
180 return 'INT' + parseInt(basicType.slice(3)) + '_MAX';
185 return 'UINT' + parseInt(basicType.slice(4)) + '_MAX';
188 error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
194 const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
195 return templateUtil.templatePromise(this.global, promise);
198 function isStrEndsWith(str, substr)
200 return str.endsWith(substr);
206 exports.hasSpecificResponse = hasSpecificResponse;
207 exports.asCallbackAttributeType = asCallbackAttributeType;
208 exports.asDelimitedCommand = asDelimitedCommand;
209 exports.asTypeMinValue = asTypeMinValue;
210 exports.asTypeMaxValue = asTypeMaxValue;
211 exports.isStrEndsWith = isStrEndsWith;