-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathzclFrames.js
More file actions
288 lines (242 loc) · 9.89 KB
/
Copy pathzclFrames.js
File metadata and controls
288 lines (242 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'use strict';
const { ZCLDataType, ZCLDataTypes, ZCLStruct } = require('./zclTypes');
const { ZCLError } = require('./util');
const ZCLFrameControlBitmap = ZCLDataTypes.map8('clusterSpecific', null, 'manufacturerSpecific', 'directionToClient', 'disableDefaultResponse');
const ZCLStandardHeader = ZCLStruct('ZCLStandardHeader', {
frameControl: ZCLFrameControlBitmap,
trxSequenceNumber: ZCLDataTypes.data8,
cmdId: ZCLDataTypes.data8,
data: ZCLDataTypes.buffer,
});
const ZCLMfgSpecificHeader = ZCLStruct('ZCLMfgSpecificHeader', {
frameControl: ZCLFrameControlBitmap,
manufacturerId: ZCLDataTypes.uint16,
trxSequenceNumber: ZCLDataTypes.data8,
cmdId: ZCLDataTypes.data8,
data: ZCLDataTypes.buffer,
});
const ZCLConfigureReportingRecordStruct = ZCLStruct('ConfigureReportingRecord', {
direction: ZCLDataTypes.enum8({
reported: 0,
received: 1,
}),
attributeId: ZCLDataTypes.uint16,
attributeDataType: ZCLDataTypes.uint8,
minInterval: ZCLDataTypes.uint16,
maxInterval: ZCLDataTypes.uint16,
minChange: ZCLDataTypes.buffer,
timeoutPeriod: ZCLDataTypes.uint16,
});
const ZCLAttributeDataRecord = (withStatus, attributes) => new ZCLDataType(NaN, 'attributeRecord', -3, ((buf, v, i) => {
const startByte = i;
i += ZCLDataTypes.uint16.toBuffer(buf, v.id, i);
if (withStatus) {
i += ZCLDataTypes.enum8Status.toBuffer(buf, v.status, i);
}
if (!withStatus || v.status === 'SUCCESS') {
i += ZCLDataTypes.uint8.toBuffer(buf, attributes[v.id].type.id, i);
i += attributes[v.id].type.toBuffer(buf, v.value, i);
}
return i - startByte;
}), function fromBuf(buf, i, returnLength) {
i = i || 0;
const startByte = i;
const res = {};
if (buf.length >= i + Math.abs(this.length)) {
res.id = ZCLDataTypes.uint16.fromBuffer(buf, i);
i += ZCLDataTypes.uint16.length;
if (withStatus) {
res.status = ZCLDataTypes.enum8Status.fromBuffer(buf, i);
i += ZCLDataTypes.enum8Status.length;
}
if (!withStatus || res.status === 'SUCCESS') {
const dataTypeId = ZCLDataTypes.uint8.fromBuffer(buf, i);
i += ZCLDataTypes.uint8.length;
// don't use the attribute's type if the type in the data is 0 (noData).
// This breaks when the attribute ID is 0
const DataType = attributes[res.id] && dataTypeId !== 0
? attributes[res.id].type
: Object.values(ZCLDataTypes).find(type => type && type.id === dataTypeId);
if (!DataType) throw new TypeError(`Invalid Type for Attribute: ${res.id}`);
if (attributes[res.id]) {
res.name = attributes[res.id].name || `unknown_attr_${res.id}`;
}
// Validate that enough bytes remain for the value field before parsing.
// Without this, fixed-width primitives in @athombv/data-types silently
// return 0 on short input - which for IAS Zone `zoneStatus` (map16)
// produces an all-bits-false bitmap that looks like "all alarms clear",
// and for any status-bearing attribute promotes a truncated value into
// a SUCCESS-looking enum.
if (DataType.length > 0 && buf.length - i < DataType.length) {
throw new ZCLError('MALFORMED_COMMAND');
}
const entry = DataType.fromBuffer(buf, i, true);
if (DataType.length > 0) {
i += DataType.length;
res.value = entry;
} else {
res.value = entry.result;
i += entry.length;
}
}
}
if (returnLength) {
return { result: res, length: i - startByte };
}
return res;
});
const ZCLConfigureReportingRecords = ({ withStatus } = {}) => new ZCLDataType(
NaN,
'configureReportingRecords',
-0,
((buf, value, i) => {
// toBuffer
const startByte = i;
// Loop each configure reporting record
for (const configureReportingRecord of value) {
// Remove the minChange property (because its size will vary)
const { minChange, direction } = configureReportingRecord;
// Check if this record is expected to have a status field
if (withStatus) {
ZCLDataTypes.enum8Status.toBuffer(buf, configureReportingRecord.status, i);
i += ZCLDataTypes.enum8Status.length;
}
ZCLConfigureReportingRecordStruct.fields.direction
.toBuffer(buf, configureReportingRecord.direction, i);
i += ZCLConfigureReportingRecordStruct.fields.direction.length;
ZCLConfigureReportingRecordStruct.fields.attributeId
.toBuffer(buf, configureReportingRecord.attributeId, i);
i += ZCLConfigureReportingRecordStruct.fields.attributeId.length;
// Nothing further to parse if it was not a success
if (withStatus && configureReportingRecord.status !== 'SUCCESS') {
continue;
}
if (direction === 'reported') {
// Determine the type of the minChange property (equal to attributeDataType of this record)
const minChangeDataType = Object.values(ZCLDataTypes)
.find(DataType => DataType.id === configureReportingRecord.attributeDataType);
ZCLConfigureReportingRecordStruct.fields.attributeDataType
.toBuffer(buf, configureReportingRecord.attributeDataType, i);
i += ZCLConfigureReportingRecordStruct.fields.attributeDataType.length;
ZCLConfigureReportingRecordStruct.fields.minInterval
.toBuffer(buf, configureReportingRecord.minInterval, i);
i += ZCLConfigureReportingRecordStruct.fields.minInterval.length;
ZCLConfigureReportingRecordStruct.fields.maxInterval
.toBuffer(buf, configureReportingRecord.maxInterval, i);
i += ZCLConfigureReportingRecordStruct.fields.maxInterval.length;
if (minChangeDataType && typeof minChange === 'number') {
minChangeDataType.toBuffer(buf, minChange, i);
i += minChangeDataType.length;
}
} else {
ZCLConfigureReportingRecordStruct.fields.timeoutPeriod
.toBuffer(buf, configureReportingRecord.timeoutPeriod, i);
i += ZCLConfigureReportingRecordStruct.fields.timeoutPeriod.length;
}
}
return i - startByte;
}), ((buf, i, returnLength) => {
// fromBuffer
i = i || 0;
// Keep a reference to the current buffer index while reading it
const startByte = i;
const res = [];
// Read the entire buffer
while (buf.length > i) {
// Parse status only if part of record struct (the read configuration response has a
// status field whereas the configure reporting command does not)
let status;
// Check if this record is expected to have a status field
if (withStatus) {
status = ZCLDataTypes.enum8Status.fromBuffer(buf, i);
i += ZCLDataTypes.enum8Status.length;
}
// Parse direction
const direction = ZCLConfigureReportingRecordStruct.fields.direction.fromBuffer(buf, i);
i += 1;
// Parse attributeId
const attributeId = ZCLConfigureReportingRecordStruct.fields.attributeId.fromBuffer(buf, i);
i += 2;
// Check if the status is success, only then we need to continue parsing for other values
if (withStatus && status !== 'SUCCESS') {
// Buffer parsing is complete
res.push({ status, direction, attributeId });
// Continue reading the buffer in a next iteration
continue;
}
// eslint-disable-next-line default-case
switch (direction) {
// Only if the direction is 'reported' are the minInterval/maxInterval and minChange
// properties available
case 'reported': {
// Parse attributeDataType, we need this to find the respective ZCLDataType in the
// next step
const attributeDataType = ZCLConfigureReportingRecordStruct.fields.attributeDataType
.fromBuffer(buf, i);
i += 1;
// Find the respective ZCLDataType, we need it later on to determine if the data type
// is analog or discrete
const parsedAttributeDataType = Object.values(ZCLDataTypes)
.find(x => x.id === attributeDataType);
// Parse minInterval
const minInterval = ZCLDataTypes.uint16.fromBuffer(buf, i);
i += 2;
// Parse maxInterval
const maxInterval = ZCLDataTypes.uint16.fromBuffer(buf, i);
i += 2;
// If the ZCLDataType is not analog there will be no minChange field
if (!parsedAttributeDataType.isAnalog) {
const resultRecord = {
direction, attributeId, attributeDataType, minInterval, maxInterval,
};
if (withStatus) resultRecord.status = status;
res.push(resultRecord);
// Continue reading the buffer in a next iteration
continue;
}
// Apparently the ZCLDataType is analog so we should parse the minChange field
const minChange = ZCLDataTypes[parsedAttributeDataType.shortName]
.fromBuffer(buf, i);
i += parsedAttributeDataType.length;
const resultRecord = {
direction,
attributeId,
attributeDataType,
minInterval,
maxInterval,
minChange,
};
if (withStatus) resultRecord.status = status;
res.push(resultRecord);
// Buffer parsing is complete
break;
}
// If the direction is 'received' we only have to parse the
case 'received': {
// timeoutPeriod
// Parse timeout period
const timeoutPeriod = ZCLConfigureReportingRecordStruct.fields.timeoutPeriod
.fromBuffer(buf, i);
i += 2;
const resultRecord = {
direction, attributeId, timeoutPeriod,
};
if (withStatus) resultRecord.status = status;
res.push(resultRecord);
// Buffer parsing is complete
break;
}
}
}
if (returnLength) {
return { result: res, length: i - startByte };
}
return res;
}),
);
module.exports = {
ZCLStandardHeader,
ZCLMfgSpecificHeader,
ZCLAttributeDataRecord,
ZCLConfigureReportingRecords,
};