summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs42
1 files changed, 41 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..6092e8d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,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() {
- println!("Hello, world!");
+ 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(())
+ }
+ }),
+ );
}