Skip to content

Commit

Permalink
zboss: fix INDICATION payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Sep 19, 2024
1 parent 824a801 commit f5bc52b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
19 changes: 8 additions & 11 deletions src/adapter/zboss/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -158,7 +155,7 @@ export function readZBOSSFrame(buffer: Buffer): ZBOSSFrame {
tsn,
payload: {
category,
zdoClusterId,
zdoClusterId: zdoResponseClusterId,
zdo,
},
};
Expand Down
50 changes: 36 additions & 14 deletions test/adapter/zboss/fixZdoResponse.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit f5bc52b

Please sign in to comment.