uas-cot 0.3.1

Drone telemetry to Cursor on Target (CoT) for TAK: Remote ID, MISB ST 0601 KLV (STANAG 4609 video), MAVLink vehicle telemetry and MAVLink-M (military.xml) friendly position reports to ATAK/WinTAK/TAK Server, with a MAVLink-M framing checker and the uas2cot command line bridge. Telemetry and position interoperability only
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! Conformance checks of MAVLink frames and payloads against a dialect
//! definition (by default the embedded MAVLink-M `military.xml`).
//!
//! Every check comes from the XML itself, so a newer `military.xml` loaded
//! with [`Dialect::from_file`] is checked by its own rules:
//!
//! | Check | Source in the XML |
//! |---|---|
//! | message id is defined | `<message id>` |
//! | payload length (MAVLink 1: exact; MAVLink 2: not longer than the message, trailing zeros trimmed) | field types and `<extensions/>` |
//! | checksum, with the message's `CRC_EXTRA` | field names, types and order |
//! | enum fields hold a defined value | `enum=` and `<entry value>` |
//! | protected enums: an undefined value is reported as corruption, with the distance to the nearest valid codeword | entries whose pairwise Hamming distance is 4 or more |
//! | bitmask fields set no undefined bits | `bitmask="true"` |
//! | `degE7` latitude and longitude are in range | `units="degE7"` |
//! | ranges stated in descriptions: `[0.0-1.0]`, `[0-100]` (with `255 = N/A`), `0 to 10000` | field `<description>` |
//! | fields described "Never 0" are not 0 | field `<description>` |
//! | `char[N]` text is valid UTF-8 | field types |
//! | a track UID is not all zero, unless the description allows it | field `<description>` |
//!
//! Out-of-scope messages (target, track, handover, fires, munition, arming,
//! weapon, engagement, payload, see [`super::scope`]) get the frame checks only: id,
//! length and checksum. Their fields are never decoded, range-checked or
//! printed; the report marks them [`Report::framing_only`].

use super::dialect::{crc_accumulate, Dialect, Field, FieldType, Message, Value};
use std::fmt;

/// How serious a finding is.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
    /// Allowed by the definition but worth a look.
    Warning,
    /// The message does not conform to the definition.
    Error,
}

/// One problem found in a message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Finding {
    /// Severity.
    pub severity: Severity,
    /// Field the finding is about, if any.
    pub field: Option<String>,
    /// What is wrong.
    pub message: String,
}

/// The result of checking one message.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Report {
    /// Message id.
    pub msg_id: u32,
    /// Message name, if the id is defined.
    pub name: Option<String>,
    /// Sender system id (frames only).
    pub sys_id: Option<u8>,
    /// Sender component id (frames only).
    pub comp_id: Option<u8>,
    /// Findings, in check order.
    pub findings: Vec<Finding>,
    /// True for an out-of-scope message ([`super::scope::is_blocked`]):
    /// only id, length and checksum were checked; no field was decoded.
    pub framing_only: bool,
}

impl Report {
    /// True if there is no [`Severity::Error`] finding.
    pub fn is_ok(&self) -> bool {
        self.findings.iter().all(|f| f.severity != Severity::Error)
    }

    fn push(&mut self, severity: Severity, field: Option<&str>, message: String) {
        self.findings.push(Finding {
            severity,
            field: field.map(str::to_string),
            message,
        });
    }
}

impl fmt::Display for Report {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = self.name.as_deref().unwrap_or("?");
        write!(f, "{} ({})", name, self.msg_id)?;
        if let (Some(s), Some(c)) = (self.sys_id, self.comp_id) {
            write!(f, " from {s}/{c}")?;
        }
        if self.framing_only {
            write!(f, " [out of scope: framing, length and checksum only]")?;
        }
        if self.findings.is_empty() {
            return write!(f, ": ok");
        }
        for x in &self.findings {
            let sev = match x.severity {
                Severity::Error => "error",
                Severity::Warning => "warning",
            };
            write!(f, "\n  {sev}")?;
            if let Some(fl) = &x.field {
                write!(f, " [{fl}]")?;
            }
            write!(f, ": {}", x.message)?;
        }
        Ok(())
    }
}

const MAGIC_V1: u8 = 0xFE;
const MAGIC_V2: u8 = 0xFD;

/// Length of the frame at the start of `data`, if it is complete.
pub fn frame_len(data: &[u8]) -> Option<usize> {
    match *data.first()? {
        MAGIC_V1 => Some(8 + *data.get(1)? as usize),
        MAGIC_V2 => {
            let len = *data.get(1)? as usize;
            let signed = *data.get(2)? & 0x01 != 0;
            Some(12 + len + if signed { 13 } else { 0 })
        }
        _ => None,
    }
    .filter(|&n| n <= data.len())
}

/// Checks one complete MAVLink 1 or 2 frame (start byte to checksum, plus
/// the signature if the frame is signed). The signature itself is not
/// verified; that needs the link's secret key.
pub fn check_frame(d: &Dialect, frame: &[u8]) -> Report {
    let mut r = Report {
        msg_id: 0,
        name: None,
        sys_id: None,
        comp_id: None,
        findings: Vec::new(),
        framing_only: false,
    };
    let Some(&magic) = frame.first() else {
        r.push(Severity::Error, None, "empty frame".into());
        return r;
    };
    let (hdr, v2) = match magic {
        MAGIC_V1 => (6, false),
        MAGIC_V2 => (10, true),
        m => {
            r.push(Severity::Error, None, format!("bad start byte 0x{m:02X}"));
            return r;
        }
    };
    let Some(total) = frame_len(frame) else {
        r.push(Severity::Error, None, "truncated frame".into());
        return r;
    };
    let len = frame[1] as usize;
    if v2 {
        if frame[2] & !0x01 != 0 {
            r.push(
                Severity::Error,
                None,
                format!("unknown incompatibility flags 0x{:02X}", frame[2]),
            );
        }
        r.sys_id = Some(frame[5]);
        r.comp_id = Some(frame[6]);
        r.msg_id = u32::from_le_bytes([frame[7], frame[8], frame[9], 0]);
    } else {
        r.sys_id = Some(frame[3]);
        r.comp_id = Some(frame[4]);
        r.msg_id = frame[5] as u32;
    }
    let payload = &frame[hdr..hdr + len];
    let found = u16::from_le_bytes([frame[hdr + len], frame[hdr + len + 1]]);
    let inner = check_payload(d, r.msg_id, payload, v2);
    r.name = inner.name.clone();
    r.framing_only = inner.framing_only;
    if let Some(m) = d.message(r.msg_id) {
        let mut crc = 0xFFFF;
        for &b in &frame[1..hdr + len] {
            crc = crc_accumulate(b, crc);
        }
        crc = crc_accumulate(m.crc_extra, crc);
        if crc != found {
            r.push(
                Severity::Error,
                None,
                format!(
                    "checksum 0x{found:04X} does not match 0x{crc:04X} (CRC_EXTRA {} from the definition): the sender uses a different definition of {} or the frame is corrupt",
                    m.crc_extra, m.name
                ),
            );
        }
    }
    if total != frame.len() {
        r.push(
            Severity::Warning,
            None,
            format!("{} bytes after the frame", frame.len() - total),
        );
    }
    r.findings.extend(inner.findings);
    r
}

/// Checks a payload of message `msg_id`. `v2` selects the MAVLink 2 length
/// rules (trailing zero bytes trimmed).
pub fn check_payload(d: &Dialect, msg_id: u32, payload: &[u8], v2: bool) -> Report {
    let mut r = Report {
        msg_id,
        name: None,
        sys_id: None,
        comp_id: None,
        findings: Vec::new(),
        framing_only: false,
    };
    let Some(m) = d.message(msg_id) else {
        r.push(
            Severity::Error,
            None,
            format!("message id {msg_id} is not defined in {}", d.name),
        );
        return r;
    };
    r.name = Some(m.name.clone());
    check_length(m, payload, v2, &mut r);
    if m.is_blocked() {
        // out of scope: no field is decoded, checked or reported
        r.framing_only = true;
        return r;
    }
    for f in &m.fields {
        let v = super::dialect::decode_field(f, payload);
        check_field(d, f, &v, &mut r);
    }
    r
}

fn check_length(m: &Message, payload: &[u8], v2: bool, r: &mut Report) {
    let n = payload.len();
    if v2 {
        if n > m.max_len {
            r.push(
                Severity::Error,
                None,
                format!("payload is {n} bytes; {} is at most {}", m.name, m.max_len),
            );
        } else if n == 0 {
            r.push(
                Severity::Error,
                None,
                "MAVLink 2 payloads carry at least one byte".into(),
            );
        } else if n > 1 && payload[n - 1] == 0 {
            r.push(
                Severity::Warning,
                None,
                "trailing zero bytes not trimmed (MAVLink 2 senders trim them)".into(),
            );
        }
    } else {
        if m.id > 255 {
            r.push(
                Severity::Error,
                None,
                format!("message id {} does not fit a MAVLink 1 frame", m.id),
            );
        }
        if n != m.base_len {
            r.push(
                Severity::Error,
                None,
                format!(
                    "MAVLink 1 payload is {n} bytes; {} is {}",
                    m.name, m.base_len
                ),
            );
        }
    }
}

fn check_field(d: &Dialect, f: &Field, v: &Value, r: &mut Report) {
    let name = Some(f.name.as_str());
    let desc = f.description.as_str();
    // enums
    if let Some(en) = f.enum_name.as_deref() {
        let values: Vec<u64> = match v {
            Value::Array(items) => items.iter().filter_map(Value::as_u64).collect(),
            other => other.as_u64().into_iter().collect(),
        };
        match d.enums.get(en) {
            None => r.push(
                Severity::Warning,
                name,
                format!("enum {en} is not defined in {} (missing include?)", d.name),
            ),
            Some(e) if e.bitmask => {
                let all = e.all_bits();
                for x in values {
                    if x & !all != 0 {
                        r.push(
                            Severity::Error,
                            name,
                            format!("bits 0x{:X} are not defined in bitmask {en}", x & !all),
                        );
                    }
                }
            }
            Some(e) => {
                for x in values {
                    if e.entry(x).is_some() {
                        continue;
                    }
                    let protected = e.entries.len() >= 3 && e.min_hamming_distance() >= Some(4);
                    let mut msg = format!("{x} is not a defined {en} value");
                    if protected {
                        let near = e
                            .entries
                            .iter()
                            .map(|y| ((y.value ^ x).count_ones(), &y.name))
                            .min();
                        msg.push_str(
                            ": this enum's values are codewords at least 4 bits apart, so the field is corrupt; reject it, do not correct it",
                        );
                        if let Some((dist, n)) = near {
                            msg.push_str(&format!(" (nearest codeword {n}, {dist} bits away)"));
                        }
                    }
                    r.push(Severity::Error, name, msg);
                }
            }
        }
    }
    // latitude / longitude
    if f.units.as_deref() == Some("degE7") {
        if let Some(x) = v.as_i64() {
            let limit = if f.name.contains("lon") {
                1_800_000_000
            } else if f.name.contains("lat") {
                900_000_000
            } else {
                i64::MAX
            };
            if x.abs() > limit {
                r.push(
                    Severity::Error,
                    name,
                    format!("{x} degE7 is out of range (|value| <= {limit})"),
                );
            }
        }
    }
    // description-stated ranges
    if let Some(x) = v.as_f64() {
        if desc.contains("[0.0-1.0]") {
            if x.is_nan() {
                if !desc.contains("NaN") {
                    r.push(
                        Severity::Warning,
                        name,
                        "NaN where the description gives the range [0.0-1.0]".into(),
                    );
                }
            } else if !(0.0..=1.0).contains(&x) {
                r.push(Severity::Error, name, format!("{x} is outside [0.0-1.0]"));
            }
        }
        if desc.contains("[0-100]") {
            let na = desc.contains("255 = N/A") && x == 255.0;
            if !na && !(0.0..=100.0).contains(&x) {
                r.push(Severity::Error, name, format!("{x} is outside [0-100]"));
            }
        }
        if desc.contains("0 to 10000") && x > 10_000.0 {
            r.push(Severity::Error, name, format!("{x} is above 10000"));
        }
        if desc.contains("Never 0") && x == 0.0 {
            r.push(
                Severity::Error,
                name,
                "is 0; the definition says it is never 0".into(),
            );
        }
        if f.ty == FieldType::F32 && x.is_infinite() {
            r.push(Severity::Error, name, "is infinite".into());
        }
    }
    // text
    if let (FieldType::Char, Some(_), Value::Text(s)) = (f.ty, f.array_len, v) {
        if s.contains('\u{FFFD}') {
            r.push(Severity::Warning, name, "text is not valid UTF-8".into());
        }
    }
    // track UIDs
    if f.name.ends_with("track_uid") && !f.name.starts_with("parent") {
        if let Some(b) = v.as_bytes() {
            if b.iter().all(|&x| x == 0) && !desc.contains("All-zero if") {
                r.push(
                    Severity::Warning,
                    name,
                    "track UID is all zero, so it cannot correlate this track".into(),
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn frame_len_handles_signed_and_short() {
        assert_eq!(frame_len(&[]), None);
        assert_eq!(
            frame_len(&[0xFD, 1, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0]),
            Some(13)
        );
        assert_eq!(frame_len(&[0xFD, 1, 1, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0]), None);
        assert_eq!(frame_len(&[0x00, 1]), None);
    }
}