Trait socketcan::socket::Socket

source ·
pub trait Socket: AsRawFd {
    type FrameType;

    // Required methods
    fn open_addr(addr: &CanAddr) -> Result<Self>
       where Self: Sized;
    fn read_frame(&self) -> Result<Self::FrameType>;
    fn write_frame<F>(&self, frame: &F) -> Result<()>
       where F: Into<Self::FrameType> + AsPtr;

    // Provided methods
    fn open(ifname: &str) -> Result<Self>
       where Self: Sized { ... }
    fn open_iface(ifindex: u32) -> Result<Self>
       where Self: Sized { ... }
    fn set_nonblocking(&self, nonblocking: bool) -> Result<()> { ... }
    fn set_read_timeout(&self, duration: Duration) -> Result<()> { ... }
    fn set_write_timeout(&self, duration: Duration) -> Result<()> { ... }
    fn read_frame_timeout(&self, timeout: Duration) -> Result<Self::FrameType> { ... }
    fn write_frame_insist<F>(&self, frame: &F) -> Result<()>
       where F: Into<Self::FrameType> + AsPtr { ... }
}
Expand description

Common trait for SocketCAN sockets.

Note that a socket it created by opening it, and then closed by dropping it.

Required Associated Types§

source

type FrameType

The type of CAN frame that can be read and written by the socket.

This is typically distinguished by the size of the supported frame, with the primary difference between a CanFrame and a CanFdFrame.

Required Methods§

source

fn open_addr(addr: &CanAddr) -> Result<Self>where Self: Sized,

Open a CAN socket by address.

source

fn read_frame(&self) -> Result<Self::FrameType>

Blocking read a single can frame.

source

fn write_frame<F>(&self, frame: &F) -> Result<()>where F: Into<Self::FrameType> + AsPtr,

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. Writes a normal CAN 2.0 frame to the socket.

Provided Methods§

source

fn open(ifname: &str) -> Result<Self>where Self: Sized,

Open a named CAN device.

Usually the more common case, opens a socket can device by name, such as “can0”, “vcan0”, or “socan0”.

source

fn open_iface(ifindex: u32) -> Result<Self>where Self: Sized,

Open CAN device by interface number.

Opens a CAN device by kernel interface number.

source

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

Change socket to non-blocking mode or back to blocking mode.

source

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.

source

fn set_write_timeout(&self, duration: Duration) -> Result<()>

Sets the write timeout on the socket

source

fn read_frame_timeout(&self, timeout: Duration) -> Result<Self::FrameType>

Blocking read a single can frame with timeout.

source

fn write_frame_insist<F>(&self, frame: &F) -> Result<()>where F: Into<Self::FrameType> + AsPtr,

Blocking write a single can frame, retrying until it gets sent successfully.

Object Safety§

This trait is not object safe.

Implementors§