Mercury Modem
Mercury ARQ/Broadcast modem
Loading...
Searching...
No Matches
arq_fsm.h
Go to the documentation of this file.
1/* HERMES Modem — ARQ FSM: state/event types and session structure
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_FSM_H_
10#define ARQ_FSM_H_
11
12#include <stdbool.h>
13#include <stddef.h>
14#include <stdint.h>
15
16#include "arq.h" /* CALLSIGN_MAX_SIZE, arq_action_t/type, arq_info */
17#include "arq_timing.h" /* arq_timing_ctx_t */
18#include "arq_protocol.h"
19#include "carousel.h" /* car_t: the carousel data plane */
20
21/* ======================================================================
22 * Level 1 — Connection FSM states
23 * ====================================================================== */
24
25typedef enum
26{
27 ARQ_CONN_DISCONNECTED = 0, /* no session; idle */
28 ARQ_CONN_LISTENING = 1, /* waiting for incoming CALL frame */
29 ARQ_CONN_CALLING = 2, /* outgoing CALL sent; awaiting ACCEPT */
30 ARQ_CONN_ACCEPTING = 3, /* ACCEPT sent; awaiting the caller's first frame */
31 ARQ_CONN_CONNECTED = 4, /* the carousel runs the session */
32 ARQ_CONN_DISCONNECTING = 5, /* DISCONNECT frame being exchanged */
35
36/* The step a teardown has reached. Each value belongs to one connection
37 * state, and every state change resets it (sess_enter): nothing of one
38 * teardown is left for the next session to trip on. */
39typedef enum
40{
42 ARQ_TD_LISTEN_OFF, /* ACCEPTING: a LISTEN OFF inside the grace period,
43 * honoured when it expires or once connected */
44 ARQ_TD_DRAINING, /* CONNECTED: the application's DISCONNECT waits
45 * for delivery, until teardown_deadline_ms */
46 ARQ_TD_SENT, /* DISCONNECTING: our DISCONNECT is on the air */
47 ARQ_TD_REPLYING, /* DISCONNECTED: the reply to the peer's
48 * DISCONNECT is due when the reply guard expires */
49 ARQ_TD_NOTIFYING, /* DISCONNECTED: the reply is out; the host hears
50 * DISCONNECTED when it has finished (TX_COMPLETE),
51 * so data_rx_buffer_arq drains to it first */
53
54/* ======================================================================
55 * Events
56 * ====================================================================== */
57
58typedef enum
59{
60 /* Application events (from TCP interface via channel bus) */
61 ARQ_EV_APP_LISTEN = 0, /* LISTEN ON received */
62 ARQ_EV_APP_STOP_LISTEN, /* LISTEN OFF received */
63 ARQ_EV_APP_CONNECT, /* CONNECT <dst> received */
64 ARQ_EV_APP_DISCONNECT, /* DISCONNECT received */
65 ARQ_EV_APP_DATA_READY, /* TX data available in buffer */
66
67 /* Radio RX events (from modem worker) */
68 ARQ_EV_RX_CALL, /* CALL frame decoded */
69 ARQ_EV_RX_ACCEPT, /* ACCEPT frame decoded */
70 ARQ_EV_RX_DISCONNECT, /* DISCONNECT frame decoded */
71
72 /* Timer events */
73 ARQ_EV_TIMER_RETRY, /* CALL/ACCEPT retry, teardown waits */
74 ARQ_EV_TIMER_ACK, /* a reply guard, a DISCONNECT's wait */
75
76 /* Modem events */
77 ARQ_EV_TX_STARTED, /* PTT ON (frame on air) */
78 ARQ_EV_TX_COMPLETE, /* PTT OFF (TX finished) */
79
80 /* Carousel data plane */
81 ARQ_EV_RX_CAROUSEL, /* a frame with the session's CRC seed */
82 ARQ_EV_TIMER_CAROUSEL, /* the carousel's next deadline */
83 ARQ_EV_RX_PATTERN, /* a pattern heard (pattern_break) */
84
87
94typedef struct
95{
97
98 /* RX events: the session id, the carrier the frame came on, and the
99 * frame itself (RX_CAROUSEL). */
100 uint8_t session_id;
101 int mode;
102 uint8_t payload[1280]; /* >= largest user payload: QAM16C2
103 * carries 1213 - 8 = 1205 bytes */
105
106 /* RX_CAROUSEL: the control decoder (DATAC16) produced it; otherwise the
107 * payload decoder, in `mode`. RX_ACCEPT: the rung the callee starts our
108 * rounds on (0..6), whether it hears us below the control mode, the SNR
109 * it read our CALL at (0: unknown), its session nonce, and the CRC16 of
110 * the callsign we called, which the FSM checks. */
112 int bw_hz; /* RX_CALL / RX_ACCEPT: the bandwidth it advertises */
116 uint16_t car_nonce;
118
119 /* RX_CALL / RX_ACCEPT: the peer can only transmit MFSK (its CALL/ACCEPT
120 * carried the TX-MFSK-only capability bit). */
122
123 /* Local receive SNR at the time the frame was decoded (dB, 0 = unknown):
124 * a CALL's sets the start rung the ACCEPT names. */
125 float rx_snr;
126 /* RX_CAROUSEL: how long ago the frame ended when it was decoded. */
127 uint32_t rx_age_ms;
128
129 /* RX_PATTERN: the BREAK kind, else the session ACK. */
131
132 /* APP_DISCONNECT from the host's ABORT: drop what is still in flight and
133 * go at once, rather than drain it first as DISCONNECT does (#218). */
134 bool abort;
135
136 /* Call setup */
137 char remote_call[CALLSIGN_MAX_SIZE];
138 /* For an incoming CALL: which of OUR callsigns (primary or a secondary)
139 * the frame's DST CRC16 matched — i.e. the SSID the caller dialed. Empty
140 * when not applicable; the callee reports this as the connection's local
141 * address so a station listening on multiple SSIDs shows the dialed one. */
142 char local_call[CALLSIGN_MAX_SIZE];
144
145/* ======================================================================
146 * Session structure — replaces arq_ctx_t
147 *
148 * All state is in this struct; no hidden global flags.
149 * Monotonic timestamps are uint64_t milliseconds from hermes_log startup.
150 * ====================================================================== */
151
152typedef struct
153{
154 /* --- State machine fields --- */
155 arq_conn_state_t conn_state; /* connection state */
156
157 /* --- Identifiers --- */
158 uint8_t session_id; /* random byte chosen by caller */
159 int call_sends_done; /* CALLs sent this attempt (escalation) */
160 int call_carrier; /* the carrier the last CALL went out on */
161 int call_rx_mode; /* the carrier we ACCEPT on (accept_carrier) */
162 char remote_call[CALLSIGN_MAX_SIZE];
163 char local_call[CALLSIGN_MAX_SIZE]; /* our dialed callsign/SSID for an
164 * accepted incoming CALL (empty =>
165 * fall back to the primary) */
166
167 /* --- Mode / speed --- */
168 int payload_mode; /* MY data TX mode (ISS); per-direction,
169 * independent of peer's TX mode */
170 int control_mode; /* always FREEDV_MODE_DATAC16 */
171 int initial_payload_mode; /* startup payload mode (= broadcast RX
172 * mode); restored on disconnect so the
173 * payload decoder matches broadcast */
174 int peer_tx_mode; /* my payload RX decoder's mode: the
175 * carousel binds it to the peer's
176 * rung, idle restores the listen mode */
177
178 /* --- Retry/timeout bookkeeping --- */
179 int tx_retries_left; /* retries remaining for current frame.
180 * In ACCEPTING this counts TIMER_RETRY
181 * slots, not transmissions: a slot may be
182 * spent silently when the RX window closes
183 * without a CALL (see fsm_accepting). */
184 uint64_t state_enter_ms; /* when current conn_state was entered */
185
186 /* When a decoder was last holding sync on an incoming burst, i.e. when the
187 * peer was last observed transmitting. Written by the modem RX thread via
188 * arq_update_link_metrics() under g_sess_lock, read by the FSM; used to
189 * avoid keying into the peer's frame. */
191 /* A decoder caught a frame's preamble: the frame is on the air until
192 * this, whatever its sync does meanwhile (arq_note_rx_preamble). */
194 uint64_t nav_busy_until_ms; /* a NAV header heard: the peer's keydown runs until then */
195 uint8_t defer_count; /* consecutive busy-channel deferrals
196 * of our DISCONNECT, or our CALL */
197 bool tx_active; /* we are keyed (TX_STARTED seen, no
198 * TX_COMPLETE yet) */
199 uint64_t last_tx_end_ms; /* when our last transmission ended */
200 uint64_t disc_req_ms; /* when the application asked to hang up
201 * (diagnostics only, #442) */
202 bool host_released; /* the application ended this session
203 * (DISCONNECT/ABORT): nothing more is
204 * delivered to it until a new one.
205 * Only APP_DISCONNECT (DISCONNECT,
206 * ABORT, control client gone) sets
207 * it: it is the one teardown that stays
208 * CONNECTED (draining the backlog)
209 * after the host is told DISCONNECTED.
210 * Peer DISCONNECT, timeouts and LISTEN
211 * OFF leave CONNECTED at once, so
212 * nothing is delivered after them. */
213 int local_snr_x10; /* local SNR EMA * 10 */
214
215 /* --- Data bookkeeping --- */
216 int tx_inflight_bytes; /* bytes the carousel holds unconfirmed */
217 int pending_burst_frames; /* frames accumulated for current PTT burst;
218 * written by cb_send_tx_frame under g_sess_lock
219 * (replaces the former function-static so that
220 * the cmd-bridge SEND_CQ path and the FSM event
221 * loop do not race on a shared static) */
222
223 /* --- Connection lifecycle --- */
224 bool listen_enabled; /* app listen intent (LISTEN ON/OFF):
225 * selects the post-call idle status,
226 * LISTENING vs DISCONNECTED, so an ARQ
227 * call always returns to where it was */
228
229 /* --- Connect handshake --- */
230 bool accept_fallback; /* LISTENING was entered because our ACCEPT
231 * retries ran out: the caller may still
232 * have heard it, so its first carousel
233 * frame may complete this session. Only
234 * then. */
235 bool accept_tx_pending; /* the pending TIMER_RETRY is an ACCEPT
236 * answering a CALL we actually heard,
237 * not the RX-window timer. Only the
238 * former may key the transmitter -- see
239 * fsm_accepting's TIMER_RETRY. */
240
241 /* --- Teardown --- */
242 arq_teardown_t teardown; /* the step reached (arq_teardown_t) */
243 uint64_t teardown_deadline_ms; /* when that step gives up: a held
244 * DISCONNECT tears down regardless
245 * (CONNECTED), the callee ending stops
246 * waiting for an answer (DISCONNECTING) */
247 char pending_connect_call[CALLSIGN_MAX_SIZE];
248 /* a CONNECT that came during the
249 * teardown, placed once it completes;
250 * empty = none */
251
252 uint64_t last_rx_ms; /* last frame decoded from the peer */
253
254 /* --- Carousel data plane (carousel.h) --- *
255 * car_active while CONNECTED (and through a callee's ending); deadline_ms
256 * then mirrors the carousel's next deadline (ARQ_EV_TIMER_CAROUSEL). crc_seed rides every carousel frame's CRC16. */
257 car_t *car; /* allocated by arq_fsm_init */
259 bool car_session; /* this session ran the carousel (until idle) */
260 uint16_t crc_seed;
261 uint16_t car_nonce; /* the callee's, in the ACCEPT: part of crc_seed */
262 int car_rx_level; /* start rung for the peer's direction:
263 * the SNR measured here */
264 int car_tx_level; /* ...for ours, from the peer (-1: none) */
265 uint8_t car_ctl_deaf; /* connect: I heard the peer below the control
266 * mode (CAR_DEAF_*: MFSK_TX, it transmits only MFSK) */
267 bool car_peer_ctl_deaf; /* connect: ...and it, me (the CALL came on MFSK) */
268 float car_call_snr; /* connect: the SNR the callee read the CALL at
269 * (the callee: its own; the caller: from the
270 * ACCEPT), 0 unknown */
271 float car_my_snr; /* connect: the SNR I read the peer's connect
272 * frame at (the CALL, the ACCEPT), 0 unknown */
273 bool ctl_floor_at_stop; /* the carousel's peer_ctl_deaf when it stopped,
274 * for the DISCONNECT exchange after it */
275 int floor_mode; /* the session's MFSK floor (arq_floor_mode
276 * of its bandwidth), from its seed on */
277 uint64_t car_last_rx_ms; /* last carousel frame from the peer */
278
279 /* --- Timer mechanism --- */
280 uint64_t deadline_ms; /* absolute monotonic deadline */
281 arq_event_id_t deadline_event; /* event to fire when deadline fires */
283
284/* ======================================================================
285 * FSM action callbacks
286 *
287 * Registered once at startup via arq_fsm_set_callbacks().
288 * All callbacks are called from the ARQ event-loop thread.
289 * ====================================================================== */
290
291typedef struct
292{
294 /* burst_remaining: DATA frames still to follow in the same PTT burst
295 * (0 = last/only frame — the bridge enqueues the modem action then).
296 * Control frames always pass 0. */
297 void (*send_tx_frame)(int packet_type, int mode,
298 size_t frame_size, const uint8_t *frame,
299 int burst_remaining);
300
309 void (*notify_connected)(const char *remote_call, const char *local_call,
310 bool we_called);
311
315 void (*notify_pending)(const char *remote_call, const char *local_call);
316
323 void (*notify_pending_again)(const char *remote_call);
324
326 void (*notify_cancelpending)(void);
327
330 void (*notify_disconnected)(bool to_no_client);
331
333 void (*deliver_rx_data)(const uint8_t *data, size_t len);
334
336 int (*tx_backlog)(void);
337
339 int (*tx_read)(uint8_t *buf, size_t len);
340
342 void (*send_buffer_status)(int backlog_bytes);
343
348 bool (*channel_busy)(void);
349
353 void (*send_keydown)(const arq_keydown_t *kd);
355 void (*send_pattern)(int pattern_kind);
356 void (*set_crc_seed)(uint16_t seed);
359 void (*set_floor_mode)(int mode);
361
366
372
373/* ======================================================================
374 * FSM public API (implemented in arq_fsm.c)
375 * ====================================================================== */
376
381void arq_fsm_init(arq_session_t *sess);
382
383/* Free what arq_fsm_init allocated. */
385
395void arq_fsm_dispatch(arq_session_t *sess, const arq_event_t *event);
396
397/* How a spell of listening for calls (LISTENING or ACCEPTING) ended, if the
398 * event ended one. Zero when it did not, so the result reads as a bool. */
399typedef enum {
400 ARQ_LISTEN_GOES_ON = 0, /* still listening, or was not */
401 ARQ_LISTEN_ENDS_IN_SESSION, /* a session takes over: CALLING, CONNECTED */
402 ARQ_LISTEN_ENDS_RELEASED, /* to DISCONNECTED: the host has the radio,
403 * and a scanning host retunes it now */
405
421
431int arq_fsm_timeout_ms(const arq_session_t *sess, uint64_t now);
432
435int arq_call_carrier(const arq_session_t *sess);
437bool arq_fsm_expect_pattern(const arq_session_t *sess, uint64_t now);
440int arq_fsm_channel_keyed(const arq_session_t *sess);
441
446
451
455const char *arq_event_name(arq_event_id_t ev);
456
457#endif /* ARQ_FSM_H_ */
#define CALLSIGN_MAX_SIZE
Definition arq.h:24
arq_event_id_t
Definition arq_fsm.h:59
@ ARQ_EV_TX_STARTED
Definition arq_fsm.h:77
@ ARQ_EV_TIMER_ACK
Definition arq_fsm.h:74
@ ARQ_EV_TX_COMPLETE
Definition arq_fsm.h:78
@ ARQ_EV_TIMER_RETRY
Definition arq_fsm.h:73
@ ARQ_EV_RX_PATTERN
Definition arq_fsm.h:83
@ ARQ_EV_APP_DATA_READY
Definition arq_fsm.h:65
@ ARQ_EV_RX_ACCEPT
Definition arq_fsm.h:69
@ ARQ_EV_APP_DISCONNECT
Definition arq_fsm.h:64
@ ARQ_EV_RX_CALL
Definition arq_fsm.h:68
@ ARQ_EV_RX_DISCONNECT
Definition arq_fsm.h:70
@ ARQ_EV_APP_STOP_LISTEN
Definition arq_fsm.h:62
@ ARQ_EV_RX_CAROUSEL
Definition arq_fsm.h:81
@ ARQ_EV_TIMER_CAROUSEL
Definition arq_fsm.h:82
@ ARQ_EV_APP_CONNECT
Definition arq_fsm.h:63
@ ARQ_EV__COUNT
Definition arq_fsm.h:85
@ ARQ_EV_APP_LISTEN
Definition arq_fsm.h:61
arq_listen_end_t arq_fsm_dispatch_ends_listening(arq_session_t *sess, const arq_event_t *event)
arq_fsm_dispatch(), and whether that one event ended a spell of listening for calls (LISTENING or ACC...
Definition arq_fsm.c:2130
bool arq_fsm_expect_pattern(const arq_session_t *sess, uint64_t now)
Whether a pattern can arrive now (the modem's detector runs only then).
Definition arq_fsm.c:575
void arq_fsm_release(arq_session_t *sess)
Definition arq_fsm.c:117
void arq_fsm_init(arq_session_t *sess)
Initialise a session structure to DISCONNECTED state.
Definition arq_fsm.c:102
const char * arq_conn_state_name(arq_conn_state_t s)
Human-readable name for a connection state (for log output).
Definition arq_fsm.c:35
void arq_fsm_set_callbacks(const arq_fsm_callbacks_t *cbs)
Register FSM action callbacks (call once before first dispatch).
Definition arq_fsm.c:88
arq_conn_state_t
Definition arq_fsm.h:26
@ ARQ_CONN_DISCONNECTED
Definition arq_fsm.h:27
@ ARQ_CONN_LISTENING
Definition arq_fsm.h:28
@ ARQ_CONN_CONNECTED
Definition arq_fsm.h:31
@ ARQ_CONN_ACCEPTING
Definition arq_fsm.h:30
@ ARQ_CONN_CALLING
Definition arq_fsm.h:29
@ ARQ_CONN__COUNT
Definition arq_fsm.h:33
@ ARQ_CONN_DISCONNECTING
Definition arq_fsm.h:32
int arq_fsm_timeout_ms(const arq_session_t *sess, uint64_t now)
Return milliseconds until the next deadline, or INT_MAX if idle.
Definition arq_fsm.c:123
bool arq_conn_state_has_peer(arq_conn_state_t s)
The link to the peer exists: CONNECTED, or DISCONNECTING (the teardown is still an exchange with that...
Definition arq_fsm.c:49
arq_teardown_t
Definition arq_fsm.h:40
@ ARQ_TD_SENT
Definition arq_fsm.h:46
@ ARQ_TD_DRAINING
Definition arq_fsm.h:44
@ ARQ_TD_NONE
Definition arq_fsm.h:41
@ ARQ_TD_LISTEN_OFF
Definition arq_fsm.h:42
@ ARQ_TD_NOTIFYING
Definition arq_fsm.h:49
@ ARQ_TD_REPLYING
Definition arq_fsm.h:47
int arq_call_carrier(const arq_session_t *sess)
The carrier the next CALL goes out on: the control mode for the first ARQ_CALL_FAST_SLOTS,...
Definition arq_fsm.c:274
void arq_fsm_dispatch(arq_session_t *sess, const arq_event_t *event)
Dispatch an event through both FSM levels.
Definition arq_fsm.c:2017
arq_listen_end_t
Definition arq_fsm.h:399
@ ARQ_LISTEN_ENDS_IN_SESSION
Definition arq_fsm.h:401
@ ARQ_LISTEN_ENDS_RELEASED
Definition arq_fsm.h:402
@ ARQ_LISTEN_GOES_ON
Definition arq_fsm.h:400
int arq_fsm_channel_keyed(const arq_session_t *sess)
Why the channel is busy now (a CAR_KEYED_* sign), 0 when clear: the listen-before-talk test the FSM k...
Definition arq_fsm.c:504
const char * arq_event_name(arq_event_id_t ev)
Human-readable name for an event (for log output).
Definition arq_fsm.c:54
void arq_fsm_set_timing(arq_timing_ctx_t *timing)
Register timing context for recording (call once at init).
Definition arq_fsm.c:93
static void send_pattern(car_t *c, int kind)
Definition carousel.c:1881
int mode
Definition carousel.c:2916
ARQ event with all possible payload fields.
Definition arq_fsm.h:95
bool pattern_break
Definition arq_fsm.h:130
uint16_t car_called_crc
Definition arq_fsm.h:117
bool from_control
Definition arq_fsm.h:111
size_t payload_len
Definition arq_fsm.h:104
bool abort
Definition arq_fsm.h:134
int bw_hz
Definition arq_fsm.h:112
uint8_t session_id
Definition arq_fsm.h:100
uint16_t car_nonce
Definition arq_fsm.h:116
uint32_t rx_age_ms
Definition arq_fsm.h:127
bool tx_mfsk
Definition arq_fsm.h:121
int mode
Definition arq_fsm.h:101
int car_level
Definition arq_fsm.h:113
bool car_ctl_deaf
Definition arq_fsm.h:114
float car_call_snr
Definition arq_fsm.h:115
float rx_snr
Definition arq_fsm.h:125
arq_event_id_t id
Definition arq_fsm.h:96
Definition arq_fsm.h:292
Definition arq.h:97
Definition arq_fsm.h:153
car_t * car
Definition arq_fsm.h:257
uint64_t nav_busy_until_ms
Definition arq_fsm.h:194
uint64_t teardown_deadline_ms
Definition arq_fsm.h:243
bool accept_fallback
Definition arq_fsm.h:230
bool ctl_floor_at_stop
Definition arq_fsm.h:273
uint64_t last_rx_sync_ms
Definition arq_fsm.h:190
bool car_active
Definition arq_fsm.h:258
uint8_t session_id
Definition arq_fsm.h:158
uint64_t last_rx_ms
Definition arq_fsm.h:252
uint16_t car_nonce
Definition arq_fsm.h:261
int call_rx_mode
Definition arq_fsm.h:161
bool car_session
Definition arq_fsm.h:259
bool host_released
Definition arq_fsm.h:202
float car_my_snr
Definition arq_fsm.h:271
arq_teardown_t teardown
Definition arq_fsm.h:242
uint8_t car_ctl_deaf
Definition arq_fsm.h:265
bool accept_tx_pending
Definition arq_fsm.h:235
bool tx_active
Definition arq_fsm.h:197
uint16_t crc_seed
Definition arq_fsm.h:260
int tx_retries_left
Definition arq_fsm.h:179
int call_carrier
Definition arq_fsm.h:160
int local_snr_x10
Definition arq_fsm.h:213
int car_tx_level
Definition arq_fsm.h:264
bool listen_enabled
Definition arq_fsm.h:224
int floor_mode
Definition arq_fsm.h:275
uint64_t car_last_rx_ms
Definition arq_fsm.h:277
uint64_t last_tx_end_ms
Definition arq_fsm.h:199
uint64_t state_enter_ms
Definition arq_fsm.h:184
uint64_t disc_req_ms
Definition arq_fsm.h:200
int call_sends_done
Definition arq_fsm.h:159
int peer_tx_mode
Definition arq_fsm.h:174
int car_rx_level
Definition arq_fsm.h:262
int payload_mode
Definition arq_fsm.h:168
int initial_payload_mode
Definition arq_fsm.h:171
arq_event_id_t deadline_event
Definition arq_fsm.h:281
uint64_t rx_frame_busy_until_ms
Definition arq_fsm.h:193
float car_call_snr
Definition arq_fsm.h:268
arq_conn_state_t conn_state
Definition arq_fsm.h:155
int control_mode
Definition arq_fsm.h:170
int pending_burst_frames
Definition arq_fsm.h:217
bool car_peer_ctl_deaf
Definition arq_fsm.h:267
uint64_t deadline_ms
Definition arq_fsm.h:280
int tx_inflight_bytes
Definition arq_fsm.h:216
uint8_t defer_count
Definition arq_fsm.h:195
Definition arq_timing.h:20
Definition carousel.h:147