Skip to content

Commit 4cc939f

Browse files
authored
commonise Windows and Linux code (#7)
* create init event to commonize linux and windows * fix issues for windows and prepare for release v0.2.2 * upgrade privilege-rs version
1 parent fc7be68 commit 4cc939f

File tree

5 files changed

+118
-129
lines changed

5 files changed

+118
-129
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ winapi = { version = "0.3.9", features = ["winuser"] }
1717
pcan-basic = { git = "https://github.com/TuEmb/pcan-basic.git", branch="main"}
1818

1919
[target.'cfg(unix)'.dependencies]
20-
privilege-rs = "0.1.0"
20+
privilege-rs = "0.1.2"
2121
socketcan = { git = "https://github.com/socketcan-rs/socketcan-rs.git", rev="e0d7760eca8085b247f37ea22f0aa41e00fa25fa", features = ["enumerate"] }
2222

2323
[build-dependencies]

src/event_handler/init.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
use crate::slint_generatedAppWindow::{socket_info, AppWindow};
2+
#[cfg(target_os = "windows")]
3+
use pcan_basic::hw::attached_channels as available_interfaces;
4+
use slint::{ComponentHandle, ModelRc, SharedString, VecModel, Weak};
5+
#[cfg(target_os = "linux")]
6+
use socketcan::available_interfaces;
7+
use std::{process::exit, time::Duration};
8+
pub struct Init<'a> {
9+
pub ui_handle: &'a Weak<AppWindow>,
10+
}
11+
12+
impl<'a> Init<'a> {
13+
pub fn run(&self) {
14+
let mut previous_interfaces = Vec::default();
15+
loop {
16+
match available_interfaces() {
17+
Ok(interface) => {
18+
if interface.is_empty() {
19+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
20+
let socket_info = socket_info {
21+
index: ModelRc::new(VecModel::from(Vec::default())),
22+
name: ModelRc::new(VecModel::from(Vec::default())),
23+
};
24+
ui.set_can_sockets(socket_info);
25+
ui.set_init_string(SharedString::from("No CAN device found !"));
26+
});
27+
} else {
28+
#[cfg(target_os = "linux")]
29+
if previous_interfaces != interface {
30+
let interface_clone = interface.clone();
31+
previous_interfaces = interface.clone();
32+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
33+
ui.set_init_string(SharedString::from(format!(
34+
"Found {} CAN devices\n Please select your device ",
35+
interface.len()
36+
)));
37+
});
38+
39+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
40+
let convert_shared_string: Vec<SharedString> = interface_clone
41+
.into_iter()
42+
.map(SharedString::from)
43+
.collect();
44+
let socket_info = socket_info {
45+
index: ModelRc::new(VecModel::from(Vec::default())),
46+
name: ModelRc::new(VecModel::from(convert_shared_string)),
47+
};
48+
ui.set_can_sockets(socket_info);
49+
});
50+
}
51+
#[cfg(target_os = "windows")]
52+
{
53+
let mut interface_names = Vec::default();
54+
let mut interface_index = Vec::default();
55+
let mut count = 0;
56+
for channel in interface {
57+
let interface_name = SharedString::from(format!(
58+
"{}(0x{:02X})",
59+
channel.device_name(),
60+
channel.channel_information.device_id
61+
));
62+
interface_names.push(interface_name);
63+
interface_index
64+
.push(channel.channel_information.channel_handle as i32);
65+
count += 1;
66+
}
67+
if previous_interfaces != interface_names {
68+
previous_interfaces = interface_names.clone();
69+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
70+
ui.set_init_string(SharedString::from(format!(
71+
"Found {} CAN devices\n Please select your device ",
72+
count
73+
)));
74+
let socket_info = socket_info {
75+
index: ModelRc::new(VecModel::from(interface_index)),
76+
name: ModelRc::new(VecModel::from(interface_names)),
77+
};
78+
ui.set_can_sockets(socket_info);
79+
});
80+
}
81+
}
82+
}
83+
}
84+
Err(e) => {
85+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
86+
ui.set_init_string(SharedString::from(format!(
87+
"Can't get device list: {:?}",
88+
e
89+
)));
90+
let socket_info = socket_info {
91+
index: ModelRc::new(VecModel::from(Vec::default())),
92+
name: ModelRc::new(VecModel::from(Vec::default())),
93+
};
94+
ui.set_can_sockets(socket_info);
95+
});
96+
}
97+
};
98+
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
99+
if !ui.window().is_visible() {
100+
exit(1);
101+
}
102+
});
103+
std::thread::sleep(Duration::from_micros(50));
104+
}
105+
}
106+
}

src/event_handler/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
pub(crate) mod can_handler;
22
pub(crate) mod dbc_file;
3+
pub(crate) mod init;
34
pub(crate) mod packet_filter;
45

56
pub use can_handler::CanHandler;
67
pub use dbc_file::DBCFile;
8+
pub use init::Init;
79
pub use packet_filter::PacketFilter;
810
#[cfg(target_os = "windows")]
911
use pcan_basic::socket::Baudrate;

src/main.rs

Lines changed: 8 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
use std::io;
2-
use std::process::exit;
32
use std::sync::mpsc;
43
use std::sync::{Arc, Mutex};
5-
use std::time::Duration;
64

75
mod event_handler;
86
use can_dbc::DBC;
7+
use event_handler::{CanHandler, DBCFile, Init, PacketFilter};
98
#[cfg(target_os = "windows")]
10-
use event_handler::p_can_bitrate;
11-
use event_handler::{CanHandler, DBCFile, PacketFilter};
12-
#[cfg(target_os = "windows")]
13-
use pcan_basic::{bus::UsbBus, hw::attached_channels, socket::usb::UsbCanSocket};
9+
use pcan_basic::{bus::UsbBus, socket::usb::UsbCanSocket};
1410
#[cfg(target_os = "linux")]
1511
use privilege_rs::privilege_request;
1612
#[cfg(target_os = "windows")]
1713
use slint::Model;
18-
use slint::{ModelRc, SharedString, VecModel};
19-
#[cfg(target_os = "linux")]
20-
use socketcan::available_interfaces;
14+
use slint::SharedString;
2115
#[cfg(target_os = "windows")]
2216
use winapi::um::wincon::FreeConsole;
2317

@@ -39,125 +33,11 @@ async fn main() -> io::Result<()> {
3933

4034
// Find available socket CAN
4135
let ui_handle = ui.as_weak();
42-
#[cfg(target_os = "linux")]
43-
tokio::spawn(async move {
44-
let mut previous_interfaces = Vec::default();
45-
loop {
46-
match available_interfaces() {
47-
Ok(interface) => {
48-
if interface.is_empty() {
49-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
50-
let socket_info = socket_info {
51-
index: ModelRc::new(VecModel::from(Vec::default())),
52-
name: ModelRc::new(VecModel::from(Vec::default())),
53-
};
54-
ui.set_can_sockets(socket_info);
55-
ui.set_init_string(SharedString::from("No CAN device found !"));
56-
});
57-
} else if previous_interfaces != interface {
58-
let interface_clone = interface.clone();
59-
previous_interfaces = interface.clone();
60-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
61-
ui.set_init_string(SharedString::from(format!(
62-
"Found {} CAN devices\n Please select your device ",
63-
interface.len()
64-
)));
65-
});
66-
67-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
68-
let convert_shared_string: Vec<SharedString> = interface_clone
69-
.into_iter()
70-
.map(SharedString::from)
71-
.collect();
72-
let socket_info = socket_info {
73-
index: ModelRc::new(VecModel::from(Vec::default())),
74-
name: ModelRc::new(VecModel::from(convert_shared_string)),
75-
};
76-
ui.set_can_sockets(socket_info);
77-
});
78-
}
79-
}
80-
Err(e) => {
81-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
82-
ui.set_init_string(SharedString::from(format!(
83-
"Can't get device list: {}",
84-
e
85-
)));
86-
let socket_info = socket_info {
87-
index: ModelRc::new(VecModel::from(Vec::default())),
88-
name: ModelRc::new(VecModel::from(Vec::default())),
89-
};
90-
ui.set_can_sockets(socket_info);
91-
});
92-
}
93-
};
94-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
95-
if !ui.window().is_visible() {
96-
exit(1);
97-
}
98-
});
99-
tokio::time::sleep(Duration::from_micros(50)).await;
100-
}
101-
});
102-
103-
#[cfg(target_os = "windows")]
10436
tokio::spawn(async move {
105-
let mut previous_sockets = Vec::default();
106-
loop {
107-
// get channel_handle
108-
match attached_channels() {
109-
Ok(channels) => {
110-
if channels.is_empty() {
111-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
112-
ui.set_init_string(SharedString::from("No CAN device found !"));
113-
});
114-
} else {
115-
let mut can_socket_names = Vec::default();
116-
let mut can_socket_index = Vec::default();
117-
let mut count = 0;
118-
for channel in channels {
119-
let socket_name = SharedString::from(format!(
120-
"{}(0x{:02X})",
121-
channel.device_name(),
122-
channel.channel_information.device_id
123-
));
124-
can_socket_names.push(socket_name);
125-
can_socket_index
126-
.push(channel.channel_information.channel_handle as i32);
127-
count += 1;
128-
}
129-
if previous_sockets != can_socket_names {
130-
previous_sockets = can_socket_names.clone();
131-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
132-
ui.set_init_string(SharedString::from(format!(
133-
"Found {} CAN devices\n Please select your device ",
134-
count
135-
)));
136-
let socket_info = socket_info {
137-
index: ModelRc::new(VecModel::from(can_socket_index)),
138-
name: ModelRc::new(VecModel::from(can_socket_names)),
139-
};
140-
ui.set_can_sockets(socket_info);
141-
});
142-
}
143-
}
144-
}
145-
Err(e) => {
146-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
147-
ui.set_init_string(SharedString::from(format!(
148-
"Can't get device list: {:?}",
149-
e
150-
)));
151-
});
152-
}
153-
}
154-
let _ = ui_handle.upgrade_in_event_loop(move |ui| {
155-
if !ui.window().is_visible() {
156-
exit(1);
157-
}
158-
});
159-
tokio::time::sleep(Duration::from_micros(50)).await;
160-
}
37+
let init_event = Init {
38+
ui_handle: &ui_handle,
39+
};
40+
init_event.run();
16141
});
16242

16343
let (start_tx, start_rx) = mpsc::channel();
@@ -177,6 +57,7 @@ async fn main() -> io::Result<()> {
17757
}
17858
#[cfg(target_os = "windows")]
17959
{
60+
use event_handler::p_can_bitrate;
18061
let ui = ui_handle.unwrap();
18162
let get_device_handle = match ui.get_can_sockets().index.row_data(_index as usize) {
18263
Some(device) => device,

ui/app.slint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export component AppWindow inherits Window {
2626
callback open_dbc_file();
2727
callback filter_id(CanData, bool);
2828
callback start(string, int, string);
29-
title: @tr("CAN VIEWER (version 0.2.0)");
29+
title: @tr("CAN VIEWER (version 0.2.2)");
3030
icon: @image-url("images/can_viewer_128px.png");
3131
background: #1a1f2b;
3232
default-font-family: "Noto Sans";

0 commit comments

Comments
 (0)