mavwire 0.1.0

no_std, no_alloc parser and encoder for MAVLink v1/v2 telemetry messages
Documentation
//! HEARTBEAT (message id 0): the only message every `MAVLink` node must
//! send, showing that a system/component is present and responding.

use super::payload::{u8_at, u32_at};

/// This message's id.
pub const ID: u32 = 0;
/// This message's `crc_extra` byte (see [`crate::crc`]).
pub const CRC_EXTRA: u8 = 50;
const LEN: usize = 9;

/// A decoded HEARTBEAT message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Heartbeat {
    /// Vehicle/component type (`MAV_TYPE`).
    ///
    /// Named `mav_type` here (not `type`, a reserved word in Rust) to match
    /// the wire field's actual name as closely as Rust allows.
    pub mav_type: u8,
    /// Autopilot type/class (`MAV_AUTOPILOT`).
    pub autopilot: u8,
    /// System mode bitmask (`MAV_MODE_FLAG`).
    pub base_mode: u8,
    /// A navigation mode value private to the autopilot.
    pub custom_mode: u32,
    /// System status (`MAV_STATE`).
    pub system_status: u8,
    /// `MAVLink` version this node speaks.
    pub mavlink_version: u8,
}

impl Heartbeat {
    pub(crate) fn decode(payload: &[u8]) -> Self {
        Self {
            custom_mode: u32_at(payload, 0),
            mav_type: u8_at(payload, 4),
            autopilot: u8_at(payload, 5),
            base_mode: u8_at(payload, 6),
            system_status: u8_at(payload, 7),
            mavlink_version: u8_at(payload, 8),
        }
    }

    pub(crate) fn encode(&self) -> [u8; LEN] {
        let mut buf = [0u8; LEN];
        buf[0..4].copy_from_slice(&self.custom_mode.to_le_bytes());
        buf[4] = self.mav_type;
        buf[5] = self.autopilot;
        buf[6] = self.base_mode;
        buf[7] = self.system_status;
        buf[8] = self.mavlink_version;
        buf
    }
}

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

    #[test]
    fn decodes_a_real_captured_frame() {
        // ME payload from pymavlink's own HEARTBEAT encoder: type=2
        // autopilot=3 base_mode=81 custom_mode=0 system_status=4
        // mavlink_version=3.
        let payload = [0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03];
        let hb = Heartbeat::decode(&payload);
        assert_eq!(
            hb,
            Heartbeat {
                mav_type: 2,
                autopilot: 3,
                base_mode: 81,
                custom_mode: 0,
                system_status: 4,
                mavlink_version: 3,
            }
        );
    }

    #[test]
    fn encode_matches_the_real_captured_payload() {
        let hb = Heartbeat {
            mav_type: 2,
            autopilot: 3,
            base_mode: 81,
            custom_mode: 0,
            system_status: 4,
            mavlink_version: 3,
        };
        assert_eq!(
            hb.encode(),
            [0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x51, 0x04, 0x03]
        );
    }

    #[test]
    fn round_trips_through_decode() {
        let hb = Heartbeat {
            mav_type: 6,
            autopilot: 12,
            base_mode: 128,
            custom_mode: 0x1234_5678,
            system_status: 3,
            mavlink_version: 3,
        };
        assert_eq!(Heartbeat::decode(&hb.encode()), hb);
    }
}