Skip to content

Commit

Permalink
Minor cleanup. (#959)
Browse files Browse the repository at this point in the history
* Cleanup some helper functions.
* Some further cleanup.
* Remove reference to xvfb.
  • Loading branch information
tecimovic authored Feb 28, 2023
1 parent 5f38bdf commit 4bcb914
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 120 deletions.
122 changes: 28 additions & 94 deletions src-electron/generator/helper-zcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ function command_arguments_total_length(commandId) {
async function zcl_command_arguments(options) {
let commandArgs = this.commandArgs

let packageIds = await templateUtil.ensureZclPackageIds(this)
// When we are coming from commant_tree, then
// the commandArgs are already present and there is no need
// to do additional queries.
Expand All @@ -1066,7 +1067,6 @@ async function zcl_command_arguments(options) {
this.id
)
} else {
let packageIds = await templateUtil.ensureZclPackageIds(this)
commandArgs = await queryCommand.selectAllCommandArguments(
this.global.db,
packageIds
Expand All @@ -1075,9 +1075,10 @@ async function zcl_command_arguments(options) {
}
// Adding command argument type size and sign
for (let i = 0; i < commandArgs.length; i++) {
let sizeAndSign = await get_sign_and_size_of_zcl_type(
let sizeAndSign = await types.getSignAndSizeOfZclType(
this.global.db,
commandArgs[i].type,
this,
packageIds,
options
)
commandArgs[i].typeSize = sizeAndSign.dataTypesize
Expand Down Expand Up @@ -2489,90 +2490,6 @@ async function format_zcl_string_as_characters_for_generated_defaults(
: formatted_string.trim().slice(0, -1)
}

/**
* Given a zcl device type returns its sign, size and zcl data type info stored
* in the database table.
* Note: Enums and Bitmaps are considered to be unsigned.
* @param {*} type
* @param {*} context
* @param {*} options
* @returns returns sign, size and info of zcl device type
* Available Options:
* - size: Determine whether to calculate the size of zcl device type in bits
* or bytes
* for eg: get_sign_and_size_of_zcl_type('int8u' this size='bits') will return
* the size in bits which will be 8. If not mentioned then it will return the size
* in bytes i.e. 1 in this case.
*/
async function get_sign_and_size_of_zcl_type(type, context, options) {
const packageIds = await templateUtil.ensureZclPackageIds(context)
let isTypeSigned = false
let dataTypesize = 0
let sizeMultiple = 1
if (options && options.size == 'bits') {
sizeMultiple = 8
}

// Extracting the type in the data type table
let dataType = await queryZcl.selectDataTypeByName(
context.global.db,
type,
packageIds
)

// Checking if the type is signed or unsigned
if (
dataType &&
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.number
) {
let number = await queryZcl.selectNumberByName(
context.global.db,
packageIds,
dataType.name
)
if (number.isSigned) {
isTypeSigned = true
}
}

// Extracting the size of the data type
if (dataType) {
if (dataType.discriminatorName.toLowerCase() == dbEnum.zclType.bitmap) {
let bitmap = await queryZcl.selectBitmapByName(
context.global.db,
packageIds,
dataType.name
)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(bitmap.size)))
} else if (
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.enum
) {
let en = await queryZcl.selectEnumByName(
context.global.db,
dataType.name,
packageIds
)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(en.size)))
} else if (
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.number
) {
let number = await queryZcl.selectNumberByName(
context.global.db,
packageIds,
dataType.name
)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(number.size)))
}
} else {
env.logError(`Data Type ${type} is not defined in the data type table.`)
}
return {
isTypeSigned: isTypeSigned,
dataTypesize: dataTypesize * sizeMultiple,
dataType: dataType,
}
}

/**
* Given a zcl data type return the min allowed value for that zcl data type
* based on the language specified in the options
Expand All @@ -2586,9 +2503,15 @@ async function get_sign_and_size_of_zcl_type(type, context, options) {
* Note: If language is not specified then helper throws an error.
*/
async function as_type_min_value(type, options) {
let signAndSize = await get_sign_and_size_of_zcl_type(type, this, {
size: 'bits',
})
let packageIds = await templateUtil.ensureZclPackageIds(this)
let signAndSize = await types.getSignAndSizeOfZclType(
this.global.db,
type,
packageIds,
{
size: 'bits',
}
)
let isTypeSigned = signAndSize.isTypeSigned
let dataTypesize = signAndSize.dataTypesize
let dataType = signAndSize.dataType
Expand Down Expand Up @@ -2629,9 +2552,15 @@ due to no language option specified in the template'
* bits.
*/
async function as_type_max_value(type, options) {
let signAndSize = await get_sign_and_size_of_zcl_type(type, this, {
size: 'bits',
})
let packageIds = await templateUtil.ensureZclPackageIds(this)
let signAndSize = await types.getSignAndSizeOfZclType(
this.global.db,
type,
packageIds,
{
size: 'bits',
}
)
let isTypeSigned = signAndSize.isTypeSigned
let dataTypesize = signAndSize.dataTypesize
let dataType = signAndSize.dataType
Expand Down Expand Up @@ -2690,7 +2619,12 @@ async function structs_with_clusters(options) {
* @returns size of zcl type
*/
async function as_zcl_type_size(type, options) {
let signAndSize = await get_sign_and_size_of_zcl_type(type, this)
let packageIds = await templateUtil.ensureZclPackageIds(this)
let signAndSize = await types.getSignAndSizeOfZclType(
this.global.db,
type,
packageIds
)
let dataTypesize = signAndSize.dataTypesize
if (dataTypesize != 0) {
return dataTypesize
Expand Down
82 changes: 82 additions & 0 deletions src-electron/util/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,87 @@ function isTwoBytePrefixedString(type) {
return type == 'long_char_string' || type == 'long_octet_string'
}

/**
* Given a zcl device type returns its sign, size and zcl data type info stored
* in the database table.
* Note: Enums and Bitmaps are considered to be unsigned.
* @param {*} type
* @param {*} context
* @param {*} options
* @returns returns sign, size and info of zcl device type
* Available Options:
* - size: Determine whether to calculate the size of zcl device type in bits
* or bytes
* for eg: getSignAndSizeOfZclType('int8u' this size='bits') will return
* the size in bits which will be 8. If not mentioned then it will return the size
* in bytes i.e. 1 in this case.
*/
async function getSignAndSizeOfZclType(db, type, packageIds, options) {
let isTypeSigned = false
let dataTypesize = 0
let sizeMultiple = 1
let isKnown = true

if (options && options.size == 'bits') {
sizeMultiple = 8
}

// Extracting the type in the data type table
let dataType = await queryZcl.selectDataTypeByName(db, type, packageIds)

// Checking if the type is signed or unsigned
if (
dataType &&
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.number
) {
let number = await queryZcl.selectNumberByName(
db,
packageIds,
dataType.name
)
if (number.isSigned) {
isTypeSigned = true
}
}

// Extracting the size of the data type
if (dataType) {
if (dataType.discriminatorName.toLowerCase() == dbEnum.zclType.bitmap) {
let bitmap = await queryZcl.selectBitmapByName(
db,
packageIds,
dataType.name
)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(bitmap.size)))
} else if (
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.enum
) {
let en = await queryZcl.selectEnumByName(db, dataType.name, packageIds)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(en.size)))
} else if (
dataType.discriminatorName.toLowerCase() == dbEnum.zclType.number
) {
let number = await queryZcl.selectNumberByName(
db,
packageIds,
dataType.name
)
dataTypesize = Math.pow(2, Math.ceil(Math.log2(number.size)))
}
} else {
isKnown = false
}
let ret = {
isTypeSigned: isTypeSigned,
dataTypesize: dataTypesize * sizeMultiple,
dataType: dataType,
}
// If this type was not defined, we tag it as `isNotDefined=true`.
// One day we will do something better...
if (!isKnown) ret.isNotDefined = true
return ret
}

exports.typeSize = typeSize
exports.typeSizeAttribute = typeSizeAttribute
exports.longTypeDefaultValue = longTypeDefaultValue
Expand All @@ -324,3 +405,4 @@ exports.isFloat = isFloat
exports.isSignedInteger = isSignedInteger
exports.convertIntToBigEndian = convertIntToBigEndian
exports.convertFloatToBigEndian = convertFloatToBigEndian
exports.getSignAndSizeOfZclType = getSignAndSizeOfZclType
2 changes: 1 addition & 1 deletion src-script/zap-uitest.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function printUsage() {
This program executes the Cypress unit tests.
Valid modes:
run - [default] Executes all the Cypress tests in the headless mode. You might need to run via xvfb-run in a headless environment.
run - [default] Executes all the Cypress tests in the headless mode.
open - Executes Cypress UI for manual run of the tests.`)
process.exit(0)
}
Expand Down
2 changes: 1 addition & 1 deletion test/endpoint-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const bin = require('../src-electron/util/bin.ts')

let db
const templateCount = testUtil.testTemplate.zigbeeCount
const testFile = path.join(__dirname, 'resource/three-endpoint-device.zap')
const testFile = testUtil.zigbeeTestFile.threeEp
let sessionId
let templateContext
let zclContext
Expand Down
3 changes: 2 additions & 1 deletion test/gen-dotdot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const testUtil = require('./test-util.js')
const queryPackage = require('../src-electron/db/query-package.js')

let db
const testFile = path.join(__dirname, 'resource/three-endpoint-device.zap')
const testFile = testUtil.zigbeeTestFile.threeEp
let sessionId
let templateContext
let zclContext
Expand Down Expand Up @@ -99,6 +99,7 @@ test(
expect(genResult).not.toBeNull()
expect(genResult.partial).toBeFalsy()
expect(genResult.content).not.toBeNull()
expect(genResult.hasErrors).toBeFalsy()

let epc = genResult.content['test1.h']
expect(epc).not.toBeNull()
Expand Down
20 changes: 10 additions & 10 deletions test/gen-matter-1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
*/

const path = require('path')
const genEngine = require('../src-electron/generator/generation-engine.js')
const env = require('../src-electron/util/env.ts')
const dbApi = require('../src-electron/db/db-api.js')
const queryAttribute = require('../src-electron/db/query-attribute.js')
const querySession = require('../src-electron/db/query-session.js')
const queryZcl = require('../src-electron/db/query-zcl.js')
const zclLoader = require('../src-electron/zcl/zcl-loader.js')
const importJs = require('../src-electron/importexport/import.js')
const testUtil = require('./test-util.js')
const testQuery = require('./test-query.js')
const genEngine = require('../src-electron/generator/generation-engine')
const env = require('../src-electron/util/env')
const dbApi = require('../src-electron/db/db-api')
const queryAttribute = require('../src-electron/db/query-attribute')
const querySession = require('../src-electron/db/query-session')
const queryZcl = require('../src-electron/db/query-zcl')
const zclLoader = require('../src-electron/zcl/zcl-loader')
const importJs = require('../src-electron/importexport/import')
const testUtil = require('./test-util')
const testQuery = require('./test-query')

let db
let templateContext
Expand Down
16 changes: 8 additions & 8 deletions test/gen-test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
*/

const path = require('path')
const genEngine = require('../src-electron/generator/generation-engine.js')
const env = require('../src-electron/util/env.ts')
const dbApi = require('../src-electron/db/db-api.js')
const zclLoader = require('../src-electron/zcl/zcl-loader.js')
const importJs = require('../src-electron/importexport/import.js')
const testUtil = require('./test-util.js')
const queryPackage = require('../src-electron/db/query-package.js')
const genEngine = require('../src-electron/generator/generation-engine')
const env = require('../src-electron/util/env')
const dbApi = require('../src-electron/db/db-api')
const zclLoader = require('../src-electron/zcl/zcl-loader')
const importJs = require('../src-electron/importexport/import')
const testUtil = require('./test-util')
const queryPackage = require('../src-electron/db/query-package')

let db
const testFile = path.join(__dirname, 'resource/three-endpoint-device.zap')
const testFile = testUtil.zigbeeTestFile.threeEp
let sessionId
let templateContext
let zclContext
Expand Down
1 change: 1 addition & 0 deletions test/gen-zigbee-6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ test(
disableDeprecationWarnings: true,
}
)
expect(genResult.hasErrors).toBeFalsy()

let pv3 = genResult.content['zap-command-parser-ver-3.c']
// Test Cluster command parsers that should be defined
Expand Down
2 changes: 1 addition & 1 deletion test/script-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const queryEndpoint = require('../src-electron/db/query-endpoint')
const dbEnum = require('../src-shared/db-enum')
const utilJs = require('../src-electron/util/util')

let testFile = path.join(__dirname, 'resource/three-endpoint-device.zap')
let testFile = testUtil.zigbeeTestFile.threeEp
let testScript2 = path.join(__dirname, 'resource/test-script-2.js')
let testScript3 = path.join(__dirname, 'resource/test-script-3.js')
let testScript4 = path.join(__dirname, 'resource/test-script-4.js')
Expand Down
5 changes: 1 addition & 4 deletions test/server-with-zcl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ describe('Miscelaneous REST API tests', () => {
() =>
axiosInstance
.post(`${restApi.ide.open}?sessionId=${sessionUuid}`, {
zapFilePath: path.join(
__dirname,
'resource/three-endpoint-device.zap'
),
zapFilePath: testUtil.zigbeeTestFile.threeEp,
})
.then((response) => {
expect(response.status).toBe(StatusCodes.OK)
Expand Down

0 comments on commit 4bcb914

Please sign in to comment.