mavwire 0.1.0

no_std, no_alloc parser and encoder for MAVLink v1/v2 telemetry messages
Documentation
//! `MAVLink`'s CRC-16/MCRF4XX checksum, plus the per-message `CRC_EXTRA` byte
//! that also catches a sender/receiver message-definition mismatch, not
//! just bit errors.
//!
//! Ported byte-for-byte from `pymavlink`'s own `x25crc_slow`
//! (`pymavlink/generator/mavcrc.py`, itself based on the reference `MAVLink`
//! C library's `checksum.h`): a bit-shift accumulator, no lookup table
//! needed.

const INITIAL: u16 = 0xFFFF;

#[allow(
    clippy::cast_possible_truncation,
    reason = "intentionally takes only the low byte of crc, matching pymavlink's `accum & 0xFF`"
)]
fn accumulate_byte(crc: u16, byte: u8) -> u16 {
    let mut tmp = byte ^ (crc as u8);
    tmp ^= tmp << 4;
    let tmp = u16::from(tmp);
    (crc >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)
}

/// Computes the checksum over `header_and_payload` (everything in the frame
/// after the leading magic byte, i.e. length/flags/seq/ids/msgid/payload),
/// folding in the message's `crc_extra` byte last, matching `MAVLink`'s own
/// `_pack`/parity check order.
#[must_use]
pub fn checksum(header_and_payload: &[u8], crc_extra: u8) -> u16 {
    let mut crc = INITIAL;
    for &byte in header_and_payload {
        crc = accumulate_byte(crc, byte);
    }
    accumulate_byte(crc, crc_extra)
}

// The loop in checksum() has a compile-time-fixed trip count (the array
// length below), so CBMC can unwind it automatically -- no #[kani::unwind]
// needed (that's only required for a loop whose trip count is data-
// dependent rather than fixed at compile time).
#[cfg(kani)]
mod kani_proofs {
    use super::checksum;

    #[kani::proof]
    fn checksum_never_panics() {
        let data: [u8; 16] = kani::any();
        let crc_extra: u8 = kani::any();
        let _ = checksum(&data, crc_extra);
    }
}

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

    #[test]
    fn matches_pymavlink_on_an_arbitrary_buffer() {
        // x25crc_slow().accumulate([1,2,3,4,5]).crc, no crc_extra folded in
        // (verified directly against pymavlink's own class).
        let mut crc = INITIAL;
        for b in [1u8, 2, 3, 4, 5] {
            crc = accumulate_byte(crc, b);
        }
        assert_eq!(crc, 0xdd13);
    }

    #[test]
    fn matches_a_real_captured_heartbeat_frame() {
        // Real v2 frame from pymavlink: fd 09 00 00 00 01 01 00 00 00
        // 00000000 02 03 51 04 03 e7 1e (HEARTBEAT type=2 autopilot=3
        // base_mode=81 custom_mode=0 system_status=4 mavlink_version=3,
        // sysid=1 compid=1 seq=0). crc_extra for HEARTBEAT is 50; expected
        // checksum is the trailing e7 1e, read little-endian as 0x1ee7.
        #[rustfmt::skip]
        let header_and_payload: [u8; 18] = [
            0x09, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, // header (minus magic)
            0x00, 0x00, 0x00, 0x00, // custom_mode = 0
            0x02, // type
            0x03, // autopilot
            0x51, // base_mode = 81
            0x04, // system_status
            0x03, // mavlink_version
        ];
        assert_eq!(checksum(&header_and_payload, 50), 0x1ee7);
    }
}