Files
meshcore-ansible/tests/vectors/ota_vectors.py
T
2026-11-16 23:32:30 +01:00

135 lines
5.0 KiB
Python

"""Test vectors of the OTA protocol, for implementations in other languages.
python3 tests/vectors/ota_vectors.py > tests/vectors/ota_vectors.json
meshcore-open (Dart) implements the same transfer as module_utils/meshcore_ota
and meshcore_ota_engine; it checks itself against this JSON (a copy in its
test/ directory). tests/unit/.../test_ota_vectors.py fails when the JSON no
longer matches the Python code -- regenerate it and copy it over.
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.abspath(os.path.join(HERE, "..", "..", "..", "..", "..")))
from ansible_collections.tth.meshcore.plugins.module_utils.meshcore_ota import (
Package,
crc16,
fmt_preset,
lora_airtime,
parse_base,
parse_missing,
parse_preset,
parse_radio,
parse_status,
pick,
req_frame_len,
text_frame_len,
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) + bytes(range(64))
def build():
release = [
("x-v3.mcfw", mcfw(1, IMG_NEW)),
("x-v3.from-v1.mcfw", mcfw(2, IMG_NEW, IMG_OLD)),
("x-v3.from-v2.mcfw", mcfw(2, IMG_NEW, IMG_MID)),
]
pkgs = [Package(n, d) for n, d in release]
def picked(base, full_ok):
kind, p = pick(pkgs, base, full_ok)
return {"base": base, "full_ok": full_ok, "kind": kind, "package": p.name if p else None}
chunk = bytes((i * 7 + 3) & 0xFF for i in range(128))
return {
"packages": [
{
"name": n,
"hex": d.hex(),
"format": p.format,
"image_sha": p.image_sha,
"base_sha": p.base_sha,
"version": p.version,
}
for (n, d), p in zip(release, pkgs)
],
"pick": [
picked(IMG_OLD[:8], False),
picked(IMG_MID[:8], False),
picked(IMG_NEW[:8], False),
picked("00000000", False),
picked("00000000", True),
],
"crc16": [{"hex": h, "crc": crc16(bytes.fromhex(h))} for h in ("", "00", "313233343536373839", chunk.hex())],
"w_payload": [
{"sid": sid, "off": off, "chunk": c.hex(), "hex": w_payload(sid, off, c).hex()}
for sid, off, c in ((1, 0, chunk), (0xBEEF, 2816, chunk[:91]), (51048, 128, b"\x00" * 128))
],
"parse_status": [
{"text": t, "base": list(parse_status(t)) if parse_status(t) else None}
for t in (
"> idle, batt 4137mV (min 3800), free 352256 B, base 456492 fe748410, delta",
"> failed: not newer than this image (pkg 1794864486, node 1794864486) [b2 d0 x0 t0]",
"> rx 12/86 units, batt 4100mV [b12 d0 x0 t0]",
"Unknown command",
)
],
"parse_base": [
{"text": t, "sha8": (parse_base(t) or (None, None))[1]}
for t in ("> " + IMG_NEW, "> " + IMG_NEW + " fresh " + IMG_NEW, "ERR: fw status|begin")
],
"parse_missing": [
{
"hex": h,
"state": (parse_missing(bytes.fromhex(h)) or (None, None))[0],
"units": (parse_missing(bytes.fromhex(h)) or (None, None))[1],
}
for h in ("0100000000", "01000003000500090010000300", "040000", "03000000000000")
],
"text_missing": [
{"text": t, "offsets": text_missing(t)} for t in ("> none", "> 0 96 2688", "ERR: not receiving")
],
"preset": [
{"text": t, "preset": list(parse_preset(t)), "fmt": fmt_preset(parse_preset(t))}
for t in ("869.525,62.5,7,5", "866,62.5,7,5", "869.432,62.5,8,8")
],
"preset_bad": ["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"],
"parse_radio": [
{"text": t, "preset": list(parse_radio(t)) if parse_radio(t) else None}
for t in ("> 869.432,62.5,7,5", "> 866.5,62.5,8,5", "Unknown command")
],
"airtime": [
{"n": n, "sf": sf, "bw": bw, "cr": cr, "secs": round(lora_airtime(n, sf, bw, cr), 9)}
for n, sf, bw, cr in ((48, 8, 62.5, 1), (172, 7, 62.5, 1), (150, 7, 62.5, 4), (255, 12, 125.0, 1))
],
"frame_len": {
"req": [{"payload": n, "len": req_frame_len(n)} for n in (2, 139, 160)],
"text": [{"text": t, "len": text_frame_len(t)} for t in ("fw status", "x" * 40)],
},
}
if __name__ == "__main__":
print(json.dumps(build(), indent=1, sort_keys=True))