summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 6092e8d1044f5642089c9a2ea0762cff8fec1344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
extern crate mio;
extern crate socketcan;
extern crate tokio;

extern crate time;

mod canstream;

use tokio::prelude::*;

fn process_frame(frame: socketcan::CANFrame) {
    let ts = time::now_utc().to_timespec();
    print!(
        "({}.{:06}) {} {:03x}#",
        ts.sec,
        ts.nsec / 1000,
        "vcan0",
        frame.id()
    );
    for byte in frame.data().iter() {
        print!("{:02x}", byte);
    }
    println!("");
}

fn main() {
    let (sink, stream) = canstream::CANStream::from_name("vcan0").unwrap().split();
    let mut waiter = sink.wait();
    tokio::run(
        stream
            .map_err(|e| println!("error = {:?}", e))
            .for_each(move |frame| {
                process_frame(frame);
                if frame.id() == 0x7E4 {
                    let reply = socketcan::CANFrame::new(0x123, &[0; 8], false, false).unwrap();
                    waiter.send(reply).map_err(|_| ())?;
                    waiter.flush().map_err(|_| ())
                } else {
                    Ok(())
                }
            }),
    );
}