Skip to content

Commit

Permalink
Remove blocking calls from Driver trait
Browse files Browse the repository at this point in the history
Blocking and queuing semantics should be handled at a higher level.
  • Loading branch information
Notgnoshi committed Aug 8, 2023
1 parent 4ce5cc9 commit 63cd366
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 57 deletions.
24 changes: 2 additions & 22 deletions examples/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ struct Options {
/// Can be either a string interface name, or an integer interface index
#[clap(short, long, default_value_t = String::from("can1"))]
pub output_interface: String,

/// Whether to use blocking send()/recv()s
#[clap(short, long)]
pub blocking: bool,
}

fn create_driver(iface: &str) -> impl Driver {
Expand All @@ -36,22 +32,6 @@ fn create_driver(iface: &str) -> impl Driver {
}
}

fn read(input: &mut impl Driver, frame: &mut Frame, blocking: bool) -> Result<(), DriverReadError> {
if blocking {
input.read_blocking(frame)
} else {
input.read_nonblocking(frame)
}
}

fn write(output: &mut impl Driver, frame: &Frame, blocking: bool) -> Result<(), DriverWriteError> {
if blocking {
output.write_blocking(frame)
} else {
output.write_nonblocking(frame)
}
}

fn main() {
let opts = Options::parse();

Expand Down Expand Up @@ -84,11 +64,11 @@ fn main() {

let mut frame = Frame::default();

match read(&mut input, &mut frame, opts.blocking) {
match input.read(&mut frame) {
Ok(_) => {
tracing::info!("Read frame: {frame:?}");
tracing::info!("Attempting to write frame");
match write(&mut output, &frame, opts.blocking) {
match output.write(&frame) {
Ok(_) => tracing::info!("Wrote frame: {frame:?}"),
Err(e) => tracing::info!("Failed to write frame: {e:?}"),
}
Expand Down
5 changes: 0 additions & 5 deletions src/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ pub trait Driver {
fn open(&mut self) -> Result<(), DriverOpenError>;
fn close(&mut self) -> Result<(), DriverCloseError>;

fn read_blocking(&mut self, frame: &mut Frame) -> Result<(), DriverReadError>;
fn read_nonblocking(&mut self, frame: &mut Frame) -> Result<(), DriverReadError>;

// TODO: Do we force every driver to implement this method? Or maybe we provide a default
// implementation that they can override?
fn write_blocking(&mut self, frame: &Frame) -> Result<(), DriverWriteError>;
fn write_nonblocking(&mut self, frame: &Frame) -> Result<(), DriverWriteError>;
}
30 changes: 0 additions & 30 deletions src/driver/socketcan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,6 @@ impl Driver for SocketcanDriver {
Ok(())
}

fn read_blocking(&mut self, _frame: &mut Frame) -> Result<(), DriverReadError> {
let Some(sock) = self.sock.as_mut() else {
return Err(DriverReadError::DriverClosed);
};

loop {
// Use a timeout so that we're not calling the non-blocking read_frame() over and over
// as fast as possible.
match sock.read_frame_timeout(std::time::Duration::from_millis(100)) {
Ok(_frame) => {
// TODO: convert socketcan CanFrame to Frame
return Ok(());
}
Err(e) => match e.kind() {
std::io::ErrorKind::TimedOut => {}
_ => return Err(e.into()),
},
}
}
}
fn read_nonblocking(&mut self, _frame: &mut Frame) -> Result<(), DriverReadError> {
let Some(sock) = self.sock.as_mut() else {
return Err(DriverReadError::DriverClosed);
Expand All @@ -97,16 +77,6 @@ impl Driver for SocketcanDriver {
// TODO: Convert socketcan CanFrame to Frame.
Ok(())
}

fn write_blocking(&mut self, _frame: &Frame) -> Result<(), DriverWriteError> {
let Some(sock) = self.sock.as_mut() else {
return Err(DriverWriteError::DriverClosed);
};
// TODO: Convert Frame to socketcan CanFrame
let socketcan_frame = socketcan::CanFrame::default();
sock.write_frame_insist(&socketcan_frame)?;
Ok(())
}
fn write_nonblocking(&mut self, _frame: &Frame) -> Result<(), DriverWriteError> {
let Some(sock) = self.sock.as_mut() else {
return Err(DriverWriteError::DriverClosed);
Expand Down

0 comments on commit 63cd366

Please sign in to comment.