-
Notifications
You must be signed in to change notification settings - Fork 40
/
device.js
executable file
·622 lines (533 loc) · 20 KB
/
device.js
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
const { EventEmitter } = require('events');
const { join: joinPath } = require('path');
const debug = require('debug')('ps4:device');
const Detector = require('./detector');
const OnScreenKeyboard = require('./osk');
const Socket = require('./ps4socket');
const Waker = require('./waker');
const { delayMillis } = require('./util');
const DEFAULT_TIMEOUT = 10000;
const POST_CONNECT_SENDKEY_DELAY = 1500;
// min delay between sendKey sends
const MIN_SENDKEY_DELAY = 200;
const HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const DEFAULT_CREDS = joinPath(HOME, '.ps4-wake.credentials.json');
// I'm just guessing at the meaning of this error code...
const ERROR_OTHER_APP_RUNNING = 12;
/**
* Device is a high-level abstraction on top of a single
* PS4 device. It maintains a single, active connection
* to the device so repeated commands won't cause lots of
* annoying "connected" and "disconnected" messages to pop
* up on your device.
*
* Device is also an EventEmitter, and emits events from
* Waker and Socket.
*/
class Device extends EventEmitter {
/**
* Construct a new Device. Accepts an options map with
* the following keys:
*
* - address: (optional) IP address of a specific device.
* If omitted, will operate on the first device
* detected
* - autoLogin: (default: true) If false, will skip logging
* into an account when waking the device.
* NOTE: if autoLogin is false, ONLY the following
* functions will work:
* - turnOn()
* - getDeviceStatus()
* Everything else will encounter errors!
* - credentials: (optional) Path to a ps4-wake.credentials.json
* file to use. If not provided, uses one in the
* home directory of the current user.
* - passCode: (optional) 4-digit string, if the account whose
* credentials we're using has set one
* - timeout: How long network operations can be stalled before
* we give up on them and throw an error
*
* In addition, it respects the following keys from `detectOpts`,
* normally passed to Detector:
*
* - bindAddress: Address on which to bind the local udp detection
* socket, in case of multiple network interfaces.
* If omitted, will bind on the default interface.
* SEe dgram.Socket.bind() option `address`
* - bindPort: Port on which to bind the local udp detection socket,
* in case you need to explicitly route. If omitted,
* will bind on any available port the system wishes
* to assign.
*/
constructor(opts) {
super();
this.opts = {
autoLogin: true,
credentials: DEFAULT_CREDS,
passCode: '',
timeout: DEFAULT_TIMEOUT,
debug: false,
...opts,
};
if (!this.opts.credentials || !this.opts.credentials.length) {
this.opts.credentials = DEFAULT_CREDS;
}
if (!this.opts.timeout && this.opts.timeout !== 0) {
this.opts.timeout = DEFAULT_TIMEOUT;
}
// see getDeviceStatus for the structure of this
this.lastInfo = null;
this._retryDelay = 500;
// init clean state
this._onClose();
}
/**
* @return True if we believe we currently have an
* active connection to the device.
*/
get isConnected() {
return !!this._socket;
}
/**
* Immediately close any active connection to this Device
*/
async close() {
const socket = this._socket;
this.__waker = null;
this._socket = null;
if (!socket) return Promise.resolve();
socket.close();
return new Promise((resolve, reject) => {
socket.once('error', (err) => {
reject(err);
});
socket.once('disconnected', () => {
resolve();
});
});
}
/**
* Fetch the raw device status message from the device.
* If this device was detected, resolves to an object
* that looks like:
*
* {
* status: "Standby",
* statusCode: "620",
* statusLine: "620 Server Standby",
* address: "192.168.2.3",
* device-discovery-protocol-version: "00020020",
* host-name: "My PS4",
* host-type: "PS4",
* port: "997",
* system-version: "04550011",
* }
*/
async getDeviceStatus() {
const result = await this._detect();
return result.device;
}
/**
* Get an active Socket instance connected to this device,
* turning the device on if necessary. This is a low-level
* method that probably won't be necessary for most users.
*/
async openSocket() {
return this._connect();
}
/**
* Get an instance of OnScreenKeyboard, if it is possible to
* do so. If there is no text field on screen, this will
* reject with an error.
*/
async getKeyboard() {
if (this._osk) return this._osk;
const socket = await this.openSocket();
// ensure we're logged in properly
await this.login();
return new Promise((resolve, reject) => {
/* eslint-disable consistent-return */
socket.startOsk((err, packet) => {
if (err) return reject(err);
const osk = new OnScreenKeyboard(this, packet);
osk.once('close', () => {
this._osk = null;
});
resolve(this._osk = osk);
});
/* eslint-enable consistent-return */
});
}
/**
* Send a sequence of remote key presses to this device,
* turning the device on if necessary. Resolves to this object.
* Key names are case insensitive, and can be one of:
*
* up, down, left, right, enter, back, option, ps
*
* In addition, a key may instead be a tuple of [key, holdTime],
* where holdTime is an int indicating how long, in milliseconds,
* the key should be held for
*/
async sendKeys(keyNames) {
// validate keys:
if (!keyNames || !keyNames.length) {
throw new Error('No keys provided');
}
if (arguments.length !== 1 || !Array.isArray(keyNames)) {
throw new Error('sendKeys must be called with an array');
}
const cleanKeys = keyNames.map((key) => {
if (Array.isArray(key)) {
return [key[0].toUpperCase(), key[1]];
}
if (typeof (key) !== 'string') {
throw new Error(`Invalid key: ${key}; must be a string or a tuple`);
}
return [key.toUpperCase(), 0];
});
const invalid = cleanKeys.filter((key) => !(key[0] in Socket.RCKeys));
if (invalid.length) {
throw new Error(`Unknown key names: ${invalid.map((key) => key[0])}`);
}
const socket = await this.openSocket();
// ensure we're logged in properly
await this.login();
const msSinceConnect = Date.now() - this._connectedAt;
const delay = POST_CONNECT_SENDKEY_DELAY - msSinceConnect;
if (delay > 0) {
// give it some time to think---if we try to OPEN_RC
// too soon after connecting, the ps4 seems to disregard
await delayMillis(delay);
}
socket.remoteControl(Socket.RCKeys.OPEN_RC);
await delayMillis(MIN_SENDKEY_DELAY);
/* eslint-disable no-await-in-loop */
for (const [key, holdTime] of cleanKeys) {
// near as I can tell, here's how this works:
// - For a simple tap, you send the key with holdTime=0,
// followed by KEY_OFF and holdTime = 0
// - For a long press/hold, you still send the key with
// holdTime=0, the follow it with the key again, but
// specifying holdTime as the hold duration.
// - After sending a direction, you should send KEY_OFF
// to clean it up (since it can just be held forever).
// Doing this after a long-press of PS just breaks it,
// however.
const val = Socket.RCKeys[key];
socket.remoteControl(val, 0);
if (holdTime) {
await delayMillis(holdTime);
socket.remoteControl(val, holdTime);
}
// clean up the keypress. As mentioned above, after holding
// a direction, sending KEY_OFF seems to make further
// presses more reliable; doing that after holding PS button
// breaks it, however.
if (!holdTime || val !== Socket.RCKeys.PS) {
socket.remoteControl(Socket.RCKeys.KEY_OFF, 0);
}
if (!key.endsWith('_RC')) {
this.emit('sent-key', key);
}
await delayMillis(
val === Socket.RCKeys.PS
? 1000 // higher delay after PS button press
: MIN_SENDKEY_DELAY, // too much lower and it becomes unreliable
);
}
/* eslint-enable no-await-in-loop */
socket.remoteControl(Socket.RCKeys.CLOSE_RC);
await delayMillis(MIN_SENDKEY_DELAY);
return this;
}
/**
* Start running the application with the given ID on this
* device, turning the device on if necessary. Resolves
* to this object.
*
* @param options (optional) If provided, a map:
* - ui: (optional) an object with a logEvent method
* - autoQuit: (optional) bool, defaults to `true`; when `true`,
* if another app is running when this method is called, we will
* automatically quit it if necessary.
*/
async startTitle(titleId, options) {
await this.openSocket();
// ensure we're logged in properly
await this.login();
const config = {
ui: { logEvent() { /* nop */ } },
autoQuit: true,
...options,
};
const lastInfo = this.lastInfo || {};
if (lastInfo['running-app-titleid'] === titleId) {
config.ui.logEvent('Requested titleId already running');
return this;
}
const willQuitRunningApp = lastInfo['running-app-titleid'];
if (willQuitRunningApp) {
const appName = lastInfo['running-app-name'];
config.ui.logEvent(`"${appName}" already running; quitting it first...`);
await this.sendKeys(['ps']);
}
try {
await this._startTitle(titleId);
} catch (e) {
if (
!willQuitRunningApp
|| e.status !== ERROR_OTHER_APP_RUNNING
|| !config.autoQuit
) {
// unexpected error, or we don't want to auto-quit
throw e;
}
// we're quitting a running app; if we're leaving one game for
// another, receiving this error code probably means we need to
// accept that the application will close.
await this.sendKeys(['enter']);
}
return this;
}
/**
* Turn on this device, if it isn't already.
* Resolves to this object.
*/
async turnOn(timeOut) {
await this._connect();
let loginResult;
if (this._socket) {
// eslint-disable-next-line no-underscore-dangle
loginResult = this._socket._loginResult;
}
// try to wait for a login result
if (!loginResult || loginResult.result !== 0) {
debug('not logged in yet; waiting for login; timeout?', timeOut);
await new Promise((resolve) => {
const handler = (packet) => {
if (packet.result === 0) {
this.removeListener('login_result', handler);
resolve();
}
};
this.on('login_result', handler);
if (timeOut !== false) {
// NOTE: for backwards compatibility, failing to login
// is *not* an error
setTimeout(() => resolve(), this.opts.timeout);
}
});
} else {
debug('already logged in!');
}
return this;
}
/**
* Turn off this device (put it into standby)
* if it isn't already. Resolves to this object.
*/
async turnOff(/* _existingResolve */) { // eslint-disable-line
const socket = await this._connectIfAwake();
if (!socket) {
// it's already off
return this;
}
// ensure we're logged in properly
await this.login();
const isRetry = arguments.length > 0;
const doRequestStandby = (resolve, reject) => {
socket.requestStandby((err) => {
if (err && isRetry) {
reject(err);
} else if (err) {
// error; disconnecting and retrying
socket.close();
this._onClose();
setTimeout(() => this.turnOff(resolve, reject), this._retryDelay);
} else {
resolve(this);
}
});
};
if (isRetry) {
// we were provided a (resolve, reject) pair from the
// retry above.
// eslint-disable-next-line
doRequestStandby(arguments[0], arguments[1]);
} else {
return new Promise(doRequestStandby);
}
}
/**
* Connect to the device and wake it, if it's not already
* awake, and ensure we're logged in. In general, methods
* that require logged-in state (such as `turnOff()`) will
* call this for you, so you probably don't need to worry
* about it yourself.
*/
async login() {
const errorFromResult = (result) => {
if (!result.error) return null;
return new Error(`Failed to login: ${result.error} (${result.result})`);
};
if (this._socket) {
const { _loginResult } = this._socket;
if (_loginResult) {
const e = errorFromResult(_loginResult);
if (e) throw e;
return this;
}
}
return new Promise((resolve, reject) => {
this.once('login_result', (packet) => {
const e = errorFromResult(packet);
if (e) reject(e);
else resolve(this);
});
this._connect(/* autoLogin: */ true);
});
}
/**
* If this device is awake, connect to it and
* resolve to the socket; otherwise, resolve
* to null.
*/
async _connectIfAwake() {
const isAwake = await this._detectAwake();
if (!isAwake) return null;
return this._connect();
}
/**
* Connect to this device, waking it if necessary;
* @return the socket, or `undefined` if autoLogin is false
*/
async _connect(autoLogin) {
if (this._socket) return this._socket;
// find the right device, if any
const result = await this._detect();
const opts = {
...this.opts,
};
if (autoLogin !== undefined) {
opts.autoLogin = autoLogin;
}
return new Promise((resolve, reject) => {
// we don't need to return anything from this callback:
// eslint-disable-next-line
this._waker().wake(opts, result.device, (err, socket) => {
debug(
`wake result: (autoLogin=${opts.autoLogin})`,
err, `socket? ${!!socket}`,
);
if (err) return reject(err);
if (!opts.autoLogin) return resolve();
if (!socket) return reject(new Error('No socket'));
if (this._socket) {
// close existing socket
this._socket.close();
this._onClose();
}
this._socket = socket;
this._connectedAt = Date.now();
// forward socket events:
socket.on('connected', () => {
this.emit('connected', this);
}).on('ready', () => {
this.emit('ready', this);
}).on('login_result', (loginResult) => {
// NOTE: we're more likely to get this event from the waker
this.emit('login_result', loginResult);
}).on('login_retry', () => {
this.emit('login_retry', this);
}).on('error', (e) => {
this.emit('error', e);
}).on('disconnected', () => {
this._onClose();
this.emit('disconnected', this);
});
resolve(socket);
// checking socket.client is a hack to
// confirm that we're already connected
if (socket.client) {
// in fact, if we have a socket here
// it should be connected...
this.emit('connected', this);
}
});
});
}
/**
* Returns a Promise that resolves to `true` if
* this device is awake, and `false` if not;
* rejects if the device could not be found.
*/
async _detectAwake() {
const result = await this._detect();
return result.device.status.toUpperCase() === 'OK';
}
/**
* Detect any device that matches our this.opts.
* Resolves to a map that looks like:
* {
* device: <see getDeviceStatus()>,
* rinfo: info
* }
* TODO: more information please
*/
async _detect() {
return new Promise((resolve, reject) => {
// if the address opt was provided, detect that
// specific device. Otherwise, detect whatever
const fn = this.opts.address
? Detector.find.bind(Detector, this.opts.address)
: Detector.findAny.bind(Detector);
fn(this.opts, (err, device, rinfo) => {
if (err) return reject(err);
// NOTE: we probably don't need to pass along rinfo...
device.address = rinfo.address;
device.port = device['host-request-port'];
this.lastInfo = device;
return resolve({ device, rinfo });
});
});
}
/** reset state when disconnected */
_onClose() {
this._socket = null;
this._osk = null;
this._connectedAt = 0;
}
async _startTitle(titleId) {
const socket = await this.openSocket();
return new Promise((resolve, reject) => {
socket.startTitle(titleId, (err) => {
if (err) return reject(err);
return resolve(this);
});
});
}
/** Create a new Waker instance */
_waker() {
if (this.__waker) return this.__waker;
// eslint-disable-next-line
return this.__waker = new Waker(this.opts.credentials, {
autoLogin: this.opts.autoLogin,
debug: this.opts.debug,
errorIfAwake: false,
keepSocket: true,
ui: this.opts.ui,
}).on('need-credentials', (d) => {
this.emit('need-credentials', d);
}).on('device-notified', (d) => {
this.emit('device-notified', d);
}).on('logging-in', (d) => {
this.emit('logging-in', d);
}).on('login_result', (packet) => {
// yuck
debug('got login result from waker', packet, ' sock?', !!this._socket);
this.emit('login_result', packet);
});
}
}
module.exports = Device;