|
| 1 | +import subprocess |
| 2 | + |
| 3 | +import attr |
| 4 | + |
| 5 | +from .common import Driver |
| 6 | +from ..factory import target_factory |
| 7 | +from ..step import step |
| 8 | +from .exception import ExecutionError |
| 9 | +from ..util.helper import processwrapper |
| 10 | + |
| 11 | +@target_factory.reg_driver |
| 12 | +@attr.s(eq=False) |
| 13 | +class USBSDWire3Driver(Driver): |
| 14 | + """The USBSDWire3Driver uses the sdwire tool to control SDWire hardware |
| 15 | +
|
| 16 | + Args: |
| 17 | + bindings (dict): driver to use with usbsdmux |
| 18 | + """ |
| 19 | + bindings = { |
| 20 | + "mux": {"USBSDWire3Device", "NetworkUSBSDWire3Device"}, |
| 21 | + } |
| 22 | + |
| 23 | + def __attrs_post_init__(self): |
| 24 | + super().__attrs_post_init__() |
| 25 | + print(f"USBSDWire3Driver __attrs_post_init__ called for {self.mux}") |
| 26 | + if self.target.env: |
| 27 | + self.tool = self.target.env.config.get_tool('sdwire') |
| 28 | + else: |
| 29 | + self.tool = 'sdwire' |
| 30 | + if self.mux.control_serial is None: |
| 31 | + raise ExecutionError("USBSDWire3Driver requires 'control_serial' to be set in the resource") |
| 32 | + self.control_serial = self.match_control_serial() |
| 33 | + print(f"USBSDWire3Driver __attrs_post_init__ control_serial: {self.control_serial}") |
| 34 | + |
| 35 | + @Driver.check_active |
| 36 | + @step(title='sdmux_set', args=['mode']) |
| 37 | + def set_mode(self, mode): |
| 38 | + if not mode.lower() in ['dut', 'host']: |
| 39 | + raise ExecutionError(f"Setting mode '{mode}' not supported by USBSDWire3Driver") |
| 40 | + cmd = self.mux.command_prefix + [ |
| 41 | + self.tool, |
| 42 | + "switch", |
| 43 | + "-s", |
| 44 | + self.control_serial, |
| 45 | + "dut" if mode.lower() == "dut" else "ts", |
| 46 | + ] |
| 47 | + print(f"USBSDWire3Driver set_mode executing: {' '.join(cmd)}") |
| 48 | + processwrapper.check_output(cmd) |
| 49 | + |
| 50 | + def match_control_serial(self): |
| 51 | + cmd = self.mux.command_prefix + [ |
| 52 | + self.tool, |
| 53 | + "list" |
| 54 | + ] |
| 55 | + proc = subprocess.run( |
| 56 | + cmd, |
| 57 | + stdout=subprocess.PIPE, |
| 58 | + check=True |
| 59 | + ) |
| 60 | + output = proc.stdout.strip().decode() |
| 61 | + for line in output.splitlines(): |
| 62 | + if self.mux.control_serial is not None and line.find(self.mux.control_serial) >= 0: |
| 63 | + return line.split()[0] |
| 64 | + raise ExecutionError(f"Could not find control serial {self.mux.control_serial} in sdwire list output") |
| 65 | + |
| 66 | + @Driver.check_active |
| 67 | + @step(title='sdmux_get') |
| 68 | + def get_mode(self): |
| 69 | + raise ExecutionError("Getting mode not supported by USBSDWire3Driver") |
0 commit comments