-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzjs-cli.js
More file actions
executable file
·278 lines (240 loc) · 8.22 KB
/
zjs-cli.js
File metadata and controls
executable file
·278 lines (240 loc) · 8.22 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
#!/usr/bin/env node
// Copyright 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var device = require('./lib/usbDevice'),
program = require('commander'),
encoding = require('text-encoding'),
fs = require('fs'),
path = require('path'),
readline = require('readline'),
utils = require('./lib/cliUtils'),
transferUtils = require('./lib/transferUtils'),
data = null,
usbDevice = null,
baseName = null,
saveFile = false;
program
.version('0.0.1')
.description('JavaScript CLI for Zephyr OS')
.usage('-c -f <JavaScript file to upload>')
.option('-c, --connect', 'Connect to the WebUSB device')
.option('-d, --debug <level: 0 to 4>', 'Set the libusb debug level', parseInt)
.option('-l, --list', 'List all connected USB devices')
.option('-v, --vid <Vendor ID>', 'Vendor ID of the USB device', parseInt)
.option('-p, --pid <Product ID>', 'Product ID of the USB device', parseInt)
.option('-f, --file <JavaScript file>', 'JavaScript file to upload and execute')
.option('-s, --save <JavaScript file>', 'Save file to device')
.option('-w, --webusblist', 'List all connected WebUSB devices')
.parse(process.argv);
if (!process.argv.slice(2).length || program.h) {
program.help();
}
if (program.pid !== undefined && isNaN(program.pid)) {
console.log('Product ID must be a non-empty numeric value.');
process.exit();
}
if (program.vid !== undefined && isNaN(program.vid)) {
console.log('Vendor ID must be a non-empty numeric value.');
process.exit();
}
if (program.webusblist) {
let allDevices = device.getDevices();
console.log(utils.getWebUSBDevices(allDevices));
}
if (program.list) {
console.log(device.getDevices());
}
if (program.debug !== undefined) {
if (isNaN(program.debug) || program.debug < 0 || program.debug > 4) {
program.help();
} else {
device.setDebugLevel(program.debug);
}
}
if (program.file) {
program.connect = true;
data = fs.readFileSync(program.file, 'utf8');
}
if (program.save) {
baseName = path.basename(program.save);
if (!utils.isValidFilename(baseName)) {
console.log('Error: Filename should be in the 8.3 format');
process.exit();
}
program.connect = true;
saveFile = true;
data = fs.readFileSync(program.save, 'utf8');
}
if (program.connect) {
if (!program.vid || !program.pid) {
console.log('No VID or PID provided, so connecting to the first known WebUSB device in the configuration.');
program = utils.getFirstKnownDevice();
} else if (!utils.isWebUSBDevice(program.vid, program.pid)) {
console.log('No WebUSB device exist in the configuration for the given VID and PID');
process.exit(0);
}
device.findDevice(program.vid, program.pid).then((device) => {
usbDevice = device;
openDevice();
}).catch((error) => {
console.log('USB device error: ' + error);
});
}
function send(string) {
let buffer = new encoding.TextEncoder('utf-8').encode(string);
usbDevice.transfer(buffer).catch(function(error) {
console.log('Transfer failed. Error:', err);
});
}
function transfer(data) {
if (saveFile) {
saveToDevice(data);
return;
}
send('echo off\n');
send('set transfer ihex\n');
send('stop\n');
send('load\n');
let stripped = transferUtils.stripBlankLines(
transferUtils.stripComments(data));
let ihex = transferUtils.convIHex(stripped);
let line = ihex;
for (let line of ihex.split('\n')) {
send(line + '\n');
}
send('run temp.dat' + '\n');
send('set transfer raw\n');
send('echo on\n');
}
function saveToDevice(data) {
if (baseName.length === 0)
return;
send('echo off\n');
send('set transfer raw\n');
send('stop\n');
send('load ' + baseName + '\n');
for (let line of data.split('\n')) {
send(line + '\n');
}
send('\x1A\n');
send('echo on\n');
}
function openDevice() {
usbDevice.open().then(() => {
// Setup event handlers.
let printData = utils.parseData();
let rawMode = true;
let echoMode = true;
let previousRead;
// Setup event handlers.
usbDevice.on('data', (event) => {
let skip = true;
let skip_prompt = true;
let str = new encoding.TextDecoder('utf-8').decode(event.data);
if (str === 'raw') {
rawMode = true;
str = '';
} else if (str === 'ihex') {
rawMode = false;
str = '';
}
skip = !rawMode && /^(\n|\[.*\])/.test(str);
if (str === 'echo_off') {
echoMode = false;
str = '';
} else if (str === 'echo_on') {
echoMode = true;
str = '';
}
skip_prompt = !echoMode && /^(\r|\n|\x1b\[33macm)/.test(str);
if (!skip && !skip_prompt) {
if (str.length === 1 &&
str.charCodeAt(0) !== 13 &&
str.charCodeAt(0) !== 10 &&
previousRead !== undefined &&
previousRead.charCodeAt(
previousRead.length - 1) === 13) {
str = '\r\n' + str;
}
printData(str);
}
if (!skip)
previousRead = str;
});
usbDevice.on('error', (event) => {
console.log('Error on ' + event.type + ': ' + event.error);
process.exit();
});
usbDevice.on('detached', (event) => {
console.log('Detached device');
process.exit();
});
var webInterface = utils.getWebUSBInterface();
return usbDevice.claimInterface(webInterface ? webInterface : 2);
}).then(() => {
return usbDevice.listen();
}).then(() => {
return usbDevice.controlTransferOut(0x22, 0x01, 0x02);
}).then((res) => {
// Wait 2 sec for the device to do all settings
if (data || saveFile) {
setTimeout(() => transfer(data), 2000);
} else {
process.stdout.write('\u001b[33macm> \u001b[39;0m');
}
initReadableStream();
}).catch((error) => {
console.log('USB Error: ' + error);
exitHandler();
});
}
function initReadableStream() {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', (input) => {
if (!input) {
process.stdout.write('\u001b[33macm> \u001b[39;0m');
} else if (input.includes('load') || input.includes('eval')) {
console.log("'load' and 'eval' commands are unsupported in CLI mode");
// print 'acm>' prompt.
process.stdout.write('\u001b[33macm> \u001b[39;0m');
} else if (input === 'quit' || input === 'exit') {
exitHandler();
} else {
send(input + '\n');
}
});
rl.on('SIGINT', () => {
exitHandler();
});
}
// Cleanup when interrupted
function exitHandler() {
if (usbDevice) {
usbDevice.close().then(() => {
console.log('Device closed');
process.exit();
}).catch((error) => {
console.log('Failed to close the USB device!');
process.exit();
});
} else {
process.exit();
}
}
// Press Ctrl+C to exit the process
process.on('SIGINT', exitHandler);