Skip to main content

CanBus

Struct CanBus 

Source
pub struct CanBus { /* private fields */ }
Expand description

One node’s place on a CAN bus, shared by every clone of this handle.

§Examples

use std::time::Duration;

use pamoja_can::bus::{CanBus, Filter};
use pamoja_can::{Frame, J1939Id, Signals};

// EEC1 carries engine speed, and ET1 the engine's temperatures.
const EEC1: u32 = 61_444;
const ET1: u32 = 65_262;

let engine = CanBus::simulated();
let gateway = engine.join()?;
gateway.set_filters(&[Filter::pgn(EEC1)])?;

// The gateway keeps engine speed and lets the engine's other groups go by. Each frame
// here reports nothing yet, every signal marked not available.
let nothing = Signals::new();
let speed = J1939Id::broadcast(3, EEC1, 0).to_id();
let temperatures = J1939Id::broadcast(6, ET1, 0).to_id();
engine.send(&Frame::new(temperatures, nothing.as_bytes())?)?;
engine.send(&Frame::new(speed, nothing.as_bytes())?)?;

let heard = gateway.receive(Duration::from_millis(10))?.expect("engine speed");
assert_eq!(heard.id(), speed);
assert_eq!(gateway.receive(Duration::from_millis(10))?, None);

Implementations§

Source§

impl CanBus

Source

pub fn open(interface: &str) -> Result<CanBus, OpenError>

Opens a kernel CAN interface through SocketCAN, as one node on its bus.

The interface is brought up and given its bit rate outside the program, with ip link set can0 up type can bitrate 250000. The node takes CAN FD frames too where the interface runs CAN FD.

§Arguments
  • interface - the interface: can0 for the first controller, vcan0 for a virtual one.
§Returns

The node.

§Errors

OpenError::Unsupported anywhere but Linux, or in a build without the linux feature, and OpenError::Device when the interface does not exist or cannot be bound.

§Examples
use pamoja_can::bus::CanBus;

let bus = CanBus::open("can0")?;
Source

pub fn simulated() -> CanBus

A new bus inside the program, with this node the first on it.

§Returns

The node.

Source

pub fn join(&self) -> Result<CanBus, OpenError>

Puts another node on the same bus: another socket on the same interface, or another node on the same simulated bus. It hears what this node sends, and this node hears it.

§Returns

The new node.

§Errors

OpenError::Device when the kernel refuses another socket on the interface.

Source

pub fn kind(&self) -> CanBusKind

Returns what the bus is.

Source

pub fn interface(&self) -> Option<String>

Returns the kernel interface the node is on, or None on a simulated bus.

Source

pub fn send(&self, frame: &Frame) -> Result<(), BusError>

Sends a frame to every other node on the bus.

§Arguments
  • frame - the frame.
§Errors

BusError::Device when the kernel refuses the frame or the interface fails.

Source

pub fn receive(&self, timeout: Duration) -> Result<Option<Frame>, BusError>

Takes the next frame this node keeps, waiting up to timeout for one when none has arrived.

On a simulated bus nothing waits: with no frame there it returns at once, and the timeout is added to CanBus::waited_micros.

§Arguments
  • timeout - how long to wait.
§Returns

The frame, or None when the timeout passed with nothing.

§Errors

BusError::Device when the interface fails.

Source

pub fn set_filters(&self, filters: &[Filter]) -> Result<(), BusError>

Keeps only the frames that pass at least one of the filters, from now on.

An empty list keeps nothing, as SocketCAN’s does; clear_filters keeps everything again.

§Arguments
  • filters - the filters, at most 512, the most SocketCAN takes.
§Errors

BusError::Device when the kernel refuses the filters.

Source

pub fn clear_filters(&self) -> Result<(), BusError>

Keeps every frame again, as a node does when it joins.

§Errors

BusError::Device when the kernel refuses the change.

Source

pub fn sent(&self) -> usize

Returns how many frames this node and its clones have sent.

Source

pub fn received(&self) -> usize

Returns how many frames this node and its clones have received.

Source

pub fn waited_micros(&self) -> u64

Returns how long receives on this node have waited without a frame, in microseconds, whether or not the process slept through it.

Trait Implementations§

Source§

impl Clone for CanBus

Source§

fn clone(&self) -> CanBus

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CanBus

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.