Skip to main content

powerlink_rs/
types.rs

1use core::convert::TryFrom;
2use core::fmt;
3
4// --- Primitive Types (Based on DS 301 Section 6.1.4) ---
5
6/// Alias for BOOLEAN (8-bit unsigned integer, 0 or 1).
7pub type BOOLEAN = u8;
8/// Alias for INTEGER8 (8-bit signed integer).
9pub type INTEGER8 = i8;
10/// Alias for INTEGER16 (16-bit signed integer).
11pub type INTEGER16 = i16;
12/// Alias for INTEGER24 (32-bit signed integer, though only 24 bits used).
13pub type INTEGER24 = i32;
14/// Alias for INTEGER32 (32-bit signed integer).
15pub type INTEGER32 = i32;
16/// Alias for INTEGER64 (64-bit signed integer).
17pub type INTEGER64 = i64;
18/// Alias for UNSIGNED8 (8-bit unsigned integer).
19pub type UNSIGNED8 = u8;
20/// Alias for UNSIGNED16 (16-bit unsigned integer).
21pub type UNSIGNED16 = u16;
22/// Alias for UNSIGNED24 (32-bit unsigned integer, though only 24 bits used).
23pub type UNSIGNED24 = u32;
24/// Alias for UNSIGNED32 (32-bit unsigned integer).
25pub type UNSIGNED32 = u32;
26/// Alias for UNSIGNED64 (64-bit unsigned integer).
27pub type UNSIGNED64 = u64;
28/// Alias for REAL32 (32-bit floating point).
29pub type REAL32 = f32;
30/// Alias for REAL64 (64-bit floating point).
31pub type REAL64 = f64;
32/// Alias for a 6-byte MAC Address.
33pub type MacAddress = [u8; 6];
34/// Alias for a 4-byte IP Address.
35pub type IpAddress = [u8; 4];
36
37
38/// Represents a POWERLINK Node ID, wrapping a `u8` to ensure type safety.
39///
40/// Valid Node IDs are in the range 1-240, with special values for broadcast (255)
41/// and routers/management (253-254). This newtype pattern prevents accidental
42/// use of invalid `u8` values where a `NodeId` is required.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct NodeId(pub u8);
45
46/// Represents the POWERLINK Version.
47/// (EPSG DS 301, Table 112)
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub struct EPLVersion(pub u8);
50
51// --- Protocol Constants (Appendix 3) ---
52
53/// Ethernet EtherType for POWERLINK frames: 0x88AB.
54pub const C_DLL_ETHERTYPE_EPL: u16 = 0x88AB;
55
56/// Maximum size of PReq and PRes payload data (1490 Bytes).
57pub const C_DLL_ISOCHR_MAX_PAYL: u16 = 1490;
58
59/// Maximum asynchronous payload in bytes (1500 Bytes).
60pub const C_DLL_MAX_ASYNC_MTU: usize = 1500;
61
62/// POWERLINK default Node ID of the Managing Node (240).
63pub const C_ADR_MN_DEF_NODE_ID: u8 = 240;
64
65/// Maximum Node ID available for regular Controlled Nodes (239).
66pub const C_ADR_MAX_CN_NODE_ID: u8 = 239;
67
68/// POWERLINK Node ID for diagnostic device (253).
69pub const C_ADR_DIAG_DEF_NODE_ID: u8 = 253;
70
71/// POWERLINK Node ID for router (254).
72pub const C_ADR_RT1_DEF_NODE_ID: u8 = 254;
73
74/// POWERLINK Node ID for broadcast messages (255).
75pub const C_ADR_BROADCAST_NODE_ID: u8 = 255;
76
77/// POWERLINK PRes multicast MAC address: 01-11-1E-00-00-02.
78pub const C_DLL_MULTICAST_PRES: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x02];
79
80/// POWERLINK SoA multicast MAC address: 01-11-1E-00-00-03.
81pub const C_DLL_MULTICAST_SOA: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x03];
82
83/// POWERLINK SoC multicast MAC address: 01-11-1E-00-00-01.
84pub const C_DLL_MULTICAST_SOC: [u8; 6] = [0x01, 0x11, 0x1E, 0x00, 0x00, 0x01];
85
86// --- Core Protocol Identifiers ---
87
88/// Defines the mandatory POWERLINK Message Type IDs.
89/// (EPSG DS 301, Appendix 3.1) 
90#[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/// Error type for failed conversion from a raw u8 to a MessageType.
101#[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/// Error type for invalid Node ID creation.
120#[derive(Debug, PartialEq, Eq)]
121pub enum NodeIdError {
122    /// Node ID is outside the valid range.
123    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    /// Creates a `NodeId` from a `u8`, returning an error if the value is not valid.
141    ///
142    /// Valid IDs are 1-240, 253, 254, and 255.
143    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    /// Converts a `NodeId` back into its underlying `u8` representation.
154    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}