Skip to content

Commit 9b69cc0

Browse files
committed
Update embedded-hal implementation
1 parent 4e03fb2 commit 9b69cc0

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/lib.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern crate nix;
5555
extern crate try_from;
5656

5757
mod err;
58+
use embedded_hal::can::Id;
5859
pub use err::{CanError, CanErrorDecodingFailure};
5960
pub mod dump;
6061
mod nl;
@@ -523,7 +524,7 @@ impl Drop for CanSocket {
523524
}
524525
}
525526

526-
impl embedded_hal::can::Transmitter for CanSocket {
527+
impl embedded_hal::can::Can for CanSocket {
527528
type Frame = CanFrame;
528529
type Error = io::Error;
529530
fn transmit(
@@ -538,6 +539,15 @@ impl embedded_hal::can::Transmitter for CanSocket {
538539
}
539540
})
540541
}
542+
fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
543+
self.read_frame().map_err(|io_err| {
544+
if io_err.kind() == io::ErrorKind::WouldBlock {
545+
nb::Error::WouldBlock
546+
} else {
547+
nb::Error::Other(io_err)
548+
}
549+
})
550+
}
541551
}
542552

543553
/// CanFrame
@@ -674,30 +684,41 @@ impl fmt::UpperHex for CanFrame {
674684
}
675685

676686
impl embedded_hal::can::Frame for CanFrame {
677-
fn new_standard(id: u32, data: &[u8]) -> Self {
678-
CanFrame::new(id, data, false, false).unwrap()
679-
}
680-
fn new_extended(id: u32, data: &[u8]) -> Self {
681-
CanFrame::new(id, data, false, false).unwrap()
687+
fn new(id: Id, data: &[u8]) -> Result<Self, ()> {
688+
let id = match id {
689+
Id::Standard(x) => x,
690+
Id::Extended(x) => x,
691+
};
692+
CanFrame::new(id, data, false, false).map_err(|_err| ())
682693
}
683-
fn with_rtr(&mut self, dlc: usize) -> &mut Self {
684-
unimplemented!()
694+
fn new_remote(id: Id, dlc: usize) -> Result<Self, ()> {
695+
let id = match id {
696+
Id::Standard(x) => x,
697+
Id::Extended(x) => x,
698+
};
699+
CanFrame::new(id, &[], true, false).map_err(|_err| ())
685700
}
701+
686702
fn is_extended(&self) -> bool {
687703
self.is_extended()
688704
}
689705
fn is_remote_frame(&self) -> bool {
690706
self.is_rtr()
691707
}
692-
fn id(&self) -> u32 {
693-
self.id()
694-
}
708+
695709
fn dlc(&self) -> usize {
696710
self._data_len as usize
697711
}
698712
fn data(&self) -> &[u8] {
699713
self.data()
700714
}
715+
fn id(&self) -> embedded_hal::can::Id {
716+
if self.is_extended() {
717+
Id::Extended(self.id())
718+
} else {
719+
Id::Standard(self.id())
720+
}
721+
}
701722
}
702723

703724
/// CanFilter

0 commit comments

Comments
 (0)