Skip to content

Commit

Permalink
support debug page for windows also
Browse files Browse the repository at this point in the history
  • Loading branch information
TuEmb committed Sep 20, 2024
1 parent ba0c3c8 commit c900c19
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 59 deletions.
113 changes: 77 additions & 36 deletions src/event_handler/can_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use can_dbc::DBC;
use chrono::Utc;
use pcan_basic::bus::UsbBus;
#[cfg(target_os = "windows")]
use pcan_basic::socket::usb::UsbCanSocket;
use pcan_basic::socket::{usb::UsbCanSocket, CanFrame};
use slint::{Model, ModelRc, SharedString, VecModel, Weak};
#[cfg(target_os = "linux")]
use socketcan::{CanFrame, CanInterface, CanSocket, EmbeddedFrame, Frame, Socket};
Expand All @@ -19,7 +20,7 @@ pub struct CanHandler<'a> {
#[cfg(target_os = "linux")]
pub iface: &'a str,
#[cfg(target_os = "windows")]
pub iface: UsbCanSocket,
pub iface: UsbBus,
pub ui_handle: &'a Weak<AppWindow>,
pub mspc_rx: &'a Arc<Mutex<Receiver<DBC>>>,
pub can_tx: Sender<CanFrame>,
Expand All @@ -28,7 +29,7 @@ pub struct CanHandler<'a> {
}

static mut NEW_DBC_CHECK: bool = false;
use super::{EVEN_COLOR, ODD_COLOR};
use super::{p_can_bitrate, EVEN_COLOR, ODD_COLOR};

impl<'a> CanHandler<'a> {
pub fn process_can_messages(&mut self) {
Expand All @@ -43,7 +44,17 @@ impl<'a> CanHandler<'a> {
self.process_ui_events(tx_can_socket, rx_can_socket, can_if);
}
#[cfg(target_os = "windows")]
self.process_ui_events(dbc);
{
let baudrate = p_can_bitrate(&self.bitrate).unwrap();
match UsbCanSocket::open(self.iface, baudrate) {
Ok(socket) => {
self.process_ui_events(socket);
}
Err(e) => {
println!("Failed to open CAN socket: {:?}", e);
}
}
}
}
#[cfg(target_os = "linux")]
fn open_can_socket(&self) -> CanSocket {
Expand Down Expand Up @@ -182,11 +193,39 @@ impl<'a> CanHandler<'a> {
}
}
#[cfg(target_os = "windows")]
fn process_ui_events(&mut self, dbc: DBC) {
use pcan_basic::{error::PcanError, socket::RecvCan};
fn process_ui_events(&mut self, can_if: UsbCanSocket) {
use pcan_basic::{
error::PcanError,
socket::{MessageType, RecvCan, SendCan},
};
let mut start_bus_load = Instant::now();
let mut total_bits = 0;
self.check_to_kill_thread();
let refer_socket = UsbCanSocket::open_with_usb_bus(self.iface);
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
ui.on_can_transmit(move |is_extended, can_id, can_data| {
match Self::convert_hex_string_u32(&can_id) {
Ok(id) => match Self::convert_hex_string_arr(&can_data) {
Ok(data) => {
if is_extended {
let can_frame =
CanFrame::new(id, MessageType::Extended, &data).unwrap();
let _ = refer_socket.send(can_frame);
} else {
let can_frame =
CanFrame::new(id, MessageType::Standard, &data).unwrap();
let _ = refer_socket.send(can_frame);
};
}
Err(e) => {
println!("Failed to parse can data {}, error {}", can_data, e);
}
},
Err(e) => {
println!("Failed to parse can id {}, error: {}", can_id, e);
}
}
});
});
loop {
let bitrate = self.bitrate().unwrap();
let busload = if start_bus_load.elapsed() >= Duration::from_millis(1000) {
Expand All @@ -199,11 +238,7 @@ impl<'a> CanHandler<'a> {
};
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| unsafe {
if ui.get_is_new_dbc() {
if ui.get_is_first_open() {
ui.set_is_first_open(false);
} else {
NEW_DBC_CHECK = true;
}
NEW_DBC_CHECK = true;
ui.set_is_new_dbc(false);
}
ui.set_bitrate(bitrate as i32);
Expand All @@ -213,37 +248,42 @@ impl<'a> CanHandler<'a> {
});
unsafe {
if NEW_DBC_CHECK {
NEW_DBC_CHECK = false;
break;
if let Ok(dbc) = self.mspc_rx.lock().unwrap().try_recv() {
self.dbc = Some(dbc);
NEW_DBC_CHECK = false;
}
}
}
match self.iface.recv_frame() {
match can_if.recv_frame() {
Ok(frame) => {
let _ = self.can_tx.send(frame);
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
ui.set_state("OK".into());
});
total_bits += (frame.dlc() as u32 + 6) * 8; // Data length + overhead (approximation)
let id = frame.can_id();
let frame_id = id & !0x80000000;
for message in dbc.messages() {
if frame_id == (message.message_id().raw() & !0x80000000) {
let padding_data = Self::pad_to_8_bytes(frame.data());
let hex_string = Self::array_to_hex_string(frame.data());
let signal_data = message.parse_from_can(&padding_data);
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
let is_filter = ui.get_is_filter();
let messages: ModelRc<CanData> = if !is_filter {
ui.get_messages()
} else {
ui.get_filter_messages()
};
Self::update_ui_with_signals(
&messages,
frame_id,
signal_data,
hex_string,
);
});
if let Some(dbc) = &self.dbc {
let id = frame.can_id();
let frame_id = id & !0x80000000;
for message in dbc.messages() {
if frame_id == (message.message_id().raw() & !0x80000000) {
let padding_data = Self::pad_to_8_bytes(frame.data());
let hex_string = Self::array_to_hex_string(frame.data());
let signal_data = message.parse_from_can(&padding_data);
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
let is_filter = ui.get_is_filter();
let messages: ModelRc<CanData> = if !is_filter {
ui.get_messages()
} else {
ui.get_filter_messages()
};
Self::update_ui_with_signals(
&messages,
frame_id,
signal_data,
hex_string,
);
});
}
}
}
}
Expand All @@ -253,6 +293,7 @@ impl<'a> CanHandler<'a> {
ui.set_state(format!("{:?}", e).into());
}
});
sleep(Duration::from_millis(1));
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/event_handler/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use std::{

use crate::slint_generatedAppWindow::{raw_can, AppWindow};
use chrono::Local;
#[cfg(target_os = "windows")]
use pcan_basic::socket::CanFrame;
use slint::{Model, SharedString, VecModel, Weak};
#[cfg(target_os = "linux")]
use socketcan::{CanFrame, EmbeddedFrame, Frame};

const MAX_LEN: usize = 1000;
Expand All @@ -21,13 +24,22 @@ pub struct DebugHandler<'a> {
impl<'a> DebugHandler<'a> {
pub fn run(&mut self) {
let (tx, rx) = mpsc::channel();
let mut debug_enable = true;
let mut debug_enable = false;
let tx_clone = tx.clone();
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
ui.on_change_state(move |state| {
let _ = tx_clone.send(state);
});
});
loop {
if let Ok(en) = rx.try_recv() {
debug_enable = en;
}
if debug_enable {
if let Ok(frame) = self.can_rx.try_recv() {
#[cfg(target_os = "windows")]
let frame_id = frame.can_id() & !0x80000000;
#[cfg(target_os = "linux")]
let frame_id = frame.raw_id() & !0x80000000;
if frame_id >= self.filter.0 && frame_id <= self.filter.1 {
let bitrate = self.bitrate().unwrap();
Expand All @@ -49,7 +61,10 @@ impl<'a> DebugHandler<'a> {
),
data: SharedString::from(format!("{:?}", frame.data())),
id: SharedString::from(format!("0x{:08X}", frame_id)),
#[cfg(target_os = "linux")]
len: frame.len() as i32,
#[cfg(target_os = "windows")]
len: frame.dlc() as i32,
},
);
let message_vec: Rc<VecModel<raw_can>> =
Expand All @@ -63,13 +78,6 @@ impl<'a> DebugHandler<'a> {
} else {
std::thread::sleep(Duration::from_millis(50));
}
let tx_clone = tx.clone();
let _ = self.ui_handle.upgrade_in_event_loop(move |ui| {
let enable = ui.get_is_debug_en();
if enable != debug_enable {
let _ = tx_clone.send(enable);
}
});
}
}

Expand Down
19 changes: 4 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod event_handler;
use can_dbc::DBC;
use event_handler::{CanHandler, DBCFile, DebugHandler, Init, PacketFilter};
#[cfg(target_os = "windows")]
use pcan_basic::{bus::UsbBus, socket::usb::UsbCanSocket};
use pcan_basic::bus::UsbBus;
#[cfg(target_os = "linux")]
use privilege_rs::privilege_request;
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -62,7 +62,6 @@ 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 All @@ -72,19 +71,9 @@ async fn main() -> io::Result<()> {
}
};
let usb_can = UsbBus::try_from(get_device_handle as u16).unwrap();
let ui_handle = ui.as_weak();
let baudrate = p_can_bitrate(&bitrate).unwrap();
match UsbCanSocket::open(usb_can, baudrate) {
Ok(socket) => {
ui_handle.unwrap().set_is_init(true);
let _ = start_tx_1.send((socket, bitrate));
}
Err(e) => {
ui_handle
.unwrap()
.set_init_string(SharedString::from(format!("Failed to start: {:?}", e)));
}
}
let _ = start_tx_1.send((usb_can, bitrate.clone()));
let _ = start_tx_2.send((usb_can, bitrate));
ui.set_is_init(true);
}
});

Expand Down
2 changes: 2 additions & 0 deletions ui/app.slint
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export component AppWindow inherits Window {
callback can_transmit(bool, string, string);
callback can_id_check_string(bool, string) -> bool;
callback can_data_check_string(string) -> bool;
callback change_state(bool);
title: @tr("CAN VIEWER (version 0.2.2)");
icon: @image-url("images/can_viewer_128px.png");
background: #1a1f2b;
Expand Down Expand Up @@ -124,6 +125,7 @@ export component AppWindow inherits Window {
raw_data: raw_data;
change_state(en) => {
is_debug_en = en;
change_state(en);
}
can_transmit(is_extended, can_id, can_data) => {
can_transmit(is_extended, can_id, can_data)
Expand Down

0 comments on commit c900c19

Please sign in to comment.