@@ -38,8 +38,9 @@ impl<'a> CanHandler<'a> {
38
38
let _ = can_if. bring_down ( ) ;
39
39
let _ = can_if. set_bitrate ( self . bitrate ( ) . unwrap ( ) , None ) ;
40
40
let _ = can_if. bring_up ( ) ;
41
- let can_socket = self . open_can_socket ( ) ;
42
- self . process_ui_events ( can_socket, can_if) ;
41
+ let tx_can_socket = self . open_can_socket ( ) ;
42
+ let rx_can_socket = self . open_can_socket ( ) ;
43
+ self . process_ui_events ( tx_can_socket, rx_can_socket, can_if) ;
43
44
}
44
45
#[ cfg( target_os = "windows" ) ]
45
46
self . process_ui_events ( dbc) ;
@@ -63,9 +64,54 @@ impl<'a> CanHandler<'a> {
63
64
}
64
65
}
65
66
#[ cfg( target_os = "linux" ) ]
66
- fn process_ui_events ( & mut self , can_socket : CanSocket , can_if : CanInterface ) {
67
+ fn process_ui_events (
68
+ & mut self ,
69
+ tx_can_socket : CanSocket ,
70
+ rx_can_socket : CanSocket ,
71
+ can_if : CanInterface ,
72
+ ) {
73
+ use socketcan:: { ExtendedId , StandardId } ;
74
+
67
75
let mut start_bus_load = Instant :: now ( ) ;
68
76
let mut total_bits = 0 ;
77
+ let _ = self . ui_handle . upgrade_in_event_loop ( move |ui| {
78
+ ui. on_can_transmit ( move |is_extended, can_id, can_data| {
79
+ match Self :: convert_hex_string_u32 ( & can_id) {
80
+ Ok ( id) => match Self :: convert_hex_string_arr ( & can_data) {
81
+ Ok ( data) => {
82
+ if is_extended {
83
+ match ExtendedId :: new ( id) {
84
+ Some ( id) => {
85
+ let can_frame = CanFrame :: new ( id, & data) . unwrap ( ) ;
86
+ let _ = tx_can_socket. write_frame ( & can_frame) ;
87
+ }
88
+ None => {
89
+ println ! ( "Invalid CAN extended ID {}" , id)
90
+ }
91
+ }
92
+ } else {
93
+ match StandardId :: new ( id as u16 ) {
94
+ Some ( id) => {
95
+ let can_frame = CanFrame :: new ( id, & data) . unwrap ( ) ;
96
+ let _ = tx_can_socket. write_frame ( & can_frame) ;
97
+ }
98
+ None => {
99
+ println ! ( "Invalid CAN standard ID {}" , id)
100
+ }
101
+ }
102
+ } ;
103
+ }
104
+ Err ( e) => {
105
+ println ! ( "Failed to parse can data {}, error {}" , can_data, e) ;
106
+ }
107
+ } ,
108
+ Err ( e) => {
109
+ println ! ( "Failed to parse can id {}, error: {}" , can_id, e) ;
110
+ }
111
+ }
112
+ } ) ;
113
+ } ) ;
114
+
69
115
loop {
70
116
let bus_state = match can_if. state ( ) . unwrap ( ) . unwrap ( ) {
71
117
socketcan:: nl:: CanState :: ErrorActive => "ERR_ACTIVE" ,
@@ -103,7 +149,7 @@ impl<'a> CanHandler<'a> {
103
149
}
104
150
}
105
151
}
106
- if let Ok ( frame) = can_socket . read_frame ( ) {
152
+ if let Ok ( frame) = rx_can_socket . read_frame ( ) {
107
153
let _ = self . can_tx . send ( frame) ;
108
154
total_bits += ( frame. len ( ) + 6 ) * 8 ; // Data length + overhead (approximation)
109
155
let frame_id = frame. raw_id ( ) & !0x80000000 ;
@@ -293,6 +339,30 @@ impl<'a> CanHandler<'a> {
293
339
padded_data
294
340
}
295
341
342
+ fn convert_hex_string_u32 ( hex_str : & str ) -> Result < u32 , String > {
343
+ // Attempt to parse the hex string as a u32
344
+ u32:: from_str_radix ( hex_str, 16 ) . map_err ( |e| format ! ( "Failed to convert to u32: {}" , e) )
345
+ }
346
+
347
+ fn convert_hex_string_arr ( hex_str : & str ) -> Result < Vec < u8 > , String > {
348
+ // Remove any whitespace from the input string
349
+ let hex_str = hex_str. trim ( ) ;
350
+
351
+ // Ensure the string has an even length
352
+ if hex_str. len ( ) % 2 != 0 {
353
+ return Err ( "Hex string must have an even number of characters" . to_string ( ) ) ;
354
+ }
355
+
356
+ // Convert the string into a vector of u8 bytes
357
+ ( 0 ..hex_str. len ( ) )
358
+ . step_by ( 2 )
359
+ . map ( |i| {
360
+ u8:: from_str_radix ( & hex_str[ i..i + 2 ] , 16 )
361
+ . map_err ( |e| format ! ( "Failed to convert to u8: {}" , e) )
362
+ } )
363
+ . collect ( )
364
+ }
365
+
296
366
fn array_to_hex_string ( data : & [ u8 ] ) -> String {
297
367
// Preallocate space for efficiency
298
368
let mut hex_string = String :: with_capacity ( data. len ( ) * 3 ) ;
0 commit comments