|
23 | 23 | from .common import ResourceEntry, queue_as_aiter |
24 | 24 | from .generated import labgrid_coordinator_pb2, labgrid_coordinator_pb2_grpc |
25 | 25 | from ..util import get_free_port, labgrid_version |
| 26 | +from ..util.helper import processwrapper |
26 | 27 |
|
27 | 28 |
|
28 | 29 | exports: Dict[str, Type[ResourceEntry]] = {} |
@@ -304,6 +305,107 @@ def _stop(self, start_params): |
304 | 305 | exports["RawSerialPort"] = SerialPortExport |
305 | 306 |
|
306 | 307 |
|
| 308 | +@attr.s(eq=False) |
| 309 | +class CANPortExport(ResourceExport): |
| 310 | + """ResourceExport for a USB and Raw CANPort""" |
| 311 | + |
| 312 | + def __attrs_post_init__(self): |
| 313 | + super().__attrs_post_init__() |
| 314 | + if self.cls == "RawCANPort": |
| 315 | + from ..resource.canport import RawCANPort |
| 316 | + |
| 317 | + self.local = RawCANPort(target=None, name=None, **self.local_params) |
| 318 | + elif self.cls == "USBCANPort": |
| 319 | + from ..resource.udev import USBCANPort |
| 320 | + |
| 321 | + self.local = USBCANPort(target=None, name=None, **self.local_params) |
| 322 | + self.data["cls"] = "NetworkCANPort" |
| 323 | + self.child = None |
| 324 | + self.port = None |
| 325 | + self.cannelloni_bin = shutil.which("cannelloni") |
| 326 | + if self.cannelloni_bin is None: |
| 327 | + self.cannelloni_bin = "/usr/bin/cannelloni" |
| 328 | + warnings.warn("cannelloni binary not found, falling back to %s", self.cannelloni_bin) |
| 329 | + |
| 330 | + def __del__(self): |
| 331 | + if self.child is not None: |
| 332 | + self.stop() |
| 333 | + |
| 334 | + def _get_start_params(self): |
| 335 | + return { |
| 336 | + "ifname": self.local.ifname, |
| 337 | + } |
| 338 | + |
| 339 | + def _get_params(self): |
| 340 | + """Helper function to return parameters""" |
| 341 | + return { |
| 342 | + "host": self.host, |
| 343 | + "port": self.port, |
| 344 | + "speed": self.local.speed, |
| 345 | + "extra": { |
| 346 | + "ifname": self.local.ifname, |
| 347 | + }, |
| 348 | + } |
| 349 | + |
| 350 | + def _start(self, start_params): |
| 351 | + """Start ``cannelloni`` subprocess""" |
| 352 | + assert self.local.avail |
| 353 | + assert self.child is None |
| 354 | + self.port = get_free_port() |
| 355 | + |
| 356 | + # XXX How to handle the permissions on the exporer? Via the helper with sudo? |
| 357 | + cmd_down = f"ip link set {self.local.ifname} down" |
| 358 | + processwrapper.check_output(cmd_down.split()) |
| 359 | + |
| 360 | + cmd_type_bitrate = f"ip link set {self.local.ifname} type can bitrate {self.local.speed}" |
| 361 | + processwrapper.check_output(cmd_type_bitrate.split()) |
| 362 | + |
| 363 | + cmd_up = f"ip link set {self.local.ifname} up" |
| 364 | + processwrapper.check_output(cmd_up.split()) |
| 365 | + |
| 366 | + cmd_cannelloni = [ |
| 367 | + self.cannelloni_bin, |
| 368 | + "-C", "s", |
| 369 | + # XXX Set "no peer checking" mode. Is it ok? It seems so for serial... |
| 370 | + "-p", |
| 371 | + "-I", f"{self.local.ifname}", |
| 372 | + "-l", f"{self.port}", |
| 373 | + ] |
| 374 | + self.logger.info("Starting cannelloni with: %s", " ".join(cmd_cannelloni)) |
| 375 | + self.child = subprocess.Popen(cmd_cannelloni) |
| 376 | + try: |
| 377 | + self.child.wait(timeout=2) |
| 378 | + raise ExporterError(f"cannelloni for {start_params['ifname']} exited immediately") |
| 379 | + except subprocess.TimeoutExpired: |
| 380 | + # good, cannelloni didn't exit immediately |
| 381 | + pass |
| 382 | + self.logger.info("cannelloni started for %s on port %d", start_params["ifname"], self.port) |
| 383 | + |
| 384 | + def _stop(self, start_params): |
| 385 | + """Stop ``cannelloni`` subprocess and disable the interface""" |
| 386 | + assert self.child |
| 387 | + child = self.child |
| 388 | + self.child = None |
| 389 | + port = self.port |
| 390 | + self.port = None |
| 391 | + child.terminate() |
| 392 | + try: |
| 393 | + child.wait(3.0) |
| 394 | + except subprocess.TimeoutExpired: |
| 395 | + self.logger.warning("cannelloni for %s still running after SIGTERM", start_params["ifname"]) |
| 396 | + log_subprocess_kernel_stack(self.logger, child) |
| 397 | + child.kill() |
| 398 | + child.wait(1.0) |
| 399 | + self.logger.info("cannelloni stopped for %s on port %d", start_params["ifname"], port) |
| 400 | + |
| 401 | + cmd_down = f"ip link set {start_params['ifname']} down" |
| 402 | + processwrapper.check_output(cmd_down.split()) |
| 403 | + |
| 404 | + |
| 405 | +exports["USBCANPort"] = CANPortExport |
| 406 | +exports["RawCANPort"] = CANPortExport |
| 407 | + |
| 408 | + |
307 | 409 | @attr.s(eq=False) |
308 | 410 | class NetworkInterfaceExport(ResourceExport): |
309 | 411 | """ResourceExport for a network interface""" |
|
0 commit comments