Skip to main content

ace_uds/
error.rs

1// region: UdsError
2
3#[derive(Debug)]
4pub enum UdsError {
5    Transport(ace_core::DiagError),
6    NegativeResponse(u8),
7    Parse(heapless::String<64>),
8    ResponsePending,
9    Validation(ValidationError),
10}
11
12// endregion: UdsError
13
14// region: UdsError Display
15
16impl core::fmt::Display for UdsError {
17    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        match self {
19            UdsError::Transport(e) => write!(f, "transport error: {:?}", e),
20            UdsError::NegativeResponse(nrc) => {
21                write!(f, "negative response: 0x{:02X}", nrc)
22            }
23            UdsError::Parse(msg) => write!(f, "parse error: {}", msg),
24            UdsError::ResponsePending => write!(f, "response pending (NRC 0x78)"),
25            UdsError::Validation(e) => write!(f, "validation error: {}", e),
26        }
27    }
28}
29
30// endregion: UdsError Display
31
32// region: UdsError From Impls
33
34impl From<ace_core::DiagError> for UdsError {
35    fn from(e: ace_core::DiagError) -> Self {
36        UdsError::Transport(e)
37    }
38}
39
40impl From<ValidationError> for UdsError {
41    fn from(e: ValidationError) -> Self {
42        UdsError::Validation(e)
43    }
44}
45
46impl From<UdsError> for ace_core::DiagError {
47    fn from(e: UdsError) -> Self {
48        match e {
49            UdsError::Transport(diag) => diag,
50            UdsError::Parse(msg) => ace_core::DiagError::InvalidFrame(msg),
51            UdsError::NegativeResponse(nrc) => ace_core::DiagError::InvalidFrame(
52                heapless::String::try_from(format!("negative response: {nrc}").as_str())
53                    .unwrap_or_default(),
54            ),
55            UdsError::ResponsePending => ace_core::DiagError::InvalidFrame(
56                heapless::String::try_from("response pending").unwrap_or_default(),
57            ),
58            UdsError::Validation(e) => ace_core::DiagError::InvalidFrame(
59                heapless::String::try_from(format!("validation error: {e}").as_str())
60                    .unwrap_or_default(),
61            ),
62        }
63    }
64}
65
66// endregion: UdsError From Impls
67
68// region: ValidationError
69
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub enum ValidationError {
72    InvalidEventType(u8),
73    UnsupportedService(u8),
74    InvalidSubFunction(u8),
75    InvalidLength { expected: usize, actual: usize },
76    InvalidSessionType(u8),
77    InvalidAddressAndLengthFormat(u8),
78    InvalidDataIdentifier(u16),
79    InvalidSecurityAccessType(u8),
80    InvalidCommunicationTypeValue(u8),
81    InvalidCommunicationReserved(u8),
82    InvalidSubnet(u8),
83    ServiceSpecific(heapless::String<64>),
84    InvalidDtcGroup(u32),
85}
86
87// endregion: ValidationError
88
89// region: ValidationError Display
90
91impl core::fmt::Display for ValidationError {
92    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
93        match self {
94            ValidationError::UnsupportedService(sid) => {
95                write!(f, "invalid service identifier: {:?}", sid)
96            }
97            ValidationError::InvalidSubFunction(sf) => {
98                write!(f, "invalid sub-function: 0x{:02X}", sf)
99            }
100            ValidationError::InvalidLength { expected, actual } => {
101                write!(f, "invalid length: expected {}, got {}", expected, actual)
102            }
103            ValidationError::InvalidSessionType(st) => {
104                write!(f, "invalid session type: 0x{:02X}", st)
105            }
106            ValidationError::InvalidAddressAndLengthFormat(byte) => {
107                write!(
108                    f,
109                    "invalid addressAndLengthFormatIdentifier: 0x{:02X}",
110                    byte
111                )
112            }
113            ValidationError::InvalidDataIdentifier(did) => {
114                write!(f, "invalid data identifier: 0x{:04X}", did)
115            }
116            ValidationError::ServiceSpecific(msg) => {
117                write!(f, "service validation error: {}", msg)
118            }
119            ValidationError::InvalidSecurityAccessType(byte) => {
120                write!(f, "invalid securityAccessType: 0x{:02X}", byte)
121            }
122            ValidationError::InvalidCommunicationReserved(byte) => {
123                write!(f, "invalid communicationReserved: 0x{:02X}", byte)
124            }
125            ValidationError::InvalidSubnet(byte) => {
126                write!(f, "invalid subnet: 0x{:02X}", byte)
127            }
128            ValidationError::InvalidCommunicationTypeValue(byte) => {
129                write!(f, "invalid communicationTypeValue: 0x{:02X}", byte)
130            }
131            ValidationError::InvalidEventType(byte) => {
132                write!(f, "invalid eventType: 0x{:02X}", byte)
133            }
134            ValidationError::InvalidDtcGroup(byte) => {
135                write!(f, "invalid dtcGroup: 0x{:02X}", byte)
136            }
137        }
138    }
139}
140
141// endregion: ValidationError Display