Skip to content

Commit 93f5d14

Browse files
authored
Fixing the generation for Attribute Min Max used in endpoint_config.h (#1617)
- Fixing the generation of min and max values to be auto assumed and generated correctly when either min or max are mentioned instead of both min and max - Also making sure we do not always typecase everything to (uint16_t) but instead typecast the values based on size and sign - Updating existing tests - For unit testing removing max from BallastFactorAdjustment attribute and making sure 0xFF is generated for '{ (uint8_t)0xFF, (uint8_t)0x64, (uint8_t)0xFF }, /* BallastFactorAdjustment */' - Github: ZAP#1615
1 parent e1b5456 commit 93f5d14

File tree

6 files changed

+67
-22
lines changed

6 files changed

+67
-22
lines changed

src-electron/generator/helper-endpointconfig.js

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ function endpoint_attribute_min_max_list(options) {
613613

614614
if (isNaN(def)) def = 0
615615
if (isNaN(min)) min = 0
616-
if (isNaN(max)) max = 0xffff
616+
if (isNaN(max)) max = '0x' + 'FF'.repeat(mm.typeSize)
617+
617618
let defS =
618619
(def >= 0 ? '' : '-') + '0x' + Math.abs(def).toString(16).toUpperCase()
619620
let minS =
@@ -627,13 +628,19 @@ function endpoint_attribute_min_max_list(options) {
627628
.forEach((tok) => {
628629
switch (tok) {
629630
case 'def':
630-
defMinMaxItems.push(`(uint16_t)${defS}`)
631+
defMinMaxItems.push(
632+
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${defS}`
633+
)
631634
break
632635
case 'min':
633-
defMinMaxItems.push(`(uint16_t)${minS}`)
636+
defMinMaxItems.push(
637+
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${minS}`
638+
)
634639
break
635640
case 'max':
636-
defMinMaxItems.push(`(uint16_t)${maxS}`)
641+
defMinMaxItems.push(
642+
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${maxS}`
643+
)
637644
break
638645
}
639646
})
@@ -854,7 +861,13 @@ async function determineAttributeDefaultValue(
854861
* 2.) If client is included on at least one endpoint add client atts.
855862
* 3.) If server is included on at least one endpoint add server atts.
856863
*/
857-
async function collectAttributes(db, sessionId, endpointTypes, options) {
864+
async function collectAttributes(
865+
db,
866+
sessionId,
867+
endpointTypes,
868+
options,
869+
zclPackageIds
870+
) {
858871
let commandMfgCodes = [] // Array of { index, mfgCode } objects
859872
let clusterMfgCodes = [] // Array of { index, mfgCode } objects
860873
let attributeMfgCodes = [] // Array of { index, mfgCode } objects
@@ -1028,14 +1041,44 @@ async function collectAttributes(db, sessionId, endpointTypes, options) {
10281041
let mask = []
10291042
if ((a.min != null || a.max != null) && a.isWritable) {
10301043
mask.push('min_max')
1044+
let type_size_and_sign = await types.getSignAndSizeOfZclType(
1045+
db,
1046+
a.type,
1047+
zclPackageIds
1048+
)
1049+
let min, max
1050+
if (a.min != null) {
1051+
min = a.min
1052+
} else {
1053+
if (type_size_and_sign.isTypeSigned) {
1054+
// Signed min: -2^(typeSize*8-1)
1055+
min = -(2 ** (typeSize * 8 - 1))
1056+
} else {
1057+
// Unsigned min: 0
1058+
min = 0
1059+
}
1060+
}
1061+
if (a.max != null) {
1062+
max = a.max
1063+
} else {
1064+
if (type_size_and_sign.isTypeSigned) {
1065+
// Signed max: 2^(typeSize*8-1) - 1
1066+
max = 2 ** (typeSize * 8 - 1) - 1
1067+
} else {
1068+
// Unsigned max: 2^(typeSize*8) - 1
1069+
max = 2 ** (typeSize * 8) - 1
1070+
}
1071+
}
10311072
let minMax = {
10321073
default: attributeDefaultValue,
1033-
min: a.min,
1034-
max: a.max,
1074+
min: min,
1075+
max: max,
10351076
name: a.name,
10361077
comment: cluster.comment,
1037-
typeSize: typeSize
1078+
typeSize: typeSize,
1079+
isTypeSigned: type_size_and_sign.isTypeSigned
10381080
}
1081+
10391082
attributeDefaultValue = `ZAP_MIN_MAX_DEFAULTS_INDEX(${minMaxIndex})`
10401083
defaultValueIsMacro = true
10411084
minMaxList.push(minMax)
@@ -1442,7 +1485,13 @@ function endpoint_config(options) {
14421485
collectAttributeSizes(db, this.global.zclPackageIds, endpointTypes)
14431486
)
14441487
.then((endpointTypes) =>
1445-
collectAttributes(db, sessionId, endpointTypes, collectAttributesOptions)
1488+
collectAttributes(
1489+
db,
1490+
sessionId,
1491+
endpointTypes,
1492+
collectAttributesOptions,
1493+
this.global.zclPackageIds
1494+
)
14461495
)
14471496
.then((collection) => {
14481497
Object.assign(newContext, collection)

test/custom-matter-xml.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ test(
443443
' /* Endpoint: 1, Cluster: Sample Custom Cluster (server) */ \\'
444444
)
445445
expect(endpointConfig).toContain(
446-
'{ (uint16_t)0x0, (uint16_t)0x0, (uint16_t)0xFFFF }, /* Sample Mfg Specific Attribute 2 */ \\'
446+
'{ (uint8_t)0x0, (uint8_t)0x0, (uint8_t)0xFF }, /* Sample Mfg Specific Attribute 2 */ \\'
447447
)
448448

449449
let endpointOut = genResult.content['endpoints.out']
@@ -490,7 +490,7 @@ test(
490490
' /* Endpoint: 1, Cluster: Sample Custom Cluster (server) */ \\'
491491
)
492492
expect(endpointConfig).not.toContain(
493-
'{ (uint16_t)0x0, (uint16_t)0x0, (uint16_t)0xFFFF }, /* Sample Mfg Specific Attribute 2 */ \\'
493+
'{ (uint8_t)0x0, (uint8_t)0x0, (uint8_t)0xFF }, /* Sample Mfg Specific Attribute 2 */ \\'
494494
)
495495

496496
// create state from database and session to verify contents of .zap file

test/gen-matter-3-1.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,10 @@ test(
126126
`{ 0x00000005, ZAP_TYPE(ENUM8), 1, ZAP_ATTRIBUTE_MASK(EXTERNAL_STORAGE) | ZAP_ATTRIBUTE_MASK(NULLABLE), ZAP_EMPTY_DEFAULT() }, /* LastNetworkingStatus */`
127127
)
128128
expect(ept).toContain(
129-
'{ (uint16_t)0xFF, (uint16_t)0x64, (uint16_t)0xFFFF }, /* BallastFactorAdjustment */'
129+
'{ (uint8_t)0xFF, (uint8_t)0x64, (uint8_t)0xFF }, /* BallastFactorAdjustment */'
130130
)
131131
expect(ept).toContain(`6, 'C', 'o', 'f', 'f', 'e', 'e', \\`)
132-
expect(ept).toContain(
133-
'{ (uint16_t)-0x64, (uint16_t)-0x96, (uint16_t)0xC8 }'
134-
)
132+
expect(ept).toContain('{ (int16_t)-0x64, (int16_t)-0x96, (int16_t)0xC8 }')
135133
expect(ept).toContain('#define GENERATED_MIN_MAX_DEFAULT_COUNT 51')
136134
expect(ept).toContain('#define GENERATED_ATTRIBUTE_COUNT 739')
137135
expect(ept).toContain(`/* EventList (index=8) */ \\
@@ -238,12 +236,10 @@ test(
238236
' { 0x00000000, ZAP_TYPE(TEMPERATURE), 2, ZAP_ATTRIBUTE_MASK(NULLABLE), ZAP_SIMPLE_DEFAULT(0x8000) },'
239237
)
240238
expect(ept).toContain(
241-
'{ (uint16_t)0xFF, (uint16_t)0x64, (uint16_t)0xFFFF }, /* BallastFactorAdjustment */'
239+
'{ (uint8_t)0xFF, (uint8_t)0x64, (uint8_t)0xFF }, /* BallastFactorAdjustment */'
242240
)
243241
expect(ept).toContain(`6, 'C', 'o', 'f', 'f', 'e', 'e', \\`)
244-
expect(ept).toContain(
245-
'{ (uint16_t)-0x64, (uint16_t)-0x96, (uint16_t)0xC8 }'
246-
)
242+
expect(ept).toContain('{ (int16_t)-0x64, (int16_t)-0x96, (int16_t)0xC8 }')
247243
expect(ept).toContain('#define GENERATED_MIN_MAX_DEFAULT_COUNT 51')
248244
expect(ept).toContain('#define GENERATED_ATTRIBUTE_COUNT 739')
249245
expect(ept).toContain(`/* EventList (index=8) */ \\

test/resource/custom-cluster/matter-conflict.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ limitations under the License.
2828
</cluster>
2929

3030
<clusterExtension code="0x0006">
31-
<attribute side="server" code="0xFFF10000" define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME_2" type="INT8U" min="0x0000" max="0xFFFF" writable="true" default="0x0000" optional="true">Sample Mfg Specific Attribute 2 Conflict</attribute>
31+
<attribute side="server" code="0xFFF10000" define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME_2" type="INT8U" min="0x0000" max="0xFF" writable="true" default="0x0000" optional="true">Sample Mfg Specific Attribute 2 Conflict</attribute>
3232
<command source="client" code="0xFFF100" name="SampleMfgSpecificOnWithTransition2Conflict" optional="true">
3333
<description>Client command that turns the device on with a transition given
3434
by the transition time in the Ember Sample transition time attribute.</description>

test/resource/custom-cluster/matter-custom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ limitations under the License.
5252

5353
<!-- Use the cluster extension Extend the on/off cluster -->
5454
<clusterExtension code="0x0006">
55-
<attribute side="server" code="0xFFF10000" define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME_2" type="INT8U" min="0x0000" max="0xFFFF" writable="true" default="0x0000" optional="true">Sample Mfg Specific Attribute 2</attribute>
55+
<attribute side="server" code="0xFFF10000" define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME_2" type="INT8U" min="0x0000" max="0xFF" writable="true" default="0x0000" optional="true">Sample Mfg Specific Attribute 2</attribute>
5656
<attribute side="server" code="0xFFF10001" define="SAMPLE_MFG_SPECIFIC_TRANSITION_TIME_4" type="INT16U" min="0x0000" max="0xFFFF" writable="true" default="0x0000" optional="true">Sample Mfg Specific Attribute 4</attribute>
5757
<command source="client" code="0xFFF100" name="SampleMfgSpecificOnWithTransition2">
5858
<description>Client command that turns the device on with a transition given

zcl-builtin/dotdot/BallastConfiguration.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ applicable to this document can be found in the LICENSE.md file.
4343
<!-- PowerOnFadeTime is deprecated -->
4444
<attribute id="0013" name="PowerOnFadeTime" type="uint16" writable="true" max="65534" default="0" deprecated="true" />
4545
<attribute id="0014" name="IntrinsicBallastFactor" type="uint8" max="254" writable="true" />
46-
<attribute id="0015" name="BallastFactorAdjustment" type="uint8" min="100" max="255" writable="true" default="255" />
46+
<attribute id="0015" name="BallastFactorAdjustment" type="uint8" min="100" writable="true" default="255" />
4747
<!-- Lamp Information Attribute Set -->
4848
<attribute id="0020" name="LampQuantity" type="uint8" max="254" />
4949
<!-- Lamp Settings Attribute Set -->

0 commit comments

Comments
 (0)