10#define ARQ_PROTOCOL_H_
21#define ARQ_PROTO_VERSION 4
49#define ARQ_HDR_SUBTYPE_IDX 1
50#define ARQ_HDR_FLAGS_IDX 2
51#define ARQ_HDR_SESSION_IDX 3
52#define ARQ_HDR_SEQ_IDX 4
53#define ARQ_HDR_ACK_IDX 5
54#define ARQ_HDR_SNR_IDX 6
55#define ARQ_HDR_DELAY_IDX 7
56#define ARQ_FRAME_HDR_SIZE 8
66#define ARQ_CONNECT_SESSION_IDX 1
67#define ARQ_CONNECT_PAYLOAD_IDX 2
68#define ARQ_CONNECT_SESSION_MASK 0x7F
69#define ARQ_CONNECT_ACCEPT_FLAG 0x80
70#define ARQ_CONTROL_FRAME_SIZE 14
71#define ARQ_CONNECT_META_SIZE 2
72#define ARQ_CONNECT_MAX_ENCODED (ARQ_CONTROL_FRAME_SIZE - ARQ_CONNECT_META_SIZE)
73#define ARQ_CONNECT_DST_CRC_SIZE 2
74#define ARQ_CONNECT_SRC_MAX_ENCODED (ARQ_CONNECT_MAX_ENCODED - ARQ_CONNECT_DST_CRC_SIZE)
82#define ARQ_CQ_PAYLOAD_IDX 1
83#define ARQ_CQ_SRC_MAX_ENCODED (ARQ_CONTROL_FRAME_SIZE - ARQ_CQ_PAYLOAD_IDX)
86#define ARQ_BW_TOKEN_NONE 0
87#define ARQ_BW_TOKEN_500 1
88#define ARQ_BW_TOKEN_2300 2
89#define ARQ_BW_TOKEN_2750 3
95#define ARQ_FLAG_TURN_REQ 0x80
96#define ARQ_FLAG_HAS_DATA 0x40
97#define ARQ_FLAG_LEN_HI 0x20
137 uint8_t ack_delay_raw;
159 float frame_duration_s;
162 float retry_interval_s;
167#define ARQ_CHANNEL_GUARD_MS 700
175#define ARQ_ISS_POST_ACK_GUARD_MS 900
185#define ARQ_TURN_WAIT_AFTER_ACK_MS 3500
187#define ARQ_ACCEPT_RX_WINDOW_MS 9000
192#define ARQ_ACK_GUARD_S 1
193#define ARQ_CALL_RETRY_SLOTS_DEFAULT 4
194#define ARQ_ACCEPT_RETRY_SLOTS_DEFAULT 4
195#define ARQ_DATA_RETRY_SLOTS_DEFAULT 10
196#define ARQ_DISCONNECT_RETRY_SLOTS_DEFAULT 2
205#define ARQ_CALL_RETRY_SLOTS atomic_load(&arq_call_retry_slots)
206#define ARQ_ACCEPT_RETRY_SLOTS atomic_load(&arq_accept_retry_slots)
207#define ARQ_DATA_RETRY_SLOTS atomic_load(&arq_data_retry_slots)
208#define ARQ_DISCONNECT_RETRY_SLOTS atomic_load(&arq_disconnect_retry_slots)
209#define ARQ_CONNECT_GRACE_SLOTS 2
210#define ARQ_CONNECT_BUSY_EXT_S 2
211#define ARQ_KEEPALIVE_INTERVAL_S 20
212#define ARQ_KEEPALIVE_MISS_LIMIT 5
213#define ARQ_TURN_REQ_RETRIES 2
214#define ARQ_MODE_REQ_RETRIES 2
215#define ARQ_PEER_PAYLOAD_HOLD_S 15
216#define ARQ_IRS_INACTIVITY_CYCLES 5
218#define ARQ_IRS_INACTIVITY_S (ARQ_PEER_PAYLOAD_HOLD_S * \
219 ARQ_IRS_INACTIVITY_CYCLES)
220#define ARQ_MODE_SWITCH_HYST_COUNT 1
221#define ARQ_STARTUP_MAX_S 8
222#define ARQ_STARTUP_ACKS_REQUIRED 1
223#define ARQ_SNR_HYST_DB 1.0f
224#define ARQ_SNR_MIN_DATAC4_DB -4.0f
225#define ARQ_SNR_MIN_DATAC3_DB -1.0f
226#define ARQ_SNR_MIN_DATAC1_DB 3.0f
227#define ARQ_BACKLOG_MIN_DATAC3 56
228#define ARQ_BACKLOG_MIN_DATAC1 126
229#define ARQ_BACKLOG_MIN_BIDIR_UPGRADE 48
230#define ARQ_LADDER_LEVELS 3
231#define ARQ_LADDER_UP_SUCCESSES 4
232#define ARQ_RETRY_DOWNGRADE_THRESHOLD 2
233#define ARQ_MODE_HOLD_AFTER_DOWNGRADE_S 15
241#define ARQ_DATA_LEN_FULL 0
327 uint8_t session_id, uint8_t rx_ack_seq,
328 uint8_t flags, uint8_t snr_raw,
329 uint8_t ack_delay_raw);
333 uint8_t session_id, uint8_t snr_raw);
337 uint8_t session_id, uint8_t snr_raw);
341 uint8_t session_id, uint8_t snr_raw);
352 uint8_t session_id, uint8_t rx_ack_seq,
357 uint8_t session_id, uint8_t snr_raw);
368 uint8_t session_id, uint8_t snr_raw,
373 uint8_t session_id, uint8_t snr_raw,
392 uint8_t session_id, uint8_t tx_seq,
393 uint8_t rx_ack_seq, uint8_t flags,
394 uint8_t snr_raw, uint8_t payload_valid,
395 const uint8_t *payload,
size_t payload_len);
411 const char *src,
const char *dst,
425 const char *src,
const char *dst,
439 uint8_t *session_id_out,
440 char *src_out,
char *dst_out,
447 uint8_t *session_id_out,
448 char *src_out,
char *dst_out,
455 const char *src,
int bw_hz);
461 char *src_out,
int *bw_hz_out);
int arq_protocol_bw_hz_from_token(uint8_t bw_token)
Map an on-air BW token back to Hz.
Definition arq_protocol.c:137
int arq_protocol_build_mode_ack(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw, int freedv_mode)
MODE_ACK frame — accept peer's mode request.
Definition arq_protocol.c:298
int arq_protocol_build_accept(uint8_t *buf, size_t buf_len, uint8_t session_id, const char *src, const char *dst, int bw_hz)
Build an ACCEPT frame.
Definition arq_protocol.c:491
int arq_protocol_parse_cq(const uint8_t *buf, size_t buf_len, char *src_out, int *bw_hz_out)
Parse a compact DATAC13 CQ frame.
Definition arq_protocol.c:560
_Atomic int arq_disconnect_retry_slots
Definition arq_protocol.c:19
int arq_protocol_build_turn_ack(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw)
TURN_ACK frame.
Definition arq_protocol.c:278
uint16_t arq_protocol_callsign_crc16(const char *callsign)
Compute CRC16-CCITT of an uppercase-normalised callsign.
Definition arq_protocol.c:337
int arq_protocol_parse_call(const uint8_t *buf, size_t buf_len, uint8_t *session_id_out, char *src_out, char *dst_out, int *bw_hz_out)
Parse a CALL frame; extract callsigns.
Definition arq_protocol.c:518
_Atomic int arq_accept_retry_slots
Definition arq_protocol.c:17
const int arq_mode_table_count
Definition arq_protocol.c:67
_Atomic int arq_call_retry_slots
Definition arq_protocol.c:16
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:74
int arq_protocol_build_ack(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t rx_ack_seq, uint8_t flags, uint8_t snr_raw, uint8_t ack_delay_raw)
ACK frame.
Definition arq_protocol.c:230
int arq_protocol_build_disconnect(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw)
DISCONNECT frame.
Definition arq_protocol.c:241
int arq_protocol_build_keepalive_ack(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw)
KEEPALIVE_ACK frame.
Definition arq_protocol.c:259
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:122
int arq_protocol_build_keepalive(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw)
KEEPALIVE frame.
Definition arq_protocol.c:250
uint8_t arq_protocol_encode_ack_delay(uint32_t delay_ms)
Encode ack_delay_ms to the 8-bit wire value (10ms units, max 2.55s).
Definition arq_protocol.c:181
int arq_protocol_build_turn_req(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t rx_ack_seq, uint8_t snr_raw)
TURN_REQ frame.
Definition arq_protocol.c:268
_Atomic int arq_data_retry_slots
Definition arq_protocol.c:18
int arq_protocol_build_data(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t tx_seq, uint8_t rx_ack_seq, uint8_t flags, uint8_t snr_raw, uint8_t payload_valid, const uint8_t *payload, size_t payload_len)
DATA frame — 8-byte header + payload bytes.
Definition arq_protocol.c:309
int arq_protocol_build_call(uint8_t *buf, size_t buf_len, uint8_t session_id, const char *src, const char *dst, int bw_hz)
Build a CALL frame.
Definition arq_protocol.c:483
uint32_t arq_protocol_decode_ack_delay(uint8_t raw)
Decode the 8-bit wire ack_delay to milliseconds.
Definition arq_protocol.c:191
int arq_protocol_build_mode_req(uint8_t *buf, size_t buf_len, uint8_t session_id, uint8_t snr_raw, int freedv_mode)
MODE_REQ frame.
Definition arq_protocol.c:287
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:159
int arq_protocol_build_cq(uint8_t *buf, size_t buf_len, const char *src, int bw_hz)
Build a compact DATAC13 CQ frame carrying source callsign and BW token.
Definition arq_protocol.c:536
const arq_mode_timing_t arq_mode_table[]
Definition arq_protocol.c:59
int arq_protocol_parse_accept(const uint8_t *buf, size_t buf_len, uint8_t *session_id_out, char *src_out, char *dst_out, int *bw_hz_out)
Parse an ACCEPT frame; same layout as CALL.
Definition arq_protocol.c:527
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:105
float arq_protocol_decode_snr(uint8_t snr_raw)
Decode snr_raw wire byte back to float dB.
Definition arq_protocol.c:167
arq_subtype_t
Definition arq_protocol.h:104
@ ARQ_SUBTYPE_DISCONNECT
Definition arq_protocol.h:108
@ ARQ_SUBTYPE_ACCEPT
Definition arq_protocol.h:106
@ ARQ_SUBTYPE_DATA
Definition arq_protocol.h:109
@ ARQ_SUBTYPE_CALL
Definition arq_protocol.h:105
@ ARQ_SUBTYPE_KEEPALIVE
Definition arq_protocol.h:110
@ ARQ_SUBTYPE_MODE_ACK
Definition arq_protocol.h:113
@ ARQ_SUBTYPE_TURN_ACK
Definition arq_protocol.h:115
@ ARQ_SUBTYPE_TURN_REQ
Definition arq_protocol.h:114
@ ARQ_SUBTYPE_KEEPALIVE_ACK
Definition arq_protocol.h:111
@ ARQ_SUBTYPE_MODE_REQ
Definition arq_protocol.h:112
@ ARQ_SUBTYPE_ACK
Definition arq_protocol.h:107
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:89
Definition arq_protocol.h:124
Definition arq_protocol.h:153