diff --git a/src/main.rs b/src/main.rs index 8006681..963dc85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ use daemonize::Daemonize; use serialport; use std::{ + env, fs, path::Path, sync::{mpsc, Arc, Mutex}, @@ -74,6 +75,28 @@ fn ensure_config() { } fn find_serial_port() -> Option { + // Prefer environment variable if provided (e.g., by entrypoint for mock PTY) + if let Ok(path) = env::var("HACI_SERIAL_PORT") { + let p = Path::new(&path); + if !p.exists() { + // Wait up to ~5 seconds for the PTY to appear + for _ in 0..50 { + if p.exists() { + break; + } + thread::sleep(std::time::Duration::from_millis(100)); + } + } + if p.exists() { + return Some(path); + } else { + log::warn!( + "HACI_SERIAL_PORT='{}' does not exist. Falling back to port scan.", + path + ); + } + } + if let Ok(ports) = serialport::available_ports() { for p in ports { if p.port_name.starts_with("/dev/ttyACM") @@ -172,7 +195,7 @@ async fn main() { let serial_port_path = match find_serial_port() { Some(port) => port, None => { - log::error!("❌ No serial port found. Please connect the Teensy device."); + log::error!("❌ No serial port found. Set HACI_SERIAL_PORT to a device path (e.g., /dev/ttyACM0) or connect the Teensy device."); return; } };