From 61dba62f5d1d8a3c2358fcf031f189775a6bb916 Mon Sep 17 00:00:00 2001 From: Robert Sammelson Date: Wed, 17 May 2023 21:09:42 -0400 Subject: [PATCH] Reorganize commands module --- .../implementation.rs} | 76 +------------------ src/commands/mod.rs | 42 ++++++++++ src/commands/types.rs | 43 +++++++++++ 3 files changed, 87 insertions(+), 74 deletions(-) rename src/{commands.rs => commands/implementation.rs} (58%) create mode 100644 src/commands/mod.rs create mode 100644 src/commands/types.rs diff --git a/src/commands.rs b/src/commands/implementation.rs similarity index 58% rename from src/commands.rs rename to src/commands/implementation.rs index 473ccbc..2543ecf 100644 --- a/src/commands.rs +++ b/src/commands/implementation.rs @@ -1,31 +1,6 @@ -//! High level OBD-II interface +use crate::{commands::Obd2DataRetrieval, Error, Obd2Device, Result}; -use std::fmt; - -use crate::{Error, Obd2Device, Result}; - -/// Trait for devices that can retrieve data over OBD-II -/// -/// Automatically impelemted for implementors of [Obd2Device]. -pub trait Obd2DataRetrieval: private::Sealed { - /// Retreive the VIN (vehicle identification number) - /// - /// This should match the number printed on the vehicle, and is a good command for checking - /// that the OBD-II interface is working correctly. - fn get_vin(&mut self) -> Result; - - /// Get DTC (diagnostic trouble code) metadata for each ECU - fn get_dtc_info(&mut self) -> Result>; - - /// Get DTCs for each ECU - fn get_dtcs(&mut self) -> Result>>; - - /// Get the RPM in increments of 0.25 - fn get_rpm(&mut self) -> Result; - - /// Get the speed in km/h - fn get_speed(&mut self) -> Result; -} +use super::{Dtc, DtcsInfo}; impl Obd2DataRetrieval for T { fn get_vin(&mut self) -> Result { @@ -106,50 +81,3 @@ impl Obd2DataRetrieval for T { Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0]) } } - -/// DTC (diagnostic trouble code) metadata -#[derive(Debug)] -#[non_exhaustive] -pub struct DtcsInfo { - /// Whether the "check engine" light is illuminated - pub malfunction_indicator_light: bool, - - /// Number of DTCs for this ECU - pub dtc_count: u8, - - /// Bit field showing availability of seven common tests; the upper bit is currently unused. - pub common_test_availability: u8, - - /// Whether the engine is Diesel - pub is_compression_engine: bool, - - /// Bit field showing availability of sixteen engine-specific tests. What the tests are is - /// based on the value of `is_compression_engine`. - pub specific_test_availability: u16, -} - -/// An individual trouble code from an ECU -#[derive(Debug)] -pub enum Dtc { - Powertrain(u16), - Chassis(u16), - Body(u16), - Network(u16), -} - -impl fmt::Display for Dtc { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let (c, n) = match self { - Self::Powertrain(n) => ('P', n), - Self::Chassis(n) => ('C', n), - Self::Body(n) => ('B', n), - Self::Network(n) => ('U', n), - }; - f.write_fmt(format_args!("{}{:03X}", c, n)) - } -} - -mod private { - pub trait Sealed {} - impl Sealed for T {} -} diff --git a/src/commands/mod.rs b/src/commands/mod.rs new file mode 100644 index 0000000..8de799c --- /dev/null +++ b/src/commands/mod.rs @@ -0,0 +1,42 @@ +//! High level OBD-II interface + +mod implementation; + +mod types; +pub use types::{Dtc, DtcsInfo}; + +use crate::Result; + +/// Trait for devices that can retrieve data over OBD-II +/// +/// Automatically implemented for implementors of [Obd2Device](crate::Obd2Device). +pub trait Obd2DataRetrieval: private::Sealed { + /// Check which getters are supported by the current vehicle + // fn get_support() -> Obd2FunctionSupport; + + /// Retreive the VIN (vehicle identification number) + /// + /// This should match the number printed on the vehicle, and is a good command for checking + /// that the OBD-II interface is working correctly. + fn get_vin(&mut self) -> Result; + + /// Get DTC (diagnostic trouble code) metadata for each ECU + fn get_dtc_info(&mut self) -> Result>; + + /// Get DTCs for each ECU + fn get_dtcs(&mut self) -> Result>>; + + /// Get the calculated engine + // fn get_engine_load(&mut self) -> Result; + + /// Get the RPM in increments of 0.25 + fn get_rpm(&mut self) -> Result; + + /// Get the speed in km/h + fn get_speed(&mut self) -> Result; +} + +mod private { + pub trait Sealed {} + impl Sealed for T {} +} diff --git a/src/commands/types.rs b/src/commands/types.rs new file mode 100644 index 0000000..011cdb4 --- /dev/null +++ b/src/commands/types.rs @@ -0,0 +1,43 @@ +use std::fmt; + +/// DTC (diagnostic trouble code) metadata +#[derive(Debug)] +#[non_exhaustive] +pub struct DtcsInfo { + /// Whether the "check engine" light is illuminated + pub malfunction_indicator_light: bool, + + /// Number of DTCs for this ECU + pub dtc_count: u8, + + /// Bit field showing availability of seven common tests; the upper bit is currently unused. + pub common_test_availability: u8, + + /// Whether the engine is Diesel + pub is_compression_engine: bool, + + /// Bit field showing availability of sixteen engine-specific tests. What the tests are is + /// based on the value of `is_compression_engine`. + pub specific_test_availability: u16, +} + +/// An individual trouble code from an ECU +#[derive(Debug)] +pub enum Dtc { + Powertrain(u16), + Chassis(u16), + Body(u16), + Network(u16), +} + +impl fmt::Display for Dtc { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let (c, n) = match self { + Self::Powertrain(n) => ('P', n), + Self::Chassis(n) => ('C', n), + Self::Body(n) => ('B', n), + Self::Network(n) => ('U', n), + }; + f.write_fmt(format_args!("{}{:03X}", c, n)) + } +}