Skip to content

Commit

Permalink
DRAFT: WIP Support EventList for Matter
Browse files Browse the repository at this point in the history
  • Loading branch information
emargolis committed Feb 1, 2023
1 parent 7f47fc1 commit bfc391a
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cypress/matterFixtures/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"server2": "Client & Server",
"previewBtnData": "access.out",
"outOfRangeAmount1": "9999999999999999",
"availableAttributes1": "28",
"availableAttributes1": "32",
"availableClusters1": "5",
"enum8inputpath": "2",
"int16inputpath": "1",
Expand Down
46 changes: 46 additions & 0 deletions src-electron/db/query-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,51 @@ ORDER BY C.CODE
})
}

/**
* Retrieves endpoint cluster events.
*
* @param {*} db
* @param {*} clusterId
* @param {*} endpointTypeId
* @returns promise that resolves into endpoint cluster events
*/
async function selectEndpointClusterEvents(db, clusterId, endpointTypeId) {
let rows = await dbApi.dbAll(
db,
`
SELECT
E.EVENT_ID,
E.NAME,
E.CODE,
E.MANUFACTURER_CODE,
E.IS_OPTIONAL
FROM
EVENT AS E
LEFT JOIN
ENDPOINT_TYPE_EVENT AS ETE
ON
E.EVENT_ID = ETE.EVENT_REF
WHERE
(E.CLUSTER_REF = ? OR E.CLUSTER_REF IS NULL)
AND ETE.ENDPOINT_TYPE_REF = ?
ORDER BY E.MANUFACTURER_CODE, E.CODE
`,
[clusterId, endpointTypeId]
)

return rows.map((row) => {
return {
id: row['EVENT_ID'],
name: row['NAME'],
code: row['CODE'],
clusterId: clusterId,
manufacturerCode: row['MANUFACTURER_CODE'],
isOptional: dbApi.fromDbBool(row['IS_OPTIONAL']),
hexCode: '0x' + bin.int16ToHex(row['CODE']),
}
})
}

/**
* Deletes an endpoint.
*
Expand Down Expand Up @@ -379,6 +424,7 @@ WHERE
exports.selectEndpointClusters = selectEndpointClusters
exports.selectEndpointClusterAttributes = selectEndpointClusterAttributes
exports.selectEndpointClusterCommands = selectEndpointClusterCommands
exports.selectEndpointClusterEvents = selectEndpointClusterEvents
exports.insertEndpoint = insertEndpoint
exports.deleteEndpoint = deleteEndpoint
exports.selectEndpoint = selectEndpoint
Expand Down
41 changes: 39 additions & 2 deletions src-electron/generator/helper-endpointconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,12 @@ async function collectAttributes(endpointTypes, options) {
let commandMfgCodes = [] // Array of { index, mfgCode } objects
let clusterMfgCodes = [] // Array of { index, mfgCode } objects
let attributeMfgCodes = [] // Array of { index, mfgCode } objects
let eventMfgCodes = [] // Array of { index, mfgCode } objects
let eventList = []
let attributeList = []
let commandList = []
let endpointList = [] // Array of { clusterIndex, clusterCount, attributeSize }
let clusterList = [] // Array of { clusterId, attributeIndex, attributeCount, attributeSize, mask, functions, comment }
let clusterList = [] // Array of { clusterId, attributeIndex, attributeCount, attributeSize, eventIndex, eventCount, mask, functions, comment }
let longDefaults = [] // Array of strings representing bytes
let longDefaultsIndex = 0
let minMaxIndex = 0
Expand All @@ -531,6 +533,7 @@ async function collectAttributes(endpointTypes, options) {
let reportList = [] // Array of { direction, endpoint, clusterId, attributeId, mask, mfgCode, minOrSource, maxOrEndpoint, reportableChangeOrTimeout }
let longDefaultsList = [] // Array of { value, size. comment }
let attributeIndex = 0
let eventIndex = 0
let spaceForDefaultValue =
options.spaceForDefaultValue !== undefined
? options.spaceForDefaultValue
Expand Down Expand Up @@ -563,6 +566,8 @@ async function collectAttributes(endpointTypes, options) {
attributeIndex: attributeIndex,
attributeCount: c.attributes.length,
attributeSize: 0,
eventIndex: eventIndex,
eventCount: c.events.length,
mask: [],
commands: [],
functions: 'NULL',
Expand All @@ -573,6 +578,7 @@ async function collectAttributes(endpointTypes, options) {

clusterIndex++
attributeIndex += c.attributes.length
eventIndex += c.events.length

c.attributes.sort(zclUtil.attributeComparator)

Expand Down Expand Up @@ -850,6 +856,27 @@ async function collectAttributes(endpointTypes, options) {
commandMfgCodes.push(mfgCmd)
}
})

// Go over the events
c.events.sort(zclUtil.eventComparator)

c.events.forEach((ev) => {
let event = {
eventId: asMEI(ev.manufacturerCode, ev.code),
name: ev.name,
comment: cluster.comment,
}
eventList.push(event)

if (ev.manufacturerCode) {
let mfgEv = {
index: eventList.length - 1,
mfgCode: ev.manufacturerCode,
}
eventMfgCodes.push(mfgEv)
}
})

endpointAttributeSize += clusterAttributeSize
cluster.attributeSize = clusterAttributeSize
clusterList.push(cluster)
Expand All @@ -871,7 +898,9 @@ async function collectAttributes(endpointTypes, options) {
clusterList: clusterList,
attributeList: attributeList,
commandList: commandList,
eventList: eventList,
longDefaults: longDefaults,
eventMfgCodes: eventMfgCodes,
clusterMfgCodes: clusterMfgCodes,
commandMfgCodes: commandMfgCodes,
attributeMfgCodes: attributeMfgCodes,
Expand Down Expand Up @@ -1011,11 +1040,12 @@ function endpoint_config(options) {
ept.clusters = clusters // Put 'clusters' into endpoint
let ps = []
clusters.forEach((cl) => {
// No client-side attributes or commands (at least for
// No client-side attributes, commands, and events (at least for
// endpoint_config purposes) in Matter.
if (cl.side == dbEnum.side.client) {
cl.attributes = []
cl.commands = []
cl.events = []
return
}
ps.push(
Expand All @@ -1042,6 +1072,13 @@ function endpoint_config(options) {
cl.commands = commands
})
)
ps.push(
queryEndpoint
.selectEndpointClusterEvents(db, cl.clusterId, ept.id)
.then((events) => {
cl.events = events
})
)
})
return Promise.all(ps)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const TestHelper = require('../../common/ClusterTestGeneration.js');
const kGlobalAttributes = [
0xfff8, // GeneratedCommandList
0xfff9, // AcceptedCommandList
0xfffa, // EventList
0xfffb, // AttributeList
0xfffc, // ClusterRevision
0xfffd, // FeatureMap
Expand Down Expand Up @@ -187,6 +188,28 @@ function chip_endpoint_generated_commands_list(options) {
return templateUtil.collectBlocks(ret, options, this);
}

function chip_endpoint_generated_event_count(options) {
return this.eventList.length
}

function chip_endpoint_generated_event_list(options) {
let comment = null

let index = 0
let ret = '{ \\\n'
this.eventList.forEach((ev) => {
if (ev.comment != comment) {
ret += ` /* ${ev.comment} */ \\\n`
ret += ` /* EventList (index=${index}) */ \\\n`
comment = ev.comment
}
ret += ` ${ev.eventId}, /* ${ev.name} */ \\\n`
index++
})
ret += '}\n'
return ret
}

/**
* Return endpoint config GENERATED_CLUSTER MACRO
* To be used as a replacement of endpoint_cluster_list since this one
Expand Down Expand Up @@ -268,6 +291,8 @@ function chip_endpoint_cluster_list() {
.functions = ${functionArray}, \\
.acceptedCommandList = ${acceptedCommandsListVal} ,\\
.generatedCommandList = ${generatedCommandsListVal} ,\\
.eventList = ZAP_GENERATED_EVENTS_INDEX(${c.eventIndex}), \\
.eventCount = ${c.eventCount}, \\
},\\\n`);

totalCommands = totalCommands + acceptedCommands + generatedCommands;
Expand Down Expand Up @@ -920,6 +945,8 @@ exports.chip_endpoint_cluster_list = chip_endpoint_cluster_list;
exports.chip_endpoint_data_version_count = chip_endpoint_data_version_count;
exports.chip_endpoint_generated_commands_list =
chip_endpoint_generated_commands_list;
exports.chip_endpoint_generated_event_count = chip_endpoint_generated_event_count;
exports.chip_endpoint_generated_event_list = chip_endpoint_generated_event_list;
exports.asTypedExpression = asTypedExpression;
exports.asTypedLiteral = asTypedLiteral;
exports.asLowerCamelCase = asLowerCamelCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ function chip_server_cluster_attributes(options) {
}

/**
* Creates block iterator over the server side cluster attributes
* Creates block iterator over the server side cluster events
* for a given cluster.
*
* This function is meant to be used inside a {{#chip_server_clusters}}
Expand Down
18 changes: 18 additions & 0 deletions src-electron/util/zcl-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ function commandComparator(a, b) {
return 0
}

/**
* Comparator for sorting events.
*
* @param {*} a
* @param {*} b
* @returns -1, 0 or 1
*/
function eventComparator(a, b) {
if (a.manufacturerCode < b.manufacturerCode) return -1
if (a.manufacturerCode > b.manufacturerCode) return 1

if (a.hexCode < b.hexCode) return -1
if (a.hexCode > b.hexCode) return 1

return 0
}

function findStructByName(structs, name) {
for (const s of structs) {
if (s.name == name) {
Expand Down Expand Up @@ -783,6 +800,7 @@ async function createCommandSignature(db, packageId, cmd) {
exports.clusterComparator = clusterComparator
exports.attributeComparator = attributeComparator
exports.commandComparator = commandComparator
exports.eventComparator = eventComparator
exports.sortStructsByDependency = sortStructsByDependency
exports.isEnum = isEnum
exports.isBitmap = isBitmap
Expand Down
5 changes: 5 additions & 0 deletions test/gen-template/matter/endpoint-config.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@

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

#define GENERATED_EVENT_COUNT {{ chip_endpoint_generated_event_count }}
#define GENERATED_EVENTS {{ chip_endpoint_generated_event_list }}

#define ZAP_GENERATED_EVENTS_INDEX(index) ((chip::EventId *) (&generatedEvents[index]))

// Cluster function static arrays
#define GENERATED_FUNCTION_ARRAYS {{chip_endpoint_generated_functions}}

Expand Down
2 changes: 1 addition & 1 deletion test/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ exports.totalMatterClusters = 58
exports.totalMatterDeviceTypes = 40
exports.totalMatterCommandArgs = 424
exports.totalMatterCommands = 203
exports.totalMatterAttributes = 509
exports.totalMatterAttributes = 510
exports.totalMatterTags = 15
exports.totalMatterEvents = 56
exports.totalMatterEventFields = 83
3 changes: 2 additions & 1 deletion zcl-builtin/matter/global-attributes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2021 Project CHIP Authors
Copyright (c) 2021-2023 Project CHIP Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ limitations under the License.
<attribute side="client" code="0xFFFC" define="FEATURE_MAP_CLIENT" type="bitmap32" default="0" optional="true">FeatureMap</attribute>
<attribute side="server" code="0xFFFC" define="FEATURE_MAP_SERVER" type="bitmap32" default="0" optional="true">FeatureMap</attribute>
<attribute side="server" code="0xFFFB" define="ATTRIBUTE_LIST_SERVER" type="array" entryType="attrib_id">AttributeList</attribute>
<attribute side="server" code="0xFFFA" define="EVENT_LIST" type="array" entryType="event_id">EventList</attribute>
<attribute side="server" code="0xFFF9" define="ACCEPTED_COMMAND_LIST" type="array" entryType="command_id">AcceptedCommandList</attribute>
<attribute side="server" code="0xFFF8" define="GENERATED_COMMAND_LIST" type="array" entryType="command_id">GeneratedCommandList</attribute>

Expand Down

0 comments on commit bfc391a

Please sign in to comment.