Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Linux SocketCAN access in Rust

License

NotificationsYou must be signed in to change notification settings

socketcan-rs/socketcan-rs

Repository files navigation

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 thedocumentation for details about the Rust API provided by this library.

Latest News

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 thedump module and improved the usability of several frame and socket types. See below for details.

Version 3.x adds integrated async/await, improved Netlink coverage, and more!

Version 3.0 adds integrated support for async/await, with the most popular runtimes,tokio, async-std, andsmol. We have merged thetokio-socketcan crate into this one and implementedasync-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, orsmol.

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.

What's New in Version 3.5

  • CanAnyFrame implementsFrom trait forCanDataFrame,CanRemoteFrame, andCanErrorFrame.
  • CanFdSocket implementaTryFrom trait forCanSocket
  • Added FdFlags::FDF bit mask for CANFD_FDF
    • The FDF flag is forced on when creating a CanFdFrame.
  • Updates todump module:
    • Re-implemented with text parsing
    • ParseError now implements stdError trait viathiserror::Error
    • Parses FdFlags field properly
    • CANFD_FDF bit flag recognized on input
    • Fixed reading remote frames
    • Now reads remote length
    • CanDumpRecord changes:
    • Removed lifetime and madedevice field an ownedString
    • ImplementedClone andDisplay traits.
    • Display trait is compatible with the candump log record format
    • dump::Reader is now an Iterator itself, returning fullCanDumpRecord items
    • New unit tests
  • #59 Embedded Hal for CanFdSocket

What's New in Version 3.4

Version 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:

  • A new build feature,enumerate to provide code for enumerating CAN interfaces on the host.
  • Added aCanId type with better usability thanembedded_can::Id
  • Fixes to the Flexible Data (CAN FD) implementation, including proper frame padding and DLC/length queries.
  • MadeCanState public.
  • Improved and modernized thetokio implementation.

For a full list of updates, see thev3.4.0 CHANGELOG

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

Minimum Supported Rust Version (MSRV)

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.

Async Support

Tokio

Thetokio-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 withtokio

This is a simple example of sending data frames from one CAN interface to another. It is included inthe example applications astokio_bridge.rs.

use futures_util::StreamExt;use socketcan::{tokio::CanSocket,CanFrame,Result};use tokio;#[tokio::main]asyncfnmain() ->Result<()>{letmut sock_rx =CanSocket::open("vcan0")?;let sock_tx =CanSocket::open("can0")?;whileletSome(Ok(frame)) = sock_rx.next().await{ifmatches!(frame,CanFrame::Data(_)){            sock_tx.write_frame(frame)?.await?;}}Ok(())}

async-io (async-std &smol)

New support was added for theasync-io runtime, supporting theasync-std andsmol runtimes.

This is enabled with the optional feature,async-io. It can also be enabled with either feature,async-std orsmol. Either of those specific runtime flags will simply build theasync-io support but then also alias theasync-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 theasync-std orsmol feature(s).

Example bridge withasync-std

This is a simple example of sending data frames from one CAN interface to another. It is included inthe example applications asasync_std_bridge.rs.

use socketcan::{async_std::CanSocket,CanFrame,Result};#[async_std::main]asyncfnmain() ->Result<()>{let sock_rx =CanSocket::open("vcan0")?;let sock_tx =CanSocket::open("can0")?;loop{let frame = sock_rx.read_frame().await?;ifmatches!(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 avcan0 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 requiringvcan0 were placed behind an optional feature,vcan_tests.

The steps to install and add a virtual interface to Linux are in thescripts/vcan.sh script. Run it with root proveleges, then run the tests:

$ sudo ./scripts/vcan.sh$ cargotest --features=vcan_tests

About

Linux SocketCAN access in Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp