#can #socketcan #bcm

tokio-socketcan-bcm

Asynchronous Linux SocketCAN - Broadcast Manager support (BCM) with tokio

3 unstable releases

✓ Uses Rust 2018 edition

0.2.0 Mar 19, 2019
0.1.1 Mar 13, 2019
0.1.0 Mar 12, 2019

#3 in #can

Download history 114/week @ 2019-03-08 163/week @ 2019-03-15 76/week @ 2019-03-22 2/week @ 2019-03-29 48/week @ 2019-04-05 13/week @ 2019-04-12 41/week @ 2019-04-19 17/week @ 2019-04-26

118 downloads per month
Used in 1 crate

MIT license

27KB
484 lines

tokio-socketcan-bcm

LICENSE VERSION docs

The Broadcast Manager protocol provides a command based configuration interface to filter and send (e.g. cyclic) CAN messages in kernel space. Filtering messages in kernel space may significantly reduce the load in an application.

A BCM socket is not intended for sending individual CAN frames. To send invidiual frames use the tokio-socketcan crate.

This crate would not have been possible without the socketcan crate.

Example 1

use futures::stream::Stream;
use std::time;
use tokio_socketcan_bcm::*;
fn main() {
    let socket = BCMSocket::open_nb("vcan0").unwrap();
    // Throttle messages in kernel space to max every 5 seconds
    let ival = time::Duration::from_secs(5);
    let f = socket
        .filter_id_incoming_frames(0x123.into(), ival, ival)
        .unwrap()
        .map_err(|err| eprintln!("IO error {:?}", err))
        .for_each(|frame| {
            println!("Frame {:?}", frame);
            Ok(())
        });
    tokio::run(f);
}

Example 2 (async/await)

Notice: async/await currently requires nightly rust and the tokio async-await-preview feature.

#![feature(await_macro, async_await, futures_api)]
#[macro_use]
extern crate tokio;
use std::time;
use tokio::prelude::*;
use tokio_socketcan_bcm::*;
fn main() {
    tokio::run_async(
        async {
            let socket = BCMSocket::open_nb("vcan0").unwrap();
            let ival = time::Duration::from_millis(0);
            // create a stream of messages that filters by the can frame id 0x123
            let mut can_frame_stream = socket
                .filter_id_incoming_frames(0x123.into(), ival, ival)
                .unwrap();
            while let Some(frame) = await!(can_frame_stream.next()) {
                println!("Frame {:?}", frame);
                ()
            }
        },
    );
}

Dependencies

~3MB
~53K SLoC