-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlib.rs
More file actions
111 lines (102 loc) · 7.89 KB
/
Copy pathlib.rs
File metadata and controls
111 lines (102 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! # The Automotive Crate
//! [](https://crates.io/crates/automotive)
//! [](https://docs.rs/automotive/latest/automotive/)
//!
//! Welcome to the `automotive` crate documentation. The purpose of this crate is to help you with all things automotive related. Most importantly, it provides a fully async CAN interface supporting multiple adapters.
//!
//! ## Async CAN Example
//! The following adapter opens the first available adapter on the system, and then receives all frames. Note how the sent frame is awaited, which waits until the message is ACKed on the CAN bus.
//!
//! ```rust
//! use automotive::StreamExt;
//! async fn can_example() -> automotive::Result<()> {
//! let adapter = automotive::can::get_adapter()?;
//! let mut stream = adapter.recv();
//!
//! let frame = automotive::can::Frame::new(0, 0x541.into(), &[0xff; 8])?;
//! adapter.send(&frame).await;
//!
//! while let Some(frame) = stream.next().await {
//! let id: u32 = frame.id.into();
//! println!("[{}]\t0x{:x}\t{}", frame.bus, id, hex::encode(frame.data));
//! }
//! Ok(())
//! }
//! ```
//!
//! ## UDS Example
//! 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 [automotive#21](https://github.com/I-CAN-hack/automotive/issues/21) for progress on the supported SIDs.
//!
//! ```rust
//! async fn uds_example() -> automotive::Result<()> {
//! let adapter = automotive::can::get_adapter()?;
//! let isotp = automotive::isotp::IsoTPAdapter::from_id(&adapter, 0x7a1);
//! let uds = automotive::uds::UDSClient::new(&isotp);
//!
//! uds.tester_present().await.unwrap();
//! let response = uds.read_data_by_identifier(automotive::uds::DataIdentifier::ApplicationSoftwareIdentification as u16).await?;
//!
//! println!("Application Software Identification: {}", hex::encode(response));
//! Ok(())
//! }
//! ```
//!
//! ## CAN Adapters
//! The following CAN adapters are supported.
//!
//! ### Supported CAN adapters
//! - SocketCAN (Linux only, enable with the `socketcan` feature)
//! - comma.ai panda (all platforms using [rusb](https://crates.io/crates/rusb), enable with the `panda` feature)
//! - J2534 PassThru Devices (Windows only, enable with the `j2534` feature)
//! - Vector Devices (Windows x64 only, enable with the `vector-xl` feature)
//!
//! Adapter support is opt-in. The default feature set does not enable any hardware adapters.
//!
//! ### Known limitations / Notes
//! This library has some unique features that might expose (performance) issues in drivers you wouldn't otherwise notice, so check the list of known limitations below.
//!
//! This library supports awaiting a sent frame and waiting for the ACK on the CAN bus. This requires receiving these ACKs from the adapter, and matching them to the appropriate sent frame. This requires some level of hardware support that is not offered by all adapters/drivers. If this is not supported by the driver, an ACK will be simulated as soon as the frame is transmitted, but this can cause issues if precise timing is needed.
//!
//! - SocketCAN Devices
//! - SocketCAN drivers without `IFF_ECHO`: This class of SocketCAN drivers has no hardware support for notifying the driver when a frame was ACKed. This is instead emulated by the [Linux kernel](https://github.com/torvalds/linux/blob/master/net/can/af_can.c#L256). Due to transmitted frames immediately being received again this can cause the receive queue to fill up if more than 476 (default RX queue size on most systems) are transmitted in one go. To solve this we implement emulated ACKs ourself, instead of relying on the ACKs from the kernel.
//! - PCAN-USB: The Peak CAN adapters have two drivers:
//! - Kenel built in driver (`peak_usb`). The kernel driver properly implements `IFF_ECHO`, but has a rather small TX queue. This should not cause any issues, but it can be increased with `ifconfig can0 txqueuelen <size>`.
//! - Out-of-tree driver (`pcan`) that can be [downloaded](https://www.peak-system.com/fileadmin/media/linux/index.htm) from Peak System's website. The out-of-tree driver is not recommended as it does not implement `IFF_ECHO`.
//! - neoVI/ValueCAN: Use of Intrepid Control System's devices is not recommended due to issues in their SocketCAN driver. If many frames are transmitted simultaneously it will cause the whole system/kernel to hang. [intrepid-socketcan-kernel-module#20](https://github.com/intrepidcs/intrepid-socketcan-kernel-module/issues/20) tracks this issue.
//! - comma.ai panda
//! - The panda does not retry frames that are not ACKed, and drops them instead. This can cause panics in some internal parts of the library when frames are dropped. [panda#1922](https://github.com/commaai/panda/issues/1922) tracks this issue.
//! - J2534 PassThru Devices are supported through the `j2534` feature. The library provides `J2534CanAdapter` for raw CAN access, and `J2534NativeIsoTpTransport` to offload ISO-TP framing, flow-control, and timing to the adapter. It is recommended to use `J2534CanAdapter` unless you have specific speed or latency requirements.
//! - Vector Devices are supported through the Vector XL Driver Library, and support can be enabled using the `vector-xl` feature. Make sure to distribute `vxlapi64.dll` alongside your application.
//!
//!
//! ### Implementing a New Adapter
//! Implementing a new adapter is done by implementing the `CanAdapter` Trait. Hardware implementations can be blocking, as the [AsyncCanAdapter](https://docs.rs/automotive/latest/automotive/async_can/struct.AsyncCanAdapter.html) takes care of presenting an async interface to the user. The library makes some assumptions around sending/receiving frames. These assumption are also verified by the tests in `tests/adapter_tests.rs`.
//!
//! - The `send` function takes a `&mut VecDequeue` of frames. Frames to be sent are taken from the *front* of this queue. If there is no space in the hardware or driver buffer to send out all messages it's OK to return before the queue is fully empty. If an error occurs make sure to put the message back at the beginning of the queue and return.
//! - The hardware or driver is free to prioritize sending frames with a lower Arbitration ID to prevent priority inversion. However frames with the same Arbitration ID need to be send out on the CAN bus in the same order as they were queued. This assumption is needed to match a received ACK to the correct frame.
//! - Once a frame is ACKed it should be put in the receive queue with the `loopback` flag set. The `AsyncCanAdapter` wrapper will take care of matching it against the right transmit frame and resolving the Future. If this is not supported by the underlying hardware, this can be faked by looping back all transmitted frames immediately.
#![cfg_attr(docsrs, feature(doc_cfg))]
pub mod can;
mod error;
pub mod isotp;
pub mod uds;
/// Re-export of relevant stream traits from `tokio_stream`.
pub use tokio_stream::{Stream, StreamExt, Timeout};
pub use error::Error;
pub use isotp::TransportLayer;
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(all(target_os = "linux", feature = "socketcan"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", feature = "socketcan"))))]
pub mod socketcan;
#[cfg(all(target_os = "windows", feature = "vector-xl"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "windows", feature = "vector-xl"))))]
pub mod vector;
#[cfg(all(target_os = "windows", feature = "j2534"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "windows", feature = "j2534"))))]
pub mod j2534;
#[cfg(feature = "panda")]
#[cfg_attr(docsrs, doc(cfg(feature = "panda")))]
pub mod panda;
#[cfg(feature = "peak")]
#[cfg_attr(docsrs, doc(cfg(feature = "peak")))]
pub mod peak;