summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9c79ffc6eb8f24d453272228de60f08ccd080906 (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
44
45
46
47
48
49
50
extern crate futures;
extern crate mio;
extern crate socketcan;
extern crate time;
extern crate tokio;

mod canstream;
mod isotp;

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();
    let mut mytp = isotp::ISOTP::new();
    tokio::run(
        stream
            .map_err(|e| println!("error = {:?}", e))
            .for_each(move |frame| {
                process_frame(&frame);
                match mytp.handle_frame(&frame) {
                    None => println!("Nothing TPish"),
                    Some((id, data)) => {
                        println!("Received from {:03x} data {:?}", id, data);
                    }
                }
                while let Some(packet) = mytp.get_outgoing() {
                    println!("What's more, sending {:?}", packet);
                    waiter.send(packet).map_err(|_| ())?;
                    waiter.flush().map_err(|_| ())?;
                }
                Ok(())
            }),
    );
}