Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/zspec/zcl/buffaloZcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,25 @@ const EXTENSION_FIELD_SETS_DATA_TYPE: {[key: number]: DataType[]} = {
768: [DataType.UINT16, DataType.UINT16, DataType.UINT16, DataType.UINT8, DataType.UINT8, DataType.UINT8, DataType.UINT16, DataType.UINT16],
};

// UINT8_TMP_FIX: temporary return 0xff instead of Number.NaN
// Will be replaced by https://github.com/Koenkk/zigbee-herdsman/pull/1503
// https://github.com/Koenkk/zigbee-herdsman/issues/1498
// https://github.com/Koenkk/zigbee-herdsman/pull/1510

export class BuffaloZcl extends Buffalo {
private writeZclUInt8(value: number): void {
this.writeUInt8(Number.isNaN(value) ? 0xff : value);
this.writeUInt8(value);
// See UINT8_TMP_FIX
// this.writeUInt8(Number.isNaN(value) ? 0xff : value);
}

private readZclUInt8(): number {
const value = this.readUInt8();

return value === 0xff ? Number.NaN : value;
return value;

// See UINT8_TMP_FIX
// return value === 0xff ? Number.NaN : value;
}

private writeZclUInt16(value: number): void {
Expand Down Expand Up @@ -200,14 +210,20 @@ export class BuffaloZcl extends Buffalo {
if (value) {
this.writeUInt8(value.length);
this.writeBuffer(value, value.length);
/* v8 ignore start */
} else {
// ignore because of UINT8_TMP_FIX
this.writeUInt8(0xff); // non-value
}
/* v8 ignore stop */
}

private readOctetStr(): Buffer {
const length = this.readZclUInt8();
return Number.isNaN(length) ? Buffer.from([]) : this.readBuffer(length);

// See UINT8_TMP_FIX
return length < 0xff ? this.readBuffer(length) : Buffer.from([]); // non-value
// return Number.isNaN(length) ? Buffer.from([]) : this.readBuffer(length);
}

private writeCharStr(value?: string | number[]): void {
Expand All @@ -227,7 +243,9 @@ export class BuffaloZcl extends Buffalo {
private readCharStr(): string {
const length = this.readZclUInt8();

return Number.isNaN(length) ? "" : this.readUtf8String(length);
// See UINT8_TMP_FIX
return length < 0xff ? this.readUtf8String(length) : ""; // non-value
// return Number.isNaN(length) ? "" : this.readUtf8String(length);
}

private writeLongOctetStr(value?: number[]): void {
Expand Down
2 changes: 1 addition & 1 deletion test/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8624,7 +8624,7 @@ describe("Controller", () => {
type: "commandIndividualLedEffect",
data: {
color: 0,
duration: null,
duration: 255,
effect: 1,
led: 5,
level: 100,
Expand Down
8 changes: 4 additions & 4 deletions test/zcl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ describe("Zcl", () => {
"12": 0,
"100": 1,
"101": 0,
"110": Number.NaN,
"111": Number.NaN,
"110": 255,
"111": 255,
"148": 4,
"149": 0.14562499523162842,
"150": 2335.614013671875,
Expand Down Expand Up @@ -738,11 +738,11 @@ describe("Zcl", () => {

const payload = {
srcID: 4650238,
commandFrame: {},
commandFrame: {raw: Buffer.from([])},
commandID: 16,
frameCounter: 1253,
options: 5280,
payloadSize: Number.NaN,
payloadSize: 255,
};

expect(frame.header).toStrictEqual(header);
Expand Down
52 changes: 26 additions & 26 deletions test/zspec/zcl/buffalo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ describe("ZCL Buffalo", () => {
});

it.each([
["boolean", {value: Number.NaN, types: [Zcl.DataType.BOOLEAN]}, {written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"}],
[
"uint8-like",
{value: Number.NaN, types: [Zcl.DataType.DATA8, Zcl.DataType.BITMAP8, Zcl.DataType.UINT8, Zcl.DataType.ENUM8]},
{written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"},
],
// ["boolean", {value: Number.NaN, types: [Zcl.DataType.BOOLEAN]}, {written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"}],
// [
// "uint8-like",
// {value: Number.NaN, types: [Zcl.DataType.DATA8, Zcl.DataType.BITMAP8, Zcl.DataType.UINT8, Zcl.DataType.ENUM8]},
// {written: 0xff, position: 1, write: "writeUInt8", read: "readUInt8"},
// ],
[
"uint16-like",
{
Expand Down Expand Up @@ -218,21 +218,21 @@ describe("ZCL Buffalo", () => {
{value: undefined, types: [Zcl.DataType.DATA64, Zcl.DataType.BITMAP64, Zcl.DataType.UINT64]},
{written: 0xffffffffffffffffn, position: 8, write: "writeUInt64", read: "readUInt64"},
],
[
"octectStr",
{value: undefined, types: [Zcl.DataType.OCTET_STR]},
{written: 0xff, valueRead: Buffer.from([]), position: 1, write: "writeUInt8", read: "readUInt8"},
],
// [
// "octectStr",
// {value: undefined, types: [Zcl.DataType.OCTET_STR]},
// {written: 0xff, valueRead: Buffer.from([]), position: 1, write: "writeUInt8", read: "readUInt8"},
// ],
[
"longOctectStr",
{value: undefined, types: [Zcl.DataType.LONG_OCTET_STR]},
{written: 0xffff, valueRead: Buffer.from([]), position: 2, write: "writeUInt16", read: "readUInt16"},
],
[
"charStr",
{value: undefined, types: [Zcl.DataType.CHAR_STR]},
{written: 0xff, valueRead: "", position: 1, write: "writeUInt8", read: "readUInt8"},
],
// [
// "charStr",
// {value: undefined, types: [Zcl.DataType.CHAR_STR]},
// {written: 0xff, valueRead: "", position: 1, write: "writeUInt8", read: "readUInt8"},
// ],
[
"longCharStr",
{value: undefined, types: [Zcl.DataType.LONG_CHAR_STR]},
Expand Down Expand Up @@ -660,14 +660,14 @@ describe("ZCL Buffalo", () => {
});

it.each([
[
"time of day",
{type: Zcl.DataType.TOD, position: 4, returned: {hours: Number.NaN, minutes: Number.NaN, seconds: Number.NaN, hundredths: Number.NaN}},
],
[
"date",
{type: Zcl.DataType.DATE, position: 4, returned: {year: Number.NaN, month: Number.NaN, dayOfMonth: Number.NaN, dayOfWeek: Number.NaN}},
],
// [
// "time of day",
// {type: Zcl.DataType.TOD, position: 4, returned: {hours: Number.NaN, minutes: Number.NaN, seconds: Number.NaN, hundredths: Number.NaN}},
// ],
// [
// "date",
// {type: Zcl.DataType.DATE, position: 4, returned: {year: Number.NaN, month: Number.NaN, dayOfMonth: Number.NaN, dayOfWeek: Number.NaN}},
// ],
["mi struct", {type: Zcl.BuffaloZclDataType.MI_STRUCT, position: 1, returned: {}}],
])("Reads Non-Value for %s", (_name, payload) => {
const buffalo = new BuffaloZcl(Buffer.alloc(50, 0xff));
Expand Down Expand Up @@ -703,8 +703,8 @@ describe("ZCL Buffalo", () => {
});

it.each([
["time of day", {type: Zcl.DataType.TOD, value: {hours: 1, minutes: 2, seconds: Number.NaN, hundredths: 3}, written: [1, 2, 0xff, 3]}],
["date", {type: Zcl.DataType.DATE, value: {year: 1901, month: 2, dayOfMonth: Number.NaN, dayOfWeek: 3}, written: [1, 2, 0xff, 3]}],
["time of day", {type: Zcl.DataType.TOD, value: {hours: 1, minutes: 2, seconds: 0xff, hundredths: 3}, written: [1, 2, 0xff, 3]}],
["date", {type: Zcl.DataType.DATE, value: {year: 1901, month: 2, dayOfMonth: 0xff, dayOfWeek: 3}, written: [1, 2, 0xff, 3]}],
])("Writes & Reads partial Non-Value for %s", (_name, payload) => {
const buffer = Buffer.alloc(10);
const buffalo = new BuffaloZcl(buffer);
Expand Down