aboutsummaryrefslogtreecommitdiff
path: root/usb-control/command_processor.py
blob: ee03ae2fc858336b3d8d5dec2d9e1bba1d9808af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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)