iso14229_1/common/
secured_data_trans.rs

1//! Commons of Service 84
2
3
4use std::ops::{BitAnd, BitXorAssign};
5use bitfield_struct::bitfield;
6use crate::{Error, utils};
7
8
9/// Table 490 — Definition of Administrative Parameter
10///
11/// ### Repr: `u16`
12/// | Field                                  | Size (bits) |
13/// |----------------------------------------|-------------|
14/// | Message is request message             | 1           |
15/// | ISO Reserved                           | 2           |
16/// | A pre-established key is used          | 1           |
17/// | Message is encrypted                   | 1           |
18/// | Message is signed                      | 1           |
19/// | Signature on the response is requested | 1           |
20/// | ISO reserved                           | 4           |
21/// | ISO reserved                           | 5           |
22#[bitfield(u16, order = Lsb)]
23pub struct AdministrativeParameter {
24    #[bits(1)]
25    request: bool,
26    #[bits(2)]
27    reserved0: u8,
28    #[bits(1)]
29    pre_established: bool,
30    #[bits(1)]
31    encrypted: bool,
32    #[bits(1)]
33    signed: bool,
34    #[bits(1)]
35    signature_on_response: bool,
36    #[bits(4)]
37    reserved1: u8,
38    #[bits(5)]
39    reserved2: u8,
40}
41
42impl Into<Vec<u8>> for AdministrativeParameter {
43    #[inline]
44    fn into(self) -> Vec<u8> {
45        self.0.to_be_bytes().to_vec()
46    }
47}
48
49impl AdministrativeParameter {
50
51    #[inline]
52    pub const fn is_request(&self) -> bool {
53        self.request()
54    }
55
56    #[inline]
57    pub fn request_set(&mut self, value: bool) -> &mut Self {
58        self.set_request(value);
59        self
60    }
61
62    #[inline]
63    pub const fn is_pre_established(&self) -> bool {
64        self.pre_established()
65    }
66
67    #[inline]
68    pub fn pre_established_set(&mut self, value: bool) -> &mut Self {
69        self.set_pre_established(value);
70        self
71    }
72
73    #[inline]
74    pub const fn is_encrypted(&self) -> bool {
75        self.encrypted()
76    }
77
78    #[inline]
79    pub fn encrypted_set(&mut self, value: bool) -> &mut Self {
80        self.set_encrypted(value);
81        self
82    }
83
84    #[inline]
85    pub const fn is_signed(&self) -> bool {
86        self.signed()
87    }
88
89    #[inline]
90    pub fn signed_set(&mut self, value: bool) -> &mut Self {
91        self.set_signed(value);
92        self
93    }
94
95    #[inline]
96    pub const fn is_signature_on_response(&self) -> bool {
97        self.signature_on_response()
98    }
99
100    #[inline]
101    pub fn signature_on_response_set(&mut self, value: bool) -> &mut Self {
102        self.set_signature_on_response(value);
103        self
104    }
105}
106
107/// Table 491 — Definition of Signature/Encryption calculation parameter
108#[derive(Debug, Copy, Clone, Eq, PartialEq)]
109pub enum SignatureEncryptionCalculation {
110    VehicleManufacturerSpecific(u8),    // 00 to 7F
111    SystemSupplier(u8),                 // 80 to 8F
112}
113
114impl TryFrom<u8> for SignatureEncryptionCalculation {
115    type Error = Error;
116    #[inline]
117    fn try_from(value: u8) -> Result<Self, Self::Error> {
118        match value {
119            0x00..=0x7F => Ok(Self::VehicleManufacturerSpecific(value)),
120            0x80..=0x8F => Ok(Self::SystemSupplier(value)),
121            v => Err(Error::InvalidParam(utils::err_msg(v)))
122        }
123    }
124}
125
126impl Into<u8> for SignatureEncryptionCalculation {
127    #[inline]
128    fn into(self) -> u8 {
129        match self {
130            SignatureEncryptionCalculation::VehicleManufacturerSpecific(v) |
131            SignatureEncryptionCalculation::SystemSupplier(v) => v,
132        }
133    }
134}
135
136#[cfg(test)]
137mod test_apar {
138    use super::AdministrativeParameter;
139
140    #[test]
141    fn apar() -> anyhow::Result<()> {
142        let mut value: AdministrativeParameter = Default::default();
143        assert_eq!(value.is_request(), false);
144
145        value.request_set(true);
146        assert_eq!(value.is_request(), true);
147
148        Ok(())
149    }
150}