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-07-17 10:33:39 -03:00
|
|
|
current_time = datetime.datetime.now()
|
|
|
|
print('[{0}] {1}'.format(current_time.isoformat(timespec='milliseconds'), 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-08-17 00:42:34 -03:00
|
|
|
ser = serial.Serial(port, baudrate, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=0.2, xonxoff=True, rtscts=False, dsrdtr=False)
|
2019-06-15 13:03:37 -03:00
|
|
|
|
|
|
|
timeout_start = time.time()
|
2020-11-10 23:45:50 -04:00
|
|
|
timeout = 10 # 10 seconds
|
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# wait for nsh prompt
|
|
|
|
while True:
|
|
|
|
ser.write("\n".encode("ascii"))
|
|
|
|
ser.flush()
|
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-07-15 20:17:48 -03:00
|
|
|
if time.time() > timeout_start + timeout:
|
|
|
|
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
|
|
|
|
ser.readlines()
|
|
|
|
|
2021-07-15 20:17:48 -03:00
|
|
|
# run command
|
|
|
|
timeout_start = time.time()
|
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
|
|
|
|
serial_cmd = '{0}; echo "{1}"\r\n'.format(cmd, success_cmd)
|
|
|
|
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
|
|
|
|
|
|
|
if time.time() > timeout_start + timeout:
|
|
|
|
print("Error, timeout waiting for command echo")
|
|
|
|
break
|
2020-09-28 17:41:28 -03:00
|
|
|
|
|
|
|
|
2019-06-15 13:03:37 -03:00
|
|
|
timeout_start = time.time()
|
2021-07-18 12:09:49 -03:00
|
|
|
timeout = 180 # 3 minutes
|
2019-06-15 13:03:37 -03:00
|
|
|
|
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:
|
2019-06-15 13:03:37 -03:00
|
|
|
break
|
2020-11-10 23:45:50 -04:00
|
|
|
else:
|
|
|
|
if len(serial_line) > 0:
|
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
|
|
|
|
|
|
|
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()
|
2019-06-15 13:03:37 -03:00
|
|
|
|
|
|
|
if time.time() > timeout_start + timeout:
|
|
|
|
print("Error, timeout")
|
2020-11-10 23:45:50 -04:00
|
|
|
sys.exit(-1)
|
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()
|