summaryrefslogtreecommitdiffstats
path: root/scripts/remote/main.py
diff options
context:
space:
mode:
authorJan Luebbe <jlu@pengutronix.de>2015-06-05 09:18:56 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2016-01-18 09:25:50 +0100
commitbe6c6e653683e86d4c7aeb2b67c62ec0895befa3 (patch)
tree16de4d407994470101aed97834a4b6249f4952cd /scripts/remote/main.py
parent06fc3557c94c2b9d43bcfab72f4a024c7860be64 (diff)
downloadbarebox-be6c6e653683e86d4c7aeb2b67c62ec0895befa3.tar.gz
barebox-be6c6e653683e86d4c7aeb2b67c62ec0895befa3.tar.xz
host side for barebox remote control
This contains the host tool for barebox remote control. It is written in Phython with its own implementation of the RATP protocol. Currently this is a very simple tool which needs more work, but the code can also be used as a library. Example output: console: '. ' console: '.. ' console: 'dev ' console: 'env ' console: 'mnt ' console: '\n' Result: BBPacketCommandReturn(exit_code=0) Signed-off-by: Jan Lübbe <j.luebbe@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Tested-by: Andrey Smirnov <andrew.smirnov@gmail.com>
Diffstat (limited to 'scripts/remote/main.py')
-rw-r--r--scripts/remote/main.py168
1 files changed, 168 insertions, 0 deletions
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
new file mode 100644
index 0000000000..bd304723bb
--- /dev/null
+++ b/scripts/remote/main.py
@@ -0,0 +1,168 @@
+#!/usr/bin/env python2
+
+from __future__ import absolute_import, division, print_function
+
+import sys
+import os
+import argparse
+import logging
+from Queue import Queue
+from .ratp import RatpError
+
+try:
+ import serial
+except:
+ print("error: No python-serial package found", file=sys.stderr)
+ exit(2)
+
+
+def versiontuple(v):
+ return tuple(map(int, (v.split("."))))
+
+if versiontuple(serial.VERSION) < (2, 7):
+ print("warning: python-serial package is buggy in RFC2217 mode,",
+ "consider updating to at least 2.7", file=sys.stderr)
+
+from .ratp import SerialRatpConnection
+from .controller import Controller
+from .threadstdio import ConsoleInput
+
+
+def get_controller(args):
+ port = serial.serial_for_url(args.port, args.baudrate)
+ conn = SerialRatpConnection(port)
+
+ while True:
+ try:
+ ctrl = Controller(conn)
+ break
+ except (RatpError):
+ if args.wait == True:
+ pass
+ else:
+ raise
+
+ return ctrl
+
+
+def handle_run(args):
+ ctrl = get_controller(args)
+ ctrl.export(args.export)
+ res = ctrl.command(' '.join(args.arg))
+ if res:
+ res = 1
+ ctrl.close()
+ return res
+
+
+def handle_ping(args):
+ ctrl = get_controller(args)
+ res = ctrl.ping()
+ if res:
+ res = 1
+ ctrl.close()
+ return res
+
+
+def handle_getenv(args):
+ ctrl = get_controller(args)
+ value = ctrl.getenv(' '.join(args.arg))
+ if not value:
+ res = 1
+ else:
+ print(value)
+ res = 0
+ ctrl.close()
+ return res
+
+
+def handle_listen(args):
+ port = serial.serial_for_url(args.port, args.baudrate)
+ conn = SerialRatpConnection(port)
+ conn.listen()
+ while True:
+ conn.wait(None)
+ conn.close()
+
+
+def handle_console(args):
+ queue = Queue()
+ ctrl = get_controller(args)
+ ctrl.export(args.export)
+ ctrl.start(queue)
+ ctrl.send_async_console('\r')
+ cons = ConsoleInput(queue, exit='\x14') # CTRL-T
+ cons.start()
+ try:
+ while True:
+ src, data = queue.get(block=True)
+ if src == cons:
+ if data is None: # shutdown
+ cons.join()
+ break
+ elif data == '\x10': # CTRL-P
+ ctrl.send_async_ping()
+ else:
+ ctrl.send_async_console(data)
+ elif src == ctrl:
+ if data is None: # shutdown
+ sys.exit(1)
+ break
+ else:
+ os.write(sys.stdout.fileno(), data)
+ ctrl.stop()
+ ctrl.close()
+ finally:
+ print()
+ print("total retransmits=%i crc-errors=%i" % (
+ ctrl.conn.total_retransmits,
+ ctrl.conn.total_crc_errors))
+
+VERBOSITY = {
+ 0: logging.WARN,
+ 1: logging.INFO,
+ 2: logging.DEBUG,
+ }
+
+parser = argparse.ArgumentParser(prog='bbremote')
+parser.add_argument('-v', '--verbose', action='count', default=0)
+parser.add_argument('--port', type=str, default=os.environ.get('BBREMOTE_PORT', None))
+parser.add_argument('--baudrate', type=int, default=os.environ.get('BBREMOTE_BAUDRATE', 115200))
+parser.add_argument('--export', type=str, default=os.environ.get('BBREMOTE_EXPORT', None))
+parser.add_argument('-w', '--wait', action='count', default=0)
+subparsers = parser.add_subparsers(help='sub-command help')
+
+parser_run = subparsers.add_parser('run', help="run a barebox command")
+parser_run.add_argument('arg', nargs='+', help="barebox command to run")
+parser_run.set_defaults(func=handle_run)
+
+parser_ping = subparsers.add_parser('ping', help="test connection")
+parser_ping.set_defaults(func=handle_ping)
+
+parser_ping = subparsers.add_parser('getenv', help="get a barebox environment variable")
+parser_ping.add_argument('arg', nargs='+', help="variable name")
+parser_ping.set_defaults(func=handle_getenv)
+
+parser_run = subparsers.add_parser('listen', help="listen for an incoming connection")
+parser_run.set_defaults(func=handle_listen)
+
+parser_run = subparsers.add_parser('console', help="connect to the console")
+parser_run.set_defaults(func=handle_console)
+
+args = parser.parse_args()
+logging.basicConfig(level=VERBOSITY[args.verbose],
+ format='%(levelname)-8s %(module)-8s %(funcName)-16s %(message)s')
+try:
+ res = args.func(args)
+ exit(res)
+except RatpError as detail:
+ print("Ratp error:", detail, file=sys.stderr);
+ exit(127)
+except KeyboardInterrupt:
+ print("\nInterrupted", file=sys.stderr);
+ exit(1)
+#try:
+# res = args.func(args)
+#except Exception as e:
+# print("error: failed to establish connection: %s" % e, file=sys.stderr)
+# exit(2)