#linux #frame #network-communication #linux-networking #bus #async-io #sockets

bin+lib socketcan

Linux SocketCAN library. Send and receive CAN frames via CANbus on Linux.

18 stable releases

3.3.1 Nov 9, 2024
3.3.0 Oct 27, 2023
3.0.0 Sep 19, 2023
2.1.0 Sep 19, 2023
1.2.1 Jul 14, 2016

#39 in Network programming

Download history 8742/week @ 2024-08-24 8166/week @ 2024-08-31 9754/week @ 2024-09-07 8814/week @ 2024-09-14 9585/week @ 2024-09-21 7720/week @ 2024-09-28 9661/week @ 2024-10-05 8901/week @ 2024-10-12 10006/week @ 2024-10-19 9435/week @ 2024-10-26 9967/week @ 2024-11-02 10052/week @ 2024-11-09 11282/week @ 2024-11-16 9116/week @ 2024-11-23 9166/week @ 2024-11-30 7741/week @ 2024-12-07

39,081 downloads per month
Used in 38 crates (31 directly)

MIT license

190KB
3.5K SLoC

Rust SocketCAN

This library implements Controller Area Network (CAN) communications on Linux using the SocketCAN subsystem. This provides a network socket interface to the CAN bus.

Linux SocketCAN

Please see the documentation for details about the Rust API provided by this library.

Latest News

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.

Unfortunaly 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 inteface, etc.

v3.2 increased the interface configuration coverage with Netlink, allowing an application to set most interface CAN parameters and query them all back.

What's New in Version 3.3

  • #53 Added CanFD support for tokio
  • Serialized tokio unit tests and put them behind the "vcan_tests" feature

What's New in Version 3.2

  • #32 Further expanded netlink functionality:
    • Added setters for most additional interface CAN parameters
    • Ability to query back interface CAN parameters
    • Expanded InterfaceDetails to include CAN-specific parameters
    • Better integration of low-level types with neli
    • Significant cleanup of the nl module
    • Split the nl module into separate sources for higher and lower-level code

Next Steps

A number of items still did not make it into a release. These will be added in v3.x, coming soon.

  • Issue #22 Timestamps, including optional hardware timestamps
  • Better documentation. This README will be expanded with basic usage information, along with better doc comments, and perhaps creation of the wiki

Minimum Supported Rust Version (MSRV)

The current version of the crate targets Rust Edition 2021 with an MSRV of Rust v1.65.0.

Note that, at this time, the MSRV is mostly diven by use of the clap v4.0 crate for managing command-line parameters in the utilities and example applications. The core library could likely be built with an earlier version of the compiler if required.

Async Support

Tokio

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.

Example bridge with 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(())
}

async-io (async-std & smol)

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).

Example bridge with async-std

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?;
        }
    }
}

Testing

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

Dependencies

~2–14MB
~181K SLoC