use std::path::PathBuf;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Map, Value};
pub const API_VERSION: u32 = 1;
pub const EXIT_CONFIG: i32 = 78;
pub mod topic {
pub const CALL_START: &str = "call.start";
pub const CALL_END: &str = "call.end";
pub const CALL_CONCLUDED: &str = "call.concluded";
pub const UNIT: &str = "unit";
pub const AUDIO: &str = "audio";
pub const STATUS: &str = "status";
pub const ALL: &[&str] = &[CALL_START, CALL_END, CALL_CONCLUDED, UNIT, AUDIO, STATUS];
}
pub mod format {
pub const M4A: &str = "m4a";
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Manifest {
pub id: String,
pub name: String,
pub version: String,
pub description: String,
pub api: u32,
pub subscribe: Vec<String>,
pub audio_formats: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system_config: Option<Value>,
#[serde(skip_serializing_if = "String::is_empty")]
pub homepage: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub repository: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub authors: Vec<String>,
#[serde(skip_serializing_if = "String::is_empty")]
pub license: String,
}
impl Manifest {
pub fn subscribes(&self, topic: &str) -> bool {
self.subscribe.iter().any(|t| t == topic)
}
pub fn wants_format(&self, format: &str) -> bool {
self.audio_formats.iter().any(|f| f == format)
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum HostMessage {
#[serde(rename = "hello")]
Hello(Hello),
#[serde(rename = "call.start")]
CallStart(CallInfo),
#[serde(rename = "call.end")]
CallEnd(CallInfo),
#[serde(rename = "call.concluded")]
CallConcluded(ConcludedCall),
#[serde(rename = "unit")]
Unit(UnitEvent),
#[serde(rename = "audio")]
Audio(AudioChunk),
#[serde(rename = "status")]
Status(Status),
#[serde(rename = "shutdown")]
Shutdown(Shutdown),
#[serde(other)]
Unknown,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Hello {
pub api: u32,
pub host: HostInfo,
pub config: Value,
pub systems: Vec<SystemInfo>,
pub capture_dir: PathBuf,
pub data_dir: PathBuf,
pub audio_formats: Vec<String>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct HostInfo {
pub name: String,
pub version: String,
}
pub const CONVENTIONAL: u16 = 65535;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SystemInfo {
pub index: u16,
pub short_name: String,
pub kind: String,
pub config: Value,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct CallInfo {
pub id: u32,
pub system: u16,
pub short_name: String,
pub talkgroup: u32,
pub talkgroup_tag: String,
pub freq_hz: u64,
pub tdma_slot: Option<u8>,
pub analog: bool,
pub encrypted: bool,
pub emergency: bool,
pub recording: bool,
pub reason: Option<String>,
pub start_time: f64,
pub units: Vec<u32>,
pub patched_talkgroups: Vec<u32>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ConcludedCall {
pub path: String,
pub system: u16,
pub call: CallRecord,
pub files: CallFiles,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct CallFiles {
pub json: PathBuf,
pub wav: PathBuf,
#[serde(skip_serializing_if = "Option::is_none")]
pub m4a: Option<PathBuf>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct CallRecord {
pub call_num: u64,
pub short_name: String,
pub talkgroup: u32,
pub talkgroup_tag: String,
pub talkgroup_description: String,
pub talkgroup_group_tag: String,
pub talkgroup_group: String,
pub freq: u64,
pub start_time: i64,
pub stop_time: i64,
pub call_length: f64,
#[serde(deserialize_with = "flag")]
pub emergency: bool,
#[serde(deserialize_with = "flag")]
pub encrypted: bool,
pub priority: i64,
#[serde(deserialize_with = "flag")]
pub phase2_tdma: bool,
pub tdma_slot: i64,
pub audio_type: String,
#[serde(rename = "freqList")]
pub freq_list: Vec<FreqEntry>,
#[serde(rename = "srcList")]
pub src_list: Vec<SrcEntry>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub patched_talkgroups: Vec<u32>,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
impl CallRecord {
pub fn error_count(&self) -> u64 {
self.freq_list.iter().map(|f| f.error_count).sum()
}
pub fn spike_count(&self) -> u64 {
self.freq_list.iter().map(|f| f.spike_count).sum()
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct FreqEntry {
pub freq: u64,
pub time: i64,
pub pos: f64,
pub len: f64,
pub error_count: u64,
pub spike_count: u64,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SrcEntry {
pub src: i64,
pub time: i64,
pub pos: f64,
#[serde(deserialize_with = "flag")]
pub emergency: bool,
pub signal_system: String,
pub tag: String,
pub tag_ota: String,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
fn flag<'de, D: Deserializer<'de>>(d: D) -> Result<bool, D::Error> {
Ok(match Value::deserialize(d)? {
Value::Bool(b) => b,
Value::Number(n) => n.as_f64().is_some_and(|v| v != 0.0),
Value::String(s) => s == "true" || s == "1",
_ => false,
})
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct UnitEvent {
pub system: u16,
pub short_name: String,
pub kind: String,
pub unit: u32,
pub talkgroup: Option<u32>,
pub time: f64,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct AudioChunk {
pub call_id: u32,
pub system: u16,
pub short_name: String,
pub talkgroup: u32,
pub sample_rate: u32,
pub pcm: String,
}
impl AudioChunk {
pub fn new(call_id: u32, system: u16, talkgroup: u32, sample_rate: u32, samples: &[i16]) -> Self {
let bytes: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
AudioChunk { call_id, system, talkgroup, sample_rate, pcm: base64::encode(&bytes), ..Default::default() }
}
pub fn samples(&self) -> Vec<i16> {
let b = base64::decode(&self.pcm);
b.chunks_exact(2).map(|c| i16::from_le_bytes([c[0], c[1]])).collect()
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Status {
pub time: f64,
pub systems: Vec<SystemStatus>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct SystemStatus {
pub index: u16,
pub short_name: String,
pub control_channel_hz: Option<u64>,
pub decode_rate: f64,
pub active_calls: u32,
pub recording: u32,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct Shutdown {
pub grace_s: f64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum PluginMessage {
#[serde(rename = "ready")]
Ready,
#[serde(rename = "log")]
Log { level: Level, message: String },
#[serde(rename = "status")]
Status {
state: State,
#[serde(default)]
message: String,
},
#[serde(rename = "call.result")]
CallResult {
path: String,
outcome: Outcome,
#[serde(default, skip_serializing_if = "String::is_empty")]
message: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
url: String,
},
#[serde(rename = "metrics")]
Metrics(Metrics),
#[serde(other)]
Unknown,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default, rename_all = "camelCase")]
pub struct Metrics {
#[serde(skip_serializing_if = "Option::is_none")]
pub queued: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub retrying: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub in_flight: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub retries: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bytes_sent: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub latency_ms: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_ok: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_error: Option<f64>,
#[serde(skip_serializing_if = "String::is_empty")]
pub last_error_text: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub endpoints: Vec<Endpoint>,
#[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")]
pub extra: std::collections::BTreeMap<String, serde_json::Value>,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
#[serde(default, rename_all = "camelCase")]
pub struct Endpoint {
pub name: String,
pub state: EndpointState,
#[serde(skip_serializing_if = "Option::is_none")]
pub latency_ms: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_ok: Option<f64>,
#[serde(skip_serializing_if = "String::is_empty")]
pub last_error: String,
}
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum EndpointState {
#[default]
Unknown,
Up,
Degraded,
Down,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum Level {
Error,
Warn,
Info,
Debug,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum State {
Ok,
Warning,
Error,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Outcome {
Ok,
Skipped,
Failed,
}
pub mod base64 {
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn encode(b: &[u8]) -> String {
let mut o = String::with_capacity(b.len().div_ceil(3) * 4);
for c in b.chunks(3) {
let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
for i in 0..4 {
if i <= c.len() {
o.push(A[(n >> (18 - 6 * i) & 63) as usize] as char);
} else {
o.push('=');
}
}
}
o
}
pub fn decode(s: &str) -> Vec<u8> {
let mut o = Vec::with_capacity(s.len() / 4 * 3);
let (mut acc, mut bits) = (0u32, 0);
for ch in s.bytes() {
let v = match ch {
b'A'..=b'Z' => ch - b'A',
b'a'..=b'z' => ch - b'a' + 26,
b'0'..=b'9' => ch - b'0' + 52,
b'+' => 62,
b'/' => 63,
_ => continue,
};
acc = acc << 6 | v as u32;
bits += 6;
if bits >= 8 {
bits -= 8;
o.push((acc >> bits) as u8);
}
}
o
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn base64_roundtrip() {
for n in 0..10 {
let b: Vec<u8> = (0..n).map(|i| (i * 37 + 5) as u8).collect();
assert_eq!(base64::decode(&base64::encode(&b)), b);
}
assert_eq!(base64::encode(b"hello"), "aGVsbG8=");
}
#[test]
fn unknown_types_and_fields_are_ignored() {
let m: HostMessage = serde_json::from_str(r#"{"type":"from.the.future","x":1}"#).unwrap();
assert!(matches!(m, HostMessage::Unknown));
let m: HostMessage = serde_json::from_str(r#"{"type":"shutdown","grace_s":5,"new_field":true}"#).unwrap();
assert!(matches!(m, HostMessage::Shutdown(Shutdown { grace_s }) if grace_s == 5.0));
}
#[test]
fn call_record_reads_trunk_recorder_json() {
let j = r#"{"call_num":7,"freq":851012500,"start_time":1700000000,"stop_time":1700000005,"emergency":0,"encrypted":1,
"call_length":5,"talkgroup":101,"talkgroup_tag":"Fire Disp","audio_type":"digital","short_name":"dcfd","phase2_tdma":0,
"freqList":[{"freq":851012500,"time":1700000000,"pos":0,"len":5,"error_count":3,"spike_count":1}],
"srcList":[{"src":1234,"time":1700000000,"pos":0.5,"emergency":0,"signal_system":"","tag":"","tag_ota":"E1"}],
"color_code":-1,"patched_talkgroups":[101,65001]}"#;
let c: CallRecord = serde_json::from_str(j).unwrap();
assert!(c.encrypted && !c.emergency);
assert_eq!(c.patched_talkgroups, [101, 65001]);
assert_eq!((c.error_count(), c.spike_count()), (3, 1));
assert_eq!(c.src_list[0].tag_ota, "E1");
assert_eq!(c.extra["color_code"], -1);
let v = serde_json::to_value(&c).unwrap();
assert_eq!(v["color_code"], -1);
assert_eq!(v["srcList"][0]["src"], 1234);
assert_eq!(v["patched_talkgroups"], serde_json::json!([101, 65001]));
let unpatched = serde_json::to_value(CallRecord::default()).unwrap();
assert!(unpatched.get("patched_talkgroups").is_none());
}
}