iso14229_1/common/
communication_ctrl.rs

1//! Commons of Service 28
2
3use crate::{Error, utils};
4
5#[repr(u8)]
6#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
7pub enum CommunicationCtrlType {
8    EnableRxAndTx = 0x00,
9    EnableRxAndDisableTx = 0x01,
10    DisableRxAndEnableTx = 0x02,
11    DisableRxAndTx = 0x03,
12    EnableRxAndDisableTxWithEnhancedAddressInformation = 0x04,
13    EnableRxAndTxWithEnhancedAddressInformation = 0x05,
14    VehicleManufacturerSpecific(u8),
15    SystemSupplierSpecific(u8),
16    Reserved(u8),
17}
18
19impl TryFrom<u8> for CommunicationCtrlType {
20    type Error = Error;
21    fn try_from(value: u8) -> Result<Self, Self::Error> {
22        match value {
23            0x00 => Ok(Self::EnableRxAndTx),
24            0x01 => Ok(Self::EnableRxAndDisableTx),
25            0x02 => Ok(Self::DisableRxAndEnableTx),
26            0x03 => Ok(Self::DisableRxAndTx),
27            0x04 => Ok(Self::EnableRxAndDisableTxWithEnhancedAddressInformation),
28            0x05 => Ok(Self::EnableRxAndTxWithEnhancedAddressInformation),
29            0x06..=0x3F => Ok(Self::Reserved(value)),
30            0x40..=0x5F => Ok(Self::VehicleManufacturerSpecific(value)),
31            0x60..=0x7E => Ok(Self::SystemSupplierSpecific(value)),
32            0x7F => Ok(Self::Reserved(value)),
33            v => Err(Error::InvalidParam(utils::err_msg(v))),
34        }
35    }
36}
37
38impl Into<u8> for CommunicationCtrlType {
39    fn into(self) -> u8 {
40        match self {
41            Self::EnableRxAndTx => 0x00,
42            Self::EnableRxAndDisableTx => 0x01,
43            Self::DisableRxAndEnableTx => 0x02,
44            Self::DisableRxAndTx => 0x03,
45            Self::EnableRxAndDisableTxWithEnhancedAddressInformation => 0x04,
46            Self::EnableRxAndTxWithEnhancedAddressInformation => 0x05,
47            Self::VehicleManufacturerSpecific(v) => v,
48            Self::SystemSupplierSpecific(v) => v,
49            Self::Reserved(v) => v,
50        }
51    }
52}
53
54
55#[derive(Debug, Copy, Clone, Eq, PartialEq)]
56pub struct CommunicationType(pub(crate) u8);
57
58bitflags::bitflags! {
59    impl CommunicationType: u8 {
60        const NormalCommunicationMessages = 0x01;
61        const NetworkManagementCommunicationMessages = 0x02;
62    }
63}
64
65impl CommunicationType {
66    #[inline]
67    pub fn new(
68        comm_type: CommunicationType,
69        subnet: u8,
70    ) -> Result<Self, Error> {
71        if subnet > 0x0F {
72            return Err(Error::InvalidParam(utils::err_msg(subnet)));
73        }
74
75        let mut result = comm_type.bits();
76        result |= subnet << 4;
77
78        Ok(Self(result))
79    }
80
81    #[inline]
82    pub fn value(&self) -> u8 {
83        self.0
84    }
85}
86