Struct CanSocket

Source
pub struct CanSocket { /* private fields */ }
Available on crate feature tokio only.
Expand description

An asynchronous CAN socket for tokio.

Implementations§

Source§

impl CanSocket

Source

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

Create a new socket bound to a named CAN interface.

This function is not async as it will either succeed or fail immediately.

Source

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

Create a new socket bound to a interface by index.

This function is not async as it will either succeed or fail immediately.

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.

This function is not async as it will either succeed or fail immediately.

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 async 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 sucessfully transmitted over the CAN bus.

Source

pub async fn send_timeout( &self, frame: &CanFrame, timeout: impl Deadline, ) -> Result<()>

Send a frame over the socket with a timeout.

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 sucessfully transmitted over the CAN bus.

The timeout can be a std::time::Duration, std::time::Instant, tokio::time::Instant or any other implementator of the Deadline trait.

Source

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

Try to send a frame over the socket without waiting for the socket to become writable.

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 sucessfully transmitted over the CAN bus.

Source

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

Send a frame over a particular interface.

The interface must match the interface the socket was bound to, or the socket must have been bound to all interfaces.

Source

pub async fn send_to_timeout( &self, frame: &CanFrame, interface: &CanInterface, timeout: impl Deadline, ) -> Result<()>

Send a frame over a particular interface.

The interface must match the interface the socket was bound to, or the socket must have been bound to all interfaces.

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 sucessfully transmitted over the CAN bus.

The timeout can be a std::time::Duration, std::time::Instant, tokio::time::Instant or any other implementator of the Deadline trait.

Source

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

Try to send a frame over the socket without waiting for the socket to become writable.

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 sucessfully transmitted over the CAN bus.

Source

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

Receive a frame from the socket.

Source

pub async fn recv_timeout(&self, timeout: impl Deadline) -> Result<CanFrame>

Receive a frame from the socket with a timeout.

The timeout can be a std::time::Duration, std::time::Instant, tokio::time::Instant or any other implementator of the Deadline trait.

Source

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

Receive a frame from the socket, without waiting for one to become available.

Source

pub async 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 async fn recv_from_timeout( &self, timeout: impl Deadline, ) -> Result<(CanFrame, CanInterface)>

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

The timeout can be a std::time::Duration, std::time::Instant, tokio::time::Instant or any other implementator of the Deadline trait.

Source

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

Receive a frame from the socket, without waiting for one to become available.

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 IntoRawFd for CanSocket

Source§

fn into_raw_fd(self) -> RawFd

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

impl TryFrom<OwnedFd> for CanSocket

Source§

type Error = Error

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

fn try_from(value: OwnedFd) -> Result<Self>

Performs the conversion.

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.