From f5bc52b492c1c3516722ccb6c3de6d91beb728db Mon Sep 17 00:00:00 2001 From: Nerivec <62446222+Nerivec@users.noreply.github.com> Date: Thu, 19 Sep 2024 21:52:39 +0200 Subject: [PATCH] zboss: fix INDICATION payload --- src/adapter/zboss/frame.ts | 19 ++++----- test/adapter/zboss/fixZdoResponse.test.ts | 50 ++++++++++++++++------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/adapter/zboss/frame.ts b/src/adapter/zboss/frame.ts index 80593a04d5..b44e5b7b76 100644 --- a/src/adapter/zboss/frame.ts +++ b/src/adapter/zboss/frame.ts @@ -135,20 +135,17 @@ function fixNonStandardZdoRspPayload(clusterId: ZdoClusterId, buffer: Buffer): B export function readZBOSSFrame(buffer: Buffer): ZBOSSFrame { const buf = new ZBOSSBuffaloZcl(buffer); const version = buf.readUInt8(); - const type = buf.readUInt8(); + const type: FrameType = buf.readUInt8(); const commandId: CommandId = buf.readUInt16(); - let tsn = 0; + const tsn = type === FrameType.REQUEST || type === FrameType.RESPONSE ? buf.readUInt8() : 0; - if ([FrameType.REQUEST, FrameType.RESPONSE].includes(type)) { - tsn = buf.readUInt8(); - } - - const zdoResponseClusterId = ZBOSS_COMMAND_ID_TO_ZDO_RSP_CLUSTER_ID[commandId]; + const zdoResponseClusterId = + type === FrameType.RESPONSE || type === FrameType.INDICATION ? ZBOSS_COMMAND_ID_TO_ZDO_RSP_CLUSTER_ID[commandId] : undefined; if (zdoResponseClusterId !== undefined) { - const category = buf.readUInt8(); // XXX: should always be ZDO here? - const zdoClusterId = zdoResponseClusterId; - const zdoPayload = fixNonStandardZdoRspPayload(zdoClusterId, buffer.subarray(6)); + // FrameType.INDICATION has no tsn (above), no category + const category = type === FrameType.RESPONSE ? buf.readUInt8() : undefined; + const zdoPayload = fixNonStandardZdoRspPayload(zdoResponseClusterId, buffer.subarray(type === FrameType.RESPONSE ? 6 : 4)); const zdo = BuffaloZdo.readResponse(false, zdoResponseClusterId, zdoPayload); return { @@ -158,7 +155,7 @@ export function readZBOSSFrame(buffer: Buffer): ZBOSSFrame { tsn, payload: { category, - zdoClusterId, + zdoClusterId: zdoResponseClusterId, zdo, }, }; diff --git a/test/adapter/zboss/fixZdoResponse.test.ts b/test/adapter/zboss/fixZdoResponse.test.ts index 2c30cc3e91..0235273d04 100644 --- a/test/adapter/zboss/fixZdoResponse.test.ts +++ b/test/adapter/zboss/fixZdoResponse.test.ts @@ -1,13 +1,14 @@ -import {readZBOSSFrame, ZBOSSFrame} from '../../../src/adapter/zboss/frame'; +import {CommandId} from '../../../src/adapter/zboss/enums'; +import {FrameType, readZBOSSFrame, ZBOSSFrame} from '../../../src/adapter/zboss/frame'; import * as Zdo from '../../../src/zspec/zdo'; import * as ZdoTypes from '../../../src/zspec/zdo/definition/tstypes'; describe('ZBOSS fix non-standard ZDO response payloads', () => { - it('No fix needed', async () => { + it('No fix needed FrameType.RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('0001010211000088776655443322113412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 513, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_NWK_ADDR_REQ, tsn: 17, payload: { category: 0, @@ -25,11 +26,32 @@ describe('ZBOSS fix non-standard ZDO response payloads', () => { } as ZBOSSFrame); }); + it('No fix needed FrameType.INDICATION', async () => { + expect(readZBOSSFrame(Buffer.from('00020c020cda603602602bd5b3708e', 'hex'))).toStrictEqual({ + version: 0, + type: FrameType.INDICATION, + commandId: CommandId.ZDO_DEV_ANNCE_IND, + tsn: 0, + payload: { + category: undefined, + zdoClusterId: Zdo.ClusterId.END_DEVICE_ANNOUNCE, + zdo: [ + Zdo.Status.SUCCESS, + { + nwkAddress: 0xda0c, + eui64: '0x70b3d52b60023660', + capabilities: Zdo.Utils.getMacCapFlags(0x8e), + } as ZdoTypes.EndDeviceAnnounce, + ], + }, + } as ZBOSSFrame); + }); + it('NODE_DESCRIPTOR_RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('000104021100000000000000000000432c0000003412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 516, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_NODE_DESC_REQ, tsn: 17, payload: { category: 0, @@ -68,8 +90,8 @@ describe('ZBOSS fix non-standard ZDO response payloads', () => { it('POWER_DESCRIPTOR_RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('0001030211000001023412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 515, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_POWER_DESC_REQ, tsn: 17, payload: { category: 0, @@ -91,8 +113,8 @@ describe('ZBOSS fix non-standard ZDO response payloads', () => { it('MATCH_DESCRIPTORS_RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('0001070211000002f2013412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 519, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_MATCH_DESC_REQ, tsn: 17, payload: { category: 0, @@ -111,8 +133,8 @@ describe('ZBOSS fix non-standard ZDO response payloads', () => { it('ACTIVE_ENDPOINTS_RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('0001060211000002f2013412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 518, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_ACTIVE_EP_REQ, tsn: 17, payload: { category: 0, @@ -131,8 +153,8 @@ describe('ZBOSS fix non-standard ZDO response payloads', () => { it('SIMPLE_DESCRIPTOR_RESPONSE', async () => { expect(readZBOSSFrame(Buffer.from('0001050211000001040100000301022c2ffefebcbc3412', 'hex'))).toStrictEqual({ version: 0, - type: 1, - commandId: 517, + type: FrameType.RESPONSE, + commandId: CommandId.ZDO_SIMPLE_DESC_REQ, tsn: 17, payload: { category: 0,