Skip to content

Commit fd8826d

Browse files
authored
fix: correct hex padding for as_generated_default_macro to fix 24-bit attribute defaults (#1648)
- Ensure proper hex padding based on attribute size (attributeSize * 2 digits) - Fix issue where 0x186A0 for 24-bit attributes generated incorrect byte sequence - Handle both hex (0x prefixed) and decimal input values correctly - Maintain backward compatibility with existing test expectations - Fixes customer-reported issue where 24-bit values were 16x larger than expected - The padding logic now ensures that hex values like 0x186A0 are properly padded to 0x0186A0 for 3-byte attributes, generating the correct byte sequence 0x01, 0x86, 0xA0 instead of 0x18, 0x6A, 0x0. - adding test for little endian as well - JIRA: ZAPP-1658
1 parent b7e6050 commit fd8826d

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed

src-electron/generator/helper-zcl.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,7 @@ async function as_generated_default_macro(value, attributeSize, options) {
23902390
let temp = ''
23912391
let isCommaTerminated =
23922392
'isCommaTerminated' in options.hash ? options.hash.isCommaTerminated : true
2393+
23932394
if (attributeSize > 2) {
23942395
// String value
23952396
if (isNaN(value)) {
@@ -2403,9 +2404,17 @@ async function as_generated_default_macro(value, attributeSize, options) {
24032404
if (!isNaN(value) && value.toString().indexOf('.') != -1) {
24042405
temp = types.convertFloatToBigEndian(value, attributeSize)
24052406
} else {
2406-
if (value > 0) {
2407-
// Positive value
2408-
temp = helperC.asHex(value, null, null)
2407+
if (value >= 0) {
2408+
// Positive or zero value - ensure proper padding for the attribute size
2409+
const ret = value.trim ? value.trim() : value.toString()
2410+
let hexValue
2411+
if (ret.startsWith('0x') || ret.startsWith('0X')) {
2412+
hexValue = ret.slice(2)
2413+
} else {
2414+
hexValue = parseInt(ret, 10).toString(16)
2415+
}
2416+
const requiredHexDigits = attributeSize * 2
2417+
temp = `0x${hexValue.toUpperCase().padStart(requiredHexDigits, '0')}`
24092418
} else {
24102419
// Negative value
24112420
temp = types.convertIntToBigEndian(value, attributeSize)
@@ -2430,9 +2439,13 @@ async function as_generated_default_macro(value, attributeSize, options) {
24302439
.reverse()
24312440
.join(' ')
24322441
}
2442+
2443+
// Remove trailing spaces but preserve the comma structure
2444+
default_macro_signature = default_macro_signature.trim()
2445+
24332446
return isCommaTerminated
24342447
? default_macro_signature
2435-
: default_macro_signature.trim().slice(0, -1)
2448+
: default_macro_signature.slice(0, -1)
24362449
}
24372450

24382451
/**

test/gen-zigbee-3.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ test(
133133
let cfgVer2 = genResult.content['zap-config-version-2.h']
134134
// Test GENERATED_DEFAULTS
135135
expect(cfgVer2).toContain(
136-
'0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0,DEFAULT value for cluster: Over the Air Bootloading, attribute: OTA Upgrade Server ID, side: client*/'
136+
'0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 0,DEFAULT value for cluster: Over the Air Bootloading, attribute: OTA Upgrade Server ID, side: client*/'
137137
)
138138
// Test GENERATED_ATTRIBUTE_COUNT
139139
expect(cfgVer2).toContain('#define GENERATED_ATTRIBUTE_COUNT 81')

test/gen-zigbee-4.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ test(
145145

146146
// Test GENERATED_DEFAULTS little endian
147147
expect(cfgVer2).toContain(
148-
'0x2F, 0xAE, 0x0F, /* 0,DEFAULT value for cluster: Green Power, attribute: gps functionality, side: server*/'
148+
'0x2F, 0xAE, 0x0F, /* 0,DEFAULT value for cluster: Green Power, attribute: gps functionality, side: server*/'
149149
)
150150

151151
// Test GENERATED_DEFAULTS big endian for attribute of size > 8

test/helpers.test.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ test(
241241
return zclHelper
242242
.as_generated_default_macro('0x003840', 3, options)
243243
.then((res) => {
244-
return expect(res).toBe('0x40, 0x38, 0x00, ')
244+
return expect(res).toBe('0x40, 0x38, 0x00,')
245245
})
246246
},
247247
testUtil.timeout.short()
@@ -254,7 +254,7 @@ test(
254254
return zclHelper
255255
.as_generated_default_macro('0x003840', 3, options)
256256
.then((res) => {
257-
return expect(res).toBe(' 0x00, 0x38, 0x40,')
257+
return expect(res).toBe('0x00, 0x38, 0x40,')
258258
})
259259
},
260260
testUtil.timeout.short()
@@ -266,7 +266,7 @@ test(
266266
let options = { hash: { endian: 'little' } }
267267
return zclHelper
268268
.as_generated_default_macro('0x00003840', 4, options)
269-
.then((res) => expect(res).toBe('0x40, 0x38, 0x00, 0x00, '))
269+
.then((res) => expect(res).toBe('0x40, 0x38, 0x00, 0x00,'))
270270
},
271271
testUtil.timeout.short()
272272
)
@@ -277,7 +277,7 @@ test(
277277
let options = { hash: { endian: 'big' } }
278278
return zclHelper
279279
.as_generated_default_macro('0x00003840', 4, options)
280-
.then((res) => expect(res).toBe(' 0x00, 0x00, 0x38, 0x40,'))
280+
.then((res) => expect(res).toBe('0x00, 0x00, 0x38, 0x40,'))
281281
},
282282
testUtil.timeout.short()
283283
)
@@ -289,7 +289,7 @@ test(
289289
return zclHelper
290290
.as_generated_default_macro('-5', 8, options)
291291
.then((res) =>
292-
expect(res).toBe(' 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFB,')
292+
expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFB,')
293293
)
294294
},
295295
testUtil.timeout.short()
@@ -302,7 +302,7 @@ test(
302302
return zclHelper
303303
.as_generated_default_macro('-5', 8, options)
304304
.then((res) =>
305-
expect(res).toBe('0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ')
305+
expect(res).toBe('0xFB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,')
306306
)
307307
},
308308
testUtil.timeout.short()
@@ -314,7 +314,7 @@ test(
314314
let options = { hash: { endian: 'big' } }
315315
return zclHelper
316316
.as_generated_default_macro('-5', 5, options)
317-
.then((res) => expect(res).toBe(' 0xFF, 0xFF, 0xFF, 0xFF, 0xFB,'))
317+
.then((res) => expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFB,'))
318318
},
319319
testUtil.timeout.short()
320320
)
@@ -325,7 +325,7 @@ test(
325325
let options = { hash: { endian: 'little' } }
326326
return zclHelper
327327
.as_generated_default_macro('-5', 5, options)
328-
.then((res) => expect(res).toBe('0xFB, 0xFF, 0xFF, 0xFF, 0xFF, '))
328+
.then((res) => expect(res).toBe('0xFB, 0xFF, 0xFF, 0xFF, 0xFF,'))
329329
},
330330
testUtil.timeout.short()
331331
)
@@ -337,7 +337,7 @@ test(
337337
return zclHelper
338338
.as_generated_default_macro('17.0', 8, options)
339339
.then((res) =>
340-
expect(res).toBe('0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x40, ')
340+
expect(res).toBe('0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x40,')
341341
)
342342
},
343343
testUtil.timeout.short()
@@ -350,7 +350,7 @@ test(
350350
return zclHelper
351351
.as_generated_default_macro('17.0', 8, options)
352352
.then((res) =>
353-
expect(res).toBe(' 0x40, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,')
353+
expect(res).toBe('0x40, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,')
354354
)
355355
},
356356
testUtil.timeout.short()
@@ -363,7 +363,7 @@ test(
363363
return zclHelper
364364
.as_generated_default_macro('-17.0', 8, options)
365365
.then((res) =>
366-
expect(res).toBe('0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0xC0, ')
366+
expect(res).toBe('0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0xC0,')
367367
)
368368
},
369369
testUtil.timeout.short()
@@ -376,19 +376,52 @@ test(
376376
return zclHelper
377377
.as_generated_default_macro('-17.0', 8, options)
378378
.then((res) =>
379-
expect(res).toBe(' 0xC0, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,')
379+
expect(res).toBe('0xC0, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,')
380380
)
381381
},
382382
testUtil.timeout.short()
383383
)
384384

385+
test(
386+
'Generated Macro for 3 byte integer big endian with no padding',
387+
() => {
388+
let options = { hash: { endian: 'big' } }
389+
return zclHelper
390+
.as_generated_default_macro('0x186A0', 3, options)
391+
.then((res) => expect(res).toBe('0x01, 0x86, 0xA0,'))
392+
},
393+
testUtil.timeout.short()
394+
)
395+
396+
test(
397+
'Generated Macro for 3 byte integer big endian with padding',
398+
() => {
399+
let options = { hash: { endian: 'little' } }
400+
return zclHelper
401+
.as_generated_default_macro('0x186A0', 3, options)
402+
.then((res) => expect(res).toBe('0xA0, 0x86, 0x01,'))
403+
},
404+
testUtil.timeout.short()
405+
)
406+
407+
test(
408+
'Generated Macro for 3 byte integer big endian with padding',
409+
() => {
410+
let options = { hash: { endian: 'big' } }
411+
return zclHelper
412+
.as_generated_default_macro('0x0186A0', 3, options)
413+
.then((res) => expect(res).toBe('0x01, 0x86, 0xA0,'))
414+
},
415+
testUtil.timeout.short()
416+
)
417+
385418
test(
386419
'Generated Macro for 5 byte integer big endian',
387420
() => {
388421
let options = { hash: { endian: 'big' } }
389422
return zclHelper
390423
.as_generated_default_macro('549755813887', 5, options)
391-
.then((res) => expect(res).toBe(' 0x7F, 0xFF, 0xFF, 0xFF, 0xFF,'))
424+
.then((res) => expect(res).toBe('0x7F, 0xFF, 0xFF, 0xFF, 0xFF,'))
392425
},
393426
testUtil.timeout.short()
394427
)
@@ -399,7 +432,7 @@ test(
399432
let options = { hash: { endian: 'little' } }
400433
return zclHelper
401434
.as_generated_default_macro('549755813887', 5, options)
402-
.then((res) => expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0x7F, '))
435+
.then((res) => expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0x7F,'))
403436
},
404437
testUtil.timeout.short()
405438
)
@@ -410,7 +443,7 @@ test(
410443
let options = { hash: { endian: 'big' } }
411444
return zclHelper
412445
.as_generated_default_macro('140737488355327', 6, options)
413-
.then((res) => expect(res).toBe(' 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,'))
446+
.then((res) => expect(res).toBe('0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,'))
414447
},
415448
testUtil.timeout.short()
416449
)
@@ -421,7 +454,7 @@ test(
421454
let options = { hash: { endian: 'little' } }
422455
return zclHelper
423456
.as_generated_default_macro('140737488355327', 6, options)
424-
.then((res) => expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, '))
457+
.then((res) => expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F,'))
425458
},
426459
testUtil.timeout.short()
427460
)
@@ -433,7 +466,7 @@ test(
433466
return zclHelper
434467
.as_generated_default_macro('140737488355327', 7, options)
435468
.then((res) =>
436-
expect(res).toBe('0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,')
469+
expect(res).toBe('0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,')
437470
)
438471
},
439472
testUtil.timeout.short()
@@ -446,7 +479,7 @@ test(
446479
return zclHelper
447480
.as_generated_default_macro('140737488355327', 7, options)
448481
.then((res) =>
449-
expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00,')
482+
expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00,')
450483
)
451484
},
452485
testUtil.timeout.short()
@@ -459,7 +492,7 @@ test(
459492
return zclHelper
460493
.as_generated_default_macro('140737488355327', 8, options)
461494
.then((res) =>
462-
expect(res).toBe('0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,')
495+
expect(res).toBe('0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,')
463496
)
464497
},
465498
testUtil.timeout.short()
@@ -472,7 +505,7 @@ test(
472505
return zclHelper
473506
.as_generated_default_macro('140737488355327', 8, options)
474507
.then((res) =>
475-
expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 0x00,')
508+
expect(res).toBe('0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00, 0x00,')
476509
)
477510
},
478511
testUtil.timeout.short()
@@ -484,7 +517,7 @@ test(
484517
let options = { hash: { endian: 'little' } }
485518
return zclHelper
486519
.as_generated_default_macro('1600', 4, options)
487-
.then((res) => expect(res).toBe('0x40, 0x06, 0x00, 0x00,'))
520+
.then((res) => expect(res).toBe('0x40, 0x06, 0x00, 0x00,'))
488521
},
489522
testUtil.timeout.short()
490523
)
@@ -495,7 +528,7 @@ test(
495528
let options = { hash: { endian: 'big' } }
496529
return zclHelper
497530
.as_generated_default_macro('1600', 4, options)
498-
.then((res) => expect(res).toBe('0x00, 0x00, 0x06, 0x40,'))
531+
.then((res) => expect(res).toBe('0x00, 0x00, 0x06, 0x40,'))
499532
},
500533
testUtil.timeout.short()
501534
)

0 commit comments

Comments
 (0)