osdp 0.3.1

Pure-Rust, no_std-friendly implementation of the SIA Open Supervised Device Protocol (OSDP) v2.2
Documentation
//! `osdp_CHLNG` (`0x76`) โ€” initiate Secure Channel session.
//!
//! # Spec: ยง6.17, Annex D.4
//!
//! Body is `RND.A` โ€” an 8-byte random challenge generated by the ACU.
//! The packet must carry an SCB of type `SCS_11`.

use crate::error::Error;
use crate::payload_util::require_exact_len;
use alloc::vec::Vec;

/// `osdp_CHLNG` body.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Chlng {
    /// `RND.A` โ€” 8 random bytes from the ACU.
    pub rnd_a: [u8; 8],
}

impl Chlng {
    /// New with the given `RND.A`.
    pub const fn new(rnd_a: [u8; 8]) -> Self {
        Self { rnd_a }
    }

    /// Encode.
    pub fn encode(&self) -> Result<Vec<u8>, Error> {
        Ok(self.rnd_a.to_vec())
    }

    /// Decode.
    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        require_exact_len(data, 8, 0x76)?;
        let mut rnd_a = [0u8; 8];
        rnd_a.copy_from_slice(data);
        Ok(Self { rnd_a })
    }
}

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

    #[test]
    fn roundtrip() {
        let body = Chlng::new([0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]);
        let bytes = body.encode().unwrap();
        assert_eq!(bytes.len(), 8);
        assert_eq!(Chlng::decode(&bytes).unwrap(), body);
    }

    #[test]
    fn decode_rejects_wrong_length() {
        assert!(matches!(
            Chlng::decode(&[0; 7]),
            Err(Error::PayloadLength { code: 0x76, .. })
        ));
        assert!(matches!(
            Chlng::decode(&[0; 9]),
            Err(Error::PayloadLength { code: 0x76, .. })
        ));
    }
}