Fix APRS counters, BBS reads and chat parsing; add reliable packet TX helpers

This commit is contained in:
alberto committed 2026-10-29 16:37:51 +01:00
1 parent 0668a73d40
commit 3643638090
15 files changed
+119 -56

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
# VR-N7500 Bluetooth bridge helpers
`packet_tx.py` provides a bounded AX.25 packet queue and a fresh-status transmit gate for custom Bluetooth bridges. It does not configure a radio or open a serial port. Station-specific bridge scripts and radio settings are not distributed here.
Integration:
- Call `RadioGate.update(monotonic_time, transmitting_or_receiving_or_rssi_nonzero)` on each radio status notification.
- Poll status every 150 ms while packets are queued. `allows()` requires status no older than 350 ms and 300 ms of quiet before starting a packet. Pass `starting=False` for continuation fragments.
- Enqueue complete packets with `PacketTx.add()`. Send only the fragment returned by `next()` and wait for its acknowledgement before requesting another.
- Pass the reply status to `ack()`. Status 6 restarts only the affected packet, with backoff and at most three retries. Other pending packets are retained. Permanent errors discard only the affected packet.
- Clear the queue on disconnect, frequency changes or transmission suspension. Never carry stale frames to another channel. Retain an acknowledgement timeout in the bridge.
The module uses monotonic timestamps. Pending packets expire after 20 seconds; one second separates complete packets. These controls reduce incorrect-state replies but cannot eliminate races with incoming RF traffic.
Run tests with `python integrations/vr-n7500/test_packet_tx.py`.
+42
View File
@@ -0,0 +1,42 @@
"""Bounded, packet-aware transmit queue for the Bluetooth TNC."""
from collections import deque
class PacketTx:
def __init__(self):self.pending=deque();self.current=None;self.index=0;self.retries=0;self.waiting=False;self.sentAt=0;self.readyAt=0;self.dropped=0;self.retried=0
def __bool__(self):return self.current is not None or bool(self.pending)
def __len__(self):return len(self.pending)+(self.current is not None)
def clear(self):self.pending.clear();self.current=None;self.waiting=False;self.index=0
def add(self,packet,now):
if len(self)>=128:return False
parts=[]
for i,pos in enumerate(range(0,len(packet),50)):
part=packet[pos:pos+50];parts.append(bytes([i|64|(128 if pos+len(part)==len(packet) else 0)])+part+b'\x00')
self.pending.append((now,parts));return True
def next(self,now):
if self.waiting or now<self.readyAt:return None
if self.current is None:
while self.pending and now-self.pending[0][0]>20:self.pending.popleft();self.dropped+=1
if not self.pending:return None
self.current=self.pending.popleft();self.index=0;self.retries=0
if now-self.current[0]>20:self.current=None;self.dropped+=1;return None
self.waiting=True;self.sentAt=now;return self.current[1][self.index]
def ack(self,status,now):
if not self.waiting:return 'unsolicited'
self.waiting=False
if status==0:
self.index+=1
if self.index==len(self.current[1]):self.current=None;self.readyAt=now+1.0
return 'accepted'
if status==6 and self.retries<3 and now-self.current[0]<20:
self.retries+=1;self.retried+=1;self.index=0;self.readyAt=now+1.5
return 'retry'
self.current=None;self.dropped+=1;self.readyAt=now+1.5;return 'dropped'
class RadioGate:
"""Require fresh status and a quiet channel before starting a packet."""
def __init__(self):self.updated=None;self.quietSince=None;self.blocked=True
def update(self,now,blocked):
self.updated=now;self.blocked=blocked
if blocked:self.quietSince=None
elif self.quietSince is None:self.quietSince=now
def allows(self,now,starting=True):
return self.updated is not None and 0<=now-self.updated<=0.35 and not self.blocked and (not starting or self.quietSince is not None and now-self.quietSince>=0.3)
+25
View File
@@ -0,0 +1,25 @@
import sys,pathlib,unittest
sys.path.insert(0,str(pathlib.Path(__file__).parent))
from packet_tx import PacketTx,RadioGate
class Tests(unittest.TestCase):
def test_retry_restarts_packet_and_preserves_next(self):
q=PacketTx();q.add(b'A'*80,0);q.add(b'B'*10,0)
first=q.next(0);q.ack(0,.1);second=q.next(.2);self.assertNotEqual(first,second)
self.assertEqual(q.ack(6,.3),'retry');self.assertIsNone(q.next(1));self.assertEqual(q.next(2),first)
q.ack(0,2.1);q.next(2.2);q.ack(0,2.3);self.assertIsNone(q.next(2.4));self.assertIn(b'B'*10,q.next(4))
def test_retry_limit_drops_only_current(self):
q=PacketTx();q.add(b'A',0);q.add(b'B',0)
for i in range(4):q.next(i*2);action=q.ack(6,i*2+.1)
self.assertEqual(action,'dropped');self.assertEqual(q.dropped,1);self.assertIn(b'B',q.next(9))
def test_expiry_and_no_unacknowledged_advance(self):
q=PacketTx();q.add(b'A'*80,0);q.next(0);self.assertIsNone(q.next(1));q.ack(0,1);self.assertIsNone(q.next(21));self.assertEqual(q.dropped,1)
def test_permanent_error_preserves_pending(self):
q=PacketTx();q.add(b'A',0);q.add(b'B',0);q.next(0);self.assertEqual(q.ack(5,.1),'dropped');self.assertIn(b'B',q.next(2))
class GateTests(unittest.TestCase):
def test_unknown_stale_and_recent_busy_never_start(self):
g=RadioGate();self.assertFalse(g.allows(1));g.update(1,False);self.assertFalse(g.allows(1.1));g.update(1.2,False);self.assertTrue(g.allows(1.31));self.assertFalse(g.allows(2));g.update(2,True);g.update(2.1,False);self.assertFalse(g.allows(2.2))
def test_fragments_continue_without_repeated_quiet_delay(self):
g=RadioGate();g.update(1,False);self.assertTrue(g.allows(1.1,starting=False));g.update(1.2,True);self.assertFalse(g.allows(1.21,starting=False));g.update(1.3,False);self.assertTrue(g.allows(1.31,starting=False));self.assertFalse(g.allows(1.31))
def test_new_reception_resets_quiet_period(self):
g=RadioGate();g.update(0,False);g.update(.2,False);self.assertTrue(g.allows(.31));g.update(.32,True);g.update(.4,False);g.update(.6,False);self.assertFalse(g.allows(.65));self.assertTrue(g.allows(.71))
unittest.main()