Files
meshcore-ansible/plugins/module_utils/meshcore_fw.py
T
tthandClaude Opus 5.5 0a4cfafc78
ci / test (push) Successful in 2m39s
tth.meshcore: MeshCore repeaters as code, over RF
Ansible collection that converges MeshCore repeaters through a companion
radio (TCP, USB serial or BLE): admin ACL, CLI prefs, region tree,
firmware facts, companion remote-CLI ACL, and the meshcore-tth extras
(BLE on/off and own BLE PIN, channel-hash filter, message age, advert
fairness). Every change is read, diffed, sent and read back; --check
only reads. ansible-test sanity, unit tests and a galaxy build in CI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 02:23:24 +01:00

79 lines
2.8 KiB
Python

"""Radio-free parsing of `ver` and of the BLE commands of meshcore-tth.
Kept apart from the modules so it can be unit-tested on real replies (see
tests/unit) -- every rule here once cost time on real hardware.
"""
import re
# Stamp written by meshcore-tth's tools/build_version.py: <upstream tag>-tth<sha>[+].
# '+' = built from a dirty tree: nobody can tell what is in it, so such a node
# always counts as outdated.
# meshcore-tth v1.18.0-tth0781378; Solo v2.0-tth8345a5a, or its CI release name
# v2.0+tth.20261011195120 (two-part version, optional -rcN).
VERSION_RE = re.compile(r"v\d+(?:\.\d+)+(?:-rc\d+)?(?:-tth-?[0-9a-f]{4,}\+?|\+tth\.\d{14})")
# Stock MeshCore: `v1.18.0 (Build: ...)`. Only trusted right before "(Build:",
# a bare vX.Y.Z elsewhere in a reply or an ELF is too easy to mistake.
UPSTREAM_RE = re.compile(r"(v\d+(?:\.\d+)+(?:-rc\d+)?) \(Build: ")
def parse_version(text):
"""First firmware stamp in a `ver` reply / board_probe line / ELF, or None."""
m = VERSION_RE.search(text or "")
if m:
return m.group(0)
m = UPSTREAM_RE.search(text or "")
return m.group(1) if m else None
def fw_state(current, wanted):
"""'current' | 'outdated' | 'dirty' | 'unknown' (no readable version)."""
if not wanted or wanted.endswith("+"):
raise ValueError(f"wanted version {wanted!r} is not a clean stamp -- build from a clean tree")
if not current:
return "unknown"
if current.endswith("+"):
return "dirty"
return "current" if current == wanted else "outdated"
# ── BLE CLI replies (examples/simple_repeater/main.cpp) ─────────────────────
def parse_ble(text):
"""`ble` -> 'on' | 'off'. Reply: 'BLE: on (advertising)'."""
m = re.match(r"^BLE: (on|off)\b", (text or "").strip())
if not m:
raise ValueError(f"unexpected `ble` reply: {text!r}")
return m.group(1)
def parse_ble_pin(text):
"""`ble` -> 'own' | 'build default', or None when the firmware predates `ble pin`.
Reply: 'BLE: off, PIN own'. The PIN itself is never printed.
"""
m = re.search(r", PIN (own|build default)\s*$", (text or "").strip())
return m.group(1) if m else None
def ble_pin_valid(pin):
return isinstance(pin, str) and len(pin) == 6 and pin.isdigit()
def parse_blepwr(text):
"""`blepwr` -> connection tx power in dBm. Reply: 'BLE conn tx: 0 dBm, adv tx: 8 dBm'."""
m = re.match(r"^BLE conn tx: (-?\d+) dBm", (text or "").strip())
if not m:
raise ValueError(f"unexpected `blepwr` reply: {text!r}")
return int(m.group(1))
BLE_CONN_TX_VALID = {-40, -20, -16, -12, -8, -4, 0, 2, 3, 4, 5, 6, 7, 8}
def blepwr_set_ok(text):
"""A `blepwr <n>` reply proves the value is applied AND saved."""
t = (text or "").strip()
return t.startswith("OK") and t.endswith(", saved")