From 0bba6110564817ee6fcf8ce1bb6006bf192c1525 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Wed, 20 Mar 2024 19:55:33 +0100 Subject: [PATCH] refactor can stuff into own module --- README.md | 4 ++-- examples/can_printer.rs | 2 +- examples/isotp.rs | 2 +- examples/query_fw_versions.rs | 4 ++-- examples/uds.rs | 2 +- src/{ => can}/adapter.rs | 2 +- src/{ => can}/async_can.rs | 0 src/{can.rs => can/mod.rs} | 6 ++++++ src/isotp/mod.rs | 4 ++-- src/lib.rs | 6 ++---- src/panda/mod.rs | 2 +- src/socketcan/mod.rs | 2 +- src/uds/mod.rs | 2 +- tests/adapter_tests.rs | 2 +- tests/isotp_tests.rs | 2 +- tests/uds_tests.rs | 2 +- 16 files changed, 24 insertions(+), 20 deletions(-) rename src/{ => can}/adapter.rs (86%) rename src/{ => can}/async_can.rs (100%) rename src/{can.rs => can/mod.rs} (97%) diff --git a/README.md b/README.md index 89506f6..771b006 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Welcome to the `automotive` crate documentation. The purpose of this crate is to The following adapter opens the first available adapter on the system, and then receives all frames. ```rust -let adapter = automotive::adapter::get_adapter().unwrap(); +let adapter = automotive::can::get_adapter().unwrap(); let mut stream = adapter.recv(); while let Some(frame) = stream.next().await { @@ -23,7 +23,7 @@ while let Some(frame) = stream.next().await { The automotive crate also supplies interfaces for various diagnostic protocols such as UDS. The adapter is first wrapped to support the ISO Transport Layer, then a UDS Client is created. All methods are fully async, making it easy to communicate with multiple ECUs in parallel. See [https://github.com/I-CAN-hack/automotive/issues/21](https://github.com/I-CAN-hack/automotive/issues/21) for progress on the supported SIDs. ```rust -let adapter = automotive::adapter::get_adapter().unwrap(); +let adapter = automotive::can::get_adapter().unwrap(); let isotp = automotive::isotp::IsoTPAdapter::from_id(&adapter, 0x7a1); let uds = automotive::uds::UDSClient::new(&isotp); diff --git a/examples/can_printer.rs b/examples/can_printer.rs index 714fe06..da7bbde 100644 --- a/examples/can_printer.rs +++ b/examples/can_printer.rs @@ -5,7 +5,7 @@ use tracing_subscriber; async fn main() { tracing_subscriber::fmt::init(); - let adapter = automotive::adapter::get_adapter().unwrap(); + let adapter = automotive::can::get_adapter().unwrap(); let mut stream = adapter.recv(); while let Some(frame) = stream.next().await { diff --git a/examples/isotp.rs b/examples/isotp.rs index 0f14a19..b3f1e13 100644 --- a/examples/isotp.rs +++ b/examples/isotp.rs @@ -7,7 +7,7 @@ use tracing_subscriber; async fn main() { tracing_subscriber::fmt::init(); - let adapter = automotive::adapter::get_adapter().unwrap(); + let adapter = automotive::can::get_adapter().unwrap(); let config = IsoTPConfig::new(0, Identifier::Standard(0x7a1)); let isotp = IsoTPAdapter::new(&adapter, config); diff --git a/examples/query_fw_versions.rs b/examples/query_fw_versions.rs index 10a637e..c5340a8 100644 --- a/examples/query_fw_versions.rs +++ b/examples/query_fw_versions.rs @@ -1,4 +1,4 @@ -use automotive::async_can::AsyncCanAdapter; +use automotive::can::AsyncCanAdapter; use automotive::can::Identifier; use automotive::error::Error; use automotive::isotp::{IsoTPAdapter, IsoTPConfig}; @@ -43,7 +43,7 @@ async fn get_version(adapter: &AsyncCanAdapter, identifier: u32) -> Result<(), E async fn main() { tracing_subscriber::fmt::init(); - let adapter = automotive::adapter::get_adapter().unwrap(); + let adapter = automotive::can::get_adapter().unwrap(); let standard_ids = 0x700..=0x7ff; let extended_ids = (0x00..=0xff).map(|i| 0x18da0000 + (i << 8) + 0xf1); diff --git a/examples/uds.rs b/examples/uds.rs index 8d1f775..3f0fe86 100644 --- a/examples/uds.rs +++ b/examples/uds.rs @@ -5,7 +5,7 @@ use bstr::ByteSlice; async fn main() -> Result<(), Box> { tracing_subscriber::fmt::init(); - let adapter = automotive::adapter::get_adapter()?; + let adapter = automotive::can::get_adapter()?; let isotp = automotive::isotp::IsoTPAdapter::from_id(&adapter, 0x7a1); let uds = automotive::uds::UDSClient::new(&isotp); diff --git a/src/adapter.rs b/src/can/adapter.rs similarity index 86% rename from src/adapter.rs rename to src/can/adapter.rs index a00dc1c..bcc8286 100644 --- a/src/adapter.rs +++ b/src/can/adapter.rs @@ -1,7 +1,7 @@ //! Convenience functions to get a CAN adapter. /// Convenience function to get the first available adapter on the system. Supports both comma.ai panda, and SocketCAN. -pub fn get_adapter() -> Result { +pub fn get_adapter() -> Result { if let Ok(panda) = crate::panda::Panda::new_async() { return Ok(panda); } diff --git a/src/async_can.rs b/src/can/async_can.rs similarity index 100% rename from src/async_can.rs rename to src/can/async_can.rs diff --git a/src/can.rs b/src/can/mod.rs similarity index 97% rename from src/can.rs rename to src/can/mod.rs index cceccf8..12a6562 100644 --- a/src/can.rs +++ b/src/can/mod.rs @@ -1,7 +1,13 @@ //! Generic CAN types and traits +pub mod adapter; +pub mod async_can; + use std::fmt; +pub use adapter::get_adapter; +pub use async_can::AsyncCanAdapter; + pub static DLC_TO_LEN: &[usize] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64]; /// Identifier for a CAN frame diff --git a/src/isotp/mod.rs b/src/isotp/mod.rs index c7b1a2c..e142d65 100644 --- a/src/isotp/mod.rs +++ b/src/isotp/mod.rs @@ -3,7 +3,7 @@ //! ```rust //! use futures_util::stream::StreamExt; //! async fn isotp_example() { -//! let adapter = automotive::adapter::get_adapter().unwrap(); +//! let adapter = automotive::can::get_adapter().unwrap(); //! let config = automotive::isotp::IsoTPConfig::new(0, automotive::can::Identifier::Standard(0x7a1)); //! let isotp = automotive::isotp::IsoTPAdapter::new(&adapter, config); //! @@ -17,7 +17,7 @@ pub mod constants; pub mod error; pub mod types; -use crate::async_can::AsyncCanAdapter; +use crate::can::AsyncCanAdapter; use crate::can::{Frame, Identifier, DLC_TO_LEN}; use crate::error::Error; use crate::isotp::constants::FlowStatus; diff --git a/src/lib.rs b/src/lib.rs index 0315ea8..1009fef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ //! ```rust //! use futures_util::stream::StreamExt; //! async fn can_example() { -//! let adapter = automotive::adapter::get_adapter().unwrap(); +//! let adapter = automotive::can::get_adapter().unwrap(); //! let mut stream = adapter.recv(); //! //! while let Some(frame) = stream.next().await { @@ -24,7 +24,7 @@ //! //! ```rust //! async fn uds_example() { -//! let adapter = automotive::adapter::get_adapter().unwrap(); +//! let adapter = automotive::can::get_adapter().unwrap(); //! let isotp = automotive::isotp::IsoTPAdapter::from_id(&adapter, 0x7a1); //! let uds = automotive::uds::UDSClient::new(&isotp); //! @@ -40,8 +40,6 @@ //! - comma.ai panda (all platforms) //! -pub mod adapter; -pub mod async_can; pub mod can; pub mod error; pub mod isotp; diff --git a/src/panda/mod.rs b/src/panda/mod.rs index c849920..f7778f5 100644 --- a/src/panda/mod.rs +++ b/src/panda/mod.rs @@ -6,7 +6,7 @@ mod usb_protocol; use std::vec; -use crate::async_can::AsyncCanAdapter; +use crate::can::AsyncCanAdapter; use crate::can::CanAdapter; use crate::error::Error; use crate::panda::constants::{Endpoint, HwType, SafetyModel}; diff --git a/src/socketcan/mod.rs b/src/socketcan/mod.rs index dd5cece..833fbd7 100644 --- a/src/socketcan/mod.rs +++ b/src/socketcan/mod.rs @@ -1,5 +1,5 @@ //! This module provides a [`CanAdapter`] implementation for the [`socketcan`] crate. -use crate::async_can::AsyncCanAdapter; +use crate::can::AsyncCanAdapter; use crate::can::CanAdapter; use crate::error::Error; diff --git a/src/uds/mod.rs b/src/uds/mod.rs index ac4b7fd..6f9c65e 100644 --- a/src/uds/mod.rs +++ b/src/uds/mod.rs @@ -2,7 +2,7 @@ //! ## Example //! ```rust //! async fn uds_example() { -//! let adapter = automotive::adapter::get_adapter().unwrap(); +//! let adapter = automotive::can::get_adapter().unwrap(); //! let isotp = automotive::isotp::IsoTPAdapter::from_id(&adapter, 0x7a1); //! let uds = automotive::uds::UDSClient::new(&isotp); //! diff --git a/tests/adapter_tests.rs b/tests/adapter_tests.rs index 3c01349..81244e8 100644 --- a/tests/adapter_tests.rs +++ b/tests/adapter_tests.rs @@ -1,5 +1,5 @@ #![allow(dead_code, unused_imports)] -use automotive::async_can::AsyncCanAdapter; +use automotive::can::AsyncCanAdapter; use automotive::can::{CanAdapter, Frame}; use automotive::panda::Panda; use std::time::Duration; diff --git a/tests/isotp_tests.rs b/tests/isotp_tests.rs index c235997..7873c6e 100644 --- a/tests/isotp_tests.rs +++ b/tests/isotp_tests.rs @@ -1,5 +1,5 @@ #![allow(dead_code, unused_imports)] -use automotive::async_can::AsyncCanAdapter; +use automotive::can::AsyncCanAdapter; use automotive::can::Identifier; use automotive::isotp::{IsoTPAdapter, IsoTPConfig}; use std::process::{Child, Command}; diff --git a/tests/uds_tests.rs b/tests/uds_tests.rs index d1de2b9..cf4aac8 100644 --- a/tests/uds_tests.rs +++ b/tests/uds_tests.rs @@ -1,5 +1,5 @@ #![allow(dead_code, unused_imports)] -use automotive::async_can::AsyncCanAdapter; +use automotive::can::AsyncCanAdapter; use automotive::can::Identifier; use automotive::isotp::{IsoTPAdapter, IsoTPConfig}; use automotive::uds::error::Error as UDSError;