Skip to content

Commit

Permalink
fix: Zigate: parse all values as big endian (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
devbis authored Aug 30, 2024
1 parent 53de8d4 commit 1a50e2e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 66 deletions.
66 changes: 38 additions & 28 deletions src/adapter/zigate/driver/buffaloZiGate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore file */

import {Buffalo} from '../../../buffalo';
import {EUI64} from '../../../zspec/tstypes';
import {BuffaloZclOptions} from '../../../zspec/zcl/definition/tstype';
import {LOG_LEVEL} from './constants';
import ParameterType from './parameterType';
Expand All @@ -17,13 +18,13 @@ class BuffaloZiGate extends Buffalo {
return this.writeUInt8(value);
}
case ParameterType.UINT16: {
return this.writeUInt16(value);
return this.writeUInt16BE(value);
}
case ParameterType.UINT32: {
return this.writeUInt32(value);
return this.writeUInt32BE(value);
}
case ParameterType.IEEEADDR: {
return this.writeIeeeAddr(value);
return this.writeIeeeAddrBE(value);
}
case ParameterType.BUFFER: {
return this.writeBuffer(value, value.length);
Expand All @@ -50,24 +51,18 @@ class BuffaloZiGate extends Buffalo {
return this.writeListUInt8(value);
}
case ParameterType.LIST_UINT16: {
return this.writeListUInt16(value);
return this.writeListUInt16BE(value);
}
case ParameterType.INT8: {
return this.writeInt8(value);
}
case ParameterType.ADDRESS_WITH_TYPE_DEPENDENCY: {
const addressMode = this.buffer.readUInt8(this.position - 1);
return addressMode == 3 ? this.writeIeeeAddr(value) : this.writeUInt16BE(value);
return addressMode == 3 ? this.writeIeeeAddrBE(value) : this.writeUInt16BE(value);
}
case ParameterType.RAW: {
return this.writeRaw(value);
}
case ParameterType.UINT16BE: {
return this.writeUInt16BE(value);
}
case ParameterType.UINT32BE: {
return this.writeUInt32BE(value);
}
}

throw new Error(`Write for '${type}' not available`);
Expand All @@ -79,13 +74,13 @@ class BuffaloZiGate extends Buffalo {
return this.readUInt8();
}
case ParameterType.UINT16: {
return this.readUInt16();
return this.readUInt16BE();
}
case ParameterType.UINT32: {
return this.readUInt32();
return this.readUInt32BE();
}
case ParameterType.IEEEADDR: {
return this.readIeeeAddr();
return this.readIeeeAddrBE();
}
case ParameterType.BUFFER: {
// if length option not specified, read the whole buffer
Expand Down Expand Up @@ -113,7 +108,7 @@ class BuffaloZiGate extends Buffalo {
return this.readListUInt8(options.length ?? 0); // XXX: should always be valid?
}
case ParameterType.LIST_UINT16: {
return this.readListUInt16(options.length ?? 0); // XXX: should always be valid?
return this.readListUInt16BE(options.length ?? 0); // XXX: should always be valid?
}
case ParameterType.INT8: {
return this.readInt8();
Expand Down Expand Up @@ -141,13 +136,7 @@ class BuffaloZiGate extends Buffalo {
}
case ParameterType.ADDRESS_WITH_TYPE_DEPENDENCY: {
const addressMode = this.buffer.readUInt8(this.position - 1);
return addressMode == 3 ? this.readIeeeAddr() : this.readUInt16BE();
}
case ParameterType.UINT16BE: {
return this.readUInt16BE();
}
case ParameterType.UINT32BE: {
return this.readUInt32BE();
return addressMode == 3 ? this.readIeeeAddrBE() : this.readUInt16BE();
}
case ParameterType.BUFFER_RAW: {
const buffer = this.buffer.subarray(this.position);
Expand Down Expand Up @@ -180,22 +169,43 @@ class BuffaloZiGate extends Buffalo {
this.position += 2;
return value;
}
public writeUInt16BE(value: number): void {
this.buffer.writeUInt16BE(value, this.position);
this.position += 2;
}

public readUInt32BE(): number {
const value = this.buffer.readUInt32BE(this.position);
this.position += 4;
return value;
}

public writeUInt16BE(value: number): void {
this.buffer.writeUInt16BE(value, this.position);
this.position += 2;
}

public writeUInt32BE(value: number): void {
this.buffer.writeUInt32BE(value, this.position);
this.position += 4;
}

public readListUInt16BE(length: number): number[] {
const value: number[] = [];
for (let i = 0; i < length; i++) {
value.push(this.readUInt16BE());
}

return value;
}
public writeListUInt16BE(values: number[]): void {
for (const value of values) {
this.writeUInt16BE(value);
}
}

public readIeeeAddrBE(): EUI64 {
const octets = Array.from(this.readBuffer(8));
return `0x${octets.map((octet) => octet.toString(16).padStart(2, '0')).join('')}`;
}
public writeIeeeAddrBE(value: string /*TODO: EUI64*/): void {
this.writeUInt32BE(parseInt(value.slice(2, 10), 16));
this.writeUInt32BE(parseInt(value.slice(10), 16));
}
}

export default BuffaloZiGate;
30 changes: 15 additions & 15 deletions src/adapter/zigate/driver/commandType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
[ZiGateCommandCode.ManagementLQI]: {
// 0x004E
request: [
{name: 'targetAddress', parameterType: ParameterType.UINT16BE}, //<Target Address : uint16_t> Status
{name: 'targetAddress', parameterType: ParameterType.UINT16}, //<Target Address : uint16_t> Status
{name: 'startIndex', parameterType: ParameterType.UINT8}, //<Start Index : uint8_t>
],
response: [
Expand Down Expand Up @@ -162,13 +162,13 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.SetChannelMask]: {
request: [
{name: 'channelMask', parameterType: ParameterType.UINT32BE}, //<channel mask:uint32_t>
{name: 'channelMask', parameterType: ParameterType.UINT32}, //<channel mask:uint32_t>
],
},

[ZiGateCommandCode.ManagementLeaveRequest]: {
request: [
{name: 'shortAddress', parameterType: ParameterType.UINT16BE},
{name: 'shortAddress', parameterType: ParameterType.UINT16},
{name: 'extendedAddress', parameterType: ParameterType.IEEEADDR}, // <extended address: uint64_t>
{name: 'rejoin', parameterType: ParameterType.UINT8},
{name: 'removeChildren', parameterType: ParameterType.UINT8}, // <Remove Children: uint8_t>
Expand Down Expand Up @@ -223,7 +223,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.PermitJoin]: {
request: [
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, //<target short address: uint16_t> -
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, //<target short address: uint16_t> -
// broadcast 0xfffc
{name: 'interval', parameterType: ParameterType.UINT8}, //<interval: uint8_t>
// 0 = Disable Joining
Expand All @@ -236,7 +236,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.PermitJoinStatus]: {
request: [
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, //<target short address: uint16_t> -
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, //<target short address: uint16_t> -
// broadcast 0xfffc
{name: 'interval', parameterType: ParameterType.UINT8}, //<interval: uint8_t>
// 0 = Disable Joining
Expand All @@ -251,11 +251,11 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
[ZiGateCommandCode.RawAPSDataRequest]: {
request: [
{name: 'addressMode', parameterType: ParameterType.UINT8}, // <address mode: uint8_t>
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, // <target short address: uint16_t>
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, // <target short address: uint16_t>
{name: 'sourceEndpoint', parameterType: ParameterType.UINT8}, // <source endpoint: uint8_t>
{name: 'destinationEndpoint', parameterType: ParameterType.UINT8}, // <destination endpoint: uint8_t>
{name: 'clusterID', parameterType: ParameterType.UINT16BE}, // <cluster ID: uint16_t>
{name: 'profileID', parameterType: ParameterType.UINT16BE}, // <profile ID: uint16_t>
{name: 'clusterID', parameterType: ParameterType.UINT16}, // <cluster ID: uint16_t>
{name: 'profileID', parameterType: ParameterType.UINT16}, // <profile ID: uint16_t>
{name: 'securityMode', parameterType: ParameterType.UINT8}, // <security mode: uint8_t>
{name: 'radius', parameterType: ParameterType.UINT8}, // <radius: uint8_t>
{name: 'dataLength', parameterType: ParameterType.UINT8}, // <data length: uint8_t>
Expand All @@ -264,7 +264,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.NodeDescriptor]: {
request: [
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, // <target short address: uint16_t>
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, // <target short address: uint16_t>
],
response: [
[
Expand All @@ -288,7 +288,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.ActiveEndpoint]: {
request: [
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, // <target short address: uint16_t>
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, // <target short address: uint16_t>
],
response: [
[
Expand All @@ -312,7 +312,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
},
[ZiGateCommandCode.SimpleDescriptor]: {
request: [
{name: 'targetShortAddress', parameterType: ParameterType.UINT16BE}, // <target short address: uint16_t>
{name: 'targetShortAddress', parameterType: ParameterType.UINT16}, // <target short address: uint16_t>
{name: 'endpoint', parameterType: ParameterType.UINT8}, // <endpoint: uint8_t>
],
response: [
Expand All @@ -335,7 +335,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
request: [
{name: 'targetExtendedAddress', parameterType: ParameterType.IEEEADDR}, // <target extended address: uint64_t>
{name: 'targetEndpoint', parameterType: ParameterType.UINT8}, // <target endpoint: uint8_t>
{name: 'clusterID', parameterType: ParameterType.UINT16BE}, // <cluster ID: uint16_t>
{name: 'clusterID', parameterType: ParameterType.UINT16}, // <cluster ID: uint16_t>
{name: 'destinationAddressMode', parameterType: ParameterType.UINT8}, // <destination address mode: uint8_t>
{
name: 'destinationAddress',
Expand Down Expand Up @@ -373,7 +373,7 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
request: [
{name: 'targetExtendedAddress', parameterType: ParameterType.IEEEADDR}, // <target extended address: uint64_t>
{name: 'targetEndpoint', parameterType: ParameterType.UINT8}, // <target endpoint: uint8_t>
{name: 'clusterID', parameterType: ParameterType.UINT16BE}, // <cluster ID: uint16_t>
{name: 'clusterID', parameterType: ParameterType.UINT16}, // <cluster ID: uint16_t>
{name: 'destinationAddressMode', parameterType: ParameterType.UINT8}, // <destination address mode: uint8_t>
{
name: 'destinationAddress',
Expand Down Expand Up @@ -410,10 +410,10 @@ export const ZiGateCommand: {[key: string]: ZiGateCommandType} = {
[ZiGateCommandCode.AddGroup]: {
request: [
{name: 'addressMode', parameterType: ParameterType.UINT8}, //<device type: uint8_t>
{name: 'shortAddress', parameterType: ParameterType.UINT16BE},
{name: 'shortAddress', parameterType: ParameterType.UINT16},
{name: 'sourceEndpoint', parameterType: ParameterType.UINT8},
{name: 'destinationEndpoint', parameterType: ParameterType.UINT8},
{name: 'groupAddress', parameterType: ParameterType.UINT16BE},
{name: 'groupAddress', parameterType: ParameterType.UINT16},
],
},
};
Loading

0 comments on commit 1a50e2e

Please sign in to comment.