#[cfg(not(target_os = "android"))]
use crate::error::Error;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::state::{BLUETOOTH, PCI, UNKNOWN, USB};
use std::collections::HashMap;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use serialport::SerialPortType;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn port_info_from_type(port: SerialPortType) -> HashMap<String, String> {
let mut port_info: HashMap<String, String> = HashMap::new();
port_info.insert("type".to_string(), UNKNOWN.to_string());
port_info.insert("vid".to_string(), UNKNOWN.to_string());
port_info.insert("pid".to_string(), UNKNOWN.to_string());
port_info.insert("serial_number".to_string(), UNKNOWN.to_string());
port_info.insert("manufacturer".to_string(), UNKNOWN.to_string());
port_info.insert("product".to_string(), UNKNOWN.to_string());
match port {
SerialPortType::UsbPort(info) => {
port_info.insert("type".to_string(), USB.to_string());
port_info.insert("vid".to_string(), info.vid.to_string());
port_info.insert("pid".to_string(), info.pid.to_string());
port_info.insert(
"serial_number".to_string(),
info.serial_number.unwrap_or_else(|| UNKNOWN.to_string()),
);
port_info.insert(
"manufacturer".to_string(),
info.manufacturer.unwrap_or_else(|| UNKNOWN.to_string()),
);
port_info.insert(
"product".to_string(),
info.product.unwrap_or_else(|| UNKNOWN.to_string()),
);
}
SerialPortType::BluetoothPort => {
port_info.insert("type".to_string(), BLUETOOTH.to_string());
}
SerialPortType::PciPort => {
port_info.insert("type".to_string(), PCI.to_string());
}
SerialPortType::Unknown => {
port_info.insert("type".to_string(), UNKNOWN.to_string());
}
}
port_info
}
#[cfg(not(target_os = "android"))]
pub fn enumerate_available_ports(
single_port_per_device: bool,
) -> Result<HashMap<String, HashMap<String, String>>, Error> {
#[cfg(target_os = "ios")]
{
let _ = single_port_per_device;
Ok(HashMap::new())
}
#[cfg(not(target_os = "ios"))]
{
Ok(enumerate_with_locations(single_port_per_device)?.0)
}
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
type PortListMap = HashMap<String, HashMap<String, String>>;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub(crate) fn raw_ports() -> Result<Vec<serialport::SerialPortInfo>, Error> {
let mut list = serialport::available_ports()
.map_err(|e| Error::String(format!("Failed to enumerate serial ports: {}", e)))?;
list.sort_by(|a, b| a.port_name.cmp(&b.port_name));
Ok(list)
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub(crate) fn enumerate_with_locations(
single_port_per_device: bool,
) -> Result<(PortListMap, HashMap<String, String>), Error> {
let list = raw_ports()?;
let mut locations = HashMap::new();
let mut result_list: HashMap<String, HashMap<String, String>> = HashMap::new();
for p in list {
if let Some(loc) = crate::port::details::usb_location(&p.port_type) {
locations.insert(p.port_name.clone(), loc);
}
result_list.insert(p.port_name, port_info_from_type(p.port_type));
}
#[cfg(target_os = "windows")]
for (port, instances) in windows_full_serials() {
if let Some(info) = result_list.get_mut(&port) {
let current = info.get("serial_number").map(String::as_str).unwrap_or("");
if let Some(full) = pick_full_serial(current, &instances) {
let full = full.to_string();
info.insert("serial_number".to_string(), full);
}
}
}
Ok((
apply_single_port_per_device(result_list, single_port_per_device),
locations,
))
}
#[cfg(any(target_os = "windows", test))]
pub(crate) fn extends_serial(current: &str, full: &str) -> bool {
full.len() > current.len() && full.starts_with(current) && !current.is_empty()
}
#[cfg(any(target_os = "windows", test))]
pub(crate) fn pick_full_serial<'a>(current: &str, instances: &'a [String]) -> Option<&'a str> {
instances
.iter()
.map(String::as_str)
.find(|full| extends_serial(current, full))
}
#[cfg(target_os = "windows")]
pub(crate) fn windows_full_serials() -> HashMap<String, Vec<String>> {
use winreg::enums::HKEY_LOCAL_MACHINE;
use winreg::RegKey;
let mut out = HashMap::new();
let Ok(usb) =
RegKey::predef(HKEY_LOCAL_MACHINE).open_subkey(r"SYSTEM\CurrentControlSet\Enum\USB")
else {
return out;
};
for vid_pid in usb.enum_keys().flatten() {
let Ok(device) = usb.open_subkey(&vid_pid) else {
continue;
};
for instance in device.enum_keys().flatten() {
let Ok(params) = device.open_subkey(format!(r"{instance}\Device Parameters")) else {
continue;
};
if let Ok(port_name) = params.get_value::<String, _>("PortName") {
out.entry(port_name).or_insert_with(Vec::new).push(instance);
}
}
}
out
}
pub fn apply_single_port_per_device(
ports: HashMap<String, HashMap<String, String>>,
single_port_per_device: bool,
) -> HashMap<String, HashMap<String, String>> {
if !single_port_per_device {
return ports;
}
filter_macos_single_port_per_device(ports)
}
pub fn apply_android_single_port_per_device(
ports: HashMap<String, HashMap<String, String>>,
single_port_per_device: bool,
) -> HashMap<String, HashMap<String, String>> {
if !single_port_per_device {
return ports;
}
let mut best: HashMap<String, (u32, String, HashMap<String, String>)> = HashMap::new();
for (path, info) in ports {
let (device, port_idx) = android_path_parts(&path);
let replace = match best.get(device) {
None => true,
Some((idx, _, _)) => port_idx < *idx,
};
if replace {
best.insert(device.to_string(), (port_idx, path, info));
}
}
best.into_values()
.map(|(_, path, info)| (path, info))
.collect()
}
fn android_path_parts(path: &str) -> (&str, u32) {
if let Some(idx) = path.rfind('#') {
let port = path[idx + 1..].parse().unwrap_or(0);
(&path[..idx], port)
} else {
(path, 0)
}
}
fn filter_macos_single_port_per_device(
ports: HashMap<String, HashMap<String, String>>,
) -> HashMap<String, HashMap<String, String>> {
let mut callout: HashMap<String, (String, HashMap<String, String>)> = HashMap::new();
let mut dialin: HashMap<String, (String, HashMap<String, String>)> = HashMap::new();
let mut other: HashMap<String, HashMap<String, String>> = HashMap::new();
for (path, info) in ports {
if let Some(suffix) = path.strip_prefix("/dev/cu.") {
callout.insert(suffix.to_string(), (path, info));
} else if let Some(suffix) = path.strip_prefix("/dev/tty.") {
dialin.insert(suffix.to_string(), (path, info));
} else {
other.insert(path, info);
}
}
let mut result = other;
for (suffix, entry) in dialin {
callout.entry(suffix).or_insert(entry);
}
for (_, (path, info)) in callout {
result.insert(path, info);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_truncated_serial_is_completed_from_its_instances() {
for (current, full, extends) in [
("A1B2", "A1B2-C3", true),
("", "6&2f3c&0&0000", false),
("A1B2", "6&2f3c&0&0000", false),
("A1B2", "A1B2", false),
] {
assert_eq!(extends_serial(current, full), extends, "{current} {full}");
}
let instances = ["6&2f3c&0&0000", "A1B2-C3", "ZZZ"].map(String::from);
assert_eq!(pick_full_serial("A1B2", &instances), Some("A1B2-C3"));
assert_eq!(pick_full_serial("Q9", &instances), None);
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[test]
fn locations_belong_to_listed_ports() {
let (map, locations) = enumerate_with_locations(false).unwrap();
assert!(locations.keys().all(|k| map.contains_key(k)));
#[cfg(not(feature = "usb-port-details"))]
assert!(locations.is_empty());
}
}