-
Notifications
You must be signed in to change notification settings - Fork 87
Fixing the generation for Attribute Min Max used in endpoint_config.h #1617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -613,7 +613,8 @@ function endpoint_attribute_min_max_list(options) { | |
|
||
if (isNaN(def)) def = 0 | ||
if (isNaN(min)) min = 0 | ||
if (isNaN(max)) max = 0xffff | ||
if (isNaN(max)) max = '0x' + 'FF'.repeat(mm.typeSize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the correct max for a signed type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
let defS = | ||
(def >= 0 ? '' : '-') + '0x' + Math.abs(def).toString(16).toUpperCase() | ||
let minS = | ||
|
@@ -627,13 +628,19 @@ function endpoint_attribute_min_max_list(options) { | |
.forEach((tok) => { | ||
switch (tok) { | ||
case 'def': | ||
defMinMaxItems.push(`(uint16_t)${defS}`) | ||
defMinMaxItems.push( | ||
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${defS}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this will completely break things, since the actual type those structs store is in fact uint16_t, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did not cross check that. Just fixed this based on the typecasting being wrong |
||
) | ||
break | ||
case 'min': | ||
defMinMaxItems.push(`(uint16_t)${minS}`) | ||
defMinMaxItems.push( | ||
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${minS}` | ||
) | ||
break | ||
case 'max': | ||
defMinMaxItems.push(`(uint16_t)${maxS}`) | ||
defMinMaxItems.push( | ||
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${maxS}` | ||
) | ||
break | ||
} | ||
}) | ||
|
@@ -854,7 +861,13 @@ async function determineAttributeDefaultValue( | |
* 2.) If client is included on at least one endpoint add client atts. | ||
* 3.) If server is included on at least one endpoint add server atts. | ||
*/ | ||
async function collectAttributes(db, sessionId, endpointTypes, options) { | ||
async function collectAttributes( | ||
db, | ||
sessionId, | ||
endpointTypes, | ||
options, | ||
zclPackageIds | ||
) { | ||
let commandMfgCodes = [] // Array of { index, mfgCode } objects | ||
let clusterMfgCodes = [] // Array of { index, mfgCode } objects | ||
let attributeMfgCodes = [] // Array of { index, mfgCode } objects | ||
|
@@ -1028,14 +1041,44 @@ async function collectAttributes(db, sessionId, endpointTypes, options) { | |
let mask = [] | ||
if ((a.min != null || a.max != null) && a.isWritable) { | ||
mask.push('min_max') | ||
let type_size_and_sign = await types.getSignAndSizeOfZclType( | ||
db, | ||
a.type, | ||
zclPackageIds | ||
) | ||
let min, max | ||
if (a.min != null) { | ||
min = a.min | ||
} else { | ||
if (type_size_and_sign.isTypeSigned) { | ||
// Signed min: -2^(typeSize*8-1) | ||
min = -(2 ** (typeSize * 8 - 1)) | ||
} else { | ||
// Unsigned min: 0 | ||
min = 0 | ||
} | ||
} | ||
if (a.max != null) { | ||
max = a.max | ||
} else { | ||
if (type_size_and_sign.isTypeSigned) { | ||
// Signed max: 2^(typeSize*8-1) - 1 | ||
max = 2 ** (typeSize * 8 - 1) - 1 | ||
} else { | ||
// Unsigned max: 2^(typeSize*8) - 1 | ||
max = 2 ** (typeSize * 8) - 1 | ||
} | ||
} | ||
Comment on lines
+1049
to
+1071
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The calculation of default To ensure correctness for all integer sizes, these calculations should be performed using let min = a.min
let max = a.max
if (min == null) {
if (type_size_and_sign.isTypeSigned) {
// Signed min: -2^(typeSize*8-1)
min = -(2n ** (BigInt(typeSize) * 8n - 1n))
} else {
// Unsigned min: 0
min = 0n
}
}
if (max == null) {
if |
||
let minMax = { | ||
default: attributeDefaultValue, | ||
min: a.min, | ||
max: a.max, | ||
min: min, | ||
max: max, | ||
name: a.name, | ||
comment: cluster.comment, | ||
typeSize: typeSize | ||
typeSize: typeSize, | ||
isTypeSigned: type_size_and_sign.isTypeSigned | ||
} | ||
|
||
attributeDefaultValue = `ZAP_MIN_MAX_DEFAULTS_INDEX(${minMaxIndex})` | ||
defaultValueIsMacro = true | ||
minMaxList.push(minMax) | ||
|
@@ -1442,7 +1485,13 @@ function endpoint_config(options) { | |
collectAttributeSizes(db, this.global.zclPackageIds, endpointTypes) | ||
) | ||
.then((endpointTypes) => | ||
collectAttributes(db, sessionId, endpointTypes, collectAttributesOptions) | ||
collectAttributes( | ||
db, | ||
sessionId, | ||
endpointTypes, | ||
collectAttributesOptions, | ||
this.global.zclPackageIds | ||
) | ||
) | ||
.then((collection) => { | ||
Object.assign(newContext, collection) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the correct min for a signed type. Should at least have a comment explaining why this is here in this form and why it's OK.
If the expectation is that this case never happens, shouldn't this error out instead of silently doing something wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created https://github.com/project-chip/zap/pull/1618/files