diff --git a/meshterm/core/connection.py b/meshterm/core/connection.py index 4db0ec2..547d8f9 100644 --- a/meshterm/core/connection.py +++ b/meshterm/core/connection.py @@ -2400,8 +2400,12 @@ class MockDevice(Device): async def get_device_info(self) -> dict: # noqa: D102 - inherited docstring # The simulator reports a stable, obviously-synthetic model so the hardware column - # renders identically to a real board without pretending to be one. - return {"model": "MeshCore Simulator", "ver": "mock", "fw_build": "mock"} + # renders identically to a real board without pretending to be one. ``ble_pin`` + # rides here rather than in SELF_INFO because that is where real firmware puts it. + return { + "model": "MeshCore Simulator", "ver": "mock", "fw_build": "mock", + "ble_pin": self._device_pin, + } async def get_contacts(self) -> list[Contact]: # noqa: D102 - inherited docstring return list(self._contacts) diff --git a/meshterm/core/device_config.py b/meshterm/core/device_config.py index aa3ae6c..b016e21 100644 --- a/meshterm/core/device_config.py +++ b/meshterm/core/device_config.py @@ -227,12 +227,34 @@ async def build_snapshot( snapshot["flood_scope"] = await device.get_default_flood_scope() except Exception: # noqa: BLE001 - optional read; absence is acceptable pass + # The device-query frame is a *different* payload from SELF_INFO, and some settings + # only exist there — the BLE pairing code among them, which real firmware reports as + # ``ble_pin`` and never puts in SELF_INFO. Without this the Device PIN row could only + # ever render "?" on hardware: writable, but with no way to read back what you wrote. + # Merged underneath, so a key SELF_INFO also carries keeps the SELF_INFO value (the + # same precedence ``probe_device`` uses when it folds the two together). + try: + snapshot = {**await device.get_device_info(), **snapshot} + except Exception: # noqa: BLE001 - optional read; absence is acceptable + pass return snapshot # --- coupled-command apply helpers ------------------------------------------- +# Several settings do not have a command of their own: the firmware takes latitude and +# longitude together, and the four radio parameters together, so changing one means +# re-sending its siblings unchanged. Those siblings are read from ``snapshot``, which is +# the caller's picture of the device — and that picture is read *once*, before the first +# write. So each of these appliers records what it just set, and the reason is a bug that +# reached a real device: restoring a backup that differed in both latitude and longitude +# applied ``adv_lat`` (preserving the stale longitude), then ``adv_lon`` (preserving the +# stale *latitude*) — and the second write silently undid the first. Same for a restore +# touching two radio fields. Writing back keeps every later sibling in the same batch +# honest, whether the batch comes from ``config restore`` or the editor's staged changes. + + def _radio_apply(field_name: str) -> Callable[[Device, Any, dict], Awaitable[None]]: """Build an apply that updates one radio field, preserving the others.""" @@ -245,6 +267,7 @@ def _radio_apply(field_name: str) -> Callable[[Device, Any, dict], Awaitable[Non } params[field_name] = value await device.set_radio(params["freq"], params["bw"], params["sf"], params["cr"]) + snapshot[f"radio_{field_name}"] = value return apply @@ -256,6 +279,7 @@ def _coords_apply(field_name: str) -> Callable[[Device, Any, dict], Awaitable[No lat = value if field_name == "adv_lat" else snapshot.get("adv_lat", 0.0) lon = value if field_name == "adv_lon" else snapshot.get("adv_lon", 0.0) await device.set_coords(float(lat or 0.0), float(lon or 0.0)) + snapshot[field_name] = value return apply @@ -267,6 +291,7 @@ def _tuning_apply(field_name: str) -> Callable[[Device, Any, dict], Awaitable[No rx = value if field_name == "rx_delay" else snapshot.get("rx_delay", 0.0) af = value if field_name == "airtime_factor" else snapshot.get("airtime_factor", 0.0) await device.set_tuning(float(rx or 0.0), float(af or 0.0)) + snapshot[field_name] = value return apply @@ -376,9 +401,16 @@ DEVICE_SETTINGS: list[SettingSpec] = [ getter=_get("adv_lon"), apply=_coords_apply("adv_lon"), ), SettingSpec( + # Written as ``device_pin`` (the name the CLI, the backup TOML and ``set_devicepin`` + # all use) but *read* as ``ble_pin``, which is what the firmware calls it in the + # device-query frame — the only place it appears. Keeping the key means existing + # backups still restore; reading the firmware's own name means the row shows a + # value instead of "?" on real hardware. The fallback keeps the canonical key + # working for anything that reports it directly. "device_pin", "Device PIN", "BLE pairing PIN", "Identity", "int", minimum=0, maximum=999999, - getter=_get("device_pin"), apply=lambda d, v, s: d.set_device_pin(v), + getter=lambda snapshot: snapshot.get("ble_pin", snapshot.get("device_pin")), + apply=lambda d, v, s: d.set_device_pin(v), ), # Radio SettingSpec( diff --git a/tests/test_paired_settings.py b/tests/test_paired_settings.py new file mode 100644 index 0000000..45b43b6 --- /dev/null +++ b/tests/test_paired_settings.py @@ -0,0 +1,214 @@ +"""Tests for settings that share one device command, and for the PIN's readback. + +Both were found restoring a reflashed companion from its own backup. + +Latitude and longitude go to the firmware together, and so do the four radio parameters, +so changing one means re-sending its siblings unchanged. Those siblings come from the +caller's snapshot — read once, before the first write. A restore that differed in both +coordinates therefore applied ``adv_lat`` (preserving the stale longitude), then +``adv_lon`` (preserving the stale *latitude*), and the second write silently undid the +first. On the real device that left latitude at 0.0 and the frequency on the factory +default while every op reported success. +""" + +from __future__ import annotations + +import asyncio + +import pytest + +from meshterm.core.device_config import ( + DEVICE_SETTINGS, + build_snapshot, + get_spec, + parse_value, +) + + +class _RecordingDevice: + """Captures the coupled commands, and models the firmware: last write wins.""" + + def __init__(self, **state) -> None: + self.state = dict(state) + self.radio_calls: list[tuple] = [] + self.coord_calls: list[tuple] = [] + self.tuning_calls: list[tuple] = [] + + async def set_radio(self, freq, bw, sf, cr) -> None: + self.radio_calls.append((freq, bw, sf, cr)) + self.state.update(radio_freq=freq, radio_bw=bw, radio_sf=sf, radio_cr=cr) + + async def set_coords(self, lat, lon) -> None: + self.coord_calls.append((lat, lon)) + self.state.update(adv_lat=lat, adv_lon=lon) + + async def set_tuning(self, rx_delay, airtime_factor) -> None: + self.tuning_calls.append((rx_delay, airtime_factor)) + self.state.update(rx_delay=rx_delay, airtime_factor=airtime_factor) + + +async def _apply_all(device, snapshot, changes): + """Apply each change the way a restore or a staged-edit batch does: in sequence.""" + for key, raw in changes: + spec = get_spec(key) + await spec.apply(device, parse_value(spec, raw, snapshot), snapshot) + + +def test_both_coordinates_survive_being_applied_together() -> None: + """THE bug: the second coordinate must not re-send the first one's stale value. + + Restoring a backup onto a factory-fresh node changes both at once. Before the fix the + device ended at ``(0.0, -73.708724)`` — longitude right, latitude silently reverted. + """ + device = _RecordingDevice(adv_lat=0.0, adv_lon=0.0) + snapshot = dict(device.state) + + asyncio.run(_apply_all(device, snapshot, [ + ("adv_lat", "45.535445"), + ("adv_lon", "-73.708724"), + ])) + + assert device.state["adv_lat"] == pytest.approx(45.535445) + assert device.state["adv_lon"] == pytest.approx(-73.708724) + # The last write must carry both, not one real value and one stale zero. + assert device.coord_calls[-1] == pytest.approx((45.535445, -73.708724)) + + +def test_the_order_the_coordinates_are_applied_in_does_not_matter() -> None: + """Neither ordering may lose a value — a plan's op order is not something to rely on.""" + for changes in ( + [("adv_lat", "45.5"), ("adv_lon", "-73.7")], + [("adv_lon", "-73.7"), ("adv_lat", "45.5")], + ): + device = _RecordingDevice(adv_lat=0.0, adv_lon=0.0) + asyncio.run(_apply_all(device, dict(device.state), changes)) + assert (device.state["adv_lat"], device.state["adv_lon"]) == \ + pytest.approx((45.5, -73.7)), changes + + +def test_two_radio_fields_applied_together_both_stick() -> None: + """The frequency reverted to the factory default when the spreading factor followed it.""" + device = _RecordingDevice(radio_freq=869.618, radio_bw=62.5, radio_sf=8, radio_cr=5) + snapshot = dict(device.state) + + asyncio.run(_apply_all(device, snapshot, [ + ("radio_freq", "910.525"), + ("radio_sf", "7"), + ])) + + assert device.state["radio_freq"] == pytest.approx(910.525) + assert device.state["radio_sf"] == 7 + assert device.state["radio_bw"] == pytest.approx(62.5) # untouched siblings preserved + assert device.state["radio_cr"] == 5 + + +def test_all_four_radio_fields_at_once() -> None: + """A full radio change is the worst case: three chances for a sibling to go stale.""" + device = _RecordingDevice(radio_freq=869.618, radio_bw=250.0, radio_sf=8, radio_cr=8) + + asyncio.run(_apply_all(device, dict(device.state), [ + ("radio_freq", "910.525"), ("radio_bw", "62.5"), + ("radio_sf", "7"), ("radio_cr", "5"), + ])) + + assert device.radio_calls[-1] == pytest.approx((910.525, 62.5, 7, 5)) + + +def test_both_tuning_fields_applied_together_both_stick() -> None: + """rx_delay and airtime_factor share a command on the same terms.""" + device = _RecordingDevice(rx_delay=0.0, airtime_factor=0.0) + + asyncio.run(_apply_all(device, dict(device.state), [ + ("rx_delay", "1.5"), ("airtime_factor", "2.0"), + ])) + + assert device.tuning_calls[-1] == pytest.approx((1.5, 2.0)) + + +def test_a_coupled_apply_leaves_the_snapshot_describing_the_device() -> None: + """The write-back is the mechanism, so state it: the snapshot tracks what was set.""" + device = _RecordingDevice(adv_lat=0.0, adv_lon=0.0) + snapshot = dict(device.state) + + asyncio.run(_apply_all(device, snapshot, [("adv_lat", "45.5")])) + + assert snapshot["adv_lat"] == pytest.approx(45.5) + + +# --- the Device PIN's readback ---------------------------------------------------- + + +class _InfoDevice: + """A device whose two info frames carry different keys, as real firmware's do.""" + + def __init__(self, self_info: dict, device_info: dict) -> None: + self._self_info = self_info + self._device_info = device_info + + async def get_self_info(self) -> dict: + return dict(self._self_info) + + async def get_device_info(self) -> dict: + return dict(self._device_info) + + async def get_tuning(self): + raise NotImplementedError + + async def get_path_hash_mode(self): + raise NotImplementedError + + async def get_autoadd_config(self): + raise NotImplementedError + + async def get_default_flood_scope(self): + raise NotImplementedError + + +def test_the_snapshot_folds_in_the_device_query_frame() -> None: + """``ble_pin`` lives only in the device-query payload, so the snapshot must read it.""" + device = _InfoDevice({"name": "Homestead"}, {"ble_pin": 701307, "model": "T1000-E"}) + + snapshot = asyncio.run(build_snapshot(device)) + + assert snapshot["ble_pin"] == 701307 + assert snapshot["model"] == "T1000-E" + assert snapshot["name"] == "Homestead" + + +def test_self_info_wins_where_the_two_frames_overlap() -> None: + """SELF_INFO is the authority on anything it reports; device-query only fills gaps.""" + device = _InfoDevice({"name": "from-self-info"}, {"name": "from-device-query"}) + + assert asyncio.run(build_snapshot(device))["name"] == "from-self-info" + + +def test_the_device_pin_row_reads_the_firmware_s_own_name() -> None: + """It rendered "?" on every real companion: written as device_pin, reported as ble_pin.""" + spec = get_spec("device_pin") + + assert spec.getter({"ble_pin": 701307}) == 701307 + assert spec.getter({"device_pin": 424242}) == 424242 # canonical key still honoured + assert spec.getter({}) is None # genuinely unknown stays unknown + + +def test_a_snapshot_read_failure_still_yields_the_rest() -> None: + """The device-query frame is optional: older firmware simply contributes nothing.""" + + class _NoQuery(_InfoDevice): + async def get_device_info(self): + raise RuntimeError("firmware predates the device query") + + snapshot = asyncio.run(build_snapshot(_NoQuery({"name": "Homestead"}, {}))) + assert snapshot["name"] == "Homestead" + assert get_spec("device_pin").getter(snapshot) is None + + +def test_every_coupled_setting_is_covered_here() -> None: + """A new setting sharing a coupled command must be added to these tests, not forgotten.""" + coupled = { + "adv_lat", "adv_lon", + "radio_freq", "radio_bw", "radio_sf", "radio_cr", + "rx_delay", "airtime_factor", + } + known = {spec.key for spec in DEVICE_SETTINGS} + assert coupled <= known, coupled - known