diff --git a/README.md b/README.md index 07235d2..cf437f8 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,21 @@ Cesty: - **přes BLE konzoli uzlu** (`transport: {type: ble_console, address: }`): nic nevysílá do meshe a projde i plný obraz. Uzel musí být spárovaný na hostu, kde modul běží (konzole chce šifrované spojení), a nesmí k němu být připojený - jiný centrál — připojený uzel neadvertuje. + jiný centrál — připojený uzel neadvertuje; +- **přes rádio, ale mimo sdílený kanál** (`side: "869.525,62.5,7,5"`): + přihlášení a `fw status` jdou meshem, pak uzel dostane `tempradio` na + vedlejší preset, companion se přeladí na stejný a delta jde tam, napřímo + (zero-hop) — companion tedy musí být v přímém dosahu uzlu a musí to být + companion **firmware**, který umí přeladit vlastní rádio. Virtuální companion + openHopu změnu rádia jen potvrdí; modul to pozná a skončí chybou. Uzel se + vrací sám: rebootem po `fw apply`, příkazem na konci, nebo vlastním + časovačem, když se companion ztratí (okno 3 min, po ověření spojení `window`, + obnovuje se). Companion dostane zpátky svůj preset i cestu ke kontaktu; jen + když úlohu zabije `async` timeout, musí se přeladit ručně. + +Celý přenos je v `plugins/module_utils/meshcore_ota_engine.py` a nepotřebuje +Ansible — stejný kód volají i nástroje `tools/ota_mesh_push.py` a +`tools/ota_push.py` v meshcore-tth. Protokol se mění jen tam. Stažení releasu a kontrola podpisu jsou na volajícím (`examples/firmware-ota.yml` to dělá s `ssh-keygen -Y verify`); uzel každý diff --git a/plugins/module_utils/meshcore_console.py b/plugins/module_utils/meshcore_console.py index 9276d4c..238b031 100644 --- a/plugins/module_utils/meshcore_console.py +++ b/plugins/module_utils/meshcore_console.py @@ -1,4 +1,5 @@ -"""The text console of a meshcore-tth repeater over BLE (Nordic UART Service). +"""The text console of a meshcore-tth repeater over BLE (Nordic UART Service), +or over USB serial (SerialConsole, the same calls). A local console: no login, no RF, nothing on air -- the OTA module prefers it wherever a bonded host hears the node. The node echoes each command and answers @@ -27,6 +28,13 @@ try: except ImportError: # reported by the module via missing_required_lib HAS_BLEAK = False +try: + import serial + + HAS_SERIAL = True +except ImportError: + HAS_SERIAL = False + NUS_RX = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" # write (host -> node) NUS_TX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" # notify (node -> host) REPLY = re.compile(rb"-> ?(.*?)\r?\n") @@ -90,3 +98,58 @@ class BleConsole: await asyncio.sleep(0.02) shown = line if len(line) <= 24 else line[:24] + "..." raise ConsoleError(f"no reply to `{shown}` on the BLE console") + + +class SerialConsole: + """The same console on USB (CDC): a port path, preferably /dev/serial/by-id/.""" + + def __init__(self, port): + self.port = port + self.s = None + self.log = [] + + async def open(self): + try: + self.s = serial.Serial(self.port, 115200, timeout=0) + except (OSError, serial.SerialException) as e: + raise ConsoleError(f"cannot open {self.port}: {e}") from None + self.s.dtr = True + self.s.rts = True + await asyncio.sleep(0.9) + # wake the console; over USB the bare line's reply is drained right here + self.s.write(b"\r\n") + await asyncio.sleep(0.4) + self.s.reset_input_buffer() + self.log.append(f"USB console {self.port} open") + + async def close(self): + if self.s is not None: + try: + self.s.close() + except (OSError, serial.SerialException) as e: # after `fw apply` the port is gone already + self.log.append(f"USB close: {type(e).__name__}") + self.s = None + + async def cmd(self, line, timeout=5.0, quiet=False): + """One console command -> the reply after '-> '.""" + self.s.reset_input_buffer() + self.s.write(line.encode() + b"\r\n") + buf = b"" + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + d = self.s.read(4096) + except (OSError, serial.SerialException) as e: + raise ConsoleError(f"USB console: {e}") from None + if d: + buf += d + m = REPLY.search(buf) + if m: + text = m.group(1).decode(errors="replace").strip() + if not quiet: + self.log.append(f"$ {line} -> {text}") + return text + else: + await asyncio.sleep(0.005) + shown = line if len(line) <= 24 else line[:24] + "..." + raise ConsoleError(f"no reply to `{shown}` on the USB console") diff --git a/plugins/module_utils/meshcore_ota.py b/plugins/module_utils/meshcore_ota.py index 66aa789..e43cccb 100644 --- a/plugins/module_utils/meshcore_ota.py +++ b/plugins/module_utils/meshcore_ota.py @@ -2,7 +2,8 @@ Kept apart from meshcore_fw_ota so they can be unit-tested: which package fits the node, the chunk framing, and what a transfer costs on air. The protocol -itself is the reference client's, tools/ota_mesh_push.py of meshcore-tth. +itself is in meshcore_ota_engine, which tools/ota_mesh_push.py of meshcore-tth +uses too. A package (`.mcfw`, tools/ota_pack.py) is a 256 B manifest and a payload: 0 magic "MCFW" | 4 format 1 (full image) or 2 (delta) | 5 layout @@ -52,6 +53,14 @@ def parse_status(text): return (int(m.group(1)), m.group(2)) if m else None +def parse_base(text): + """`fw base` -> (None, sha256 prefix) or None. It answers in any state of a + transfer ('> <64 hex>', on the local console a fresh hash after it), where + `fw status` shows the base only when idle.""" + m = re.match(r">\s*([0-9a-f]{64})", text or "") + return (None, m.group(1)[:8]) if m else None + + def pick(packages, base, full_ok): """('current', pkg) | ('delta', pkg) | ('full', pkg) | ('none', None). @@ -169,3 +178,43 @@ class Bucket: self.used += secs return 0.0 return (secs - self.level) / self.rate + + +# ── side channel (tempradio) ──────────────────────────────────────────────── + + +def parse_preset(text): + """'869.525,62.5,7,5' (freq MHz, bw kHz, sf, cr) -> tuple, in the ranges `tempradio` takes.""" + try: + f, bw, sf, cr = str(text).split(",") + p = (float(f), float(bw), int(sf), int(cr)) + except ValueError: + raise ValueError(f"side preset {text!r}: freq,bw,sf,cr (e.g. 869.525,62.5,7,5)") from None + if not (150 <= p[0] <= 2500 and 7 <= p[1] <= 500 and 5 <= p[2] <= 12 and 5 <= p[3] <= 8): + raise ValueError(f"side preset {text!r}: out of range (freq MHz, bw kHz, sf 5-12, cr 5-8)") + return p + + +def fmt_preset(p): + return f"{p[0]:g},{p[1]:g},{p[2]},{p[3]}" + + +def parse_radio(reply): + """`get radio` ('> 869.432,62.5,7,5') -> preset tuple, or None.""" + m = re.fullmatch(r">\s*([\d.]+),([\d.]+),(\d+),(\d+)", (reply or "").strip()) + return parse_preset(",".join(m.groups())) if m else None + + +def same_preset(self_info, p): + """Does a companion's self_info run preset p?""" + return ( + abs(float(self_info.get("radio_freq") or 0) - p[0]) < 0.001 + and abs(float(self_info.get("radio_bw") or 0) - p[1]) < 0.01 + and int(self_info.get("radio_sf") or 0) == p[2] + and int(self_info.get("radio_cr") or 0) == p[3] + ) + + +def lora_params(p): + """Preset -> the (sf, bw, cr 1..4) lora_airtime takes.""" + return p[2], p[1], p[3] - 4 diff --git a/plugins/module_utils/meshcore_ota_engine.py b/plugins/module_utils/meshcore_ota_engine.py new file mode 100644 index 0000000..f2229d6 --- /dev/null +++ b/plugins/module_utils/meshcore_ota_engine.py @@ -0,0 +1,540 @@ +"""The OTA firmware transfer of meshcore-tth (src/helpers/FwUpdate), Ansible-free. + +ONE implementation for every client: the meshcore_fw_ota module wraps it, and +so do tools/ota_mesh_push.py of meshcore-tth (RF through a companion) and +tools/ota_push.py (the node's own console). Change the protocol here, nowhere +else. + +Two ways to the node, the same `fw` commands on both: + +* Console -- the node's own console, BLE (bonded on this host) or USB: local, + nothing on air, a full image may go too. +* Radio -- remote admin CLI through a companion, inside an airtime budget; + binary chunks (REQ_TYPE_FW_OTA) with text chunks as the fallback; deltas + only (the node refuses a full image over the mesh). + +Radio(side=...) carries the transfer on a SIDE CHANNEL, off the shared mesh: +login, `fw status` and `get radio` go over the mesh, then the node gets +`tempradio ,3`, the companion is retuned to the same preset (and that is +checked: an openHop virtual companion acks set_radio and stays put), a fresh +login goes zero-hop and the chunks follow. The window grows to `window` minutes +once the link is proven and is renewed while the transfer runs. The node comes +back on `fw apply` (the reboot), on `tempradio ,1` when we are done, +or by its own timer -- a lost companion costs at most the window. The companion +gets its preset, client repeat and the contact's path back in close(). +""" + +import asyncio +import base64 +import os +import random +import time + +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_console import BleConsole, ConsoleError +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_link import Link, LinkError +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import ( + BCHUNK, + CHUNK, + MANIFEST, + MAX_FRAME, + REPLY_TEXT, + REQ_FW, + UNIT, + Bucket, + Package, + fmt_preset, + hops_of, + lora_airtime, + lora_params, + parse_base, + parse_missing, + parse_radio, + parse_status, + pick, + req_frame_len, + same_preset, + text_frame_len, + text_missing, + w_payload, +) + +try: + from meshcore import EventType +except ImportError: # meshcore_link.HAS_MESHCORE says so + EventType = None + +# asyncio.wait_for raises asyncio.TimeoutError, which is the builtin only from +# Python 3.11 on -- dopey's 3.10 let a lost bitmap reply end the whole transfer. +TIMEOUTS = (TimeoutError, asyncio.TimeoutError) + +# what each reply looks like -- a late answer to an earlier command must not pass +EXPECT = { + "status": r"^(>|ERR|Err|Unknown|\?)", + "begin": r"^(OK|ERR|Err)", + "w": r"^(ok|ERR|Err)", + "list": r"^(>|ERR|Err)", + "apply": r"^(OK|ERR|Err)", + "ver": r"\(Build: ", + "tempradio": r"^(OK - temp|Error)", +} + +SIDE_FIRST = 3 # min: the node's first side window, until the link there is proven +SIDE_MARGIN = 150 # s before the window ends: renew (a text command retries up to 80 s) + + +class OtaError(Exception): + pass + + +def load_packages(path): + names = sorted(f for f in os.listdir(path) if f.endswith(".mcfw")) if os.path.isdir(path) else [path] + out = [] + for n in names: + full = os.path.join(path, n) if os.path.isdir(path) else n + with open(full, "rb") as f: + out.append(Package(os.path.basename(n), f.read())) + if not out: + raise OtaError(f"no .mcfw package in {path}") + return out + + +class Console: + """`fw` commands over the node's own console (BLE or USB): local, nothing on air.""" + + rf = False + + def __init__(self, con, log=None): + self.con = BleConsole(con) if isinstance(con, str) else con # a MAC, or a console object + if log is not None: + self.con.log = log + self.log = self.con.log + self.airtime = None + + async def open(self): + await self.con.open() + + async def close(self): + await self.con.close() + + async def ask(self, line, kind, timeout=5.0, tries=3, quiet=False): + del kind # a console answers in order + for attempt in range(tries): + try: + return await self.con.cmd(line, timeout=timeout, quiet=quiet) + except ConsoleError: + if attempt + 1 == tries: + raise + return "" + + async def after_apply(self, wait): + await self.con.close() + await asyncio.sleep(wait) + await self.con.open() + + +class Radio: + """`fw` commands as remote admin CLI through a companion, inside an airtime budget.""" + + rf = True + + def __init__(self, transport, target, password="", name=None, duty=3.0, gap=5.0, side=None, window=15, log=None): + self.link = Link(transport, target, password, name=name) + if log is not None: + self.link.log = log + self.log = self.link.log + self.bucket = Bucket(duty) + self.gap = gap + self.radio = None # (sf, bw, cr 1..4) of the channel the transfer is on + self.side = Side(self, side, window) if side else None + + @property + def airtime(self): + return round(self.bucket.used, 1) + + async def open(self): + await self.link.open() + si = self.link.mc.self_info or {} + self.radio = (int(si["radio_sf"]), float(si["radio_bw"]), int(si["radio_cr"]) - 4) + await self.spend(lora_airtime(48, *self.radio)) + await self.login() + + async def login(self): + await self.link.login() + await self.link.mc.ensure_contacts(follow=True) # the login may have taught it a path + self.link.contact = self.link._find_contact() or self.link.contact + self.log.append(f"path: {hops_of(self.link.contact)} hops") + + async def start(self): + """Called once the package is chosen: move to the side channel, if any.""" + if self.side is not None: + await self.side.enter() + + async def close(self): + try: + if self.side is not None: + await self.side.leave() + finally: + await self.link.close() + + async def spend(self, secs): + while True: + wait = self.bucket.wait_for(secs) + if wait == 0: + return + await asyncio.sleep(wait) + + def hops(self): + return hops_of(self.link.contact or {}) + + async def ask(self, line, kind, timeout=20.0, tries=4, quiet=False): + if self.side is not None: + await self.side.keep() + one = lora_airtime(text_frame_len(line), *self.radio) + lora_airtime( + text_frame_len("x" * REPLY_TEXT), *self.radio + ) + reply = await self.link.cli( + line, + EXPECT[kind], + retries=tries - 1, + timeout=timeout, + quiet=quiet, + before_send=lambda: self.spend(one * (self.hops() + 1)), + ) + if self.gap: + await asyncio.sleep(self.gap) + return reply + + # ── binary chunks (REQ_TYPE_FW_OTA) ────────────────────────────────────── + + def req_cost(self, payload_len, reply_len=0): + t = lora_airtime(req_frame_len(payload_len), *self.radio) + if reply_len: + t += lora_airtime(req_frame_len(reply_len), *self.radio) + return t * (self.hops() + 1) + + async def send_req(self, payload): + if 1 + 32 + len(payload) > MAX_FRAME: + raise OtaError(f"binary request of {len(payload)} B exceeds the companion frame") + ev = await self.link.mc.commands.send( + b"\x32" + bytes.fromhex(self.link.target_pk) + payload, [EventType.MSG_SENT, EventType.ERROR] + ) + if ev is None or ev.type != EventType.MSG_SENT: + return None + tag = ev.payload.get("expected_ack") + return tag.hex() if isinstance(tag, (bytes, bytearray)) else str(tag) + + async def write(self, sid, off, chunk): + if self.side is not None: + await self.side.keep() + payload = w_payload(sid, off, chunk) + c = self.req_cost(len(payload)) + await self.spend(c) + await self.send_req(payload) + # no reply comes: give the packet time to go out along the whole path + await asyncio.sleep(max(self.gap, c + 1.0)) + + async def missing(self, timeout=20.0): + """(state, missing unit indices) or None when the node does not answer.""" + for _attempt in range(2): + if self.side is not None: + await self.side.keep() + await self.spend(self.req_cost(2, 160)) + fut = asyncio.get_running_loop().create_future() + + def got(ev, fut=fut): + if not fut.done(): + fut.set_result(ev) + + sub = self.link.mc.subscribe(EventType.BINARY_RESPONSE, got) + try: + tag = await self.send_req(bytes([REQ_FW, ord("m")])) + ev = await asyncio.wait_for(fut, timeout) + except TIMEOUTS: + ev = None + finally: + self.link.mc.unsubscribe(sub) + if ev is None or (tag and ev.payload.get("tag") != tag): + continue + st = parse_missing(bytes.fromhex(ev.payload["data"])) + if st is None: + continue + await asyncio.sleep(self.gap) + return st + return None + + async def settled(self): + """The bitmap once the node is done checking (state 4 = busy). A lost + query after a long round must not throw the round away.""" + lost = 0 + st = None + for _query in range(10): + st = await self.missing() + if st is None and lost < 4: + lost += 1 + await asyncio.sleep(10) + continue + if st is None or st[0] != 4: + return st + await asyncio.sleep(3) + return st + + async def after_apply(self, wait): + if self.side is not None: + # the reboot takes the node back to its own preset: follow it + self.side.node_on_side = False + await self.side.leave() + await asyncio.sleep(wait) + await self.login() + + +class Side: + """The node and the companion on a side channel (tempradio), and the way back.""" + + def __init__(self, radio, preset, window): + self.r = radio + self.preset = preset + self.window = int(window) + self.node_main = None # the node's own preset (`get radio`) + self.comp_main = None # the companion's, set once it was moved + self.comp_repeat = None + self.path = None # the contact's mesh path, put back at the end + self.node_on_side = False + self.renew = None # (window min, monotonic deadline) while the node is there + + async def keep(self): + """Renew the node's window before its own timer takes it back.""" + if self.renew is None or time.monotonic() < self.renew[1] - SIDE_MARGIN: + return + win = self.renew[0] + self.renew = (win, time.monotonic() + win * 60) # first: ask() comes back here + r = await self.r.ask(f"tempradio {fmt_preset(self.preset)},{win}", "tempradio") + if not r.startswith("OK"): + raise OtaError(f"tempradio renewal: {r}") + await asyncio.sleep(3) # the node re-applies the preset 2 s after its reply + + async def enter(self): + r = self.r + mc = r.link.mc + node_main = parse_radio(await r.ask("get radio", "list")) + if node_main is None: + raise OtaError("`get radio` gave no preset") + self.node_main = node_main + c = r.link.contact + self.path = (c.get("out_path") or "", int(c.get("out_path_len", -1)), int(c.get("out_path_hash_mode") or 0)) + side = fmt_preset(self.preset) + reply = await r.ask(f"tempradio {side},{SIDE_FIRST}", "tempradio") + if not reply.startswith("OK"): + raise OtaError(f"tempradio: {reply}") + self.node_on_side = True + self.renew = (SIDE_FIRST, time.monotonic() + SIDE_FIRST * 60) + r.log.append(f"node {fmt_preset(node_main)} -> side channel {side} for {SIDE_FIRST} min") + await asyncio.sleep(3) # the node switches 2 s after its reply + + si = mc.self_info or {} + main = (float(si["radio_freq"]), float(si["radio_bw"]), int(si["radio_sf"]), int(si["radio_cr"])) + di = await mc.commands.send_device_query() + repeat = (di.payload or {}).get("repeat") if di is not None and di.type == EventType.DEVICE_INFO else None + await mc.commands.set_radio(*self.preset, repeat=None if repeat is None else 0) + ev = await mc.commands.send_appstart() + if ev is None or ev.type != EventType.SELF_INFO or not same_preset(ev.payload or {}, self.preset): + raise OtaError( + "the companion did not retune (an openHop virtual companion acks set_radio and keeps " + "the host's radio): a side channel needs a MeshCore companion firmware. " + f"The node returns to its preset within {SIDE_FIRST} min." + ) + self.comp_main, self.comp_repeat = main, repeat + r.radio = lora_params(self.preset) + r.log.append(f"companion on {side}") + # Zero-hop from here: the mesh path means nothing on the side channel, + # and the node answers along the path of the last login -- log in again. + try: + await r.login() + except LinkError as e: + raise OtaError(f"login on the side channel: {e} (out of direct range?)") from None + self.renew = (self.window, 0.0) # the link works: the full window now + await self.keep() + + async def leave(self): + """Back: the node (unless `fw apply` rebooted it), the companion, the contact's path.""" + r = self.r + self.renew = None + if self.node_on_side and self.comp_main is not None: + try: + # tempradio with its own preset: back in 2 s, on any firmware + reply = await r.ask(f"tempradio {fmt_preset(self.node_main)},1", "tempradio", tries=2) + r.log.append(f"node back to {fmt_preset(self.node_main)}: {reply}") + except (LinkError, OtaError): + r.log.append("node does not answer: it returns by itself when its window runs out") + self.node_on_side = False + if self.comp_main is None: + return + mc = r.link.mc + main, self.comp_main = self.comp_main, None + await mc.commands.set_radio(*main, repeat=None if self.comp_repeat is None else int(self.comp_repeat)) + ev = await mc.commands.send_appstart() + ok = ev is not None and ev.type == EventType.SELF_INFO and same_preset(ev.payload or {}, main) + r.log.append(f"companion back on {fmt_preset(main)}: {'ok' if ok else 'CHECK IT'}") + r.radio = lora_params(main) + out_path, out_len, mode = self.path + if out_len < 0: + await mc.commands.reset_path(r.link.contact) + else: + await mc.commands.update_contact(r.link.contact, path=out_path, path_hash_mode=mode) + if not ok: + raise OtaError(f"the companion did not come back to {fmt_preset(main)} -- set_radio it by hand") + + +async def binary_rounds(io, data, sid): + """Binary chunks in rounds; returns the text chunks still to send ([] = all in), or None = no binary.""" + if await io.missing() is None: + io.log.append("node does not answer binary requests: text chunks") + return None + todo = list(range((len(data) + BCHUNK - 1) // BCHUNK)) + last_miss = 1 << 30 + manifest_checked = False + for rnd in range(6): + for k, i in enumerate(todo): + off = i * BCHUNK + await io.write(sid, off, data[off : off + BCHUNK]) + if not manifest_checked and off + BCHUNK >= MANIFEST and k + 1 < len(todo): + # the node checks the manifest as soon as it is in: ask before + # the rest goes on air, a refused package would cost the round + manifest_checked = True + st = await io.settled() + if st is not None and st[0] == 3: + raise OtaError(f"node refused the package: {await io.ask('fw status', 'status')}") + st = await io.settled() + if st is None: + raise OtaError("no answer to the missing-units query (the node keeps what it has: resume)") + state, miss = st + io.log.append(f"binary round {rnd + 1}: {len(todo)} chunks sent, {len(miss)} units missing") + if state == 3: + raise OtaError(f"node refused the package: {await io.ask('fw status', 'status')}") + if not miss: + return [] + if rnd > 0 and len(miss) >= last_miss: + io.log.append("binary chunks do not arrive: text chunks for the rest") + return sorted({u * UNIT // CHUNK for u in miss}) + last_miss = len(miss) + todo = sorted({u * UNIT // BCHUNK for u in miss}) + raise OtaError("binary transfer did not converge in 6 rounds") + + +async def text_rounds(io, data, todo): + """Text chunks (each answered) until `fw missing` says none.""" + stuck = 0 + for rnd in range(40): + for i in todo: + off = i * CHUNK + r = await io.ask(f"fw w {off} {base64.b64encode(data[off : off + CHUNK]).decode()}", "w", quiet=True) + if r != "ok": + raise OtaError(f"chunk {off}: {r}") + reply = await io.ask("fw missing", "list") + new = text_missing(reply) + io.log.append(f"text round {rnd + 1}: {len(todo)} chunks sent, missing: {reply[:60]}") + if new == []: + return + if new is None: + stuck += 1 # a reply that is no list: ask again next round + if stuck > 3: + raise OtaError(f"`fw missing` does not answer: {reply}") + todo = [] + continue + new = [o // CHUNK for o in new] + stuck = stuck + 1 if new == todo else 0 + if stuck > 3: + raise OtaError("the same chunks keep missing") + todo = new + raise OtaError("text transfer did not converge in 40 rounds") + + +async def update(io, packages, apply=True, resume=False, check=False, apply_wait=60, text=False, force=False): + """Pick the package for the node's image and send it; returns the result dict. + + state: current | no_package | would_update | staged | applied. + force: `fw apply force`, below the battery gate -- the node takes it from + its local console only (a bench board without a cell). + """ + pkgs = load_packages(packages) + out = {"changed": False, "no_package": False} + await io.open() + try: + status = await io.ask("fw status", "status") + base = parse_status(status) + if base is None and status.startswith(">"): + # `fw status` shows the base only when idle: after a refused or + # half-done transfer it says failed/ready/rx, and that is no reason + # to fall back to a full DFU. `fw base` gives it in any state. + base = parse_base(await io.ask("fw base", "list")) + if base is None: + out.update(state="no_package", no_package=True, status=status) + io.log.append("no base image in `fw status`: not an -ota build") + return out + out["base"] = f"{base[0]} {base[1]}" if base[0] else base[1] + kind, pkg = pick(pkgs, base[1], full_ok=not io.rf) + if kind == "current": + out.update(state="current", version=pkg.version) + return out + if kind == "none": + out.update(state="no_package", no_package=True) + io.log.append( + f"no package starts from {base[1]}" + (" (a full image does not go over RF)" if io.rf else "") + ) + return out + out.update(package=pkg.name, kind=kind, version=pkg.version, changed=True) + io.log.append(f"package {pkg.name} ({kind}, {len(pkg.data)} B)") + if check: + out["state"] = "would_update" + return out + if io.rf: + await io.start() + data = pkg.data + if resume: + reply = await io.ask("fw missing", "list") + todo = text_missing(reply) + if todo is None: + raise OtaError(f"resume: {reply}") + if todo: + await text_rounds(io, data, [o // CHUNK for o in todo]) + else: + sid = random.randint(1, 0xFFFF) # chunks of an earlier transfer are dropped + r = await io.ask(f"fw begin {len(data)} {sid}", "begin") + if not r.startswith("OK"): + raise OtaError(f"fw begin: {r}") + todo = None + if io.rf and not text: + todo = await binary_rounds(io, data, sid) + if todo is None: + todo = list(range((len(data) + CHUNK - 1) // CHUNK)) + if todo: + await text_rounds(io, data, todo) + r = await io.ask("fw verify", "list", timeout=40) + for _poll in range(20): # the node runs it in loop(), not in the handler + if "verifying" not in r: + break + await asyncio.sleep(3) + r = await io.ask("fw status", "status") + if not r.startswith(">") or "ready" not in r: + raise OtaError(f"fw verify: {r}") + await io.ask("fw bl", "list") + if not apply: + out["state"] = "staged" + return out + r = await io.ask("fw apply force" if force else "fw apply", "apply", timeout=30, tries=1) + if not r.startswith("OK"): + raise OtaError(f"fw apply: {r}") + out["state"] = "applied" + try: + await io.after_apply(apply_wait) + out["version_after"] = await io.ask("ver", "ver") + await io.ask("fw status", "status") + except (LinkError, ConsoleError) as e: + # applied all the same; the caller reads the version its own way + io.log.append(f"after the apply: {e}") + return out + finally: + try: + await io.close() + finally: + if io.airtime is not None: + out["airtime"] = io.airtime diff --git a/plugins/modules/meshcore_fw_ota.py b/plugins/modules/meshcore_fw_ota.py index 1751063..eb0cb5c 100644 --- a/plugins/modules/meshcore_fw_ota.py +++ b/plugins/modules/meshcore_fw_ota.py @@ -60,6 +60,23 @@ options: type: bool default: false apply_wait: {description: Seconds to wait after C(fw apply) before reading the new version., type: int, default: 60} + text: {description: RF only. Text chunks only (each answered) instead of binary requests., type: bool, default: false} + side: + description: + - "RF only. Carry the transfer on a side channel, off the shared mesh: + C(freq,bw,sf,cr), e.g. C(869.525,62.5,7,5). Login, C(fw status) and + C(get radio) go over the mesh, then the node gets C(tempradio) and the + companion is retuned to the same preset; the chunks go there zero-hop, + so the companion must be in direct range of the node." + - The companion must retune its own radio (a MeshCore companion firmware). + An openHop virtual companion acks the change and stays put -- checked, + and the task fails; the node returns within 3 minutes. + - The node comes back on C(fw apply) (the reboot), by C(tempradio) with its + own preset at the end, or by its own timer. The companion gets its + preset back at the end -- unless the task is killed (async timeout), + then set its radio back by hand. + type: str + window: {description: "Side channel window in minutes, renewed while the transfer runs.", type: int, default: 15} requirements: [meshcore (RF), bleak (ble_console)] """ @@ -80,6 +97,16 @@ EXAMPLES = r""" transport: {type: ble_console, address: "AA:BB:CC:DD:EE:FF"} packages: /tmp/ota/ +- name: Off the shared channel, through a MeshCore companion in direct range + tth.meshcore.meshcore_fw_ota: + transport: {type: serial, port: /dev/serial/by-id/usb-Seeed_Studio_XIAO_nRF52840_0123-if00, expect_freq: 869.432} + target: "{{ mc_target }}" + password: "{{ vault_meshcore_admin_password }}" + packages: /tmp/ota/ + side: 869.525,62.5,7,5 + duty: 10 + gap: 1 + - name: No package fits the node's image -- full DFU instead ansible.builtin.debug: msg: "{{ ota.base }}: no delta" @@ -125,376 +152,16 @@ log: type: list elements: str """ -import asyncio -import base64 -import os -import random - from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_console import ( - HAS_BLEAK, - BleConsole, - ConsoleError, +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_console import HAS_BLEAK, ConsoleError +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_link import HAS_MESHCORE, LinkError, run +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import parse_preset +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota_engine import ( + Console, + OtaError, + Radio, + update, ) -from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_link import HAS_MESHCORE, Link, LinkError, run -from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import ( - BCHUNK, - CHUNK, - MANIFEST, - MAX_FRAME, - REPLY_TEXT, - REQ_FW, - UNIT, - Bucket, - Package, - hops_of, - lora_airtime, - parse_missing, - parse_status, - pick, - req_frame_len, - text_frame_len, - text_missing, - w_payload, -) - -try: - from meshcore import EventType -except ImportError: # HAS_MESHCORE says so - EventType = None - -# what each reply looks like -- a late answer to an earlier command must not pass -EXPECT = { - "status": r"^(>|ERR|Err|Unknown|\?)", - "begin": r"^(OK|ERR|Err)", - "w": r"^(ok|ERR|Err)", - "list": r"^(>|ERR|Err)", - "apply": r"^(OK|ERR|Err)", - "ver": r"\(Build: ", -} - - -class OtaError(Exception): - pass - - -def load_packages(path): - names = sorted(f for f in os.listdir(path) if f.endswith(".mcfw")) if os.path.isdir(path) else [path] - out = [] - for n in names: - full = os.path.join(path, n) if os.path.isdir(path) else n - with open(full, "rb") as f: - out.append(Package(os.path.basename(n), f.read())) - if not out: - raise OtaError(f"no .mcfw package in {path}") - return out - - -class Console: - """`fw` commands over the BLE console: local, nothing on air.""" - - rf = False - - def __init__(self, transport): - self.con = BleConsole(transport["address"]) - self.log = self.con.log - self.airtime = None - - async def open(self): - await self.con.open() - - async def close(self): - await self.con.close() - - async def ask(self, line, kind, timeout=5.0, tries=3, quiet=False): - del kind # a console answers in order - for attempt in range(tries): - try: - return await self.con.cmd(line, timeout=timeout, quiet=quiet) - except ConsoleError: - if attempt + 1 == tries: - raise - return "" - - async def after_apply(self, wait): - await self.con.close() - await asyncio.sleep(wait) - await self.con.open() - - -class Radio: - """`fw` commands as remote admin CLI through a companion, inside an airtime budget.""" - - rf = True - - def __init__(self, module): - p = module.params - self.link = Link(p["transport"], p["target"], p["password"], name=p["name"]) - self.log = self.link.log - self.bucket = Bucket(p["duty"]) - self.gap = p["gap"] - self.radio = None - - @property - def airtime(self): - return round(self.bucket.used, 1) - - async def open(self): - await self.link.open() - si = self.link.mc.self_info or {} - self.radio = (int(si["radio_sf"]), float(si["radio_bw"]), int(si["radio_cr"]) - 4) - await self.spend(lora_airtime(48, *self.radio)) - await self.login() - - async def login(self): - await self.link.login() - await self.link.mc.ensure_contacts(follow=True) # the login may have taught it a path - self.link.contact = self.link._find_contact() or self.link.contact - self.log.append(f"path: {hops_of(self.link.contact)} hops") - - async def close(self): - await self.link.close() - - async def spend(self, secs): - while True: - wait = self.bucket.wait_for(secs) - if wait == 0: - return - await asyncio.sleep(wait) - - def hops(self): - return hops_of(self.link.contact or {}) - - async def ask(self, line, kind, timeout=20.0, tries=4, quiet=False): - one = lora_airtime(text_frame_len(line), *self.radio) + lora_airtime( - text_frame_len("x" * REPLY_TEXT), *self.radio - ) - reply = await self.link.cli( - line, - EXPECT[kind], - retries=tries - 1, - timeout=timeout, - quiet=quiet, - before_send=lambda: self.spend(one * (self.hops() + 1)), - ) - if self.gap: - await asyncio.sleep(self.gap) - return reply - - # ── binary chunks (REQ_TYPE_FW_OTA) ────────────────────────────────────── - - def req_cost(self, payload_len, reply_len=0): - t = lora_airtime(req_frame_len(payload_len), *self.radio) - if reply_len: - t += lora_airtime(req_frame_len(reply_len), *self.radio) - return t * (self.hops() + 1) - - async def send_req(self, payload): - if 1 + 32 + len(payload) > MAX_FRAME: - raise OtaError(f"binary request of {len(payload)} B exceeds the companion frame") - ev = await self.link.mc.commands.send( - b"\x32" + bytes.fromhex(self.link.target_pk) + payload, [EventType.MSG_SENT, EventType.ERROR] - ) - if ev is None or ev.type != EventType.MSG_SENT: - return None - tag = ev.payload.get("expected_ack") - return tag.hex() if isinstance(tag, (bytes, bytearray)) else str(tag) - - async def write(self, sid, off, chunk): - payload = w_payload(sid, off, chunk) - c = self.req_cost(len(payload)) - await self.spend(c) - await self.send_req(payload) - # no reply comes: give the packet time to go out along the whole path - await asyncio.sleep(max(self.gap, c + 1.0)) - - async def missing(self, timeout=20.0): - """(state, missing unit indices) or None when the node does not answer.""" - for _attempt in range(2): - await self.spend(self.req_cost(2, 160)) - fut = asyncio.get_running_loop().create_future() - - def got(ev, fut=fut): - if not fut.done(): - fut.set_result(ev) - - sub = self.link.mc.subscribe(EventType.BINARY_RESPONSE, got) - try: - tag = await self.send_req(bytes([REQ_FW, ord("m")])) - ev = await asyncio.wait_for(fut, timeout) - except TimeoutError: - ev = None - finally: - self.link.mc.unsubscribe(sub) - if ev is None or (tag and ev.payload.get("tag") != tag): - continue - st = parse_missing(bytes.fromhex(ev.payload["data"])) - if st is None: - continue - await asyncio.sleep(self.gap) - return st - return None - - async def settled(self): - """The bitmap once the node is done checking (state 4 = busy). A lost - query after a long round must not throw the round away.""" - lost = 0 - st = None - for _query in range(10): - st = await self.missing() - if st is None and lost < 4: - lost += 1 - await asyncio.sleep(10) - continue - if st is None or st[0] != 4: - return st - await asyncio.sleep(3) - return st - - async def after_apply(self, wait): - await asyncio.sleep(wait) - await self.login() - - -async def binary_rounds(io, data, sid): - """Binary chunks in rounds; returns the text chunks still to send ([] = all in), or None = no binary.""" - if await io.missing() is None: - io.log.append("node does not answer binary requests: text chunks") - return None - todo = list(range((len(data) + BCHUNK - 1) // BCHUNK)) - last_miss = 1 << 30 - manifest_checked = False - for rnd in range(6): - for k, i in enumerate(todo): - off = i * BCHUNK - await io.write(sid, off, data[off : off + BCHUNK]) - if not manifest_checked and off + BCHUNK >= MANIFEST and k + 1 < len(todo): - # the node checks the manifest as soon as it is in: ask before - # the rest goes on air, a refused package would cost the round - manifest_checked = True - st = await io.settled() - if st is not None and st[0] == 3: - raise OtaError(f"node refused the package: {await io.ask('fw status', 'status')}") - st = await io.settled() - if st is None: - raise OtaError("no answer to the missing-units query (the node keeps what it has: resume)") - state, miss = st - io.log.append(f"binary round {rnd + 1}: {len(todo)} chunks sent, {len(miss)} units missing") - if state == 3: - raise OtaError(f"node refused the package: {await io.ask('fw status', 'status')}") - if not miss: - return [] - if rnd > 0 and len(miss) >= last_miss: - io.log.append("binary chunks do not arrive: text chunks for the rest") - return sorted({u * UNIT // CHUNK for u in miss}) - last_miss = len(miss) - todo = sorted({u * UNIT // BCHUNK for u in miss}) - raise OtaError("binary transfer did not converge in 6 rounds") - - -async def text_rounds(io, data, todo): - """Text chunks (each answered) until `fw missing` says none.""" - stuck = 0 - for rnd in range(40): - for i in todo: - off = i * CHUNK - r = await io.ask(f"fw w {off} {base64.b64encode(data[off : off + CHUNK]).decode()}", "w", quiet=True) - if r != "ok": - raise OtaError(f"chunk {off}: {r}") - reply = await io.ask("fw missing", "list") - new = text_missing(reply) - io.log.append(f"text round {rnd + 1}: {len(todo)} chunks sent, missing: {reply[:60]}") - if new == []: - return - if new is None: - stuck += 1 # a reply that is no list: ask again next round - if stuck > 3: - raise OtaError(f"`fw missing` does not answer: {reply}") - todo = [] - continue - new = [o // CHUNK for o in new] - stuck = stuck + 1 if new == todo else 0 - if stuck > 3: - raise OtaError("the same chunks keep missing") - todo = new - raise OtaError("text transfer did not converge in 40 rounds") - - -async def work(module, io): - p = module.params - pkgs = load_packages(p["packages"]) - out = {"changed": False, "no_package": False} - await io.open() - try: - status = await io.ask("fw status", "status") - base = parse_status(status) - if base is None: - out.update(state="no_package", no_package=True, status=status) - io.log.append("no base image in `fw status`: not an -ota build") - return out - out["base"] = f"{base[0]} {base[1]}" - kind, pkg = pick(pkgs, base[1], full_ok=not io.rf) - if kind == "current": - out.update(state="current", version=pkg.version) - return out - if kind == "none": - out.update(state="no_package", no_package=True) - io.log.append( - f"no package starts from {base[1]}" + (" (a full image does not go over RF)" if io.rf else "") - ) - return out - out.update(package=pkg.name, kind=kind, version=pkg.version, changed=True) - if module.check_mode: - out["state"] = "would_update" - return out - data = pkg.data - if p["resume"]: - reply = await io.ask("fw missing", "list") - todo = text_missing(reply) - if todo is None: - raise OtaError(f"resume: {reply}") - if todo: - await text_rounds(io, data, [o // CHUNK for o in todo]) - else: - sid = random.randint(1, 0xFFFF) # chunks of an earlier transfer are dropped - r = await io.ask(f"fw begin {len(data)} {sid}", "begin") - if not r.startswith("OK"): - raise OtaError(f"fw begin: {r}") - todo = None - if io.rf: - todo = await binary_rounds(io, data, sid) - if todo is None: - todo = list(range((len(data) + CHUNK - 1) // CHUNK)) - if todo: - await text_rounds(io, data, todo) - r = await io.ask("fw verify", "list", timeout=40) - for _poll in range(20): # the node runs it in loop(), not in the handler - if "verifying" not in r: - break - await asyncio.sleep(3) - r = await io.ask("fw status", "status") - if not r.startswith(">") or "ready" not in r: - raise OtaError(f"fw verify: {r}") - await io.ask("fw bl", "list") - if not p["apply"]: - out["state"] = "staged" - return out - r = await io.ask("fw apply", "apply", timeout=30, tries=1) - if not r.startswith("OK"): - raise OtaError(f"fw apply: {r}") - out["state"] = "applied" - try: - await io.after_apply(p["apply_wait"]) - out["version_after"] = await io.ask("ver", "ver") - await io.ask("fw status", "status") - except (LinkError, ConsoleError) as e: - # applied all the same; the caller reads the version its own way - io.log.append(f"after the apply: {e}") - return out - finally: - if io.airtime is not None: - out["airtime"] = io.airtime - await io.close() def main(): @@ -510,20 +177,49 @@ def main(): "gap": {"type": "float", "default": 5}, "resume": {"type": "bool", "default": False}, "apply_wait": {"type": "int", "default": 60}, + "text": {"type": "bool", "default": False}, + "side": {"type": "str"}, + "window": {"type": "int", "default": 15}, }, supports_check_mode=True, ) - console = module.params["transport"].get("type") == "ble_console" + p = module.params + console = p["transport"].get("type") == "ble_console" if console and not HAS_BLEAK: module.fail_json(msg=missing_required_lib("bleak")) if not console and not HAS_MESHCORE: module.fail_json(msg=missing_required_lib("meshcore")) - if not console and not module.params["target"]: + if not console and not p["target"]: module.fail_json(msg="target (the node's pubkey) is required over RF") + if console and p["side"]: + module.fail_json(msg="side is RF only: the BLE console puts nothing on air") io = None try: - io = Console(module.params["transport"]) if console else Radio(module) - result = run(work(module, io)) + side = parse_preset(p["side"]) if p["side"] else None + if console: + io = Console(p["transport"]["address"]) + else: + io = Radio( + p["transport"], + p["target"], + p["password"], + name=p["name"], + duty=p["duty"], + gap=p["gap"], + side=side, + window=p["window"], + ) + result = run( + update( + io, + p["packages"], + apply=p["apply"], + resume=p["resume"], + check=module.check_mode, + apply_wait=p["apply_wait"], + text=p["text"], + ) + ) except (LinkError, ConsoleError, OtaError, ValueError, OSError) as e: module.fail_json(msg=str(e), log=io.log if io else []) module.exit_json(log=io.log, **result) diff --git a/tests/unit/plugins/module_utils/test_meshcore_ota.py b/tests/unit/plugins/module_utils/test_meshcore_ota.py index 4f08c7a..9028149 100644 --- a/tests/unit/plugins/module_utils/test_meshcore_ota.py +++ b/tests/unit/plugins/module_utils/test_meshcore_ota.py @@ -7,11 +7,17 @@ from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import ( Bucket, Package, crc16, + fmt_preset, hops_of, lora_airtime, + lora_params, + parse_base, parse_missing, + parse_preset, + parse_radio, parse_status, pick, + same_preset, text_missing, w_payload, ) @@ -144,3 +150,38 @@ def test_bucket_paces_to_the_duty(): def test_bucket_refuses_a_silly_duty(): with pytest.raises(ValueError): Bucket(0) + + +# ── side channel ──────────────────────────────────────────────────────────── + + +def test_parse_preset(): + assert parse_preset("869.525,62.5,7,5") == (869.525, 62.5, 7, 5) + assert fmt_preset((869.525, 62.5, 7, 5)) == "869.525,62.5,7,5" + assert fmt_preset((866.0, 62.5, 7, 5)) == "866,62.5,7,5" # tempradio parses it with strtof + for bad in ("869.525,62.5,7", "869.525,62.5,13,5", "869.525,62.5,7,4", "x,62.5,7,5", "869.525,600,7,5"): + with pytest.raises(ValueError): + parse_preset(bad) + + +def test_parse_radio(): + assert parse_radio("> 869.432,62.5,7,5") == (869.432, 62.5, 7, 5) + assert parse_radio("> 866.5,62.5,8,5") == (866.5, 62.5, 8, 5) + assert parse_radio("Unknown command") is None + + +def test_same_preset_and_lora_params(): + si = {"radio_freq": 869.525, "radio_bw": 62.5, "radio_sf": 7, "radio_cr": 5} + assert same_preset(si, (869.525, 62.5, 7, 5)) + assert not same_preset(si, (869.432, 62.5, 7, 5)) + assert not same_preset({}, (869.525, 62.5, 7, 5)) + assert lora_params((869.525, 62.5, 7, 5)) == (7, 62.5, 1) + + +def test_parse_base(): + # `fw status` after a refused package shows no base -- `fw base` does + assert parse_status("> failed: not newer than this image (pkg 1794864486, node 1794864486) [b2 d0]") is None + sha = "d81446b7" + "0" * 56 + assert parse_base(f"> {sha}") == (None, "d81446b7") + assert parse_base(f"> {sha} fresh {sha}") == (None, "d81446b7") + assert parse_base("ERR: fw status|begin|w|missing") is None diff --git a/tests/unit/plugins/module_utils/test_meshcore_ota_engine.py b/tests/unit/plugins/module_utils/test_meshcore_ota_engine.py new file mode 100644 index 0000000..a4fb1eb --- /dev/null +++ b/tests/unit/plugins/module_utils/test_meshcore_ota_engine.py @@ -0,0 +1,144 @@ +"""The side channel of meshcore_ota_engine against a fake companion: what goes +to the node and to the companion, and that both come back.""" + +import asyncio +import time + +import pytest +from ansible_collections.tth.meshcore.plugins.module_utils import meshcore_ota_engine as engine +from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota_engine import OtaError, Radio + +meshcore = pytest.importorskip("meshcore") +EventType = meshcore.EventType + +NODE = "1d7710553156342d8368f60c0382a0e77a615bbc672f79ea1d2179037bcda9b5" +MAIN = {"radio_freq": 866.5, "radio_bw": 62.5, "radio_sf": 8, "radio_cr": 5} + + +class Ev: + def __init__(self, type_, payload): + self.type, self.payload = type_, payload + + +class Commands: + def __init__(self, mc): + self.mc = mc + + async def send_device_query(self): + return Ev(EventType.DEVICE_INFO, {"repeat": False}) + + async def set_radio(self, freq, bw, sf, cr, repeat=None): + self.mc.calls.append(("set_radio", freq, bw, sf, cr, repeat)) + if self.mc.retunes: + self.mc.self_info.update(radio_freq=freq, radio_bw=bw, radio_sf=sf, radio_cr=cr) + + async def send_appstart(self): + return Ev(EventType.SELF_INFO, dict(self.mc.self_info)) + + async def reset_path(self, contact): + self.mc.calls.append(("reset_path",)) + + async def update_contact(self, contact, path=None, path_hash_mode=None): + self.mc.calls.append(("update_contact", path, path_hash_mode)) + + +class MC: + def __init__(self, retunes=True): + self.calls = [] + self.retunes = retunes + self.self_info = dict(MAIN) + self.contacts = {} + self.commands = Commands(self) + + async def ensure_contacts(self, follow=False): + pass + + +def radio(retunes=True): + r = Radio({"type": "serial", "port": "x"}, NODE, side=(866.0, 62.5, 7, 5), window=15, gap=0) + mc = MC(retunes) + r.link.mc = mc + r.link.contact = {"public_key": NODE, "out_path": "c327a1b2", "out_path_len": 2, "out_path_hash_mode": 1} + r.radio = (8, 62.5, 1) + + async def cli(cmd, expect, **kw): + mc.calls.append(("cmd", cmd)) + if cmd == "get radio": + return "> 866.5,62.5,8,5" + if cmd.startswith("tempradio"): + return "OK - temp params for 3 mins" + return "ok" + + async def login(): + mc.calls.append(("login",)) + + r.link.cli = cli + r.link.login = login + return r, mc + + +@pytest.fixture(autouse=True) +def no_sleep(monkeypatch): + async def sleep(_secs): + return None + + monkeypatch.setattr(engine.asyncio, "sleep", sleep) + + +def test_side_there_and_back(): + r, mc = radio() + + async def go(): + await r.start() + assert r.radio == (7, 62.5, 1) + await r.side.leave() + + asyncio.run(go()) + assert mc.calls == [ + ("cmd", "get radio"), + ("cmd", "tempradio 866,62.5,7,5,3"), + ("set_radio", 866.0, 62.5, 7, 5, 0), + ("login",), + ("cmd", "tempradio 866,62.5,7,5,15"), # the link works: the full window + ("cmd", "tempradio 866.5,62.5,8,5,1"), # the node back + ("set_radio", 866.5, 62.5, 8, 5, 0), # the companion back, client repeat as it was + ("update_contact", "c327a1b2", 1), # and its mesh path + ] + assert r.radio == (8, 62.5, 1) + + +def test_companion_that_does_not_retune(): + r, mc = radio(retunes=False) + with pytest.raises(OtaError, match="did not retune"): + asyncio.run(r.start()) + + asyncio.run(r.side.leave()) + # nothing to put back, and no `tempradio` back over a channel the node is not on + assert [c for c in mc.calls if c[0] == "cmd"] == [("cmd", "get radio"), ("cmd", "tempradio 866,62.5,7,5,3")] + assert sum(c[0] == "set_radio" for c in mc.calls) == 1 + + +def test_window_renewed_before_it_ends(): + r, mc = radio() + asyncio.run(r.start()) + n = len(mc.calls) + asyncio.run(r.ask("fw status", "status")) + assert mc.calls[n:] == [("cmd", "fw status")] # a fresh window: nothing to renew + r.side.renew = (15, time.monotonic() + 60) # a minute left + asyncio.run(r.ask("fw status", "status")) + assert mc.calls[n + 1 :] == [("cmd", "tempradio 866,62.5,7,5,15"), ("cmd", "fw status")] + + +def test_after_apply_follows_the_node_home(): + r, mc = radio() + + async def go(): + await r.start() + await r.after_apply(0) + + asyncio.run(go()) + # the reboot took the node back: no `tempradio` back, the companion follows + cmds = [c for c in mc.calls if c[0] == "cmd"] + assert ("cmd", "tempradio 866.5,62.5,8,5,1") not in cmds + assert mc.calls[-1] == ("login",) + assert ("set_radio", 866.5, 62.5, 8, 5, 0) in mc.calls