pub struct IsoTpSocket { /* private fields */ }
Expand description
An ISO-TP socketcan socket.
Will be closed upon deallocation. To close manually, use std::drop::Drop
.
Internally this is just a wrapped file-descriptor.
Implementations§
Source§impl IsoTpSocket
impl IsoTpSocket
Sourcepub fn open(
ifname: &str,
rx_id: impl Into<Id>,
tx_id: impl Into<Id>,
) -> Result<Self, Error>
pub fn open( ifname: &str, rx_id: impl Into<Id>, tx_id: impl Into<Id>, ) -> Result<Self, Error>
Open a named CAN ISO-TP device.
Usually the more common case, opens a socket can device by name, such as “vcan0” or “socan0”.
Examples found in repository?
examples/isotprecv.rs (lines 4-8)
3fn main() -> Result<(), socketcan_isotp::Error> {
4 let mut tp_socket = IsoTpSocket::open(
5 "vcan0",
6 StandardId::new(0x123).expect("Invalid rx id"),
7 StandardId::new(0x321).expect("Invalid tx id"),
8 )?;
9
10 let buffer = tp_socket.read()?;
11 println!("read {} bytes", buffer.len());
12
13 for x in buffer {
14 print!("{:X?} ", x);
15 }
16
17 println!("");
18
19 Ok(())
20}
More examples
examples/isotpsend.rs (lines 5-9)
4fn main() -> Result<(), socketcan_isotp::Error> {
5 let tp_socket = IsoTpSocket::open(
6 "vcan0",
7 StandardId::new(0x321).expect("Invalid rx id"),
8 StandardId::new(0x123).expect("Invalid tx id"),
9 )?;
10
11 loop {
12 tp_socket.write(&[0xAA, 0x11, 0x22, 0x33, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF])?;
13 println!("Sent frame");
14 std::thread::sleep(Duration::from_millis(1000));
15 }
16}
examples/uds.rs (lines 12-16)
8fn main() -> Result<(), socketcan_isotp::Error> {
9 let (tx, rx) = mpsc::channel();
10
11 // Reader
12 let mut reader_tp_socket = IsoTpSocket::open(
13 "vcan0",
14 StandardId::new(0x7E8).expect("Invalid rx CAN ID"),
15 StandardId::new(0x77A).expect("Invalid tx CAN ID"),
16 )?;
17 std::thread::spawn(move || loop {
18 let buffer = reader_tp_socket.read().expect("Failed to read from socket");
19 tx.send(buffer.to_vec()).expect("Receiver deallocated");
20 });
21
22 let tp_socket = IsoTpSocket::open(
23 "vcan0",
24 StandardId::new(0x77A).expect("Invalid rx CAN ID"),
25 StandardId::new(0x7E0).expect("Invalid tx CAN ID"),
26 )?;
27
28 // 0x22 - Service Identifier for "Read data by identifier" request
29 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
30 tp_socket.write(&[0x22, 0xF1, 0x89])?;
31
32 println!("Sent read data by identifier 0xF189 - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier");
33
34 loop {
35 let recv_buffer = rx.recv().expect("Failed to receive");
36 // 0x62 - Service Identifier for "Read data by identifier" response
37 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
38 if recv_buffer[0..=2] != [0x62, 0xF1, 0x89] {
39 println!("Skipping: {:X?}", recv_buffer);
40 } else {
41 println!("Response: {:X?}", &recv_buffer[3..]);
42 }
43 }
44}
Sourcepub fn open_with_opts(
ifname: &str,
rx_id: impl Into<Id>,
tx_id: impl Into<Id>,
isotp_options: Option<IsoTpOptions>,
rx_flow_control_options: Option<FlowControlOptions>,
link_layer_options: Option<LinkLayerOptions>,
) -> Result<Self, Error>
pub fn open_with_opts( ifname: &str, rx_id: impl Into<Id>, tx_id: impl Into<Id>, isotp_options: Option<IsoTpOptions>, rx_flow_control_options: Option<FlowControlOptions>, link_layer_options: Option<LinkLayerOptions>, ) -> Result<Self, Error>
Open a named CAN ISO-TP device, passing additional options.
Usually the more common case, opens a socket can device by name, such as “vcan0” or “socan0”.
Sourcepub fn open_if(
if_index: c_int,
rx_id: impl Into<Id>,
tx_id: impl Into<Id>,
) -> Result<Self, Error>
pub fn open_if( if_index: c_int, rx_id: impl Into<Id>, tx_id: impl Into<Id>, ) -> Result<Self, Error>
Open CAN ISO-TP device device by interface number.
Opens a CAN device by kernel interface number.
Sourcepub fn open_if_with_opts(
if_index: c_int,
rx_id: impl Into<Id>,
tx_id: impl Into<Id>,
isotp_options: Option<IsoTpOptions>,
rx_flow_control_options: Option<FlowControlOptions>,
link_layer_options: Option<LinkLayerOptions>,
) -> Result<Self, Error>
pub fn open_if_with_opts( if_index: c_int, rx_id: impl Into<Id>, tx_id: impl Into<Id>, isotp_options: Option<IsoTpOptions>, rx_flow_control_options: Option<FlowControlOptions>, link_layer_options: Option<LinkLayerOptions>, ) -> Result<Self, Error>
Open CAN ISO-TP device device by interface number, passing additional options.
Opens a CAN device by kernel interface number.
Sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Change socket to non-blocking mode
Sourcepub fn read(&mut self) -> Result<&[u8]>
pub fn read(&mut self) -> Result<&[u8]>
Blocking read data
Examples found in repository?
examples/isotprecv.rs (line 10)
3fn main() -> Result<(), socketcan_isotp::Error> {
4 let mut tp_socket = IsoTpSocket::open(
5 "vcan0",
6 StandardId::new(0x123).expect("Invalid rx id"),
7 StandardId::new(0x321).expect("Invalid tx id"),
8 )?;
9
10 let buffer = tp_socket.read()?;
11 println!("read {} bytes", buffer.len());
12
13 for x in buffer {
14 print!("{:X?} ", x);
15 }
16
17 println!("");
18
19 Ok(())
20}
More examples
examples/uds.rs (line 18)
8fn main() -> Result<(), socketcan_isotp::Error> {
9 let (tx, rx) = mpsc::channel();
10
11 // Reader
12 let mut reader_tp_socket = IsoTpSocket::open(
13 "vcan0",
14 StandardId::new(0x7E8).expect("Invalid rx CAN ID"),
15 StandardId::new(0x77A).expect("Invalid tx CAN ID"),
16 )?;
17 std::thread::spawn(move || loop {
18 let buffer = reader_tp_socket.read().expect("Failed to read from socket");
19 tx.send(buffer.to_vec()).expect("Receiver deallocated");
20 });
21
22 let tp_socket = IsoTpSocket::open(
23 "vcan0",
24 StandardId::new(0x77A).expect("Invalid rx CAN ID"),
25 StandardId::new(0x7E0).expect("Invalid tx CAN ID"),
26 )?;
27
28 // 0x22 - Service Identifier for "Read data by identifier" request
29 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
30 tp_socket.write(&[0x22, 0xF1, 0x89])?;
31
32 println!("Sent read data by identifier 0xF189 - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier");
33
34 loop {
35 let recv_buffer = rx.recv().expect("Failed to receive");
36 // 0x62 - Service Identifier for "Read data by identifier" response
37 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
38 if recv_buffer[0..=2] != [0x62, 0xF1, 0x89] {
39 println!("Skipping: {:X?}", recv_buffer);
40 } else {
41 println!("Response: {:X?}", &recv_buffer[3..]);
42 }
43 }
44}
Sourcepub fn write(&self, buffer: &[u8]) -> Result<()>
pub fn write(&self, buffer: &[u8]) -> Result<()>
Blocking write a slice of data
Examples found in repository?
examples/isotpsend.rs (line 12)
4fn main() -> Result<(), socketcan_isotp::Error> {
5 let tp_socket = IsoTpSocket::open(
6 "vcan0",
7 StandardId::new(0x321).expect("Invalid rx id"),
8 StandardId::new(0x123).expect("Invalid tx id"),
9 )?;
10
11 loop {
12 tp_socket.write(&[0xAA, 0x11, 0x22, 0x33, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF])?;
13 println!("Sent frame");
14 std::thread::sleep(Duration::from_millis(1000));
15 }
16}
More examples
examples/uds.rs (line 30)
8fn main() -> Result<(), socketcan_isotp::Error> {
9 let (tx, rx) = mpsc::channel();
10
11 // Reader
12 let mut reader_tp_socket = IsoTpSocket::open(
13 "vcan0",
14 StandardId::new(0x7E8).expect("Invalid rx CAN ID"),
15 StandardId::new(0x77A).expect("Invalid tx CAN ID"),
16 )?;
17 std::thread::spawn(move || loop {
18 let buffer = reader_tp_socket.read().expect("Failed to read from socket");
19 tx.send(buffer.to_vec()).expect("Receiver deallocated");
20 });
21
22 let tp_socket = IsoTpSocket::open(
23 "vcan0",
24 StandardId::new(0x77A).expect("Invalid rx CAN ID"),
25 StandardId::new(0x7E0).expect("Invalid tx CAN ID"),
26 )?;
27
28 // 0x22 - Service Identifier for "Read data by identifier" request
29 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
30 tp_socket.write(&[0x22, 0xF1, 0x89])?;
31
32 println!("Sent read data by identifier 0xF189 - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier");
33
34 loop {
35 let recv_buffer = rx.recv().expect("Failed to receive");
36 // 0x62 - Service Identifier for "Read data by identifier" response
37 // 0xF189 - Data identifer - VehicleManufacturerECUSoftwareVersionNumberDataIdentifier
38 if recv_buffer[0..=2] != [0x62, 0xF1, 0x89] {
39 println!("Skipping: {:X?}", recv_buffer);
40 } else {
41 println!("Response: {:X?}", &recv_buffer[3..]);
42 }
43 }
44}
Trait Implementations§
Source§impl AsRawFd for IsoTpSocket
impl AsRawFd for IsoTpSocket
Source§impl Drop for IsoTpSocket
impl Drop for IsoTpSocket
Source§impl FromRawFd for IsoTpSocket
impl FromRawFd for IsoTpSocket
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Constructs a new instance of
Self
from the given raw file
descriptor. Read moreSource§impl IntoRawFd for IsoTpSocket
impl IntoRawFd for IsoTpSocket
Source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
Consumes this object, returning the raw underlying file descriptor. Read more
Auto Trait Implementations§
impl Freeze for IsoTpSocket
impl RefUnwindSafe for IsoTpSocket
impl Send for IsoTpSocket
impl Sync for IsoTpSocket
impl Unpin for IsoTpSocket
impl UnwindSafe for IsoTpSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more