summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-03 18:33:24 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2018-04-03 18:33:24 +0100
commitef9fc91c613fc038a5f160e595176fdf7b51efe0 (patch)
tree72b152e22eb4fa752785b74300657dc6a0232dca
parentf99a3974a624a8a98abf11cd5548c7462616e641 (diff)
downloadcanopied-ef9fc91c613fc038a5f160e595176fdf7b51efe0.tar.bz2
Switch to env_logger/log and clean things up a bit
-rw-r--r--Cargo.toml2
-rw-r--r--src/canstream.rs22
-rw-r--r--src/isotp.rs20
-rw-r--r--src/main.rs31
4 files changed, 32 insertions, 43 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 20247f8..cea6c29 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,7 +3,9 @@ authors = ["Daniel Silverstone <dsilvers@digital-scurf.org>"]
name = "canopied"
version = "0.1.0"
[dependencies]
+env_logger = "0.5.6"
futures = "0.1.20"
+log = "0.4.1"
mio = "0.6.14"
socketcan = "1.7.0"
time = "0.1.39"
diff --git a/src/canstream.rs b/src/canstream.rs
index ce985db..9401047 100644
--- a/src/canstream.rs
+++ b/src/canstream.rs
@@ -109,23 +109,21 @@ impl Sink for CANStream {
item: Self::SinkItem,
) -> Result<AsyncSink<Self::SinkItem>, Self::SinkError> {
if !self.flushed {
- println!("Checking poll_complete in start_send");
+ trace!("Checking poll_complete in start_send");
match self.poll_complete()? {
Async::Ready(()) => {}
Async::NotReady => return Ok(AsyncSink::NotReady(item)),
}
}
- println!("Okay, storing frame for transmission later");
+ trace!("Okay, storing frame for transmission later");
self.pending_frame = Some(item);
self.flushed = false;
- // println!("Depressingly we now need to do *something* get us scheduled");
- // self.poll_complete().and_then(|_| Ok(AsyncSink::Ready))
Ok(AsyncSink::Ready)
}
fn poll_complete(&mut self) -> Result<Async<()>, Self::SinkError> {
if self.flushed {
- println!("poll_complete already flushed, stop");
+ trace!("poll_complete already flushed, stop");
return Ok(Async::Ready(()));
}
@@ -133,36 +131,36 @@ impl Sink for CANStream {
Async::NotReady => return Ok(Async::NotReady),
Async::Ready(r) => r,
};
- println!("Ready!");
+ trace!("Ready!");
if !readiness.is_writable() {
return Ok(Async::NotReady);
}
- println!("Writable");
+ trace!("Writable");
match self.socket
.get_ref()
.get_ref()
.write_frame(&self.pending_frame.unwrap())
{
Ok(()) => {
- println!("Written, clearing pending frame");
+ trace!("Written, clearing pending frame");
self.pending_frame = None;
}
Err(e) => {
if !e.should_retry() {
- println!("Bad error");
+ trace!("Bad error");
return Err(e);
}
- println!("Retryable error");
+ trace!("Retryable error");
}
}
if self.pending_frame.is_none() {
self.flushed = true;
- println!("Okay, we finished, flushed is true");
+ trace!("Okay, we finished, flushed is true");
Ok(Async::Ready(()))
} else {
- println!("Not finished, perhaps EAGAIN?");
+ trace!("Not finished, perhaps EAGAIN?");
Ok(Async::NotReady)
}
}
diff --git a/src/isotp.rs b/src/isotp.rs
index f9a6274..4d4794a 100644
--- a/src/isotp.rs
+++ b/src/isotp.rs
@@ -77,7 +77,7 @@ impl ISOTP {
let data = frame.data();
match data[0] >> 4 {
0 => {
- println!("SingleFrame");
+ trace!("SingleFrame");
// Received a SingleFrame frame, return a Vec of the
// requisite number of bytes...
let len = (data[0] & 0xf) as usize;
@@ -88,11 +88,11 @@ impl ISOTP {
return Some((frame.id() as u16, Vec::from(sliced)));
}
1 => {
- println!("FirstFrame");
+ trace!("FirstFrame");
// Received a FirstFrame frame, we need to cancel any
// ongoing receive for this ID
if self.incoming.contains_key(&id) {
- println!("Removed partial");
+ trace!("Removed partial");
self.incoming.remove(&id);
}
// Now we need to construct an incoming packet
@@ -105,7 +105,7 @@ impl ISOTP {
return None;
}
2 => {
- println!("ConsecutiveFrame");
+ trace!("ConsecutiveFrame");
// Received a ConsecutiveFrame frame, we need to find the
// incoming which we want, and manipulate it with the incoming
// frame and hopefully we either succeed in completing packet
@@ -115,9 +115,9 @@ impl ISOTP {
let mut do_flow = false;
let mut do_delete = false;
if let Some(packet) = self.incoming.get_mut(&id) {
- println!("Found packet");
+ trace!("Found packet");
if let Ok(flow) = packet.process_more(frame) {
- println!("Processed a frame, no error");
+ trace!("Processed a frame, no error");
if packet.remaining_bytes == 0 {
// We have received the packet fully
ret = Some((id, packet.content.clone()));
@@ -130,11 +130,11 @@ impl ISOTP {
return None;
}
} else {
- println!("Error processing frame");
+ trace!("Error processing frame");
do_delete = true;
}
} else {
- println!("Didn't find a packet");
+ trace!("Didn't find a packet");
}
if do_flow {
let mut packet = self.incoming.remove(&id).unwrap();
@@ -147,7 +147,7 @@ impl ISOTP {
return ret;
}
3 => {
- println!("Oh gods, a flow control frame!");
+ trace!("Oh gods, a flow control frame!");
}
_ => {}
}
@@ -158,7 +158,7 @@ impl ISOTP {
// We queue a flow control packet here
let frame = CANFrame::new(
(packet.id as u32) - 8,
- &[0x30, packet.block_size, 0x00],
+ &[0x30, packet.block_size, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
false,
false,
).unwrap();
diff --git a/src/main.rs b/src/main.rs
index 9c79ffc..0548678 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,46 +1,35 @@
extern crate futures;
extern crate mio;
extern crate socketcan;
-extern crate time;
extern crate tokio;
+#[macro_use]
+extern crate log;
+extern crate env_logger;
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() {
+ 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| println!("error = {:?}", e))
+ .map_err(|e| error!("error = {:?}", e))
.for_each(move |frame| {
- process_frame(&frame);
+ info!("Received CAN frame: {:X}", frame);
match mytp.handle_frame(&frame) {
- None => println!("Nothing TPish"),
+ None => {}
Some((id, data)) => {
- println!("Received from {:03x} data {:?}", id, data);
+ info!("Received from {:03x} data {:?}", id, data);
}
}
while let Some(packet) = mytp.get_outgoing() {
- println!("What's more, sending {:?}", packet);
+ info!(" Sending CAN frame: {:X}", packet);
waiter.send(packet).map_err(|_| ())?;
waiter.flush().map_err(|_| ())?;
}