Skip to main content

trunk_recorder_plugin/
protocol.rs

1//! The wire protocol: JSON lines.
2//!
3//! The recorder starts a plugin's executable with no arguments and writes one
4//! [`HostMessage`] per line to its stdin; the plugin writes one
5//! [`PluginMessage`] per line to its stdout. stderr is the plugin's log.
6//! Either side ignores message types and fields it doesn't know, so both can
7//! grow within an [`API_VERSION`].
8//!
9//! `plugin --describe` prints the plugin's [`Manifest`] (one JSON object) and exits.
10
11use std::path::PathBuf;
12
13use serde::{Deserialize, Deserializer, Serialize};
14use serde_json::{Map, Value};
15
16/// The protocol version. A plugin states the version it was built for in its
17/// manifest; the recorder runs plugins whose `api` it supports.
18pub const API_VERSION: u32 = 1;
19
20/// Exit status of a plugin that can't run with its configuration: the recorder
21/// shows the error and doesn't restart it until recording next starts.
22/// (Any other exit is a crash, and the plugin is restarted with backoff.)
23pub const EXIT_CONFIG: i32 = 78;
24
25/// What a plugin can subscribe to ([`Manifest::subscribe`]).
26pub mod topic {
27    /// A call started ([`super::CallInfo`]).
28    pub const CALL_START: &str = "call.start";
29    /// A call ended ([`super::CallInfo`]); its files follow in `call.concluded`.
30    pub const CALL_END: &str = "call.end";
31    /// A recorded call's files are on disk ([`super::ConcludedCall`]).
32    pub const CALL_CONCLUDED: &str = "call.concluded";
33    /// A radio registered, affiliated, … ([`super::UnitEvent`]).
34    pub const UNIT: &str = "unit";
35    /// Live audio of recording calls ([`super::AudioChunk`]).
36    pub const AUDIO: &str = "audio";
37    /// Every system's state, every few seconds ([`super::Status`]).
38    pub const STATUS: &str = "status";
39
40    pub const ALL: &[&str] = &[CALL_START, CALL_END, CALL_CONCLUDED, UNIT, AUDIO, STATUS];
41}
42
43/// Audio formats a plugin can ask for ([`Manifest::audio_formats`]). A plugin
44/// that asks for none gets the WAV.
45pub mod format {
46    /// AAC in an MP4 container (OpenMHz, Broadcastify Calls).
47    pub const M4A: &str = "m4a";
48    /// The WAV file as well as the M4A: with an .m4a made, the WAV is
49    /// otherwise written only when it's kept.
50    pub const WAV: &str = "wav";
51}
52
53/// Who a plugin is and what it wants — printed by `plugin --describe`.
54#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
55#[serde(default)]
56pub struct Manifest {
57    /// Unique, lowercase, `a-z0-9-` (the registry key and the install folder).
58    pub id: String,
59    /// Shown in the interface.
60    pub name: String,
61    /// Semver of the plugin.
62    pub version: String,
63    pub description: String,
64    /// The protocol version the plugin speaks ([`API_VERSION`]).
65    pub api: u32,
66    /// Topics ([`topic`]) to receive. Nothing else is sent — or even built.
67    pub subscribe: Vec<String>,
68    /// Extra audio formats ([`format`](mod@format)) for `call.concluded`. The recorder
69    /// encodes each call once for every plugin that asks, when it can.
70    pub audio_formats: Vec<String>,
71    /// JSON Schema of the plugin's settings (an object), for the settings form.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub config: Option<Value>,
74    /// JSON Schema of the settings the plugin takes for each system (an
75    /// object); the form repeats it under each system.
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub system_config: Option<Value>,
78    #[serde(skip_serializing_if = "String::is_empty")]
79    pub homepage: String,
80    #[serde(skip_serializing_if = "String::is_empty")]
81    pub repository: String,
82    #[serde(skip_serializing_if = "Vec::is_empty")]
83    pub authors: Vec<String>,
84    #[serde(skip_serializing_if = "String::is_empty")]
85    pub license: String,
86}
87
88impl Manifest {
89    pub fn subscribes(&self, topic: &str) -> bool {
90        self.subscribe.iter().any(|t| t == topic)
91    }
92    pub fn wants_format(&self, format: &str) -> bool {
93        self.audio_formats.iter().any(|f| f == format)
94    }
95}
96
97// ─── Recorder → plugin ────────────────────────────────────────────────────
98
99// (Messages are read, handled and dropped one at a time: their size doesn't matter.)
100#[allow(clippy::large_enum_variant)]
101#[derive(Clone, Debug, Serialize, Deserialize)]
102#[serde(tag = "type")]
103pub enum HostMessage {
104    /// Always first, once.
105    #[serde(rename = "hello")]
106    Hello(Hello),
107    #[serde(rename = "call.start")]
108    CallStart(CallInfo),
109    #[serde(rename = "call.end")]
110    CallEnd(CallInfo),
111    #[serde(rename = "call.concluded")]
112    CallConcluded(ConcludedCall),
113    #[serde(rename = "unit")]
114    Unit(UnitEvent),
115    #[serde(rename = "audio")]
116    Audio(AudioChunk),
117    #[serde(rename = "status")]
118    Status(Status),
119    /// The recorder is stopping: finish what's queued, then exit. stdin closes
120    /// after this, and the process is killed if it hasn't exited within
121    /// [`Shutdown::grace_s`].
122    #[serde(rename = "shutdown")]
123    Shutdown(Shutdown),
124    /// A message type from a newer recorder.
125    #[serde(other)]
126    Unknown,
127}
128
129#[derive(Clone, Debug, Default, Serialize, Deserialize)]
130#[serde(default)]
131pub struct Hello {
132    /// The recorder's protocol version.
133    pub api: u32,
134    pub host: HostInfo,
135    /// The plugin's settings, as the user entered them (see [`Manifest::config`]);
136    /// each system's, with its overrides, are in [`SystemInfo::settings`].
137    pub config: Value,
138    /// Every system calls can come from, including conventional channels.
139    pub systems: Vec<SystemInfo>,
140    /// Where calls are stored.
141    pub capture_dir: PathBuf,
142    /// A folder of the plugin's own that survives restarts and upgrades (queues, state).
143    pub data_dir: PathBuf,
144    /// The audio formats `call.concluded` will carry this run: always "wav",
145    /// plus those the plugin asked for that the recorder can encode.
146    pub audio_formats: Vec<String>,
147}
148
149#[derive(Clone, Debug, Default, Serialize, Deserialize)]
150#[serde(default)]
151pub struct HostInfo {
152    pub name: String,
153    pub version: String,
154}
155
156/// The index calls and events use for the first conventional system; the
157/// next ones count down from it (65534, 65533, …). Each is a system of its
158/// own in [`Hello::systems`], with its own short name and settings.
159pub const CONVENTIONAL: u16 = 65535;
160
161#[derive(Clone, Debug, Default, Serialize, Deserialize)]
162#[serde(default)]
163pub struct SystemInfo {
164    /// The number events carry for it (`system`) while the recorder runs
165    /// (conventional systems: [`CONVENTIONAL`] and down). It can change
166    /// between runs: know systems by `short_name`.
167    pub index: u16,
168    /// The system's short name: its identity (unique among all the
169    /// recorder's systems), its folder, and what users know it by. Every
170    /// event carries it.
171    pub short_name: String,
172    /// "p25" | "smartnet" | "dmr" | "nxdn" | "conventional"
173    pub kind: String,
174    /// The plugin's settings for this system (see [`Manifest::system_config`]);
175    /// null when the user left them empty.
176    pub config: Value,
177    /// The plugin's own settings ([`Hello::config`]) as they are for this
178    /// system: a system can override some of them (another server, say).
179    /// Null: as in `Hello::config`.
180    #[serde(skip_serializing_if = "Value::is_null")]
181    pub settings: Value,
182}
183
184/// A call as it starts or ends.
185#[derive(Clone, Debug, Default, Serialize, Deserialize)]
186#[serde(default)]
187pub struct CallInfo {
188    /// Unique while the recorder runs (Trunk Recorder's call_num).
189    pub id: u32,
190    /// [`SystemInfo::index`]: for this run only; `short_name` is the system's identity.
191    pub system: u16,
192    pub short_name: String,
193    pub talkgroup: u32,
194    /// The talkgroup's alpha tag, when known.
195    pub talkgroup_tag: String,
196    pub freq_hz: u64,
197    /// The TDMA slot (0 / 1) of a call on a two-slot channel: P25 Phase 2 or DMR.
198    pub tdma_slot: Option<u8>,
199    pub analog: bool,
200    pub encrypted: bool,
201    /// P25: the cipher an encrypted call's voice named, once heard: the
202    /// algorithm ID (0x84 AES-256, 0xAA ADP, …) and key ID.
203    pub algid: Option<u8>,
204    pub key_id: Option<u16>,
205    /// Some of its encrypted voice was decrypted (the system had the key).
206    pub decrypted: bool,
207    pub emergency: bool,
208    /// Full duplex, packet mode and priority (P25's service options: 1
209    /// lowest, 4 the default, 7 highest; 0 not given), as the grant said.
210    pub duplex: bool,
211    pub packet: bool,
212    pub priority: u8,
213    /// Recorded (false: only followed, or not recorded — see `reason`).
214    pub recording: bool,
215    /// Why it isn't recorded: "encrypted", "unknown_tg", "no_recorder", "no_source", …
216    pub reason: Option<String>,
217    /// Unix seconds.
218    pub start_time: f64,
219    /// Radios heard on the call so far.
220    pub units: Vec<u32>,
221    /// The same radios, in order, with what each one's transmission said of
222    /// itself.
223    pub sources: Vec<CallSource>,
224    /// Talkgroups patched with this one so far, its own included, ascending;
225    /// empty when it isn't patched.
226    pub patched_talkgroups: Vec<u32>,
227}
228
229/// A radio heard on a call, and what its transmission said of itself: P25's
230/// service options (from the grant, link control and Phase 2's voice
231/// channel user messages), or the flags other protocols have.
232#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
233#[serde(default)]
234pub struct CallSource {
235    pub unit: u32,
236    /// Unix seconds it was first heard on the call.
237    pub time: f64,
238    pub emergency: bool,
239    pub encrypted: bool,
240    /// Full duplex (half otherwise).
241    pub duplex: bool,
242    /// Packet mode (circuit otherwise).
243    pub packet: bool,
244    /// 1 lowest, 4 the default, 7 highest; 0 not given.
245    pub priority: u8,
246}
247
248/// A recorded call whose files are on disk.
249#[derive(Clone, Debug, Default, Serialize, Deserialize)]
250#[serde(default)]
251pub struct ConcludedCall {
252    /// The call's key: its files' path relative to the capture folder, without
253    /// an extension (`<shortName>/<Y>/<M>/<D>/<tg>-<start>_<freq>`).
254    pub path: String,
255    /// [`SystemInfo::index`]: for this run only; the system's identity is `call.short_name`.
256    pub system: u16,
257    /// Its call JSON, in Trunk Recorder's format (the `.json` file's contents).
258    pub call: CallRecord,
259    pub files: CallFiles,
260}
261
262#[derive(Clone, Debug, Default, Serialize, Deserialize)]
263#[serde(default)]
264pub struct CallFiles {
265    pub json: PathBuf,
266    /// 16-bit mono WAV, 8 kHz. There whenever `m4a` isn't; with an .m4a it
267    /// may not be (the recorder writes the WAV only when it's kept, or a
268    /// plugin asks for [`format::WAV`]).
269    pub wav: PathBuf,
270    /// When the plugin asked for M4A and the recorder could encode it.
271    #[serde(skip_serializing_if = "Option::is_none")]
272    pub m4a: Option<PathBuf>,
273}
274
275/// Trunk Recorder's call JSON. Fields this crate doesn't name are in `extra`.
276#[derive(Clone, Debug, Default, Serialize, Deserialize)]
277#[serde(default)]
278pub struct CallRecord {
279    pub call_num: u64,
280    pub short_name: String,
281    pub talkgroup: u32,
282    pub talkgroup_tag: String,
283    pub talkgroup_description: String,
284    pub talkgroup_group_tag: String,
285    pub talkgroup_group: String,
286    /// Hz.
287    pub freq: u64,
288    /// Unix seconds.
289    pub start_time: i64,
290    pub stop_time: i64,
291    /// Seconds of audio.
292    pub call_length: f64,
293    #[serde(deserialize_with = "flag")]
294    pub emergency: bool,
295    #[serde(deserialize_with = "flag")]
296    pub encrypted: bool,
297    /// Its encrypted voice was decrypted, at least in part (this recorder's
298    /// addition, beside `algid` and `key_id`, which are in `extra`).
299    #[serde(deserialize_with = "flag")]
300    pub decrypted: bool,
301    pub priority: i64,
302    #[serde(deserialize_with = "flag")]
303    pub phase2_tdma: bool,
304    pub tdma_slot: i64,
305    /// "digital" | "digital tdma" | "analog"
306    pub audio_type: String,
307    #[serde(rename = "freqList")]
308    pub freq_list: Vec<FreqEntry>,
309    #[serde(rename = "srcList")]
310    pub src_list: Vec<SrcEntry>,
311    /// Every talkgroup patched with this one during the call, its own
312    /// included; absent (empty) when it wasn't patched with another.
313    #[serde(skip_serializing_if = "Vec::is_empty")]
314    pub patched_talkgroups: Vec<u32>,
315    #[serde(flatten)]
316    pub extra: Map<String, Value>,
317}
318
319impl CallRecord {
320    /// Voice frames with bit errors, over the call.
321    pub fn error_count(&self) -> u64 {
322        self.freq_list.iter().map(|f| f.error_count).sum()
323    }
324    pub fn spike_count(&self) -> u64 {
325        self.freq_list.iter().map(|f| f.spike_count).sum()
326    }
327}
328
329#[derive(Clone, Debug, Default, Serialize, Deserialize)]
330#[serde(default)]
331pub struct FreqEntry {
332    pub freq: u64,
333    /// Unix seconds.
334    pub time: i64,
335    /// Seconds into the audio.
336    pub pos: f64,
337    /// Seconds.
338    pub len: f64,
339    pub error_count: u64,
340    pub spike_count: u64,
341    #[serde(flatten)]
342    pub extra: Map<String, Value>,
343}
344
345/// A radio heard on the call.
346#[derive(Clone, Debug, Default, Serialize, Deserialize)]
347#[serde(default)]
348pub struct SrcEntry {
349    /// Radio ID.
350    pub src: i64,
351    /// Unix seconds.
352    pub time: i64,
353    /// Seconds into the audio.
354    pub pos: f64,
355    /// What its transmission said of itself (this recorder's: Trunk
356    /// Recorder writes only `emergency` here).
357    #[serde(deserialize_with = "flag")]
358    pub emergency: bool,
359    #[serde(deserialize_with = "flag")]
360    pub encrypted: bool,
361    #[serde(deserialize_with = "flag")]
362    pub duplex: bool,
363    /// Packet mode.
364    #[serde(deserialize_with = "flag")]
365    pub mode: bool,
366    /// 1 lowest, 4 the default, 7 highest; 0 not given.
367    pub priority: i64,
368    pub signal_system: String,
369    /// The radio's name from the unit tags file.
370    pub tag: String,
371    /// Its talker alias, heard over the air.
372    pub tag_ota: String,
373    /// Where it said it was (GPS, decimal degrees), when it did.
374    #[serde(skip_serializing_if = "Option::is_none")]
375    pub lat: Option<f64>,
376    #[serde(skip_serializing_if = "Option::is_none")]
377    pub lon: Option<f64>,
378    #[serde(flatten)]
379    pub extra: Map<String, Value>,
380}
381
382/// Trunk Recorder writes flags as 0/1 and as booleans; take either.
383fn flag<'de, D: Deserializer<'de>>(d: D) -> Result<bool, D::Error> {
384    Ok(match Value::deserialize(d)? {
385        Value::Bool(b) => b,
386        Value::Number(n) => n.as_f64().is_some_and(|v| v != 0.0),
387        Value::String(s) => s == "true" || s == "1",
388        _ => false,
389    })
390}
391
392/// Something a radio did on the control channel.
393#[derive(Clone, Debug, Default, Serialize, Deserialize)]
394#[serde(default)]
395pub struct UnitEvent {
396    /// [`SystemInfo::index`]: for this run only; `short_name` is the system's identity.
397    pub system: u16,
398    pub short_name: String,
399    /// "registration" | "deregistration" | "affiliation" | "acknowledge" |
400    /// "location" | "data_grant" | "answer_request" | "call_alert" |
401    /// "emergency_alarm"
402    pub kind: String,
403    /// Radio ID.
404    pub unit: u32,
405    /// The talkgroup involved (affiliation, location, answer request, call
406    /// alert, emergency alarm).
407    pub talkgroup: Option<u32>,
408    /// The message said it is an emergency (an emergency alarm, a data
409    /// grant's options).
410    pub emergency: bool,
411    /// Unix seconds.
412    pub time: f64,
413}
414
415/// A slice of a recording call's audio: 16-bit mono PCM, little-endian, base64.
416#[derive(Clone, Debug, Default, Serialize, Deserialize)]
417#[serde(default)]
418pub struct AudioChunk {
419    pub call_id: u32,
420    /// [`SystemInfo::index`]: for this run only; `short_name` is the system's identity.
421    pub system: u16,
422    pub short_name: String,
423    pub talkgroup: u32,
424    pub sample_rate: u32,
425    pub pcm: String,
426}
427
428impl AudioChunk {
429    pub fn new(call_id: u32, system: u16, talkgroup: u32, sample_rate: u32, samples: &[i16]) -> Self {
430        let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
431        AudioChunk { call_id, system, talkgroup, sample_rate, pcm: base64::encode(&bytes), ..Default::default() }
432    }
433    /// The samples.
434    pub fn samples(&self) -> Vec<i16> {
435        let b = base64::decode(&self.pcm);
436        b.chunks_exact(2).map(|c| i16::from_le_bytes([c[0], c[1]])).collect()
437    }
438}
439
440#[derive(Clone, Debug, Default, Serialize, Deserialize)]
441#[serde(default)]
442pub struct Status {
443    /// Unix seconds.
444    pub time: f64,
445    pub systems: Vec<SystemStatus>,
446}
447
448#[derive(Clone, Debug, Default, Serialize, Deserialize)]
449#[serde(default)]
450pub struct SystemStatus {
451    pub index: u16,
452    pub short_name: String,
453    /// The control channel being decoded (none: searching).
454    pub control_channel_hz: Option<u64>,
455    /// Control channel messages decoded per second.
456    pub decode_rate: f64,
457    pub active_calls: u32,
458    pub recording: u32,
459    /// The system's identity, as far as its control channel has stated it
460    /// (none until it has): P25 NAC, WACN, system ID, RFSS and site; site
461    /// also for SmartNet when its OBT sends one.
462    #[serde(skip_serializing_if = "Option::is_none")]
463    pub nac: Option<u32>,
464    #[serde(skip_serializing_if = "Option::is_none")]
465    pub wacn: Option<u32>,
466    #[serde(skip_serializing_if = "Option::is_none")]
467    pub sys_id: Option<u32>,
468    #[serde(skip_serializing_if = "Option::is_none")]
469    pub rfss: Option<u32>,
470    #[serde(skip_serializing_if = "Option::is_none")]
471    pub site: Option<u32>,
472}
473
474#[derive(Clone, Debug, Default, Serialize, Deserialize)]
475#[serde(default)]
476pub struct Shutdown {
477    /// Seconds the plugin has to exit.
478    pub grace_s: f64,
479}
480
481// ─── Plugin → recorder ────────────────────────────────────────────────────
482
483#[derive(Clone, Debug, Serialize, Deserialize)]
484#[serde(tag = "type")]
485pub enum PluginMessage {
486    /// The plugin read its hello and is running.
487    #[serde(rename = "ready")]
488    Ready,
489    #[serde(rename = "log")]
490    Log { level: Level, message: String },
491    /// The plugin's health, shown next to it in the interface.
492    #[serde(rename = "status")]
493    Status {
494        state: State,
495        #[serde(default)]
496        message: String,
497    },
498    /// What became of a concluded call (`path`: [`ConcludedCall::path`]).
499    #[serde(rename = "call.result")]
500    CallResult {
501        path: String,
502        outcome: Outcome,
503        #[serde(default, skip_serializing_if = "String::is_empty")]
504        message: String,
505        /// Where the call can be found now (a link to it on the service).
506        #[serde(default, skip_serializing_if = "String::is_empty")]
507        url: String,
508    },
509    /// How the plugin's work is going, for the dashboard: queue, results,
510    /// timing and the services it talks to. Send it when it changes, at most
511    /// every few seconds ([`crate::CallQueue`] does it for you).
512    #[serde(rename = "metrics")]
513    Metrics(Metrics),
514    #[serde(other)]
515    Unknown,
516}
517
518/// A plugin's figures ([`PluginMessage::Metrics`]). Every field is optional:
519/// send what you know. Totals are since the plugin started.
520#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
521#[serde(default, rename_all = "camelCase")]
522pub struct Metrics {
523    /// Calls waiting to be worked on (ready, or waiting to retry).
524    #[serde(skip_serializing_if = "Option::is_none")]
525    pub queued: Option<u64>,
526    /// Of those, waiting to retry after a failure.
527    #[serde(skip_serializing_if = "Option::is_none")]
528    pub retrying: Option<u64>,
529    /// Being worked on now.
530    #[serde(skip_serializing_if = "Option::is_none")]
531    pub in_flight: Option<u64>,
532    /// Tries that failed and will be retried, in total.
533    #[serde(skip_serializing_if = "Option::is_none")]
534    pub retries: Option<u64>,
535    /// Bytes sent, in total.
536    #[serde(skip_serializing_if = "Option::is_none")]
537    pub bytes_sent: Option<u64>,
538    /// How long one piece of work takes (an upload), ms, smoothed.
539    #[serde(skip_serializing_if = "Option::is_none")]
540    pub latency_ms: Option<f64>,
541    /// Unix seconds of the last success and the last failure.
542    #[serde(skip_serializing_if = "Option::is_none")]
543    pub last_ok: Option<f64>,
544    #[serde(skip_serializing_if = "Option::is_none")]
545    pub last_error: Option<f64>,
546    #[serde(skip_serializing_if = "String::is_empty")]
547    pub last_error_text: String,
548    /// The services it talks to.
549    #[serde(skip_serializing_if = "Vec::is_empty")]
550    pub endpoints: Vec<Endpoint>,
551    /// Anything else worth showing, by name (shown as it is).
552    #[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
553    pub extra: std::collections::BTreeMap<String, serde_json::Value>,
554}
555
556/// A service a plugin talks to, and how it's answering.
557#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
558#[serde(default, rename_all = "camelCase")]
559pub struct Endpoint {
560    /// What it is ("Broadcastify Calls", a host name).
561    pub name: String,
562    pub state: EndpointState,
563    #[serde(skip_serializing_if = "Option::is_none")]
564    pub latency_ms: Option<f64>,
565    #[serde(skip_serializing_if = "Option::is_none")]
566    pub last_ok: Option<f64>,
567    #[serde(skip_serializing_if = "String::is_empty")]
568    pub last_error: String,
569}
570
571#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
572#[serde(rename_all = "lowercase")]
573pub enum EndpointState {
574    #[default]
575    Unknown,
576    /// Answering.
577    Up,
578    /// Answering, with failures (retries).
579    Degraded,
580    /// Not answering.
581    Down,
582}
583
584#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
585#[serde(rename_all = "lowercase")]
586pub enum Level {
587    Error,
588    Warn,
589    Info,
590    Debug,
591}
592
593#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
594#[serde(rename_all = "lowercase")]
595pub enum State {
596    /// Working.
597    Ok,
598    /// Working, but something needs attention (a service down, calls queued).
599    Warning,
600    /// Not working (bad settings, nothing it can do).
601    Error,
602}
603
604#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
605#[serde(rename_all = "lowercase")]
606pub enum Outcome {
607    /// Done (uploaded, sent, stored).
608    Ok,
609    /// Deliberately not handled (a system it isn't set up for, a filtered talkgroup).
610    Skipped,
611    /// Gave up on it.
612    Failed,
613}
614
615/// Standard base64 (for [`AudioChunk::pcm`]).
616pub mod base64 {
617    const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
618
619    pub fn encode(b: &[u8]) -> String {
620        let mut o = String::with_capacity(b.len().div_ceil(3) * 4);
621        for c in b.chunks(3) {
622            let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
623            for i in 0..4 {
624                if i <= c.len() {
625                    o.push(A[(n >> (18 - 6 * i) & 63) as usize] as char);
626                } else {
627                    o.push('=');
628                }
629            }
630        }
631        o
632    }
633
634    /// Invalid characters are skipped.
635    pub fn decode(s: &str) -> Vec<u8> {
636        let mut o = Vec::with_capacity(s.len() / 4 * 3);
637        let (mut acc, mut bits) = (0u32, 0);
638        for ch in s.bytes() {
639            let v = match ch {
640                b'A'..=b'Z' => ch - b'A',
641                b'a'..=b'z' => ch - b'a' + 26,
642                b'0'..=b'9' => ch - b'0' + 52,
643                b'+' => 62,
644                b'/' => 63,
645                _ => continue,
646            };
647            acc = acc << 6 | v as u32;
648            bits += 6;
649            if bits >= 8 {
650                bits -= 8;
651                o.push((acc >> bits) as u8);
652            }
653        }
654        o
655    }
656}
657
658#[cfg(test)]
659mod tests {
660    use super::*;
661
662    #[test]
663    fn base64_roundtrip() {
664        for n in 0..10 {
665            let b: Vec<u8> = (0..n).map(|i| (i * 37 + 5) as u8).collect();
666            assert_eq!(base64::decode(&base64::encode(&b)), b);
667        }
668        assert_eq!(base64::encode(b"hello"), "aGVsbG8=");
669    }
670
671    #[test]
672    fn unknown_types_and_fields_are_ignored() {
673        let m: HostMessage = serde_json::from_str(r#"{"type":"from.the.future","x":1}"#).unwrap();
674        assert!(matches!(m, HostMessage::Unknown));
675        let m: HostMessage = serde_json::from_str(r#"{"type":"shutdown","grace_s":5,"new_field":true}"#).unwrap();
676        assert!(matches!(m, HostMessage::Shutdown(Shutdown { grace_s }) if grace_s == 5.0));
677    }
678
679    #[test]
680    fn call_record_reads_trunk_recorder_json() {
681        let j = r#"{"call_num":7,"freq":851012500,"start_time":1700000000,"stop_time":1700000005,"emergency":0,"encrypted":1,
682            "call_length":5,"talkgroup":101,"talkgroup_tag":"Fire Disp","audio_type":"digital","short_name":"dcfd","phase2_tdma":0,
683            "freqList":[{"freq":851012500,"time":1700000000,"pos":0,"len":5,"error_count":3,"spike_count":1}],
684            "srcList":[{"src":1234,"time":1700000000,"pos":0.5,"emergency":0,"signal_system":"","tag":"","tag_ota":"E1"}],
685            "color_code":-1,"patched_talkgroups":[101,65001]}"#;
686        let c: CallRecord = serde_json::from_str(j).unwrap();
687        assert!(c.encrypted && !c.emergency);
688        assert_eq!(c.patched_talkgroups, [101, 65001]);
689        assert_eq!((c.error_count(), c.spike_count()), (3, 1));
690        assert_eq!(c.src_list[0].tag_ota, "E1");
691        assert_eq!(c.extra["color_code"], -1);
692        // And back, with the unknown field kept.
693        let v = serde_json::to_value(&c).unwrap();
694        assert_eq!(v["color_code"], -1);
695        assert_eq!(v["srcList"][0]["src"], 1234);
696        assert_eq!(v["patched_talkgroups"], serde_json::json!([101, 65001]));
697        let unpatched = serde_json::to_value(CallRecord::default()).unwrap();
698        assert!(unpatched.get("patched_talkgroups").is_none());
699    }
700
701    #[test]
702    fn system_identity_is_there_once_stated() {
703        // From a recorder that doesn't send it: none of it.
704        let s: SystemStatus = serde_json::from_str(r#"{"index":0,"short_name":"county","decode_rate":39.8}"#).unwrap();
705        assert_eq!((s.nac, s.wacn, s.sys_id, s.rfss, s.site), (None, None, None, None, None));
706        // Nothing stated yet: nothing written.
707        let v = serde_json::to_value(&s).unwrap();
708        assert!(["nac", "wacn", "sys_id", "rfss", "site"].iter().all(|k| v.get(k).is_none()), "{v}");
709        let s = SystemStatus { nac: Some(0x3A0), wacn: Some(0xBEE00), sys_id: Some(0x3AB), rfss: Some(1), site: Some(5), ..s };
710        let v = serde_json::to_value(&s).unwrap();
711        assert_eq!((v["nac"].as_u64(), v["wacn"].as_u64(), v["site"].as_u64()), (Some(0x3A0), Some(0xBEE00), Some(5)));
712    }
713}