iso14229_1/common/
request_load.rs

1//! Commons of Service 34|35
2
3
4use crate::Error;
5
6/// This parameter is a one-byte value with each nibble encoded separately:
7/// ⎯ bit 7 - 4: length (number of bytes) of the maxNumberOfBlockLength parameter;
8/// ⎯ bit 3 - 0: reserved by document, to be set to 0 hex.
9/// The format of this parameter is compatible to the format of the addressAndLengthFormatIdentifier parameter contained
10/// in the request message, except that the lower nibble has to be set to 0 hex.
11#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
12pub struct LengthFormatIdentifier(pub(crate) u8);
13
14impl LengthFormatIdentifier {
15    #[inline]
16    pub fn new(value: u8) -> Result<Self, Error>{
17        if value > 0x0F {
18            return Err(Error::InvalidParam("`LengthFormatIdentifier` must be between 0x00 and 0xF0".to_string()));
19        }
20
21        Ok(Self(value << 4))
22    }
23
24    #[inline]
25    pub const fn max_number_of_block_length(&self) -> usize {
26        (self.0 >> 4) as usize
27    }
28}
29
30impl Into<u8> for LengthFormatIdentifier {
31    fn into(self) -> u8 {
32        self.0
33    }
34}
35
36impl TryFrom<u8> for LengthFormatIdentifier {
37    type Error = Error;
38    fn try_from(value: u8) -> Result<Self, Self::Error> {
39        if value > 0xF0 {
40            return Err(Error::InvalidParam("`LengthFormatIdentifier` must be between 0x00 and 0xF0".to_string()));
41        }
42
43        Ok(Self(value))
44    }
45}
46
47/// Defined by the vehicle manufacturer
48#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
49pub struct DataFormatIdentifier(pub(crate) u8);
50
51impl DataFormatIdentifier {
52    #[inline]
53    pub fn new(compression: u8, encryption: u8) -> Self {
54        Self((compression << 4) | encryption)
55    }
56    #[inline]
57    pub fn compression(&self) -> u8 {
58        self.0 >> 4
59    }
60    #[inline]
61    pub fn encryption(&self) -> u8 {
62        self.0 & 0x0F
63    }
64}
65
66impl From<u8> for DataFormatIdentifier {
67    #[inline]
68    fn from(value: u8) -> Self {
69        Self(value)
70    }
71}
72
73impl Into<u8> for DataFormatIdentifier {
74    #[inline]
75    fn into(self) -> u8 {
76        self.0
77    }
78}
79
80/// This parameter is a one Byte value with each nibble encoded separately (see Table H.1 for example values):
81/// — bit 7 - 4: Length (number of bytes) of the memorySize parameter
82/// — bit 3 - 0: Length (number of bytes) of the memoryAddress parameter
83#[derive(Debug, Clone, Copy, Eq, PartialEq)]
84pub struct AddressAndLengthFormatIdentifier(u8);
85
86impl AddressAndLengthFormatIdentifier {
87    pub fn new(addr_len: u8, size_len: u8) -> Result<Self, Error> {
88        let value = (size_len << 4) | addr_len;
89        Self::try_from(value)
90    }
91
92    #[inline]
93    pub const fn length_of_memory_address(&self) -> usize {
94        (self.0 & 0x0F) as usize
95    }
96
97    #[inline]
98    pub const fn length_of_memory_size(&self) -> usize {
99        ((self.0 & 0xF0) >> 4) as usize
100    }
101}
102
103impl Into<u8> for AddressAndLengthFormatIdentifier {
104    fn into(self) -> u8 {
105        self.0
106    }
107}
108
109impl TryFrom<u8> for AddressAndLengthFormatIdentifier {
110    type Error = Error;
111    fn try_from(value: u8) -> Result<Self, Self::Error> {
112        if value & 0x0F == 0
113            || value & 0xF0 == 0 {
114            return Err(Error::InvalidParam("all field of `AddressAndLengthFormatIdentifier` must be rather than 0".into()));
115        }
116
117        Ok(Self(value))
118    }
119}