Mercury Modem
Mercury ARQ/Broadcast modem
Loading...
Searching...
No Matches
arq_protocol.h
Go to the documentation of this file.
1/* HERMES Modem — ARQ Protocol: wire format, mode timing, codec API
2 *
3 * Copyright (C) 2025 Rhizomatica
4 * Author: Rafael Diniz <rafael@riseup.net>
5 *
6 * SPDX-License-Identifier: GPL-3.0-or-later
7 */
8
9#ifndef ARQ_PROTOCOL_H_
10#define ARQ_PROTOCOL_H_
11
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdint.h>
15#include <stdatomic.h>
16
17/* ======================================================================
18 * Plain session frame header (8 bytes): the DISCONNECT's. A session's other
19 * frames are the carousel's (carousel.h), CALL/ACCEPT have their own layout
20 * (below).
21 *
22 * Byte 0: framer byte — set/read by write_frame_header()/parse_frame_header()
23 * bits [7:5] = packet_type (PACKET_TYPE_ARQ_CONTROL)
24 * bits [4:0] = extension field (0)
25 * Byte 1: subtype — arq_subtype_t
26 * Byte 2: flags — 0
27 * Byte 3: session_id — the session's id, chosen by the caller
28 * Byte 4: tx_seq — 0
29 * Byte 5: rx_ack_seq — 0
30 * Byte 6: snr_raw — local RX SNR; 0=unknown, else round(dB) + 128
31 * Byte 7: ack_delay — 0
32 * ====================================================================== */
33
34#define ARQ_HDR_SUBTYPE_IDX 1
35#define ARQ_HDR_FLAGS_IDX 2
36#define ARQ_HDR_SESSION_IDX 3
37#define ARQ_HDR_SEQ_IDX 4
38#define ARQ_HDR_ACK_IDX 5
39#define ARQ_HDR_SNR_IDX 6
40#define ARQ_HDR_DELAY_IDX 7
41#define ARQ_FRAME_HDR_SIZE 8 /* bytes 0-7 inclusive */
42
43/* CONNECT frames (CALL/ACCEPT) compact layout — 14 bytes, DATAC16 only.
44 * Uses PACKET_TYPE_ARQ_CALL2 in the framer byte.
45 *
46 * Byte 0: framer byte (PACKET_TYPE_ARQ_CALL2 | BW token, minor revision,
47 * TX-MFSK-only bit, set by write_frame_header)
48 * Byte 1: connect_meta = (session_id & 0x7F) | (is_accept ? 0x80 : 0x00)
49 * Bytes 2-3: CRC16-CCITT of DST callsign (little-endian) — for local validation
50 * Bytes 4-13: arithmetic_encode(SRC callsign only) — 10 bytes, fits callsigns up to ~14 chars
51 */
52#define ARQ_CONNECT_SESSION_IDX 1
53#define ARQ_CONNECT_PAYLOAD_IDX 2
54#define ARQ_CONNECT_SESSION_MASK 0x7F
55#define ARQ_CONNECT_ACCEPT_FLAG 0x80
56#define ARQ_CONTROL_FRAME_SIZE 14
57#define ARQ_CONNECT_META_SIZE 2 /* framer byte + connect_meta byte */
58#define ARQ_CONNECT_MAX_ENCODED (ARQ_CONTROL_FRAME_SIZE - ARQ_CONNECT_META_SIZE)
59#define ARQ_CONNECT_DST_CRC_SIZE 2 /* CRC16-CCITT of DST at bytes [2..3], little-endian */
60#define ARQ_CONNECT_SRC_MAX_ENCODED (ARQ_CONNECT_MAX_ENCODED - ARQ_CONNECT_DST_CRC_SIZE) /* 10 */
61
62/* Compact CQ frame layout — 14 bytes, DATAC16 only.
63 * Uses PACKET_TYPE_ARQ_CQ in the framer byte.
64 *
65 * Byte 0: framer byte (PACKET_TYPE_ARQ_CQ | BW token)
66 * Bytes 1-13: arithmetic_encode(SRC callsign only)
67 */
68#define ARQ_CQ_PAYLOAD_IDX 1
69#define ARQ_CQ_SRC_MAX_ENCODED (ARQ_CONTROL_FRAME_SIZE - ARQ_CQ_PAYLOAD_IDX) /* 13 */
70
71/* BW token values carried in the framer byte extension field for ARQ_CALL/ARQ_CQ. */
72#define ARQ_BW_TOKEN_NONE 0
73#define ARQ_BW_TOKEN_500 1
74#define ARQ_BW_TOKEN_2300 2
75#define ARQ_BW_TOKEN_2750 3
76
77/* ======================================================================
78 * Frame subtypes. 1.9.x sent others (DATA, ACK, KEEPALIVE, MODE and TURN
79 * requests); this version reads only the DISCONNECT.
80 * ====================================================================== */
81
82typedef enum
83{
86
87/* ======================================================================
88 * Parsed frame header (in-memory representation, not wire layout)
89 * ====================================================================== */
90
91typedef struct
92{
93 uint8_t packet_type; /* PACKET_TYPE_ARQ_CONTROL (from framer byte) */
94 uint8_t frame_ext; /* low 5 bits of framer byte */
95 uint8_t subtype; /* arq_subtype_t */
96 uint8_t flags;
97 uint8_t session_id;
98 uint8_t tx_seq;
99 uint8_t rx_ack_seq;
100 uint8_t snr_raw; /* 0=unknown; decode via arq_protocol_decode_snr */
103
104/* ======================================================================
105 * Per-mode timing table (arq_protocol.c)
106 * ====================================================================== */
107
108/* Upper bound on frames per modem TX action. */
109#define ARQ_BURST_MAX 5
110
111typedef struct
112{
113 int freedv_mode; /* FREEDV_MODE_* constant */
114 float frame_duration_s; /* measured TX duration */
115 float retry_interval_s; /* wait before repeating a CALL/ACCEPT/DISCONNECT */
116 int payload_bytes; /* frame payload */
118
119/* The one FreeDV mode used for the ARQ control frames (CALL/ACCEPT/
120 * DISCONNECT/CQ, and the carousel's control frames). Swapping the control mode is a
121 * one-line change here; everything else references this define. The mode
122 * must carry exactly ARQ_CONTROL_FRAME_SIZE payload bytes and must be the
123 * only mode with that frame size in arq_mode_table. */
124#define ARQ_CONTROL_MODE FREEDV_MODE_DATAC16
125
126/* Timing constants shared across modules. The two guards are measured, not
127 * tuned, and are no longer configuration: they bundle the radio's TX->RX
128 * switch with the audio backend's buffering, which nobody can tune blind. */
129#define ARQ_CHANNEL_GUARD_MS 700 /* IRS response guard after frame decode.
130 * OFDM decode fires ~200ms before sender
131 * PTT-OFF, so effective gap at sender is
132 * (guard - 200ms) ~= 500ms. Radio needs
133 * ~340ms for TX->RX switch -> 160ms margin
134 * for preamble detection. At 500ms the
135 * effective gap was ~300ms, causing ~50%
136 * ACK loss on DATAC1 (< 340ms switch). */
137
138#define ARQ_ISS_POST_ACK_GUARD_MS 900 /* ISS guard before resuming DATA TX
139 * after receiving an ACK from the IRS.
140 * Larger than ARQ_CHANNEL_GUARD_MS:
141 * ack_rx fires ~168ms before IRS PTT-OFF,
142 * so the effective gap at IRS is only
143 * (guard + 100ms head) - 168ms.
144 * At 500ms: gap=432ms, too tight for
145 * DATAC1 re-sync after IRS ACK TX.
146 * At 900ms: gap=832ms -- 492ms of clear
147 * air before the DATAC1 preamble. */
148#define ARQ_ACCEPT_RX_WINDOW_MS 9000 /* ACCEPTING RX window after ACCEPT TX:
149 * ISS_guard(900)+DATAC15(4400)+margin(3700)
150 * Old value 7000 left only ~300ms margin
151 * and raced with TIMER_RETRY, causing
152 * 3-4 wasted ACCEPT retries (~28s). */
153/* Grace period after entering CALLING/ACCEPTING (i.e. after PENDING is sent)
154 * during which LISTEN OFF is deferred. Scanning hosts (BPQ32) need a moment to
155 * process PENDING and cancel their own dwell timers; without this grace, a
156 * LISTEN OFF that crosses PENDING on the wire cancels the handshake — the host
157 * moves to the next channel and the reply goes out on the wrong frequency.
158 * 2 s is conservative: ~0.5 s of TCP+host processing with headroom. */
159#define ARQ_LISTEN_OFF_GRACE_MS 2000
160#define ARQ_CALL_RETRY_SLOTS_DEFAULT 4 /* CALL retries before giving up */
161/* CALLs on the fast control mode before the caller escalates to the MFSK
162 * floor; after that it alternates MFSK and the control mode (#235: under
163 * fading an MFSK CALL is no surer than a DATAC16 one at -9..-11 dB, and all-
164 * MFSK lost connects DATAC16 made; MFSK is what connects at -13 dB). A good
165 * link connects exactly as quickly as before. */
166#define ARQ_CALL_FAST_SLOTS_DEFAULT 2
167/* ...and at most this many, whatever RETRIES says: each fast CALL puts the
168 * first MFSK CALL ~12 s later, and a path only the floor carries connects
169 * that much later (specs/connect: 112 s at 2, ~134 s at 4, 205 s at 10, where
170 * BPQ32 gave up after ~87 s, #442). */
171#define ARQ_CALL_FAST_SLOTS_MAX 4
172/* Runtime value: RETRIES N above the default raises it to N/2, capped at
173 * ARQ_CALL_FAST_SLOTS_MAX, so a long retry count buys more fast CALLs (what a
174 * scanning BBS hears while it dwells on the channel; at any bandwidth), not
175 * only more slow MFSK steps (#442). */
176extern _Atomic int arq_call_fast_slots;
177#define ARQ_CALL_FAST_SLOTS atomic_load(&arq_call_fast_slots)
178/* Derive the fast CALL count from a RETRIES value (<= 0: the default). */
179void arq_protocol_set_call_fast_slots(int retries);
180/* After an ACCEPT on MFSK the callee listens this long for the caller's first
181 * round: the caller decodes the ACCEPT ~4 s after it ends, keys one ISS guard
182 * later, and a floor round is a 13.5 s frame the callee decodes ~4 s after. */
183#define ARQ_ACCEPT_RX_WINDOW_FLOOR_MS 26000
184#define ARQ_ACCEPT_RETRY_SLOTS_DEFAULT 4 /* ACCEPT retries before returning */
185#define ARQ_DISCONNECT_RETRY_SLOTS_DEFAULT 2 /* DISCONNECT frame retries */
186
187/* Runtime-configurable retry counts (set via RETRIES TCP command). */
188extern _Atomic int arq_call_retry_slots;
189extern _Atomic int arq_accept_retry_slots;
190extern _Atomic int arq_disconnect_retry_slots;
192/* Station capability: transmit MFSK only (FSK-only radios — QDX, AFSK III).
193 * When set, this station's CALL/ACCEPT/control/payload all transmit on the
194 * MFSK floor rung, and its peers keep its transmit direction there. Set via
195 * arq:mfsk_tx_only (arq_set_tx_mfsk_only). */
196extern _Atomic int arq_tx_mfsk_only;
197#define ARQ_TX_MFSK_ONLY atomic_load(&arq_tx_mfsk_only)
198
199/* Runtime-configurable CALL/ACCEPT retry interval in seconds (set via CALLINT
200 * TCP command). 0.0 = use compiled default (7.0s). Minimum enforced: 4.0s.
201 * Only affects CALL/ACCEPT retry scheduling during connection setup; the
202 * DISCONNECT uses the immutable table values. */
203#define ARQ_CALLINT_MIN_S 4.0f
204#define ARQ_CALLINT_DEFAULT_S 0.0f /* 0 = use table default */
205extern _Atomic float arq_callint_override_s;
206
207/* Deterministic retry stagger (ms). The two peers of a session compare the
208 * same pair of exchanged callsigns, so exactly one of them defers by this
209 * fixed offset on every retry — no random draw, no tail. 0 disables the
210 * stagger (an explicit off-switch); tunable via arq:retry_stagger_ms. */
211#define ARQ_RETRY_STAGGER_MS_DEFAULT 2000
212#define ARQ_RETRY_STAGGER_MS_MAX 5000
213extern _Atomic int arq_retry_stagger_ms;
214
215/* How long a session hears nothing before its control moves to the MFSK floor
216 * (the carousel's SILENT_MS), in seconds. The receiver's own wait for the
217 * floor (S_FALLBACK_MS) is 60 % of it. Default 150 (so 90), range 110..600.
218 * Shorter reacts faster to QRM but moved multipath links to a floor they did
219 * not need in the sim (#442); see carousel.c.
221 * The minimum keeps each timer at least one exchange long, which is what
222 * specs/rungs/FloorSilence.tla counts them in (specs/rungs/README.md): the
223 * longest gap between two keydowns of M's that S decodes, on a working link,
224 * is ~61 s (BW500 at the floor: S's 27.6 s round, M's 27.6 s MFSK control,
225 * the guards and MFSK's 4.5 s decode), so S_FALLBACK_MS >= 61 s and
226 * SILENT_MS >= 102 s; 110 leaves a margin. Shorter, S fell back between the
227 * exchanges of a link that was answering. */
228#define ARQ_SILENCE_FALLBACK_S_DEFAULT 150
229#define ARQ_SILENCE_FALLBACK_S_MIN 110
230#define ARQ_SILENCE_FALLBACK_S_MAX 600
231extern _Atomic int arq_silence_fallback_s;
233
234/* The longest a station calls, in seconds, whatever RETRIES asks for: no
235 * new CALL is keyed past it. A host's RETRIES 20 (BPQ32) is ~8 min of CALLs,
236 * and on air (K7EK, Nov 3-5) 18 calls ran all of it unanswered, while of
237 * the 21 that connected 20 did so within 300 s (the slowest 254 s; one at
238 * 321 s). 0 = no cap: RETRIES alone decides, as before. */
239#define ARQ_CALL_MAX_S_DEFAULT 300
240#define ARQ_CALL_MAX_S_MAX 3600
241extern _Atomic int arq_call_max_s;
242void arq_protocol_set_call_max_s(int seconds);
243
244/* Deterministic per-node retry rank from the exchanged callsign pair.
245 * strcmp() is antisymmetric, so the two peers always disagree: one returns 0
246 * (send first) and the other 1 (defer). Returns -1 when the pair is
247 * degenerate (identical or missing callsigns) — a misconfiguration that falls
248 * back to a random draw. */
249int arq_protocol_retry_rank(const char *local_call, const char *remote_call);
250
251uint64_t arq_protocol_retry_deadline_ms(float seconds, int rank);
252
253/* The two callsigns a CONNECTED reports: src is the station that called, dst
254 * the one that was called, on both peers.
255 * we_called which side we are, as the FSM knows it
256 * remote_call the peer
257 * local_call callee: the callsign the caller dialed (empty: the primary)
258 * connect_src caller: the source of the host's CONNECT (empty: the primary)
259 * my_call our primary callsign
260 * src and dst hold bufsz bytes each. */
261void arq_protocol_session_calls(bool we_called, const char *remote_call,
262 const char *local_call, const char *connect_src,
263 const char *my_call,
264 char *src, char *dst, size_t bufsz);
265
266#define ARQ_CALL_RETRY_SLOTS atomic_load(&arq_call_retry_slots)
267#define ARQ_ACCEPT_RETRY_SLOTS atomic_load(&arq_accept_retry_slots)
268#define ARQ_DISCONNECT_RETRY_SLOTS atomic_load(&arq_disconnect_retry_slots)
270/* The silence between two frames of one keydown (the carousel's poll and its
271 * first burst). The peer is already receiving, so this is not a turnaround:
272 * it only has to let the peer's control decoder report the first frame and
273 * its payload decoder drop any sync taken off it before the next preamble
274 * arrives. Measured, not tuned: see the one-keydown bench. */
275#define ARQ_ACK_DATA_GAP_MS 300
276
277/* Listen before talk. "Busy" is a decoder holding sync on an incoming burst
278 * (arq_update_link_metrics()); HOLD_MS is how stale a sync observation may be
279 * and still count as busy. The modem updates it once per RX chunk, so this
280 * only covers scheduling jitter, not a whole frame: sync keeps being
281 * re-observed for as long as the burst lasts.
282 *
283 * A deferred transmission (the DISCONNECT) waits in DEFER_MS steps, at most
284 * DEFER_MAX of them: false sync is real (a payload decoder can latch onto a
285 * control burst), and a decoder stuck in sync must not block a transmission
286 * forever, so after that we key anyway and take the collision. */
287#define ARQ_CHANNEL_SYNC_HOLD_MS 250
288#define ARQ_CHANNEL_DEFER_MS 500
289#define ARQ_CHANNEL_DEFER_MAX 20 /* ~10 s, then key regardless */
290/* Our own keydown is no false sync: its end is TX_COMPLETE, and a DISCONNECT
291 * keyed then goes out behind it with no reply window (a frame queued while
292 * keyed plays right after). So steps while we are keyed have a cap of their
293 * own, the longest keydown (the carousel's 30 s) and a margin, only for a
294 * TX_COMPLETE that never comes; the end of the keydown starts the budget
295 * above afresh. With the shared 10 s, an MFSK floor round (27 s) used it up
296 * and the DISCONNECT keyed 22 ms after it, over the peer's ACK: both lost,
297 * the teardown 46 s and two retries (NW8L-KA8RYU, IONOS MPD -5 dB, 1.9.22). */
298#define ARQ_CHANNEL_DEFER_KEYED_MAX 64 /* 32 s */
299/* A CALL held for a frame (arq_fsm.c, call_must_wait) waits this long past
300 * the frame's end, so that the frame can decode first: an ACCEPT does, and a
301 * CALL keyed just before it went out from a caller already connected. The
302 * floor's decode allowance (carousel.c D_MFSK_MS 4500; ~3.7 s on air) and one
303 * deferral step: a decode at the allowance's very end otherwise ties with the
304 * CALL (specs/connect/ConnectCarrier.tla, DW). */
305#define ARQ_CALL_DECODE_WAIT_MS (4500 + ARQ_CHANNEL_DEFER_MS)
306/* Before a DISCONNECT: how long past the longer reply guard to leave for a
307 * frame our last transmission invited to start and be heard, before keying
308 * over the gap. */
309#define ARQ_REPLY_WINDOW_MARGIN_MS 1000
311/* Decode thresholds (dB) of the rungs, which set the carousel's start rung
312 * (carousel.c, car_start_level) and MFSK's place below DATAC15. */
313#define ARQ_SNR_HYST_DB 5.0f
314#define ARQ_SNR_MIN_DATAC15_DB -7.0f /* its cliff (sim, bench): below
315 * it the carousel's MFSK rung wins */
316#define ARQ_SNR_MIN_DATAC4_DB -6.0f /* bench (docs/MODES.md):
317 * DATAC15/DATAC4 goodput crossover
318 * ~-7.5 dB MPP / -8.5 dB AWGN */
319#define ARQ_SNR_MIN_DATAC3_DB -1.0f
320#define ARQ_SNR_MIN_DATAC1_DB 3.0f
321#define ARQ_SNR_MIN_DATAC17_DB 7.0f /* bench (docs/MODES.md): goodput
322 * crossover vs DATAC1 ~ +6 dB MPP;
323 * 89/100 delivery at +8 */
324#define ARQ_SNR_MIN_QAM16C2_DB 13.0f /* bench: goodput crossover vs
325 * DATAC17 ~ +11 dB MPP, 84/100 at
326 * +13.7; upstream's published
327 * operating point is +15 */
328
329/* Absolute cap on how long an APP_DISCONNECT may stay deferred while the FSM
330 * tries to drain the last app bytes. Once the application has asked to
331 * disconnect, the deferral must always resolve into a clean air-side
332 * DISCONNECT handshake within this window — otherwise a stuck session keeps
333 * keying the rig forever (the K7EK "Mercury kept hanging on" report). On a
334 * healthy link the drain completes in seconds, so this only bites when the
335 * link is failing. */
336#define ARQ_DISCONNECT_DRAIN_TIMEOUT_S_DEFAULT 45
337extern _Atomic int arq_disconnect_drain_timeout_s;
338#define ARQ_DISCONNECT_DRAIN_TIMEOUT_S atomic_load(&arq_disconnect_drain_timeout_s)
339
340/* Mode table (defined in arq_protocol.c) */
341extern const arq_mode_timing_t arq_mode_table[];
342extern const int arq_mode_table_count;
343
344/* ======================================================================
345 * Frame codec API
346 * ====================================================================== */
347
352int arq_protocol_encode_hdr(uint8_t *buf, size_t buf_len, const arq_frame_hdr_t *hdr);
353
358int arq_protocol_decode_hdr(const uint8_t *buf, size_t buf_len, arq_frame_hdr_t *hdr);
359
365
370int arq_protocol_bw_hz_from_token(uint8_t bw_token);
371
376uint8_t arq_protocol_encode_snr(float snr_db);
377
382float arq_protocol_decode_snr(uint8_t snr_raw);
383
388const arq_mode_timing_t *arq_protocol_mode_timing(int freedv_mode);
389
399
405/* The wait after a CALL on this carrier. After an MFSK CALL: the callee
406 * decodes it ~4 s after it ends, answers one guard later with a 13.5 s MFSK
407 * ACCEPT, which this end decodes ~4 s after -- about 24 s, where the table's
408 * DATAC16-shaped interval would key the next CALL over the answer. */
410
411/* The MFSK floor of a session at this bandwidth (Hz): the 500 Hz geometry
412 * (MERCURY_MODE_MFSK_N32, 32 tones 15.6 Hz apart) for BW500, whose stations
413 * may sit behind a 500 Hz filter that no 1 kHz MFSK passes; the 1 kHz floor
414 * (MERCURY_MODE_MFSK) otherwise. Per bit the two reach equally far on AWGN
415 * (the narrow one's 3 dB is twice the airtime), the 1 kHz one further on
416 * flutter, in half the keydown. */
417int arq_floor_mode(int bw_hz);
418
423uint16_t arq_protocol_callsign_crc16(const char *callsign);
424
425/* ======================================================================
426 * Frame builder API
427 *
428 * Each function fills `buf` (caller-provided) with a complete ready-to-TX
429 * frame (framer byte + header/payload) and returns the total byte count,
430 * or -1 if buf_len < required size or arguments are invalid. Control frames
431 * are padded to ARQ_CONTROL_FRAME_SIZE (14 bytes) on the air.
432 * ====================================================================== */
433
435int arq_protocol_build_disconnect(uint8_t *buf, size_t buf_len,
436 uint8_t session_id, uint8_t snr_raw);
437
438/* --- CALL/ACCEPT of Mercury 2.0 (PACKET_TYPE_ARQ_CALL2) ---
439 *
440 * 14 bytes (a DATAC16 frame), padded to the frame on MFSK. Byte 0 is the
441 * framer: type CALL2, and in its extension the bandwidth token (2 bits), the
442 * 2.x minor revision (2 bits, 1) and the TX-MFSK-only bit. Every 1.9.x
443 * release drops the type,
444 * and a CALL of theirs (PACKET_TYPE_ARQ_CALL) addressed to this station is
445 * logged, not answered: the versions do not connect (docs/CAROUSEL-ARQ.md,
446 * "Wire formats").
448 * The 1.9.x frames, type CALL, carried in their extension the bandwidth
449 * token, a 2-bit protocol version and a carousel bit:
450 * Version 1 was 1.9.18. Version 2 moved MFSK's tones from 1531..2500 Hz to
451 * 1015.6..1984.4 Hz, centred on 1500 Hz like the codec2 modes; the frames are
452 * the same, but a pair of versions 1 and 2 would connect on DATAC16 and then
453 * never hear each other at the floor, so the version keeps them apart.
454 * Version 3 (after 1.9.19) gave a BW500 session its own floor, the 500 Hz
455 * MFSK (arq_floor_mode), and was the last value the two bits held.
456 * Mercury 2.0 took another packet type (CALL2, above).
457 *
458 * Minor 1 added the TX-MFSK-only bit, a station capability: "I can only
459 * transmit MFSK" (arq:mfsk_tx_only; an FSK-only radio). 1.9.20 sent minor
460 * 0 in 3 bits and reads any minor, so it reads a minor-1 frame as before,
461 * the bit as part of the minor; a station of minor 1 or later reads 1.9.20's
462 * frames as before too. Only a station that sets the bit needs a peer that
463 * reads it, so it ignores frames of minor 0 (arq_protocol_connect_minor).
464 *
465 * CALL 1: 0 | session id 2-3: CRC16 of the called callsign
466 * 4-13: the caller's callsign, arithmetic-coded
467 * ACCEPT 1: 1 | session id 2-3: CRC16 of the caller's callsign
468 * 4-5: CRC16 of the called callsign 6-7: the callee's nonce
469 * 8: the caller's start rung (3 bits), the callee hears the caller
470 * below the control mode (bit 3), 4 bits zero
471 * 9: the SNR the callee read the CALL at, dB + 128 (0: unknown)
472 * 10-13: zero */
473#define ARQ_CONNECT_MINOR 1
474#define ARQ_CONNECT_EXT_BW_MASK 0x03
475#define ARQ_CONNECT_EXT_MINOR_SHIFT 2
476#define ARQ_CONNECT_EXT_MINOR_MASK 0x0C
477#define ARQ_CONNECT_EXT_TXMFSK 0x10 /* the sender transmits only MFSK */
478#define ARQ_ACCEPT_CALLED_CRC_IDX 4
479#define ARQ_ACCEPT_NONCE_IDX 6
480#define ARQ_ACCEPT_LEVEL_IDX 8
481#define ARQ_ACCEPT_LEVEL_MASK 0x07
482#define ARQ_ACCEPT_CTL_DEAF_BIT 0x08 /* the callee hears the caller below
483 the control mode */
484#define ARQ_ACCEPT_SNR_IDX 9
485
488int arq_protocol_build_call(uint8_t *buf, size_t buf_len,
489 uint8_t session_id,
490 const char *caller, const char *called,
491 int bw_hz, bool tx_mfsk);
492
498int arq_protocol_build_accept(uint8_t *buf, size_t buf_len,
499 uint8_t session_id,
500 const char *called, const char *caller,
501 int bw_hz, int start_level, bool ctl_deaf,
502 float call_snr_db, uint16_t nonce,
503 bool tx_mfsk);
504
506bool arq_protocol_connect_version_ok(const uint8_t *buf, size_t buf_len);
507
509int arq_protocol_connect_minor(const uint8_t *buf);
510
513bool arq_protocol_connect_dst_matches(const uint8_t *buf, size_t buf_len,
514 const char *callsign);
515
519int arq_protocol_parse_call(const uint8_t *buf, size_t buf_len,
520 uint8_t *session_id_out, char *caller_out,
521 int *bw_hz_out, bool *tx_mfsk_out);
522
523typedef struct {
524 uint8_t session_id;
525 int bw_hz;
526 uint16_t called_crc; /* CRC16 of the callsign the caller called */
527 uint16_t nonce; /* the callee's session nonce */
528 int start_level; /* 0..6 */
529 bool ctl_deaf; /* the callee hears the caller below the control mode */
530 float call_snr_db; /* what the callee read the CALL at; NAN: unknown */
531 bool tx_mfsk; /* the callee can only transmit MFSK */
533
536int arq_protocol_parse_accept(const uint8_t *buf, size_t buf_len, arq_accept_t *out);
537
541int arq_protocol_build_cq(uint8_t *buf, size_t buf_len,
542 const char *src, int bw_hz);
543
547int arq_protocol_parse_cq(const uint8_t *buf, size_t buf_len,
548 char *src_out, int *bw_hz_out);
549
550#endif /* ARQ_PROTOCOL_H_ */
int arq_protocol_bw_hz_from_token(uint8_t bw_token)
Map an on-air BW token back to Hz.
Definition arq_protocol.c:277
int arq_protocol_build_accept(uint8_t *buf, size_t buf_len, uint8_t session_id, const char *called, const char *caller, int bw_hz, int start_level, bool ctl_deaf, float call_snr_db, uint16_t nonce, bool tx_mfsk)
Build an ACCEPT.
Definition arq_protocol.c:528
int arq_protocol_parse_call(const uint8_t *buf, size_t buf_len, uint8_t *session_id_out, char *caller_out, int *bw_hz_out, bool *tx_mfsk_out)
Parse a CALL: session id, the caller's callsign, the bandwidth, and the caller's TX-MFSK-only capabil...
Definition arq_protocol.c:561
int arq_protocol_parse_cq(const uint8_t *buf, size_t buf_len, char *src_out, int *bw_hz_out)
Parse a compact DATAC16 CQ frame.
Definition arq_protocol.c:631
_Atomic int arq_disconnect_retry_slots
Definition arq_protocol.c:22
_Atomic float arq_callint_override_s
Definition arq_protocol.c:92
int arq_protocol_build_call(uint8_t *buf, size_t buf_len, uint8_t session_id, const char *caller, const char *called, int bw_hz, bool tx_mfsk)
Build a CALL.
Definition arq_protocol.c:498
_Atomic int arq_call_max_s
Definition arq_protocol.c:24
int arq_protocol_parse_accept(const uint8_t *buf, size_t buf_len, arq_accept_t *out)
Parse an ACCEPT.
Definition arq_protocol.c:579
_Atomic int arq_retry_stagger_ms
Definition arq_protocol.c:25
int arq_floor_mode(int bw_hz)
Definition arq_protocol.c:163
uint16_t arq_protocol_callsign_crc16(const char *callsign)
Compute CRC16-CCITT of an uppercase-normalised callsign.
Definition arq_protocol.c:341
float arq_protocol_longest_burst_s(void)
Longest single-burst airtime across the whole mode table, in seconds.
Definition arq_protocol.c:104
_Atomic int arq_accept_retry_slots
Definition arq_protocol.c:21
const int arq_mode_table_count
Definition arq_protocol.c:81
_Atomic int arq_call_retry_slots
Definition arq_protocol.c:19
const arq_mode_timing_t * arq_protocol_mode_timing(int freedv_mode)
Look up mode timing entry for a FreeDV mode.
Definition arq_protocol.c:94
float arq_protocol_call_interval_s(void)
Return the CALL/ACCEPT retry interval in seconds, applying any CALLINT override.
Definition arq_protocol.c:117
_Atomic int arq_silence_fallback_s
Definition arq_protocol.c:23
int arq_protocol_build_disconnect(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw)
DISCONNECT frame (PACKET_TYPE_ARQ_CONTROL).
Definition arq_protocol.c:322
void arq_protocol_set_call_fast_slots(int retries)
Definition arq_protocol.c:153
int arq_protocol_connect_minor(const uint8_t *buf)
The 2.x minor revision of a CALL or ACCEPT (version_ok already checked).
Definition arq_protocol.c:483
void arq_protocol_set_silence_fallback_s(int seconds)
Definition arq_protocol.c:139
void arq_protocol_set_call_max_s(int seconds)
Definition arq_protocol.c:146
uint8_t arq_protocol_bw_token_from_hz(int bw_hz)
Map a configured bandwidth in Hz to an on-air BW token.
Definition arq_protocol.c:262
_Atomic int arq_tx_mfsk_only
Definition arq_protocol.c:27
bool arq_protocol_connect_dst_matches(const uint8_t *buf, size_t buf_len, const char *callsign)
Is a CALL or ACCEPT addressed to callsign (bytes 2-3, all 16 bits)? Requires at least ARQ_CONNECT_PAY...
Definition arq_protocol.c:553
uint64_t arq_protocol_retry_deadline_ms(float seconds, int rank)
Definition arq_protocol.c:178
_Atomic int arq_call_fast_slots
Definition arq_protocol.c:20
void arq_protocol_session_calls(bool we_called, const char *remote_call, const char *local_call, const char *connect_src, const char *my_call, char *src, char *dst, size_t bufsz)
Definition arq_protocol.c:205
uint8_t arq_protocol_encode_snr(float snr_db)
Encode a floating-point SNR (dB) into the snr_raw wire byte.
Definition arq_protocol.c:299
int arq_protocol_retry_rank(const char *local_call, const char *remote_call)
Definition arq_protocol.c:168
_Atomic int arq_disconnect_drain_timeout_s
Definition arq_protocol.c:26
int arq_protocol_build_cq(uint8_t *buf, size_t buf_len, const char *src, int bw_hz)
Build a compact DATAC16 CQ frame carrying source callsign and BW token.
Definition arq_protocol.c:607
const arq_mode_timing_t arq_mode_table[]
Definition arq_protocol.c:61
float arq_protocol_call_interval_for_mode_s(int mode)
Definition arq_protocol.c:127
bool arq_protocol_connect_version_ok(const uint8_t *buf, size_t buf_len)
Is buf a CALL or ACCEPT of this protocol (2.0, any minor revision)?
Definition arq_protocol.c:475
int arq_protocol_decode_hdr(const uint8_t *buf, size_t buf_len, arq_frame_hdr_t *hdr)
Decode the ARQ header from the first bytes of buf.
Definition arq_protocol.c:245
float arq_protocol_decode_snr(uint8_t snr_raw)
Decode snr_raw wire byte back to float dB.
Definition arq_protocol.c:310
arq_subtype_t
Definition arq_protocol.h:83
@ ARQ_SUBTYPE_DISCONNECT
Definition arq_protocol.h:84
int arq_protocol_encode_hdr(uint8_t *buf, size_t buf_len, const arq_frame_hdr_t *hdr)
Encode a parsed header into the first ARQ_FRAME_HDR_SIZE bytes of buf.
Definition arq_protocol.c:229
int mode
Definition carousel.c:2916
int bw_hz
Definition carousel.c:2962
Definition arq_protocol.h:494
Definition arq_protocol.h:92
uint8_t packet_type
Definition arq_protocol.h:93
uint8_t session_id
Definition arq_protocol.h:97
uint8_t frame_ext
Definition arq_protocol.h:94
uint8_t flags
Definition arq_protocol.h:96
uint8_t tx_seq
Definition arq_protocol.h:98
uint8_t subtype
Definition arq_protocol.h:95
uint8_t ack_delay_raw
Definition arq_protocol.h:101
uint8_t snr_raw
Definition arq_protocol.h:100
uint8_t rx_ack_seq
Definition arq_protocol.h:99
Definition arq_protocol.h:112
float retry_interval_s
Definition arq_protocol.h:115
int payload_bytes
Definition arq_protocol.h:116
float frame_duration_s
Definition arq_protocol.h:114
int freedv_mode
Definition arq_protocol.h:113