pub struct CanSocket { /* private fields */ }
tokio
only.Expand description
An asynchronous CAN socket for tokio
.
Implementations§
Source§impl CanSocket
impl CanSocket
Sourcepub fn bind(interface: impl AsRef<str>) -> Result<Self>
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.
Sourcepub fn bind_interface_index(index: u32) -> Result<Self>
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.
Sourcepub fn bind_all() -> Result<Self>
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.
Sourcepub fn local_addr(&self) -> Result<CanInterface>
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.
Sourcepub async fn send(&self, frame: &CanFrame) -> Result<()>
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.
Sourcepub async fn send_timeout(
&self,
frame: &CanFrame,
timeout: impl Deadline,
) -> Result<()>
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.
Sourcepub fn try_send(&self, frame: &CanFrame) -> Result<()>
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.
Sourcepub async fn send_to(
&self,
frame: &CanFrame,
interface: &CanInterface,
) -> Result<()>
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.
Sourcepub async fn send_to_timeout(
&self,
frame: &CanFrame,
interface: &CanInterface,
timeout: impl Deadline,
) -> Result<()>
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.
Sourcepub fn try_send_to(
&self,
frame: &CanFrame,
interface: &CanInterface,
) -> Result<()>
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.
Sourcepub async fn recv_timeout(&self, timeout: impl Deadline) -> Result<CanFrame>
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.
Sourcepub fn try_recv(&self) -> Result<CanFrame>
pub fn try_recv(&self) -> Result<CanFrame>
Receive a frame from the socket, without waiting for one to become available.
Sourcepub async fn recv_from(&self) -> Result<(CanFrame, CanInterface)>
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.
Sourcepub async fn recv_from_timeout(
&self,
timeout: impl Deadline,
) -> Result<(CanFrame, CanInterface)>
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.
Sourcepub fn try_recv_from(&self) -> Result<(CanFrame, CanInterface)>
pub fn try_recv_from(&self) -> Result<(CanFrame, CanInterface)>
Receive a frame from the socket, without waiting for one to become available.
Sourcepub fn set_filters(&self, filters: &[CanFilter]) -> Result<()>
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.
Sourcepub fn get_loopback(&self) -> Result<bool>
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.
Sourcepub fn set_loopback(&self, enable: bool) -> Result<()>
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.
Sourcepub fn get_receive_own_messages(&self) -> Result<bool>
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()
).
Sourcepub fn set_receive_own_messages(&self, enable: bool) -> Result<()>
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()
).