ota_push.py, ota_mesh_push.py: a directory of a release's packages -- the delta from the node's image is picked (exit 3: none fits)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-14 20:34:57 +01:00
1 parent 96cbd7333a
commit 65f7965955
2 files changed
+105 -11

No files matched your search

+59 -8
View File
@@ -10,7 +10,9 @@
<dest> the target node's public key prefix (>= 8 hex), as in its
`get public.key`
<package> a DELTA package only (ota_pack.py diff / fetch --base): a
full image is refused here and by the node itself
full image is refused here and by the node itself. Or a
directory with one release's packages: the delta from the
image `fw status` reports is taken; exit 3 = none fits
--apply after a good `fw verify`, `fw apply` and wait for the node to
come back with the new image (ver + fw status over the mesh)
--duty P airtime budget in percent per hour (default 1: the
@@ -43,8 +45,9 @@ Every `fw w` is idempotent on the node, so a lost reply is simply resent; the
node checks the manifest as soon as its chunks arrive and answers ERR for a
wrong board or base -- that stops the transfer.
The admin password comes from platformio.local.ini ([secrets] admin_pw), or is
asked for on the terminal; it is never printed. CHECK `get radio` on both ends first: this transmits.
The admin password comes from platformio.local.ini ([secrets] admin_pw), is
asked for on the terminal, or read from stdin when that is no terminal; it is
never printed. CHECK `get radio` on both ends first: this transmits.
"""
import argparse
@@ -53,6 +56,7 @@ import base64
import configparser
import math
import random
import re
import sys
import time
from pathlib import Path
@@ -110,11 +114,50 @@ def admin_pw() -> str:
import getpass
pw = getpass.getpass("admin password of the node: ").strip()
elif not pw:
pw = sys.stdin.readline().strip() # piped in (the firmware role)
if not pw:
sys.exit(f"no admin password: [secrets] admin_pw in {INI}, or run it on a terminal")
sys.exit(f"no admin password: [secrets] admin_pw in {INI}, a terminal, or stdin")
return pw
NO_PACKAGE = 3 # exit code: no package starts from the node's image
def pick_package(where: str, status: str, full_ok: bool) -> bytes:
"""`package` may be a directory with one release's packages (the full image
and its deltas, as the firmware role fetches them): take the delta that
starts from the image `fw status` reports, else -- where a full image may
go -- the full one. Exits NO_PACKAGE when nothing fits: the node runs an
image no delta starts from, or has no `fw` at all (not an -ota build)."""
p = Path(where)
if not p.is_dir():
return p.read_bytes()
m = re.search(r"\bbase \d+ ([0-9a-f]{8})", status)
if not m:
print(f"no base image in `fw status`: {status!r}")
sys.exit(NO_PACKAGE)
base = m.group(1)
full = None
for f in sorted(p.glob("*.mcfw")):
data = f.read_bytes()
if data[:4] != b"MCFW":
continue
if data[16:48].hex().startswith(base): # raw_sha256 = the image it installs
print(f"the node already runs the image of {f.name}")
sys.exit(0)
if data[4] == 2 and data[160:192].hex().startswith(base):
print(f"package: {f.name} (delta from {base})")
return data
if data[4] == 1:
full = (f, data)
if full and full_ok:
print(f"package: {full[0].name} (full image, no delta from {base})")
return full[1]
print(f"no package in {where} starts from {base}")
sys.exit(NO_PACKAGE)
def lora_airtime(n: int, sf: int, bw_khz: float, cr: int, preamble: int = 16) -> float:
"""Seconds on air for an n-byte LoRa frame (explicit header, CRC on)."""
tsym = (2**sf) / (bw_khz * 1000)
@@ -327,16 +370,21 @@ async def find_contact(mc: MeshCore, prefix: str, wait: float) -> dict:
mc.unsubscribe(sub)
async def run(args: argparse.Namespace) -> int:
data = Path(args.package).read_bytes()
def check_delta(data: bytes, name: str) -> bytes:
# Only deltas go over the mesh -- the node refuses anything else too
# (FwUpdate: `fw begin` over 64 kB, a format 1 manifest). A full image is
# ~300 kB, hours of a shared channel: USB (ota_push.py) or the BLE console.
if data[:4] != b"MCFW" or data[4] != 2:
sys.exit(
f"{args.package}: not a delta package (format {data[4] if len(data) > 4 else '?'}) -- "
f"{name}: not a delta package (format {data[4] if len(data) > 4 else '?'}) -- "
"over the mesh only deltas; a full image goes over USB (tools/ota_push.py) or BLE"
)
return data
async def run(args: argparse.Namespace) -> int:
pkg = Path(args.package)
data = None if pkg.is_dir() else check_delta(pkg.read_bytes(), args.package)
if args.companion.startswith("tcp://"):
host, port = args.companion[6:].rsplit(":", 1)
mc = await MeshCore.create_tcp(host, int(port))
@@ -377,7 +425,10 @@ async def run(args: argparse.Namespace) -> int:
link = Link(mc, contact, bucket, radio, args.gap)
t0 = time.time()
print("status:", await link.cmd("fw status"))
status = await link.cmd("fw status")
print("status:", status)
if data is None:
data = check_delta(pick_package(args.package, status, full_ok=False), args.package)
if args.resume:
# `fw begin` would wipe what the node has; text chunks need no session
r = await link.cmd("fw missing")
+46 -3
View File
@@ -6,6 +6,11 @@
tools/ota_push.py b1 Xiao_nrf52_repeater-ota.mcfw --apply # ... and apply
tools/ota_push.py b1 pkg.mcfw --binary --drop 30 --prime old.mcfw
tools/ota_push.py CE:72:14:CD:FD:02 pkg.mcfw --apply # over BLE (NUS)
tools/ota_push.py CE:72:14:CD:FD:02 <dir> --apply # the package for the node's image
The package may be a directory with one release's packages (full image and
deltas): the delta from the image `fw status` reports is taken, else the full
image. Exit 3 = nothing there starts from the node's image (or no `fw`).
The board: a name from the local config (tools/mctools_config.py), a USB
serial number, a port path -- or a BLE MAC: the node's BLE console, bonded
@@ -38,6 +43,7 @@ import random
import re
import sys
import time
from pathlib import Path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
@@ -221,6 +227,43 @@ class BleConsole:
self._loop.call_soon_threadsafe(self._loop.stop)
NO_PACKAGE = 3 # exit code: no package starts from the node's image
def pick_package(where: str, status: str, full_ok: bool) -> bytes:
"""`package` may be a directory with one release's packages (the full image
and its deltas, as the firmware role fetches them): take the delta that
starts from the image `fw status` reports, else -- where a full image may
go -- the full one. Exits NO_PACKAGE when nothing fits: the node runs an
image no delta starts from, or has no `fw` at all (not an -ota build)."""
p = Path(where)
if not p.is_dir():
return p.read_bytes()
m = re.search(r"\bbase \d+ ([0-9a-f]{8})", status)
if not m:
print(f"no base image in `fw status`: {status!r}")
sys.exit(NO_PACKAGE)
base = m.group(1)
full = None
for f in sorted(p.glob("*.mcfw")):
data = f.read_bytes()
if data[:4] != b"MCFW":
continue
if data[16:48].hex().startswith(base): # raw_sha256 = the image it installs
print(f"the node already runs the image of {f.name}")
sys.exit(0)
if data[4] == 2 and data[160:192].hex().startswith(base):
print(f"package: {f.name} (delta from {base})")
return data
if data[4] == 1:
full = (f, data)
if full and full_ok:
print(f"package: {full[0].name} (full image, no delta from {base})")
return full[1]
print(f"no package in {where} starts from {base}")
sys.exit(NO_PACKAGE)
def open_console(target: str):
if MAC_RE.match(target):
return BleConsole(target)
@@ -280,8 +323,6 @@ def main() -> None:
)
args = ap.parse_args()
with open(args.package, "rb") as f:
data = f.read()
if args.binary and MAC_RE.match(args.board):
sys.exit("--binary is a bench test of the mesh path: USB only")
s = open_console(args.board)
@@ -289,7 +330,9 @@ def main() -> None:
if not isinstance(s, BleConsole): # over BLE its reply comes late, as the next one's
s.write(b"\r\n")
time.sleep(0.4)
print("status:", command(s, "fw status"))
st = command(s, "fw status")
print("status:", st)
data = pick_package(args.package, st, full_ok=True)
if args.prime:
with open(args.prime, "rb") as f:
other = f.read()