-
Notifications
You must be signed in to change notification settings - Fork 15
/
rawmode.js
328 lines (307 loc) · 9.2 KB
/
rawmode.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
/*
* SPDX-FileCopyrightText: 2024 Volodymyr Shymanskyy
* SPDX-License-Identifier: MIT
*
* The software is provided "as is", without any warranties or guarantees (explicit or implied).
* This includes no assurances about being fit for any specific purpose.
*/
export class MpRawMode {
constructor(port) {
this.port = port
}
static async begin(port, soft_reboot=false) {
const res = new MpRawMode(port)
await res.enterRawRepl(soft_reboot)
try {
await res.exec(`import sys,os`)
} catch (err) {
await res.end()
throw err
}
return res
}
async enterRawRepl(soft_reboot=false) {
const release = await this.port.startTransaction()
try {
await this.port.write('\r\x03\x03') // Ctrl-C twice: interrupt any running program
await this.port.flushInput()
await this.port.write('\r\x01') // Ctrl-A: enter raw REPL
await this.port.readUntil('raw REPL; CTRL-B to exit\r\n')
if (soft_reboot) {
await this.port.write('\x04\x03') // soft reboot in raw mode
await this.port.readUntil('raw REPL; CTRL-B to exit\r\n')
}
this.end = async () => {
try {
await this.port.write('\x02') // Ctrl-B: exit raw REPL
await this.port.readUntil('>\r\n')
const banner = await this.port.readUntil('>>> ')
//term.clear()
//term.write(banner)
} finally {
release()
}
}
} catch (err) {
release()
//report("Cannot enter RAW mode", err)
throw err
}
}
async exec(cmd, timeout=5000, emit=false) {
await this.port.readUntil('>')
await this.port.write(cmd)
await this.port.write('\x04') // Ctrl-D: execute
const status = await this.port.readExactly(2)
if (status != 'OK') {
throw new Error(status)
}
this.port.emit = emit
if (emit) {
this.port.prevRecvCbk(this.port.receivedData)
}
const res = (await this.port.readUntil('\x04', timeout)).slice(0, -1)
const err = (await this.port.readUntil('\x04', timeout)).slice(0, -1)
if (err.length) {
throw new Error(err)
}
return res
}
async readFile(fn) {
const rsp = await this.exec(`
try:
import binascii
h=lambda x: binascii.hexlify(x).decode()
h(b'')
except:
h=lambda b: ''.join('{:02x}'.format(byte) for byte in b)
with open('${fn}','rb') as f:
while 1:
b=f.read(64)
if not b:break
print(h(b),end='')
`)
if (rsp.length) {
return new Uint8Array(rsp.match(/../g).map(h=>parseInt(h,16)))
} else {
return new Uint8Array()
}
}
async writeFile(fn, data, chunk_size=128, direct=false) {
console.log(`Writing ${fn}`)
if (typeof data === 'string' || data instanceof String) {
const encoder = new TextEncoder('utf-8')
data = new Uint8Array(Array.from(encoder.encode(data)))
}
function hexlify(data) {
return [...new Uint8Array(data)]
.map(x => x.toString(16).padStart(2, '0'))
.join('')
}
function repr(arr) {
arr = new Uint8Array(arr)
let result = "b'";
for (let byte of arr) {
if (byte >= 32 && byte <= 126) { // Printable ASCII range
if (byte === 92 || byte === 39) { // Escape backslash and single quote
result += '\\' + String.fromCharCode(byte);
} else {
result += String.fromCharCode(byte);
}
} else {
result += '\\x' + byte.toString(16).padStart(2, '0');
}
}
result += "'";
return result;
}
const dest = direct ? fn : '.viper.tmp'
await this.exec(`
try:
import binascii
h=binascii.unhexlify
h('')
except:
h=lambda s: bytes(int(s[i:i+2], 16) for i in range(0, len(s), 2))
f=open('${dest}','wb')
w=lambda d: f.write(h(d))
o=f.write
`)
// Split into chunks and send
for (let i = 0; i < data.byteLength; i += chunk_size) {
const chunk = data.slice(i, i + chunk_size)
const cmdHex = "w('" + hexlify(chunk) + "')"
const cmdRepr = "o(" + repr(chunk) + ")"
// Use the optimal command
if (cmdHex.length < cmdRepr.length) {
await this.exec(cmdHex)
} else {
await this.exec(cmdRepr)
}
}
if (direct) {
await this.exec(`f.close()`)
} else {
await this.exec(`f.close()
try: os.remove('${fn}')
except: pass
os.rename('${dest}','${fn}')
`)
}
}
async getDeviceInfo() {
const rsp = await this.exec(`
try: u=os.uname()
except: u=('','','','',sys.platform)
try: v=sys.version.split(';')[1].strip()
except: v='MicroPython '+u[2]
mpy=getattr(sys.implementation, '_mpy', 0)
sp=':'.join(sys.path)
d=[u[4],u[2],u[0],v,mpy>>10,mpy&0xFF,(mpy>>8)&3,sp]
print('|'.join(str(x) for x in d))
`)
let [machine, release, sysname, version, mpy_arch, mpy_ver, mpy_sub, sys_path] = rsp.trim().split('|')
sys_path = sys_path.split(':')
// See https://docs.micropython.org/en/latest/reference/mpyfiles.html
try {
mpy_arch = [null, 'x86', 'x64', 'armv6', 'armv6m', 'armv7m', 'armv7em', 'armv7emsp', 'armv7emdp', 'xtensa', 'xtensawin', 'rv32imc'][mpy_arch]
} catch (err) {
mpy_arch = null
}
mpy_ver = parseInt(mpy_ver, 10)
mpy_sub = parseInt(mpy_sub, 10)
if (!mpy_ver) { mpy_ver = 'py' }
return { machine, release, sysname, version, mpy_arch, mpy_ver, mpy_sub, sys_path }
}
async touchFile(fn) {
await this.exec(`
f=open('${fn}','wb')
f.close()
`)
}
async makePath(path) {
// TODO: remove error code 20 once it is fixed in wasm port
await this.exec(`
p=''
for d in filter(len,'${path}'.split('/')):
p += '/'+d
try: os.mkdir(p)
except OSError as e:
if e.args[0] not in (17, 20): raise
`)
}
async removeFile(path) {
await this.exec(`
try:
os.remove('${path}')
except OSError as e:
if e.args[0] == 39:
raise Exception('Directory not empty')
else:
raise
`)
}
async removeDir(path) {
await this.exec(`
try:
os.rmdir('${path}')
except OSError as e:
if e.args[0] == 39:
raise Exception('Directory not empty')
else:
raise
`)
}
async getFsStats(path='/') {
const rsp = await this.exec(`
s = os.statvfs('${path}')
fs = s[1] * s[2]
ff = s[3] * s[0]
fu = fs - ff
print('%s|%s|%s'%(fu,ff,fs))
`)
// fs_used, fs_free, fs_size
return rsp.trim().split('|')
}
async walkFs() {
const rsp = await this.exec(`
def walk(p):
for n in os.listdir(p if p else '/'):
fn=p+'/'+n
try: s=os.stat(fn)
except: s=(0,)*7
if s[0] & 0x4000 == 0:
print('f|'+fn+'|'+str(s[6]))
elif n not in ('.','..'):
print('d|'+fn+'|'+str(s[6]))
walk(fn)
walk('')
`)
let result = []
// Build file tree
for (const line of rsp.split('\n')) {
if (line === '') continue
let current = result
let [type, fullpath, size] = line.trim().split('|')
let path = fullpath.split('/')
let file
if (type == 'f') {
file = path.pop()
}
for (const segment of path) {
if (segment === '') continue
let next = current.filter(x => x.name === segment && "content" in x)
if (next.length) {
current = next[0].content
} else {
const prev = current
current = []
prev.push({ name: segment, path: path.join('/'), content: current })
}
}
if (type == 'f') {
current.push({ name: file, path: fullpath, size: parseInt(size, 10) })
}
}
return result
}
async readSysInfoMD() {
return await this.exec(`
import gc
gc.collect()
mu = gc.mem_alloc()
mf = gc.mem_free()
ms = mu + mf
uname=os.uname()
p=print
def size_fmt(size):
suffixes = ['B','KiB','MiB','GiB','TiB']
i = 0
while size > 1024 and i < len(suffixes)-1:
i += 1
size //= 1024
return "%d%s" % (size, suffixes[i])
p('## Machine')
p('- Name: \`'+uname.machine+'\`')
try:
gc.collect()
import microcontroller as uc
p('- CPU: \`%s @ %s MHz\`' % (sys.platform, uc.cpu.frequency // 1_000_000))
p('- UID: \`%s\`' % (uc.cpu.uid.hex(),))
p('- Temp.: \`%s °C\`' % (uc.cpu.temperature,))
p('- Voltage: \`%s V\`' % (uc.cpu.voltage,))
except:
try:
gc.collect()
import machine
p('- CPU: \`%s @ %s MHz\`' % (sys.platform, machine.freq() // 1_000_000))
except:
p('- CPU: \`'+sys.platform+'\`')
p()
p('## System')
p('- Version: \`'+sys.version.split(";")[1].strip()+'\`')
if ms:
p('- Memory use: \`%s / %s, free: %d%%\`' % (size_fmt(mu), size_fmt(ms), (mf * 100) // ms))
`)
}
}