Skip to content

Commit b4f65ac

Browse files
committed
Receive timeout option added.
1 parent a0b543a commit b4f65ac

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use bitflags::bitflags;
4545
pub use embedded_can::{ExtendedId, Id, StandardId};
4646
use libc::{
4747
bind, c_int, c_short, c_void, close, fcntl, read, setsockopt, sockaddr, socket, write, F_GETFL,
48-
F_SETFL, O_NONBLOCK, SOCK_DGRAM,
48+
F_SETFL, O_NONBLOCK, SOCK_DGRAM, SOL_SOCKET, SO_RCVTIMEO,
4949
};
5050
use nix::net::if_::if_nametoindex;
5151
use std::convert::TryFrom;
@@ -424,6 +424,7 @@ impl IsoTpSocket {
424424
Some(IsoTpOptions::default()),
425425
Some(FlowControlOptions::default()),
426426
Some(LinkLayerOptions::default()),
427+
None,
427428
)
428429
}
429430

@@ -438,6 +439,7 @@ impl IsoTpSocket {
438439
isotp_options: Option<IsoTpOptions>,
439440
rx_flow_control_options: Option<FlowControlOptions>,
440441
link_layer_options: Option<LinkLayerOptions>,
442+
rx_timeout: Option<std::time::Duration>,
441443
) -> Result<Self, Error> {
442444
let if_index = if_nametoindex(ifname)?;
443445
Self::open_if_with_opts(
@@ -447,6 +449,7 @@ impl IsoTpSocket {
447449
isotp_options,
448450
rx_flow_control_options,
449451
link_layer_options,
452+
rx_timeout,
450453
)
451454
}
452455

@@ -465,6 +468,7 @@ impl IsoTpSocket {
465468
Some(IsoTpOptions::default()),
466469
Some(FlowControlOptions::default()),
467470
Some(LinkLayerOptions::default()),
471+
None,
468472
)
469473
}
470474

@@ -478,6 +482,7 @@ impl IsoTpSocket {
478482
isotp_options: Option<IsoTpOptions>,
479483
rx_flow_control_options: Option<FlowControlOptions>,
480484
link_layer_options: Option<LinkLayerOptions>,
485+
rx_timeout: Option<std::time::Duration>,
481486
) -> Result<Self, Error> {
482487
let rx_id = match rx_id.into() {
483488
Id::Standard(standard_id) => standard_id.as_raw() as u32,
@@ -523,6 +528,26 @@ impl IsoTpSocket {
523528
}
524529
}
525530

531+
// Set receive timeout
532+
if let Some(timeout) = rx_timeout {
533+
let tv = nix::sys::time::TimeVal::new(
534+
timeout.as_secs() as i64,
535+
timeout.subsec_micros() as i64,
536+
);
537+
let err = unsafe {
538+
setsockopt(
539+
sock_fd,
540+
SOL_SOCKET,
541+
SO_RCVTIMEO,
542+
&tv as *const _ as *const c_void,
543+
size_of::<nix::sys::time::TimeVal>().try_into().unwrap(),
544+
)
545+
};
546+
if err == -1 {
547+
return Err(Error::from(io::Error::last_os_error()));
548+
}
549+
}
550+
526551
// Set FlowControlOptions
527552
if let Some(rx_flow_control_options) = rx_flow_control_options {
528553
let rx_flow_control_options_ptr: *const c_void =

0 commit comments

Comments
 (0)