desky-agent 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/assets/clawd.ico +0 -0
- package/assets/clawd.png +0 -0
- package/bin/cli.js +206 -0
- package/electron/icons.js +79 -0
- package/electron/main.js +219 -0
- package/firmware/boot_app0.bin +0 -0
- package/firmware/bootloader.bin +0 -0
- package/firmware/firmware.bin +0 -0
- package/firmware/manifest.json +33 -0
- package/firmware/partitions.bin +0 -0
- package/package.json +38 -4
- package/src/autostart.js +106 -0
- package/src/board.js +69 -0
- package/src/core.js +172 -0
- package/src/daemon.js +446 -0
- package/src/firmware.js +207 -0
- package/src/flasher.js +113 -0
- package/src/hook.js +86 -0
- package/src/install.js +375 -0
- package/src/link.js +207 -0
- package/src/paths.js +44 -0
- package/src/updater.js +138 -0
- package/src/webserial.js +130 -0
- package/ui/app.html +1470 -0
- package/ui/clawd3d.js +375 -0
- package/ui/models/A04_cap_J2.stl +0 -0
- package/ui/models/A04_cap_J3.stl +0 -0
- package/ui/models/A04_cap_J4.stl +0 -0
- package/ui/models/B0_base.stl +0 -0
- package/ui/models/B1_body.stl +0 -0
- package/ui/models/B2_face_plate.stl +0 -0
- package/ui/models/B3_side_left.stl +0 -0
- package/ui/models/B4_side_right.stl +0 -0
- package/ui/models/C1_column.stl +0 -0
- package/ui/models/C2_column_lid.stl +0 -0
- package/ui/models/D1_desk_frame.stl +0 -0
- package/ui/models/D2_desk_top.stl +0 -0
- package/ui/models/L1_laptop.stl +0 -0
- package/ui/models/P1_mug.stl +0 -0
- package/ui/models/P2_plant.stl +0 -0
- package/ui/models/R1_arm_left.stl +0 -0
- package/ui/models/R2_arm_right.stl +0 -0
- package/ui/models/S1_seat.stl +0 -0
- package/ui/models/S2_spacer.stl +0 -0
- package/ui/vendor/OrbitControls.js +1532 -0
- package/ui/vendor/RoomEnvironment.js +144 -0
- package/ui/vendor/STLLoader.js +410 -0
- package/ui/vendor/three.LICENSE +21 -0
- package/ui/vendor/three.module.min.js +6 -0
package/src/link.js
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Desky's board (ESP32-S3-Zero) on USB: find it, open it without resetting it, keep it. docs/firmware_contract.md.
|
|
3
|
+
//
|
|
4
|
+
// The chip's USB-Serial/JTAG resets the chip on a DTR/RTS toggle (RTS up with DTR down is the reset pulse).
|
|
5
|
+
// Windows: serialport opens with hupcl:false as DTR and RTS both disabled, so nothing moves; leave the lines alone.
|
|
6
|
+
// macOS and Linux: open() itself raises both lines. Dropping DTR first would leave RTS up with DTR down for a
|
|
7
|
+
// moment, so RTS goes down first and only then DTR: up/up, up/down, down/down, never the reset state.
|
|
8
|
+
|
|
9
|
+
const VID = '303a'; // Espressif USB JTAG/serial: any S3/C3/C6, so "who" tells Desky from other boards
|
|
10
|
+
const { parseWho } = require('./board');
|
|
11
|
+
const PID = '1001';
|
|
12
|
+
const BAUD = 115200;
|
|
13
|
+
|
|
14
|
+
let serialport = null;
|
|
15
|
+
function lib() {
|
|
16
|
+
if (!serialport) serialport = require('serialport');
|
|
17
|
+
return serialport;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Every 303A:1001 port: { path, serialNumber }. serialNumber is the chip's MAC on the USB-Serial/JTAG, so it
|
|
21
|
+
* stays the same when the port is renamed after a reset. */
|
|
22
|
+
async function ports() {
|
|
23
|
+
try {
|
|
24
|
+
const list = await lib().SerialPort.list();
|
|
25
|
+
return list.filter(p => String(p.vendorId || '').toLowerCase() === VID && String(p.productId || '').toLowerCase() === PID)
|
|
26
|
+
.map(p => ({ path: process.platform === 'darwin' ? p.path.replace(/^\/dev\/tty\./, '/dev/cu.') : p.path,
|
|
27
|
+
serialNumber: p.serialNumber ? String(p.serialNumber) : null }));
|
|
28
|
+
} catch (e) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function candidates() {
|
|
34
|
+
return (await ports()).map(p => p.path);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
38
|
+
const call = (port, fn, ...args) => new Promise((res, rej) => port[fn](...args, e => (e ? rej(e) : res())));
|
|
39
|
+
|
|
40
|
+
async function openPort(dev) {
|
|
41
|
+
const { SerialPort } = lib();
|
|
42
|
+
const port = new SerialPort({ path: dev, baudRate: BAUD, autoOpen: false, hupcl: false });
|
|
43
|
+
port.on('error', () => {}); // an unplug mid-handshake must not throw; attach() listens for real
|
|
44
|
+
await call(port, 'open');
|
|
45
|
+
if (process.platform !== 'win32') {
|
|
46
|
+
await call(port, 'set', { dtr: true, rts: false });
|
|
47
|
+
await call(port, 'set', { dtr: false, rts: false });
|
|
48
|
+
}
|
|
49
|
+
return port;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Ask "who" once a second until a Desky line comes back (contract, section 2): the parsed answer, or null after ms.
|
|
53
|
+
* stop() lets the caller give up early. Leaves the port open and unpiped. */
|
|
54
|
+
function askWho(port, ms = 8000, stop = () => false) {
|
|
55
|
+
const { ReadlineParser } = lib();
|
|
56
|
+
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
57
|
+
return new Promise(resolve => {
|
|
58
|
+
let ask = null;
|
|
59
|
+
let give = null;
|
|
60
|
+
const done = ok => {
|
|
61
|
+
clearInterval(ask);
|
|
62
|
+
clearTimeout(give);
|
|
63
|
+
parser.removeAllListeners('data');
|
|
64
|
+
port.unpipe(parser);
|
|
65
|
+
resolve(ok);
|
|
66
|
+
};
|
|
67
|
+
parser.on('data', ln => {
|
|
68
|
+
const info = parseWho(ln);
|
|
69
|
+
if (info) done(info);
|
|
70
|
+
});
|
|
71
|
+
const who = () => {
|
|
72
|
+
if (stop()) return done(null);
|
|
73
|
+
try { port.write('who\n'); } catch (e) { done(null); }
|
|
74
|
+
};
|
|
75
|
+
who();
|
|
76
|
+
ask = setInterval(who, 1000);
|
|
77
|
+
give = setTimeout(() => done(null), ms);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class Link {
|
|
82
|
+
/** Keeps the port open. onConnect() runs after every (re)connect, once "who" has answered; lines from the C3
|
|
83
|
+
* go to onLine(). send() drops the line when nothing is connected. */
|
|
84
|
+
constructor({ onConnect = () => {}, onLine = () => {}, onSilent = () => {}, retry = 3000 } = {}) {
|
|
85
|
+
this.onConnect = onConnect;
|
|
86
|
+
this.onSilent = onSilent; // a 303A:1001 port that did not answer "who": maybe an interrupted update
|
|
87
|
+
this.onLine = onLine;
|
|
88
|
+
this.retry = retry;
|
|
89
|
+
this.port = null;
|
|
90
|
+
this.dev = null;
|
|
91
|
+
this.usb = null; // { path, serialNumber } of the connected port
|
|
92
|
+
this.info = null; // the board's "who": { proto, fw, chip, sn }
|
|
93
|
+
this.stopping = false;
|
|
94
|
+
this.timer = null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get connected() {
|
|
98
|
+
return this.port !== null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
start() {
|
|
102
|
+
this.stopping = false;
|
|
103
|
+
this.scan();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async stop() {
|
|
107
|
+
this.stopping = true;
|
|
108
|
+
clearTimeout(this.timer);
|
|
109
|
+
await this.drop();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
send(line) {
|
|
113
|
+
if (!this.port) return false;
|
|
114
|
+
try {
|
|
115
|
+
this.port.write(line + '\n');
|
|
116
|
+
return true;
|
|
117
|
+
} catch (e) {
|
|
118
|
+
this.drop();
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Restart the C3: an RTS pulse with DTR (BOOT) low. The chip drops off USB for a moment; the port closes, the
|
|
124
|
+
* scan finds it again and onConnect() says the state again. */
|
|
125
|
+
async reset() {
|
|
126
|
+
const port = this.port;
|
|
127
|
+
if (!port) return false;
|
|
128
|
+
try {
|
|
129
|
+
await call(port, 'set', { dtr: false, rts: true });
|
|
130
|
+
await sleep(120);
|
|
131
|
+
await call(port, 'set', { dtr: false, rts: false });
|
|
132
|
+
return true;
|
|
133
|
+
} catch (e) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async drop() {
|
|
139
|
+
const port = this.port;
|
|
140
|
+
this.port = null;
|
|
141
|
+
this.dev = null;
|
|
142
|
+
this.usb = null;
|
|
143
|
+
this.info = null;
|
|
144
|
+
if (port && port.isOpen) await call(port, 'close').catch(() => {});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
later() {
|
|
148
|
+
if (!this.stopping) this.timer = setTimeout(() => this.scan(), this.retry);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async scan() {
|
|
152
|
+
if (this.stopping || this.port) return;
|
|
153
|
+
for (const usb of await ports()) {
|
|
154
|
+
const dev = usb.path;
|
|
155
|
+
let port;
|
|
156
|
+
try {
|
|
157
|
+
port = await openPort(dev);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
continue; // held by the remote or a serial monitor
|
|
160
|
+
}
|
|
161
|
+
const info = await this.handshake(port);
|
|
162
|
+
if (info && !this.stopping) {
|
|
163
|
+
this.info = info;
|
|
164
|
+
this.usb = usb;
|
|
165
|
+
this.attach(port, dev);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
await call(port, 'close').catch(() => {});
|
|
169
|
+
if (this.stopping) return; // stop() came while asking: the port is someone else's now
|
|
170
|
+
try { this.onSilent(usb); } catch (e) { /* the daemon's problem */ }
|
|
171
|
+
}
|
|
172
|
+
this.later();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
handshake(port) {
|
|
176
|
+
// a freshly plugged board is still booting (about 4 s to the first prompt): askWho repeats until it answers
|
|
177
|
+
return askWho(port, 8000, () => this.stopping);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
attach(port, dev) {
|
|
181
|
+
const { ReadlineParser } = lib();
|
|
182
|
+
this.port = port;
|
|
183
|
+
this.dev = dev;
|
|
184
|
+
// read everything: unread lines once filled the C3's USB buffer and its watchdog reset it
|
|
185
|
+
const parser = port.pipe(new ReadlineParser({ delimiter: '\n' }));
|
|
186
|
+
parser.on('data', ln => {
|
|
187
|
+
ln = String(ln).trim();
|
|
188
|
+
if (ln) {
|
|
189
|
+
try { this.onLine(ln); } catch (e) { /* the daemon's problem, not the port's */ }
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const gone = () => {
|
|
193
|
+
if (this.port !== port) return;
|
|
194
|
+
this.port = null;
|
|
195
|
+
this.dev = null;
|
|
196
|
+
this.usb = null;
|
|
197
|
+
this.info = null;
|
|
198
|
+
if (port.isOpen) port.close(() => {});
|
|
199
|
+
this.later();
|
|
200
|
+
};
|
|
201
|
+
port.on('close', gone);
|
|
202
|
+
port.on('error', gone);
|
|
203
|
+
try { this.onConnect(); } catch (e) { /* as above */ }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
module.exports = { Link, candidates, ports, openPort, askWho, VID, PID };
|
package/src/paths.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Where Desky keeps things. Everything lives in one folder in the home directory (~/.clawd-desk), not in
|
|
3
|
+
// %LOCALAPPDATA%: hooks started by the Claude desktop app on Windows (an MSIX package) see a virtualised AppData.
|
|
4
|
+
// CLAWD_HOME moves it (tests), CLAWD_PORT overrides the port.
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const os = require('os');
|
|
8
|
+
const path = require('path');
|
|
9
|
+
|
|
10
|
+
const HOME = process.env.CLAWD_HOME || path.join(os.homedir(), '.clawd-desk');
|
|
11
|
+
const STATE = path.join(HOME, 'state');
|
|
12
|
+
|
|
13
|
+
const paths = {
|
|
14
|
+
HOME,
|
|
15
|
+
STATE,
|
|
16
|
+
CONFIG: path.join(HOME, 'config.json'),
|
|
17
|
+
LAUNCH: path.join(HOME, 'launch.json'), // how to start the app: written by the installer, read by the hook
|
|
18
|
+
STOPPED: path.join(STATE, 'stopped'), // "clawd stop" or "Вийти": hooks must not start the app again
|
|
19
|
+
LOG: path.join(STATE, 'clawd.log'),
|
|
20
|
+
WINDOW: path.join(STATE, 'window.json'),
|
|
21
|
+
DEFAULT_PORT: 47811,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
paths.port = function port() {
|
|
25
|
+
if (process.env.CLAWD_PORT) return Number(process.env.CLAWD_PORT);
|
|
26
|
+
try {
|
|
27
|
+
const p = Number(JSON.parse(fs.readFileSync(paths.CONFIG, 'utf8')).port);
|
|
28
|
+
if (p) return p;
|
|
29
|
+
} catch (e) { /* no config: the default */ }
|
|
30
|
+
return paths.DEFAULT_PORT;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
paths.markStopped = function markStopped(on) {
|
|
34
|
+
try {
|
|
35
|
+
if (on) {
|
|
36
|
+
fs.mkdirSync(STATE, { recursive: true });
|
|
37
|
+
fs.writeFileSync(paths.STOPPED, '');
|
|
38
|
+
} else {
|
|
39
|
+
fs.rmSync(paths.STOPPED, { force: true });
|
|
40
|
+
}
|
|
41
|
+
} catch (e) { /* best effort */ }
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
module.exports = paths;
|
package/src/updater.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Find the boards, decide, flash, check. Shared by the installer, "desky-agent firmware" and the tray app.
|
|
3
|
+
// Whoever calls it must have the port free: the tray app stops its Link first, the CLI stops the tray app.
|
|
4
|
+
|
|
5
|
+
const fw = require('./firmware');
|
|
6
|
+
const link = require('./link');
|
|
7
|
+
|
|
8
|
+
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
9
|
+
|
|
10
|
+
/** Open a port without resetting the board and ask "who": { usb, who, busy }. */
|
|
11
|
+
async function probe(usb, ms = 8000) {
|
|
12
|
+
let port;
|
|
13
|
+
try {
|
|
14
|
+
port = await link.openPort(usb.path);
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return { usb, who: null, busy: true };
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return { usb, who: await link.askWho(port, ms), busy: false };
|
|
20
|
+
} finally {
|
|
21
|
+
await new Promise(r => port.close(() => r()));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function scan() {
|
|
26
|
+
let found = await link.ports();
|
|
27
|
+
for (let i = 0; i < 5 && !found.length; i++) { // a board in the middle of a reboot is off USB for a moment
|
|
28
|
+
await sleep(1000);
|
|
29
|
+
found = await link.ports();
|
|
30
|
+
}
|
|
31
|
+
const out = [];
|
|
32
|
+
for (const usb of found) out.push(await probe(usb));
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** After the reset the board drops off USB and comes back (maybe under another name on macOS): find it by its USB
|
|
37
|
+
* serial and wait for "who". The new program needs a few seconds to boot. */
|
|
38
|
+
async function waitWho(usb, ms = 30000) {
|
|
39
|
+
const end = Date.now() + ms;
|
|
40
|
+
while (Date.now() < end) {
|
|
41
|
+
const now = (await link.ports()).find(p => (usb.serialNumber && p.serialNumber ? p.serialNumber === usb.serialNumber
|
|
42
|
+
: p.path === usb.path));
|
|
43
|
+
if (now) {
|
|
44
|
+
const r = await probe(now, Math.min(8000, Math.max(1000, end - Date.now())));
|
|
45
|
+
if (r.who) return r.who;
|
|
46
|
+
}
|
|
47
|
+
await sleep(1000);
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function bundle() {
|
|
53
|
+
return fw.loadBundle();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Flash one board and check it: { ok, refused?, who?, text }, text being one Russian line for people.
|
|
57
|
+
* The "update started" marker goes to state/ once the bootloader said ESP32-S3, before the first byte; it is
|
|
58
|
+
* removed only when the board answers "who" with the new version. */
|
|
59
|
+
async function update(usb, { bundle: b, from = null, sn = null, log = () => {}, onProgress = () => {}, deps = {} } = {}) {
|
|
60
|
+
const writeParts = deps.writeParts || require('./flasher').writeParts;
|
|
61
|
+
const wait = deps.waitWho || waitWho;
|
|
62
|
+
const { ChipError } = require('./flasher');
|
|
63
|
+
try {
|
|
64
|
+
await writeParts({
|
|
65
|
+
path: usb.path, bundle: b, log, onProgress,
|
|
66
|
+
beforeWrite: async () => fw.writeMarker({ port: usb.path, usbSerial: usb.serialNumber, sn, from, to: b.version }),
|
|
67
|
+
});
|
|
68
|
+
} catch (e) {
|
|
69
|
+
if (e instanceof ChipError || (e && e.chip)) {
|
|
70
|
+
fw.rememberRefused(usb, e.chip);
|
|
71
|
+
return { ok: false, refused: true, text: `${usb.path}: ${e.message}. Не чіпаю й більше не пробую` };
|
|
72
|
+
}
|
|
73
|
+
return { ok: false, text: `прошивка на ${usb.path} не оновилася: ${e.message}. Не від'єднуй плату й запусти `
|
|
74
|
+
+ 'npx desky-agent firmware update' };
|
|
75
|
+
}
|
|
76
|
+
const who = await wait(usb);
|
|
77
|
+
if (who && who.fw === b.version) {
|
|
78
|
+
fw.clearMarker();
|
|
79
|
+
return { ok: true, who, text: `прошивку Desky оновлено: ${from || 'без версії'} → ${b.version} (${usb.path})` };
|
|
80
|
+
}
|
|
81
|
+
return { ok: false, who, text: who
|
|
82
|
+
? `після запису плата на ${usb.path} відповідає fw=${who.fw || '?'}, а чекали ${b.version}`
|
|
83
|
+
: `після запису ${usb.path} не відповів на who. Запусти npx desky-agent firmware update` };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The whole pass over every 303A:1001 port, for the installer and the CLI.
|
|
87
|
+
* force "firmware update": write the same version again too (never an older one)
|
|
88
|
+
* yes a silent board counts as confirmed
|
|
89
|
+
* ask(q) a terminal question for a silent board without a marker -> true/false; null: do not ask
|
|
90
|
+
* quiet say nothing about boards that are fine
|
|
91
|
+
* -> { flashed, failed, boards } */
|
|
92
|
+
async function run({ force = false, yes = false, ask = null, quiet = false, say = console.log, log = () => {},
|
|
93
|
+
onProgress = () => {}, deps = {} } = {}) {
|
|
94
|
+
const result = { flashed: 0, failed: 0, boards: 0 };
|
|
95
|
+
let b;
|
|
96
|
+
try {
|
|
97
|
+
b = (deps.bundle || bundle)();
|
|
98
|
+
} catch (e) {
|
|
99
|
+
say('прошивка в пакеті не пройшла перевірку: ' + e.message);
|
|
100
|
+
result.failed += 1;
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
const boards = await (deps.scan || scan)();
|
|
104
|
+
result.boards = boards.length;
|
|
105
|
+
for (const { usb, who, busy } of boards) {
|
|
106
|
+
if (busy) {
|
|
107
|
+
say(`${usb.path} зайнятий іншою програмою: прошивку на ньому не перевірив`);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
const base = { who, usb, bundle: b, marker: fw.readMarker(), refused: fw.refusedChip(usb), force };
|
|
111
|
+
let d = fw.decide({ ...base, confirmed: yes });
|
|
112
|
+
if (d.action === 'ask') {
|
|
113
|
+
const sure = ask ? await ask(`${usb.path}: плата ESP32 (303A:1001) мовчить на who. Це твій Desky, у якого `
|
|
114
|
+
+ 'перервалося оновлення прошивки? Прошити? [y/N] ') : false;
|
|
115
|
+
if (!sure) {
|
|
116
|
+
say(`${d.why}: не чіпаю. Якщо це Desky після перерваного оновлення: npx desky-agent firmware update --yes`);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
d = fw.decide({ ...base, confirmed: true });
|
|
120
|
+
}
|
|
121
|
+
if (d.action === 'none') {
|
|
122
|
+
if (!quiet) say(`${usb.path}: Desky, ${d.why}${who.sn ? ', номер ' + who.sn : ''}`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (d.action === 'warn' || d.action === 'skip') {
|
|
126
|
+
say(`${usb.path}: ${d.why}`);
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
say(`${usb.path}: ${d.why}. Оновлюю прошивку, не від'єднуй плату…`);
|
|
130
|
+
const r = await (deps.update || update)(usb, { bundle: b, from: who && who.fw, sn: who && who.sn, log, onProgress });
|
|
131
|
+
say(r.text);
|
|
132
|
+
if (r.ok) result.flashed += 1;
|
|
133
|
+
else if (!r.refused) result.failed += 1;
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
module.exports = { run, update, scan, probe, waitWho, bundle };
|
package/src/webserial.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// A Web Serial SerialPort made of node serialport, just enough for esptool-js's Transport:
|
|
3
|
+
// open/close, readable/writable web streams, setSignals, getInfo, setBaudRate.
|
|
4
|
+
//
|
|
5
|
+
// Two things differ from Web Serial and matter here:
|
|
6
|
+
// - serialport's set() fills every line it is not given with its default (DTR and RTS up), so setting RTS alone
|
|
7
|
+
// would raise DTR too. The adapter remembers both lines and always sends both.
|
|
8
|
+
// - readable and writable are recreated on access after a cancel or close, as the Web Serial spec says, while
|
|
9
|
+
// the port is open; esptool-js relies on that between its reads.
|
|
10
|
+
|
|
11
|
+
const { ReadableStream, WritableStream } = require('stream/web');
|
|
12
|
+
|
|
13
|
+
class NodeSerial {
|
|
14
|
+
/** path: the OS port; vendorId/productId: numbers, as Web Serial's getInfo() gives them (esptool-js picks the
|
|
15
|
+
* USB-Serial/JTAG reset by 303A:1001). SerialPort: the serialport class, replaceable in tests. */
|
|
16
|
+
constructor({ path, vendorId = 0x303a, productId = 0x1001, SerialPort } = {}) {
|
|
17
|
+
this.path = path;
|
|
18
|
+
this.vendorId = vendorId;
|
|
19
|
+
this.productId = productId;
|
|
20
|
+
this.SerialPort = SerialPort || require('serialport').SerialPort;
|
|
21
|
+
this.port = null;
|
|
22
|
+
this.lines = { dtr: false, rts: false, brk: false };
|
|
23
|
+
this._readable = null;
|
|
24
|
+
this._writable = null;
|
|
25
|
+
this._feed = null; // the data listener feeding the current readable
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getInfo() {
|
|
29
|
+
return { usbVendorId: this.vendorId, usbProductId: this.productId };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get opened() {
|
|
33
|
+
return Boolean(this.port && this.port.isOpen);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async open({ baudRate = 115200 } = {}) {
|
|
37
|
+
if (this.opened) throw new Error('port already open');
|
|
38
|
+
const port = new this.SerialPort({ path: this.path, baudRate, autoOpen: false, hupcl: false });
|
|
39
|
+
port.on('error', () => {}); // an unplug mid-flash surfaces as a failed read or write, not a crash
|
|
40
|
+
await new Promise((res, rej) => port.open(e => (e ? rej(e) : res())));
|
|
41
|
+
this.port = port;
|
|
42
|
+
port.on('close', () => this._ended());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async close() {
|
|
46
|
+
const port = this.port;
|
|
47
|
+
this._ended();
|
|
48
|
+
this.port = null;
|
|
49
|
+
if (port && port.isOpen) await new Promise(res => port.close(() => res()));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async setSignals({ dataTerminalReady, requestToSend, break: brk } = {}) {
|
|
53
|
+
if (!this.opened) throw new Error('port not open');
|
|
54
|
+
if (dataTerminalReady !== undefined) this.lines.dtr = Boolean(dataTerminalReady);
|
|
55
|
+
if (requestToSend !== undefined) this.lines.rts = Boolean(requestToSend);
|
|
56
|
+
if (brk !== undefined) this.lines.brk = Boolean(brk);
|
|
57
|
+
const { dtr, rts } = this.lines;
|
|
58
|
+
await new Promise((res, rej) => this.port.set({ dtr, rts, brk: this.lines.brk }, e => (e ? rej(e) : res())));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async setBaudRate(baudRate) {
|
|
62
|
+
if (!this.opened) throw new Error('port not open');
|
|
63
|
+
await new Promise((res, rej) => this.port.update({ baudRate }, e => (e ? rej(e) : res())));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get readable() {
|
|
67
|
+
if (!this.opened) return null;
|
|
68
|
+
if (!this._readable) this._readable = this._makeReadable();
|
|
69
|
+
return this._readable;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get writable() {
|
|
73
|
+
if (!this.opened) return null;
|
|
74
|
+
if (!this._writable) this._writable = this._makeWritable();
|
|
75
|
+
return this._writable;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
_makeReadable() {
|
|
79
|
+
const port = this.port;
|
|
80
|
+
let stream = null;
|
|
81
|
+
let feed = null;
|
|
82
|
+
const detach = () => {
|
|
83
|
+
if (feed) port.off('data', feed);
|
|
84
|
+
if (this._feed === feed) this._feed = null;
|
|
85
|
+
if (this._readable === stream) this._readable = null;
|
|
86
|
+
};
|
|
87
|
+
stream = new ReadableStream({
|
|
88
|
+
start: controller => {
|
|
89
|
+
feed = chunk => {
|
|
90
|
+
try { controller.enqueue(new Uint8Array(chunk)); } catch (e) { /* closed meanwhile */ }
|
|
91
|
+
};
|
|
92
|
+
feed.end = () => {
|
|
93
|
+
detach();
|
|
94
|
+
try { controller.close(); } catch (e) { /* already closed */ }
|
|
95
|
+
};
|
|
96
|
+
this._feed = feed;
|
|
97
|
+
port.on('data', feed);
|
|
98
|
+
},
|
|
99
|
+
cancel: () => detach(),
|
|
100
|
+
});
|
|
101
|
+
return stream;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
_makeWritable() {
|
|
105
|
+
const port = this.port;
|
|
106
|
+
let stream = null;
|
|
107
|
+
const forget = () => {
|
|
108
|
+
if (this._writable === stream) this._writable = null;
|
|
109
|
+
};
|
|
110
|
+
stream = new WritableStream({
|
|
111
|
+
write: chunk => new Promise((res, rej) => {
|
|
112
|
+
port.write(Buffer.from(chunk), e => {
|
|
113
|
+
if (e) return rej(e);
|
|
114
|
+
return port.drain(d => (d ? rej(d) : res()));
|
|
115
|
+
});
|
|
116
|
+
}),
|
|
117
|
+
close: () => forget(),
|
|
118
|
+
abort: () => forget(),
|
|
119
|
+
});
|
|
120
|
+
return stream;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_ended() {
|
|
124
|
+
if (this._feed && this._feed.end) this._feed.end();
|
|
125
|
+
this._readable = null;
|
|
126
|
+
this._writable = null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
module.exports = { NodeSerial };
|