1use crate::od::ObjectValue;
2use crate::pdo::PayloadSizeError;
3use crate::types::{InvalidMessageTypeError, NodeIdError};
4use alloc::collections::BTreeMap;
5use core::array::TryFromSliceError;
6use core::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum PowerlinkError {
11 BufferTooShort,
13 IoError,
15 InvalidEthernetFrame,
17 InvalidPlFrame,
19 InvalidMessageType(u8),
21 InvalidNmtState(u8),
23 InvalidServiceId(u8),
25 InvalidRequestedServiceId(u8),
27 InvalidNodeId(u8),
29 InvalidPayloadSize(u16),
31 InvalidEnumValue,
33 SliceConversion,
35 FrameTooLarge,
37 NotReady,
39 ObjectNotFound,
41 SubObjectNotFound,
43 TypeMismatch,
45 StorageError(&'static str),
47 ValidationError(&'static str),
49 SdoSequenceError,
51 SdoAborted,
53}
54
55impl fmt::Display for PowerlinkError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match self {
58 Self::BufferTooShort => write!(f, "Buffer is too short for the frame"),
59 Self::IoError => write!(f, "An underlying I/O error occurred"),
60 Self::InvalidEthernetFrame => write!(f, "Frame is not a valid Ethernet II frame"),
61 Self::InvalidPlFrame => write!(f, "Frame is not a valid POWERLINK frame"),
62 Self::InvalidMessageType(v) => write!(f, "Invalid MessageType value: {v:#04x}"),
63 Self::InvalidNmtState(v) => write!(f, "Invalid NMT State value: {v:#04x}"),
64 Self::InvalidServiceId(v) => write!(f, "Invalid ServiceId value: {v:#04x}"),
65 Self::InvalidRequestedServiceId(v) => write!(f, "Invalid RequestedServiceId value: {v:#04x}"),
66 Self::InvalidNodeId(v) => write!(f, "Invalid NodeId value: {v}"),
67 Self::InvalidPayloadSize(v) => write!(f, "Invalid PayloadSize value: {v}"),
68 Self::SliceConversion => write!(f, "Failed to convert slice to a fixed-size array"),
69 Self::FrameTooLarge => write!(f, "Frame size exceeds maximum allowed MTU"),
70 Self::NotReady => write!(f, "Device is not ready or configured"),
71 Self::ObjectNotFound => write!(f, "The requested Object Dictionary index was not found"),
72 Self::SubObjectNotFound => write!(f, "The requested sub-index was not found for this object"),
73 Self::TypeMismatch => write!(f, "The provided value's type does not match the object's type"),
74 Self::InvalidEnumValue => write!(f, "A value in the frame is not a valid enum variant"),
75 Self::StorageError(s) => write!(f, "Storage error: {}", s),
76 Self::ValidationError(s) => write!(f, "OD Validation Error: {}", s),
77 Self::SdoSequenceError => write!(f, "SDO sequence number mismatch detected"),
78 Self::SdoAborted => write!(f, "SDO transfer was aborted"),
79 }
80 }
81}
82
83#[cfg(feature = "std")]
84impl std::error::Error for PowerlinkError {}
85
86impl From<InvalidMessageTypeError> for PowerlinkError {
89 fn from(_: InvalidMessageTypeError) -> Self {
90 PowerlinkError::InvalidEnumValue
91 }
92}
93
94impl From<TryFromSliceError> for PowerlinkError {
95 fn from(_: TryFromSliceError) -> Self {
96 PowerlinkError::SliceConversion
97 }
98}
99
100impl From<NodeIdError> for PowerlinkError {
101 fn from(err: NodeIdError) -> Self {
102 match err {
103 NodeIdError::InvalidRange(val) => PowerlinkError::InvalidNodeId(val),
104 }
105 }
106}
107
108impl From<PayloadSizeError> for PowerlinkError {
109 fn from(err: PayloadSizeError) -> Self {
110 match err {
111 PayloadSizeError::InvalidRange(val) => PowerlinkError::InvalidPayloadSize(val),
112 }
113 }
114}
115
116impl From<&'static str> for PowerlinkError {
117 fn from(s: &'static str) -> Self {
118 PowerlinkError::StorageError(s)
119 }
120}
121
122pub trait NetworkInterface {
127 fn send_frame(&mut self, frame: &[u8]) -> Result<(), PowerlinkError>;
131
132 fn receive_frame(&mut self, buffer: &mut [u8]) -> Result<usize, PowerlinkError>;
139
140 fn local_node_id(&self) -> u8;
142
143 fn local_mac_address(&self) -> [u8; 6];
145}
146
147pub trait ObjectDictionaryStorage {
151 fn load(&mut self) -> Result<BTreeMap<(u16, u8), ObjectValue>, PowerlinkError>;
154
155 fn save(&mut self, parameters: &BTreeMap<(u16, u8), ObjectValue>) -> Result<(), PowerlinkError>;
157
158 fn clear(&mut self) -> Result<(), PowerlinkError>;
160
161 fn restore_defaults_requested(&self) -> bool;
164
165 fn request_restore_defaults(&mut self) -> Result<(), PowerlinkError>;
168
169 fn clear_restore_defaults_flag(&mut self) -> Result<(), PowerlinkError>;
172}