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
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.
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.
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.
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 fn set_nonblocking(&self, non_blocking: bool) -> Result<()>
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.
Sourcepub fn send(&self, frame: &CanFrame) -> Result<()>
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.
Sourcepub fn send_to(&self, frame: &CanFrame, interface: &CanInterface) -> Result<()>
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.
Sourcepub fn recv_from(&self) -> Result<(CanFrame, CanInterface)>
pub fn recv_from(&self) -> Result<(CanFrame, CanInterface)>
Receive a frame from the socket, including information about which interface the frame was received on.
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()
).