Skip to content

Commit 1bf40e8

Browse files
authored
Custom type phase 1 (#1333)
1 parent 1cc9343 commit 1bf40e8

File tree

11 files changed

+64
-36
lines changed

11 files changed

+64
-36
lines changed

src-electron/db/query-atomic.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ async function selectAtomicById(db, id) {
8282
.then((rows) => rows.map(dbMapping.map.atomic))
8383
}
8484

85+
/**
86+
* Checks if a type by a given name is signed.
87+
*
88+
* @param {object} db - The database connection object.
89+
* @param {string} name - The name of the type.
90+
* @param {Array} sessionPackages - An array of session packages.
91+
* @returns {Promise<boolean>} - A promise that resolves to true if the type is signed, false otherwise.
92+
*/
93+
async function isTypeSignedByNameAndPackage(db, name, sessionPackages) {
94+
const sessionPackage = sessionPackages[0].packageRef
95+
const row = await dbApi.dbGet(
96+
db,
97+
`SELECT IS_SIGNED FROM ATOMIC WHERE NAME = ? AND PACKAGE_REF = ?`,
98+
[name, sessionPackage]
99+
)
100+
101+
return row ? row.IS_SIGNED === 1 : false
102+
}
103+
104+
// exports
105+
exports.isTypeSignedByNameAndPackage = isTypeSignedByNameAndPackage
85106
exports.selectAllAtomics = selectAllAtomics
86107
exports.selectAtomicType = dbCache.cacheQuery(selectAtomicType)
87108
exports.selectAtomicById = selectAtomicById

src-electron/db/query-zcl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ ORDER BY
11451145
return rows.map(dbMapping.map.endpointTypeEvent)
11461146
}
11471147

1148-
// exports
1148+
//exports
11491149
exports.selectClusterBitmaps = selectClusterBitmaps
11501150
exports.selectAllBitmapFields = selectAllBitmapFields
11511151
exports.selectAllBitmapFieldsById = selectAllBitmapFieldsById

src-electron/generator/helper-endpointconfig.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,13 @@ function asMEI(manufacturerCode, code) {
645645

646646
// The representation of null depends on the type, so we can't use a single
647647
// macro that's defined elsewhere for "null value".
648-
function determineAttributeDefaultValue(
648+
async function determineAttributeDefaultValue(
649649
specifiedDefault,
650650
type,
651651
typeSize,
652-
isNullable
652+
isNullable,
653+
db,
654+
sessionId
653655
) {
654656
if (specifiedDefault !== null || !isNullable) {
655657
return specifiedDefault
@@ -660,7 +662,7 @@ function determineAttributeDefaultValue(
660662
return null
661663
}
662664

663-
if (types.isSignedInteger(type)) {
665+
if (await types.isSignedInteger(db, sessionId, type)) {
664666
return '0x80' + '00'.repeat(typeSize - 1)
665667
}
666668

@@ -681,7 +683,7 @@ function determineAttributeDefaultValue(
681683
* 2.) If client is included on at least one endpoint add client atts.
682684
* 3.) If server is included on at least one endpoint add server atts.
683685
*/
684-
async function collectAttributes(endpointTypes, options) {
686+
async function collectAttributes(db, sessionId, endpointTypes, options) {
685687
let commandMfgCodes = [] // Array of { index, mfgCode } objects
686688
let clusterMfgCodes = [] // Array of { index, mfgCode } objects
687689
let attributeMfgCodes = [] // Array of { index, mfgCode } objects
@@ -710,7 +712,7 @@ async function collectAttributes(endpointTypes, options) {
710712
? options.spaceForDefaultValue
711713
: 2
712714

713-
endpointTypes.forEach((ept) => {
715+
for (let ept of endpointTypes) {
714716
let endpoint = {
715717
clusterIndex: clusterIndex,
716718
clusterCount: ept.clusters.length,
@@ -728,7 +730,7 @@ async function collectAttributes(endpointTypes, options) {
728730

729731
ept.clusters.sort(zclUtil.clusterComparator)
730732

731-
ept.clusters.forEach((c) => {
733+
for (let c of ept.clusters) {
732734
let cluster = {
733735
endpointId: ept.endpointId,
734736
clusterId: asMEI(c.manufacturerCode, c.code),
@@ -754,18 +756,20 @@ async function collectAttributes(endpointTypes, options) {
754756
c.attributes.sort(zclUtil.attributeComparator)
755757

756758
// Go over all the attributes in the endpoint and add them to the list.
757-
c.attributes.forEach((a) => {
759+
for (let a of c.attributes) {
758760
// typeSize is the size of a buffer needed to hold the attribute, if
759761
// that's known.
760762
let typeSize = a.typeSize
761763
// defaultSize is the size of the attribute in the readonly defaults
762764
// store.
763765
let defaultSize = typeSize
764-
let attributeDefaultValue = determineAttributeDefaultValue(
766+
let attributeDefaultValue = await determineAttributeDefaultValue(
765767
a.defaultValue,
766768
a.type,
767769
typeSize,
768-
a.isNullable
770+
a.isNullable,
771+
db,
772+
sessionId
769773
)
770774
// Various types store the length of the actual content in bytes.
771775
// For those, we can size the default storage to be just big enough for
@@ -963,7 +967,7 @@ async function collectAttributes(endpointTypes, options) {
963967
}
964968
attributeMfgCodes.push(att)
965969
}
966-
})
970+
}
967971

968972
// Go over the commands
969973
c.commands.sort(zclUtil.commandComparator)
@@ -1051,10 +1055,10 @@ async function collectAttributes(endpointTypes, options) {
10511055
}
10521056
clusterMfgCodes.push(clt)
10531057
}
1054-
})
1058+
}
10551059
endpoint.attributeSize = endpointAttributeSize
10561060
endpointList.push(endpoint)
1057-
})
1061+
}
10581062

10591063
return {
10601064
endpointList: endpointList,
@@ -1269,7 +1273,7 @@ function endpoint_config(options) {
12691273
collectAttributeSizes(db, this.global.zclPackageIds, endpointTypes)
12701274
)
12711275
.then((endpointTypes) =>
1272-
collectAttributes(endpointTypes, collectAttributesOptions)
1276+
collectAttributes(db, sessionId, endpointTypes, collectAttributesOptions)
12731277
)
12741278
.then((collection) => {
12751279
Object.assign(newContext, collection)

src-electron/util/types.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717

1818
const queryZcl = require('../db/query-zcl.js')
19+
const queryAtomic = require('../db/query-atomic.js')
20+
const queryPackages = require('../db/query-package.js')
1921
const dbEnum = require('../../src-shared/db-enum.js')
2022
const bin = require('./bin')
2123
const env = require('./env')
@@ -272,24 +274,20 @@ function isFloat(type) {
272274
}
273275

274276
/**
275-
* Returns true if a given ZCL type is a signed integer.
276-
* @param {*} type
277-
* @returns true if type is signed integer, false otherwise
277+
* Checks if a given ZCL type is a signed integer.
278+
*
279+
* @param {object} db - The database connection object.
280+
* @param {string} sessionId - The session ID.
281+
* @param {string} type - The name of the ZCL type.
282+
* @returns {Promise<boolean>} - A promise that resolves to true if the type is a signed integer, false otherwise.
278283
*/
279-
function isSignedInteger(type) {
280-
switch (type) {
281-
case 'int8s':
282-
case 'int16s':
283-
case 'int24s':
284-
case 'int32s':
285-
case 'int40s':
286-
case 'int48s':
287-
case 'int56s':
288-
case 'int64s':
289-
return true
290-
default:
291-
return false
292-
}
284+
async function isSignedInteger(db, sessionId, type) {
285+
let sessionPackages = await queryPackages.getSessionPackages(db, sessionId)
286+
return await queryAtomic.isTypeSignedByNameAndPackage(
287+
db,
288+
type,
289+
sessionPackages
290+
)
293291
}
294292

295293
/**

src-electron/validation/validation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function validateSpecificAttribute(
115115
//Interpreting float values
116116
if (!checkAttributeBoundsFloat(attribute, endpointAttribute))
117117
defaultAttributeIssues.push('Out of range')
118-
} else if (types.isSignedInteger(attribute.type)) {
118+
} else if (await types.isSignedInteger(db, zapSessionId, attribute.type)) {
119119
if (!isValidSignedNumberString(endpointAttribute.defaultValue)) {
120120
defaultAttributeIssues.push('Invalid Integer')
121121
} else if (

test/gen-matter-4.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ test(
231231
expect(ept).toContain(
232232
`{ 0x00000005, ZAP_TYPE(ENUM8), 1, ZAP_ATTRIBUTE_MASK(EXTERNAL_STORAGE) | ZAP_ATTRIBUTE_MASK(NULLABLE), ZAP_EMPTY_DEFAULT() }, /* LastNetworkingStatus */`
233233
)
234+
expect(ept).toContain(
235+
' { 0x00000000, ZAP_TYPE(TEMPERATURE), 2, ZAP_ATTRIBUTE_MASK(NULLABLE), ZAP_SIMPLE_DEFAULT(0x8000) },'
236+
)
234237
expect(ept).toContain(
235238
'{ (uint16_t)0xFF, (uint16_t)0x64, (uint16_t)0xFFFF }, /* BallastFactorAdjustment */'
236239
)

test/resource/matter-all-clusters-file-format-2.zap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8887,12 +8887,12 @@
88878887
"code": 0,
88888888
"mfgCode": null,
88898889
"side": "server",
8890-
"type": "int16s",
8890+
"type": "temperature",
88918891
"included": 1,
88928892
"storageOption": "RAM",
88938893
"singleton": 0,
88948894
"bounded": 0,
8895-
"defaultValue": "",
8895+
"defaultValue": null,
88968896
"reportable": 1,
88978897
"minInterval": 0,
88988898
"maxInterval": 65344,

test/resource/matter-all-clusters.zap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,7 +2291,7 @@
22912291
"0x0000 | | server | 1 | 0 => GetWeeklyScheduleResponse"
22922292
],
22932293
"attributes": [
2294-
"+ | 0x0000 | | server | RAM | | | | 1 | 0 | 65344 | 0 => LocalTemperature [int16s]",
2294+
"+ | 0x0000 | | server | RAM | | | | 1 | 0 | 65344 | 0 => LocalTemperature [temperature]",
22952295
"+ | 0x0003 | | server | RAM | | | 0x02BC | 1 | 0 | 65344 | 0 => AbsMinHeatSetpointLimit [int16s]",
22962296
"+ | 0x0004 | | server | RAM | | | 0x0BB8 | 1 | 0 | 65344 | 0 => AbsMaxHeatSetpointLimit [int16s]",
22972297
"+ | 0x0005 | | server | RAM | | | 0x0640 | 1 | 0 | 65344 | 0 => AbsMinCoolSetpointLimit [int16s]",

test/resource/old-matter/chip-types.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ limitations under the License.
8282
<type id="0xFD" description="IPv6 Prefix" name="ipv6pre" composite="true"/>
8383
<type id="0xFE" description="Hardware Address" name="hwadr" composite="true"/>
8484
<type id="0xFF" description="Unknown" name="unknown" size="0" />
85+
<type id="0xD8" description="Temperature" name="temperature" size="2" analog="true" signed="true" />
8586
</atomic>
8687
</configurator>

zcl-builtin/matter/data-model/chip/chip-types.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ limitations under the License.
8181
<type id="0xFD" description="IPv6 Prefix" name="ipv6pre" composite="true"/>
8282
<type id="0xFE" description="Hardware Address" name="hwadr" composite="true"/>
8383
<type id="0xFF" description="Unknown" name="unknown" size="0" />
84+
<type id="0xD8" description="Temperature" name="temperature" size="2" analog="true" signed="true" />
8485
</atomic>
8586
</configurator>

0 commit comments

Comments
 (0)