meshcore_fw_ota: OTA firmware update of a meshcore-tth repeater, over RF or its BLE console
ci / test (push) Successful in 2m21s

Picks the package of a release that fits the image `fw status` reports
(changed=false when the node runs it already, no_package when none fits or
the node has no `fw`), transfers it -- over RF as binary chunks with a
bitmap of the missing ones and text chunks as the fallback, inside an
airtime budget; over the BLE console as text, a full image too -- then
verifies and applies. Port of tools/ota_mesh_push.py / ota_push.py of
meshcore-tth; radio-free parts in module_utils/meshcore_ota.py with unit
tests (the airtime formula they caught is fixed in the tool as well).
Link.cli takes retries, timeout, quiet and a before_send hook.

Tested on two live repeaters forced onto their current image (BLE console;
RF through a companion: 'current', 0.6 s airtime) and with a package of
another base (no_package).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-14 20:54:29 +01:00
1 parent d6f2d5cf28
commit 891be3afbe
7 files changed
+966 -5

No files matched your search

@@ -0,0 +1,146 @@
"""Unit tests for the radio-free OTA parts. Replies are real ones from the masts."""
import base64
import pytest
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import (
Bucket,
Package,
crc16,
hops_of,
lora_airtime,
parse_missing,
parse_status,
pick,
text_missing,
w_payload,
)
IMG_OLD = "fe7484101f1cad46e055d09488b5aa0a29d00e3172e4394df31fa8d9cc7744a3"
IMG_MID = "43a84e0150b8649544af685a5ee19c8254428cccc2b525ed6591a77efacd158a"
IMG_NEW = "490418bc6fa12b22edbf6c3925cd8536145a44ebfc3006f7a4ba3a2e65faa9bb"
def mcfw(fmt, image, base=None, version="v1.18.0-tthcc0598b"):
head = bytearray(256)
head[0:4] = b"MCFW"
head[4] = fmt
head[5] = 2
head[16:48] = bytes.fromhex(image)
head[128 : 128 + len(version)] = version.encode()
if base:
head[160:192] = bytes.fromhex(base)
return bytes(head) + b"\x00" * 64
RELEASE = [
Package("x-v3.mcfw", mcfw(1, IMG_NEW)),
Package("x-v3.from-v1.mcfw", mcfw(2, IMG_NEW, IMG_OLD)),
Package("x-v3.from-v2.mcfw", mcfw(2, IMG_NEW, IMG_MID)),
]
# ── fw status ───────────────────────────────────────────────────────────────
def test_status_of_an_ota_node():
s = "> idle, batt 4137mV (min 3800), free 352256 B, base 456492 fe748410, delta"
assert parse_status(s) == (456492, "fe748410")
@pytest.mark.parametrize("reply", ["Unknown command", "", "> idle"])
def test_status_without_fw(reply):
assert parse_status(reply) is None
# ── which package ───────────────────────────────────────────────────────────
def test_delta_from_the_node_image():
kind, p = pick(RELEASE, "43a84e01", full_ok=False)
assert (kind, p.name) == ("delta", "x-v3.from-v2.mcfw")
def test_already_runs_the_image():
assert pick(RELEASE, "490418bc", full_ok=True)[0] == "current"
def test_no_delta_full_image_only_where_allowed():
assert pick(RELEASE, "deadbeef", full_ok=False) == ("none", None)
kind, p = pick(RELEASE, "deadbeef", full_ok=True)
assert (kind, p.name) == ("full", "x-v3.mcfw")
def test_package_manifest():
p = RELEASE[1]
assert p.delta and p.base_sha == IMG_OLD and p.image_sha == IMG_NEW
assert p.version == "v1.18.0-tthcc0598b"
assert not RELEASE[0].delta and RELEASE[0].base_sha is None
def test_not_a_package():
with pytest.raises(ValueError):
Package("zip.zip", b"PK\x03\x04" + b"\x00" * 300)
# ── framing ─────────────────────────────────────────────────────────────────
def test_crc16_ccitt():
assert crc16(b"123456789") == 0x29B1 # CRC-16/CCITT-FALSE check value
def test_binary_chunk_layout():
chunk = bytes(range(128))
p = w_payload(0x1234, 0x100, chunk)
assert p[:2] == b"\xf0w"
assert p[2:4] == b"\x34\x12" and p[4:8] == b"\x00\x01\x00\x00" and p[8] == 128
assert int.from_bytes(p[9:11], "little") == crc16(chunk, crc16(p[2:9]))
assert p[11:] == chunk
assert 1 + 32 + len(p) <= 176 # the companion frame
def test_missing_bitmap_reply():
assert parse_missing(bytes([0, 0, 0, 2, 0, 5, 0, 9, 0])) == (0, [5, 9])
assert parse_missing(bytes([3, 0, 0, 0, 0])) == (3, [])
assert parse_missing(b"\x00") is None
def test_text_missing():
assert text_missing("> none") == []
assert text_missing("> 96 192") == [96, 192]
assert text_missing("ERR no transfer") is None
def test_text_chunk_fits_the_console_line():
line = f"fw w 999936 {base64.b64encode(bytes(96)).decode()}"
assert len(line) < 160 # command[160] on the node
# ── airtime ─────────────────────────────────────────────────────────────────
def test_airtime_matches_the_semtech_calculator():
# SX1262 calculator: 10 B, SF7, BW125, CR4/5, preamble 8, explicit header, CRC = 41.22 ms
assert lora_airtime(10, 7, 125, 1, preamble=8) == pytest.approx(0.04122, abs=1e-4)
def test_hops():
assert hops_of({"out_path_len": -1}) == 0
assert hops_of({"out_path_len": 0x42}) == 2 # 2 hops, 2-byte hashes
def test_bucket_paces_to_the_duty():
now = [0.0]
b = Bucket(1, clock=lambda: now[0]) # 1 % = 36 s per hour, all of it at once
assert b.wait_for(30) == 0
w = b.wait_for(30)
assert w == pytest.approx((30 - 6) / 0.01)
now[0] += w
assert b.wait_for(30) == 0
assert b.used == pytest.approx(60)
def test_bucket_refuses_a_silly_duty():
with pytest.raises(ValueError):
Bucket(0)