|
| 1 | +#[cfg(target_os = "android")] |
| 2 | +use std::{collections::HashMap, io::{Read, Write}, sync::Mutex, time::Duration}; |
| 3 | + |
| 4 | +#[cfg(target_os = "android")] |
| 5 | +use tauri::{plugin::Builder as PluginBuilder, Manager, Runtime, State}; |
| 6 | + |
| 7 | +#[cfg(target_os = "android")] |
| 8 | +use serialport::{SerialPort, SerialPortType}; |
| 9 | + |
| 10 | +#[cfg(target_os = "android")] |
| 11 | +type PortMap = Mutex<HashMap<String, PortEntry>>; |
| 12 | + |
| 13 | +#[cfg(target_os = "android")] |
| 14 | +struct PortEntry { |
| 15 | + port: Box<dyn SerialPort + Send>, |
| 16 | +} |
| 17 | + |
| 18 | +#[cfg(target_os = "android")] |
| 19 | +#[derive(Debug, serde::Serialize, serde::Deserialize)] |
| 20 | +struct OpenOptions { |
| 21 | + path: String, |
| 22 | + #[serde(default = "default_baud")] |
| 23 | + baudRate: u32, |
| 24 | +} |
| 25 | + |
| 26 | +#[cfg(target_os = "android")] |
| 27 | +fn default_baud() -> u32 { 115_200 } |
| 28 | + |
| 29 | +#[cfg(target_os = "android")] |
| 30 | +#[tauri::command] |
| 31 | +fn available_ports_android() -> Result<serde_json::Value, String> { |
| 32 | + // Android: serialport enumeration is limited. Probe common device nodes. |
| 33 | + let candidates = ["/dev/ttyACM0", "/dev/ttyUSB0"]; |
| 34 | + let mut map = serde_json::Map::new(); |
| 35 | + for path in candidates { |
| 36 | + if std::fs::metadata(path).is_ok() { |
| 37 | + // Provide fake but plausible VID/PID so UI filter accepts it. |
| 38 | + // STM32 VCP: VID 1155 (0x0483), PID 22336 (0x57C0) |
| 39 | + map.insert(path.to_string(), serde_json::json!({ |
| 40 | + "vid": 1155, |
| 41 | + "pid": 22336, |
| 42 | + "serial_number": serde_json::Value::Null |
| 43 | + })); |
| 44 | + } |
| 45 | + } |
| 46 | + Ok(serde_json::Value::Object(map)) |
| 47 | +} |
| 48 | + |
| 49 | +#[cfg(target_os = "android")] |
| 50 | +#[tauri::command] |
| 51 | +fn open_android(state: State<'_, PortMap>, opts: OpenOptions) -> Result<bool, String> { |
| 52 | + let port = serialport::new(&opts.path, opts.baudRate) |
| 53 | + .timeout(Duration::from_millis(100)) |
| 54 | + .open() |
| 55 | + .map_err(|e| format!("failed to open: {e}"))?; |
| 56 | + let mut map = state.lock().unwrap(); |
| 57 | + map.insert(opts.path, PortEntry { port }); |
| 58 | + Ok(true) |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(target_os = "android")] |
| 62 | +#[tauri::command] |
| 63 | +fn set_timeout_android(state: State<'_, PortMap>, path: String, timeout: u64) -> Result<bool, String> { |
| 64 | + let mut map = state.lock().unwrap(); |
| 65 | + let entry = map.get_mut(&path).ok_or_else(|| "port not open".to_string())?; |
| 66 | + entry.port.set_timeout(Duration::from_millis(timeout)).map_err(|e| e.to_string())?; |
| 67 | + Ok(true) |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(target_os = "android")] |
| 71 | +#[tauri::command] |
| 72 | +fn read_binary_android(state: State<'_, PortMap>, path: String, size: usize, timeout: Option<u64>) -> Result<Vec<u8>, String> { |
| 73 | + let mut map = state.lock().unwrap(); |
| 74 | + let entry = map.get_mut(&path).ok_or_else(|| "port not open".to_string())?; |
| 75 | + if let Some(ms) = timeout { let _ = entry.port.set_timeout(Duration::from_millis(ms)); } |
| 76 | + let mut buf = vec![0u8; size.max(1)]; |
| 77 | + match entry.port.read(buf.as_mut_slice()) { |
| 78 | + Ok(n) if n > 0 => { buf.truncate(n); Ok(buf) }, |
| 79 | + Ok(_n) => Err("no data received".to_string()), |
| 80 | + Err(e) => { |
| 81 | + let msg = e.to_string(); |
| 82 | + if msg.to_lowercase().contains("timed out") { Err("no data received".to_string()) } else { Err(msg) } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(target_os = "android")] |
| 88 | +#[tauri::command] |
| 89 | +fn write_binary_android(state: State<'_, PortMap>, path: String, value: Vec<u8>) -> Result<usize, String> { |
| 90 | + let mut map = state.lock().unwrap(); |
| 91 | + let entry = map.get_mut(&path).ok_or_else(|| "port not open".to_string())?; |
| 92 | + entry.port.write_all(&value).map_err(|e| e.to_string())?; |
| 93 | + Ok(value.len()) |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(target_os = "android")] |
| 97 | +#[tauri::command] |
| 98 | +fn close_android(state: State<'_, PortMap>, path: String) -> Result<bool, String> { |
| 99 | + let mut map = state.lock().unwrap(); |
| 100 | + map.remove(&path); |
| 101 | + Ok(true) |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg(target_os = "android")] |
| 105 | +pub fn init_android<R: Runtime>() -> tauri::plugin::TauriPlugin<R> { |
| 106 | + PluginBuilder::new("serialplugin") |
| 107 | + .setup(|app, _api| { |
| 108 | + app.manage(Mutex::new(HashMap::<String, PortEntry>::new())); |
| 109 | + Ok(()) |
| 110 | + }) |
| 111 | + .invoke_handler(tauri::generate_handler![ |
| 112 | + available_ports_android, |
| 113 | + open_android, |
| 114 | + set_timeout_android, |
| 115 | + read_binary_android, |
| 116 | + write_binary_android, |
| 117 | + close_android |
| 118 | + ]) |
| 119 | + .build() |
| 120 | +} |
0 commit comments