@@ -45,7 +45,7 @@ use bitflags::bitflags;
45
45
pub use embedded_can:: { ExtendedId , Id , StandardId } ;
46
46
use libc:: {
47
47
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 ,
49
49
} ;
50
50
use nix:: net:: if_:: if_nametoindex;
51
51
use std:: convert:: TryFrom ;
@@ -424,6 +424,7 @@ impl IsoTpSocket {
424
424
Some ( IsoTpOptions :: default ( ) ) ,
425
425
Some ( FlowControlOptions :: default ( ) ) ,
426
426
Some ( LinkLayerOptions :: default ( ) ) ,
427
+ None ,
427
428
)
428
429
}
429
430
@@ -438,6 +439,7 @@ impl IsoTpSocket {
438
439
isotp_options : Option < IsoTpOptions > ,
439
440
rx_flow_control_options : Option < FlowControlOptions > ,
440
441
link_layer_options : Option < LinkLayerOptions > ,
442
+ rx_timeout : Option < std:: time:: Duration > ,
441
443
) -> Result < Self , Error > {
442
444
let if_index = if_nametoindex ( ifname) ?;
443
445
Self :: open_if_with_opts (
@@ -447,6 +449,7 @@ impl IsoTpSocket {
447
449
isotp_options,
448
450
rx_flow_control_options,
449
451
link_layer_options,
452
+ rx_timeout,
450
453
)
451
454
}
452
455
@@ -465,6 +468,7 @@ impl IsoTpSocket {
465
468
Some ( IsoTpOptions :: default ( ) ) ,
466
469
Some ( FlowControlOptions :: default ( ) ) ,
467
470
Some ( LinkLayerOptions :: default ( ) ) ,
471
+ None ,
468
472
)
469
473
}
470
474
@@ -478,6 +482,7 @@ impl IsoTpSocket {
478
482
isotp_options : Option < IsoTpOptions > ,
479
483
rx_flow_control_options : Option < FlowControlOptions > ,
480
484
link_layer_options : Option < LinkLayerOptions > ,
485
+ rx_timeout : Option < std:: time:: Duration > ,
481
486
) -> Result < Self , Error > {
482
487
let rx_id = match rx_id. into ( ) {
483
488
Id :: Standard ( standard_id) => standard_id. as_raw ( ) as u32 ,
@@ -523,6 +528,26 @@ impl IsoTpSocket {
523
528
}
524
529
}
525
530
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
+
526
551
// Set FlowControlOptions
527
552
if let Some ( rx_flow_control_options) = rx_flow_control_options {
528
553
let rx_flow_control_options_ptr: * const c_void =
0 commit comments