Skip to content

Commit

Permalink
parsing new messages
Browse files Browse the repository at this point in the history
debug.error for incompletely parsed messages
  • Loading branch information
G1K committed Jan 13, 2021
1 parent 22ad06b commit 4dfb667
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 59 deletions.
7 changes: 6 additions & 1 deletion src/adapter/zigate/driver/buffaloZiGate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -121,6 +123,9 @@ class BuffaloZiGate extends Buffalo {
this.position += 4;
}

public isMore(): boolean {
return this.position < this.buffer.length;
}
}

export default BuffaloZiGate;
7 changes: 5 additions & 2 deletions src/adapter/zigate/driver/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
150 changes: 95 additions & 55 deletions src/adapter/zigate/driver/messageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export interface ZiGateMessageType {
export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
[ZiGateMessageCode.GetTimeServer]: {
response: [
{name: 'timestampUTC', parameterType:'UINT32'}, // <Timestamp UTC: uint32_t> from 2000-01-01 00:00:00
{name: 'timestampUTC', parameterType: 'UINT32'}, // <Timestamp UTC: uint32_t> 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
Expand All @@ -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'}, // <status:uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status:uint8_t>
// 0 = Success
// 1 = Incorrect parameters
// 2 = Unhandled command
Expand All @@ -48,21 +48,31 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
// Packet Type: The value of the initiating command request.
{name: 'sequence', parameterType: 'UINT8'}, // <sequence number: uint8_t>
{name: 'packetType', parameterType: 'UINT16BE'}, // <Packet Type: uint16_t>
{name: 'requestSent', parameterType: 'UINT8'},// <requestSent: uint8_t> - 1 if a request been sent to

// from 3.1d
{name: 'requestSent', parameterType: 'MAYBE_UINT8'},// <requestSent: uint8_t> - 1 if a request been sent to
// a device(aps ack/nack 8011 should be expected) , 0 otherwise
{name: 'seqApsNum', parameterType: 'UINT8'},// <seqApsNum: uint8_t> - sqn of the APS layer - used to
{name: 'seqApsNum', parameterType: 'MAYBE_UINT8'},// <seqApsNum: uint8_t> - 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'}, // <status:uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status:uint8_t>
]
},
[ZiGateMessageCode.DataIndication]: {
response: [
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'profileID', parameterType:'UINT16BE'}, // <Profile ID: uint16_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'profileID', parameterType: 'UINT16BE'}, // <Profile ID: uint16_t>
{name: 'clusterID', parameterType: 'UINT16BE'}, // <cluster ID: uint16_t>
{name: 'sourceEndpoint', parameterType: 'UINT8'}, // <source endpoint: uint8_t>
{name: 'destinationEndpoint', parameterType: 'UINT8'}, // <destination endpoint: uint8_t>
Expand All @@ -78,26 +88,56 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
// uint8_t>
]
},
[ZiGateMessageCode.APSDataConfirm]: {
[ZiGateMessageCode.APSDataACK]: {
response: [
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
// {name: 'sourceEndpoint', parameterType:'UINT8'}, // <source endpoint: uint8_t>
// {name: 'destinationAddressMode', parameterType:'UINT8'},
// // <destination address mode: uint8_t>
{name: 'destinationAddress', parameterType:'UINT16BE'},
{name: 'destinationEndpoint', parameterType:'UINT8'}, // <destination endpoint: uint8_t>
{name: 'clusterID', parameterType:'UINT16BE'},
{name: 'destinationAddress', parameterType: 'UINT16BE'},
{name: 'destinationEndpoint', parameterType: 'UINT8'}, // <destination endpoint: uint8_t>
{name: 'clusterID', parameterType: 'UINT16BE'},
// // <destination address: uint16_t or uint64_t>
{name: 'seqNumber', parameterType:'UINT8'}, // <seq number: uint8_t>
{name: 'seqNumber', parameterType: 'UINT8'}, // <seq number: uint8_t>
]
},
[ZiGateMessageCode.APSDataConfirm]: {
response: [
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'sourceEndpoint', parameterType: 'UINT8'}, // <source endpoint: uint8_t>
{name: 'destinationAddressMode', parameterType: 'UINT8'},

{name: 'destinationAddressMode', parameterType: 'UINT8'},
// <destination address mode: uint8_t>
{name: 'destinationAddress', parameterType: 'ADDRESS_WITH_TYPE_DEPENDENCY'},
// <destination address: uint16_t or uint64_t>
{name: 'seqNumber', parameterType: 'UINT8'}, // <seq number: uint8_t>
// from 3.1e
{name: 'PDUM_u8GetNpduUse', parameterType: 'MAYBE_UINT8'},
{name: 'u8GetApduUse', parameterType: 'MAYBE_UINT8'},
]
},
[ZiGateMessageCode.APSDataConfirmFailed]: {
response: [
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'sourceEndpoint', parameterType: 'UINT8'}, // <src endpoint: uint8_t>
{name: 'destinationEndpoint', parameterType: 'UINT8'}, // <dst endpoint: uint8_t>
{name: 'destinationAddressMode', parameterType: 'UINT8'}, // <dst address mode: uint8_t>
{name: 'destinationAddress', parameterType: 'ADDRESS_WITH_TYPE_DEPENDENCY'},
// <destination address: uint64_t>
{name: 'seqNumber', parameterType: 'UINT8'}, // <seq number: uint8_t>
// from 3.1e
{name: 'PDUM_u8GetNpduUse', parameterType: 'MAYBE_UINT8'},
{name: 'u8GetApduUse', parameterType: 'MAYBE_UINT8'},
]
},
[ZiGateMessageCode.NetworkState]: {
response: [
{name: 'shortAddress', parameterType:'UINT16BE'}, // <Short Address: uint16_t>
{name: 'extendedAddress', parameterType:'IEEEADDR'}, // <Extended Address: uint64_t>
{name: 'PANID', parameterType:'UINT16BE'}, // <PAN ID: uint16_t>
{name: 'ExtPANID', parameterType:'IEEEADDR'}, // <Ext PAN ID: uint64_t>
{name: 'Channel', parameterType:'UINT8'}, // <Channel: uint8_t>
{name: 'shortAddress', parameterType: 'UINT16BE'}, // <Short Address: uint16_t>
{name: 'extendedAddress', parameterType: 'IEEEADDR'}, // <Extended Address: uint64_t>
{name: 'PANID', parameterType: 'UINT16BE'}, // <PAN ID: uint16_t>
{name: 'ExtPANID', parameterType: 'IEEEADDR'}, // <Ext PAN ID: uint64_t>
{name: 'Channel', parameterType: 'UINT8'}, // <Channel: uint8_t>
]
},
[ZiGateMessageCode.VersionList]: {
Expand All @@ -108,14 +148,14 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
},
[ZiGateMessageCode.NetworkJoined]: {
response: [
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
// Status:
// 0 = Joined existing network
// 1 = Formed new network
// 128 – 244 = Failed (ZigBee event codes)
{name: 'shortAddress', parameterType:'UINT16BE'}, // <short address: uint16_t>
{name: 'extendedAddress', parameterType:'IEEEADDR'}, // <extended address:uint64_t>
{name: 'channel', parameterType:'UINT8'}, // <channel: uint8_t>
{name: 'shortAddress', parameterType: 'UINT16BE'}, // <short address: uint16_t>
{name: 'extendedAddress', parameterType: 'IEEEADDR'}, // <extended address:uint64_t>
{name: 'channel', parameterType: 'UINT8'}, // <channel: uint8_t>
]
},
[ZiGateMessageCode.LeaveIndication]: {
Expand All @@ -132,27 +172,17 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
},
[ZiGateMessageCode.RouterDiscoveryConfirm]: {
response: [
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'nwkStatus', parameterType:'UINT8'}, // <nwk status: uint8_t>
]
},
[ZiGateMessageCode.APSDataConfirmFail]: {
response: [
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'sourceEndpoint', parameterType:'UINT8'}, // <src endpoint: uint8_t>
{name: 'destinationEndpoint', parameterType:'UINT8'}, // <dst endpoint: uint8_t>
{name: 'destinationAddressMode', parameterType:'UINT8'}, // <dst address mode: uint8_t>
{name: 'destinationAddress', parameterType:'ADDRESS_WITH_TYPE_DEPENDENCY'},
// <destination address: uint64_t>
{name: 'seqNumber', parameterType:'UINT8'}, // <seq number: uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'nwkStatus', parameterType: 'UINT8'}, // <nwk status: uint8_t>
{name: 'dstAddress', parameterType: 'UINT16BE'}, // <nwk status: uint16_t>
]
},
[ZiGateMessageCode.ActiveEndpointResponse]: {
response: [
{name: 'sequence', parameterType:'UINT8'}, // <sequence: uint8_t>
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'nwkAddr', parameterType:'UINT16BE'},
{name: 'endpointCount', parameterType:'UINT8'},
{name: 'sequence', parameterType: 'UINT8'}, // <sequence: uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'nwkAddr', parameterType: 'UINT16BE'},
{name: 'endpointCount', parameterType: 'UINT8'},
{name: 'endpoints', parameterType: 'LIST_UINT8'},
]
},
Expand All @@ -166,20 +196,20 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
// },
[ZiGateMessageCode.ManagementLQIResponse]: {
response: [
{name: 'sequence', parameterType:'UINT8'}, // <Sequence number: uint8_t>
{name: 'status', parameterType:'UINT8'}, // <status: uint8_t>
{name: 'neighbourTableEntries', parameterType:'UINT8'}, // <Neighbour Table Entries : uint8_t>
{name: 'neighbourTableListCount', parameterType:'UINT8'}, // <Neighbour Table List Count : uint8_t>
{name: 'startIndex', parameterType:'UINT8'}, // <Start Index : uint8_t>
{name: 'sequence', parameterType: 'UINT8'}, // <Sequence number: uint8_t>
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
{name: 'neighbourTableEntries', parameterType: 'UINT8'}, // <Neighbour Table Entries : uint8_t>
{name: 'neighbourTableListCount', parameterType: 'UINT8'}, // <Neighbour Table List Count : uint8_t>
{name: 'startIndex', parameterType: 'UINT8'}, // <Start Index : uint8_t>
// @TODO list TYPE
// <List of Entries elements described below :>
// 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
Expand All @@ -191,14 +221,18 @@ export const ZiGateMessage: { [k: number]: ZiGateMessageType } = {
{name: 'srcAddress', parameterType: 'UINT16BE'}, // <Src Address : uint16_t> ( only from v3.1a)
]
},

[ZiGateMessageCode.PDMEvent]: {
response: [
{name: 'eventStatus', parameterType: 'UINT8'}, // <event status: uint8_t>
{name: 'recordID', parameterType: 'UINT32BE'}, // <record ID: uint32_t>

]
},
[ZiGateMessageCode.PDMLoaded]: {
response: [
{name: 'length', parameterType: 'UINT8'},
]
},
[ZiGateMessageCode.RestartNonFactoryNew]: { // Non “Factory new” Restart
response: [
{name: 'status', parameterType: 'UINT8'}, // <status: uint8_t>
Expand All @@ -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
]
}
};
10 changes: 10 additions & 0 deletions src/adapter/zigate/driver/ziGateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/zigate/driver/zigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 4dfb667

Please sign in to comment.