summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 36307e603add7b6f10f20154019499514e2a41dc (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
extern crate futures;
extern crate mio;
extern crate socketcan;
extern crate tokio;
#[macro_use]
extern crate log;
extern crate env_logger;

mod canstream;
mod isotp;

use tokio::prelude::*;

fn main() {
    env_logger::init();

    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| error!("error = {:?}", e))
            .for_each(move |frame| {
                info!("Received CAN frame: {:X}", frame);
                match mytp.handle_frame(&frame) {
                    None => {}
                    Some((id, data)) => {
                        info!("Received from {:03x} data {:?}", id, data);
                        mytp.send_packet(0x123, &data);
                    }
                }
                while let Some(packet) = mytp.get_txqueue() {
                    info!(" Sending CAN frame: {:X}", packet);
                    waiter.send(packet).map_err(|_| ())?;
                    waiter.flush().map_err(|_| ())?;
                }
                Ok(())
            }),
    );
}