2020-11-10 23:45:50 -04:00
|
|
|
#! /usr/bin/env python3
|
2019-06-15 13:03:37 -03:00
|
|
|
|
|
|
|
import serial, time
|
|
|
|
import subprocess
|
|
|
|
from subprocess import call, Popen
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
import re
|
2020-11-10 23:45:50 -04:00
|
|
|
import sys
|
2021-07-17 10:33:39 -03:00
|
|
|
import datetime
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_RED = "\x1b[31m"
|
2021-07-16 11:46:44 -03:00
|
|
|
COLOR_GREEN = "\x1b[32m"
|
2021-07-16 22:08:08 -03:00
|
|
|
COLOR_YELLOW = "\x1b[33m"
|
2021-07-16 11:46:44 -03:00
|
|
|
COLOR_WHITE = "\x1b[37m"
|
|
|
|
COLOR_RESET = "\x1b[0m"
|
|
|
|
|
|
|
|
def print_line(line):
|
|
|
|
if "WARNING" in line:
|
|
|
|
line = line.replace("WARNING", f"{COLOR_YELLOW}WARNING{COLOR_RESET}", 1)
|
|
|
|
elif "WARN" in line:
|
|
|
|
line = line.replace("WARN", f"{COLOR_YELLOW}WARN{COLOR_RESET}", 1)
|
|
|
|
elif "ERROR" in line:
|
|
|
|
line = line.replace("ERROR", f"{COLOR_RED}ERROR{COLOR_RESET}", 1)
|
|
|
|
elif "INFO" in line:
|
|
|
|
line = line.replace("INFO", f"{COLOR_WHITE}INFO{COLOR_RESET}", 1)
|
|
|
|
|
|
|
|
if "PASSED" in line:
|
|
|
|
line = line.replace("PASSED", f"{COLOR_GREEN}PASSED{COLOR_RESET}", 1)
|
|
|
|
|
|
|
|
if "FAILED" in line:
|
|
|
|
line = line.replace("FAILED", f"{COLOR_RED}FAILED{COLOR_RESET}", 1)
|
|
|
|
|
2021-08-24 10:15:48 -03:00
|
|
|
if "\n" in line:
|
|
|
|
current_time = datetime.datetime.now()
|
|
|
|
print('[{0}] {1}'.format(current_time.isoformat(timespec='milliseconds'), line), end='')
|
|
|
|
else:
|
|
|
|
print('{0}'.format(line), end='')
|
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
|
2019-06-15 13:03:37 -03:00
|
|
|
def do_nsh_cmd(port, baudrate, cmd):
|
2021-12-31 19:55:59 -04:00
|
|
|
ser = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1, xonxoff=False, rtscts=False, dsrdtr=False)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-08-23 17:45:46 -03:00
|
|
|
timeout = 30 # 30 seconds
|
2020-11-10 23:45:50 -04:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# wait for nsh prompt
|
|
|
|
while True:
|
|
|
|
ser.write("\n".encode("ascii"))
|
2020-11-13 16:17:31 -04:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
if "nsh>" in serial_line:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if time.monotonic() > timeout_start + timeout:
|
2021-07-15 20:17:48 -03:00
|
|
|
print("Error, timeout waiting for prompt")
|
|
|
|
sys.exit(1)
|
2020-11-10 23:45:50 -04:00
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
# clear
|
2021-12-31 19:55:59 -04:00
|
|
|
ser.reset_input_buffer()
|
2021-07-16 11:46:44 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# run command
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-07-18 12:09:49 -03:00
|
|
|
timeout = 1 # 1 second
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
success_cmd = "cmd succeeded!"
|
2020-11-13 16:17:31 -04:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# wait for command echo
|
2021-12-11 14:41:06 -04:00
|
|
|
print("Running command: \'{0}\'".format(cmd))
|
2021-09-21 19:38:37 -03:00
|
|
|
serial_cmd = '{0}; echo "{1}"; echo "{2}";\r\n'.format(cmd, success_cmd, success_cmd)
|
2021-07-15 20:17:48 -03:00
|
|
|
ser.write(serial_cmd.encode("ascii"))
|
|
|
|
ser.flush()
|
|
|
|
while True:
|
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
if cmd in serial_line:
|
|
|
|
break
|
2021-07-19 11:15:06 -03:00
|
|
|
elif serial_line.startswith(success_cmd) and len(serial_line) <= len(success_cmd) + 2:
|
|
|
|
print_line(serial_line)
|
|
|
|
# we missed the echo, but command ran and succeeded
|
|
|
|
sys.exit(0)
|
2021-07-15 20:17:48 -03:00
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2021-07-15 20:17:48 -03:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if (len(serial_line) <= 0) and (time.monotonic() > timeout_start + timeout):
|
2021-07-15 20:17:48 -03:00
|
|
|
print("Error, timeout waiting for command echo")
|
|
|
|
break
|
2020-09-28 17:41:28 -03:00
|
|
|
|
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
timeout_start = time.monotonic()
|
2021-12-06 18:24:16 -04:00
|
|
|
timeout = 240 # 4 minutes
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2021-08-23 17:45:46 -03:00
|
|
|
return_code = 0
|
|
|
|
|
2020-09-28 17:41:28 -03:00
|
|
|
while True:
|
2020-11-13 16:17:31 -04:00
|
|
|
serial_line = ser.readline().decode("ascii", errors='ignore')
|
2019-06-15 13:03:37 -03:00
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
if success_cmd in serial_line:
|
2021-08-23 17:45:46 -03:00
|
|
|
sys.exit(return_code)
|
2019-06-15 13:03:37 -03:00
|
|
|
break
|
2020-11-10 23:45:50 -04:00
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
2021-08-23 17:45:46 -03:00
|
|
|
if "ERROR " in serial_line:
|
|
|
|
return_code = -1
|
|
|
|
|
2021-07-16 11:46:44 -03:00
|
|
|
print_line(serial_line)
|
2020-11-10 23:45:50 -04:00
|
|
|
|
|
|
|
if "nsh>" in serial_line:
|
2021-07-15 20:17:48 -03:00
|
|
|
sys.exit(1) # error, command didn't complete successfully
|
2020-11-10 23:45:50 -04:00
|
|
|
elif "NuttShell (NSH)" in serial_line:
|
2021-07-15 20:17:48 -03:00
|
|
|
sys.exit(1) # error, command didn't complete successfully
|
2020-11-10 23:45:50 -04:00
|
|
|
|
2021-12-11 14:41:06 -04:00
|
|
|
if (len(serial_line) <= 0) and (time.monotonic() > timeout_start + timeout):
|
|
|
|
print("Error, timeout")
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2020-11-10 23:45:50 -04:00
|
|
|
if len(serial_line) <= 0:
|
2021-07-15 20:17:48 -03:00
|
|
|
ser.write("\r\n".encode("ascii"))
|
2020-11-10 23:45:50 -04:00
|
|
|
ser.flush()
|
2021-12-11 14:41:06 -04:00
|
|
|
time.sleep(0.2)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
|
|
|
ser.close()
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = ArgumentParser(description=__doc__)
|
2020-11-10 23:45:50 -04:00
|
|
|
parser.add_argument('--device', "-d", nargs='?', default=None, help='', required=True)
|
2019-06-15 13:03:37 -03:00
|
|
|
parser.add_argument("--baudrate", "-b", dest="baudrate", type=int, help="Mavlink port baud rate (default=57600)", default=57600)
|
|
|
|
parser.add_argument("--cmd", "-c", dest="cmd", help="Command to run")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
do_nsh_cmd(args.device, args.baudrate, args.cmd)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|