summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a0a4b7c5276f77944d647801ff3b3abb08e6a761 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
extern crate futures;
extern crate mio;
extern crate socketcan;
extern crate tokio;
#[macro_use]
extern crate log;
extern crate env_logger;

extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_yaml;

mod canstream;
mod isotp;
mod definitions;

use tokio::prelude::*;
use std::env::args;
use futures::sync::mpsc;
use tokio::timer::Delay;
use std::time::{Duration, Instant};

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

    let raw_defs = definitions::RawECUEntry::get();

    info!("Read {} raw ecu entries", raw_defs.len());

    let world = futures::future::lazy(move || {
        let args: Vec<String> = args().collect();
        let iface = if args.len() > 1 {
            args[1].clone()
        } else {
            "vcan0".into()
        };
        info!("Connecting Canopied to {}", iface);
        let (sink, stream) = isotp::ISOTP::new(&iface).split();
        let (sender, receiver) = mpsc::channel(100);

        trace!("Spawning ISOTP forwarder");

        tokio::spawn(
            receiver
                .map(|f| {
                    trace!("Forwarding into ISOTP {:?}", f);
                    f
                })
                .forward(sink.sink_map_err(|_| ()))
                .map(|_| ()),
        );

        let mut counter = 1;
        for def in &raw_defs {
            let req = def.request();
            let dest = req.sendto();
            let packet = req.packet().to_vec();
            let mut targ = sender.clone();
            let when = Instant::now() + Duration::from_millis(counter * 5000);
            info!(
                "Spawning read for {} ({}) at {:?}",
                def.name(),
                def.description(),
                when
            );
            tokio::spawn(Delay::new(when).map_err(|_| ()).and_then(move |_| {
                targ.send((dest, packet))
                    .wait()
                    .and_then(|mut s| {
                        s.close().unwrap();
                        Ok(())
                    })
                    .map_err(|_| ())
            }));
            counter += 1;
        }

        tokio::spawn(
            stream
                .map_err(|e| error!("error = {:?}", e))
                .for_each(move |frame| {
                    info!("Received ISO-TP frame: {:?}", frame);

                    Ok(())
                }),
        )
    });
    tokio::run(world);
}