This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nuttx-ntfc.git
The following commit(s) were added to refs/heads/main by this push:
new 7449a06 device: add the console over a NuttX netdev console
7449a06 is described below
commit 7449a06fe43623a26d0db85888a521bba46f7d07
Author: p-szafonimateusz <[email protected]>
AuthorDate: Tue Nov 10 09:44:08 2026 +0100
device: add the console over a NuttX netdev console
A target built with CONFIG_CONSOLE_NETDEV has its NSH console on a
network transport, UDP or CAN ISO-TP, not on a serial port.
- The new netconsole core option runs the sim/qemu console over the
transport instead of the child pty; the pty output goes to the debug
log.
- The new netdev device is for real hardware: NTFC talks to the board
only over the transport and resets it with the reboot command.
The host opens the transport before the target is started or reset, so
the boot output is not lost. UDP learns the target address from the
datagrams it receives. Writes are split into datagrams that fit the
target console receive buffer, and each one waits for the previous one
to leave, as an ISO-TP socket sends one message at a time.
Signed-off-by: p-szafonimateusz <[email protected]>
Assisted-by: Claude Code:claude-opus-5-5
---
CHANGES.md | 2 +
Documentation/api.rst | 2 +
Documentation/config-yaml.rst | 175 ++++++++++++++++++++-
Documentation/config.yaml | 9 +-
src/ntfc/coreconfig.py | 10 ++
src/ntfc/device/getdev.py | 2 +
src/ntfc/device/host.py | 36 ++++-
src/ntfc/device/netconsole.py | 260 +++++++++++++++++++++++++++++++
src/ntfc/device/netdev.py | 138 ++++++++++++++++
tests/device/test_host.py | 41 +++++
tests/device/test_netconsole.py | 249 +++++++++++++++++++++++++++++
tests/device/test_netdev.py | 104 +++++++++++++
tests/resources/netconsole/udp_target.py | 44 ++++++
13 files changed, 1065 insertions(+), 7 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 7aaf991..a676aae 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,8 @@
- Refuse commands longer than the target CONFIG_LINE_MAX.
- Reopen serial ports that disappear on target reset (USB re-enumeration).
- Do not treat a target idle at its prompt as a busy loop.
+- Add the console over a NuttX netdev console (UDP or CAN ISO-TP) for sim, QEMU
+ and real hardware (``netdev`` device).
## 0.0.3
diff --git a/Documentation/api.rst b/Documentation/api.rst
index b680641..1012443 100644
--- a/Documentation/api.rst
+++ b/Documentation/api.rst
@@ -34,6 +34,8 @@ Device Interface
ntfc.device.common
ntfc.device.host
+ ntfc.device.netconsole
+ ntfc.device.netdev
ntfc.device.nuttx
ntfc.device.qemu
ntfc.device.renode
diff --git a/Documentation/config-yaml.rst b/Documentation/config-yaml.rst
index 06d663b..9e4f2fd 100644
--- a/Documentation/config-yaml.rst
+++ b/Documentation/config-yaml.rst
@@ -16,12 +16,12 @@ This file defines device-under-test (DUT) setup and global
configuration.
cores: # List of product cores
core0: # Core0 entry
name: 'core0-name'
- device: 'sim|qemu|renode|serial'
+ device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration
core1: # Core1 entry
name: 'core1-name'
- device: 'sim|qemu|renode|serial'
+ device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration
@@ -37,7 +37,7 @@ This file defines device-under-test (DUT) setup and global
configuration.
cores:
core0:
name: 'core-name'
- device: 'sim|qemu|renode|serial'
+ device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration
product1:
@@ -45,7 +45,7 @@ This file defines device-under-test (DUT) setup and global
configuration.
cores:
core0:
name: 'core-name'
- device: 'sim|qemu|renode|serial'
+ device: 'sim|qemu|renode|serial|netdev'
# Device-specific configuration
@@ -272,6 +272,169 @@ For real hardware with UART communication:
- DATABITS: 5, 6, 7, or 8
- STOPBITS: 1, 1.5, or 2
+Netdev Console
+==============
+
+The ``netconsole`` core option runs the NuttX console over a network
+transport (``CONFIG_CONSOLE_NETDEV``) instead of a serial port or the
+emulator's pty. The transport is implemented in
+:mod:`ntfc.device.netconsole`. It can be used in two ways:
+
+- with the spawned devices (``sim`` and ``qemu``, through
+ :class:`ntfc.device.host.DeviceHost`): add ``netconsole`` to the core;
+ the child pty output is kept in the debug log;
+- with real hardware: use ``device: 'netdev'``
+ (:class:`ntfc.device.netdev.DeviceNetdev`). NTFC only talks to the board
+ over the transport; the board is reset and powered off with the
+ ``reboot`` and ``poweroff`` system commands, as for a serial device.
+
+ .. code-block:: yaml
+
+ cores:
+ core0:
+ name: 'main'
+ device: 'netdev'
+ reboot: 'st-flash reset'
+ boot_timeout: 30
+ netconsole:
+ type: 'udp'
+ port: 55000
+
+ The UDP target address is learned from the console output, so the board
+ must print something after the transport is started: use a ``reboot``
+ command that resets the board.
+
+NTFC opens the transport before it spawns the target, so the console
+output sent at boot is not lost. Both transports carry datagrams, so a
+command is split into datagrams of ``chunk`` bytes that fit the target
+receive buffer (``CONFIG_CONSOLE_NETDEV_RXBUFSIZE``, 256 by default).
+
+UDP
+---
+
+.. code-block:: yaml
+
+ cores:
+ core0:
+ name: 'main'
+ device: 'qemu'
+ netconsole:
+ type: 'udp'
+ port: 55000 # CONFIG_CONSOLE_NETDEV_PORT
+ bind: '0.0.0.0' # optional
+ chunk: 128 # optional
+
+NTFC listens on ``port`` for the datagrams the target sends to
+``CONFIG_CONSOLE_NETDEV_IPADDR`` and replies to the address the last
+datagram came from, so the target address is not configured. Commands
+written before the target sent anything are dropped.
+
+.. list-table::
+ :header-rows: 1
+
+ * - Key
+ - Description
+ * - ``port``
+ - UDP port, ``CONFIG_CONSOLE_NETDEV_PORT``. Defaults to ``55000``
+ * - ``bind``
+ - (Optional) Host address to listen on. Defaults to ``0.0.0.0``
+ * - ``chunk``
+ - (Optional) Bytes per datagram. Defaults to ``128``
+
+CAN ISO-TP
+----------
+
+.. code-block:: yaml
+
+ cores:
+ core0:
+ name: 'main'
+ device: 'qemu'
+ netconsole:
+ type: 'isotp'
+ interface: 'can0'
+ rx_id: 0x7d0 # target TX ID, CONFIG_CONSOLE_NETDEV_PORT
+ tx_id: 0x7d1 # target RX ID, CONFIG_CONSOLE_NETDEV_PORT + 1
+ fd: false # true when the target has CONFIG_NET_CAN_CANFD
+
+The host uses a Linux ``can_isotp`` socket, so the ``can_isotp`` kernel
+module must be loaded (``sudo modprobe can_isotp``).
+
+.. list-table::
+ :header-rows: 1
+
+ * - Key
+ - Description
+ * - ``interface``
+ - Host CAN interface. Defaults to ``can0``
+ * - ``rx_id``
+ - CAN ID the target sends on, ``CONFIG_CONSOLE_NETDEV_PORT``.
+ Defaults to ``2000`` (``0x7d0``)
+ * - ``tx_id``
+ - CAN ID the target receives on, ``CONFIG_CONSOLE_NETDEV_PORT + 1``.
+ Defaults to ``rx_id + 1``
+ * - ``fd``
+ - (Optional) Use CAN FD frames, required when the target is built with
+ ``CONFIG_NET_CAN_CANFD``. Defaults to ``false``
+ * - ``chunk``
+ - (Optional) Bytes per ISO-TP message. Defaults to ``128``
+
+Example: qemu-intel64
+---------------------
+
+The NuttX ``qemu-intel64:eth_console`` configuration runs the console over
+UDP on an e1000 card: the target is ``192.168.8.104`` and sends the console
+to ``192.168.8.1`` port ``55000``. Give the host that address on a TAP
+interface (or on a bridge the TAP interface is attached to):
+
+.. code-block:: bash
+
+ sudo ip tuntap add dev tap0 mode tap user $USER
+ sudo ip addr add 192.168.8.1/24 dev tap0
+ sudo ip link set tap0 up
+
+.. code-block:: yaml
+
+ cores:
+ core0:
+ name: 'main'
+ device: 'qemu'
+ exec_path: 'qemu-system-x86_64'
+ exec_args: '-m 2G -nographic -serial mon:stdio
+ -device e1000,netdev=n0
+ -netdev tap,id=n0,ifname=tap0,script=no,downscript=no'
+ netconsole:
+ type: 'udp'
+ port: 55000
+
+For ISO-TP, build the target with ``CONFIG_NET_CAN_ISOTP``,
+``CONFIG_NET_CAN_SOCK_OPTS`` and a PCI CAN controller (``CONFIG_CAN_KVASER``
+with ``CONFIG_CAN_KVASER_SOCKET``) and connect the QEMU CAN bus to the host
+interface ``can0`` (a ``vcan`` interface works as well):
+
+.. code-block:: bash
+
+ sudo modprobe can_isotp
+ sudo ip link add dev can0 type vcan
+ sudo ip link set can0 up
+
+.. code-block:: yaml
+
+ cores:
+ core0:
+ name: 'main'
+ device: 'qemu'
+ exec_path: 'qemu-system-x86_64'
+ exec_args: '-m 2G -nographic -serial mon:stdio
+ -object can-bus,id=canbus0
+ -object
can-host-socketcan,id=canhost0,if=can0,canbus=canbus0
+ -device kvaser_pci,canbus=canbus0'
+ netconsole:
+ type: 'isotp'
+ interface: 'can0'
+ rx_id: 0x7d0
+ tx_id: 0x7d1
+
Configuration Approaches
========================
@@ -518,6 +681,10 @@ These fields are parsed by
:class:`ntfc.coreconfig.CoreConfig`.
* - ``kv``
- Per-core Kconfig overrides applied before build. Overrides matching
keys from global ``config.kv``
+ * - ``netconsole``
+ - (Optional) Run the console over a NuttX netdev console (UDP or CAN
+ ISO-TP) instead of the emulator pty (sim/qemu only).
+ See `Netdev Console`_
Linux Targets
=============
diff --git a/Documentation/config.yaml b/Documentation/config.yaml
index f1a8d5e..e09af25 100644
--- a/Documentation/config.yaml
+++ b/Documentation/config.yaml
@@ -63,7 +63,7 @@ product: # many products can be
supported in tests (pro
cores: # Cores configuration
core0: # Main core (core 0)
name: '' # Core name
- device: '' # Choose a device: sim, qemu, renode, serial
+ device: '' # Choose a device: sim, qemu, renode,
serial, netdev
exec_path: '' # Path of emulator execution for QEMU
targets eg: `qemu-system-x86_64`
# Renode launcher for renode targets, eg:
`renode`
# Path of serial port for serial targets,
eg: `/dev/ttyACM0`
@@ -99,6 +99,13 @@ product: # many products can be
supported in tests (pro
# Keep false for the default byte-wise
behavior.
# Enable for sim or a serial transport with
flow control.
# Target serial RX buffer must hold a whole
command line.
+ netconsole: # (optional, required for netdev) NuttX
netdev console instead of
+ type: 'udp' # the sim/qemu pty or for real HW: 'udp' or
'isotp'
+ port: 55000 # udp: CONFIG_CONSOLE_NETDEV_PORT, target
address is learned
+ bind: '0.0.0.0' # udp: (optional) host address to listen on
+ chunk: 128 # (optional) bytes per datagram (<
CONFIG_CONSOLE_NETDEV_RXBUFSIZE)
+ # isotp keys: interface (can0), rx_id
(target TX ID = PORT),
+ # tx_id (target RX ID, rx_id + 1), fd (CAN
FD, false)
dcmake: # (optional) Defines passed to CMake build
DEFINE1: "VALUE1"
DEFINE2: "VALUE2"
diff --git a/src/ntfc/coreconfig.py b/src/ntfc/coreconfig.py
index ed06769..fa04723 100644
--- a/src/ntfc/coreconfig.py
+++ b/src/ntfc/coreconfig.py
@@ -200,6 +200,16 @@ class CoreConfig:
"""Return whether commands are sent in one transport write."""
return bool(self._config.get("line_buffered", False))
+ @property
+ def netconsole(self) -> Optional[Dict[str, Any]]:
+ """Return the netdev console section, or None to use the pty."""
+ cfg = self._config.get("netconsole", None)
+ if cfg is None:
+ return None
+ if not isinstance(cfg, dict):
+ raise TypeError("netconsole must be a mapping")
+ return cfg
+
def kv_check(self, cfg: str) -> Any:
"""Check Kconfig option and return its value.
diff --git a/src/ntfc/device/getdev.py b/src/ntfc/device/getdev.py
index 7156274..742ccf6 100644
--- a/src/ntfc/device/getdev.py
+++ b/src/ntfc/device/getdev.py
@@ -22,6 +22,7 @@
from typing import TYPE_CHECKING, Callable, Dict
+from .netdev import DeviceNetdev
from .qemu import DeviceQemu
from .renode import DeviceRenode
from .serial import DeviceSerial
@@ -38,6 +39,7 @@ _DEVICE_FACTORIES: Dict[str, Callable[["CoreConfig"],
"DeviceCommon"]] = {
"qemu": DeviceQemu,
"serial": DeviceSerial,
"renode": DeviceRenode,
+ "netdev": DeviceNetdev,
}
###############################################################################
diff --git a/src/ntfc/device/host.py b/src/ntfc/device/host.py
index 2aed060..6d06f51 100644
--- a/src/ntfc/device/host.py
+++ b/src/ntfc/device/host.py
@@ -31,6 +31,7 @@ import psutil # type: ignore
from ntfc.log.logger import logger
from .common import DeviceCommon
+from .netconsole import get_netconsole
if TYPE_CHECKING:
from ntfc.coreconfig import CoreConfig
@@ -55,6 +56,8 @@ class DeviceHost(DeviceCommon):
self._child = None
self._cwd = conf.exec_cwd
self._cmd: Optional[List[str]] = None
+ # console over a network transport instead of the child pty
+ self._netcon = get_netconsole(conf.netconsole)
@property
def pid(self) -> Optional[int]:
@@ -81,6 +84,9 @@ class DeviceHost(DeviceCommon):
if not self._child.isalive():
return False
+ if self._netcon and not self._netcon.is_open:
+ return False
+
return True
def _dev_reopen(self) -> pexpect.spawn:
@@ -102,6 +108,10 @@ class DeviceHost(DeviceCommon):
if data[-1] != ord("\n"):
data += self.NEWLINE_PAD
+ if self._netcon:
+ self._netcon.write(data)
+ return
+
if self._conf.line_buffered:
self._child.send(data)
return
@@ -116,6 +126,11 @@ class DeviceHost(DeviceCommon):
return
assert self._child
+
+ if self._netcon:
+ self._netcon.write(bytes([ord(c.upper()) & 0x1F]))
+ return
+
self._child.sendcontrol(c)
def _read(self) -> bytes: # pragma: no cover
@@ -125,10 +140,18 @@ class DeviceHost(DeviceCommon):
try:
assert self._child
- return self._child.read_nonblocking(size=5120, timeout=0)
+ data = self._child.read_nonblocking(size=5120, timeout=0)
except (pexpect.TIMEOUT, pexpect.EOF):
- return b""
+ data = b""
+
+ if self._netcon:
+ # the child pty is not the console: keep its output in the log
+ if data:
+ logger.debug(f"{self.name}: {data!r}")
+ return self._netcon.read()
+
+ return data
def _kill_process_group(self, process: pexpect.spawn) -> None:
"""Kill process group."""
@@ -165,6 +188,10 @@ class DeviceHost(DeviceCommon):
# we need command to reopen file in the case of crash
self._cmd = cmd
+ if self._netcon:
+ # listen before the target boots, it sends its console at once
+ self._netcon.start()
+
logger.info(f"spawn cmd: {''.join(cmd)}")
child = pexpect.spawn(
"".join(cmd), timeout=10, maxread=20000, cwd=self._cwd
@@ -199,6 +226,8 @@ class DeviceHost(DeviceCommon):
def _stop_impl(self) -> None:
"""Stop host device and kill the underlying process."""
if not self._child:
+ if self._netcon:
+ self._netcon.stop()
return
if self._child.isalive(): # pragma: no cover
@@ -212,6 +241,9 @@ class DeviceHost(DeviceCommon):
self._child = None
+ if self._netcon:
+ self._netcon.stop()
+
logger.info("host device closed")
@property
diff --git a/src/ntfc/device/netconsole.py b/src/ntfc/device/netconsole.py
new file mode 100644
index 0000000..0f9e23f
--- /dev/null
+++ b/src/ntfc/device/netconsole.py
@@ -0,0 +1,260 @@
+############################################################################
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+"""Host side of the NuttX netdev console (CONFIG_CONSOLE_NETDEV).
+
+The NuttX console is carried over a network transport instead of the
+emulator's serial port. UDP and CAN ISO-TP are supported; both are
+datagram transports, so writes are split into datagrams that fit the
+console receive buffer of the target.
+"""
+
+import select
+import socket
+import struct
+from typing import Any, Dict, Optional, Tuple
+
+from ntfc.log.logger import logger
+
+###############################################################################
+# Class: NetConsole
+###############################################################################
+
+
+class NetConsole:
+ """Common interface of a host netdev console transport."""
+
+ # bytes per datagram, below the default CONSOLE_NETDEV_RXBUFSIZE (256)
+ DEFAULT_CHUNK = 128
+ RECV_SIZE = 4096
+ # wait for the previous datagram to leave (ISO-TP sends one at a time)
+ SEND_TIMEOUT = 2.0
+
+ def __init__(self, cfg: Dict[str, Any]):
+ """Initialize the transport.
+
+ :param cfg: ``netconsole`` section of the core configuration
+ """
+ self._cfg = cfg
+ self._sock: Optional[socket.socket] = None
+ self._chunk = int(cfg.get("chunk", self.DEFAULT_CHUNK))
+ if self._chunk <= 0:
+ raise ValueError("netconsole chunk must be positive")
+
+ @property
+ def is_open(self) -> bool:
+ """Return True if the transport socket is open."""
+ return self._sock is not None
+
+ def _socket(self) -> socket.socket: # pragma: no cover
+ """Create and bind the transport socket."""
+ raise NotImplementedError
+
+ def _send(self, data: bytes) -> None: # pragma: no cover
+ """Send one datagram to the target."""
+ raise NotImplementedError
+
+ def _recv(self) -> bytes:
+ """Receive one datagram from the target."""
+ assert self._sock
+ return self._sock.recv(self.RECV_SIZE)
+
+ def start(self) -> None:
+ """Start the transport. Must be done before the target boots."""
+ if self._sock:
+ return
+ sock = self._socket()
+ sock.setblocking(False)
+ self._sock = sock
+
+ def stop(self) -> None:
+ """Stop the transport."""
+ if self._sock:
+ self._sock.close()
+ self._sock = None
+
+ def read(self) -> bytes:
+ """Read the console data that is available now."""
+ if not self._sock:
+ return b""
+
+ data = b""
+ while True:
+ ready, _, _ = select.select([self._sock], [], [], 0)
+ if not ready:
+ break
+ try:
+ data += self._recv()
+ except (BlockingIOError, InterruptedError):
+ break
+ except OSError as e:
+ logger.warning(f"netconsole: receive failed: {e}")
+ break
+ return data
+
+ def write(self, data: bytes) -> None:
+ """Write console data, one datagram per chunk."""
+ if not self._sock:
+ return
+
+ for i in range(0, len(data), self._chunk):
+ _, ready, _ = select.select(
+ [], [self._sock], [], self.SEND_TIMEOUT
+ )
+ if not ready:
+ logger.warning("netconsole: send timeout, dropped")
+ return
+ self._send(data[i : i + self._chunk])
+
+
+###############################################################################
+# Class: NetConsoleUdp
+###############################################################################
+
+
+class NetConsoleUdp(NetConsole):
+ """UDP netdev console.
+
+ NTFC listens on ``port`` (CONFIG_CONSOLE_NETDEV_PORT) for the datagrams
+ the target sends to CONFIG_CONSOLE_NETDEV_IPADDR and replies to the
+ address the last datagram came from.
+ """
+
+ DEFAULT_PORT = 55000
+
+ def __init__(self, cfg: Dict[str, Any]):
+ """Initialize the UDP transport.
+
+ :param cfg: ``netconsole`` section of the core configuration
+ """
+ NetConsole.__init__(self, cfg)
+ self._bind = str(cfg.get("bind", "0.0.0.0"))
+ self._port = int(cfg.get("port", self.DEFAULT_PORT))
+ self._peer: Optional[Tuple[str, int]] = None
+
+ def _socket(self) -> socket.socket:
+ """Create the UDP socket bound to the console port."""
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ sock.bind((self._bind, self._port))
+ logger.info(f"netconsole: UDP on {self._bind}:{self._port}")
+ return sock
+
+ def _recv(self) -> bytes:
+ """Receive one datagram and remember where it came from."""
+ assert self._sock
+ data, addr = self._sock.recvfrom(self.RECV_SIZE)
+ if self._peer != (addr[0], addr[1]):
+ logger.info(f"netconsole: target is {addr[0]}:{addr[1]}")
+ self._peer = (addr[0], addr[1])
+ return data
+
+ def _send(self, data: bytes) -> None:
+ """Send one datagram to the target."""
+ assert self._sock
+ if self._peer is None:
+ logger.warning("netconsole: target address unknown, dropped")
+ return
+ self._sock.sendto(data, self._peer)
+
+
+###############################################################################
+# Class: NetConsoleIsotp
+###############################################################################
+
+
+class NetConsoleIsotp(NetConsole):
+ """CAN ISO-TP netdev console (Linux ``can_isotp``).
+
+ ``rx_id`` is the CAN ID the target sends on (CONFIG_CONSOLE_NETDEV_PORT)
+ and ``tx_id`` the CAN ID it receives on (CONFIG_CONSOLE_NETDEV_PORT + 1).
+ With ``fd: true`` the host uses CAN FD frames like a target built with
+ CONFIG_NET_CAN_CANFD.
+ """
+
+ DEFAULT_RX_ID = 2000
+ SOL_CAN_ISOTP = 106 # SOL_CAN_BASE (100) + CAN_ISOTP (6)
+ CAN_ISOTP_LL_OPTS = 5
+ CANFD_MTU = 72
+ CANFD_MAX_DLEN = 64
+
+ def __init__(self, cfg: Dict[str, Any]):
+ """Initialize the ISO-TP transport.
+
+ :param cfg: ``netconsole`` section of the core configuration
+ """
+ NetConsole.__init__(self, cfg)
+ self._ifname = str(cfg.get("interface", "can0"))
+ self._rx_id = int(cfg.get("rx_id", self.DEFAULT_RX_ID))
+ self._tx_id = int(cfg.get("tx_id", self._rx_id + 1))
+ self._fd = bool(cfg.get("fd", False))
+
+ def _socket(self) -> socket.socket:
+ """Create the ISO-TP socket bound to the console CAN IDs."""
+ proto = getattr(socket, "CAN_ISOTP", 6)
+ sock = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, proto)
+ try:
+ if self._fd:
+ opts = struct.pack(
+ "=BBB", self.CANFD_MTU, self.CANFD_MAX_DLEN, 0
+ )
+ sock.setsockopt(
+ self.SOL_CAN_ISOTP, self.CAN_ISOTP_LL_OPTS, opts
+ )
+ sock.bind((self._ifname, self._rx_id, self._tx_id))
+ except OSError:
+ sock.close()
+ raise
+ logger.info(
+ f"netconsole: ISO-TP on {self._ifname} "
+ f"rx=0x{self._rx_id:x} tx=0x{self._tx_id:x}"
+ )
+ return sock
+
+ def _send(self, data: bytes) -> None:
+ """Send one ISO-TP message to the target."""
+ assert self._sock
+ self._sock.send(data)
+
+
+###############################################################################
+# Function: get_netconsole
+###############################################################################
+
+_NETCONSOLES = {
+ "udp": NetConsoleUdp,
+ "isotp": NetConsoleIsotp,
+}
+
+
+def get_netconsole(cfg: Optional[Dict[str, Any]]) -> Optional[NetConsole]:
+ """Return the netdev console transport for a ``netconsole`` section.
+
+ :param cfg: ``netconsole`` section of the core configuration or None
+ :return: transport instance or None if the section is not given
+ """
+ if not isinstance(cfg, dict) or not cfg:
+ return None
+
+ kind = cfg.get("type")
+ factory = _NETCONSOLES.get(str(kind))
+ if factory is None:
+ raise ValueError(f"unsupported netconsole type: {kind}")
+ return factory(cfg)
diff --git a/src/ntfc/device/netdev.py b/src/ntfc/device/netdev.py
new file mode 100644
index 0000000..c396593
--- /dev/null
+++ b/src/ntfc/device/netdev.py
@@ -0,0 +1,138 @@
+############################################################################
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+"""Device whose only console is a NuttX netdev console (real hardware)."""
+
+from typing import TYPE_CHECKING
+
+from ntfc.log.logger import logger
+
+from .common import DeviceCommon
+from .netconsole import NetConsole, get_netconsole
+
+if TYPE_CHECKING:
+ from ntfc.coreconfig import CoreConfig
+
+###############################################################################
+# Class: DeviceNetdev
+###############################################################################
+
+
+class DeviceNetdev(DeviceCommon):
+ """Target reached only through its netdev console.
+
+ This is the device for real hardware built with CONFIG_CONSOLE_NETDEV:
+ NTFC does not spawn anything, it talks to the board over the transport
+ of the ``netconsole`` section. The board is reset and powered off with
+ the ``reboot`` and ``poweroff`` system commands of the configuration,
+ as for a serial device. The transport is started before the reset so
+ the boot output, which also gives NTFC the UDP target address, is not
+ lost.
+ """
+
+ def __init__(self, conf: "CoreConfig"):
+ """Initialize the netdev console device.
+
+ :param conf: configuration handler
+ """
+ DeviceCommon.__init__(self, conf, echo=False)
+ con = get_netconsole(conf.netconsole)
+ if con is None:
+ raise ValueError("netdev device needs a netconsole section")
+ self._con: NetConsole = con
+
+ @property
+ def name(self) -> str:
+ """Get device name."""
+ return "netdev"
+
+ @property
+ def notalive(self) -> bool:
+ """Check if the device is dead."""
+ return not self._con.is_open
+
+ def _dev_is_health_priv(self) -> bool:
+ """Check if the console transport is open."""
+ return self._con.is_open
+
+ def _write(self, data: bytes) -> None:
+ """Write a command to the console."""
+ if not self.dev_is_health():
+ return
+
+ if data[-1] != ord("\n"):
+ data += b"\n"
+
+ self._con.write(data)
+
+ def _write_ctrl(self, c: str) -> None:
+ """Write a control character to the console."""
+ if not self.dev_is_health():
+ return
+
+ self._con.write(bytes([ord(c.upper()) & 0x1F]))
+
+ def _read(self) -> bytes:
+ """Read console data."""
+ if not self.dev_is_health():
+ return b""
+
+ return self._con.read()
+
+ def _start_impl(self) -> None:
+ """Start the transport, reset the board and wait for the prompt."""
+ self._con.start()
+ self.reboot()
+ ret = self._wait_for_boot(self._conf.boot_timeout)
+ if ret is False:
+ raise TimeoutError("device boot timeout")
+
+ def _stop_impl(self) -> None:
+ """Stop the console transport."""
+ self._con.stop()
+ logger.info("netdev device closed")
+
+ def _poweroff_impl(self) -> bool:
+ """Hardware poweroff: run the poweroff command from config.
+
+ :return: (bool) True on success, False if no poweroff command
+ configured.
+ """
+ if self._conf.poweroff:
+ logger.info("poweroff core")
+ self._system_cmd(self._conf.poweroff)
+ self.clear_fault_flags()
+ return True
+ return False
+
+ def _reboot_impl(self, timeout: int = 1) -> bool:
+ """Hardware reboot: run the reboot command from config.
+
+ :param timeout: (int) Unused for netdev devices.
+ :return: (bool) True on success, False if no reboot command
+ configured.
+ """
+ del timeout
+ if self._conf.reboot:
+ logger.info("reboot core")
+ self._system_cmd(self._conf.reboot)
+ self.clear_fault_flags()
+ return True
+ return False
diff --git a/tests/device/test_host.py b/tests/device/test_host.py
index 65aeb4e..84313ea 100644
--- a/tests/device/test_host.py
+++ b/tests/device/test_host.py
@@ -18,6 +18,7 @@
#
############################################################################
+import socket
from unittest.mock import MagicMock
import pytest
@@ -289,3 +290,43 @@ def test_device_host_post_spawn_hook(envconfig_dummy,
monkeypatch):
assert order == ["post_spawn", "boot", "post_spawn", "boot"]
dev.stop()
+
+
+def test_device_host_netconsole(envconfig_dummy, monkeypatch):
+ """The console runs over a UDP netdev console, not the child pty."""
+
+ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
+ sock.bind(("127.0.0.1", 0))
+ port = sock.getsockname()[1]
+
+ conf = envconfig_dummy.product[0].cfg_core(0)
+ conf._config["prompt"] = "nsh> "
+ conf._config["netconsole"] = {
+ "type": "udp",
+ "bind": "127.0.0.1",
+ "port": port,
+ }
+ target = "./tests/resources/netconsole/udp_target.py"
+ dev = DeviceHost2(conf)
+ dev._write = DeviceHost._write.__get__(dev)
+
+ # the transport is closed on stop even if the device never started
+ dev._netcon.start()
+ dev.stop()
+ assert dev._netcon.is_open is False
+
+ assert dev.host_open([f"python3 {target} {port}"]) is not None
+ assert dev._netcon.is_open is True
+ assert dev._dev_is_health_priv() is True
+
+ ret = dev.send_command(b"hello", 1)
+ assert b"Hello, World!" in ret
+ assert dev.send_ctrl_cmd("C") == 0
+
+ # a closed transport makes the device unhealthy
+ dev._netcon.stop()
+ assert dev._dev_is_health_priv() is False
+ dev._netcon.start()
+
+ dev.stop()
+ assert dev._netcon.is_open is False
diff --git a/tests/device/test_netconsole.py b/tests/device/test_netconsole.py
new file mode 100644
index 0000000..77acf58
--- /dev/null
+++ b/tests/device/test_netconsole.py
@@ -0,0 +1,249 @@
+############################################################################
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+import socket
+import struct
+import time
+
+import pytest
+
+from ntfc.coreconfig import CoreConfig
+from ntfc.device import netconsole
+from ntfc.device.netconsole import (
+ NetConsoleIsotp,
+ NetConsoleUdp,
+ get_netconsole,
+)
+
+
+def _wait_read(con, timeout=2.0):
+ deadline = time.monotonic() + timeout
+ data = b""
+ while time.monotonic() < deadline:
+ data += con.read()
+ if data:
+ return data
+ time.sleep(0.01)
+ return data
+
+
+def test_netconsole_config():
+ assert CoreConfig({"name": "t"}).netconsole is None
+
+ cfg = {"type": "udp", "port": 1234}
+ assert CoreConfig({"name": "t", "netconsole": cfg}).netconsole == cfg
+
+ with pytest.raises(TypeError):
+ _ = CoreConfig({"name": "t", "netconsole": "udp"}).netconsole
+
+
+def test_get_netconsole():
+ assert get_netconsole(None) is None
+ assert get_netconsole({}) is None
+ assert isinstance(get_netconsole({"type": "udp"}), NetConsoleUdp)
+ assert isinstance(get_netconsole({"type": "isotp"}), NetConsoleIsotp)
+
+ with pytest.raises(ValueError):
+ get_netconsole({"type": "tcp"})
+
+ with pytest.raises(ValueError):
+ get_netconsole({"type": "udp", "chunk": 0})
+
+
+def test_netconsole_udp():
+ con = NetConsoleUdp({"type": "udp", "bind": "127.0.0.1", "port": 0})
+
+ # closed transport is a no-op
+ assert con.is_open is False
+ assert con.read() == b""
+ assert con.write(b"x") is None
+ con.stop()
+
+ con.start()
+ assert con.is_open is True
+ # second open keeps the socket
+ sock = con._sock
+ con.start()
+ assert con._sock is sock
+
+ port = con._sock.getsockname()[1]
+ target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ target.bind(("127.0.0.1", 0))
+ target.settimeout(2)
+
+ # no data yet, and nowhere to send to
+ assert _wait_read(con, 0.05) == b""
+ con.write(b"dropped\n")
+
+ # the target speaks first, the host learns its address
+ target.sendto(b"nsh> ", ("127.0.0.1", port))
+ assert _wait_read(con) == b"nsh> "
+ assert con._peer == target.getsockname()
+
+ # writes are split into chunks
+ con._chunk = 4
+ con.write(b"hello\n")
+ assert target.recv(64) == b"hell"
+ assert target.recv(64) == b"o\n"
+
+ target.close()
+ con.stop()
+ assert con.is_open is False
+
+
+def test_netconsole_read_errors(monkeypatch):
+ con = NetConsoleUdp({"type": "udp", "bind": "127.0.0.1", "port": 0})
+ con.start()
+ monkeypatch.setattr(
+ netconsole.select, "select", lambda r, w, x, t: (r, w, x)
+ )
+
+ def would_block():
+ raise BlockingIOError
+
+ monkeypatch.setattr(con, "_recv", would_block)
+ assert con.read() == b""
+
+ def failed():
+ raise ConnectionRefusedError
+
+ monkeypatch.setattr(con, "_recv", failed)
+ assert con.read() == b""
+ con.stop()
+
+
+def test_netconsole_write_timeout(monkeypatch):
+ con = NetConsoleUdp({"type": "udp", "bind": "127.0.0.1", "port": 0})
+ con.start()
+ con._peer = ("127.0.0.1", 9)
+ sent = []
+ monkeypatch.setattr(con, "_send", sent.append)
+
+ # the previous datagram never left: the write is dropped
+ monkeypatch.setattr(
+ netconsole.select, "select", lambda r, w, x, t: ([], [], [])
+ )
+ con.write(b"help\n")
+ assert sent == []
+ con.stop()
+
+
+class FakeIsotpSocket:
+ def __init__(self, family, kind, proto, fail_bind=False):
+ self.args = (family, kind, proto)
+ self.opts = []
+ self.bound = None
+ self.sent = []
+ self.closed = False
+ self.blocking = True
+ self.fail_bind = fail_bind
+ self.rx = [b"nsh> "]
+
+ def setsockopt(self, level, opt, value):
+ self.opts.append((level, opt, value))
+
+ def bind(self, addr):
+ if self.fail_bind:
+ raise OSError("no such device")
+ self.bound = addr
+
+ def setblocking(self, flag):
+ self.blocking = flag
+
+ def send(self, data):
+ self.sent.append(data)
+
+ def recv(self, size):
+ return self.rx.pop(0)
+
+ def close(self):
+ self.closed = True
+
+
+def test_netconsole_isotp(monkeypatch):
+ socks = []
+
+ def factory(family, kind, proto=0):
+ sock = FakeIsotpSocket(family, kind, proto)
+ socks.append(sock)
+ return sock
+
+ monkeypatch.setattr(netconsole.socket, "socket", factory)
+ monkeypatch.setattr(
+ netconsole.select,
+ "select",
+ lambda r, w, x, t: ([s for s in r if s.rx], w, x),
+ )
+
+ # classic CAN with the default CAN IDs
+ con = get_netconsole({"type": "isotp"})
+ con.start()
+ sock = socks[-1]
+ assert sock.args[1] == socket.SOCK_DGRAM
+ assert sock.bound == ("can0", 2000, 2001)
+ assert sock.opts == []
+ assert sock.blocking is False
+
+ assert con.read() == b"nsh> "
+ con._chunk = 3
+ con.write(b"help\n")
+ assert sock.sent == [b"hel", b"p\n"]
+ con.stop()
+ assert sock.closed is True
+
+ # CAN FD link layer
+ con = get_netconsole(
+ {
+ "type": "isotp",
+ "interface": "vcan1",
+ "rx_id": 0x123,
+ "tx_id": 0x321,
+ "fd": True,
+ }
+ )
+ con.start()
+ sock = socks[-1]
+ assert sock.bound == ("vcan1", 0x123, 0x321)
+ assert sock.opts == [
+ (
+ NetConsoleIsotp.SOL_CAN_ISOTP,
+ NetConsoleIsotp.CAN_ISOTP_LL_OPTS,
+ struct.pack("=BBB", 72, 64, 0),
+ )
+ ]
+ con.stop()
+
+
+def test_netconsole_isotp_bind_error(monkeypatch):
+ socks = []
+
+ def factory(family, kind, proto=0):
+ sock = FakeIsotpSocket(family, kind, proto, fail_bind=True)
+ socks.append(sock)
+ return sock
+
+ monkeypatch.setattr(netconsole.socket, "socket", factory)
+
+ con = get_netconsole({"type": "isotp"})
+ with pytest.raises(OSError):
+ con.start()
+
+ assert socks[-1].closed is True
+ assert con.is_open is False
diff --git a/tests/device/test_netdev.py b/tests/device/test_netdev.py
new file mode 100644
index 0000000..e5bdfde
--- /dev/null
+++ b/tests/device/test_netdev.py
@@ -0,0 +1,104 @@
+############################################################################
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+import socket
+import subprocess
+import sys
+
+import pytest
+
+from ntfc.coreconfig import CoreConfig
+from ntfc.device.getdev import get_device
+from ntfc.device.netdev import DeviceNetdev
+
+TARGET = "./tests/resources/netconsole/udp_target.py"
+
+
+def _port():
+ with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
+ sock.bind(("127.0.0.1", 0))
+ return sock.getsockname()[1]
+
+
+def _conf(port, **extra):
+ cfg = {
+ "name": "t",
+ "device": "netdev",
+ "prompt": "nsh> ",
+ "boot_timeout": 5,
+ "netconsole": {"type": "udp", "bind": "127.0.0.1", "port": port},
+ }
+ cfg.update(extra)
+ return CoreConfig(cfg)
+
+
+def test_device_netdev_needs_netconsole():
+ with pytest.raises(ValueError):
+ DeviceNetdev(CoreConfig({"name": "t", "device": "netdev"}))
+
+
+def test_device_netdev(monkeypatch):
+ port = _port()
+ dev = get_device(_conf(port))
+ assert isinstance(dev, DeviceNetdev)
+ assert dev.name == "netdev"
+
+ # not started: unhealthy, I/O is a no-op, no reboot/poweroff command
+ assert dev.notalive is True
+ assert dev._dev_is_health_priv() is False
+ assert dev._read() == b""
+ assert dev._write(b"a") is None
+ assert dev._write_ctrl("C") is None
+ assert dev._reboot_impl() is False
+ assert dev._poweroff_impl() is False
+
+ # the board boots after the transport is up, like after a reset
+ dev._con.start()
+ target = subprocess.Popen([sys.executable, TARGET, str(port)])
+ try:
+ dev.start()
+ assert dev.notalive is False
+
+ ret = dev.send_command(b"hello", 1)
+ assert b"Hello, World!" in ret
+ assert dev.send_ctrl_cmd("C") == 0
+ finally:
+ dev.stop()
+ target.kill()
+ target.wait()
+
+ assert dev.notalive is True
+
+
+def test_device_netdev_boot_timeout():
+ dev = get_device(_conf(_port(), boot_timeout=1))
+ with pytest.raises(TimeoutError):
+ dev.start()
+ dev.stop()
+
+
+def test_device_netdev_system_commands(monkeypatch):
+ dev = get_device(_conf(_port(), reboot="reset-board", poweroff="off"))
+ cmds = []
+ monkeypatch.setattr(dev, "_system_cmd", cmds.append)
+
+ assert dev._reboot_impl() is True
+ assert dev._poweroff_impl() is True
+ assert cmds == ["reset-board", "off"]
diff --git a/tests/resources/netconsole/udp_target.py
b/tests/resources/netconsole/udp_target.py
new file mode 100755
index 0000000..fd6314a
--- /dev/null
+++ b/tests/resources/netconsole/udp_target.py
@@ -0,0 +1,44 @@
+############################################################################
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership. The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+"""Fake NuttX target with its NSH console on a UDP netdev console."""
+
+import socket
+import sys
+
+host = ("127.0.0.1", int(sys.argv[1]))
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind(("127.0.0.1", 0))
+
+# output on the pty is not the console
+print("netconsole target up", flush=True)
+
+sock.sendto(b"\nNuttShell (NSH)\nnsh> ", host)
+line = b""
+while True:
+ line += sock.recv(4096)
+ while b"\n" in line:
+ cmd, line = line.split(b"\n", 1)
+ out = cmd + b"\n"
+ if cmd.strip() == b"hello":
+ out += b"Hello, World!\n"
+ elif cmd and cmd[0] == 0x03:
+ out = b"^C\n"
+ sock.sendto(out + b"nsh> ", host)