diff options
Diffstat (limited to 'usb-control/command_processor.py')
-rw-r--r-- | usb-control/command_processor.py | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/usb-control/command_processor.py b/usb-control/command_processor.py new file mode 100644 index 0000000..ee03ae2 --- /dev/null +++ b/usb-control/command_processor.py @@ -0,0 +1,105 @@ +""" +Generic class for arg parsing and command processing. +""" + +__copyright__ = 'Copyright (C) 2019 Theobroma Systems Design und Consulting GmbH' +__license__ = 'MIT' + +import sys +import cp210x_controller + +def do_delay(dev): + time.sleep(0.3) + +def do_list(devs): + print "Found %d Baseboard%s:" % (len(devs), "s"[len(devs) == 1:]) + for idx, dev in enumerate(devs): + print "%d: %s %s" % (idx, dev.serial_number, dev.product) + +class CommandProcessor: + def __init__(self, product_string, options_dict, command_dict): + self.product_string = product_string + self.options_dict = options_dict + self.command_dict = { + 'delay': (do_delay, + 'delays execution for 300 ms'), + 'list': (do_list, + 'lists serial numbers of attached Haikou Baseboards'), + } + + self.command_dict.update(command_dict) + + def print_helpstring(self): + print 'Tool to controls power and BIOS disable state' + print 'Synopsis: ' + sys.argv[0] + ' OPTIONS COMMAND(S)' + print 'Options:' + print "\t-h, --help...displays this help text" + print "\t-i, --ignore-product-string...ignore the product string of the device" + print "\t-s SERIAL, --serial SERIAL...specify the serial number of the device" + if self.options_dict: + for o in self.options_dict: + (cb, helpstr) = self.options_dict[o] + print "\t" + o + "..." + helpstr + print 'Commands:' + for c in self.command_dict: + (cb, helpstr) = self.command_dict[c] + print "\t" + c + "..." + helpstr + print "" + + def validate_commandline(self, argv): + self.ignore_product_string = False + self.serialnumber = None + self.command_list = [] + + consume = None + for arg in argv: + if arg == '-h' or arg == '--help': + self.print_helpstring() + sys.exit(0) + elif arg == '-i' or arg == '--ignore-product-string': + self.ignore_product_string = True; + elif arg == '-s' or arg == '--serial': + consume = '-s' + elif consume == '-s': + self.serialnumber = arg + consume = None + else: + if self.options_dict and arg in self.options_dict: + (cb, helpstr) = self.options_dict[arg] + cb() + elif arg in self.command_dict: + self.command_list.append(arg) + else: + print "Unkown command " + arg + sys.exit(1) + + def run(self): + # Collect list of potential devices + if self.ignore_product_string == True: + self.product_string = None + + # Get list of matching USB devices + devs = cp210x_controller.find_board_list(self.product_string, self.serialnumber) + + if len(devs) == 0: + print "No devices found!" + sys.exit(2) + + # List devices if required + if 'list' in self.command_list: + do_list(devs) + sys.exit(0) + + # Other commands than 'list' require exactly one device + if len(devs) != 1: + print "Please specify target device (found " + str(len(devs)) + " matching devices)" + sys.exit(3) + + # Get the one and only device + dev = devs[0] + + # Execute commands + for cmd in self.command_list: + (cb, helpstr) = self.command_dict[cmd] + cb(dev) + |