iso14229_1/request/
routine_ctrl.rs1use crate::{Configuration,Error, Placeholder, RequestData, RoutineCtrlType, RoutineId, Service, utils};
5
6#[derive(Debug, Clone)]
7pub struct RoutineCtrl {
8 pub routine_id: RoutineId,
9 pub option_record: Vec<u8>,
10}
11
12impl<'a> TryFrom<&'a [u8]> for RoutineCtrl {
13 type Error = Error;
14 fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
15 utils::data_length_check(data.len(), 2, false)?;
16
17 let mut offset = 0;
18 let routine_id = u16::from_be_bytes([data[offset], data[offset + 1]]);
19 offset += 2;
20 let routine_id = RoutineId::from(routine_id);
21
22 Ok(Self { routine_id, option_record: data[offset..].to_vec() })
23 }
24}
25
26impl Into<Vec<u8>> for RoutineCtrl {
27 fn into(mut self) -> Vec<u8> {
28 let routine_id: u16 = self.routine_id.into();
29 let mut result = routine_id.to_be_bytes().to_vec();
30 result.append(&mut self.option_record);
31
32 result
33 }
34}
35
36impl RequestData for RoutineCtrl {
37 type SubFunc = Placeholder;
38
39 #[inline]
40 fn try_parse(data: &[u8], _: Option<Self::SubFunc>, _: &Configuration) -> Result<Self, Error> {
41 Self::try_from(data)
42 }
43
44 #[inline]
45 fn to_vec(self, _: &Configuration) -> Vec<u8> {
46 self.into()
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use crate::{CheckProgrammingDependencies, RoutineCtrlType};
53 use super::RoutineCtrl;
54
55 #[test]
56 fn new() -> anyhow::Result<()> {
57 let source = hex::decode("3101FF01")?;
58 let request = RoutineCtrl {
59 routine_id: CheckProgrammingDependencies,
60 option_record: vec![],
61 };
62 let result: Vec<_> = request.into();
63 assert_eq!(result, source[2..].to_vec());
64
65 let source = hex::decode("3101FF01112233445566")?;
66 let request = RoutineCtrl::try_from(&source[2..])?;
67
68 assert_eq!(request.routine_id, CheckProgrammingDependencies);
69 assert_eq!(request.option_record, hex::decode("112233445566")?);
70
71 Ok(())
72 }
73}
74