Struct CanSocket

Source
pub struct CanSocket { /* private fields */ }
Expand description

A socket for a CAN device.

Will be closed upon deallocation. To close manually, use std::drop::Drop. Internally this is just a wrapped file-descriptor.

Implementations§

Source§

impl CanSocket

Source

pub fn open(ifname: &str) -> Result<CanSocket, CanSocketOpenError>

Open a named CAN device.

Usually the more common case, opens a socket can device by name, such as “vcan0” or “socan0”.

Source

pub fn open_if(if_index: c_uint) -> Result<CanSocket, CanSocketOpenError>

Open CAN device by interface number.

Opens a CAN device by kernel interface number.

Source

pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>

Change socket to non-blocking mode

Source

pub fn set_read_timeout(&self, duration: Duration) -> Result<()>

Sets the read timeout on the socket

For convenience, the result value can be checked using ShouldRetry::should_retry when a timeout is set.

Source

pub fn set_write_timeout(&self, duration: Duration) -> Result<()>

Sets the write timeout on the socket

Source

pub fn read_frame(&self) -> Result<CanFrame>

Blocking read a single can frame.

Source

pub fn read_frame_with_timestamp(&mut self) -> Result<(CanFrame, SystemTime)>

Blocking read a single can frame with timestamp

Note that reading a frame and retrieving the timestamp requires two consecutive syscalls. To avoid race conditions, exclusive access to the socket is enforce through requiring a mut &self.

Source

pub fn write_frame(&self, frame: &CanFrame) -> Result<()>

Write a single can frame.

Note that this function can fail with an EAGAIN error or similar. Use write_frame_insist if you need to be sure that the message got sent or failed.

Source

pub fn write_frame_insist(&self, frame: &CanFrame) -> Result<()>

Blocking write a single can frame, retrying until it gets sent successfully.

Source

pub fn set_filters(&self, filters: &[CanFilter]) -> Result<()>

Sets filters on the socket.

CAN packages received by SocketCAN are matched against these filters, only matching packets are returned by the interface.

See CanFilter for details on how filtering works. By default, all single filter matching all incoming frames is installed.

Source

pub fn set_error_mask(&self, mask: u32) -> Result<()>

Sets the error mask on the socket.

By default (ERR_MASK_NONE) no error conditions are reported as special error frames by the socket. Enabling error conditions by setting ERR_MASK_ALL or another non-empty error mask causes the socket to receive notification about the specified conditions.

Source

pub fn set_loopback(&self, enabled: bool) -> Result<()>

Enable or disable loopback.

By default, loopback is enabled, causing other applications that open the same CAN bus to see frames emitted by different applications on the same system.

Source

pub fn set_recv_own_msgs(&self, enabled: bool) -> Result<()>

Enable or disable receiving of own frames.

When loopback is enabled, this settings controls if CAN frames sent are received back immediately by sender. Default is off.

Source

pub fn set_join_filters(&self, enabled: bool) -> Result<()>

Enable or disable join filters.

By default a frame is accepted if it matches any of the filters set with set_filters. If join filters is enabled, a frame has to match all filters to be accepted.

Trait Implementations§

Source§

impl AsRawFd for CanSocket

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for CanSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for CanSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl FromRawFd for CanSocket

Source§

unsafe fn from_raw_fd(fd: RawFd) -> CanSocket

Constructs a new instance of Self from the given raw file descriptor. Read more
Source§

impl IntoRawFd for CanSocket

Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying file descriptor. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.