Skip to content

Commit bae6ded

Browse files
task 1.2
1 parent 6baa9da commit bae6ded

3 files changed

Lines changed: 413 additions & 113 deletions

File tree

tock-ui/src-tauri/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ tauri = { version = "2", features = [] }
2222
tauri-plugin-opener = "2"
2323
serde = { version = "1", features = ["derive"] }
2424
serde_json = "1"
25-
25+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
26+
tockloader-lib = { path = "../../tockloader-lib" }
27+
tokio-serial = {version = "5.4.4", features = ["libudev"] }
28+
probe-rs = { version = "0.24.0" }

tock-ui/src-tauri/src/main.rs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
1-
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
1+
22
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
33

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

0 commit comments

Comments
 (0)