-
Notifications
You must be signed in to change notification settings - Fork 308
/
znp.ts
executable file
·378 lines (320 loc) · 13.8 KB
/
znp.ts
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import {
Writer as UnpiWriter,
Parser as UnpiParser,
Frame as UnpiFrame,
} from '../unpi';
import {Wait, Queue, Waitress, RealpathSync} from '../../../utils';
import {SerialPort} from '../../serialPort';
import SerialPortUtils from '../../serialPortUtils';
import SocketPortUtils from '../../socketPortUtils';
import * as Constants from '../constants';
import ZpiObject from './zpiObject';
import {ZpiObjectPayload} from './tstype';
import {Subsystem, Type} from '../unpi/constants';
import net from 'net';
import events from 'events';
import Equals from 'fast-deep-equal/es6';
import Debug from "debug";
const {COMMON: {ZnpCommandStatus}, Utils: {statusDescription}} = Constants;
const timeouts = {
SREQ: 6000,
reset: 30000,
default: 10000,
};
const debug = {
error: Debug('zigbee-herdsman:adapter:zStack:znp:error'),
timeout: Debug('zigbee-herdsman:adapter:zStack:znp:timeout'),
log: Debug('zigbee-herdsman:adapter:zStack:znp:log'),
SREQ: Debug('zigbee-herdsman:adapter:zStack:znp:SREQ'),
AREQ: Debug('zigbee-herdsman:adapter:zStack:znp:AREQ'),
SRSP: Debug('zigbee-herdsman:adapter:zStack:znp:SRSP'),
};
interface WaitressMatcher {
type: Type;
subsystem: Subsystem;
command: string;
payload?: ZpiObjectPayload;
}
const autoDetectDefinitions = [
{manufacturer: 'Texas Instruments', vendorId: '0451', productId: '16c8'}, // CC2538
{manufacturer: 'Texas Instruments', vendorId: '0451', productId: '16a8'}, // CC2531
{manufacturer: 'Texas Instruments', vendorId: '0451', productId: 'bef3'}, // CC1352P_2 and CC26X2R1
{manufacturer: 'Electrolama', vendorId: '0403', productId: '6015'}, // ZZH
];
class Znp extends events.EventEmitter {
private path: string;
private baudRate: number;
private rtscts: boolean;
private portType: 'serial' | 'socket';
private serialPort: SerialPort;
private socketPort: net.Socket;
private unpiWriter: UnpiWriter;
private unpiParser: UnpiParser;
private initialized: boolean;
private queue: Queue;
private waitress: Waitress<ZpiObject, WaitressMatcher>;
public constructor(path: string, baudRate: number, rtscts: boolean) {
super();
this.path = path;
this.baudRate = typeof baudRate === 'number' ? baudRate : 115200;
this.rtscts = typeof rtscts === 'boolean' ? rtscts : false;
this.portType = SocketPortUtils.isTcpPath(path) ? 'socket' : 'serial';
this.initialized = false;
this.queue = new Queue();
this.waitress = new Waitress<ZpiObject, WaitressMatcher>(this.waitressValidator, this.waitressTimeoutFormatter);
this.onUnpiParsed = this.onUnpiParsed.bind(this);
this.onPortClose = this.onPortClose.bind(this);
}
private log(type: Type, message: string): void {
if (type === Type.SRSP) {
debug.SRSP(message);
} else if (type === Type.AREQ) {
debug.AREQ(message);
} else {
/* istanbul ignore else */
if (type === Type.SREQ) {
debug.SREQ(message);
} else {
throw new Error(`Unknown type '${type}'`);
}
}
}
private onUnpiParsed(frame: UnpiFrame): void {
try {
const object = ZpiObject.fromUnpiFrame(frame);
const message =
`<-- ${Subsystem[object.subsystem]} - ${object.command} - ${JSON.stringify(object.payload)}`;
this.log(object.type, message);
this.waitress.resolve(object);
this.emit('received', object);
} catch (error) {
debug.error(`Error while parsing to ZpiObject '${error.stack}'`);
}
}
public isInitialized(): boolean {
return this.initialized;
}
private onPortClose(): void {
debug.log('Port closed');
this.initialized = false;
this.emit('close');
}
public async open(): Promise<void> {
return this.portType === 'serial' ? this.openSerialPort() : this.openSocketPort();
}
private async openSerialPort(): Promise<void> {
const options = {path: this.path, baudRate: this.baudRate, rtscts: this.rtscts, autoOpen: false};
debug.log(`Opening SerialPort with ${JSON.stringify(options)}`);
this.serialPort = new SerialPort(options);
this.unpiWriter = new UnpiWriter();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.unpiWriter.pipe(this.serialPort);
this.unpiParser = new UnpiParser();
this.serialPort.pipe(this.unpiParser);
this.unpiParser.on('parsed', this.onUnpiParsed);
return new Promise((resolve, reject): void => {
this.serialPort.open(async (error): Promise<void> => {
if (error) {
reject(new Error(`Error while opening serialport '${error}'`));
this.initialized = false;
if (this.serialPort.isOpen) {
this.serialPort.close();
}
} else {
debug.log('Serialport opened');
this.initialized = true;
await this.skipBootloader();
this.serialPort.once('close', this.onPortClose);
this.serialPort.once('error', (error) => {
debug.error(`Serialport error: ${error}`);
});
resolve();
}
});
});
}
private async openSocketPort(): Promise<void> {
const info = SocketPortUtils.parseTcpPath(this.path);
debug.log(`Opening TCP socket with ${info.host}:${info.port}`);
this.socketPort = new net.Socket();
this.socketPort.setNoDelay(true);
this.socketPort.setKeepAlive(true, 15000);
this.unpiWriter = new UnpiWriter();
this.unpiWriter.pipe(this.socketPort);
this.unpiParser = new UnpiParser();
this.socketPort.pipe(this.unpiParser);
this.unpiParser.on('parsed', this.onUnpiParsed);
return new Promise((resolve, reject): void => {
this.socketPort.on('connect', function() {
debug.log('Socket connected');
});
// eslint-disable-next-line
const self = this;
this.socketPort.on('ready', async function() {
debug.log('Socket ready');
await self.skipBootloader();
self.initialized = true;
resolve();
});
this.socketPort.once('close', this.onPortClose);
this.socketPort.on('error', function () {
debug.log('Socket error');
reject(new Error(`Error while opening socket`));
self.initialized = false;
});
this.socketPort.connect(info.port, info.host);
});
}
private async skipBootloader(): Promise<void> {
try {
await this.request(Subsystem.SYS, 'ping', {capabilities: 1}, null, 250);
} catch (error) {
// Skip bootloader on CC2530/CC2531
// Send magic byte: https://github.com/Koenkk/zigbee2mqtt/issues/1343 to bootloader
// and give ZNP 1 second to start.
try {
debug.log('Writing CC2530/CC2531 skip bootloader payload');
this.unpiWriter.writeBuffer(Buffer.from([0xef]));
await Wait(1000);
await this.request(Subsystem.SYS, 'ping', {capabilities: 1}, null, 250);
} catch (error) {
// Skip bootloader on some CC2652 devices (e.g. zzh-p)
debug.log('Skip bootloader for CC2652/CC1352');
if (this.serialPort) {
await this.setSerialPortOptions({dtr: false, rts: false});
await Wait(150);
await this.setSerialPortOptions({dtr: false, rts: true});
await Wait(150);
await this.setSerialPortOptions({dtr: false, rts: false});
await Wait(150);
}
}
}
}
private async setSerialPortOptions(options: {dtr?: boolean; rts?: boolean}): Promise<void> {
return new Promise((resolve): void => {
this.serialPort.set(options, () => {
resolve();
});
});
}
public static async isValidPath(path: string): Promise<boolean> {
// For TCP paths we cannot get device information, therefore we cannot validate it.
if (SocketPortUtils.isTcpPath(path)) {
return false;
}
try {
return SerialPortUtils.is(RealpathSync(path), autoDetectDefinitions);
} catch (error) {
debug.error(`Failed to determine if path is valid: '${error}'`);
return false;
}
}
public static async autoDetectPath(): Promise<string> {
const paths = await SerialPortUtils.find(autoDetectDefinitions);
// CC1352P_2 and CC26X2R1 lists as 2 USB devices with same manufacturer, productId and vendorId
// one is the actual chip interface, other is the XDS110.
// The chip is always exposed on the first one after alphabetical sorting.
paths.sort((a, b) => (a < b) ? -1 : 1);
return paths.length > 0 ? paths[0] : null;
}
public close(): Promise<void> {
return new Promise((resolve, reject): void => {
if (this.initialized) {
if (this.portType === 'serial') {
this.serialPort.flush((): void => {
this.serialPort.close((error): void => {
this.initialized = false;
error == null ?
resolve() :
reject(new Error(`Error while closing serialport '${error}'`));
this.emit('close');
});
});
} else {
this.socketPort.destroy();
resolve();
}
} else {
resolve();
this.emit('close');
}
});
}
public request(
subsystem: Subsystem, command: string, payload: ZpiObjectPayload, waiterID: number = null,
timeout: number = null, expectedStatuses: Constants.COMMON.ZnpCommandStatus[] = [ZnpCommandStatus.SUCCESS]
): Promise<ZpiObject> {
if (!this.initialized) {
throw new Error('Cannot request when znp has not been initialized yet');
}
const object = ZpiObject.createRequest(subsystem, command, payload);
const message = `--> ${Subsystem[object.subsystem]} - ${object.command} - ${JSON.stringify(payload)}`;
return this.queue.execute<ZpiObject>(async (): Promise<ZpiObject> => {
this.log(object.type, message);
const frame = object.toUnpiFrame();
if (object.type === Type.SREQ) {
const t = object.command === 'bdbStartCommissioning' || object.command === 'startupFromApp' ?
40000 : timeouts.SREQ;
const waiter = this.waitress.waitFor(
{type: Type.SRSP, subsystem: object.subsystem, command: object.command}, timeout || t
);
this.unpiWriter.writeFrame(frame);
const result = await waiter.start().promise;
if (result && result.payload.hasOwnProperty('status') &&
!expectedStatuses.includes(result.payload.status)) {
if (typeof waiterID === 'number') {
this.waitress.remove(waiterID);
}
throw new Error(
`SREQ '${message}' failed with status '${
statusDescription(result.payload.status)
}' (expected '${expectedStatuses.map(statusDescription)}')`
);
} else {
return result;
}
} else if (object.type === Type.AREQ && object.isResetCommand()) {
const waiter = this.waitress.waitFor(
{type: Type.AREQ, subsystem: Subsystem.SYS, command: 'resetInd'}, timeout || timeouts.reset
);
this.queue.clear();
this.unpiWriter.writeFrame(frame);
return waiter.start().promise;
} else {
/* istanbul ignore else */
if (object.type === Type.AREQ) {
this.unpiWriter.writeFrame(frame);
return undefined;
} else {
throw new Error(`Unknown type '${object.type}'`);
}
}
});
}
private waitressTimeoutFormatter(matcher: WaitressMatcher, timeout: number): string {
return `${Type[matcher.type]} - ${Subsystem[matcher.subsystem]} - ${matcher.command} after ${timeout}ms`;
}
public waitFor(
type: Type, subsystem: Subsystem, command: string, payload: ZpiObjectPayload = {},
timeout: number = timeouts.default
): {start: () => {promise: Promise<ZpiObject>; ID: number}; ID: number} {
return this.waitress.waitFor({type, subsystem, command, payload}, timeout);
}
private waitressValidator(zpiObject: ZpiObject, matcher: WaitressMatcher): boolean {
const requiredMatch = matcher.type === zpiObject.type && matcher.subsystem == zpiObject.subsystem &&
matcher.command === zpiObject.command;
let payloadMatch = true;
if (matcher.payload) {
for (const [key, value] of Object.entries(matcher.payload)) {
if (!Equals(zpiObject.payload[key], value)) {
payloadMatch = false;
break;
}
}
}
return requiredMatch && payloadMatch;
}
}
export default Znp;