osdp 0.2.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 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> {
        if data.len() != 8 {
            return Err(Error::MalformedPayload {
                code: 0x76,
                reason: "CHLNG requires 8-byte RND.A",
            });
        }
        let mut rnd_a = [0u8; 8];
        rnd_a.copy_from_slice(data);
        Ok(Self { rnd_a })
    }
}