Skip to main content

ace_server/
nrc.rs

1// region: NRC Error
2
3/// A trait for application error types that can be converted into UDS Negative Response Codes
4/// (NRCs).
5///
6/// The server state machine uses this to construct NRC responses without knowing the application's
7/// error type internals. The application defines the mapping via `Into<u8>`.
8
9pub trait NrcError: Into<u8> + core::fmt::Debug {
10    // region: Mandatory constructors - one per NRC the server may emit
11
12    /// 0x11 - serviceNotSupported
13    fn service_not_supported() -> Self;
14    /// 0x12 - subFunctionNotSupported
15    fn sub_function_not_supported() -> Self;
16    /// 0x13 - incorrectMessageLengthOrInvalidFormat
17    fn incorrect_message_length_or_invalid_format() -> Self;
18    /// 0x22 - conditionsNotCorrect
19    fn conditions_not_correct() -> Self;
20    /// 0x24 - requestSequenceError
21    fn request_sequence_error() -> Self;
22    /// 0x31 - requestOutOfRange
23    fn request_out_of_range() -> Self;
24    /// 0x33 - securityAccessDenied
25    fn security_access_denied() -> Self;
26    /// 0x35 - invalidKey
27    fn invalid_key() -> Self;
28    /// 0x36 - exceededNumberOfAttempts
29    fn exceeded_number_of_attempts() -> Self;
30    /// 0x37 - requiredTimeDelayNotExpired
31    fn required_time_delay_not_expired() -> Self;
32    /// 0x70 - uploadDownloadNotAccepted
33    fn upload_download_not_accepted() -> Self;
34    /// 0x71 - transferDataSuspended
35    fn transfer_data_suspended() -> Self;
36    /// 0x72 - generalProgrammingFailure
37    fn general_programming_failure() -> Self;
38    /// 0x73 - wrongBlockSequenceCounter
39    fn wrong_block_sequence_counter() -> Self;
40    /// 0x78 - requestCorrectlyReceivedResponsePending
41    fn response_pending() -> Self;
42    /// 0x7E - subFunctionNotSupportedInActiveSession
43    fn sub_function_not_supported_in_active_session() -> Self;
44    /// 0x7F - serviceNotSupportedInActiveSession
45    fn service_not_supported_in_active_session() -> Self;
46
47    // endregion: Mandatory constructors
48}
49
50// endregion: NRC Error
51
52// region: Builtin NRC
53
54/// A built-in NRC type covering all codes the server may emit.
55///
56/// Applications that do not need a custom error type may use this directly as `type Error =
57/// BuiltinNrc` in the `ServerHandler` impl.
58#[derive(Debug, Clone, PartialEq, Eq)]
59#[repr(u8)]
60pub enum BuiltinNrc {
61    ServiceNotSupported = 0x11,
62    SubFunctionNotSupported = 0x12,
63    IncorrectMessageLengthOrInvalidFormat = 0x13,
64    ConditionsNotCorrect = 0x22,
65    RequestSequenceError = 0x24,
66    RequestOutOfRange = 0x31,
67    SecurityAccessDenied = 0x33,
68    InvalidKey = 0x35,
69    ExceededNumberOfAttempts = 0x36,
70    RequiredTimeDelayNotExpired = 0x37,
71    UploadDownloadNotAccepted = 0x70,
72    TransferDataSuspended = 0x71,
73    GeneralProgrammingFailure = 0x72,
74    WrongBlockSequenceCounter = 0x73,
75    ResponsePending = 0x78,
76    SubFunctionNotSupportedInActiveSession = 0x7E,
77    ServiceNotSupportedInActiveSession = 0x7F,
78}
79
80impl From<BuiltinNrc> for u8 {
81    fn from(n: BuiltinNrc) -> u8 {
82        n as u8
83    }
84}
85
86impl NrcError for BuiltinNrc {
87    fn service_not_supported() -> Self {
88        Self::ServiceNotSupported
89    }
90    fn sub_function_not_supported() -> Self {
91        Self::SubFunctionNotSupported
92    }
93    fn incorrect_message_length_or_invalid_format() -> Self {
94        Self::IncorrectMessageLengthOrInvalidFormat
95    }
96    fn conditions_not_correct() -> Self {
97        Self::ConditionsNotCorrect
98    }
99    fn request_sequence_error() -> Self {
100        Self::RequestSequenceError
101    }
102    fn request_out_of_range() -> Self {
103        Self::RequestOutOfRange
104    }
105    fn security_access_denied() -> Self {
106        Self::SecurityAccessDenied
107    }
108    fn invalid_key() -> Self {
109        Self::InvalidKey
110    }
111    fn exceeded_number_of_attempts() -> Self {
112        Self::ExceededNumberOfAttempts
113    }
114    fn required_time_delay_not_expired() -> Self {
115        Self::RequiredTimeDelayNotExpired
116    }
117    fn upload_download_not_accepted() -> Self {
118        Self::UploadDownloadNotAccepted
119    }
120    fn transfer_data_suspended() -> Self {
121        Self::TransferDataSuspended
122    }
123    fn general_programming_failure() -> Self {
124        Self::GeneralProgrammingFailure
125    }
126    fn wrong_block_sequence_counter() -> Self {
127        Self::WrongBlockSequenceCounter
128    }
129    fn response_pending() -> Self {
130        Self::ResponsePending
131    }
132    fn sub_function_not_supported_in_active_session() -> Self {
133        Self::SubFunctionNotSupportedInActiveSession
134    }
135    fn service_not_supported_in_active_session() -> Self {
136        Self::ServiceNotSupportedInActiveSession
137    }
138}
139
140// endregion: Builtin NRC