feat(hbp): TS1_STATIC / TS2_STATIC reach every hotspot of their MASTER

On the shared inject-only MASTER each hotspot only got the static TGs of
its own OPTIONS, so the TS1_STATIC / TS2_STATIC of adn-server.yaml reached
a hotspot only while it was the sole one connected. The yaml lists are now
kept per MASTER when the config is loaded or reloaded and added to every
hotspot's static TGs on that MASTER, on top of its OPTIONS. A SIGHUP
updates connected hotspots and the MASTER's bridge legs.
pull/125/head
Rodrigo Pérez 2 months ago
parent dc8ca8d9d1
commit b9101552c8

@ -132,6 +132,8 @@ SYSTEMS:
DEFAULT_UA_TIMER: 60
SINGLE_MODE: false
VOICE_IDENT: false
# Static TGs for every hotspot of this MASTER, besides the ones in each hotspot's own OPTIONS
# (comma-separated, e.g. "214,730"). Service TGs 9990-9999 are ignored.
TS1_STATIC: ""
TS2_STATIC: ""
DEFAULT_REFLECTOR: 0

@ -108,6 +108,19 @@ def static_tg_list(value: Any) -> list[str]:
return [text] if text else []
# Echo/parrot and on-demand services are never static (apply_startup_subscriptions skips them too).
_NOT_SYSTEM_STATIC = frozenset({"0", "1", "2", "3", "4", "5", "9", *(str(tg) for tg in range(9990, 10000))})
def system_static_tgs(sys_cfg: dict[str, Any]) -> tuple[tuple[str, ...], tuple[str, ...]]:
"""TS1_STATIC / TS2_STATIC as written in adn-server.yaml: static TGs for every hotspot of the MASTER."""
def usable(value: Any) -> tuple[str, ...]:
tgs = dedupe_static_tg_list(static_tg_list(value))
return tuple(tg for tg in tgs if tg.isdigit() and tg not in _NOT_SYSTEM_STATIC)
return usable(sys_cfg.get("TS1_STATIC")), usable(sys_cfg.get("TS2_STATIC"))
def dedupe_static_tg_list(parts: list[str]) -> list[str]:
"""Collapse duplicate TG ids within one TS list (order preserved)."""
out: list[str] = []

@ -42,18 +42,23 @@ def invalidate_peer_options_cache(peer: dict[str, Any]) -> None:
peer.pop("_CACHED_OPTIONS_FIELDS", None)
_NO_STATIC: tuple[tuple[str, ...], tuple[str, ...]] = ((), ())
def cached_peer_static_tgs(peer: dict[str, Any]) -> tuple[tuple[str, ...], tuple[str, ...]]:
"""Memoize ``parse_peer_options_static`` per peer OPTIONS blob."""
"""The hotspot's static TGs: its OPTIONS plus its MASTER's TS1/TS2_STATIC, memoized."""
opts = peer.get("OPTIONS")
key = opts if isinstance(opts, bytes) else b""
system = peer.get("_SYSTEM_STATIC")
cached = peer.get("_CACHED_OPTIONS_STATIC")
if cached and cached[0] == key:
if cached and cached[0] is opts and cached[3] is system:
return cached[1], cached[2]
from adn_server.application.report.payloads import parse_peer_options_static
ts1, ts2 = parse_peer_options_static(opts)
t1, t2 = tuple(ts1), tuple(ts2)
peer["_CACHED_OPTIONS_STATIC"] = (key, t1, t2)
sys_ts1, sys_ts2 = system or _NO_STATIC
t1 = tuple(dict.fromkeys([*ts1, *sys_ts1]))
t2 = tuple(dict.fromkeys([*ts2, *sys_ts2]))
peer["_CACHED_OPTIONS_STATIC"] = (opts, t1, t2, system)
return t1, t2

@ -731,6 +731,9 @@ class SubscriptionTableMixin:
ts1_list, ts2_list = runtime
ts1_set.update(ts1_list)
ts2_set.update(ts2_list)
yaml_ts1, yaml_ts2 = sys_cfg.get("_YAML_STATIC") or ((), ())
ts1_set.update(int(tg) for tg in yaml_ts1)
ts2_set.update(int(tg) for tg in yaml_ts2)
protocols = self._get_protocols() if self._get_protocols else {}
proto = protocols.get(system_name)
peers = getattr(proto, "_peers", {}) if proto is not None else {}

@ -31,6 +31,8 @@ import logging
import socket
import time
from adn_server.application.report.payloads import system_static_tgs
def expand_generator(config: dict, logger: logging.Logger) -> None:
"""Replace MASTER systems with GENERATOR > 1 by SYSTEM-0, SYSTEM-1, ... (legacy generator)."""
@ -74,6 +76,8 @@ def ensure_system_runtime_config(config: dict) -> None:
for name, sys_cfg in config.get("SYSTEMS", {}).items():
if sys_cfg.get("MODE") == "MASTER":
sys_cfg.setdefault("PEERS", {})
# Read here, on the config as loaded: TS1/TS2_STATIC are later rewritten from the hotspots' OPTIONS.
sys_cfg["_YAML_STATIC"] = system_static_tgs(sys_cfg)
elif sys_cfg.get("MODE") == "PEER":
sys_cfg.setdefault("STATS", {
"CONNECTION": "NO",

@ -362,6 +362,8 @@ class HBPProtocol(DatagramProtocol):
self._peer_mesh_config_cache = None
if sys_cfg.get("MODE") == "MASTER":
self._peers = sys_cfg.setdefault("PEERS", {})
for peer in self._peers.values():
peer["_SYSTEM_STATIC"] = sys_cfg.get("_YAML_STATIC")
self._refresh_connected_peer_count()
self._mark_downlink_index_dirty()
@ -1743,6 +1745,7 @@ class HBPProtocol(DatagramProtocol):
_this_peer = self._peers[_peer_id]
_this_peer["CONNECTION"] = "YES"
_this_peer["CONNECTED"] = time.time()
_this_peer["_SYSTEM_STATIC"] = self._config.get("_YAML_STATIC")
_this_peer["LAST_PING"] = time.time()
_this_peer["CALLSIGN"] = _data[8:16]
_this_peer["RX_FREQ"] = _data[16:25]

@ -0,0 +1,128 @@
# ADN DMR Peer Server - tests infrastructure TS1_STATIC / TS2_STATIC of a MASTER
#
# Copyright (C) 2026 Rodrigo Pérez, CE5RPY <ce5rpy@qmd.cl>
#
###############################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
###############################################################################
"""TS1_STATIC / TS2_STATIC in adn-server.yaml only reached a MASTER's hotspots when one was connected."""
from __future__ import annotations
from tests.harness.deterministic import DeterministicScenario, PacketSpec, minimal_config
from tests.support.hbp_repeat_stack import build_hbp_repeat_stack
from adn_server.application.report.payloads import system_static_tgs
from adn_server.application.routing.peer_downlink_index import invalidate_peer_options_cache
from adn_server.domain import bytes_4
from adn_server.infrastructure.config_normalizer import ensure_system_runtime_config
from adn_server.infrastructure.hbp_constants import DMRD, RPTC, RPTK, RPTL
from adn_server.infrastructure.twisted_adapters.udp_hbp import _calc_hash, _get_passphrase_bytes
_A, _B, _C = bytes_4(730044401), bytes_4(730044402), bytes_4(730044403)
_ADDR = {_A: ("10.0.0.1", 62001), _B: ("10.0.0.2", 62002), _C: ("10.0.0.3", 62003)}
_STATIC_TG = 730777
def _stack(ts2_static: str = str(_STATIC_TG), other_master_static: str = "", ts1_static: str = ""):
stack = build_hbp_repeat_stack(talker_alias=False, system_name="MASTER-A")
stack.config["PROXY"] = {"TARGET_SYSTEM": "MASTER-A"}
stack.hbp._CONFIG = stack.config
stack.config["SYSTEMS"]["MASTER-A"].update(TS1_STATIC=ts1_static, TS2_STATIC=ts2_static, PASSPHRASE="passw0rd")
if other_master_static:
stack.config["SYSTEMS"]["MASTER-B"] = {"MODE": "MASTER", "TS2_STATIC": other_master_static}
ensure_system_runtime_config(stack.config)
return stack
def _login(stack, peer_id: bytes, options: str | None = None) -> None:
addr = _ADDR[peer_id]
stack.hbp.datagramReceived(RPTL + peer_id, addr)
salt = bytes_4(stack.hbp._peers[peer_id]["SALT"])
stack.hbp.datagramReceived(RPTK + peer_id + _calc_hash(salt, _get_passphrase_bytes(stack.hbp._config)), addr)
stack.hbp.datagramReceived(RPTC + peer_id + b"CE5RPY " + b"\x00" * 85 + b"4", addr)
peer = stack.hbp._peers[peer_id]
assert peer["CONNECTION"] == "YES"
if options is not None:
peer["OPTIONS"] = options.encode()
invalidate_peer_options_cache(peer)
stack.hbp._mark_downlink_index_dirty()
def _heard(stack, tg: int, slot: int = 2) -> set[bytes]:
stack.transport.clear()
spec = PacketSpec(peer_id=714009901, rf_src=7140099, dst_id=tg, slot=slot, stream_id=0x0D0D0D0D, payload=b"\x00" * 33)
stack.hbp.send_peers(DeterministicScenario.voice_burst_spec(spec, seq=1, dtype_vseq=1).data())
return {p for p, addr in _ADDR.items() if any(pkt[:4] == DMRD for pkt in stack.transport.for_addr(addr))}
def test_a_yaml_static_tg_reaches_every_hotspot_of_its_master() -> None:
stack = _stack()
_login(stack, _A, "TS2=91;")
_login(stack, _B)
_login(stack, _C, "TS2=730778;")
assert _heard(stack, _STATIC_TG) == {_A, _B, _C}
def test_a_yaml_ts1_static_tg_reaches_every_hotspot_on_ts1() -> None:
stack = _stack(ts2_static="", ts1_static="730555")
_login(stack, _A, "TS2=91;")
_login(stack, _B)
assert _heard(stack, 730555, slot=1) == {_A, _B}
def test_each_hotspots_own_options_still_apply() -> None:
stack = _stack()
_login(stack, _A, "TS2=91;")
_login(stack, _B)
_login(stack, _C, "TS2=730778;")
assert _heard(stack, 730778) == {_C}
assert _heard(stack, 91) == {_A}
def test_another_masters_static_tgs_stay_on_that_master() -> None:
stack = _stack(ts2_static="", other_master_static=str(_STATIC_TG))
_login(stack, _A, "TS2=91;")
_login(stack, _B)
assert _heard(stack, _STATIC_TG) == set()
def test_a_reload_gives_connected_hotspots_the_new_static_tgs() -> None:
stack = _stack(ts2_static="")
_login(stack, _A, "TS2=91;")
_login(stack, _B)
assert _heard(stack, _STATIC_TG) == set()
stack.config["SYSTEMS"]["MASTER-A"]["TS2_STATIC"] = str(_STATIC_TG)
ensure_system_runtime_config(stack.config)
stack.hbp.apply_system_config(stack.config)
assert _heard(stack, _STATIC_TG) == {_A, _B}
def test_service_tgs_are_never_static_for_every_hotspot() -> None:
assert system_static_tgs({"TS1_STATIC": "9990,730, 214", "TS2_STATIC": 9}) == (("730", "214"), ())
def test_the_master_keeps_its_yaml_static_bridge_leg_after_hotspots_rewrite_ts2_static() -> None:
config = minimal_config(("SYSTEM",))
config["SYSTEMS"]["SYSTEM"].update(TS1_STATIC="730555", TS2_STATIC=str(_STATIC_TG))
ensure_system_runtime_config(config)
config["SYSTEMS"]["SYSTEM"].update(TS1_STATIC="", TS2_STATIC="91") # union of the hotspots' OPTIONS, as options_config writes it
sc = DeterministicScenario(config=config)
sc.routing.apply_startup_subscriptions()
table = sc.routing.routing_table_for_report()
for tg, ts in ((_STATIC_TG, 2), (730555, 1)):
legs = table.get(str(tg), [])
assert any(leg.get("SYSTEM") == "SYSTEM" and leg.get("TS") == ts and leg.get("ACTIVE") for leg in legs), tg
Loading…
Cancel
Save

Powered by TurnKey Linux.