Skip to content

Commit

Permalink
refactor can stuff into own module
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Mar 20, 2024
1 parent b5cd5e6 commit 0bba611
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion examples/can_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/isotp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions examples/query_fw_versions.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion examples/uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bstr::ByteSlice;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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);

Expand Down
2 changes: 1 addition & 1 deletion src/adapter.rs → src/can/adapter.rs
Original file line number Diff line number Diff line change
@@ -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<crate::async_can::AsyncCanAdapter, crate::error::Error> {
pub fn get_adapter() -> Result<crate::can::AsyncCanAdapter, crate::error::Error> {
if let Ok(panda) = crate::panda::Panda::new_async() {
return Ok(panda);
}
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions src/can.rs → src/can/mod.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/isotp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//!
Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
//!
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/panda/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/socketcan/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/uds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//!
Expand Down
2 changes: 1 addition & 1 deletion tests/adapter_tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/isotp_tests.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion tests/uds_tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 0bba611

Please sign in to comment.