1use core::convert::TryFrom;
2use core::fmt;
3
4pub type BOOLEAN = u8;
8pub type INTEGER8 = i8;
10pub type INTEGER16 = i16;
12pub type INTEGER24 = i32;
14pub type INTEGER32 = i32;
16pub type INTEGER64 = i64;
18pub type UNSIGNED8 = u8;
20pub type UNSIGNED16 = u16;
22pub type UNSIGNED24 = u32;
24pub type UNSIGNED32 = u32;
26pub type UNSIGNED64 = u64;
28pub type REAL32 = f32;
30pub type REAL64 = f64;
32pub type MacAddress = [u8; 6];
34pub type IpAddress = [u8; 4];
36
37
38#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct NodeId(pub u8);
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct EPLVersion(pub u8);
50
51pub const C_DLL_ETHERTYPE_EPL: u16 = 0x88AB;
55
56pub const C_DLL_ISOCHR_MAX_PAYL: u16 = 1490;
58
59pub const C_DLL_MAX_ASYNC_MTU: usize = 1500;
61
62pub const C_ADR_MN_DEF_NODE_ID: u8 = 240;
64
65pub const C_ADR_MAX_CN_NODE_ID: u8 = 239;
67
68pub const C_ADR_DIAG_DEF_NODE_ID: u8 = 253;
70
71pub const C_ADR_RT1_DEF_NODE_ID: u8 = 254;
73
74pub const C_ADR_BROADCAST_NODE_ID: u8 = 255;
76
77pub const C_DLL_MULTICAST_PRES: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x02];
79
80pub const C_DLL_MULTICAST_SOA: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x03];
82
83pub const C_DLL_MULTICAST_SOC: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x01];
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91#[repr(u8)]
92pub enum MessageType {
93 SoC = 0x01,
94 PReq = 0x03,
95 PRes = 0x04,
96 SoA = 0x05,
97 ASnd = 0x06,
98}
99
100#[derive(Debug)]
102pub struct InvalidMessageTypeError;
103
104impl TryFrom<u8> for MessageType {
105 type Error = InvalidMessageTypeError;
106
107 fn try_from(value: u8) -> Result<Self, Self::Error> {
108 match value {
109 0x01 => Ok(MessageType::SoC),
110 0x03 => Ok(MessageType::PReq),
111 0x04 => Ok(MessageType::PRes),
112 0x05 => Ok(MessageType::SoA),
113 0x06 => Ok(MessageType::ASnd),
114 _ => Err(InvalidMessageTypeError),
115 }
116 }
117}
118
119#[derive(Debug, PartialEq, Eq)]
121pub enum NodeIdError {
122 InvalidRange(u8),
124}
125
126impl fmt::Display for NodeIdError {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 match self {
129 NodeIdError::InvalidRange(value) => write!(f, "Invalid NodeId value: {}. Valid range is 1-240 or 253-255.", value),
130 }
131 }
132}
133
134#[cfg(feature = "std")]
135impl std::error::Error for NodeIdError {}
136
137impl TryFrom<u8> for NodeId {
138 type Error = NodeIdError;
139
140 fn try_from(value: u8) -> Result<Self, Self::Error> {
144 match value {
145 1..=C_ADR_MN_DEF_NODE_ID => Ok(NodeId(value)),
146 C_ADR_DIAG_DEF_NODE_ID | C_ADR_RT1_DEF_NODE_ID | C_ADR_BROADCAST_NODE_ID => Ok(NodeId(value)),
147 _ => Err(NodeIdError::InvalidRange(value)),
148 }
149 }
150}
151
152impl From<NodeId> for u8 {
153 fn from(node_id: NodeId) -> Self {
155 node_id.0
156 }
157}
158
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_nodeid_valid_ranges() {
166 assert_eq!(NodeId::try_from(1), Ok(NodeId(1)));
167 assert_eq!(NodeId::try_from(239), Ok(NodeId(239)));
168 assert_eq!(NodeId::try_from(240), Ok(NodeId(C_ADR_MN_DEF_NODE_ID)));
169 assert_eq!(NodeId::try_from(253), Ok(NodeId(C_ADR_DIAG_DEF_NODE_ID)));
170 assert_eq!(NodeId::try_from(254), Ok(NodeId(C_ADR_RT1_DEF_NODE_ID)));
171 assert_eq!(NodeId::try_from(255), Ok(NodeId(C_ADR_BROADCAST_NODE_ID)));
172 }
173
174 #[test]
175 fn test_nodeid_invalid_range() {
176 assert_eq!(NodeId::try_from(0), Err(NodeIdError::InvalidRange(0)));
177 assert_eq!(NodeId::try_from(241), Err(NodeIdError::InvalidRange(241)));
178 assert_eq!(NodeId::try_from(252), Err(NodeIdError::InvalidRange(252)));
179 }
180}