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
impl CanInterface
sourcepub fn open(ifname: &str) -> Result<Self, Error>
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.
sourcepub fn open_iface(if_index: u32) -> Self
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.
sourcepub fn bring_down(&self) -> Result<(), NlError>
pub fn bring_down(&self) -> Result<(), NlError>
Bring down this interface.
Use a netlink control socket to set the interface status to “down”.
sourcepub fn bring_up(&self) -> Result<(), NlError>
pub fn bring_up(&self) -> Result<(), NlError>
Bring up this interface
Brings the interface up by settings its “up” flag enabled via netlink.
sourcepub fn create_vcan(name: &str, index: Option<u32>) -> Result<Self, NlError>
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.
sourcepub fn create<I>(name: &str, index: I, kind: &str) -> Result<Self, NlError>where
I: Into<Option<u32>>,
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.
sourcepub fn delete(self) -> Result<(), (Self, NlError)>
pub fn delete(self) -> Result<(), (Self, NlError)>
Delete the interface.
PRIVILEGED: This requires root privilege.
sourcepub fn details(&self) -> Result<InterfaceDetails, NlError<Rtm, Ifinfomsg>>
pub fn details(&self) -> Result<InterfaceDetails, NlError<Rtm, Ifinfomsg>>
Attempt to query detailed information on the interface.
sourcepub fn set_mtu(&self, mtu: Mtu) -> Result<(), NlError>
pub fn set_mtu(&self, mtu: Mtu) -> Result<(), NlError>
Set the MTU of this interface.
PRIVILEGED: This requires root privilege.
sourcepub fn set_can_param<P>(
&self,
param_type: IflaCan,
param: P
) -> Result<(), NlError>where
P: ToBytes + Size,
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.
sourcepub fn can_param<P>(
&self,
param: IflaCan
) -> Result<Option<P>, NlError<Rtm, Ifinfomsg>>where
P: for<'a> FromBytes<'a> + Clone,
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.
sourcepub fn bit_rate(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
pub fn bit_rate(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
Gets the current bit rate for the interface.
sourcepub fn set_bitrate<P>(
&self,
bitrate: u32,
sample_point: P
) -> Result<(), NlError>where
P: Into<Option<u32>>,
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.
sourcepub fn bit_timing(
&self
) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>
pub fn bit_timing( &self ) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>
Gets the bit timing params for the interface
sourcepub fn set_bit_timing(&self, timing: CanBitTiming) -> Result<(), NlError>
pub fn set_bit_timing(&self, timing: CanBitTiming) -> Result<(), NlError>
Sets the bit timing params for the interface
PRIVILEGED: This requires root privilege.
sourcepub fn bit_timing_const(
&self
) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>
pub fn bit_timing_const( &self ) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>
Gets the bit timing const data for the interface
sourcepub fn clock(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
pub fn clock(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
Gets the clock frequency for the interface
sourcepub fn state(&self) -> Result<Option<CanState>, NlError<Rtm, Ifinfomsg>>
pub fn state(&self) -> Result<Option<CanState>, NlError<Rtm, Ifinfomsg>>
Gets the state of the interface
sourcepub fn set_full_ctrlmode(&self, ctrlmode: can_ctrlmode) -> Result<(), NlError>
👎Deprecated since 3.2.0: Use set_ctrlmodes
instead
pub fn set_full_ctrlmode(&self, ctrlmode: can_ctrlmode) -> Result<(), NlError>
set_ctrlmodes
insteadSet the full control mode (bit) collection.
PRIVILEGED: This requires root privilege.
sourcepub fn set_ctrlmodes<M>(&self, ctrlmode: M) -> Result<(), NlError>where
M: Into<CanCtrlModes>,
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.
sourcepub fn set_ctrlmode(&self, mode: CanCtrlMode, on: bool) -> Result<(), NlError>
pub fn set_ctrlmode(&self, mode: CanCtrlMode, on: bool) -> Result<(), NlError>
Set or clear an individual control mode parameter.
PRIVILEGED: This requires root privilege.
sourcepub fn restart_ms(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
pub fn restart_ms(&self) -> Result<Option<u32>, NlError<Rtm, Ifinfomsg>>
Gets the automatic CANbus restart time for the interface, in milliseconds.
sourcepub fn set_restart_ms(&self, restart_ms: u32) -> Result<(), NlError>
pub fn set_restart_ms(&self, restart_ms: u32) -> Result<(), NlError>
Set the automatic restart milliseconds of the interface
PRIVILEGED: This requires root privilege.
sourcepub fn restart(&self) -> Result<(), NlError>
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
sourcepub fn berr_counter(
&self
) -> Result<Option<CanBerrCounter>, NlError<Rtm, Ifinfomsg>>
pub fn berr_counter( &self ) -> Result<Option<CanBerrCounter>, NlError<Rtm, Ifinfomsg>>
Gets the bus error counter from the interface
sourcepub fn data_bit_timing(
&self
) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>
pub fn data_bit_timing( &self ) -> Result<Option<CanBitTiming>, NlError<Rtm, Ifinfomsg>>
Gets the data bit timing params for the interface
sourcepub fn set_data_bit_timing(&self, timing: CanBitTiming) -> Result<(), NlError>
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.
sourcepub fn set_data_bitrate<P>(
&self,
bitrate: u32,
sample_point: P
) -> Result<(), NlError>where
P: Into<Option<u32>>,
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.
sourcepub fn data_bit_timing_const(
&self
) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>
pub fn data_bit_timing_const( &self ) -> Result<Option<CanBitTimingConst>, NlError<Rtm, Ifinfomsg>>
Gets the data bit timing const params for the interface