From 9e84abf6c40829349caabb87b3bb1aa139ec25d1 Mon Sep 17 00:00:00 2001 From: kb1lqc Date: Sun, 11 Mar 2018 00:47:17 -0800 Subject: [PATCH] Added baud rate user input value checking per #30 * Added baud rate value and type checking * Changed argparse input type to integer --- faradayio_cli/faradayio_cli.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/faradayio_cli/faradayio_cli.py b/faradayio_cli/faradayio_cli.py index 11fb2dd..7e3f102 100644 --- a/faradayio_cli/faradayio_cli.py +++ b/faradayio_cli/faradayio_cli.py @@ -32,7 +32,7 @@ def setupArgparse(): # Optional arguments parser.add_argument("--addr", default="10.0.0.1", help="Set IP Address of TUN adapter (Faraday Radio)") - parser.add_argument("-b", "--baud", default="115200", + parser.add_argument("-b", "--baud", default=115200, type=int, help="Set serial port baud rate") parser.add_argument("-l", "--loopback", action="store_true", help="Use software loopback serial port") @@ -78,6 +78,17 @@ def checkUserInput(args): # Expect an IP address that is valid ipaddress.IPv4Address(args.addr) + # Check Baud Rate + # Expect and integer + if not isinstance(args.baud, int): + raise TypeError + # Expect and integer that is a standard serial value + # Should be able to use argparse choices too + baudrate = [50, 75, 110, 134, 150, 200, 600, 1200, 1800, 2400, 4800, 9600, + 19200, 38400, 57600, 115200, 230400, 460800, 500000, 57600, + 921600] + if args.baud not in baudrate: + raise ValueError def setupSerialPort(loopback, port, baud, readtimeout, writetimeout): """Sets up serial port by connecting to phsyical or software port.