-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlants-udp.ts
307 lines (284 loc) · 9.38 KB
/
lants-udp.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
// Note -- bug fixed for CIDR "cidr":"172.29.239.177/28", "broadcast":"172.29.239.191"
import { lifxMsgType, mParser, LifxLanHeader } from "./lants-parser.js";
import { delayms } from "./lants.js";
import { getNetworkInterfaces } from "./lants-address.js";
/* ------------------------------------------------------------------
* node-lifx-lan - lifx-lan-udp.js
*
* Copyright (c) 2017-2018, Futomi Hatano, All rights reserved.
* Released under the MIT license
* Date: 2018-07-01
* ---------------------------------------------------------------- */
// 'use strict';
import * as os from 'os';
import * as mDgram from 'dgram';
import * as Composer from './lants-composer.js';
const mComposer = new Composer.LifxLanComposer();
import * as la from './lants-address.js';
import { LifxLanDevice, passure } from "./lants-device.js";
// import { promises } from "fs";
/**
* Handle incoming message that is not otherwise handled
*/
// const mAddress = new la.LifxLanAddress();
export interface udpParams {
address?: string, // IP address of the destination (e.g., "192.168.10.10") (optional for broadcast)
type: lifxMsgType, // Message Type (e.g., 101)
payload?: object, // Depends on the type
ack_required?: boolean, // The default value is `false`
res_required?: boolean, // The default value is `false`
target?: string, // Required | MAC Address (except if we have broadcast
broadcast?: boolean; // The default is `false`.
};
/**
* Address info for incoming packet
*/
export interface udpRinfo {
address: string; // The sender address.
family: string; // 'IPv4' or 'IPv6'
port: number; // Sender port
size: number; // The message size.
}
export interface udpDiscover {
seq: number,
address: string,
buffer: Buffer,
}
/**
* Parsed message including header and payload
*/
export interface udpParsed {
header: LifxLanHeader;
payload: {}
address?: string; // We can add IP
mac?: string;
} // {"service:1,"port:56700}}"
export class LifxLanUdp {
static async GetUDP() {
const llu = new LifxLanUdp();
await llu.init();
return llu;
}
private constructor() {
}
// Private
private _UDP_PORT = 56700;
private _udp: mDgram.Socket = null;
private _requests: { [seq: number]: (res: udpParsed) => void } = {};
private _sequence = 0;
private _timeout = 3000; // msec
private _source_id = 0;
private _netif_list: os.NetworkInterfaceInfo[] = null;
/* ------------------------------------------------------------------
* Method: destroy()
* ---------------------------------------------------------------- */
async destroy() {
await this._udp.close();
this._udp.unref();
this._udp = null;
};
private initPromise: Promise<any> = null;
private initialized: boolean = false;
private initializing: boolean = false; // Attempt to deal with an edge case
/**
* Initialize instance. Should only be called once
*/
private async init() {
this.initPromise = new Promise((resolve, reject) => {
this._source_id = Math.floor(Math.random() * 0xffffffff);
let netif_list = getNetworkInterfaces();
if (!netif_list || netif_list.length === 0) {
reject(new Error('No available network interface was found.'));
return;
}
this._netif_list = netif_list;
// Set up a UDP tranceiver
// this._udp = mDgram.createSocket({ type: 'udp4', reuseAddr: true });
this._udp = mDgram.createSocket({ type: 'udp4' }); // , reuseAddr: true }); // Not sure about reuse
this._udp.on('error', (error: Error) => {
reject(error);
});
this._udp.once('listening', () => {
resolve(null);
});
this._udp.on('message', (buf: Buffer, rinfo: udpRinfo) => { this._receivePacket(buf, rinfo); });
// this._udp.bind({ port: this._UDP_PORT });
this._udp.bind();
});
this.initializing = false; // We now have the promise object
return this.initPromise;
};
async request(params: udpParams): Promise<udpParsed | void> {
const p = {
address: params.address,
type: params.type,
payload: params.payload,
ack_required: params.ack_required && !params.broadcast,
res_required: params.res_required && !params.broadcast,
target: params.broadcast ? '00:00:00:00:00:00' : params.target,
broadcast: !!params.broadcast
};
try {
if (p.broadcast)
return await this._requestBroadcast(p);
else
return await this._requestUnicast(p);
}
catch (e: any) {
throw e;
}
};
// private _requestUnicast(p: { [key: string]: UdpParams}) {
private async _requestUnicast(p: udpParams) {
let promise = new Promise<udpParsed>((resolve, reject) => {
// message sequence number
let seq = (this._sequence + 1) % 255;
this._sequence = seq;
// Timer
let timer: NodeJS.Timeout = null;
if (p.ack_required || p.res_required) {
timer = setTimeout(() => {
delete this._requests[seq];
reject(new Error('Timeout'));
}, this._timeout);
}
// Create a request packet
let packet = mComposer.compose({
type: p.type,
payload: p.payload,
sequence: seq,
ack_required: p.ack_required,
res_required: p.res_required,
target: p.target,
source: this._source_id,
tagged: false
});
// Set a callback
if (p.ack_required || p.res_required) {
this._requests[seq] = (res: udpParsed) => {
delete this._requests[seq];
if (timer) clearTimeout(timer); // Accommodate future changes
resolve(res);
};
}
// Send a packet
this._udp.setBroadcast(false);
let buf = packet; // .buffer;
this._udp.send(buf, 0, buf.length, this._UDP_PORT, p.address, (error: any) => {
if (error) {
delete this._requests[seq];
if (timer) {
clearTimeout(timer); // Accommodate future changes
}
reject(error);
} else {
if (!p.ack_required && !p.res_required) {
resolve(null);
}
}
});
});
return promise;
};
private async _requestBroadcast(p: udpParams) {
let req_list: udpDiscover[] = [];
this._netif_list.forEach((netif) => {
let seq = (this._sequence + 1) % 255; // message sequence number
this._sequence = seq;
// Create a request packet
let packet = mComposer.compose({
type: p.type,
payload: p.payload,
sequence: seq,
ack_required: p.ack_required, // false
res_required: p.res_required, // false
target: p.target, // 00:00:00:00:00:00
source: this._source_id,
tagged: false
});
req_list.push({ seq: seq, address: (<any>netif)['broadcast'], buffer: packet });
});
await this._sendBroadcast(req_list);
};
private _receivePacket(buf: Buffer, rinfo: udpRinfo) {
if (this._isNetworkInterfaceAddress(rinfo.address))
return; // Ignore echoes from myself
let parsed = mParser.parse(buf);
if (!parsed)
return;
parsed.address = rinfo.address;
let seq = parsed.header.sequence;
let callback = this._requests[seq];
if (callback) {
callback(parsed);
}
// // We now ignore unsolicited packets
// // else {
// // try {
// // const pay: any = parsed.payload; // QUick hack
// // let name = pay && pay.label;
// // if (!name) {
// // if (this.device_list_hack && this.device_list_hack[parsed.address])
// // name = this.device_list_hack[parsed.address].deviceInfo.label;
// // else
// // name = parsed.address;
// // }
// // name = name.split(' ')[0];
// // // Hack
// // if (name == "My") name += ' ' + parsed.address;
// // const type = lifxMsgType[parsed.header.type] || parsed.header.type.toString();
// // const id = parsed.header.target.split(':').slice(3, 3 + 3).join('');
// // console.log(`${new Date().toLocaleTimeString()} ${id} ${name.padEnd(18)} ${type} ${JSON.stringify(parsed.payload)}`);
// // // https://community.lifx.com/t/why-are-some-bulbs-chatty/4777/3
// // }
// // catch (e) {
// // console.error(`_receivePacket ${e}`);
// // }
// // }
};
// device_list_hack: { [ip: string]: LifxLanDevice }; // So we can report heard
private _isNetworkInterfaceAddress(addr: string) {
return this._netif_list.some(netif => netif.address == addr);
};
async discover(params: { wait?: number }) {
params = passure(params);
const wait = params.wait || 3000;
const req_list: udpDiscover[] = [];
const devices: { [key: string]: udpParsed } = {};
this._netif_list.forEach((netif) => {
let seq = (this._sequence + 1) % 255; // message sequence number
this._sequence = seq;
// Create a request packet
const packet = mComposer.compose({
type: lifxMsgType.GetService,
payload: null,
sequence: seq,
ack_required: false,
res_required: false,
target: '00:00:00:00:00:00',
source: this._source_id,
tagged: true
});
this._requests[seq] = (res: udpParsed) => { // When we get a response add/update the address
let ip = res.address;
// console.log(`${res.header.type} ${res.address}:${(<any>res.payload).port}`);
if (!devices[ip]) devices[ip] = res;
};
const req = { seq: seq, address: (<any>netif)['broadcast'], buffer: packet }
req_list.push(req); // Record it
// this._sendBroadcast(req);
});
await this._sendBroadcast(req_list);
await delayms(wait * 4); // Wait to see that the cats drag in?
const deviceArray: udpParsed[] = [];
for (let ip in devices) deviceArray.push(devices[ip]);
return deviceArray;
};
private async _sendBroadcast(req_list: udpDiscover[]) {
this._udp.setBroadcast(true);
req_list.forEach(req => {
this._udp.send(req.buffer, 0, req.buffer.length, this._UDP_PORT, req.address);
delayms(10); // Why delays?
});
};
}