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
impl CanSocket
Sourcepub fn open(ifname: &str) -> Result<CanSocket, CanSocketOpenError>
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”.
Sourcepub fn open_if(if_index: c_uint) -> Result<CanSocket, CanSocketOpenError>
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.
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Change socket to non-blocking mode
Sourcepub fn set_read_timeout(&self, duration: Duration) -> Result<()>
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.
Sourcepub fn set_write_timeout(&self, duration: Duration) -> Result<()>
pub fn set_write_timeout(&self, duration: Duration) -> Result<()>
Sets the write timeout on the socket
Sourcepub fn read_frame(&self) -> Result<CanFrame>
pub fn read_frame(&self) -> Result<CanFrame>
Blocking read a single can frame.
Sourcepub fn read_frame_with_timestamp(&mut self) -> Result<(CanFrame, SystemTime)>
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
.
Sourcepub fn write_frame(&self, frame: &CanFrame) -> Result<()>
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.
Sourcepub fn write_frame_insist(&self, frame: &CanFrame) -> Result<()>
pub fn write_frame_insist(&self, frame: &CanFrame) -> Result<()>
Blocking write a single can frame, retrying until it gets sent successfully.
Sourcepub fn set_filters(&self, filters: &[CanFilter]) -> Result<()>
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.
Sourcepub fn set_error_mask(&self, mask: u32) -> Result<()>
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.
Sourcepub fn set_loopback(&self, enabled: bool) -> Result<()>
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.
Sourcepub fn set_recv_own_msgs(&self, enabled: bool) -> Result<()>
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.
Sourcepub fn set_join_filters(&self, enabled: bool) -> Result<()>
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§
Auto Trait Implementations§
impl Freeze for CanSocket
impl RefUnwindSafe for CanSocket
impl Send for CanSocket
impl Sync for CanSocket
impl Unpin for CanSocket
impl UnwindSafe for CanSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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