use super::{tag, ServiceError};
pub mod state_error {
pub const SERVICE_NOT_ALLOWED: u8 = 1;
pub const SERVICE_UNKNOWN: u8 = 2;
}
pub mod service_error {
pub const OPERATION_NOT_POSSIBLE: u8 = 1;
pub const SERVICE_NOT_SUPPORTED: u8 = 2;
pub const OTHER_REASON: u8 = 3;
pub const PDU_TOO_LONG: u8 = 4;
pub const DECIPHERING_ERROR: u8 = 5;
pub const INVOCATION_COUNTER_ERROR: u8 = 6;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExceptionResponse {
pub state_error: u8,
pub service_error: u8,
}
impl ExceptionResponse {
pub fn encode(&self) -> Vec<u8> {
vec![tag::EXCEPTION_RESPONSE, self.state_error, self.service_error]
}
pub fn decode(bytes: &[u8]) -> Result<ExceptionResponse, ServiceError> {
if bytes.first() != Some(&tag::EXCEPTION_RESPONSE) {
return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
}
let state_error = *bytes.get(1).ok_or(ServiceError::Truncated)?;
let service_error = *bytes.get(2).ok_or(ServiceError::Truncated)?;
Ok(ExceptionResponse { state_error, service_error })
}
}
pub mod service {
pub const INITIATE_ERROR: u8 = 1;
pub const GET_STATUS: u8 = 2;
pub const READ: u8 = 5;
pub const WRITE: u8 = 6;
}
pub mod category {
pub const APPLICATION_REFERENCE: u8 = 1;
pub const HARDWARE_RESOURCE: u8 = 2;
pub const INITIATE: u8 = 6;
pub const ACCESS: u8 = 9;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfirmedServiceError {
pub service: u8,
pub category: u8,
pub value: u8,
}
impl ConfirmedServiceError {
pub fn encode(&self) -> Vec<u8> {
vec![tag::CONFIRMED_SERVICE_ERROR, self.service, self.category, self.value]
}
pub fn decode(bytes: &[u8]) -> Result<ConfirmedServiceError, ServiceError> {
if bytes.first() != Some(&tag::CONFIRMED_SERVICE_ERROR) {
return Err(ServiceError::UnexpectedTag(*bytes.first().unwrap_or(&0)));
}
Ok(ConfirmedServiceError {
service: *bytes.get(1).ok_or(ServiceError::Truncated)?,
category: *bytes.get(2).ok_or(ServiceError::Truncated)?,
value: *bytes.get(3).ok_or(ServiceError::Truncated)?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exception_response_round_trips() {
let ex = ExceptionResponse {
state_error: state_error::SERVICE_NOT_ALLOWED,
service_error: service_error::OPERATION_NOT_POSSIBLE,
};
assert_eq!(ex.encode(), vec![0xD8, 0x01, 0x01]);
assert_eq!(ExceptionResponse::decode(&ex.encode()).unwrap(), ex);
}
#[test]
fn confirmed_service_error_matches_reference_bytes() {
let e = ConfirmedServiceError { service: service::INITIATE_ERROR, category: category::INITIATE, value: 2 };
assert_eq!(e.encode(), vec![0x0E, 0x01, 0x06, 0x02]);
assert_eq!(ConfirmedServiceError::decode(&e.encode()).unwrap(), e);
}
}