/
coro_cli.py
328 lines (271 loc) · 8.17 KB
/
coro_cli.py
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
from ubinascii import hexlify, unhexlify
from async_pyb import new_event_loop, set_event_loop, \
Future, TimeoutError, \
coroutine, sleep, wait_for, GetRunningLoop
import pyb
import gc
from pyb import USB_VCP, SPI, millis, elapsed_millis
class GotEOT(Exception):
pass
class CoroCLI():
def __init__(self, role):
self.role = role
self.console = USB_VCP()
self.command_dispatch = { }
self.cmd_hist = [b'']
self.prompt = bytes(self.role + '> ', 'ASCII')
self.tx_want_ack = True
self.destination = bytes(8)
@coroutine
def readline(self):
console = self.console
hi = 0
buf = b''
while console.isconnected():
if console.any():
c = console.read(1)
if c == b'\x04':
raise GotEOT
elif c == b'\x0e': # ^N
buf = self.cmd_hist[hi][:]
hi = max(hi-1, 0)
console.write(b'\r' + self.prompt + buf + b'\x1b[K')
elif c == b'\x10': # ^P
buf = self.cmd_hist[hi][:]
hi = min(hi+1, len(self.cmd_hist)-1)
console.write(b'\r' + self.prompt + buf + b'\x1b[K')
elif c == b'\x7f' or c == b'\x08':
if buf:
console.write(b'\x08 \x08')
buf = buf[:-1]
elif c == b'\r' or c == b'\n':
if buf and buf != self.cmd_hist[0]:
self.cmd_hist.insert(0, buf)
if len(self.cmd_hist) > 16:
self.cmd_hist.pop()
hi = 0
console.write(b'\r\n')
return buf
else:
buf += c
console.write(c)
else:
yield from sleep(50)
@coroutine
def write(self, buf):
self.console.write(buf)
yield
@coroutine
def writeln(self, buf):
yield from self.write(buf)
yield from self.write(b'\r\n')
@coroutine
def interpret(self, line):
if not line:
return
cmd, _, rol = line.lstrip().partition(b' ')
cmd = str(cmd, 'ASCII')
rol = rol.lstrip()
fun = self.command_dispatch.get(cmd.lower())
if fun:
yield from fun(self, cmd, rol)
elif cmd in ('help', '?'):
yield from self.write('Commands are: ')
yield from self.writeln(', '.join(sorted(self.command_dispatch.keys())))
else:
yield from self.writeln('huh?')
@coroutine
def repl(self, noquit=False):
console = self.console
while(console.isconnected()):
#console.self.write(prompt)
yield from self.write(self.prompt)
try:
line = yield from self.readline()
yield from self.interpret(line)
except GotEOT:
if noquit:
yield from self.writeln("can't quit!")
else:
return
################################################################
@coroutine
def info_cmd(cli, cmd, rol):
yield from cli.write('mem_alloc %s, mem_free %s' \
% (gc.mem_alloc(), gc.mem_free()))
loop = yield GetRunningLoop(None)
yield from cli.write(', qlen %d' % len(loop.q))
yield from cli.writeln('')
'''
@coroutine
def at_cmd(cli, cmd, rol):
print(repr(rol))
try:
v = yield from cli.xb.AT_cmd(rol[:2], rol[2:])
except ATStatusError as e:
yield from cli.writeln(e.args[0])
else:
yield from cli.writeln(repr(v))
@coroutine
def echo_cmd(cli, cmd, rol):
args = rol.split()
if args and args[0] == b'-q':
quiet = True
args.pop(0)
else:
quiet = False
try:
n = int(args[0])
except:
n = 1<<30
yield from cli.echoServer(n, quiet=quiet)
@coroutine
def dest_cmd(cli, cmd, rol):
# Set a default destination address
if len(rol):
cli.destination = unhexlify(rol)
else:
yield from cli.writeln(hexlify(cli.destination))
'''
@coroutine
def coro_cmd(cli, cmd, rol):
# launch a command in the background, as a coro
s1, _, srem = rol.partition(b' ')
try:
n = int(s1)
c_cmd, _, c_rol = srem.partition(b' ')
except:
n = 1
c_cmd, _, c_rol = rol.partition(b' ')
c_cmd = str(c_cmd, 'ASCII')
c_rol = c_rol.lstrip()
fun = cli.command_dispatch.get(c_cmd.lower())
#print(cli, c_cmd, c_rol)
if fun:
for i in range(n):
yield fun(cli, c_cmd, c_rol)
else:
yield from cli.writeln("coro can't find %s" % c_cmd)
'''
@coroutine
def ping_cmd(cli, cmd, rol):
args = rol.split()
if args and args[0] == b'-q':
quiet = True
args.pop(0)
else:
quiet = False
try:
n = int(args[0])
except:
n = 1
try:
address = unhexlify(args[1])
except:
address = cli.destination
loop = yield GetRunningLoop(None)
t0 = millis()
for i in range(n):
payload = b'%d: ping at %.3f' % (i+1, loop.time())
status = yield from wait_for((yield from cli.xb.tx(payload, address, ack=cli.tx_want_ack)))
if not quiet:
yield from cli.write('-')
try:
a, d = yield from wait_for(cli.xb.rx(), 1)
except TimeoutError:
yield from cli.write('X')
else:
if not quiet:
yield from cli.write('|')
duration = elapsed_millis(t0)
yield from cli.writeln('')
yield from cli.writeln("%d in %dms" % (n, duration))
@coroutine
def rx_cmd(cli, cmd, rol):
args = rol.split()
try:
n = int(args[0])
except:
n = 1
try:
timeout = float(str(args[1], 'ASCII'))
except:
timeout = None
print(n, timeout)
for i in range(n):
try:
a, d = yield from wait_for(cli.xb.rx(), timeout)
except TimeoutError:
yield from cli.writeln("timeout")
else:
yield from cli.writeln('from %s got %r' % \
(str(hexlify(a), 'ASCII'), d))
@coroutine
def tx_cmd(cli, cmd, rol):
addr, _, payload = rol.partition(b':')
if len(addr) == 16:
address = unhexlify(addr)
else:
address = cli.destination
yield from cli.write('sending %d bytes to %r...' % \
(len(payload), address))
status = yield from wait_for((yield from cli.xb.tx(payload, address, ack=cli.tx_want_ack)))
yield from cli.writeln('status %r' % status)
@coroutine
def ack_cmd(cli, cmd, rol):
if len(rol):
cli.tx_want_ack = rol == b'on'
else:
yield from cli.writeln(cli.tx_want_ack and 'on' or 'off')
'''
@coroutine
def gc_cmd(cli, cmd, rol):
before = gc.mem_alloc(), gc.mem_free()
gc.collect()
after = gc.mem_alloc(), gc.mem_free()
yield from cli.writeln('mem_alloc %d, mem_free %d (up %d)' \
% (after[0], after[1], after[1]-before[1]))
@coroutine
def eval_cmd(cli, cmd, rol):
d = {'cli': cli,
'loop': (yield GetRunningLoop(None)),
'pyb': pyb }
try:
v = eval(rol, d)
except Exception as e:
v = e
yield from cli.writeln(repr(v))
@coroutine
def exec_cmd(cli, cmd, rol):
d = {'cli': cli,
'loop': (yield GetRunningLoop(None)),
'pyb': pyb }
try:
v = exec(rol, d)
except Exception as e:
v = e
yield from cli.writeln(repr(v))
@coroutine
def quit_cmd(cli, cmd, rol):
raise GotEOT
yield
@coroutine
def heyUsaid_cmd(cli, cmd, rol):
yield from cli.writeln("You said %s %s" % (cmd, rol))
def inject_standard_commands(cli):
cli.command_dispatch.update({
'coro': coro_cmd,
'eval': eval_cmd,
'exec': exec_cmd,
'gc': gc_cmd,
'info': info_cmd,
'hey': heyUsaid_cmd,
'quit': quit_cmd })
def interact(role=None):
cli = CoroCLI('pre-alpha')
inject_standard_commands(cli)
loop = new_event_loop()
set_event_loop(loop)
loop.run_until_complete(cli.repl())
if __name__ == '__main__':
interact()