Skip to main content

osdp/reply/
bio.rs

1//! `osdp_BIOREADR` (`0x57`) and `osdp_BIOMATCHR` (`0x58`).
2//!
3//! # Spec: §7.14, §7.15
4
5use crate::command::{BioFormat, BioType};
6use crate::error::Error;
7use crate::payload_util::{require_at_least, require_exact_len};
8use alloc::vec::Vec;
9
10/// `osdp_BIOREADR` body.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct BioReadR {
13    /// Reader number.
14    pub reader: u8,
15    /// Biometric type captured.
16    pub bio_type: BioType,
17    /// Biometric format used.
18    pub bio_format: BioFormat,
19    /// Quality score (0..100) of the captured sample.
20    pub quality: u8,
21    /// Captured biometric data (template or raw).
22    pub data: Vec<u8>,
23}
24
25impl BioReadR {
26    /// Encode.
27    pub fn encode(&self) -> Result<Vec<u8>, Error> {
28        if self.data.len() > u16::MAX as usize {
29            return Err(Error::MalformedPayload {
30                code: 0x57,
31                reason: "BIOREADR data > 65535 bytes",
32            });
33        }
34        let mut out = Vec::with_capacity(6 + self.data.len());
35        out.push(self.reader);
36        out.push(self.bio_type.as_byte());
37        out.push(self.bio_format.as_byte());
38        out.push(self.quality);
39        out.extend_from_slice(&(self.data.len() as u16).to_le_bytes());
40        out.extend_from_slice(&self.data);
41        Ok(out)
42    }
43
44    /// Decode.
45    pub fn decode(data: &[u8]) -> Result<Self, Error> {
46        require_at_least(data, 6, 0x57)?;
47        let length = u16::from_le_bytes([data[4], data[5]]) as usize;
48        if data.len() != 6 + length {
49            return Err(Error::MalformedPayload {
50                code: 0x57,
51                reason: "BIOREADR length disagrees with payload",
52            });
53        }
54        Ok(Self {
55            reader: data[0],
56            bio_type: BioType::from_byte(data[1]),
57            bio_format: BioFormat::from_byte(data[2]),
58            quality: data[3],
59            data: data[6..6 + length].to_vec(),
60        })
61    }
62}
63
64/// `osdp_BIOMATCHR` body.
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct BioMatchR {
67    /// Reader number.
68    pub reader: u8,
69    /// Match outcome (`0` = no match, `1` = match).
70    pub result: u8,
71    /// Score (0..100).
72    pub score: u8,
73}
74
75impl BioMatchR {
76    /// Encode.
77    pub fn encode(&self) -> Result<Vec<u8>, Error> {
78        Ok(alloc::vec![self.reader, self.result, self.score])
79    }
80
81    /// Decode.
82    pub fn decode(data: &[u8]) -> Result<Self, Error> {
83        require_exact_len(data, 3, 0x58)?;
84        Ok(Self {
85            reader: data[0],
86            result: data[1],
87            score: data[2],
88        })
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn bioreadr_roundtrip() {
98        let body = BioReadR {
99            reader: 0x01,
100            bio_type: BioType::RightThumb,
101            bio_format: BioFormat::FingerprintAnsi378,
102            quality: 90,
103            data: alloc::vec![0xDE, 0xAD],
104        };
105        let bytes = body.encode().unwrap();
106        assert_eq!(bytes, [0x01, 0x01, 0x02, 90, 0x02, 0x00, 0xDE, 0xAD]);
107        assert_eq!(BioReadR::decode(&bytes).unwrap(), body);
108    }
109
110    #[test]
111    fn bioreadr_rejects_length_mismatch() {
112        assert!(matches!(
113            BioReadR::decode(&[0x01, 0x01, 0x02, 90, 0x05, 0x00, 0xAA]),
114            Err(Error::MalformedPayload { code: 0x57, .. })
115        ));
116    }
117
118    #[test]
119    fn biomatchr_roundtrip() {
120        let body = BioMatchR {
121            reader: 0x00,
122            result: 0x01,
123            score: 95,
124        };
125        let bytes = body.encode().unwrap();
126        assert_eq!(bytes, [0x00, 0x01, 95]);
127        assert_eq!(BioMatchR::decode(&bytes).unwrap(), body);
128    }
129
130    #[test]
131    fn biomatchr_rejects_wrong_length() {
132        assert!(matches!(
133            BioMatchR::decode(&[0x00, 0x01]),
134            Err(Error::PayloadLength { code: 0x58, .. })
135        ));
136    }
137}