From 2a43bf94bdf4b7273d17435305095767cff3ca45 Mon Sep 17 00:00:00 2001 From: Robert Sammelson Date: Sun, 14 May 2023 23:07:54 -0400 Subject: [PATCH] Switch to a library and move main to an example --- .gitignore | 1 + Cargo.lock | 378 -------------------------------- Cargo.toml | 7 +- {src => examples/basic}/main.rs | 5 +- src/{obd2 => }/accessors.rs | 10 + src/{obd2 => }/device/elm327.rs | 1 - src/{obd2 => }/device/mod.rs | 0 src/{obd2 => }/interface.rs | 2 +- src/{obd2/mod.rs => lib.rs} | 6 +- 9 files changed, 21 insertions(+), 389 deletions(-) delete mode 100644 Cargo.lock rename {src => examples/basic}/main.rs (86%) rename src/{obd2 => }/accessors.rs (92%) rename src/{obd2 => }/device/elm327.rs (99%) rename src/{obd2 => }/device/mod.rs (100%) rename src/{obd2 => }/interface.rs (98%) rename src/{obd2/mod.rs => lib.rs} (65%) diff --git a/.gitignore b/.gitignore index ea8c4bf..96ef6c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +Cargo.lock diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 8302691..0000000 --- a/Cargo.lock +++ /dev/null @@ -1,378 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "aho-corasick" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" -dependencies = [ - "memchr", -] - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "cc" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "env_logger" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - -[[package]] -name = "errno" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", - "libc", -] - -[[package]] -name = "ftdi" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f9c8c625c6b8634ce70cd8cbb2ab8a103e4c2b4185c86d9954d2e16b1ef7c4a" -dependencies = [ - "ftdi-mpsse", - "libftdi1-sys", - "thiserror", -] - -[[package]] -name = "ftdi-mpsse" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7cfcda69930a8d2fdcdd7ffb9234fe4c79a8c73934ed4904327d77bfb5078a" -dependencies = [ - "static_assertions", -] - -[[package]] -name = "hermit-abi" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" - -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - -[[package]] -name = "io-lifetimes" -version = "1.0.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys", -] - -[[package]] -name = "is-terminal" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" -dependencies = [ - "hermit-abi", - "io-lifetimes", - "rustix", - "windows-sys", -] - -[[package]] -name = "libc" -version = "0.2.144" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" - -[[package]] -name = "libftdi1-sys" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ff6928872c7d13bec3c8a60c4c92f41f6252f3369b7552a5b4f9c90c8ba2338" -dependencies = [ - "cfg-if", - "libc", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "linux-raw-sys" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f" - -[[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "memchr" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "obd" -version = "0.0.0" -dependencies = [ - "env_logger", - "ftdi", - "log", - "thiserror", -] - -[[package]] -name = "pkg-config" -version = "0.3.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" - -[[package]] -name = "proc-macro2" -version = "1.0.56" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "regex" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" - -[[package]] -name = "rustix" -version = "0.37.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" -dependencies = [ - "bitflags", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "static_assertions" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" - -[[package]] -name = "syn" -version = "2.0.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "termcolor" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "thiserror" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "unicode-ident" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" - -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" - -[[package]] -name = "windows_i686_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" - -[[package]] -name = "windows_i686_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" diff --git a/Cargo.toml b/Cargo.toml index b655587..d275fc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,9 @@ [package] -name = "obd" -version = "0.0.0" +name = "obd2" +description = "Utility for reading data from a vehicle over OBD-II" +license = "MIT OR Apache-2.0" +repository = "https://github.com/rsammelson/obd2" +version = "0.1.0" edition = "2021" [dependencies] diff --git a/src/main.rs b/examples/basic/main.rs similarity index 86% rename from src/main.rs rename to examples/basic/main.rs index b1c4fa3..786abd5 100644 --- a/src/main.rs +++ b/examples/basic/main.rs @@ -1,13 +1,10 @@ -#![forbid(unsafe_code)] - -mod obd2; use obd2::Obd2Device; use std::time; fn main() { env_logger::init(); - let mut device: obd2::Obd2 = obd2::Obd2::default(); + let mut device: obd2::Obd2 = obd2::Obd2::default(); println!("VIN: {:?}", device.get_vin()); println!("DTC Info: {:#?}", device.get_dtc_info()); diff --git a/src/obd2/accessors.rs b/src/accessors.rs similarity index 92% rename from src/obd2/accessors.rs rename to src/accessors.rs index 006779e..b5da403 100644 --- a/src/obd2/accessors.rs +++ b/src/accessors.rs @@ -3,7 +3,11 @@ use core::fmt; pub type Result = std::result::Result; pub trait Obd2Device { + /// Send an OBD command with mode and PID, and get a list of responses (one for each ECU that + /// responds) fn obd_command(&mut self, mode: u8, pid: u8) -> Result>>; + + /// Like [obd_command](Self::obd_command), but for commands that do not require a PID fn obd_mode_command(&mut self, mode: u8) -> Result>>; fn obd_command_len( @@ -33,12 +37,15 @@ pub trait Obd2Device { .map_err(|_| Error::IncorrectResponseLength("count", RESPONSE_COUNT, count)) } + /// Retreive the VIN (vehicle identification number), this should match the one printed on the + /// vehicle fn get_vin(&mut self) -> Result { let mut result = self.obd_command(0x09, 0x02)?.pop().unwrap(); result.remove(0); // do not know what this byte is Ok(String::from_utf8(result)?) } + /// Get DTC (diagnostic trouble code) metadata for each ECU fn get_dtc_info(&mut self) -> Result> { let result = self.obd_command(0x01, 0x01)?; @@ -65,6 +72,7 @@ pub trait Obd2Device { .collect() } + /// Get DTCs for each ECU fn get_dtcs(&mut self) -> Result>> { let result = self.obd_mode_command(0x03)?; result @@ -102,11 +110,13 @@ pub trait Obd2Device { .collect::>>>() } + /// Get the RPM in increments of 0.25 fn get_rpm(&mut self) -> Result { let result = self.obd_command_cnt_len::<1, 2>(0x01, 0x0C)?[0]; Ok(f32::from(u16::from_be_bytes(result)) / 4.0) } + /// Get the speed in km/h fn get_speed(&mut self) -> Result { Ok(self.obd_command_cnt_len::<1, 1>(0x01, 0x0C)?[0][0]) } diff --git a/src/obd2/device/elm327.rs b/src/device/elm327.rs similarity index 99% rename from src/obd2/device/elm327.rs rename to src/device/elm327.rs index 3e26e90..ad55635 100644 --- a/src/obd2/device/elm327.rs +++ b/src/device/elm327.rs @@ -39,7 +39,6 @@ impl Obd2BaseDevice for Elm327 { fn send_serial_cmd(&mut self, data: &str) -> Result<()> { self.device.write_all(data.as_bytes())?; self.device.write_all(b"\r\n")?; - // thread::sleep(time::Duration::from_millis(200)); let line = self.get_line()?; if line.as_ref().is_some_and(|v| v == data.as_bytes()) { Ok(()) diff --git a/src/obd2/device/mod.rs b/src/device/mod.rs similarity index 100% rename from src/obd2/device/mod.rs rename to src/device/mod.rs diff --git a/src/obd2/interface.rs b/src/interface.rs similarity index 98% rename from src/obd2/interface.rs rename to src/interface.rs index 61bcb04..6b1c2ed 100644 --- a/src/obd2/interface.rs +++ b/src/interface.rs @@ -1,6 +1,6 @@ use log::{debug, trace}; -use super::{Error, Obd2BaseDevice, Obd2Device, Result}; +use super::{device::Obd2BaseDevice, Error, Obd2Device, Result}; #[derive(Default)] pub struct Obd2 { diff --git a/src/obd2/mod.rs b/src/lib.rs similarity index 65% rename from src/obd2/mod.rs rename to src/lib.rs index 95af264..bba400a 100644 --- a/src/obd2/mod.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ +#![forbid(unsafe_code)] + mod accessors; use accessors::Result; pub use accessors::{Error, Obd2Device}; -mod device; -pub use device::Elm327; -use device::Obd2BaseDevice; +pub mod device; mod interface; pub use interface::Obd2;