From 999dd244c7eea2708cc79ea0c9a1bd9e7c6594e4 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Mon, 1 Jul 2024 21:51:36 +0200 Subject: [PATCH] detection --- sw/deployer/src/sc64/ftdi.rs | 9 +++++++-- sw/deployer/src/sc64/link.rs | 9 ++++++--- sw/deployer/src/sc64/serial.rs | 6 ++++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sw/deployer/src/sc64/ftdi.rs b/sw/deployer/src/sc64/ftdi.rs index 027a8f4..eb50b7d 100644 --- a/sw/deployer/src/sc64/ftdi.rs +++ b/sw/deployer/src/sc64/ftdi.rs @@ -1,5 +1,6 @@ pub struct DeviceInfo { pub port: String, + pub description: String, pub serial: String, } @@ -77,6 +78,7 @@ impl Wrapper { let result = if devices > 0 { let mut list: Vec = vec![]; + let mut description = [0i8; 128]; let mut serial = [0i8; 128]; let mut device = device_list; @@ -88,8 +90,8 @@ impl Wrapper { (*device).dev, std::ptr::null_mut(), 0, - std::ptr::null_mut(), - 0, + description.as_mut_ptr(), + description.len() as i32, serial.as_mut_ptr(), serial.len() as i32, ) @@ -98,6 +100,9 @@ impl Wrapper { if result == 0 { list.push(DeviceInfo { port: format!("i:0x{vendor:04X}:0x{product:04X}:{index}"), + description: unsafe { std::ffi::CStr::from_ptr(description.as_ptr()) } + .to_string_lossy() + .into_owned(), serial: unsafe { std::ffi::CStr::from_ptr(serial.as_ptr()) } .to_string_lossy() .into_owned(), diff --git a/sw/deployer/src/sc64/link.rs b/sw/deployer/src/sc64/link.rs index 0113567..e82104d 100644 --- a/sw/deployer/src/sc64/link.rs +++ b/sw/deployer/src/sc64/link.rs @@ -553,13 +553,14 @@ pub struct DeviceInfo { pub fn list_local_devices() -> Result, Error> { const SC64_VID: u16 = 0x0403; const SC64_PID: u16 = 0x6014; - const SC64_SID: &str = "SC64"; + const SC64_SERIAL_PREFIX: &str = "SC64"; + const SC64_DESCRIPTION: &str = "SC64"; let mut devices: Vec = Vec::new(); if let Ok(list) = FtdiDevice::list(SC64_VID, SC64_PID) { for device in list.into_iter() { - if device.serial.starts_with(SC64_SID) { + if device.description == SC64_DESCRIPTION { devices.push(DeviceInfo { backend: BackendType::Ftdi, port: format!("{FTDI_PREFIX}{}", device.port), @@ -571,7 +572,9 @@ pub fn list_local_devices() -> Result, Error> { if let Ok(list) = SerialDevice::list(SC64_VID, SC64_PID) { for device in list.into_iter() { - if device.serial.starts_with(SC64_SID) { + let is_sc64_device = device.description == SC64_DESCRIPTION + || device.serial.starts_with(SC64_SERIAL_PREFIX); + if is_sc64_device { devices.push(DeviceInfo { backend: BackendType::Serial, port: format!("{SERIAL_PREFIX}{}", device.port), diff --git a/sw/deployer/src/sc64/serial.rs b/sw/deployer/src/sc64/serial.rs index 248826e..836b331 100644 --- a/sw/deployer/src/sc64/serial.rs +++ b/sw/deployer/src/sc64/serial.rs @@ -1,5 +1,6 @@ pub struct DeviceInfo { pub port: String, + pub description: String, pub serial: String, } @@ -40,10 +41,11 @@ impl SerialDevice { for port in serialport::available_ports()? { if let serialport::SerialPortType::UsbPort(info) = port.port_type { - if info.vid == vendor && info.pid == product && info.serial_number.is_some() { + if info.vid == vendor && info.pid == product { devices.push(DeviceInfo { port: port.port_name, - serial: info.serial_number.unwrap(), + description: info.product.unwrap_or_default(), + serial: info.serial_number.unwrap_or_default(), }) } }