|
1 | | -// Prevents additional console window on Windows in release, DO NOT REMOVE!! |
2 | 1 | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] |
3 | 2 |
|
| 3 | +use probe_rs::probe::list::Lister; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use tokio_serial::available_ports; |
| 6 | +use tokio_serial::SerialPortType; |
| 7 | +#[derive(Debug, Serialize, Deserialize)] |
| 8 | +pub struct DebugProbeSummary { |
| 9 | + pub identifier: String, |
| 10 | + pub vendor_id: u16, |
| 11 | + pub product_id: u16, |
| 12 | + pub serial_number: Option<String>, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Serialize, Deserialize)] |
| 16 | +pub struct SerialPortSummary { |
| 17 | + pub port_name: String, |
| 18 | + pub usb_vid: Option<u16>, |
| 19 | + pub usb_pid: Option<u16>, |
| 20 | + pub manufacturer: Option<String>, |
| 21 | + pub product: Option<String>, |
| 22 | + pub serial_number: Option<String>, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Serialize, Deserialize)] |
| 26 | +pub struct ConnectedDevices { |
| 27 | + pub debug_probes: Vec<DebugProbeSummary>, |
| 28 | + |
| 29 | + pub serial_ports: Vec<SerialPortSummary>, |
| 30 | +} |
| 31 | + |
| 32 | +#[tauri::command] |
| 33 | +async fn list_all_devices() -> Result<ConnectedDevices, String> { |
| 34 | + // 1. List Debug Probes |
| 35 | + let probes = Lister::new().list_all(); |
| 36 | + let debug_probe_summaries: Vec<DebugProbeSummary> = probes |
| 37 | + .into_iter() |
| 38 | + .map(|p| DebugProbeSummary { |
| 39 | + identifier: p.identifier, |
| 40 | + vendor_id: p.vendor_id, |
| 41 | + product_id: p.product_id, |
| 42 | + serial_number: p.serial_number, |
| 43 | + }) |
| 44 | + .collect(); |
| 45 | + |
| 46 | + // 2. List Serial Ports |
| 47 | + let serial_ports = match available_ports() { |
| 48 | + Ok(ports) => ports, |
| 49 | + Err(e) => { |
| 50 | + eprintln!("Error listing serial ports: {e:?}"); |
| 51 | + return Err(format!("Failed to list serial ports: {e}")); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + let serial_port_summaries: Vec<SerialPortSummary> = serial_ports |
| 56 | + .into_iter() |
| 57 | + .map(|p| { |
| 58 | + let mut usb_vid = None; |
| 59 | + let mut usb_pid = None; |
| 60 | + let mut manufacturer = None; |
| 61 | + let mut product = None; |
| 62 | + let mut serial_number = None; |
| 63 | + |
| 64 | + // Extract USB-specific fields if the port is a USB port |
| 65 | + if let SerialPortType::UsbPort(usb_info) = p.port_type { |
| 66 | + usb_vid = Some(usb_info.vid); |
| 67 | + usb_pid = Some(usb_info.pid); |
| 68 | + manufacturer = usb_info.manufacturer; |
| 69 | + product = usb_info.product; |
| 70 | + serial_number = usb_info.serial_number; |
| 71 | + } |
| 72 | + |
| 73 | + SerialPortSummary { |
| 74 | + port_name: p.port_name, |
| 75 | + usb_vid, |
| 76 | + usb_pid, |
| 77 | + manufacturer, |
| 78 | + product, |
| 79 | + serial_number, |
| 80 | + } |
| 81 | + }) |
| 82 | + .collect(); |
| 83 | + |
| 84 | + // Combine results into the ConnectedDevices struct |
| 85 | + Ok(ConnectedDevices { |
| 86 | + debug_probes: debug_probe_summaries, |
| 87 | + serial_ports: serial_port_summaries, |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +// The main function for your Tauri application |
4 | 92 | fn main() { |
5 | | - tock_ui_lib::run() |
| 93 | + tauri::Builder::default() |
| 94 | + // Register the new command so it can be called from the frontend |
| 95 | + .invoke_handler(tauri::generate_handler![list_all_devices]) |
| 96 | + .run(tauri::generate_context!()) |
| 97 | + .expect("error while running tauri application"); |
6 | 98 | } |
0 commit comments