Struct CanSocket

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

A synchronous CAN socket.

Used to send and receive CanFrame’s over the network.

Although the socket is synchronous, it can be put into non-blocking mode with Self::set_nonblocking().

Implementations§

Source§

impl CanSocket

Source

pub fn bind(interface: impl AsRef<str>) -> Result<Self>

Create a new socket bound to a named CAN interface.

Source

pub fn bind_interface_index(index: u32) -> Result<Self>

Create a new socket bound to a interface by index.

Source

pub fn bind_all() -> Result<Self>

Create a new socket bound to all CAN interfaces on the system.

You can use Self::recv_from() if you need to know on which interface a frame was received, and Self::send_to() to send a frame on a particular interface.

Source

pub fn local_addr(&self) -> Result<CanInterface>

Get the interface this socket is bound to.

If the socket is bound to all interfaces, the returned CanInterface will report index 0.

Source

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

Set the socket in non-blocking or blocking mode.

If the socket is set in non-blocking mode, send and receive operations will never block. Instead, if the operation can not be completed immediately, it will fail with a std::io::ErrorKind::WouldBlock error.

Source

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

Send a frame over the socket.

Note that if this function success, it only means that the kernel accepted the frame for transmission. It does not mean the frame has been successfully transmitted over the CAN bus.

Source

pub fn send_to(&self, frame: &CanFrame, interface: &CanInterface) -> Result<()>

Send a frame over a particular interface.

Note that if this function success, it only means that the kernel accepted the frame for transmission. It does not mean the frame has been successfully transmitted over the CAN bus.

Source

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

Receive a frame from the socket.

Source

pub fn recv_from(&self) -> Result<(CanFrame, CanInterface)>

Receive a frame from the socket, including information about which interface the frame was received on.

Source

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

Set the list of filters on the socket.

When a socket is created, it will receive all frames from the CAN interface. You can restrict this by setting the filters with this function.

A frame has to match only one of the filters in the list to be received by the socket.

Source

pub fn get_loopback(&self) -> Result<bool>

Check if the loopback option of the socket is enabled.

When enabled (the default for new sockets), frames sent on the same interface by other sockets are also received by this socket.

Source

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

Enable or disabling the loopback option of the socket.

When enabled (the default for new sockets), frames sent on the same interface by other sockets are also received by this socket.

See Self::set_receive_own_messages() if you also want to receive messages sens on this socket.

Source

pub fn get_receive_own_messages(&self) -> Result<bool>

Check if the receive own messages option of the socket is enabled.

When this option is enabled, frames sent on this socket are also delivered to this socket.

Note that frames sent on this socket are subject to all the same filtering mechanisms as other frames. To receive frames send on this socket, you must also to ensure that the loopback option is enabled (Self::get_loopback()), and that the frame is not discarded by the filters (Self::set_filters()).

Source

pub fn set_receive_own_messages(&self, enable: bool) -> Result<()>

Enable or disable the receive own messages option of the socket.

When this option is enabled, frames sent on this socket are also delivered to this socket.

Note that frames sent on this socket are subject to all the same filtering mechanisms as other frames. To receive frames send on this socket, you must also to ensure that the loopback option is enabled (Self::set_loopback()), and that the frame is not discarded by the filters (Self::set_filters()).

Trait Implementations§

Source§

impl AsFd for CanSocket

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
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 From<CanSocket> for OwnedFd

Source§

fn from(value: CanSocket) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedFd> for CanSocket

Source§

fn from(value: OwnedFd) -> Self

Converts to this type from the input type.
Source§

impl FromRawFd for CanSocket

Source§

unsafe fn from_raw_fd(fd: RawFd) -> Self

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