How to access role.Board from role.BoardUboot and role.BoardLinux?
jneuhauser opened this issue · 4 comments
Hello,
I have a configuration like below where I want to access properties of my board configuration from my Uboot or Linux configuration. See the method init()
in the class MyUboot
. The configuration below works because board.Connector
provides _board
but by convention we should not rely on properties with underscore prefix.
import tbot
from tbot.machine import board, channel, connector
class MyBoard(
connector.ConsoleConnector,
board.PowerControl,
board.Board,
):
device_id = 0
device_ip = "10.20.30.43"
netmask = "255.255.255.0"
gateway_ip = "10.20.30.254"
relay_ip = "10.20.30.41"
def connect(self, mach) -> channel.Channel:
return mach.open_channel("picocom", "-b", "115200", "/dev/ttyUSB0")
def poweron(self) -> None:
self.host.exec0("curl", "http://{self.relay_ip}/io.cgi?relay={device_id}?state=1")
def poweroff(self) -> None:
self.host.exec0("curl", "http://{self.relay_ip}/io.cgi?relay={device_id}?state=0")
class MyUboot(
board.Connector,
board.UBootAutobootIntercept,
board.UBootShell,
):
def init(self) -> None:
self.env("ipaddr", self._board.device_ip)
self.env("netmask", self._board.netmask)
self.env("gatewayip", self._board.gateway_ip)
def register_machines(ctx: tbot.Context) -> None:
ctx.register(MyBoard, tbot.role.Board)
ctx.register(MyUboot, tbot.role.BoardUBoot)
Is there an intended way of accessing these objects like the context API for tests or do we need to introduce something new?
Thanks in advance, Johann
Hi,
yeah this is a valid usecase and just like you I've hacked around it by just accessing _board
in the past... But of course that's a crutch and not a nice solution.
First of all, I want to increase the scope of the issue: Similarly, with board-linux machines using board.LinuxUbootConnector
one might also want to access the board machine for information. Here, no option exists at all right now.
The problem essentially exists in the Connector
implementations used for target hardware. Right now, it is a wild-west mess of each one doing their own thing. I think the solution here is to create a sort of protocol (in the sense of typing.Protocol
) they all adhere to.
This protocol should see each of the machines "running" on the board to have a board
property by which we can reference the board machine after it was transformed. I've cobbled together such a thing in PR #66. Can you take a look if that a) fits your needs and b) looks reasonable from your point of view as well?
Okay, I've merged #66 anyway but kept this issue open so you can still report whether this is enough to accomodate your usecase. If not, I'm happy to revise the changes again.
Sorry for the late reply, I was very busy with other things....
I took a look at the implementation, which looks very clean and is exactly what I'm looking for.
I haven't tested the implementation yet unfortunately, but will do so next week and then close the issue.
Thanks for your great work, I like tbot a lot and hopefully I can contribute some time in the future.
Works like a charm. Thank you.