21
21
* @module Templating API: toplevel utility helpers
22
22
*/
23
23
24
- // Import Zcl helper from zap core
25
- const helperZcl = require ( '../../../third_party/zap/repo/src-electron/generator/helper-zcl.js' )
26
-
27
- /**
28
- * Dummy helper that add a string to the templates showing
29
- * if the strings matches. Use to demonstrate the use
30
- * of ZAP helper within the chip-helper environment
31
- *
32
- * @param {* } str1 : First string to compare
33
- * @param {* } str2 : Second string to comapre
34
- */
35
- function example_helper ( str1 , str2 ) {
36
- if ( helperZcl . isStrEqual ( str1 , str2 ) ) {
37
- return 'The two strings are identical'
38
- } else {
39
- return 'The two strings are different'
40
- }
41
- }
24
+ // Import helpers from zap core
25
+ const zapPath = '../../../third_party/zap/repo/src-electron/' ;
26
+ const cHelper = require ( zapPath + 'generator/helper-c.js' )
27
+ const zclHelper = require ( zapPath + 'generator/helper-zcl.js' )
28
+ const zclQuery = require ( zapPath + 'db/query-zcl.js' )
29
+ const templateUtil = require ( zapPath + 'generator/template-util.js' )
42
30
43
31
/**
44
32
* Produces the top-of-the-file header for a C file.
45
33
*
46
34
* @returns The header content
47
35
*/
48
- function chip_header ( ) {
36
+ function chip_header ( )
37
+ {
49
38
return `
50
39
/*
51
40
*
@@ -65,10 +54,137 @@ function chip_header() {
65
54
*/` ;
66
55
}
67
56
57
+ const stringShortTypes = [ 'CHAR_STRING' , 'OCTET_STRING' ] ;
58
+ const stringLongTypes = [ 'LONG_CHAR_STRING' , 'LONG_OCTET_STRING' ] ;
59
+
60
+ function isShortString ( type )
61
+ {
62
+ return stringShortTypes . includes ( type ) ;
63
+ }
64
+
65
+ function isLongString ( type )
66
+ {
67
+ return stringLongTypes . includes ( type ) ;
68
+ }
69
+
70
+ function isString ( type )
71
+ {
72
+ return isShortString ( type ) || isLongString ( type ) ;
73
+ }
74
+
75
+ function asValueIfNotPresent ( type , isArray )
76
+ {
77
+ if ( isString ( type ) || isArray ) {
78
+ return 'NULL' ;
79
+ }
80
+
81
+ function resolve ( packageId )
82
+ {
83
+ const options = { 'hash' : { } } ;
84
+ return cHelper . asUnderlyingZclType . call ( this , type , options ) . then ( zclType => {
85
+ switch ( zclType ) {
86
+ case 'uint8_t' :
87
+ return 'UINT8_MAX' ;
88
+ case 'uint16_t' :
89
+ return 'UINT16_MAX' ;
90
+ case 'uint32_t' :
91
+ return 'UINT32_MAX' ;
92
+ default :
93
+ error = 'Unhandled underlying type ' + zclType + ' for original type ' + type ;
94
+ throw error ;
95
+ }
96
+ } )
97
+ }
98
+
99
+ const promise = templateUtil . ensureZclPackageId ( this ) . then ( resolve . bind ( this ) ) . catch ( err => console . log ( err ) ) ;
100
+ return templateUtil . templatePromise ( this . global , promise )
101
+ }
102
+
103
+ // TODO Expose the readTypeLength as an additional member field of {{asUnderlyingZclType}} instead
104
+ // of having to call this method separately.
105
+ function asReadTypeLength ( type )
106
+ {
107
+ if ( isShortString ( type ) ) {
108
+ return '1u' ;
109
+ }
110
+
111
+ if ( isLongString ( type ) ) {
112
+ return '2u' ;
113
+ }
114
+
115
+ function resolve ( packageId )
116
+ {
117
+ const db = this . global . db ;
118
+
119
+ const defaultResolver = zclQuery . selectAtomicType ( db , packageId , type ) ;
120
+
121
+ const enumResolver = zclHelper . isEnum ( db , type , packageId ) . then ( result => {
122
+ return result == 'unknown' ? null : zclQuery . selectEnumByName ( db , type , packageId ) . then ( rec => {
123
+ return zclQuery . selectAtomicType ( db , packageId , rec . type ) ;
124
+ } ) ;
125
+ } ) ;
126
+
127
+ const bitmapResolver = zclHelper . isBitmap ( db , type , packageId ) . then ( result => {
128
+ return result == 'unknown' ? null : zclQuery . selectBitmapByName ( db , packageId , type ) . then ( rec => {
129
+ return zclQuery . selectAtomicType ( db , packageId , rec . type ) ;
130
+ } ) ;
131
+ } ) ;
132
+
133
+ const typeResolver = Promise . all ( [ defaultResolver , enumResolver , bitmapResolver ] ) ;
134
+ return typeResolver . then ( types => ( types . find ( type => type ) ) . size ) ;
135
+ }
136
+
137
+ const promise = templateUtil . ensureZclPackageId ( this ) . then ( resolve . bind ( this ) ) . catch ( err => console . log ( err ) ) ;
138
+ return templateUtil . templatePromise ( this . global , promise )
139
+ }
140
+
141
+ // TODO Expose the readType as an additional member field of {{asUnderlyingZclType}} instead
142
+ // of having to call this method separately.
143
+ function asReadType ( type )
144
+ {
145
+ if ( isShortString ( type ) ) {
146
+ return 'String' ;
147
+ }
148
+
149
+ if ( isLongString ( type ) ) {
150
+ return 'LongString' ;
151
+ }
152
+
153
+ function resolve ( packageId )
154
+ {
155
+ const options = { 'hash' : { } } ;
156
+ return zclHelper . asUnderlyingZclType . call ( this , type , options ) . then ( zclType => {
157
+ switch ( zclType ) {
158
+ case 'int8_t' :
159
+ case 'uint8_t' :
160
+ return 'Int8u' ;
161
+ case 'int16_t' :
162
+ case 'uint16_t' :
163
+ return 'Int16u' ;
164
+ case 'int24_t' :
165
+ case 'uint24_t' :
166
+ return 'Int24u' ;
167
+ case 'int32_t' :
168
+ case 'uint32_t' :
169
+ return 'Int32u' ;
170
+ default :
171
+ error = 'Unhandled underlying type ' + zclType + ' for original type ' + type ;
172
+ throw error ;
173
+ }
174
+ } )
175
+ }
176
+
177
+ const promise = templateUtil . ensureZclPackageId ( this ) . then ( resolve . bind ( this ) ) . catch ( err => console . log ( err ) ) ;
178
+ return templateUtil . templatePromise ( this . global , promise )
179
+ }
180
+
68
181
// WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!
69
182
//
70
183
// Note: these exports are public API. Templates that might have been created in the past and are
71
184
// available in the wild might depend on these names.
72
185
// If you rename the functions, you need to still maintain old exports list.
73
- exports . chip_header = chip_header ;
74
- exports . example_helper = example_helper ;
186
+ exports . chip_header = chip_header ;
187
+ exports . isString = isString ;
188
+ exports . asReadType = asReadType ;
189
+ exports . asReadTypeLength = asReadTypeLength ;
190
+ exports . asValueIfNotPresent = asValueIfNotPresent ;
0 commit comments