meshcore_fw_ota: the transfer as an Ansible-free engine shared with meshcore-tth's tools, plus a side channel via tempradio
ci / test (push) Failing after 15s

module_utils/meshcore_ota_engine.py now holds the whole transfer (Console,
Radio, binary and text rounds, update()); the module only maps its
parameters. tools/ota_mesh_push.py and tools/ota_push.py of meshcore-tth
import the same code, so the protocol lives in one place.

New:
- side/window: the node goes to another preset by `tempradio`, the
  companion is retuned (checked -- an openHop virtual companion acks
  set_radio as a no-op and is refused), a fresh login goes zero-hop and
  the delta follows; the window is renewed, and the node and companion
  come back at the end (or the node by its own timer).
- SerialConsole: the node's USB console, the same calls as BleConsole.
- `fw base` when `fw status` shows no base (failed/ready/rx): a node left
  after a refused package was taken for "not an -ota build", i.e. a full
  DFU, and `resume` could never pick its package.
- asyncio.TimeoutError caught too: on Python 3.10 (dopey) a lost bitmap
  reply ended the whole transfer.
- text: text chunks only; update(force=...) for `fw apply force`.

Bench 16. 11. 2026 (x3 companion -> x1 -ota repeater, 866.5 -> side 866.0/SF7):
the module from dopey (py3.10), the firmware role's ota.yml with
mc_fw_ota_side, ota_mesh_push.py and ota_push.py over USB -- each applied
a 2.7 kB delta, ~20 s airtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-16 23:17:41 +01:00
1 parent 8306d91b48
commit 185470de08
7 files changed
+922 -375

No files matched your search

+64 -1
View File
@@ -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")
+50 -1
View File
@@ -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
+540
View File
@@ -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 <side>,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 <its preset>,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