Debian Codegraph @

examples/usbpower/examplestrategy.py

raw symbols SCIP
1 import enum
2
3 import attr
4
5 from labgrid.driver import BareboxDriver, ShellDriver, USBSDMuxDriver
6 from labgrid import step, target_factory
7 from labgrid.protocol import PowerProtocol
8 from labgrid.strategy import Strategy, never_retry
9
10
11 @attr.s(eq=False)
12 class StrategyError(Exception):
13 msg = attr.ib(validator=attr.validators.instance_of(str))
14
15
16 class Status(enum.Enum):
17 unknown = 0
18 barebox = 1
19 shell = 2
20
21
22 @target_factory.reg_driver
23 @attr.s(eq=False)
24 class ExampleStrategy(Strategy):
25 """ExampleStrategy - Strategy to for the usbpower labgrid example"""
26
27 bindings = {
28 "power": PowerProtocol,
29 "sdmux": USBSDMuxDriver,
30 "barebox": BareboxDriver,
31 "shell": ShellDriver,
32 }
33
34 status = attr.ib(default=Status.unknown)
35
36 def __attrs_post_init__(self):
37 super().__attrs_post_init__()
38
39 @never_retry
40 @step(args=["status"])
41 def transition(self, status, *, step):
42 if not isinstance(status, Status):
43 status = Status[status]
44 if status == Status.unknown:
45 raise StrategyError(f"can not transition to {status}")
46 elif status == self.status:
47 step.skip("nothing to do")
48 return # nothing to do
49 elif status == Status.barebox:
50 self.target.activate(self.power)
51 self.target.activate(self.sdmux)
52 # power off
53 self.power.off()
54 # configure sd-mux
55 self.sdmux.set_mode("dut")
56 # cycle power
57 self.power.on()
58 # interrupt barebox
59 self.target.activate(self.barebox)
60 elif status == Status.shell:
61 # transition to barebox
62 self.transition(Status.barebox)
63 self.barebox.boot("")
64 self.barebox.await_boot()
65 self.target.activate(self.shell)
66 else:
67 raise StrategyError(f"no transition found from {self.status} to {status}")
68 self.status = status