From e536f0939c0a0c77475f9fa6647339c57143664a Mon Sep 17 00:00:00 2001 From: alberto Date: Sat, 17 Oct 2026 09:41:54 +0200 Subject: [PATCH] Prioritize idle packet radio preparation for forwarding and Winlink --- README.md | 2 ++ packet-preparation.mjs | 8 ++++++++ packet-preparation.test.mjs | 16 ++++++++++++++++ radio-slot.mjs | 4 ++-- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 packet-preparation.mjs create mode 100644 packet-preparation.test.mjs diff --git a/README.md b/README.md index 6413430..34b9391 100644 --- a/README.md +++ b/README.md @@ -195,3 +195,5 @@ Tests cover protocol parsing, mail/attachments, journal, gateway validation, pro Third-party PDF.js, font and CMap licences are retained under `public/vendor/pdfjs`; map attribution is in `public/WORLD-MAP-LICENSE.txt`. Packet controller integrations must acknowledge a reserved slot with `pttReady: true`, after checking that their BPQ PTT backend responds. A modem TCP connection alone is insufficient. The slot status must also contain the matching request ID, frequency, VHF phase and a fresh timestamp. Manual alternate routes share the partner queue; their native HF row is reserved during use and is not counted as a second active session. + +The local packet controller can use `packet-preparation.mjs` to prioritize an idle manual Packet request by advancing BPQ's native scan toward its configured packet entry. This retains the native modem interlock (for example VARA V0/LISTEN OFF), rather than directly retuning from an HF receive slot. Do not advance while attached, connecting, transmitting, or paused. Forwarding and Winlink share the same slot handshake; its measured `preparationMs` is saved with the reservation. A single radio still requires exclusive access to the selected frequency. diff --git a/packet-preparation.mjs b/packet-preparation.mjs new file mode 100644 index 0000000..2701077 --- /dev/null +++ b/packet-preparation.mjs @@ -0,0 +1,8 @@ +// Accelerate BPQ's native scanner rather than bypassing its V0 modem interlock. +export function canExpeditePacket({request,manualRequestId,txSuspended,ptt,connections,radioState,users,radioPorts=null,lastPtt=0,now=Date.now()}){ + const attached=[...(users||'').matchAll(/(?:TNC Uplink Port|Attached to Port)\s+(\d+)\//g)].some(m=>!radioPorts||radioPorts.includes(Number(m[1]))); + return Boolean(request&&request.id!==manualRequestId&&!txSuspended&&!ptt&&!connections&&now-lastPtt>=1000&&!/^(?:connecting|connected|calling|interlocked)\b/i.test(radioState||'')&&!attached); +} +export function shouldAdvancePacketScan({frequency,packetFrequencyMHz,lastFrequency,lastAdvance=0,now=Date.now()}){ + return Number.isFinite(frequency)&&Number.isFinite(packetFrequencyMHz)&&Math.abs(frequency-packetFrequencyMHz)>0.0001&&(frequency!==lastFrequency||now-lastAdvance>=1500); +} diff --git a/packet-preparation.test.mjs b/packet-preparation.test.mjs new file mode 100644 index 0000000..cc90917 --- /dev/null +++ b/packet-preparation.test.mjs @@ -0,0 +1,16 @@ +import {test} from 'node:test'; +import assert from 'node:assert/strict'; +import {canExpeditePacket,shouldAdvancePacketScan} from './packet-preparation.mjs'; +test('a manual packet request may advance the scanner from either HF band while idle',()=>{ + const idle={request:{id:'new'},manualRequestId:null,ptt:false,connections:0,radioState:'Free',users:'',lastPtt:0,now:100000}; + assert.equal(canExpeditePacket(idle),true);assert.equal(canExpeditePacket({...idle,radioPorts:[4,7],users:'Attached to Port 2/1'}),true);assert.equal(canExpeditePacket({...idle,radioPorts:[4,7],users:'Attached to Port 7/1'}),false); + for(const busy of [{txSuspended:true},{ptt:true},{connections:1},{radioState:'Connecting to remote'},{radioState:'Connected'},{radioState:'Interlocked'},{users:'Attached to Port 1/1'},{users:'TNC Uplink Port 3/1'},{lastPtt:99500},{manualRequestId:'new'},{request:null}])assert.equal(canExpeditePacket({...idle,...busy}),false,JSON.stringify(busy)); + for(const frequency of [7.050,14.122])assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency,now:100000}),true); + assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency:144.950,now:100000}),false); + assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency:NaN,now:100000}),false); +}); +test('native scan commands wait for a frequency change or a bounded retry interval',()=>{ + assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency:7.050,lastFrequency:7.050,lastAdvance:10000,now:10500}),false); + assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency:14.122,lastFrequency:7.050,lastAdvance:10000,now:10500}),true); + assert.equal(shouldAdvancePacketScan({packetFrequencyMHz:144.950,frequency:7.050,lastFrequency:7.050,lastAdvance:10000,now:11500}),true); +}); diff --git a/radio-slot.mjs b/radio-slot.mjs index cdbd987..df16277 100644 --- a/radio-slot.mjs +++ b/radio-slot.mjs @@ -2,10 +2,10 @@ import {readFile,writeFile,rename} from 'node:fs/promises'; import {randomUUID} from 'node:crypto'; export async function reservePacketSlot(client,controller,frequencyMHz,port){ if(!controller)return; - const request={id:randomUUID(),frequencyMHz,expiresAt:Date.now()+90000};client.job.controller={...controller,id:request.id};await client.persist(); + const began=Date.now(),request={id:randomUUID(),frequencyMHz,expiresAt:began+90000};client.job.controller={...controller,id:request.id};await client.persist(); await writeFile(controller.requestFile+'.tmp',JSON.stringify(request));await rename(controller.requestFile+'.tmp',controller.requestFile); const deadline=Date.now()+65000; - while(Date.now()setTimeout(resolve,300));} + while(Date.now()setTimeout(resolve,300));} throw Error('Modem packet non pronto: nessuna chiamata avviata'); } export async function releasePacketSlot(client){