Skip to main content

acex_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    /// 0x10 - general reject
13    fn general_reject() -> Self;
14
15    /// 0x11 - serviceNotSupported
16    fn service_not_supported() -> Self;
17    /// 0x12 - subFunctionNotSupported
18    fn sub_function_not_supported() -> Self;
19    /// 0x13 - incorrectMessageLengthOrInvalidFormat
20    fn incorrect_message_length_or_invalid_format() -> Self;
21
22    /// 0x14 - response too long
23    fn response_too_long() -> Self;
24
25    /// 0x21 - busy repeat request
26    fn busy_repeat_request() -> Self;
27
28    /// 0x22 - conditionsNotCorrect
29    fn conditions_not_correct() -> Self;
30    /// 0x24 - requestSequenceError
31    fn request_sequence_error() -> Self;
32
33    /// 0x25 - no response from subnet component
34    fn no_response_from_subnet_component() -> Self;
35
36    /// 0x26 - failure prevents execution of requested action
37    fn failure_prevents_execution_of_requested_action() -> Self;
38
39    /// 0x31 - requestOutOfRange
40    fn request_out_of_range() -> Self;
41    /// 0x33 - securityAccessDenied
42    fn security_access_denied() -> Self;
43    /// 0x35 - invalidKey
44    fn invalid_key() -> Self;
45    /// 0x36 - exceededNumberOfAttempts
46    fn exceeded_number_of_attempts() -> Self;
47    /// 0x37 - requiredTimeDelayNotExpired
48    fn required_time_delay_not_expired() -> Self;
49    /// 0x70 - uploadDownloadNotAccepted
50    fn upload_download_not_accepted() -> Self;
51    /// 0x71 - transferDataSuspended
52    fn transfer_data_suspended() -> Self;
53    /// 0x72 - generalProgrammingFailure
54    fn general_programming_failure() -> Self;
55    /// 0x73 - wrongBlockSequenceCounter
56    fn wrong_block_sequence_counter() -> Self;
57    /// 0x78 - requestCorrectlyReceivedResponsePending
58    fn response_pending() -> Self;
59    /// 0x7E - subFunctionNotSupportedInActiveSession
60    fn sub_function_not_supported_in_active_session() -> Self;
61    /// 0x7F - serviceNotSupportedInActiveSession
62    fn service_not_supported_in_active_session() -> Self;
63
64    /// 0x81 - rpmTooHigh
65    fn rpm_too_high() -> Self;
66
67    /// 0x82 - rpmTooLow
68    fn rpm_too_low() -> Self;
69
70    /// 0x83 - engineIsRunning
71    fn engine_is_running() -> Self;
72
73    /// 0x84 - engineIsNotRunning
74    fn engine_is_not_running() -> Self;
75
76    /// 0x85 - engineRunTimeTooLow
77    fn engine_run_time_too_low() -> Self;
78
79    /// 0x86 - temperatureTooHigh
80    fn temperature_too_high() -> Self;
81
82    /// 0x87 - temperatureTooLow
83    fn temperature_too_low() -> Self;
84
85    /// 0x88 - vehicleSpeedTooHigh
86    fn vehicle_speed_too_high() -> Self;
87
88    /// 0x89 - vehicleSpeedTooLow
89    fn vehicle_speed_too_low() -> Self;
90
91    /// 0x8A - throttlePedalTooHigh
92    fn throttle_pedal_too_high() -> Self;
93
94    /// 0x8B - throttlePedalTooLow
95    fn throttle_pedal_too_low() -> Self;
96
97    /// 0x8C - transmissionRangeNotInNeutral
98    fn transmission_range_not_in_neutral() -> Self;
99
100    /// 0x8D - transmissionRangeNotInGear
101    fn transmission_range_not_in_gear() -> Self;
102
103    /// 0x8F - brakeSwitchesNotClosed
104    fn brake_switches_not_closed() -> Self;
105
106    /// 0x90 - shifterLevelNotInPark
107    fn shifter_level_not_in_park() -> Self;
108
109    /// 0x91 - torqueConverterClutchLocked
110    fn torque_converter_clutch_locked() -> Self;
111
112    /// 0x92 - voltageTooHigh
113    fn voltage_too_high() -> Self;
114
115    /// 0x93 - voltageTooLow
116    fn voltage_too_low() -> Self;
117    // endregion: Mandatory constructors
118}
119
120// endregion: NRC Error
121
122// region: Builtin NRC
123
124/// A built-in NRC type covering all codes the server may emit.
125///
126/// Applications that do not need a custom error type may use this directly as `type Error =
127/// BuiltinNrc` in the `ServerHandler` impl.
128#[derive(Debug, Clone, PartialEq, Eq)]
129#[cfg_attr(feature = "defmt", derive(defmt::Format))]
130#[repr(u8)]
131pub enum BuiltinNrc {
132    GeneralReject = 0x10,
133    ServiceNotSupported = 0x11,
134    SubFunctionNotSupported = 0x12,
135    IncorrectMessageLengthOrInvalidFormat = 0x13,
136    ResponseTooLong = 0x14,
137    BusyRepeatRequest = 0x21,
138    ConditionsNotCorrect = 0x22,
139    RequestSequenceError = 0x24,
140    NoResponseFromSubnetComponent = 0x25,
141    FailurePreventsExecutionOfRequestedAction = 0x26,
142    RequestOutOfRange = 0x31,
143    SecurityAccessDenied = 0x33,
144    InvalidKey = 0x35,
145    ExceededNumberOfAttempts = 0x36,
146    RequiredTimeDelayNotExpired = 0x37,
147    UploadDownloadNotAccepted = 0x70,
148    TransferDataSuspended = 0x71,
149    GeneralProgrammingFailure = 0x72,
150    WrongBlockSequenceCounter = 0x73,
151    ResponsePending = 0x78,
152    SubFunctionNotSupportedInActiveSession = 0x7E,
153    ServiceNotSupportedInActiveSession = 0x7F,
154    RpmTooHigh = 0x81,
155    RpmTooLow = 0x82,
156    EngineIsRunning = 0x83,
157    EngineIsNotRunning = 0x84,
158    EngineRunTimeTooLow = 0x85,
159    TemperatureTooHigh = 0x86,
160    TemperatureTooLow = 0x87,
161    VehicleSpeedTooHigh = 0x88,
162    VehicleSpeedToLow = 0x89,
163    ThrottlePedalTooHigh = 0x8A,
164    ThrottlePedalTooLow = 0x8B,
165    TransmissionRangeNotInNeutral = 0x8C,
166    TransmissionRangeNotInGear = 0x8D,
167    BrakeSwitchesNotClosed = 0x8F,
168    ShifterLevelNotInPark = 0x90,
169    TorqueConverterClutchLocked = 0x91,
170    VoltageTooHigh = 0x92,
171    VoltageTooLow = 0x93
172}
173
174impl From<BuiltinNrc> for u8 {
175    fn from(n: BuiltinNrc) -> u8 {
176        n as u8
177    }
178}
179
180impl NrcError for BuiltinNrc {
181    fn general_reject() -> Self {
182        Self::GeneralReject
183    }
184
185    fn service_not_supported() -> Self {
186        Self::ServiceNotSupported
187    }
188    fn sub_function_not_supported() -> Self {
189        Self::SubFunctionNotSupported
190    }
191    fn incorrect_message_length_or_invalid_format() -> Self {
192        Self::IncorrectMessageLengthOrInvalidFormat
193    }
194
195    fn response_too_long() -> Self {
196        Self::ResponseTooLong
197    }
198
199    fn busy_repeat_request() -> Self {
200        Self::BusyRepeatRequest
201    }
202
203    fn conditions_not_correct() -> Self {
204        Self::ConditionsNotCorrect
205    }
206    fn request_sequence_error() -> Self {
207        Self::RequestSequenceError
208    }
209
210    fn no_response_from_subnet_component() -> Self { Self::NoResponseFromSubnetComponent }
211
212    fn failure_prevents_execution_of_requested_action() -> Self {
213        Self::FailurePreventsExecutionOfRequestedAction
214    }
215
216    fn request_out_of_range() -> Self {
217        Self::RequestOutOfRange
218    }
219    fn security_access_denied() -> Self {
220        Self::SecurityAccessDenied
221    }
222    fn invalid_key() -> Self {
223        Self::InvalidKey
224    }
225    fn exceeded_number_of_attempts() -> Self {
226        Self::ExceededNumberOfAttempts
227    }
228    fn required_time_delay_not_expired() -> Self {
229        Self::RequiredTimeDelayNotExpired
230    }
231    fn upload_download_not_accepted() -> Self {
232        Self::UploadDownloadNotAccepted
233    }
234    fn transfer_data_suspended() -> Self {
235        Self::TransferDataSuspended
236    }
237    fn general_programming_failure() -> Self {
238        Self::GeneralProgrammingFailure
239    }
240    fn wrong_block_sequence_counter() -> Self {
241        Self::WrongBlockSequenceCounter
242    }
243    fn response_pending() -> Self {
244        Self::ResponsePending
245    }
246    fn sub_function_not_supported_in_active_session() -> Self {
247        Self::SubFunctionNotSupportedInActiveSession
248    }
249    fn service_not_supported_in_active_session() -> Self {
250        Self::ServiceNotSupportedInActiveSession
251    }
252
253    fn rpm_too_high() -> Self {
254        Self::RpmTooHigh
255    }
256
257    fn rpm_too_low() -> Self {
258        Self::RpmTooLow
259    }
260
261    fn engine_is_running() -> Self {
262        Self::EngineIsRunning
263    }
264
265    fn engine_is_not_running() -> Self {
266        Self::EngineIsNotRunning
267    }
268
269    fn engine_run_time_too_low() -> Self {
270        Self::EngineRunTimeTooLow
271    }
272
273    fn temperature_too_high() -> Self {
274        Self::TemperatureTooHigh
275    }
276
277    fn temperature_too_low() -> Self {
278        Self::TemperatureTooLow
279    }
280
281    fn vehicle_speed_too_high() -> Self {
282        Self::VehicleSpeedTooHigh
283    }
284
285    fn vehicle_speed_too_low() -> Self {
286        Self::VehicleSpeedToLow
287    }
288
289    fn throttle_pedal_too_high() -> Self {
290        Self::ThrottlePedalTooHigh
291    }
292
293    fn throttle_pedal_too_low() -> Self {
294        Self::ThrottlePedalTooLow
295    }
296
297    fn transmission_range_not_in_neutral() -> Self {
298        Self::TransmissionRangeNotInNeutral
299    }
300
301    fn transmission_range_not_in_gear() -> Self {
302        Self::TransmissionRangeNotInGear
303    }
304
305    fn brake_switches_not_closed() -> Self {
306        Self::BrakeSwitchesNotClosed
307    }
308
309    fn shifter_level_not_in_park() -> Self {
310        Self::ShifterLevelNotInPark
311    }
312
313    fn torque_converter_clutch_locked() -> Self {
314        Self::TorqueConverterClutchLocked
315    }
316
317    fn voltage_too_high() -> Self {
318        Self::VoltageTooHigh
319    }
320
321    fn voltage_too_low() -> Self {
322        Self::VoltageTooLow
323    }
324}
325
326// endregion: Builtin NRC