uds_client/uds_client/
pci.rs

1/// The definition for PCI byte of each frame
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum PciType {
4    SingleFrame,      // SF: Single Frame (<= 7 bytes) - 0x00-0x0F
5    FirstFrame,       // FF: First Frame (> 7 bytes) - 0x10-0x1F
6    ConsecutiveFrame, // CF: Consecutive Frame (multi-frame) - 0x20-0x2F
7    FlowControl,      // FC: Flow Control (to control CF transmission) 0x30-0x3F
8}
9
10#[derive(Debug, Clone, Copy)]
11pub struct PciByte {
12    pub pci_type: PciType,
13    pub value: u8, // The actual PCI byte
14}
15
16impl From<PciByte> for u8 {
17    fn from(pci_byte: PciByte) -> Self {
18        match pci_byte.pci_type {
19            PciType::SingleFrame => pci_byte.value & 0x0F,
20            PciType::FirstFrame => 0x10 | (pci_byte.value & 0x0F),
21            PciType::ConsecutiveFrame => 0x20 | (pci_byte.value & 0x0F),
22            PciType::FlowControl => 0x30 | (pci_byte.value & 0x0F),
23        }
24    }
25}
26
27/// The implementation for PCI byte as ISO 15765-2
28impl PciByte {
29    pub fn new(pci_type: PciType, value: u8) -> Self {
30        Self { pci_type, value }
31    }
32
33    pub fn get_type(&self) -> PciType {
34        self.pci_type
35    }
36
37    pub fn get_value(&self) -> u8 {
38        self.value
39    }
40
41    pub fn as_byte(&self) -> u8 {
42        match self.pci_type {
43            PciType::SingleFrame => self.value & 0x0F,
44            PciType::FirstFrame => 0x10 | (self.value & 0x0F),
45            PciType::ConsecutiveFrame => 0x20 | (self.value & 0x0F),
46            PciType::FlowControl => 0x30 | (self.value & 0x0F),
47        }
48    }
49}