2020-08-10 13:56:44 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
2020-02-17 20:09:49 +01:00
|
|
|
# Copyright (c) 2020 Daniel Thompson
|
|
|
|
|
|
|
|
import argparse
|
2020-04-20 20:09:33 +02:00
|
|
|
import io
|
2020-02-17 20:09:49 +01:00
|
|
|
import random
|
|
|
|
import os.path
|
|
|
|
import pexpect
|
|
|
|
import time
|
|
|
|
import string
|
|
|
|
import sys
|
|
|
|
|
2020-04-06 22:40:34 +02:00
|
|
|
def pbar(iterable, quiet=False):
|
|
|
|
step = 100 / len(iterable)
|
|
|
|
for i, v in enumerate(iterable):
|
|
|
|
if not quiet:
|
|
|
|
percent = round(step * i, 1)
|
|
|
|
bar = int(percent) // 2
|
2020-04-10 21:31:26 +02:00
|
|
|
print(f'[{"#"*bar}{"."*(50-bar)}] {percent}%', end='\r', flush=True)
|
2020-04-06 22:40:34 +02:00
|
|
|
yield v
|
|
|
|
if not quiet:
|
2020-04-10 21:31:26 +02:00
|
|
|
print(f'[{"#"*50}] 100% ')
|
2020-04-06 22:40:34 +02:00
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
def sync(c):
|
2020-03-22 13:37:19 +01:00
|
|
|
"""Stop the watch and synchronize with the command prompt.
|
|
|
|
|
|
|
|
Sending a random print ensure the final export (of the prompt)
|
|
|
|
does not accidentally match a previously issued prompt.
|
|
|
|
"""
|
2020-10-21 21:49:48 +02:00
|
|
|
verbose = bool(c.logfile)
|
2020-02-17 20:09:49 +01:00
|
|
|
tag = ''.join([random.choice(string.ascii_uppercase) for i in range(6)])
|
|
|
|
|
2020-10-21 21:49:48 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not verbose:
|
|
|
|
c.logfile = io.StringIO()
|
|
|
|
|
|
|
|
c.send('\x03')
|
|
|
|
c.expect('>>> ')
|
|
|
|
c.sendline(f'print("{tag[:3]}""{tag[3:]}")')
|
|
|
|
c.expect(tag)
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
|
|
|
if not verbose:
|
|
|
|
c.logfile.close()
|
|
|
|
c.logfile = None
|
|
|
|
except pexpect.exceptions.EOF:
|
|
|
|
print("ERROR: Cannot sync with device")
|
|
|
|
print_log(c.logfile)
|
|
|
|
sys.exit(1)
|
2020-02-17 20:09:49 +01:00
|
|
|
|
|
|
|
def unsync(c):
|
2020-03-22 13:37:19 +01:00
|
|
|
"""Set the watch running again.
|
|
|
|
|
|
|
|
There must be an expect (or a sleep) since if we kill the subordinate
|
|
|
|
process too early then the sendline will not have completed.
|
|
|
|
"""
|
|
|
|
c.sendline('wasp.system.run()')
|
2020-07-01 00:04:01 +02:00
|
|
|
c.expect(['Watch is running, use Ctrl-C to stop',
|
|
|
|
'Watch already running in the background'])
|
2020-03-22 13:37:19 +01:00
|
|
|
c.send('\x18')
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-04-17 18:18:27 +02:00
|
|
|
def paste(c, f, verbose=False, chunk=None):
|
2020-03-09 22:34:01 +01:00
|
|
|
docstring = False
|
2020-04-06 22:40:34 +02:00
|
|
|
|
|
|
|
tosend = []
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
for ln in f.readlines():
|
|
|
|
ln = ln.rstrip()
|
2020-03-09 22:34:01 +01:00
|
|
|
|
|
|
|
# This is a bit loose (definitely not PEP-257 compliant) but
|
|
|
|
# is enough for most code.
|
|
|
|
if ln.lstrip().startswith('"""'):
|
|
|
|
docstring = True
|
|
|
|
if docstring:
|
|
|
|
if ln.rstrip().endswith('"""'):
|
|
|
|
docstring = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
if ln.lstrip().startswith('#'):
|
|
|
|
continue
|
|
|
|
|
2020-04-17 18:18:27 +02:00
|
|
|
if ln.strip() == '':
|
|
|
|
continue
|
|
|
|
|
2020-04-06 22:40:34 +02:00
|
|
|
tosend.append(ln)
|
2020-03-09 22:34:01 +01:00
|
|
|
|
2020-04-06 22:40:34 +02:00
|
|
|
for ln in pbar(tosend, verbose):
|
2020-04-17 18:18:27 +02:00
|
|
|
if chunk and ln.startswith('class'):
|
|
|
|
chunk()
|
|
|
|
|
|
|
|
c.sendline(ln)
|
2020-05-25 10:23:58 +02:00
|
|
|
|
|
|
|
choice = c.expect(['=== ', 'FATAL: uncaught exception [0-9a-f\r]*\n'])
|
|
|
|
if choice == 1:
|
|
|
|
# Capture and display the error message, then exit
|
|
|
|
if not verbose:
|
|
|
|
print('\n~~~')
|
|
|
|
while choice == 1:
|
|
|
|
if not verbose:
|
|
|
|
print(c.match.group(0).rstrip(), file=sys.stderr)
|
|
|
|
choice = c.expect([pexpect.TIMEOUT, '.*\n'], timeout=2)
|
|
|
|
if not verbose:
|
|
|
|
print('~~~')
|
|
|
|
print('\nPlease reboot your device', file=sys.stderr)
|
|
|
|
sys.exit(16)
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-04-20 20:09:33 +02:00
|
|
|
def print_log(logfile):
|
|
|
|
lines = logfile.getvalue().split('\n')
|
|
|
|
lines = [ l.strip('\x04\r') for l in lines ]
|
|
|
|
|
|
|
|
output = [ l for l in lines if l and l != '>>> ' ]
|
|
|
|
if output:
|
|
|
|
print('~~~')
|
|
|
|
print('\n'.join(output))
|
|
|
|
print('~~~')
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
def handle_eval(c, cmd):
|
|
|
|
verbose = bool(c.logfile)
|
|
|
|
|
|
|
|
c.send('\x05')
|
|
|
|
c.expect('=== ')
|
|
|
|
c.sendline(cmd)
|
|
|
|
c.expect('=== ')
|
|
|
|
|
2020-04-20 20:09:33 +02:00
|
|
|
if not verbose:
|
|
|
|
c.logfile = io.StringIO()
|
2020-02-17 20:09:49 +01:00
|
|
|
c.send('\x04')
|
|
|
|
c.expect('>>> ')
|
|
|
|
if not verbose:
|
2020-04-20 20:09:33 +02:00
|
|
|
print_log(c.logfile)
|
|
|
|
c.logfile.close()
|
2020-02-17 20:09:49 +01:00
|
|
|
c.logfile = None
|
|
|
|
|
|
|
|
def handle_exec(c, fname):
|
|
|
|
verbose = bool(c.logfile)
|
|
|
|
|
2020-04-20 20:09:33 +02:00
|
|
|
log = io.StringIO()
|
|
|
|
|
2020-04-17 18:18:27 +02:00
|
|
|
def chunk():
|
2020-04-20 20:09:33 +02:00
|
|
|
if not verbose:
|
|
|
|
c.logfile = log
|
2020-04-17 18:18:27 +02:00
|
|
|
c.send('\x04')
|
|
|
|
c.expect('>>> ')
|
|
|
|
if not verbose:
|
|
|
|
c.logfile = None
|
|
|
|
c.send('\x05')
|
|
|
|
c.expect('=== ')
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
with open(fname) as f:
|
|
|
|
if not verbose:
|
2020-04-06 22:40:34 +02:00
|
|
|
print(f'Preparing to run {fname}:')
|
2020-02-17 20:09:49 +01:00
|
|
|
|
|
|
|
c.send('\x05')
|
|
|
|
c.expect('=== ')
|
|
|
|
|
2020-04-17 18:18:27 +02:00
|
|
|
paste(c, f, verbose, chunk)
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-04-20 20:09:33 +02:00
|
|
|
if not verbose:
|
|
|
|
c.logfile = log
|
2020-02-17 20:09:49 +01:00
|
|
|
c.send('\x04')
|
|
|
|
c.expect('>>> ')
|
|
|
|
if not verbose:
|
|
|
|
c.logfile = None
|
|
|
|
|
2020-04-20 20:09:33 +02:00
|
|
|
print_log(log)
|
|
|
|
log.close()
|
|
|
|
|
2020-06-20 21:15:36 +02:00
|
|
|
def handle_reset(c, ota=False):
|
2020-05-24 15:20:50 +02:00
|
|
|
cmd = 'reset'
|
|
|
|
if ota:
|
|
|
|
cmd = 'enter_ota_dfu'
|
|
|
|
|
2020-04-17 18:17:24 +02:00
|
|
|
c.send('\x05')
|
|
|
|
c.expect('=== ')
|
|
|
|
c.sendline('import machine')
|
2020-05-24 15:20:50 +02:00
|
|
|
c.expect('=== ')
|
|
|
|
c.sendline(f'machine.{cmd}()')
|
2020-04-17 18:17:24 +02:00
|
|
|
c.expect('=== ')
|
|
|
|
c.send('\x04')
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
def handle_rtc(c):
|
|
|
|
# Wait for the clock to tick over to the next second
|
|
|
|
now = then = time.localtime()
|
|
|
|
while now[5] == then[5]:
|
|
|
|
now = time.localtime()
|
|
|
|
|
|
|
|
# Set the time
|
2020-02-19 20:32:06 +01:00
|
|
|
c.sendline(f'watch.rtc.set_localtime(({now[0]}, {now[1]}, {now[2]}, {now[3]}, {now[4]}, {now[5]}, {now[6]}, {now[7]}))')
|
2020-02-17 20:09:49 +01:00
|
|
|
c.expect('>>> ')
|
|
|
|
|
2020-05-09 15:21:39 +02:00
|
|
|
def check_rtc(c):
|
|
|
|
c.sendline('print(watch.rtc.get_localtime())')
|
|
|
|
c.expect('\(([0-9]+), ([0-9]+), ([0-9]+), ([0-9]+), ([0-9]+), ([0-9]+), ([0-9]+), ([0-9]+)\)')
|
|
|
|
t = time.localtime()
|
|
|
|
|
|
|
|
watch_hms = (int(c.match[4]), int(c.match[5]), int(c.match[6]))
|
|
|
|
watch_str = f'{watch_hms[0]:02d}:{watch_hms[1]:02d}:{watch_hms[2]:02d}'
|
|
|
|
host_hms = (t.tm_hour, t.tm_min, t.tm_sec)
|
|
|
|
host_str = f'{host_hms[0]:02d}:{host_hms[1]:02d}:{host_hms[2]:02d}'
|
|
|
|
delta = 3600 * (host_hms[0] - watch_hms[0]) + \
|
|
|
|
60 * (host_hms[1] - watch_hms[1]) + \
|
|
|
|
(host_hms[2] - watch_hms[2])
|
|
|
|
print(f"PC <-> watch: {watch_str} <-> {host_str} (delta {delta})")
|
|
|
|
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
2020-06-20 21:16:25 +02:00
|
|
|
def handle_binary_upload(c, fname):
|
|
|
|
verbose = bool(c.logfile)
|
|
|
|
|
|
|
|
c.sendline(f'f = open("{os.path.basename(fname)}", "wb")')
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
|
|
|
# Absorb the file to be uploaded
|
|
|
|
with open(fname, 'rb') as f:
|
|
|
|
data = f.read()
|
|
|
|
chunksz = 24
|
|
|
|
nchunks = len(data) // chunksz
|
|
|
|
lastchunk = len(data) % chunksz
|
|
|
|
|
|
|
|
if not verbose:
|
|
|
|
print(f'Uploading {fname}:')
|
|
|
|
|
|
|
|
# Send the data
|
|
|
|
for i in pbar(range(0, chunksz*nchunks, chunksz), verbose):
|
|
|
|
c.sendline(f'f.write({repr(data[i:i+chunksz])})')
|
|
|
|
c.expect('>>> ')
|
|
|
|
if lastchunk:
|
|
|
|
c.sendline(f'f.write({repr(data[-lastchunk:])})')
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
|
|
|
c.sendline('f.close()')
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
def handle_upload(c, fname):
|
|
|
|
verbose = bool(c.logfile)
|
|
|
|
|
2020-02-19 20:33:35 +01:00
|
|
|
c.sendline('from shell import upload')
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
with open(fname) as f:
|
|
|
|
if not verbose:
|
2020-04-06 22:40:34 +02:00
|
|
|
print(f'Uploading {fname}:')
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-02-19 20:33:35 +01:00
|
|
|
c.sendline(f'upload("{os.path.basename(fname)}")')
|
2020-02-17 20:09:49 +01:00
|
|
|
c.expect('=== ')
|
|
|
|
paste(c, f, verbose)
|
|
|
|
c.send('\x04')
|
|
|
|
|
|
|
|
c.expect('>>> ')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
2020-05-14 23:41:05 +02:00
|
|
|
description='Wasp-os command and control client')
|
2020-05-24 15:20:50 +02:00
|
|
|
parser.add_argument('--bootloader', action='store_true',
|
|
|
|
help="Reboot into the bootloader mode for OTA update")
|
2020-06-20 21:16:25 +02:00
|
|
|
parser.add_argument('--binary', action='store_true',
|
|
|
|
help="Enable non-ASCII mode for suitable commands (such as upload)")
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--console', action='store_true',
|
|
|
|
help='Launch a REPL session')
|
2020-05-09 15:21:39 +02:00
|
|
|
parser.add_argument('--check-rtc', action='store_true',
|
|
|
|
help='Compare workstation and watch times')
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--device',
|
2020-07-26 22:04:30 +02:00
|
|
|
help='Connect only to a specific named device (or MAC address)')
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--exec',
|
|
|
|
help='Execute the contents of a file')
|
|
|
|
parser.add_argument('--eval',
|
|
|
|
help='Execute the provided python string')
|
2020-04-17 18:17:24 +02:00
|
|
|
parser.add_argument('--reset', action='store_true',
|
|
|
|
help="Reboot the device (and don't stay in bootloader mode)")
|
2020-05-24 15:20:50 +02:00
|
|
|
parser.add_argument('--ota',
|
|
|
|
help="Deliver an OTA update to the device")
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--rtc', action='store_true',
|
2020-05-14 23:41:05 +02:00
|
|
|
help='Set the time on the wasp-os device')
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--upload',
|
2020-05-14 23:41:05 +02:00
|
|
|
help='Copy the specified file to the wasp-os device')
|
2020-02-17 20:09:49 +01:00
|
|
|
parser.add_argument('--verbose', action='store_true',
|
2020-05-14 23:41:05 +02:00
|
|
|
help='Log interaction with the wasp-os device')
|
2020-02-17 20:09:49 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-07-26 22:04:30 +02:00
|
|
|
if args.device:
|
|
|
|
if ':' in args.device:
|
|
|
|
device_args = ' --address ' + args.device
|
|
|
|
else:
|
|
|
|
device_args = ' --device ' + args.device
|
|
|
|
else:
|
|
|
|
device_args = ''
|
|
|
|
|
|
|
|
pynus = os.path.dirname(sys.argv[0]) + '/pynus/pynus.py' + device_args
|
2020-02-17 20:09:49 +01:00
|
|
|
console = pexpect.spawn(pynus, encoding='UTF-8')
|
|
|
|
if args.verbose:
|
|
|
|
console.logfile = sys.stdout
|
2020-10-10 22:06:32 +02:00
|
|
|
else:
|
|
|
|
console.logfile = io.StringIO()
|
|
|
|
|
|
|
|
try:
|
|
|
|
console.expect('Connect.*\(([0-9A-F:]*)\)')
|
|
|
|
except pexpect.exceptions.TIMEOUT:
|
|
|
|
print('ERROR: Cannot find suitable wasp-os device')
|
|
|
|
if not args.verbose:
|
|
|
|
print_log(console.logfile)
|
|
|
|
sys.exit(1)
|
2020-10-21 21:48:12 +02:00
|
|
|
if not args.verbose:
|
|
|
|
console.logfile.close()
|
|
|
|
console.logfile = None
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-05-24 15:20:50 +02:00
|
|
|
macaddr = console.match.group(1)
|
2020-02-17 20:09:49 +01:00
|
|
|
console.expect('Exit console using Ctrl-X')
|
|
|
|
time.sleep(0.5)
|
|
|
|
sync(console)
|
|
|
|
|
2020-03-08 11:16:49 +01:00
|
|
|
if args.rtc:
|
|
|
|
handle_rtc(console)
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-05-09 15:21:39 +02:00
|
|
|
if args.check_rtc:
|
|
|
|
check_rtc(console)
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
if args.exec:
|
|
|
|
handle_exec(console, args.exec)
|
|
|
|
|
2020-03-08 11:16:49 +01:00
|
|
|
if args.eval:
|
|
|
|
handle_eval(console, args.eval)
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
if args.upload:
|
2020-06-20 21:16:25 +02:00
|
|
|
if args.binary:
|
|
|
|
handle_binary_upload(console, args.upload)
|
|
|
|
else:
|
|
|
|
handle_upload(console, args.upload)
|
2020-02-17 20:09:49 +01:00
|
|
|
|
2020-02-19 20:35:49 +01:00
|
|
|
if args.console:
|
|
|
|
console.close()
|
2020-07-26 22:04:30 +02:00
|
|
|
argv = pynus.split()
|
|
|
|
os.execv(argv[0], argv)
|
2020-02-19 20:35:49 +01:00
|
|
|
|
2020-05-24 15:20:50 +02:00
|
|
|
if args.ota:
|
|
|
|
handle_reset(console, ota=True)
|
2020-10-10 22:02:56 +02:00
|
|
|
time.sleep(1.0)
|
2020-05-24 15:20:50 +02:00
|
|
|
dfu = os.path.dirname(sys.argv[0]) + '/ota-dfu/dfu.py'
|
|
|
|
os.execl(dfu, dfu, '-z', args.ota, '-a', macaddr, '--legacy')
|
|
|
|
|
2020-04-17 18:17:24 +02:00
|
|
|
if args.reset:
|
|
|
|
handle_reset(console)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-05-24 15:20:50 +02:00
|
|
|
if args.bootloader:
|
|
|
|
handle_reset(console, ota=True)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-02-17 20:09:49 +01:00
|
|
|
unsync(console)
|