diff --git a/src/event_handler/init.rs b/src/event_handler/init.rs new file mode 100644 index 0000000..6306290 --- /dev/null +++ b/src/event_handler/init.rs @@ -0,0 +1,106 @@ +use crate::slint_generatedAppWindow::{socket_info, AppWindow}; +#[cfg(target_os = "windows")] +use pcan_basic::hw::attached_channels as available_interfaces; +use slint::{ComponentHandle, ModelRc, SharedString, VecModel, Weak}; +#[cfg(target_os = "linux")] +use socketcan::available_interfaces; +use std::{process::exit, time::Duration}; +pub struct Init<'a> { + pub ui_handle: &'a Weak, +} + +impl<'a> Init<'a> { + pub fn run(&self) { + let mut previous_interfaces = Vec::default(); + loop { + match available_interfaces() { + Ok(interface) => { + if interface.is_empty() { + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + let socket_info = socket_info { + index: ModelRc::new(VecModel::from(Vec::default())), + name: ModelRc::new(VecModel::from(Vec::default())), + }; + ui.set_can_sockets(socket_info); + ui.set_init_string(SharedString::from("No CAN device found !")); + }); + } else { + #[cfg(target_os = "linux")] + if previous_interfaces != interface { + let interface_clone = interface.clone(); + previous_interfaces = interface.clone(); + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + ui.set_init_string(SharedString::from(format!( + "Found {} CAN devices\n Please select your device ", + interface.len() + ))); + }); + + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + let convert_shared_string: Vec = interface_clone + .into_iter() + .map(SharedString::from) + .collect(); + let socket_info = socket_info { + index: ModelRc::new(VecModel::from(Vec::default())), + name: ModelRc::new(VecModel::from(convert_shared_string)), + }; + ui.set_can_sockets(socket_info); + }); + } + #[cfg(target_os = "windows")] + { + let mut interface_names = Vec::default(); + let mut interface_index = Vec::default(); + let mut count = 0; + for channel in channels { + let interface_name = SharedString::from(format!( + "{}(0x{:02X})", + channel.device_name(), + channel.channel_information.device_id + )); + interface_names.push(socket_name); + interface_index + .push(channel.channel_information.channel_handle as i32); + count += 1; + } + if previous_interfaces != interface_names { + previous_interfaces = interface_names.clone(); + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + ui.set_init_string(SharedString::from(format!( + "Found {} CAN devices\n Please select your device ", + count + ))); + let socket_info = socket_info { + index: ModelRc::new(VecModel::from(interface_index)), + name: ModelRc::new(VecModel::from(interface_names)), + }; + ui.set_can_sockets(socket_info); + }); + } + } + } + } + Err(e) => { + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + ui.set_init_string(SharedString::from(format!( + "Can't get device list: {}", + e + ))); + let socket_info = socket_info { + index: ModelRc::new(VecModel::from(Vec::default())), + name: ModelRc::new(VecModel::from(Vec::default())), + }; + ui.set_can_sockets(socket_info); + }); + } + }; + let _ = self.ui_handle.upgrade_in_event_loop(move |ui| { + if !ui.window().is_visible() { + exit(1); + } + }); + std::thread::sleep(Duration::from_micros(50)); + } + } +} diff --git a/src/event_handler/mod.rs b/src/event_handler/mod.rs index a6fd7fd..24f618d 100644 --- a/src/event_handler/mod.rs +++ b/src/event_handler/mod.rs @@ -1,34 +1,13 @@ pub(crate) mod can_handler; pub(crate) mod dbc_file; +pub(crate) mod init; pub(crate) mod packet_filter; pub use can_handler::CanHandler; pub use dbc_file::DBCFile; +pub use init::Init; pub use packet_filter::PacketFilter; -#[cfg(target_os = "windows")] -use pcan_basic::socket::Baudrate; use slint::Color; const ODD_COLOR: Color = Color::from_rgb_u8(0x18, 0x1c, 0x27); const EVEN_COLOR: Color = Color::from_rgb_u8(0x13, 0x16, 0x1f); - -#[cfg(target_os = "windows")] -pub fn p_can_bitrate(bitrate: &str) -> Option { - match bitrate { - "1 Mbit/s" => Some(Baudrate::Baud1M), - "800 kbit/s" => Some(Baudrate::Baud800K), - "500 kbit/s" => Some(Baudrate::Baud500K), - "250 kbit/s" => Some(Baudrate::Baud250K), - "125 kbit/s" => Some(Baudrate::Baud125K), - "100 kbit/s" => Some(Baudrate::Baud100K), - "95.238 kbit/s" => Some(Baudrate::Baud95K), - "83.333 kbit/s" => Some(Baudrate::Baud83), - "50 kbit/s" => Some(Baudrate::Baud50K), - "47.619 kbit/s" => Some(Baudrate::Baud47K), - "33.333 kbit/s" => Some(Baudrate::Baud33K), - "20 kbit/s" => Some(Baudrate::Baud20K), - "10 kbit/s" => Some(Baudrate::Baud10K), - "5 kbit/s" => Some(Baudrate::Baud5K), - _ => None, - } -} diff --git a/src/main.rs b/src/main.rs index c6c220f..7f43dee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,19 @@ use std::io; -use std::process::exit; use std::sync::mpsc; use std::sync::{Arc, Mutex}; -use std::time::Duration; mod event_handler; use can_dbc::DBC; +use event_handler::{CanHandler, DBCFile, Init, PacketFilter}; #[cfg(target_os = "windows")] -use event_handler::p_can_bitrate; -use event_handler::{CanHandler, DBCFile, PacketFilter}; -#[cfg(target_os = "windows")] -use pcan_basic::{bus::UsbBus, hw::attached_channels, socket::usb::UsbCanSocket}; +use pcan_basic::{ + bus::UsbBus, hw::attached_channels as available_interfaces, socket::usb::UsbCanSocket, +}; #[cfg(target_os = "linux")] use privilege_rs::privilege_request; #[cfg(target_os = "windows")] use slint::Model; -use slint::{ModelRc, SharedString, VecModel}; -#[cfg(target_os = "linux")] -use socketcan::available_interfaces; +use slint::SharedString; #[cfg(target_os = "windows")] use winapi::um::wincon::FreeConsole; @@ -39,125 +35,11 @@ async fn main() -> io::Result<()> { // Find available socket CAN let ui_handle = ui.as_weak(); - #[cfg(target_os = "linux")] - tokio::spawn(async move { - let mut previous_interfaces = Vec::default(); - loop { - match available_interfaces() { - Ok(interface) => { - if interface.is_empty() { - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - let socket_info = socket_info { - index: ModelRc::new(VecModel::from(Vec::default())), - name: ModelRc::new(VecModel::from(Vec::default())), - }; - ui.set_can_sockets(socket_info); - ui.set_init_string(SharedString::from("No CAN device found !")); - }); - } else if previous_interfaces != interface { - let interface_clone = interface.clone(); - previous_interfaces = interface.clone(); - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - ui.set_init_string(SharedString::from(format!( - "Found {} CAN devices\n Please select your device ", - interface.len() - ))); - }); - - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - let convert_shared_string: Vec = interface_clone - .into_iter() - .map(SharedString::from) - .collect(); - let socket_info = socket_info { - index: ModelRc::new(VecModel::from(Vec::default())), - name: ModelRc::new(VecModel::from(convert_shared_string)), - }; - ui.set_can_sockets(socket_info); - }); - } - } - Err(e) => { - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - ui.set_init_string(SharedString::from(format!( - "Can't get device list: {}", - e - ))); - let socket_info = socket_info { - index: ModelRc::new(VecModel::from(Vec::default())), - name: ModelRc::new(VecModel::from(Vec::default())), - }; - ui.set_can_sockets(socket_info); - }); - } - }; - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - if !ui.window().is_visible() { - exit(1); - } - }); - tokio::time::sleep(Duration::from_micros(50)).await; - } - }); - - #[cfg(target_os = "windows")] tokio::spawn(async move { - let mut previous_sockets = Vec::default(); - loop { - // get channel_handle - match attached_channels() { - Ok(channels) => { - if channels.is_empty() { - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - ui.set_init_string(SharedString::from("No CAN device found !")); - }); - } else { - let mut can_socket_names = Vec::default(); - let mut can_socket_index = Vec::default(); - let mut count = 0; - for channel in channels { - let socket_name = SharedString::from(format!( - "{}(0x{:02X})", - channel.device_name(), - channel.channel_information.device_id - )); - can_socket_names.push(socket_name); - can_socket_index - .push(channel.channel_information.channel_handle as i32); - count += 1; - } - if previous_sockets != can_socket_names { - previous_sockets = can_socket_names.clone(); - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - ui.set_init_string(SharedString::from(format!( - "Found {} CAN devices\n Please select your device ", - count - ))); - let socket_info = socket_info { - index: ModelRc::new(VecModel::from(can_socket_index)), - name: ModelRc::new(VecModel::from(can_socket_names)), - }; - ui.set_can_sockets(socket_info); - }); - } - } - } - Err(e) => { - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - ui.set_init_string(SharedString::from(format!( - "Can't get device list: {:?}", - e - ))); - }); - } - } - let _ = ui_handle.upgrade_in_event_loop(move |ui| { - if !ui.window().is_visible() { - exit(1); - } - }); - tokio::time::sleep(Duration::from_micros(50)).await; - } + let init_event = Init { + ui_handle: &ui_handle, + }; + init_event.run(); }); let (start_tx, start_rx) = mpsc::channel(); @@ -177,6 +59,7 @@ async fn main() -> io::Result<()> { } #[cfg(target_os = "windows")] { + use event_handler::p_can_bitrate; let ui = ui_handle.unwrap(); let get_device_handle = match ui.get_can_sockets().index.row_data(_index as usize) { Some(device) => device,