Skip to content

Commit bfc391a

Browse files
committed
DRAFT: WIP Support EventList for Matter
1 parent 7f47fc1 commit bfc391a

9 files changed

Lines changed: 140 additions & 6 deletions

File tree

cypress/matterFixtures/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"server2": "Client & Server",
2626
"previewBtnData": "access.out",
2727
"outOfRangeAmount1": "9999999999999999",
28-
"availableAttributes1": "28",
28+
"availableAttributes1": "32",
2929
"availableClusters1": "5",
3030
"enum8inputpath": "2",
3131
"int16inputpath": "1",

src-electron/db/query-endpoint.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,51 @@ ORDER BY C.CODE
265265
})
266266
}
267267

268+
/**
269+
* Retrieves endpoint cluster events.
270+
*
271+
* @param {*} db
272+
* @param {*} clusterId
273+
* @param {*} endpointTypeId
274+
* @returns promise that resolves into endpoint cluster events
275+
*/
276+
async function selectEndpointClusterEvents(db, clusterId, endpointTypeId) {
277+
let rows = await dbApi.dbAll(
278+
db,
279+
`
280+
SELECT
281+
E.EVENT_ID,
282+
E.NAME,
283+
E.CODE,
284+
E.MANUFACTURER_CODE,
285+
E.IS_OPTIONAL
286+
FROM
287+
EVENT AS E
288+
LEFT JOIN
289+
ENDPOINT_TYPE_EVENT AS ETE
290+
ON
291+
E.EVENT_ID = ETE.EVENT_REF
292+
WHERE
293+
(E.CLUSTER_REF = ? OR E.CLUSTER_REF IS NULL)
294+
AND ETE.ENDPOINT_TYPE_REF = ?
295+
ORDER BY E.MANUFACTURER_CODE, E.CODE
296+
`,
297+
[clusterId, endpointTypeId]
298+
)
299+
300+
return rows.map((row) => {
301+
return {
302+
id: row['EVENT_ID'],
303+
name: row['NAME'],
304+
code: row['CODE'],
305+
clusterId: clusterId,
306+
manufacturerCode: row['MANUFACTURER_CODE'],
307+
isOptional: dbApi.fromDbBool(row['IS_OPTIONAL']),
308+
hexCode: '0x' + bin.int16ToHex(row['CODE']),
309+
}
310+
})
311+
}
312+
268313
/**
269314
* Deletes an endpoint.
270315
*
@@ -379,6 +424,7 @@ WHERE
379424
exports.selectEndpointClusters = selectEndpointClusters
380425
exports.selectEndpointClusterAttributes = selectEndpointClusterAttributes
381426
exports.selectEndpointClusterCommands = selectEndpointClusterCommands
427+
exports.selectEndpointClusterEvents = selectEndpointClusterEvents
382428
exports.insertEndpoint = insertEndpoint
383429
exports.deleteEndpoint = deleteEndpoint
384430
exports.selectEndpoint = selectEndpoint

src-electron/generator/helper-endpointconfig.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,12 @@ async function collectAttributes(endpointTypes, options) {
513513
let commandMfgCodes = [] // Array of { index, mfgCode } objects
514514
let clusterMfgCodes = [] // Array of { index, mfgCode } objects
515515
let attributeMfgCodes = [] // Array of { index, mfgCode } objects
516+
let eventMfgCodes = [] // Array of { index, mfgCode } objects
517+
let eventList = []
516518
let attributeList = []
517519
let commandList = []
518520
let endpointList = [] // Array of { clusterIndex, clusterCount, attributeSize }
519-
let clusterList = [] // Array of { clusterId, attributeIndex, attributeCount, attributeSize, mask, functions, comment }
521+
let clusterList = [] // Array of { clusterId, attributeIndex, attributeCount, attributeSize, eventIndex, eventCount, mask, functions, comment }
520522
let longDefaults = [] // Array of strings representing bytes
521523
let longDefaultsIndex = 0
522524
let minMaxIndex = 0
@@ -531,6 +533,7 @@ async function collectAttributes(endpointTypes, options) {
531533
let reportList = [] // Array of { direction, endpoint, clusterId, attributeId, mask, mfgCode, minOrSource, maxOrEndpoint, reportableChangeOrTimeout }
532534
let longDefaultsList = [] // Array of { value, size. comment }
533535
let attributeIndex = 0
536+
let eventIndex = 0
534537
let spaceForDefaultValue =
535538
options.spaceForDefaultValue !== undefined
536539
? options.spaceForDefaultValue
@@ -563,6 +566,8 @@ async function collectAttributes(endpointTypes, options) {
563566
attributeIndex: attributeIndex,
564567
attributeCount: c.attributes.length,
565568
attributeSize: 0,
569+
eventIndex: eventIndex,
570+
eventCount: c.events.length,
566571
mask: [],
567572
commands: [],
568573
functions: 'NULL',
@@ -573,6 +578,7 @@ async function collectAttributes(endpointTypes, options) {
573578

574579
clusterIndex++
575580
attributeIndex += c.attributes.length
581+
eventIndex += c.events.length
576582

577583
c.attributes.sort(zclUtil.attributeComparator)
578584

@@ -850,6 +856,27 @@ async function collectAttributes(endpointTypes, options) {
850856
commandMfgCodes.push(mfgCmd)
851857
}
852858
})
859+
860+
// Go over the events
861+
c.events.sort(zclUtil.eventComparator)
862+
863+
c.events.forEach((ev) => {
864+
let event = {
865+
eventId: asMEI(ev.manufacturerCode, ev.code),
866+
name: ev.name,
867+
comment: cluster.comment,
868+
}
869+
eventList.push(event)
870+
871+
if (ev.manufacturerCode) {
872+
let mfgEv = {
873+
index: eventList.length - 1,
874+
mfgCode: ev.manufacturerCode,
875+
}
876+
eventMfgCodes.push(mfgEv)
877+
}
878+
})
879+
853880
endpointAttributeSize += clusterAttributeSize
854881
cluster.attributeSize = clusterAttributeSize
855882
clusterList.push(cluster)
@@ -871,7 +898,9 @@ async function collectAttributes(endpointTypes, options) {
871898
clusterList: clusterList,
872899
attributeList: attributeList,
873900
commandList: commandList,
901+
eventList: eventList,
874902
longDefaults: longDefaults,
903+
eventMfgCodes: eventMfgCodes,
875904
clusterMfgCodes: clusterMfgCodes,
876905
commandMfgCodes: commandMfgCodes,
877906
attributeMfgCodes: attributeMfgCodes,
@@ -1011,11 +1040,12 @@ function endpoint_config(options) {
10111040
ept.clusters = clusters // Put 'clusters' into endpoint
10121041
let ps = []
10131042
clusters.forEach((cl) => {
1014-
// No client-side attributes or commands (at least for
1043+
// No client-side attributes, commands, and events (at least for
10151044
// endpoint_config purposes) in Matter.
10161045
if (cl.side == dbEnum.side.client) {
10171046
cl.attributes = []
10181047
cl.commands = []
1048+
cl.events = []
10191049
return
10201050
}
10211051
ps.push(
@@ -1042,6 +1072,13 @@ function endpoint_config(options) {
10421072
cl.commands = commands
10431073
})
10441074
)
1075+
ps.push(
1076+
queryEndpoint
1077+
.selectEndpointClusterEvents(db, cl.clusterId, ept.id)
1078+
.then((events) => {
1079+
cl.events = events
1080+
})
1081+
)
10451082
})
10461083
return Promise.all(ps)
10471084
})

src-electron/generator/matter/app/zap-templates/templates/app/helper.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const TestHelper = require('../../common/ClusterTestGeneration.js');
3838
const kGlobalAttributes = [
3939
0xfff8, // GeneratedCommandList
4040
0xfff9, // AcceptedCommandList
41+
0xfffa, // EventList
4142
0xfffb, // AttributeList
4243
0xfffc, // ClusterRevision
4344
0xfffd, // FeatureMap
@@ -187,6 +188,28 @@ function chip_endpoint_generated_commands_list(options) {
187188
return templateUtil.collectBlocks(ret, options, this);
188189
}
189190

191+
function chip_endpoint_generated_event_count(options) {
192+
return this.eventList.length
193+
}
194+
195+
function chip_endpoint_generated_event_list(options) {
196+
let comment = null
197+
198+
let index = 0
199+
let ret = '{ \\\n'
200+
this.eventList.forEach((ev) => {
201+
if (ev.comment != comment) {
202+
ret += ` /* ${ev.comment} */ \\\n`
203+
ret += ` /* EventList (index=${index}) */ \\\n`
204+
comment = ev.comment
205+
}
206+
ret += ` ${ev.eventId}, /* ${ev.name} */ \\\n`
207+
index++
208+
})
209+
ret += '}\n'
210+
return ret
211+
}
212+
190213
/**
191214
* Return endpoint config GENERATED_CLUSTER MACRO
192215
* To be used as a replacement of endpoint_cluster_list since this one
@@ -268,6 +291,8 @@ function chip_endpoint_cluster_list() {
268291
.functions = ${functionArray}, \\
269292
.acceptedCommandList = ${acceptedCommandsListVal} ,\\
270293
.generatedCommandList = ${generatedCommandsListVal} ,\\
294+
.eventList = ZAP_GENERATED_EVENTS_INDEX(${c.eventIndex}), \\
295+
.eventCount = ${c.eventCount}, \\
271296
},\\\n`);
272297

273298
totalCommands = totalCommands + acceptedCommands + generatedCommands;
@@ -920,6 +945,8 @@ exports.chip_endpoint_cluster_list = chip_endpoint_cluster_list;
920945
exports.chip_endpoint_data_version_count = chip_endpoint_data_version_count;
921946
exports.chip_endpoint_generated_commands_list =
922947
chip_endpoint_generated_commands_list;
948+
exports.chip_endpoint_generated_event_count = chip_endpoint_generated_event_count;
949+
exports.chip_endpoint_generated_event_list = chip_endpoint_generated_event_list;
923950
exports.asTypedExpression = asTypedExpression;
924951
exports.asTypedLiteral = asTypedLiteral;
925952
exports.asLowerCamelCase = asLowerCamelCase;

src-electron/generator/matter/app/zap-templates/templates/chip/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ function chip_server_cluster_attributes(options) {
438438
}
439439

440440
/**
441-
* Creates block iterator over the server side cluster attributes
441+
* Creates block iterator over the server side cluster events
442442
* for a given cluster.
443443
*
444444
* This function is meant to be used inside a {{#chip_server_clusters}}

src-electron/util/zcl-util.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ function commandComparator(a, b) {
7575
return 0
7676
}
7777

78+
/**
79+
* Comparator for sorting events.
80+
*
81+
* @param {*} a
82+
* @param {*} b
83+
* @returns -1, 0 or 1
84+
*/
85+
function eventComparator(a, b) {
86+
if (a.manufacturerCode < b.manufacturerCode) return -1
87+
if (a.manufacturerCode > b.manufacturerCode) return 1
88+
89+
if (a.hexCode < b.hexCode) return -1
90+
if (a.hexCode > b.hexCode) return 1
91+
92+
return 0
93+
}
94+
7895
function findStructByName(structs, name) {
7996
for (const s of structs) {
8097
if (s.name == name) {
@@ -783,6 +800,7 @@ async function createCommandSignature(db, packageId, cmd) {
783800
exports.clusterComparator = clusterComparator
784801
exports.attributeComparator = attributeComparator
785802
exports.commandComparator = commandComparator
803+
exports.eventComparator = eventComparator
786804
exports.sortStructsByDependency = sortStructsByDependency
787805
exports.isEnum = isEnum
788806
exports.isBitmap = isBitmap

test/gen-template/matter/endpoint-config.zapt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737

3838
#define ZAP_GENERATED_COMMANDS_INDEX(index) ((chip::CommandId *) (&generatedCommands[index]))
3939

40+
#define GENERATED_EVENT_COUNT {{ chip_endpoint_generated_event_count }}
41+
#define GENERATED_EVENTS {{ chip_endpoint_generated_event_list }}
42+
43+
#define ZAP_GENERATED_EVENTS_INDEX(index) ((chip::EventId *) (&generatedEvents[index]))
44+
4045
// Cluster function static arrays
4146
#define GENERATED_FUNCTION_ARRAYS {{chip_endpoint_generated_functions}}
4247

test/test-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ exports.totalMatterClusters = 58
123123
exports.totalMatterDeviceTypes = 40
124124
exports.totalMatterCommandArgs = 424
125125
exports.totalMatterCommands = 203
126-
exports.totalMatterAttributes = 509
126+
exports.totalMatterAttributes = 510
127127
exports.totalMatterTags = 15
128128
exports.totalMatterEvents = 56
129129
exports.totalMatterEventFields = 83

zcl-builtin/matter/global-attributes.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<!--
3-
Copyright (c) 2021 Project CHIP Authors
3+
Copyright (c) 2021-2023 Project CHIP Authors
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ limitations under the License.
2121
<attribute side="client" code="0xFFFC" define="FEATURE_MAP_CLIENT" type="bitmap32" default="0" optional="true">FeatureMap</attribute>
2222
<attribute side="server" code="0xFFFC" define="FEATURE_MAP_SERVER" type="bitmap32" default="0" optional="true">FeatureMap</attribute>
2323
<attribute side="server" code="0xFFFB" define="ATTRIBUTE_LIST_SERVER" type="array" entryType="attrib_id">AttributeList</attribute>
24+
<attribute side="server" code="0xFFFA" define="EVENT_LIST" type="array" entryType="event_id">EventList</attribute>
2425
<attribute side="server" code="0xFFF9" define="ACCEPTED_COMMAND_LIST" type="array" entryType="command_id">AcceptedCommandList</attribute>
2526
<attribute side="server" code="0xFFF8" define="GENERATED_COMMAND_LIST" type="array" entryType="command_id">GeneratedCommandList</attribute>
2627

0 commit comments

Comments
 (0)