#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_numeric_casts,
//unstable_features,
//unused_imports,
unused_import_braces,
unused_qualifications,
clippy::uninlined_format_args
)]
use std::sync::Arc;
use channel::ChannelError;
use hardware::HardwareError;
pub mod channel;
pub mod dtc;
pub mod dynamic_diag;
pub mod hardware;
pub mod kwp2000;
pub mod obd2;
pub mod uds;
pub use automotive_diag::ByteWrapper::*;
pub type DiagServerResult<T> = Result<T, DiagError>;
#[derive(Clone, Debug, thiserror::Error)]
pub enum DiagError {
#[error("Diagnostic server does not support the request")]
NotSupported,
#[error("ECU Negative response. Error 0x{:02X?}, definition: {:?}", code, def)]
ECUError {
code: u8,
def: Option<String>,
},
#[error("ECU did not respond to the request")]
EmptyResponse,
#[error("ECU response is out of order")]
WrongMessage,
#[error("Diagnostic server was terminated before the request")]
ServerNotRunning,
#[error("ECU response size was not the correct length")]
InvalidResponseLength,
#[error("Diagnostic function parameter invalid")]
ParameterInvalid,
#[error(transparent)]
ChannelError(#[from] ChannelError),
#[error(transparent)]
HardwareError(#[from] Arc<HardwareError>),
#[error("Diagnostic server feature is unimplemented: '{0}'")]
NotImplemented(String),
#[error("Requested Ident 0x{:04X?}, but received ident 0x{:04X?}", want, received)]
MismatchedIdentResponse {
want: u16,
received: u16
}
}
pub fn bcd_decode(input: u8) -> String {
format!("{}{}", (input & 0xF0) >> 4, input & 0x0F)
}
pub fn bcd_decode_slice(input: &[u8], sep: Option<&str>) -> String {
let mut res = String::new();
for (pos, x) in input.iter().enumerate() {
res.push_str(bcd_decode(*x).as_str());
if let Some(separator) = sep {
if pos != input.len() - 1 {
res.push_str(separator)
}
}
}
res
}