Mercury Modem
Mercury ARQ/Broadcast modem
Loading...
Searching...
No Matches
arq.h
Go to the documentation of this file.
1/* HERMES Modem
2 *
3 * Copyright (C) 2025 Rhizomatica
4 * Author: Rafael Diniz <rafael@riseup.net>
5 *
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21#ifndef ARQ_H_
22#define ARQ_H_
23
24#define CALLSIGN_MAX_SIZE 16
25#define CALLSIGN_MAX_SECONDARY 4
26
27#define ARQ_BANDWIDTH_NARROW_HZ 500
28#define ARQ_BANDWIDTH_FULL_HZ 2300
29#define ARQ_BANDWIDTH_TACTICAL_HZ 2750
30
31#define RX 0
32#define TX 1
33
34#define HEADER_SIZE 1
35
36#define PACKET_ARQ_CONTROL 0x00
37#define PACKET_ARQ_DATA 0x01
38#define PACKET_ARQ_CALL 0x02
39#define PACKET_BROADCAST_CONTROL 0x03
40#define PACKET_BROADCAST_PAYLOAD 0x04
41
42#include <stdbool.h>
43#include <stddef.h>
44#include <stdint.h>
45
46#include "arq_events.h"
47
49typedef struct
50{
51 int TRX; // RX (0) or TX (1)
52 char my_call_sign[CALLSIGN_MAX_SIZE];
55 char src_addr[CALLSIGN_MAX_SIZE], dst_addr[CALLSIGN_MAX_SIZE];
58 bool listen;
59 int bw; // configured local bandwidth in Hz
60 int session_bw; // negotiated session bandwidth in Hz (0 = not negotiated)
61 int retry_slots; // 0 = use compiled default
62 size_t frame_size;
63 int mode;
64} arq_info;
65
67typedef enum
68{
74 ARQ_ACTION_TX_PATTERN = 5 /* emit a Welch-Costas MFSK pattern: no coded
75 * frame, pattern_kind says which; ~0.64 s */
77
79typedef enum
80{
81 ARQ_PATTERN_ACK = 0, /* "got it" */
82 ARQ_PATTERN_BREAK = 1 /* "got it: the block is delivered"; reaches
83 * the FSM as RX_PATTERN with pattern_break */
85
86/* A keydown of separate bursts -- each its own preamble, frame and postamble,
87 * in its own mode -- with a silence before each but the first (the carousel
88 * data plane's rounds). Heap-allocated by the enqueuer; whoever dequeues the
89 * action frees it. */
90/* A NAV header (mfsk.h) and the silence after it, ahead of a keydown's first
91 * frame: what the carousel adds to a headed keydown's lead. */
92#define ARQ_NAV_GAP_MS 60
93#define ARQ_NAV_LEAD_MS (640 + ARQ_NAV_GAP_MS)
94#define ARQ_KEYDOWN_FRAMES 17
95#define ARQ_KEYDOWN_FRAME_MAX 1280
96typedef struct
97{
98 int n;
99 uint16_t crc_seed; /* XORed into every frame's CRC16 (0: plain) */
100 bool nav; /* open with a NAV header (ARQ_NAV_LEAD_MS) */
101 struct
102 {
103 int mode;
104 uint32_t gap_ms;
105 size_t len;
106 uint8_t bytes[ARQ_KEYDOWN_FRAME_MAX];
109
111typedef struct
112{
114 int mode;
115 size_t frame_size; /* bytes per frame */
116 int frame_count; /* frames in this PTT burst (>= 1); the modem reads
117 * frame_count * frame_size bytes and modulates them
118 * behind a single preamble */
119 arq_keydown_t *keydown; /* ARQ_ACTION_TX_KEYDOWN only */
120 int pattern_kind; /* arq_pattern_kind_t, ARQ_ACTION_TX_PATTERN only */
122
124typedef struct
125{
128 /* A pattern can arrive now: the RX pattern detector correlates only while
129 * this is set -- running it all the time costs the decoders CPU. */
131 /* A carousel session is up: the pattern detector runs throughout, for the
132 * peer's NAV headers (expect_pattern_ack still gates ACK/BREAK). */
134 /* Listening or accepting: a CALL can arrive on MFSK, and only the
135 * modem's MFSK call listener would decode it. */
137 /* How many spells of listening for calls have ended: the MFSK call
138 * listeners drop what they hold when it moves. listening_for_calls alone
139 * misses a LISTEN OFF whose LISTEN ON follows 1 ms later (BPQ32's scan
140 * hop): the modem reads it once per 20 ms. */
142 /* How many of them ended with the radio back to the host, no session
143 * after (LISTEN OFF, or a host DISCONNECT while answering): the control
144 * and payload decoders drop what they hold too. A DATAC16 CALL straddling
145 * a scan hop otherwise decodes on the next channel and is answered there
146 * (K7EK, 2026-11-07: decoded 1.4 and 1.9 s after the hop). */
148 /* The MFSK floor of my own bandwidth (arq_floor_mode), which callers at
149 * it escalate to: the one the MFSK call listener decodes. */
151 int trx;
154 int payload_mode; /* my TX mode (ISS direction) */
155 int peer_tx_mode; /* RX decoder mode = peer's TX mode (IRS direction) */
159 uint64_t tx_bytes;
160 uint64_t rx_bytes;
161 /* In a carousel session (in_carousel), for the UI: the mode of my last
162 * data round, of the last data frame I took, and of the rung the peer
163 * last reported hearing me on (carousel.h car_ui_*). -1: none. */
168
169extern arq_info arq_conn;
170
171/* Thread-safe accessors for arq_conn (see g_conn_lock in arq.c).
172 * None may be called while the caller already holds the connection lock. */
173void arq_set_trx(int trx);
174int arq_get_trx(void);
175/* Configured (non-session) bandwidth in Hz, read under g_conn_lock. */
176int arq_get_bw(void);
177/* Copy the current callsign strings out under g_conn_lock. Each of my_call,
178 * src_addr, dst_addr may be NULL to skip; each receives up to bufsz bytes,
179 * always NUL-terminated. */
180void arq_conn_get_calls(char *my_call, char *src_addr, char *dst_addr, size_t bufsz);
181
182/* Set the primary callsign (the station's own), thread-safe. Mirrors the
183 * MYCALL command: clears any secondary callsigns. Used to install the
184 * configured default callsign at engine startup before any client connects. */
185void arq_set_callsign(const char *callsign);
186
187/* Wake the ARQ event loop so it re-reads time_now_ms(). Called by the
188 * -x sock virtual-clock transport after advancing signal time; harmless
189 * (a spurious wakeup) from any other context. */
190void arq_notify_virtual_time(void);
191
198int arq_init(size_t frame_size, int mode);
199
203void arq_shutdown();
204
208void arq_tick_1hz(void);
209
214void arq_post_event(int event);
215/* The RX pattern detector heard a pattern (is_break: the BREAK kind). */
216void arq_post_pattern_ack(bool is_break);
217
222bool arq_is_link_connected(void);
225bool arq_session_active(void);
228bool arq_link_has_peer(void);
231bool arq_channel_heard(void);
232
239void arq_discard_stale_rx(void);
240
246/* The FSM took a CALL (from_call: the session gets min(ours, its)) or an
247 * ACCEPT (the session gets its) advertising bw_hz. */
248void arq_set_session_bw(int bw_hz, bool from_call);
249
256int arq_queue_data(const uint8_t *data, size_t len);
257
263
270void arq_set_disconnect_drain_timeout_s(int seconds);
271
272/* Idle ("listen") payload mode: what the payload decoder sits on while no ARQ
273 * session is up, and therefore which broadcast frames this station can hear.
274 * arq_set_listen_mode() returns 0 on success, -1 if a session is up (the ARQ
275 * carousel owns the mode then). Does not affect any later session: every
276 * session-start path resets the mode state to DATAC15. Read it back through
277 * modem_get_listen_mode(), which owns the operator-facing value. */
279
285void arq_set_active_modem_mode(int mode, size_t frame_size);
286
295bool arq_handle_incoming_connect_frame(uint8_t *data, size_t frame_size, float rx_snr, int mode);
296/* A frame that passed the session's seeded CRC (carousel data plane).
297 * from_control: the control decoder produced it; otherwise mode is the
298 * payload decoder's. */
299void arq_handle_carousel_frame(const uint8_t *data, size_t frame_size, int mode,
300 bool from_control, float rx_snr, uint32_t age_ms);
301
308bool arq_handle_incoming_cq_frame(uint8_t *data, size_t frame_size);
309
316void arq_notify_cq_tx_started(void);
318
325void arq_handle_incoming_frame(uint8_t *data, size_t frame_size, float rx_snr);
326
334void arq_update_link_metrics(int sync, float snr, int rx_status, bool frame_decoded);
335
341void arq_note_rx_preamble(int mode);
342void arq_note_rx_frame_done(void);
348void arq_note_rx_nav(uint32_t age_ms, uint32_t hold_ms);
349
356
363bool arq_wait_dequeue_action(arq_action_t *action, int timeout_ms);
364
371
377int arq_submit_tcp_cmd(const arq_cmd_msg_t *cmd);
378
385int arq_submit_tcp_payload(const uint8_t *data, size_t len);
386
391
397
407void arq_set_retry_slots(int slots);
408
409void arq_set_retry_stagger_ms(int ms);
410
411/* Station capability: transmit MFSK only (FSK-only radios). When on, this
412 * station's CALL/ACCEPT/control/payload all transmit on the MFSK floor rung
413 * and its peers keep its transmit direction there. */
414void arq_set_tx_mfsk_only(bool on);
415
416/* The longest carousel round this station asks for or sends, in seconds (0:
417 * the protocol's 30 s). For clients that abort on a long PTT: vARIM gives up
418 * after about 20 s keyed (#445). */
419void arq_set_max_keydown_s(int s);
420
424void call_remote();
425
430
431#endif
void arq_note_rx_nav(uint32_t age_ms, uint32_t hold_ms)
A NAV header that began age_ms ago says the peer's keydown ends within hold_ms of its start: the chan...
Definition arq.c:1334
#define CALLSIGN_MAX_SIZE
Definition arq.h:24
void arq_set_max_keydown_s(int s)
Definition arq.c:1514
int arq_get_bw(void)
Definition arq.c:486
void arq_set_retry_slots(int slots)
Set runtime retry slot counts.
Definition arq.c:1475
int arq_submit_tcp_cmd(const arq_cmd_msg_t *cmd)
Submit parsed control command from TCP bridge to ARQ.
Definition arq.c:1428
void arq_handle_carousel_frame(const uint8_t *data, size_t frame_size, int mode, bool from_control, float rx_snr, uint32_t age_ms)
Definition arq.c:833
bool arq_handle_incoming_cq_frame(uint8_t *data, size_t frame_size)
Handle incoming compact CQ frame and emit host-side CQFRAME notification.
Definition arq.c:1014
arq_info arq_conn
Definition arq.c:48
int arq_init(size_t frame_size, int mode)
Initialize ARQ subsystem.
Definition arq.c:1074
int arq_submit_tcp_payload(const uint8_t *data, size_t len)
Submit payload bytes from TCP bridge to ARQ.
Definition arq.c:1434
void arq_update_link_metrics(int sync, float snr, int rx_status, bool frame_decoded)
Feed decoder/link metrics into ARQ adaptation.
Definition arq.c:1352
void arq_handle_incoming_frame(uint8_t *data, size_t frame_size, float rx_snr)
Handle incoming regular ARQ control/data frame.
Definition arq.c:1045
void arq_set_tx_mfsk_only(bool on)
Definition arq.c:1508
void arq_set_retry_stagger_ms(int ms)
Definition arq.c:1499
void arq_set_session_bw(int bw_hz, bool from_call)
Definition arq.c:1005
bool arq_get_runtime_snapshot(arq_runtime_snapshot_t *snapshot)
Copy ARQ runtime snapshot.
Definition arq.c:1389
void clear_connection_data()
Clear legacy ARQ connection state and buffers.
Definition arq.c:1454
void arq_post_event(int event)
Post an FSM event to ARQ.
Definition arq.c:1200
void arq_note_rx_frame_done(void)
Definition arq.c:1345
bool arq_channel_heard(void)
Whether another station is keyed, by the FSM's listen-before-talk test: what a station message waits ...
Definition arq.c:1219
bool arq_wait_dequeue_action(arq_action_t *action, int timeout_ms)
Wait for next modem action.
Definition arq.c:1383
#define CALLSIGN_MAX_SECONDARY
Definition arq.h:25
#define ARQ_KEYDOWN_FRAME_MAX
Definition arq.h:95
void arq_post_pattern_ack(bool is_break)
Definition arq.c:1202
arq_action_type_t
Action types emitted by ARQ for modem worker execution.
Definition arq.h:68
@ ARQ_ACTION_TX_PATTERN
Definition arq.h:74
@ ARQ_ACTION_TX_PAYLOAD
Definition arq.h:71
@ ARQ_ACTION_TX_CONTROL
Definition arq.h:70
@ ARQ_ACTION_TX_KEYDOWN
Definition arq.h:73
@ ARQ_ACTION_NONE
Definition arq.h:69
@ ARQ_ACTION_MODE_SWITCH
Definition arq.h:72
void arq_notify_cq_tx_started(void)
Emit VARA-style async status for an outgoing CQFRAME transmission.
Definition arq.c:1033
int arq_get_trx(void)
Definition arq.c:454
void arq_set_active_modem_mode(int mode, size_t frame_size)
Inform ARQ of modem mode/frame size actually active in modem.
Definition arq.c:1303
bool arq_session_active(void)
A session is up or starting, either way (CALLING, ACCEPTING, CONNECTED, DISCONNECTING): what stops a ...
Definition arq.c:1210
void arq_set_callsign(const char *callsign)
Definition arq.c:472
void arq_notify_virtual_time(void)
Definition arq.c:157
bool arq_handle_incoming_connect_frame(uint8_t *data, size_t frame_size, float rx_snr, int mode)
Handle an incoming CALL/ACCEPT frame (protocol version 3).
Definition arq.c:866
void arq_set_disconnect_drain_timeout_s(int seconds)
Set the absolute cap on how long an app DISCONNECT may stay deferred while draining the last TX bytes...
Definition arq.c:1464
void arq_notify_cq_tx_complete(void)
Definition arq.c:1039
#define ARQ_KEYDOWN_FRAMES
Definition arq.h:94
bool arq_try_dequeue_action(arq_action_t *action)
Try to dequeue next modem action without blocking.
Definition arq.c:1377
int arq_set_listen_mode(int mode)
Definition arq.c:1286
arq_pattern_kind_t
Pattern kind (ARQ_ACTION_TX_PATTERN).
Definition arq.h:80
@ ARQ_PATTERN_BREAK
Definition arq.h:82
@ ARQ_PATTERN_ACK
Definition arq.h:81
void reset_arq_info(arq_info *arq_conn)
Reset external arq_info structure.
Definition arq.c:1521
void callee_accept_connection()
Trigger callee-side accept flow in compatibility path.
Definition arq.c:1541
void call_remote()
Trigger outgoing call attempt using current ARQ addresses.
Definition arq.c:1540
bool arq_link_has_peer(void)
The link to the peer exists: CONNECTED or DISCONNECTING.
Definition arq.c:1227
void arq_discard_stale_rx(void)
Drop received bytes an ended session left in the RX buffer.
Definition arq.c:1243
void arq_shutdown()
Shut down ARQ workers and release ARQ resources.
Definition arq.c:1169
void arq_tick_1hz(void)
Execute 1 Hz ARQ maintenance tick.
Definition arq.c:1198
bool arq_is_link_connected(void)
Check whether ARQ link is connected.
Definition arq.c:1235
int arq_get_tx_backlog_bytes(void)
Get pending outbound payload backlog.
Definition arq.c:1267
int arq_queue_data(const uint8_t *data, size_t len)
Queue outbound payload bytes for ARQ transmission.
Definition arq.c:1255
int arq_reported_bandwidth_hz(void)
Get the configured ARQ bandwidth token to report to VARA clients.
Definition arq.c:442
void arq_set_trx(int trx)
Definition arq.c:447
void arq_note_rx_preamble(int mode)
A decoder for mode has just caught a frame's preamble: the channel is busy for that frame's airtime e...
Definition arq.c:1322
void arq_conn_get_calls(char *my_call, char *src_addr, char *dst_addr, size_t bufsz)
Definition arq.c:462
int mode
Definition carousel.c:2916
int bw_hz
Definition carousel.c:2962
Single modem action item popped by modem TX worker.
Definition arq.h:112
size_t frame_size
Definition arq.h:115
int pattern_kind
Definition arq.h:120
int mode
Definition arq.h:114
int frame_count
Definition arq.h:116
arq_action_type_t type
Definition arq.h:113
arq_keydown_t * keydown
Definition arq.h:119
Definition arq_events.h:64
Runtime ARQ connection and mode state shared across modules.
Definition arq.h:50
int bw
Definition arq.h:59
bool encryption
Definition arq.h:56
int session_bw
Definition arq.h:60
size_t frame_size
Definition arq.h:62
int retry_slots
Definition arq.h:61
bool listen
Definition arq.h:58
int mode
Definition arq.h:63
int TRX
Definition arq.h:51
int call_burst_size
Definition arq.h:57
int secondary_call_count
Definition arq.h:54
Definition arq.h:97
bool nav
Definition arq.h:100
uint32_t gap_ms
Definition arq.h:104
int mode
Definition arq.h:103
size_t len
Definition arq.h:105
uint16_t crc_seed
Definition arq.h:99
int n
Definition arq.h:98
Snapshot of current ARQ runtime state for telemetry/decision making.
Definition arq.h:125
uint32_t listen_spells_ended
Definition arq.h:141
uint32_t listen_releases
Definition arq.h:147
int tx_backlog_bytes
Definition arq.h:152
bool initialized
Definition arq.h:126
int speed_level
Definition arq.h:153
int trx
Definition arq.h:151
int peer_tx_mode
Definition arq.h:155
int preferred_rx_mode
Definition arq.h:157
int payload_mode
Definition arq.h:154
int control_mode
Definition arq.h:156
bool expect_pattern_ack
Definition arq.h:130
int car_rx_mode
Definition arq.h:165
int preferred_tx_mode
Definition arq.h:158
bool in_carousel
Definition arq.h:133
uint64_t tx_bytes
Definition arq.h:159
int car_peer_mode
Definition arq.h:166
int listen_floor_mode
Definition arq.h:150
uint64_t rx_bytes
Definition arq.h:160
bool listening_for_calls
Definition arq.h:136
int car_tx_mode
Definition arq.h:164
bool connected
Definition arq.h:127