rtcm3 0.1.0

no_std, no_alloc parser and encoder for RTCM3 GNSS RTK correction messages
Documentation
//! A `no_std`, allocation-free parser and encoder for RTCM3 GNSS RTK
//! correction messages, the binary wire format used to distribute
//! differential GNSS/RTK corrections between base stations, NTRIP
//! casters, and rover receivers.
//!
//! # Layering
//!
//! - [`frame`] parses/encodes the raw preamble/length/CRC-24Q framing.
//! - [`bits`] reads and writes arbitrary-width, MSB-first fields against
//!   the frame's payload bytes.
//! - [`crc`] is RTCM3's CRC-24Q checksum.
//! - [`message`] decodes/encodes a frame's payload into a typed
//!   [`Rtcm3Message`], one variant per supported message type (DF002).
//!
//! # No allocation
//!
//! Nothing in this crate uses `alloc`.
#![no_std]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

pub mod bits;
pub mod crc;
pub mod error;
pub mod frame;
pub mod message;

pub use error::Rtcm3Error;
pub use frame::Frame;
pub use message::Rtcm3Message;

/// Parses and decodes a single RTCM3 frame from the start of `buf`.
///
/// # Errors
/// Returns an [`Rtcm3Error`] if the frame fails to parse/CRC-validate, or
/// its message type isn't one this crate decodes yet.
pub fn decode(buf: &[u8]) -> Result<Rtcm3Message, Rtcm3Error> {
    let frame = Frame::parse(buf)?;
    Ok(Rtcm3Message::decode(frame.payload())?)
}

/// Encodes a typed message into a full frame in `buf`, returning the
/// number of bytes written.
///
/// # Errors
/// Returns an [`Rtcm3Error`] if the encoded payload doesn't fit in `buf`
/// (size it with [`frame::MAX_FRAME_LEN`] to always be safe).
pub fn encode(message: &Rtcm3Message, buf: &mut [u8]) -> Result<usize, Rtcm3Error> {
    let mut payload = [0u8; 1023];
    let payload_len = message.encode(&mut payload)?;
    Ok(frame::encode(buf, &payload[..payload_len])?)
}

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

    fn hex_decode<const N: usize>(hex: &str) -> [u8; N] {
        let mut out = [0u8; N];
        for (i, chunk) in hex.as_bytes().chunks(2).enumerate() {
            let s = core::str::from_utf8(chunk).unwrap();
            out[i] = u8::from_str_radix(s, 16).unwrap();
        }
        out
    }

    #[test]
    fn decodes_a_real_captured_frame() {
        // Real MT1005 frame from pyrtcm's own test corpus (pygpsdata-RTCM3.log).
        let buf: [u8; 25] = hex_decode("d300133ed000038a58d9493c872f34109d07d6af48205ad7f7");
        let msg = decode(&buf).unwrap();
        let Rtcm3Message::StationArp(m) = msg else {
            panic!("expected StationArp");
        };
        assert_eq!(m.reference_station_id, 0);
        assert!((m.x_meters() - 4_444_030.802_8).abs() < 1e-3);
    }

    #[test]
    fn encode_round_trips() {
        let original = Rtcm3Message::StationArp(message::StationArp {
            reference_station_id: 42,
            itrf_realization_year: 19,
            gps_indicator: true,
            glonass_indicator: true,
            galileo_indicator: false,
            reference_station_indicator: false,
            ecef_x_raw: 44_440_308_028,
            single_oscillator_indicator: false,
            ecef_y_raw: 30_856_712_349,
            quarter_cycle_indicator: 1,
            ecef_z_raw: 33_666_582_560,
        });
        let mut buf = [0u8; frame::MAX_FRAME_LEN];
        let n = encode(&original, &mut buf).unwrap();
        assert_eq!(decode(&buf[..n]).unwrap(), original);
    }
}