|
1 | 1 | #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] |
2 | 2 |
|
| 3 | +use std::time::Duration; |
| 4 | + |
3 | 5 | use probe_rs::probe::list::Lister; |
4 | 6 | use serde::{Deserialize, Serialize}; |
| 7 | +use tockloader_lib::board_settings::BoardSettings; |
| 8 | +use tockloader_lib::connection::SerialConnection; |
| 9 | +use tockloader_lib::connection::SerialTargetInfo; |
| 10 | +use tockloader_lib::connection::{Connection, ProbeRSConnection, ProbeTargetInfo}; |
| 11 | +use tockloader_lib::CommandInfo; |
5 | 12 | use tokio_serial::available_ports; |
| 13 | +use tokio_serial::FlowControl; |
| 14 | +use tokio_serial::Parity; |
6 | 15 | use tokio_serial::SerialPortType; |
| 16 | +use tokio_serial::StopBits; |
| 17 | + |
7 | 18 | #[derive(Debug, Serialize, Deserialize)] |
8 | 19 | pub struct DebugProbeSummary { |
9 | 20 | pub identifier: String, |
@@ -88,11 +99,110 @@ async fn list_all_devices() -> Result<ConnectedDevices, String> { |
88 | 99 | }) |
89 | 100 | } |
90 | 101 |
|
| 102 | +#[tauri::command] |
| 103 | +async fn connect_to_probe( |
| 104 | + probe_identifier: String, |
| 105 | + chip: String, |
| 106 | + core: usize, |
| 107 | +) -> Result<String, String> { |
| 108 | + let probes = Lister::new().list_all(); |
| 109 | + let debug_probe_info = probes |
| 110 | + .into_iter() |
| 111 | + .find(|p| p.identifier == probe_identifier) |
| 112 | + .ok_or_else(|| format!("Probe with identifier '{probe_identifier}' not found."))?; |
| 113 | + |
| 114 | + let target_info = ProbeTargetInfo { chip, core }; |
| 115 | + let mut connection = ProbeRSConnection::new(debug_probe_info, target_info); |
| 116 | + |
| 117 | + match connection.open().await { |
| 118 | + Ok(_) => { |
| 119 | + let info_result = connection.info(&BoardSettings::default()).await; |
| 120 | + println!("Result of connection.info(): {info_result:?}"); |
| 121 | + Ok(format!( |
| 122 | + "Successfully connected to probe '{}' with chip '{}' and core {}.", |
| 123 | + probe_identifier, connection.target_info.chip, connection.target_info.core |
| 124 | + )) |
| 125 | + } |
| 126 | + Err(e) => { |
| 127 | + eprintln!("Error connecting to probe: {e:?}"); |
| 128 | + Err(format!("Failed to connect to probe: {e}")) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +//new code for serial port connection |
| 133 | +#[tauri::command] |
| 134 | +#[allow(clippy::too_many_arguments)] |
| 135 | +async fn connect_to_serial( |
| 136 | + port_name: String, |
| 137 | + baud_rate: u32, |
| 138 | + parity_str: String, |
| 139 | + stop_bits_str: String, |
| 140 | + flow_control_str: String, |
| 141 | + timeout_ms: u64, |
| 142 | + request_to_send: bool, |
| 143 | + data_terminal_ready: bool, |
| 144 | +) -> Result<String, String> { |
| 145 | + //added now |
| 146 | + println!("Attempting to connect to serial port: {port_name}"); |
| 147 | + println!("Settings: Baud={baud_rate}, Parity={parity_str}, StopBits={stop_bits_str}, Flow={flow_control_str}, Timeout={timeout_ms}ms, RTS={request_to_send}, DTR={data_terminal_ready}", |
| 148 | + ); |
| 149 | + //added |
| 150 | + // Parse string inputs into tokio_serial enums |
| 151 | + let parity = match parity_str.as_str() { |
| 152 | + "None" => Parity::None, |
| 153 | + "Odd" => Parity::Odd, |
| 154 | + "Even" => Parity::Even, |
| 155 | + _ => return Err(format!("Invalid parity: {parity_str}")), |
| 156 | + }; |
| 157 | + |
| 158 | + let stop_bits = match stop_bits_str.as_str() { |
| 159 | + "One" => StopBits::One, |
| 160 | + "Two" => StopBits::Two, |
| 161 | + _ => return Err(format!("Invalid stop bits: {stop_bits_str}")), |
| 162 | + }; |
| 163 | + |
| 164 | + let flow_control = match flow_control_str.as_str() { |
| 165 | + "None" => FlowControl::None, |
| 166 | + "Software" => FlowControl::Software, |
| 167 | + "Hardware" => FlowControl::Hardware, |
| 168 | + _ => return Err(format!("Invalid flow control: {flow_control_str}")), |
| 169 | + }; |
| 170 | + |
| 171 | + let baud_rate_val = baud_rate; |
| 172 | + let serial_target_info = SerialTargetInfo { |
| 173 | + baud_rate: baud_rate_val, |
| 174 | + parity, |
| 175 | + stop_bits, |
| 176 | + flow_control, |
| 177 | + timeout: Duration::from_millis(timeout_ms), |
| 178 | + request_to_send, |
| 179 | + data_terminal_ready, |
| 180 | + }; |
| 181 | + |
| 182 | + let mut connection = SerialConnection::new(port_name.clone(), serial_target_info); |
| 183 | + // Attempt to open the serial connection |
| 184 | + match connection.open().await { |
| 185 | + Ok(_) => { |
| 186 | + let info_result = connection.info(&BoardSettings::default()).await; |
| 187 | + println!("Result of connection.info(): {info_result:?}"); |
| 188 | + println!("Successfully opened serial port."); |
| 189 | + Ok(format!("Successfully connected to serial port '{port_name}' at {baud_rate_val} baud with custom settings.")) |
| 190 | + } |
| 191 | + Err(e) => { |
| 192 | + eprintln!("Error connecting to serial port: {e:?}"); |
| 193 | + Err(format!("Failed to connect to serial port: {e}")) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
91 | 197 | // The main function for your Tauri application |
92 | 198 | fn main() { |
93 | 199 | tauri::Builder::default() |
94 | 200 | // Register the new command so it can be called from the frontend |
95 | | - .invoke_handler(tauri::generate_handler![list_all_devices]) |
| 201 | + .invoke_handler(tauri::generate_handler![ |
| 202 | + list_all_devices, |
| 203 | + connect_to_probe, |
| 204 | + connect_to_serial |
| 205 | + ]) |
96 | 206 | .run(tauri::generate_context!()) |
97 | 207 | .expect("error while running tauri application"); |
98 | 208 | } |
0 commit comments