detection

This commit is contained in:
Mateusz Faderewski 2024-07-01 21:51:36 +02:00
parent 688c0242c1
commit 999dd244c7
3 changed files with 17 additions and 7 deletions

View File

@ -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<DeviceInfo> = 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(),

View File

@ -553,13 +553,14 @@ pub struct DeviceInfo {
pub fn list_local_devices() -> Result<Vec<DeviceInfo>, 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<DeviceInfo> = 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<Vec<DeviceInfo>, 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),

View File

@ -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(),
})
}
}