From bbec04cc5d11df7cb37995548fb65b40b2cca81e Mon Sep 17 00:00:00 2001
From: Timotej Ecimovic <timotej.ecimovic@silabs.com>
Date: Fri, 5 Feb 2021 13:59:30 -0400
Subject: [PATCH] Add support for custom ZCL device.

---
 src-electron/db/query-zcl.js          |  2 +-
 src-electron/zcl/zcl-loader-silabs.js | 28 ++++++++++++++++++++++++++-
 test/zcl-loader-consecutive.test.js   |  2 +-
 test/zcl-loader.test.js               |  2 +-
 zcl-builtin/silabs/zcl.json           |  1 +
 5 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/src-electron/db/query-zcl.js b/src-electron/db/query-zcl.js
index 3dcadfc6..18eedd78 100644
--- a/src-electron/db/query-zcl.js
+++ b/src-electron/db/query-zcl.js
@@ -1395,7 +1395,7 @@ INSERT INTO ATTRIBUTE (
  * @export
  * @param {*} db
  * @param {*} packageId
- * @param {*} data an array of objects that must contain: code, name, description
+ * @param {*} data an array of objects that must contain: domain, code, profileId, name, description
  * @returns Promise of an insertion of device types.
  */
 async function insertDeviceTypes(db, packageId, data) {
diff --git a/src-electron/zcl/zcl-loader-silabs.js b/src-electron/zcl/zcl-loader-silabs.js
index 00c98863..0283f51a 100644
--- a/src-electron/zcl/zcl-loader-silabs.js
+++ b/src-electron/zcl/zcl-loader-silabs.js
@@ -79,6 +79,8 @@ async function collectDataFromJsonFile(ctx) {
     }
 
     ctx.version = obj.version
+    ctx.supportCustomZclDevice = obj.supportCustomZclDevice
+
     env.logInfo(`Resolving: ${ctx.zclFiles}, version: ${ctx.version}`)
     resolve(ctx)
   })
@@ -142,7 +144,7 @@ async function collectDataFromPropertiesFile(ctx) {
         if (zclProps.defaults) {
           ctx.defaults = zclProps.defaults
         }
-
+        ctx.supportCustomZclDevice = zclProps.supportCustomZclDevice
         ctx.version = zclProps.version
         env.logInfo(`Resolving: ${ctx.zclFiles}, version: ${ctx.version}`)
         resolve(ctx)
@@ -1031,6 +1033,29 @@ async function loadIndividualSilabsFile(db, filePath, boundValidator) {
     })
 }
 
+/**
+ * If custom device is supported, then this method creates it.
+ *
+ * @param {*} db
+ * @param {*} ctx
+ * @returns context
+ */
+async function processCustomZclDeviceType(db, ctx) {
+  if (ctx.supportCustomZclDevice) {
+    let customDeviceTypes = []
+    customDeviceTypes.push({
+      domain: 'Custom',
+      code: 0xffff,
+      profileId: 0xffff,
+      name: 'Custom ZCL Device Type',
+      description:
+        'Custom ZCL device type supports any combination of clusters.',
+    })
+    await queryZcl.insertDeviceTypes(db, ctx.packageId, customDeviceTypes)
+  }
+  return ctx
+}
+
 /**
  * Toplevel function that loads the toplevel metafile
  * and orchestrates the promise chain.
@@ -1056,6 +1081,7 @@ async function loadSilabsZcl(db, context, isJson = false) {
     .then((ctx) => zclLoader.recordVersion(ctx))
     .then((ctx) => parseZclFiles(db, ctx))
     .then((ctx) => parseManufacturerData(db, ctx))
+    .then((ctx) => processCustomZclDeviceType(db, ctx))
     .then((ctx) => parseOptions(db, ctx))
     .then((ctx) => parseDefaults(db, ctx))
     .then((ctx) => parseZclSchema(db, ctx))
diff --git a/test/zcl-loader-consecutive.test.js b/test/zcl-loader-consecutive.test.js
index 96eecd61..5bf3f323 100644
--- a/test/zcl-loader-consecutive.test.js
+++ b/test/zcl-loader-consecutive.test.js
@@ -105,7 +105,7 @@ test('test that consecutive loading of metafiles properly avoids duplication', (
     .then((x) => expect(x.length).toEqual(54))
     .then(() => queryZcl.selectAllBitmaps(db, jsonPackageId))
     .then(() => queryZcl.selectAllDeviceTypes(db, jsonPackageId))
-    .then((x) => expect(x.length).toEqual(174))
+    .then((x) => expect(x.length).toEqual(176))
     .then(() => queryZcl.selectAllAtomics(db, jsonPackageId))
     .then((x) => expect(x.length).toEqual(56))
     .then(() => queryZcl.selectAllClusters(db, dotdotPackageId))
diff --git a/test/zcl-loader.test.js b/test/zcl-loader.test.js
index 1cd305ca..256f82dc 100644
--- a/test/zcl-loader.test.js
+++ b/test/zcl-loader.test.js
@@ -70,7 +70,7 @@ test('test Silabs zcl data loading in memory', () => {
     .then(() => queryZcl.selectAllBitmaps(db, packageId))
     .then((x) => expect(x.length).toEqual(121))
     .then(() => queryZcl.selectAllDeviceTypes(db, packageId))
-    .then((x) => expect(x.length).toEqual(174))
+    .then((x) => expect(x.length).toEqual(175))
     .then(() => queryGeneric.selectCountFrom(db, 'COMMAND_ARG'))
     .then((x) => expect(x).toEqual(testUtil.totalCommandArgsCount))
     .then(() => queryGeneric.selectCountFrom(db, 'COMMAND'))
diff --git a/zcl-builtin/silabs/zcl.json b/zcl-builtin/silabs/zcl.json
index 97bb1bbd..74e27f9d 100644
--- a/zcl-builtin/silabs/zcl.json
+++ b/zcl-builtin/silabs/zcl.json
@@ -26,6 +26,7 @@
     "wwah-silabs-devices.xml"
   ],
   "zclSchema": "./schema/zcl.xsd",
+  "supportCustomZclDevice": true,
   "manufacturersXml": "../shared/manufacturers.xml",
   "zclValidation": "./schema/zcl-validation.js",
   "options": {
-- 
GitLab