From 8e55cbd764e7a712165e9836f65349b979c30c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20P=C3=A9rez?= Date: Wed, 14 Oct 2026 00:31:15 -0300 Subject: [PATCH] fix(routing): a terminator behind the last sequence still ends the call DVSwitch Mobile sends the terminator with seq 0 after ..., 28, 29. Since #127 seq_advance takes it as a late packet and the ingress drops it, so on HBP and OBP the call only ended on the 5 s stream timeout (5-10 s late on the dashboard) and a TG 4000 reply never fired. Before, seq 0 skipped the checks. A terminator is now always the next packet; everything else keeps #127's late/duplicate handling. --- .../application/routing/hbp_forward.py | 6 +++++- src/adn_server/application/routing/helpers.py | 8 +++++-- .../application/routing/obp_forward.py | 7 +++++-- .../application/routing_use_cases.py | 1 + tests/routing/test_obp_call_end_gaps.py | 21 +++++++++++++++++++ tests/routing/test_seq_wrap_late_packet.py | 17 +++++++++++++++ 6 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/adn_server/application/routing/hbp_forward.py b/src/adn_server/application/routing/hbp_forward.py index 426d15c..d8b1860 100644 --- a/src/adn_server/application/routing/hbp_forward.py +++ b/src/adn_server/application/routing/hbp_forward.py @@ -174,6 +174,7 @@ class HbpForwardMixin: pkt_time: float, *, loop_guard_check: bool = True, + terminator: bool = False, ) -> bool: """Legacy routerHBP group/vcsbk packet control (~3270-3399). @@ -343,7 +344,10 @@ class HbpForwardMixin: src_proto._obp_send_bcsq(dst_id, stream_id) _slot_st["_bcsq"] = True return False - _step = seq_advance(_slot_st.get("lastSeq"), seq, pkt_time - _slot_st.get("lastSeqTime", pkt_time)) + _step = seq_advance( + _slot_st.get("lastSeq"), seq, pkt_time - _slot_st.get("lastSeqTime", pkt_time), + terminator=terminator, + ) if _step <= 0: # same sequence again, or a late / out-of-order packet _slot_st["loss"] = _slot_st.get("loss", 0) + 1 return False diff --git a/src/adn_server/application/routing/helpers.py b/src/adn_server/application/routing/helpers.py index d053f11..7812225 100644 --- a/src/adn_server/application/routing/helpers.py +++ b/src/adn_server/application/routing/helpers.py @@ -71,7 +71,7 @@ SIMPLEX_VOICE_SLOT = 2 SEQ_RESYNC_SILENCE_S = 1.0 -def seq_advance(last_seq: Any, seq: int, silence: float = 0.0) -> int: +def seq_advance(last_seq: Any, seq: int, silence: float = 0.0, *, terminator: bool = False) -> int: """Steps from ``last_seq`` to ``seq`` on the 8-bit HBP/OBP sequence, which wraps 255 -> 0. 1: the next packet (or no previous one: ``last_seq`` False/None). 2..127: a forward jump, @@ -85,6 +85,10 @@ def seq_advance(last_seq: Any, seq: int, silence: float = 0.0) -> int: ``SEQ_RESYNC_SILENCE_S`` the stream is resuming from a pause, where the jump can be 128 or more (or a whole lap, 0): the frame is taken as a forward jump of ``step`` (at least 1) and the caller resyncs to it. + + A ``terminator`` behind ``last_seq`` is still the next packet: it closes the call, and + DVSwitch Mobile sends it with ``seq`` 0 whatever came before (dropped, the call only ended + on the 5 s stream timeout). """ if last_seq is False or last_seq is None: return 1 @@ -94,7 +98,7 @@ def seq_advance(last_seq: Any, seq: int, silence: float = 0.0) -> int: if step == 0 and seq == 0: return 1 if step >= 128: - return 1 if seq == 1 else -1 + return 1 if seq == 1 or terminator else -1 return step diff --git a/src/adn_server/application/routing/obp_forward.py b/src/adn_server/application/routing/obp_forward.py index 925698a..7e407c7 100644 --- a/src/adn_server/application/routing/obp_forward.py +++ b/src/adn_server/application/routing/obp_forward.py @@ -49,7 +49,7 @@ from hashlib import blake2b from time import perf_counter from typing import Any -from ...domain import HBPF_DATA_SYNC, HBPF_SLT_VHEAD, int_id +from ...domain import HBPF_DATA_SYNC, HBPF_SLT_VHEAD, HBPF_SLT_VTERM, int_id from ...domain.dmr import decode from ...domain.dmr.const import LC_OPT from ..subscription.obp_source_ops import ensure_obp_source_for_tg_store, obp_source_needs_ensure @@ -394,7 +394,10 @@ class ObpForwardMixin: ((st["loss"] / st["packets"]) * 100) if st.get("packets") else 0.0, ) return False - _step = seq_advance(st["lastSeq"], seq, pkt_time - st.get("lastSeqTime", pkt_time)) + _step = seq_advance( + st["lastSeq"], seq, pkt_time - st.get("lastSeqTime", pkt_time), + terminator=frame_type == HBPF_DATA_SYNC and dtype_vseq == HBPF_SLT_VTERM, + ) if _step == 0: st["loss"] += 1 logger.debug( diff --git a/src/adn_server/application/routing_use_cases.py b/src/adn_server/application/routing_use_cases.py index fbc01ce..2c989f9 100644 --- a/src/adn_server/application/routing_use_cases.py +++ b/src/adn_server/application/routing_use_cases.py @@ -279,6 +279,7 @@ class RoutingUseCases( if not self._hbp_group_voice_ingress_controls( system_name, peer_id, rf_src, dst_id, seq, slot, stream_id, data, pkt_time, loop_guard_check=plugin_origin is None and not synthetic_announcement, + terminator=frame_type == HBPF_DATA_SYNC and dtype_vseq == HBPF_SLT_VTERM, ): return # Arm ON in-band rules on VHEAD (echo 9990 and UA bridges); VTERM handled in udp_hbp too. diff --git a/tests/routing/test_obp_call_end_gaps.py b/tests/routing/test_obp_call_end_gaps.py index d536f28..b78b3e9 100644 --- a/tests/routing/test_obp_call_end_gaps.py +++ b/tests/routing/test_obp_call_end_gaps.py @@ -111,6 +111,8 @@ def test_seq_advance_wraps_and_keeps_late_packets_behind() -> None: assert seq_advance(10, 9) == -1 assert seq_advance(9, 9) == 0 assert seq_advance(0, 0) == 1 # repeated voice header + assert seq_advance(29, 0, terminator=True) == 1 # a terminator closes the call whatever its sequence + assert seq_advance(29, 0) == -1 assert seq_advance(50, 1) == 1 # source restarting its count (1 behind the last) # after a silence the stream is resuming, whatever the jump assert seq_advance(10, 160, silence=9.0) == 150 @@ -137,3 +139,22 @@ def test_a_stream_resuming_after_a_long_pause_is_forwarded(caplog: pytest.LogCap line = _call_end(caplog, _PAUSE, sent) assert sent[-41:] == list(range(160, 201)), "all 40 frames after the pause and the terminator" assert _field(line, "Max gap") == pytest.approx(9.0, abs=0.01) + + +def test_a_terminator_with_its_sequence_reset_ends_the_call(caplog: pytest.LogCaptureFixture) -> None: + # DVSwitch Mobile sends the terminator with seq 0: dropped as late, no CALL END until the stream timeout. + config = minimal_config(("MASTER-A",)) + add_openbridge_system(config, "OBP-1") + sc = DeterministicScenario(config=config) + sc.seed_routing_table(active_routing_table(_TG, (("MASTER-A", 2), ("OBP-1", 1)))) + spec = PacketSpec(dst_id=_TG, stream_id=0x0D0D0D0D, rf_src=7300391, slot=1) + with caplog.at_level(logging.INFO), patch_routing_wall_time(sc.clock): + sc.inject_obp("OBP-1", DeterministicScenario.voice_head_spec(spec)) + for seq in range(1, 30): + sc.clock.advance(0.06) + sc.inject_obp("OBP-1", DeterministicScenario.voice_burst_spec(spec, seq=seq, dtype_vseq=(seq - 1) % 6 + 1)) + sc.clock.advance(0.06) + sc.inject_obp("OBP-1", DeterministicScenario.voice_term_spec(spec, seq=0)) + lines = [r.getMessage() for r in caplog.records if "*CALL END*" in r.getMessage()] + assert len(lines) == 1, "no CALL END: the terminator was dropped" + assert _field(lines[0], "Loss") == 0.0 diff --git a/tests/routing/test_seq_wrap_late_packet.py b/tests/routing/test_seq_wrap_late_packet.py index b32e654..4d782f4 100644 --- a/tests/routing/test_seq_wrap_late_packet.py +++ b/tests/routing/test_seq_wrap_late_packet.py @@ -58,3 +58,20 @@ def test_an_over_resuming_after_a_long_pause_is_forwarded() -> None: sent = [parse_dmr_fields(p.packet)["seq"] for p in scenario.capture.for_system("MASTER-B")] assert [s for s in sent if s >= 160] == list(range(160, 201)) + + +def test_a_terminator_with_its_sequence_reset_ends_the_over() -> None: + # DVSwitch Mobile, as captured on 7302: header seq 0, bursts 1..29, terminator seq 0 again. + # Taken as a late packet it was dropped, and the over only ended on the 5 s stream timeout. + scenario = DeterministicScenario(routing_table=active_routing_table(91, (("MASTER-A", 2), ("MASTER-B", 2)))) + base = PacketSpec(dst_id=91, stream_id=0x10101010, rf_src=7300391, peer_id=1001) + t = scenario.clock.time() + scenario.inject_hbp("MASTER-A", DeterministicScenario.voice_head_spec(base), ingress_pkt_time=t) + for seq in range(1, 30): + spec = DeterministicScenario.voice_burst_spec(base, seq=seq, dtype_vseq=(seq - 1) % 6 + 1) + scenario.inject_hbp("MASTER-A", spec, ingress_pkt_time=t + 0.06 * seq) + scenario.inject_hbp("MASTER-A", DeterministicScenario.voice_term_spec(base, seq=0), ingress_pkt_time=t + 0.06 * 30) + + last = parse_dmr_fields(scenario.capture.for_system("MASTER-B")[-1].packet) + assert (last["frame_type"], last["dtype_vseq"]) == (2, 2), "the terminator is forwarded" + assert scenario.protocols["MASTER-A"].STATUS[2]["RX_TYPE"] == 2 # the over has ended