spodes_rs/service/
error.rs1use super::{tag, ServiceError};
11
12pub mod state_error {
14 pub const SERVICE_NOT_ALLOWED: u8 = 1;
16 pub const SERVICE_UNKNOWN: u8 = 2;
18}
19
20pub mod service_error {
22 pub const OPERATION_NOT_POSSIBLE: u8 = 1;
24 pub const SERVICE_NOT_SUPPORTED: u8 = 2;
26 pub const OTHER_REASON: u8 = 3;
28 pub const PDU_TOO_LONG: u8 = 4;
30 pub const DECIPHERING_ERROR: u8 = 5;
32 pub const INVOCATION_COUNTER_ERROR: u8 = 6;
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct ExceptionResponse {
42 pub state_error: u8,
44 pub service_error: u8,
46}
47
48impl ExceptionResponse {
49 pub fn encode(&self) -> Vec<u8> {
51 vec![tag::EXCEPTION_RESPONSE, self.state_error, self.service_error]
52 }
53
54 pub fn decode(bytes: &[u8]) -> Result<ExceptionResponse, ServiceError> {
56 if bytes.first() != Some(&tag::EXCEPTION_RESPONSE) {
57 return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
58 }
59 let state_error = *bytes.get(1).ok_or(ServiceError::Truncated)?;
60 let service_error = *bytes.get(2).ok_or(ServiceError::Truncated)?;
61 Ok(ExceptionResponse { state_error, service_error })
62 }
63}
64
65pub mod service {
67 pub const INITIATE_ERROR: u8 = 1;
69 pub const GET_STATUS: u8 = 2;
71 pub const READ: u8 = 5;
73 pub const WRITE: u8 = 6;
75}
76
77pub mod category {
79 pub const APPLICATION_REFERENCE: u8 = 1;
81 pub const HARDWARE_RESOURCE: u8 = 2;
83 pub const INITIATE: u8 = 6;
85 pub const ACCESS: u8 = 9;
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
92pub struct ConfirmedServiceError {
93 pub service: u8,
95 pub category: u8,
97 pub value: u8,
99}
100
101impl ConfirmedServiceError {
102 pub fn encode(&self) -> Vec<u8> {
104 vec![tag::CONFIRMED_SERVICE_ERROR, self.service, self.category, self.value]
105 }
106
107 pub fn decode(bytes: &[u8]) -> Result<ConfirmedServiceError, ServiceError> {
109 if bytes.first() != Some(&tag::CONFIRMED_SERVICE_ERROR) {
110 return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
111 }
112 Ok(ConfirmedServiceError {
113 service: *bytes.get(1).ok_or(ServiceError::Truncated)?,
114 category: *bytes.get(2).ok_or(ServiceError::Truncated)?,
115 value: *bytes.get(3).ok_or(ServiceError::Truncated)?,
116 })
117 }
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn exception_response_round_trips() {
126 let ex = ExceptionResponse {
128 state_error: state_error::SERVICE_NOT_ALLOWED,
129 service_error: service_error::OPERATION_NOT_POSSIBLE,
130 };
131 assert_eq!(ex.encode(), vec![0xD8, 0x01, 0x01]);
132 assert_eq!(ExceptionResponse::decode(&ex.encode()).unwrap(), ex);
133 }
134
135 #[test]
136 fn confirmed_service_error_matches_reference_bytes() {
137 let e = ConfirmedServiceError { service: service::INITIATE_ERROR, category: category::INITIATE, value: 2 };
140 assert_eq!(e.encode(), vec![0x0E, 0x01, 0x06, 0x02]);
141 assert_eq!(ConfirmedServiceError::decode(&e.encode()).unwrap(), e);
142 }
143}