Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / chip-tool / templates / helper.js
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
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')
25
26 const StringHelper    = require('../../../src/app/zap-templates/common/StringHelper.js');
27 const ChipTypesHelper = require('../../../src/app/zap-templates/common/ChipTypesHelper.js');
28
29 function hasSpecificResponse(commandName)
30 {
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.';
36     console.log(error);
37     throw error;
38   }
39
40   function filterCommand(cmd)
41   {
42     return cmd.clusterName == clusterName && cmd.name == (commandName + "Response");
43   }
44
45   function fn(pkgId)
46   {
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)
52   }
53
54   const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
55   return templateUtil.templatePromise(this.global, promise);
56 }
57
58 function asDelimitedCommand(name)
59 {
60   return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
61 }
62
63 function asTypeMinValue(type)
64 {
65   function fn(pkgId)
66   {
67     const options = { 'hash' : {} };
68     this.isArray  = false;
69     return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
70       const basicType = ChipTypesHelper.asBasicType(zclType);
71       switch (basicType) {
72       case 'int8_t':
73       case 'int16_t':
74       case 'int32_t':
75       case 'int64_t':
76         return 'INT' + parseInt(basicType.slice(3)) + '_MIN';
77       case 'uint8_t':
78       case 'uint16_t':
79       case 'uint32_t':
80       case 'uint64_t':
81         return '0';
82       default:
83         error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
84         throw error;
85       }
86     })
87   }
88
89   const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
90   return templateUtil.templatePromise(this.global, promise);
91 }
92
93 function asTypeMaxValue(type)
94 {
95   function fn(pkgId)
96   {
97     const options = { 'hash' : {} };
98     return zclHelper.asUnderlyingZclType.call(this, type, options).then(zclType => {
99       const basicType = ChipTypesHelper.asBasicType(zclType);
100       switch (basicType) {
101       case 'int8_t':
102       case 'int16_t':
103       case 'int32_t':
104       case 'int64_t':
105         return 'INT' + parseInt(basicType.slice(3)) + '_MAX';
106       case 'uint8_t':
107       case 'uint16_t':
108       case 'uint32_t':
109       case 'uint64_t':
110         return 'UINT' + parseInt(basicType.slice(4)) + '_MAX';
111       default:
112         return 'err';
113         error = 'Unhandled underlying type ' + zclType + ' for original type ' + type;
114         throw error;
115       }
116     })
117   }
118
119   const promise = templateUtil.ensureZclPackageId(this).then(fn.bind(this)).catch(err => console.log(err));
120   return templateUtil.templatePromise(this.global, promise);
121 }
122
123 function isStrEndsWith(str, substr)
124 {
125   return str.endsWith(substr);
126 }
127
128 //
129 // Module exports
130 //
131 exports.hasSpecificResponse = hasSpecificResponse;
132 exports.asDelimitedCommand  = asDelimitedCommand;
133 exports.asTypeMinValue      = asTypeMinValue;
134 exports.asTypeMaxValue      = asTypeMaxValue;
135 exports.isStrEndsWith       = isStrEndsWith;