Skip to content

osiler/socketcan-rs

 
 

Repository files navigation

Rust SocketCAN

This library allows Controller Area Network (CAN) communications on Linux using the SocketCAN interfaces. 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 2.0 is finally released!

What's New in v2.0

The v2.0 release is a fairly large rewrite of the library and adds the following features:

  • CAN Flexible Data Rate (FD) support
  • Proper handling of Extended CAN IDs
  • Integration with the Rust Embedded HAL APIs for CAN
  • Some control of the CAN network interfaces via netlink with the neli crate.
  • Tighter integration with libc and nix crates, including changes we pushed upstream to support SocketCAN
  • Update to Rust Edition 2021, with updates to the dependencies.
  • Update error types conforming to std::error::Error
  • Distinct separate frame types:
    • CanDataFrame, CanRemoteFrame, CanErrorFrame, and CanFdFrame
    • Enum wrapper types CanFrame for the classic 2.0 frames and CanAnyFrame for any type of frame including the larger FD frames
  • Updated documentation
  • Targeting Rust Edition 2021 w/ MSRV 1.64.0

Next Steps

A number of items did not make it into the 2.0 release. These will be added in a follow-up v2.1, coming soon.

  • Issue #22 Timestamps, including optional hardware timestamps
  • Issue #32 Better coverage of the Netlink API to manipulate the CAN interfaces programatically.
  • Better documentation. This README will be expanded with basic usage information, along with better doc comments, and perhaps creation of the wiki

We will also start looking into support of Rust async/await, prefereably in a portable way without lying on a particular library/executor. But certainly support for the main ones like Tokio would be the goal. Some folks have suggested putting this into a separate wrapper crate, but it would be better to add it here for convenience, but certainly made optional through a Cargo build feature.

Minimum Supported Rust Version (MSRV)

The current version of the crate targets Rust Edition 2021 with an MSRV of Rust v1.64.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.

Example echo server with tokio

use futures_util::stream::StreamExt;
use tokio_socketcan::{CANSocket, Error};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let mut socket_rx = CANSocket::open("vcan0")?;
    let socket_tx = CANSocket::open("vcan0")?;
    while let Some(Ok(frame)) = socket_rx.next().await {
        socket_tx.write_frame(frame)?.await?;
    }
    Ok(())
}

Testing tokio

Integrating the test into a CI system is non-trivial as it relies on a vcan0 virtual can device existing. Adding one to most linux systems is pretty easy with root access but attaching a vcan device to a container for CI seems difficult to find support for.

To run the tests locally, though, setup should be simple:

sudo modprobe vcan
sudo ip link add vcan0 type vcan
sudo ip link set vcan0 up
cargo test

About

Linux SocketCAN access in Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 99.5%
  • Shell 0.5%