@@ -55,6 +55,7 @@ extern crate nix;
55
55
extern crate try_from;
56
56
57
57
mod err;
58
+ use embedded_hal:: can:: Id ;
58
59
pub use err:: { CanError , CanErrorDecodingFailure } ;
59
60
pub mod dump;
60
61
mod nl;
@@ -523,7 +524,7 @@ impl Drop for CanSocket {
523
524
}
524
525
}
525
526
526
- impl embedded_hal:: can:: Transmitter for CanSocket {
527
+ impl embedded_hal:: can:: Can for CanSocket {
527
528
type Frame = CanFrame ;
528
529
type Error = io:: Error ;
529
530
fn transmit (
@@ -538,6 +539,15 @@ impl embedded_hal::can::Transmitter for CanSocket {
538
539
}
539
540
} )
540
541
}
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
+ }
541
551
}
542
552
543
553
/// CanFrame
@@ -674,30 +684,41 @@ impl fmt::UpperHex for CanFrame {
674
684
}
675
685
676
686
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| ( ) )
682
693
}
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| ( ) )
685
700
}
701
+
686
702
fn is_extended ( & self ) -> bool {
687
703
self . is_extended ( )
688
704
}
689
705
fn is_remote_frame ( & self ) -> bool {
690
706
self . is_rtr ( )
691
707
}
692
- fn id ( & self ) -> u32 {
693
- self . id ( )
694
- }
708
+
695
709
fn dlc ( & self ) -> usize {
696
710
self . _data_len as usize
697
711
}
698
712
fn data ( & self ) -> & [ u8 ] {
699
713
self . data ( )
700
714
}
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
+ }
701
722
}
702
723
703
724
/// CanFilter
0 commit comments