Crates.io | socketcan |
lib.rs | socketcan |
version | |
source | src |
created_at | 2016-05-08 15:41:29.769804 |
updated_at | 2024-12-29 18:10:13.911066 |
description | Linux SocketCAN library. Send and receive CAN frames via CANbus on Linux. |
homepage | |
repository | https://github.com/mbr/socketcan-rs |
max_upload_size | |
id | 5005 |
Cargo.toml error: | TOML parse error at line 22, column 1 | 22 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This library implements Controller Area Network (CAN) communications on Linux using the SocketCAN subsystem. This provides a network socket interface to the CAN bus.
Please see the documentation for details about the Rust API provided by this library.
Then end of 2024 saw two back-to-back release, v3.4 and v3.5. The first rolled up the PRs and bug fixes that had been sitting the the repository for the year. The second updated the dump
module and improved the usability of several frame and socket types. See below for details.
Version 3.0 adds integrated support for async/await, with the most popular runtimes, tokio, async-std, and smol. We have merged the tokio-socketcan crate into this one and implemented async-io
.
Unfortunately this required a minor breaking change to the existing API, so we bumped the version to 3.0.
The async support is optional, and can be enabled with a feature for the target runtime: tokio
, async-std
, or smol
.
Additional implementation of the netlink control of the CAN interface was added in v3.1 allowing an application to do things like set the bitrate on the interface, set control modes, restart the interface, etc.
v3.2 increased the interface configuration coverage with Netlink, allowing an application to set most interface CAN parameters and query them all back.
CanAnyFrame
implements From
trait for CanDataFrame
, CanRemoteFrame
, and CanErrorFrame
.CanFdSocket
implementa TryFrom
trait for CanSocket
dump
module:
ParseError
now implements std Error
trait via thiserror::Error
CanDumpRecord
changes:device
field an owned String
Clone
and Display
traits.Display
trait is compatible with the candump log record formatdump::Reader
is now an Iterator itself, returning full CanDumpRecord
itemsVersion 3.4.0 was primarily a service release to publish a number of new feature and bug fixes that had accumulated in the repository over the previous months. Those included:
enumerate
to provide code for enumerating CAN interfaces on the host.CanId
type with better usability than embedded_can::Id
CanState
public.For a full list of updates, see the v3.4.0 CHANGELOG
A number of items still did not make it into a release. These will be added in v3.x, coming soon.
The current version of the crate targets Rust Edition 2021 with an MSRV of Rust v1.70.
Note that, the core library can likely compile with an earlier version if dependencies are carefully selected, but tests are being done with the latest stable compiler and the stated MSRV.
The tokio-socketcan crate was merged into this one to provide async support for CANbus using tokio.
This is enabled with the optional feature, tokio
.
This is a simple example of sending data frames from one CAN interface to another. It is included in the example applications as tokio_bridge.rs.
use futures_util::StreamExt;
use socketcan::{tokio::CanSocket, CanFrame, Result};
use tokio;
#[tokio::main]
async fn main() -> Result<()> {
let mut sock_rx = CanSocket::open("vcan0")?;
let sock_tx = CanSocket::open("can0")?;
while let Some(Ok(frame)) = sock_rx.next().await {
if matches!(frame, CanFrame::Data(_)) {
sock_tx.write_frame(frame)?.await?;
}
}
Ok(())
}
New support was added for the async-io runtime, supporting the async-std and smol runtimes.
This is enabled with the optional feature, async-io
. It can also be enabled with either feature, async-std
or smol
. Either of those specific runtime flags will simply build the async-io
support but then also alias the async-io
submodule with the specific feature/runtime name. This is simply for convenience.
Additionally, when building examples, the specific examples for the runtime will be built if specifying the async-std
or smol
feature(s).
This is a simple example of sending data frames from one CAN interface to another. It is included in the example applications as async_std_bridge.rs.
use socketcan::{async_std::CanSocket, CanFrame, Result};
#[async_std::main]
async fn main() -> Result<()> {
let sock_rx = CanSocket::open("vcan0")?;
let sock_tx = CanSocket::open("can0")?;
loop {
let frame = sock_rx.read_frame().await?;
if matches!(frame, CanFrame::Data(_)) {
sock_tx.write_frame(&frame).await?;
}
}
}
Integrating the full suite of tests into a CI system is non-trivial as it relies on a vcan0
virtual CAN device existing. Adding it to most Linux systems is pretty easy with root access, but attaching a vcan device to a container for CI seems difficult to implement.
Therefore, tests requiring vcan0
were placed behind an optional feature, vcan_tests
.
The steps to install and add a virtual interface to Linux are in the scripts/vcan.sh
script. Run it with root proveleges, then run the tests:
$ sudo ./scripts/vcan.sh
$ cargo test --features=vcan_tests