Skip to main content

zeroclaw_hardware/
util.rs

1const SERIAL_ALLOWED_PATH_PREFIXES: &[&str] = &[
2    "/dev/ttyACM",
3    "/dev/ttyUSB",
4    "/dev/tty.usbmodem",
5    "/dev/cu.usbmodem",
6    "/dev/tty.usbserial",
7    "/dev/cu.usbserial",
8    "COM",
9    #[cfg(feature = "dev-sim")]
10    DEV_SIM_SERIAL_PATH_PREFIX,
11];
12
13#[cfg(feature = "dev-sim")]
14const DEV_SIM_SERIAL_PATH_PREFIX: &str = "/tmp/zc-sim-";
15
16pub fn is_serial_path_allowed(path: &str) -> bool {
17    SERIAL_ALLOWED_PATH_PREFIXES
18        .iter()
19        .any(|prefix| path.starts_with(prefix))
20}
21
22pub fn serial_path_allowlist_hint() -> String {
23    SERIAL_ALLOWED_PATH_PREFIXES
24        .iter()
25        .map(|prefix| format!("{prefix}*"))
26        .collect::<Vec<_>>()
27        .join(", ")
28}
29
30fn is_dev_sim_serial_path(path: &str) -> bool {
31    #[cfg(feature = "dev-sim")]
32    {
33        path.starts_with(DEV_SIM_SERIAL_PATH_PREFIX)
34    }
35    #[cfg(not(feature = "dev-sim"))]
36    {
37        let _ = path;
38        false
39    }
40}
41
42pub fn should_open_serial_nonexclusive(path: &str) -> bool {
43    is_dev_sim_serial_path(path)
44}
45
46pub fn serial_open_baud(path: &str, configured_baud: u32) -> u32 {
47    if is_dev_sim_serial_path(path) {
48        0
49    } else {
50        configured_baud
51    }
52}