Skip to content

Commit

Permalink
create init event to commonize linux and windows
Browse files Browse the repository at this point in the history
  • Loading branch information
TuEmb committed Sep 16, 2024
1 parent fc7be68 commit da76edb
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 150 deletions.
106 changes: 106 additions & 0 deletions src/event_handler/init.rs
Original file line number Diff line number Diff line change
@@ -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<AppWindow>,
}

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<SharedString> = 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));
}
}
}
25 changes: 2 additions & 23 deletions src/event_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Baudrate> {
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,
}
}
137 changes: 10 additions & 127 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<SharedString> = 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();
Expand All @@ -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,
Expand Down

0 comments on commit da76edb

Please sign in to comment.