Skip to main content

can_hal_socketcan/
lib.rs

1//! # can-hal-socketcan
2//!
3//! Linux SocketCAN backend for [`can_hal`] traits.
4//!
5//! This crate provides [`SocketCanDriver`] and [`SocketCanChannel`] which
6//! implement the hardware-agnostic CAN traits defined in `can-hal`, enabling
7//! portable CAN application code to run on Linux systems with SocketCAN
8//! interfaces.
9//!
10//! # Example
11//!
12//! ```rust,no_run
13//! use can_hal::{CanId, CanFrame, Transmit, Receive, ChannelBuilder};
14//! use can_hal_socketcan::SocketCanDriver;
15//!
16//! let driver = SocketCanDriver::new();
17//! let mut channel = driver
18//!     .channel_by_name("vcan0")
19//!     .unwrap()
20//!     .bitrate(500_000)
21//!     .unwrap()
22//!     .connect()
23//!     .unwrap();
24//!
25//! let id = CanId::new_standard(0x123).unwrap();
26//! let frame = CanFrame::new(id, &[0xDE, 0xAD]).unwrap();
27//! channel.transmit(&frame).unwrap();
28//! ```
29//!
30//! # Bitrate Configuration
31//!
32//! SocketCAN bitrate is configured at the OS level, not through the socket API.
33//! Use `ip link set` or netlink before opening a channel:
34//!
35//! ```bash
36//! sudo ip link set can0 type can bitrate 500000
37//! sudo ip link set can0 up
38//! ```
39//!
40//! The builder's `bitrate()` / `data_bitrate()` / `sample_point()` methods
41//! store values for informational purposes but do not apply them.
42
43pub mod channel;
44pub mod driver;
45pub mod error;
46
47mod convert;
48
49pub use channel::SocketCanChannel;
50pub use driver::{SocketCanChannelBuilder, SocketCanDriver};
51pub use error::SocketCanError;
52
53// Compile-time assertion: channel must be Send so it can be moved across threads.
54const _: fn() = || {
55    fn assert_send<T: Send>() {}
56    assert_send::<SocketCanChannel>();
57};