Struct socketcan::nl::CanInterface

source ·
pub struct CanInterface { /* private fields */ }
Expand description

SocketCAN Netlink CanInterface

Controlled through the kernel’s Netlink interface, CAN devices can be brought up or down or configured or queried through this.

Note while that this API is designed in an RAII-fashion, it cannot really make the same guarantees: It is entirely possible for another user/process to modify, remove and re-add an interface while you are holding this object with a reference to it.

Some actions possible on this interface require the process/user to have the CAP_NET_ADMIN capability, like the root user does. This is indicated by their documentation starting with “PRIVILEGED:”.

Implementations§

source§

impl CanInterface

source

pub fn open(ifname: &str) -> Result<Self, Error>

Open a CAN interface by name.

Similar to open_iface, but looks up the device by name instead of the interface index.

source

pub fn open_iface(if_index: u32) -> Self

Open a CAN interface.

Creates a new CanInterface instance.

Note that no actual “opening” or checks are performed when calling this function, nor does it test to determine if the interface with the specified index actually exists.

source

pub fn bring_down(&self) -> Result<(), NlError>

Bring down this interface.

Use a netlink control socket to set the interface status to “down”.

source

pub fn bring_up(&self) -> Result<(), NlError>

Bring up this interface

Brings the interface up by settings its “up” flag enabled via netlink.

source

pub fn create_vcan(name: &str, index: Option<u32>) -> Result<Self, NlError>

Create a virtual CAN (VCAN) interface.

Useful for testing applications when a physical CAN interface and bus is not available.

Note that the length of the name is capped by libc::IFNAMSIZ.

PRIVILEGED: This requires root privilege.

source

pub fn create<I>(name: &str, index: I, kind: &str) -> Result<Self, NlError>where I: Into<Option<u32>>,

Create an interface of the given kind.

Note that the length of the name is capped by libc::IFNAMSIZ.

PRIVILEGED: This requires root privilege.

source

pub fn delete(self) -> Result<(), (Self, NlError)>

Delete the interface.

PRIVILEGED: This requires root privilege.

source

pub fn details(&self) -> Result<InterfaceDetails, NlError<Rtm, Ifinfomsg>>

Attempt to query detailed information on the interface.

source

pub fn set_mtu(&self, mtu: Mtu) -> Result<(), NlError>

Set the MTU of this interface.

PRIVILEGED: This requires root privilege.

source

pub fn set_can_param<P>( &self, param_type: IflaCan, param: P ) -> Result<(), NlError>where P: ToBytes + Size,

Set a CAN-specific parameter.

This send a netlink message down to the kernel to set an attribute in the link info, such as bitrate, control modes, etc

PRIVILEGED: This requires root privilege.

source

pub fn can_param<P>( &self, param: IflaCan ) -> Result<Option<P>, NlError<Rtm, Ifinfomsg>>where P: for<'a> FromBytes<'a> + Clone,

Attempt to query an individual CAN parameter on the interface.

source

pub fn bit_rate(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>

Gets the current bit rate for the interface.

source

pub fn set_bitrate<P>( &self, bitrate: u32, sample_point: P ) -> Result<(), NlError>where P: Into<Option<u32>>,

Set the bitrate and, optionally, sample point of this interface.

The bitrate can not be changed if the interface is UP. It is specified in Hz (bps) while the sample point is given in tenths of a percent/

PRIVILEGED: This requires root privilege.

source

pub fn bit_timing( &self ) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>

Gets the bit timing params for the interface

source

pub fn set_bit_timing(&self, timing: CanBitTiming) -> Result<(), NlError>

Sets the bit timing params for the interface

PRIVILEGED: This requires root privilege.

source

pub fn bit_timing_const( &self ) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>

Gets the bit timing const data for the interface

source

pub fn clock(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>

Gets the clock frequency for the interface

source

pub fn state(&self) -> Result<Option<CanState>, NlError<Rtm, Ifinfomsg>>

Gets the state of the interface

source

pub fn set_full_ctrlmode(&self, ctrlmode: can_ctrlmode) -> Result<(), NlError>

👎Deprecated since 3.2.0: Use set_ctrlmodes instead

Set the full control mode (bit) collection.

PRIVILEGED: This requires root privilege.

source

pub fn set_ctrlmodes<M>(&self, ctrlmode: M) -> Result<(), NlError>where M: Into<CanCtrlModes>,

Set the full control mode (bit) collection.

PRIVILEGED: This requires root privilege.

source

pub fn set_ctrlmode(&self, mode: CanCtrlMode, on: bool) -> Result<(), NlError>

Set or clear an individual control mode parameter.

PRIVILEGED: This requires root privilege.

source

pub fn restart_ms(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>

Gets the automatic CANbus restart time for the interface, in milliseconds.

source

pub fn set_restart_ms(&self, restart_ms: u32) -> Result<(), NlError>

Set the automatic restart milliseconds of the interface

PRIVILEGED: This requires root privilege.

source

pub fn restart(&self) -> Result<(), NlError>

Manually restart the interface.

Note that a manual restart if only permitted if automatic restart is disabled and the device is in the bus-off state. See: linux/drivers/net/can/dev/dev.c

PRIVILEGED: This requires root privilege.

Common Errors: EINVAL - The interface is down or automatic restarts are enabled EBUSY - The interface is not in a bus-off state

source

pub fn berr_counter( &self ) -> Result<Option<CanBerrCounter>, NlError<Rtm, Ifinfomsg>>

Gets the bus error counter from the interface

source

pub fn data_bit_timing( &self ) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>

Gets the data bit timing params for the interface

source

pub fn set_data_bit_timing(&self, timing: CanBitTiming) -> Result<(), NlError>

Sets the data bit timing params for the interface

PRIVILEGED: This requires root privilege.

source

pub fn set_data_bitrate<P>( &self, bitrate: u32, sample_point: P ) -> Result<(), NlError>where P: Into<Option<u32>>,

Set the data bitrate and, optionally, data sample point of this interface.

This only applies to interfaces in FD mode.

The data bitrate can not be changed if the interface is UP. It is specified in Hz (bps) while the sample point is given in tenths of a percent/

PRIVILEGED: This requires root privilege.

source

pub fn data_bit_timing_const( &self ) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>

Gets the data bit timing const params for the interface

source

pub fn termination(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>

Gets the CANbus termination for the interface

Trait Implementations§

source§

impl Debug for CanInterface

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.