diff --git a/docs/devices/cardputer-zero.md b/docs/devices/cardputer-zero.md index 17332ec..ce8e21a 100644 --- a/docs/devices/cardputer-zero.md +++ b/docs/devices/cardputer-zero.md @@ -259,7 +259,7 @@ requires the switch chip. MeshTerm's own wiring for the Cap does not require it. `pi4io_required = false` in your profile, or remove the `pi4io_bus` line from it (refer to [Board wiring](uconsole.md#board-wiring)). -**"No SX126x answered on the SPI bus"** means that no radio answered. Turn the Cardputer +**"No radio answered on the SPI bus"** means what it says. Turn the Cardputer Zero off, push the Cap fully onto the header at the back, and check that no row of pins is left over at either end. Then turn it on again. Never add or remove a Cap while the device is on. diff --git a/meshterm/core/radionode.py b/meshterm/core/radionode.py index 8746b8c..51d6014 100644 --- a/meshterm/core/radionode.py +++ b/meshterm/core/radionode.py @@ -475,6 +475,12 @@ def drive_pi4io( return True +#: What the user reads when no radio answered on the bus, however the node found out. The +#: three ways (the chip did not start, it did not answer a register read, it answered as +#: something else) are one thing to the user, and :func:`explain` adds the question about +#: the attachment. The log keeps which of the three it was. +NO_RADIO = "no radio answered on the SPI bus" + #: The register where an SX126x holds its version string, how many bytes of it to read, #: and the first bytes of what the string must be. A bus with no chip on it reads as all #: ones or all zeros, and neither starts with these letters. @@ -498,14 +504,39 @@ def confirm_sx126x(lora: Any) -> None: try: signature = bytes(lora.readRegister(_VERSION_REGISTER, _VERSION_LENGTH)) except Exception as exc: # noqa: BLE001 - any failure here means that nothing answered - raise NodeError("absent", f"the radio did not answer a register read ({exc})") from None + log.warning("the version register could not be read: %s", exc) + raise NodeError("absent", NO_RADIO) from None if not signature.startswith(_SX126X): log.warning("the version register reads %r, which is not an SX126x", signature) - raise NodeError("absent", "no SX126x answered on the SPI bus") + raise NodeError("absent", NO_RADIO) name = signature.split(bytes(1))[0].decode("ascii", "replace") log.info("the chip reports itself as %s", name) +def begin_failure(kind: str, detail: str, expander: bool | None) -> NodeError: + """The error that a failed ``radio.begin()`` gives the user. + + Usually this is the kind that the log lines of the library give, in the words of the + library (refer to :func:`classify_begin_failure`). A board whose expander did not + answer is the exception. Nothing has answered anywhere on that board, so the first + answer the user needs is that no radio is there. The words of the library stay in the + log. A busy line keeps its own kind, because a line that another program holds is a + cause of its own and has its own correction. + + Args: + kind: What :func:`classify_begin_failure` made of the lines of the library. + detail: The last line of the library. + expander: What :func:`prepare_board` found (``False`` for an absent one). + + Returns: + The error to raise. + """ + if expander is False and kind == "failed": + log.warning("the radio did not start: %s", detail) + return NodeError("absent", NO_RADIO) + return NodeError(kind, detail) + + @dataclasses.dataclass(frozen=True) class PreparedBoard: """What :func:`prepare_board` switched on, and what it found there. @@ -915,7 +946,7 @@ async def _serve( # noqa: PLR0913 - the library modules run_node imported, hand pass if kind != "busy" or attempt == BEGIN_ATTEMPTS - 1: detail = recent.lines[-1] if recent.lines else "radio.begin() failed" - raise NodeError(kind, detail) + raise begin_failure(kind, detail, expander) await asyncio.sleep(BEGIN_BACKOFF_S * (attempt + 1)) logging.getLogger().removeHandler(recent) log.info("radio ready") diff --git a/tests/test_radionode.py b/tests/test_radionode.py index 5aa5b59..adf392a 100644 --- a/tests/test_radionode.py +++ b/tests/test_radionode.py @@ -247,6 +247,26 @@ def test_a_bus_with_nothing_on_it_is_not_a_board(answer: bytes) -> None: assert err.value.kind == "absent" +@pytest.mark.parametrize( + ("expander", "kind", "expected"), + [ + (False, "failed", "absent"), + (False, "busy", "busy"), + (True, "failed", "failed"), + (None, "failed", "failed"), + ], + ids=["no-expander", "busy-line", "with-expander", "no-expander-in-wiring"], +) +def test_a_radio_that_does_not_start_says_the_useful_thing( + expander: bool | None, kind: str, expected: str +) -> None: + """On a board where nothing answered at all, the user must read that nothing is there.""" + error = radionode.begin_failure(kind, "Something went wrong, check your wiring!", expander) + assert error.kind == expected + if expected == "absent": + assert str(error) == radionode.NO_RADIO, "one sentence for a radio that is not there" + + def test_a_chip_that_cannot_be_read_is_not_a_board() -> None: """A read that fails is the same answer as a read of nothing: no board is attached."""