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 Jan 19, 2023
1 parent fc81078 commit 50b8bfd
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src-electron/db/query-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,52 @@ 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.CLUSTER_REF,
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 = ?
AND ETE.ENDPOINT_TYPE_REF = ?
ORDER BY E.CODE
`,
[clusterId, endpointTypeId]
)

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

/**
* Deletes an endpoint.
*
Expand Down Expand Up @@ -379,6 +425,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
22 changes: 21 additions & 1 deletion src-electron/generator/helper-endpointconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ async function collectAttributes(endpointTypes, options) {
attributeSize: 0,
mask: [],
commands: [],
events: [],
functions: 'NULL',
comment: `Endpoint: ${ept.endpointId}, Cluster: ${c.name} (${c.side})`,
}
Expand Down Expand Up @@ -850,6 +851,17 @@ 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,
}
cluster.events.push(event)
})

endpointAttributeSize += clusterAttributeSize
cluster.attributeSize = clusterAttributeSize
clusterList.push(cluster)
Expand Down Expand Up @@ -1011,11 +1023,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 +1055,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,37 @@ function chip_endpoint_generated_commands_list(options) {
return templateUtil.collectBlocks(ret, options, this);
}

function chip_endpoint_generated_event_count(options) {
let count = 0;
this.clusterList.forEach((c) => {
count += c.events.length
});
return count;
}

function chip_endpoint_generated_event_list(options) {
let ret = [];
let index = 0;
this.clusterList.forEach((c) => {
let events = [];

c.events.forEach((ev) => {
events.push(`${ev.eventId} /* ${ev.name} */`);
});

if (events.length > 0) {
ret.push({ text: ` /* ${c.comment} */\\` });
ret.push({
text: ` /* EventList (index=${index}) */ \\\n ${events.join(
', \\\n '
)}, \\`,
});
index += events.length;
}
});
return templateUtil.collectBlocks(ret, options, this);
}

/**
* Return endpoint config GENERATED_CLUSTER MACRO
* To be used as a replacement of endpoint_cluster_list since this one
Expand All @@ -195,6 +227,7 @@ function chip_endpoint_generated_commands_list(options) {
function chip_endpoint_cluster_list() {
let ret = '{ \\\n';
let totalCommands = 0;
let totalEvents = 0;
this.clusterList.forEach((c) => {
let mask = '';
let functionArray = c.functions;
Expand Down Expand Up @@ -258,6 +291,17 @@ function chip_endpoint_cluster_list() {
} )`;
}

let generatedEventListVal = 'nullptr';
let generatedEventCount = 0;
c.events.forEach((event) => {
generatedEventCount++;
});

if (generatedEventCount > 0) {
totalEvents += generatedEventCount;
generatedEventListVal = `ZAP_GENERATED_EVENTS_INDEX( ${totalEvents} )`;
}

ret = ret.concat(` { \\
/* ${c.comment} */ \\
.clusterId = ${c.clusterId}, \\
Expand All @@ -268,6 +312,8 @@ function chip_endpoint_cluster_list() {
.functions = ${functionArray}, \\
.acceptedCommandList = ${acceptedCommandsListVal} ,\\
.generatedCommandList = ${generatedCommandsListVal} ,\\
.eventList = ${generatedEventListVal}, \\
.eventCount = ${generatedEventCount}, \\
},\\\n`);

totalCommands = totalCommands + acceptedCommands + generatedCommands;
Expand Down Expand Up @@ -920,6 +966,10 @@ 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
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
10 changes: 10 additions & 0 deletions test/helper-api-baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@
"isDeprecated": false,
"category": "matter"
},
{
"name": "chip_endpoint_generated_event_count",
"isDeprecated": false,
"category": "matter"
},
{
"name": "chip_endpoint_generated_event_list",
"isDeprecated": false,
"category": "matter"
},
{
"name": "chip_endpoint_generated_functions",
"isDeprecated": false,
Expand Down

0 comments on commit 50b8bfd

Please sign in to comment.