From 4dfb66734f836f702670431b8d675fe51df8bf8f Mon Sep 17 00:00:00 2001 From: mrG1K Date: Wed, 13 Jan 2021 11:51:05 +0300 Subject: [PATCH] parsing new messages debug.error for incompletely parsed messages --- src/adapter/zigate/driver/buffaloZiGate.ts | 7 +- src/adapter/zigate/driver/constants.ts | 7 +- src/adapter/zigate/driver/messageType.ts | 150 +++++++++++++-------- src/adapter/zigate/driver/ziGateObject.ts | 10 ++ src/adapter/zigate/driver/zigate.ts | 2 +- 5 files changed, 117 insertions(+), 59 deletions(-) diff --git a/src/adapter/zigate/driver/buffaloZiGate.ts b/src/adapter/zigate/driver/buffaloZiGate.ts index 88c6d18733..ef74a53b61 100644 --- a/src/adapter/zigate/driver/buffaloZiGate.ts +++ b/src/adapter/zigate/driver/buffaloZiGate.ts @@ -19,7 +19,7 @@ class BuffaloZiGate extends Buffalo { this.writeUInt32BE(value); } else if (type === 'IEEADDR') { return this.readIeeeAddr(); - }else if (type === 'ADDRESS_WITH_TYPE_DEPENDENCY') { + } else if (type === 'ADDRESS_WITH_TYPE_DEPENDENCY') { const addressMode = this.buffer.readUInt8(this.position - 1); return addressMode == 3 ? this.writeIeeeAddr(value) : this.writeUInt16BE(value); } else if (type === 'BUFFER' && (Buffer.isBuffer(value) || IsNumberArray(value))) { @@ -82,6 +82,8 @@ class BuffaloZiGate extends Buffalo { const buffer = this.buffer.slice(this.position); this.position += buffer.length; return buffer; + } else if (type === 'MAYBE_UINT8') { + if (this.isMore()) return this.readUInt8(); } else { return super.read(type, options); } @@ -121,6 +123,9 @@ class BuffaloZiGate extends Buffalo { this.position += 4; } + public isMore(): boolean { + return this.position < this.buffer.length; + } } export default BuffaloZiGate; diff --git a/src/adapter/zigate/driver/constants.ts b/src/adapter/zigate/driver/constants.ts index 2bcd589f8d..e10b8579ab 100644 --- a/src/adapter/zigate/driver/constants.ts +++ b/src/adapter/zigate/driver/constants.ts @@ -229,18 +229,21 @@ export enum ZiGateMessageCode { ActiveEndpointResponse = 0x8005, NetworkState = 0x8009, VersionList = 0x8010, - APSDataConfirm = 0x8011, // 0x8012 ?? + APSDataACK = 0x8011, + APSDataConfirm = 0x8012, + APSDataConfirmFailed = 0x8702, NetworkJoined = 0x8024, LeaveIndication = 0x8048, RouterDiscoveryConfirm = 0x8701, - APSDataConfirmFail = 0x8702, PermitJoinStatus = 0x8014, GetTimeServer = 0x8017, ManagementLQIResponse = 0x804E, ManagementLeaveResponse = 0x8047, PDMEvent = 0x8035, + PDMLoaded = 0x0302, RestartNonFactoryNew = 0x8006, RestartFactoryNew = 0x8007, + ExtendedStatusCallBack = 0x9999, } interface ZiGateOpjectDefaultPayload { diff --git a/src/adapter/zigate/driver/messageType.ts b/src/adapter/zigate/driver/messageType.ts index cf1717c698..8ee2150257 100644 --- a/src/adapter/zigate/driver/messageType.ts +++ b/src/adapter/zigate/driver/messageType.ts @@ -15,14 +15,14 @@ export interface ZiGateMessageType { export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { [ZiGateMessageCode.GetTimeServer]: { response: [ - {name: 'timestampUTC', parameterType:'UINT32'}, // from 2000-01-01 00:00:00 + {name: 'timestampUTC', parameterType: 'UINT32'}, // from 2000-01-01 00:00:00 ] }, [ZiGateMessageCode.DeviceAnnounce]: { response: [ - {name: 'shortAddress', parameterType:'UINT16BE'}, - {name: 'ieee', parameterType:'IEEEADDR'}, - {name: 'MACcapability', parameterType:'MACCAPABILITY'}, + {name: 'shortAddress', parameterType: 'UINT16BE'}, + {name: 'ieee', parameterType: 'IEEEADDR'}, + {name: 'MACcapability', parameterType: 'MACCAPABILITY'}, // MAC capability // Bit 0 – Alternate PAN Coordinator // Bit 1 – Device Type @@ -31,12 +31,12 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { // Bit 4,5 – Reserved // Bit 6 – Security capability // Bit 7 – Allocate Address - {name: 'rejoin', parameterType:'UINT8'}, + {name: 'rejoin', parameterType: 'UINT8'}, ] }, [ZiGateMessageCode.Status]: { response: [ - {name: 'status', parameterType:'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // // 0 = Success // 1 = Incorrect parameters // 2 = Unhandled command @@ -48,21 +48,31 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { // Packet Type: The value of the initiating command request. {name: 'sequence', parameterType: 'UINT8'}, // {name: 'packetType', parameterType: 'UINT16BE'}, // - {name: 'requestSent', parameterType: 'UINT8'},// - 1 if a request been sent to + + // from 3.1d + {name: 'requestSent', parameterType: 'MAYBE_UINT8'},// - 1 if a request been sent to // a device(aps ack/nack 8011 should be expected) , 0 otherwise - {name: 'seqApsNum', parameterType: 'UINT8'},// - sqn of the APS layer - used to + {name: 'seqApsNum', parameterType: 'MAYBE_UINT8'},// - sqn of the APS layer - used to // check sqn sent back in aps ack + + // from 3.1e + {name: 'PDUM_u8GetNpduUse', parameterType: 'MAYBE_UINT8'}, + {name: 'u8GetApduUse', parameterType: 'MAYBE_UINT8'}, + + // debug 3.1e++ + {name: 'PDUM_u8GetMaxNpduUse', parameterType: 'MAYBE_UINT8'}, + {name: 'u8GetMaxApduUse', parameterType: 'MAYBE_UINT8'}, ] }, [ZiGateMessageCode.PermitJoinStatus]: { response: [ - {name: 'status', parameterType:'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // ] }, [ZiGateMessageCode.DataIndication]: { response: [ - {name: 'status', parameterType:'UINT8'}, // - {name: 'profileID', parameterType:'UINT16BE'}, // + {name: 'status', parameterType: 'UINT8'}, // + {name: 'profileID', parameterType: 'UINT16BE'}, // {name: 'clusterID', parameterType: 'UINT16BE'}, // {name: 'sourceEndpoint', parameterType: 'UINT8'}, // {name: 'destinationEndpoint', parameterType: 'UINT8'}, // @@ -78,26 +88,56 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { // uint8_t> ] }, - [ZiGateMessageCode.APSDataConfirm]: { + [ZiGateMessageCode.APSDataACK]: { response: [ - {name: 'status', parameterType:'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // // {name: 'sourceEndpoint', parameterType:'UINT8'}, // // {name: 'destinationAddressMode', parameterType:'UINT8'}, // // - {name: 'destinationAddress', parameterType:'UINT16BE'}, - {name: 'destinationEndpoint', parameterType:'UINT8'}, // - {name: 'clusterID', parameterType:'UINT16BE'}, + {name: 'destinationAddress', parameterType: 'UINT16BE'}, + {name: 'destinationEndpoint', parameterType: 'UINT8'}, // + {name: 'clusterID', parameterType: 'UINT16BE'}, // // - {name: 'seqNumber', parameterType:'UINT8'}, // + {name: 'seqNumber', parameterType: 'UINT8'}, // + ] + }, + [ZiGateMessageCode.APSDataConfirm]: { + response: [ + {name: 'status', parameterType: 'UINT8'}, // + {name: 'sourceEndpoint', parameterType: 'UINT8'}, // + {name: 'destinationAddressMode', parameterType: 'UINT8'}, + + {name: 'destinationAddressMode', parameterType: 'UINT8'}, + // + {name: 'destinationAddress', parameterType: 'ADDRESS_WITH_TYPE_DEPENDENCY'}, + // + {name: 'seqNumber', parameterType: 'UINT8'}, // + // from 3.1e + {name: 'PDUM_u8GetNpduUse', parameterType: 'MAYBE_UINT8'}, + {name: 'u8GetApduUse', parameterType: 'MAYBE_UINT8'}, + ] + }, + [ZiGateMessageCode.APSDataConfirmFailed]: { + response: [ + {name: 'status', parameterType: 'UINT8'}, // + {name: 'sourceEndpoint', parameterType: 'UINT8'}, // + {name: 'destinationEndpoint', parameterType: 'UINT8'}, // + {name: 'destinationAddressMode', parameterType: 'UINT8'}, // + {name: 'destinationAddress', parameterType: 'ADDRESS_WITH_TYPE_DEPENDENCY'}, + // + {name: 'seqNumber', parameterType: 'UINT8'}, // + // from 3.1e + {name: 'PDUM_u8GetNpduUse', parameterType: 'MAYBE_UINT8'}, + {name: 'u8GetApduUse', parameterType: 'MAYBE_UINT8'}, ] }, [ZiGateMessageCode.NetworkState]: { response: [ - {name: 'shortAddress', parameterType:'UINT16BE'}, // - {name: 'extendedAddress', parameterType:'IEEEADDR'}, // - {name: 'PANID', parameterType:'UINT16BE'}, // - {name: 'ExtPANID', parameterType:'IEEEADDR'}, // - {name: 'Channel', parameterType:'UINT8'}, // + {name: 'shortAddress', parameterType: 'UINT16BE'}, // + {name: 'extendedAddress', parameterType: 'IEEEADDR'}, // + {name: 'PANID', parameterType: 'UINT16BE'}, // + {name: 'ExtPANID', parameterType: 'IEEEADDR'}, // + {name: 'Channel', parameterType: 'UINT8'}, // ] }, [ZiGateMessageCode.VersionList]: { @@ -108,14 +148,14 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { }, [ZiGateMessageCode.NetworkJoined]: { response: [ - {name: 'status', parameterType:'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // // Status: // 0 = Joined existing network // 1 = Formed new network // 128 – 244 = Failed (ZigBee event codes) - {name: 'shortAddress', parameterType:'UINT16BE'}, // - {name: 'extendedAddress', parameterType:'IEEEADDR'}, // - {name: 'channel', parameterType:'UINT8'}, // + {name: 'shortAddress', parameterType: 'UINT16BE'}, // + {name: 'extendedAddress', parameterType: 'IEEEADDR'}, // + {name: 'channel', parameterType: 'UINT8'}, // ] }, [ZiGateMessageCode.LeaveIndication]: { @@ -132,27 +172,17 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { }, [ZiGateMessageCode.RouterDiscoveryConfirm]: { response: [ - {name: 'status', parameterType:'UINT8'}, // - {name: 'nwkStatus', parameterType:'UINT8'}, // - ] - }, - [ZiGateMessageCode.APSDataConfirmFail]: { - response: [ - {name: 'status', parameterType:'UINT8'}, // - {name: 'sourceEndpoint', parameterType:'UINT8'}, // - {name: 'destinationEndpoint', parameterType:'UINT8'}, // - {name: 'destinationAddressMode', parameterType:'UINT8'}, // - {name: 'destinationAddress', parameterType:'ADDRESS_WITH_TYPE_DEPENDENCY'}, - // - {name: 'seqNumber', parameterType:'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // + {name: 'nwkStatus', parameterType: 'UINT8'}, // + {name: 'dstAddress', parameterType: 'UINT16BE'}, // ] }, [ZiGateMessageCode.ActiveEndpointResponse]: { response: [ - {name: 'sequence', parameterType:'UINT8'}, // - {name: 'status', parameterType:'UINT8'}, // - {name: 'nwkAddr', parameterType:'UINT16BE'}, - {name: 'endpointCount', parameterType:'UINT8'}, + {name: 'sequence', parameterType: 'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // + {name: 'nwkAddr', parameterType: 'UINT16BE'}, + {name: 'endpointCount', parameterType: 'UINT8'}, {name: 'endpoints', parameterType: 'LIST_UINT8'}, ] }, @@ -166,20 +196,20 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { // }, [ZiGateMessageCode.ManagementLQIResponse]: { response: [ - {name: 'sequence', parameterType:'UINT8'}, // - {name: 'status', parameterType:'UINT8'}, // - {name: 'neighbourTableEntries', parameterType:'UINT8'}, // - {name: 'neighbourTableListCount', parameterType:'UINT8'}, // - {name: 'startIndex', parameterType:'UINT8'}, // + {name: 'sequence', parameterType: 'UINT8'}, // + {name: 'status', parameterType: 'UINT8'}, // + {name: 'neighbourTableEntries', parameterType: 'UINT8'}, // + {name: 'neighbourTableListCount', parameterType: 'UINT8'}, // + {name: 'startIndex', parameterType: 'UINT8'}, // // @TODO list TYPE // // Note: If Neighbour Table list count is 0, there are no elements in the list. - {name: 'NWKAddress', parameterType:'UINT16BE'}, // NWK Address : uint16_t - {name: 'Extended PAN ID', parameterType:'UINT64'}, // Extended PAN ID : uint64_t - {name: 'IEEE Address', parameterType:'IEEEADR'}, // IEEE Address : uint64_t - {name: 'Depth', parameterType:'UINT8'}, // Depth : uint_t - {name: 'linkQuality', parameterType:'UINT8'}, // Link Quality : uint8_t - {name: 'bitMap', parameterType:'UINT8'}, // Bit map of attributes Described below: uint8_t + {name: 'NWKAddress', parameterType: 'UINT16BE'}, // NWK Address : uint16_t + {name: 'Extended PAN ID', parameterType: 'UINT64'}, // Extended PAN ID : uint64_t + {name: 'IEEE Address', parameterType: 'IEEEADR'}, // IEEE Address : uint64_t + {name: 'Depth', parameterType: 'UINT8'}, // Depth : uint_t + {name: 'linkQuality', parameterType: 'UINT8'}, // Link Quality : uint8_t + {name: 'bitMap', parameterType: 'UINT8'}, // Bit map of attributes Described below: uint8_t // bit 0-1 Device Type // (0-Coordinator 1-Router 2-End Device) // bit 2-3 Permit Join status @@ -191,7 +221,6 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { {name: 'srcAddress', parameterType: 'UINT16BE'}, // ( only from v3.1a) ] }, - [ZiGateMessageCode.PDMEvent]: { response: [ {name: 'eventStatus', parameterType: 'UINT8'}, // @@ -199,6 +228,11 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { ] }, + [ZiGateMessageCode.PDMLoaded]: { + response: [ + {name: 'length', parameterType: 'UINT8'}, + ] + }, [ZiGateMessageCode.RestartNonFactoryNew]: { // Non “Factory new” Restart response: [ {name: 'status', parameterType: 'UINT8'}, // @@ -215,5 +249,11 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = { // 6 – RUNNING // The node is not yet provisioned. ] + }, + [ZiGateMessageCode.ExtendedStatusCallBack]: { + response: [ + {name: 'status', parameterType: 'UINT8'}, + // https://github.com/fairecasoimeme/ZiGate/blob/aac14153db332eb5b898cba0f57f5999e5cf11eb/Module%20Radio/Firmware/src/sdk/JN-SW-4170/Components/ZPSNWK/Include/zps_nwk_pub.h#L89 + ] } }; diff --git a/src/adapter/zigate/driver/ziGateObject.ts b/src/adapter/zigate/driver/ziGateObject.ts index 22841110ef..ed335c7bfa 100755 --- a/src/adapter/zigate/driver/ziGateObject.ts +++ b/src/adapter/zigate/driver/ziGateObject.ts @@ -118,6 +118,16 @@ class ZiGateObject { debug.error(e.stack); } } + + if (buffalo.isMore()) { + let bufferString = buffalo.getBuffer().toString('hex'); + debug.error( + "Last bytes of data were not parsed \x1b[32m%s\x1b[31m%s", + bufferString.slice(0, (buffalo.getPosition() * 2)).replace(/../g, "$& "), + bufferString.slice(buffalo.getPosition() * 2).replace(/../g, "$& ") + ) + } + return result; } diff --git a/src/adapter/zigate/driver/zigate.ts b/src/adapter/zigate/driver/zigate.ts index 036938ebf6..21cbfdbd0c 100644 --- a/src/adapter/zigate/driver/zigate.ts +++ b/src/adapter/zigate/driver/zigate.ts @@ -87,7 +87,7 @@ export default class ZiGate extends EventEmitter { const statusResponse: ZiGateObject | void = await this.queue.execute(async () => { try { debug.log( - 'Send command \x1b[42m>>>> ' + 'Send command \x1b[32m>>>> ' + ZiGateCommandCode[code] + ' 0x' + zeroPad(code) + ' <<<<\x1b[0m \nPayload: %o',