From f7bd88c1c8536836f376c1c1b5c7694396efbbd9 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sat, 14 Nov 2026 12:40:07 +0100 Subject: [PATCH] ota_mesh_push.py --resume: continue a stopped transfer; a lost `missing` reply no longer ends it Co-Authored-By: Claude Opus 5.5 --- tools/ota_mesh_push.py | 54 +++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/tools/ota_mesh_push.py b/tools/ota_mesh_push.py index 29042226..db941c3c 100755 --- a/tools/ota_mesh_push.py +++ b/tools/ota_mesh_push.py @@ -31,6 +31,9 @@ unanswered, missing ones found by a bitmap query). Falls back to text by itself when the node does not answer the binary query (older firmware, or the sensor build). + --resume continue a transfer that stopped (the node keeps what it got + until the next `fw begin` or a reboot): no begin, only the + chunks `fw missing` lists, as text, then verify (and --apply) --wait S how long to wait for the dest's advert if the companion does not know it yet (default 120 s); send `advert.zerohop` on the dest meanwhile @@ -375,12 +378,20 @@ async def run(args: argparse.Namespace) -> int: t0 = time.time() print("status:", await link.cmd("fw status")) - sid = random.randint(1, 0xFFFF) # chunks of an earlier transfer are dropped - r = await link.cmd(f"fw begin {len(data)} {sid}") - print("begin:", r) - if not r.startswith("OK"): - return 1 - binary = None if args.text else Binary(link) + if args.resume: + # `fw begin` would wipe what the node has; text chunks need no session + r = await link.cmd("fw missing") + print("missing:", r) + if not r.startswith(">"): + return 1 + sid = 0 + else: + sid = random.randint(1, 0xFFFF) # chunks of an earlier transfer are dropped + r = await link.cmd(f"fw begin {len(data)} {sid}") + print("begin:", r) + if not r.startswith("OK"): + return 1 + binary = None if (args.text or args.resume) else Binary(link) if binary: binary.sid = sid text_todo: list[int] | None = None @@ -395,8 +406,15 @@ async def run(args: argparse.Namespace) -> int: last_miss = 1 << 30 async def settled(): + lost = 0 for _ in range(10): st = await binary.missing() + if st is None and lost < 4: + # a lost query or reply after a 40-minute round must not + # throw the round away (14. 11. 2026, mast-three: it did) + lost += 1 + await asyncio.sleep(10) + continue if st is None or st[0] != 4: # 4 = busy (manifest check) return st await asyncio.sleep(3) @@ -426,7 +444,7 @@ async def run(args: argparse.Namespace) -> int: print() st = await settled() if st is None: - print("missing: no answer") + print("missing: no answer (the node keeps what it has: --resume)") return 1 state, miss = st print(f"missing: state {state}, {len(miss)} units") @@ -447,9 +465,15 @@ async def run(args: argparse.Namespace) -> int: todo = [] elif text_todo is not None: todo = text_todo # only what the binary rounds did not deliver + elif args.resume: + # `fw missing` lists ~23 offsets per reply: rounds go on while they help + todo = [] + if not r.endswith("none"): + todo = [int(x) // CHUNK for x in r.lstrip(">").split()] else: todo = list(range((len(data) + CHUNK - 1) // CHUNK)) - for rnd in range(4 if todo else 0): + stuck = 0 + for rnd in range(40 if todo else 0): for k, i in enumerate(todo): off = i * CHUNK r = await link.cmd( @@ -469,7 +493,18 @@ async def run(args: argparse.Namespace) -> int: print("missing:", r) if r.endswith("none"): break - todo = [int(x) // CHUNK for x in r.lstrip(">").split()] + if not r.startswith(">"): + stuck += 1 # a lost reply: ask again next round + if stuck > 3: + print("fw missing does not answer, giving up (--resume continues)") + return 1 + continue + new = [int(x) // CHUNK for x in r.lstrip(">").split()] + stuck = stuck + 1 if new == todo else 0 + if stuck > 3: + print("the same chunks keep missing, giving up") + return 1 + todo = new r = await link.cmd("fw verify", timeout=40) for _ in range(20): # the node runs it in loop(), not in the handler if "verifying" not in r: @@ -512,6 +547,7 @@ def main() -> None: ap.add_argument("--gap", type=float, default=5.0) ap.add_argument("--via") ap.add_argument("--text", action="store_true") + ap.add_argument("--resume", action="store_true") sys.exit(asyncio.run(run(ap.parse_args())))