From fc45f65a52a8d07f4cee2b9a0337bac1337e31e0 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 4 Mar 2021 00:44:52 -0800 Subject: [PATCH] Port serial_init --- dfu-windows.c | 4 ++-- serial.c | 4 ++-- src/lib.rs | 2 +- src/radio.rs | 9 +++++---- src/serial.rs | 33 +++++++++++++++++++++++++++++++++ util.h | 5 +++-- 6 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/serial.rs diff --git a/dfu-windows.c b/dfu-windows.c index 4432de0..07f8a93 100644 --- a/dfu-windows.c +++ b/dfu-windows.c @@ -347,7 +347,7 @@ static const char *identify() // Return the first path found (dynamically allocated). // Return 0 when no device with such GUID is present. // -static char *find_path(GUID *guid) +static char *find_path_for_guid(GUID *guid) { char *path = 0; @@ -392,7 +392,7 @@ const char *dfu_init(unsigned vid, unsigned pid) // Find path for device. if (vid == 0x0483 && pid == 0xdf11) { - path = find_path(&guid_0483_df11); + path = find_path_for_guid(&guid_0483_df11); } else { fprintf(stderr, "No guid for vid=%04x, pid=%04x!\n", vid, pid); exit(-1); diff --git a/serial.c b/serial.c index 7de9f0d..0ca4fb0 100644 --- a/serial.c +++ b/serial.c @@ -357,7 +357,7 @@ int serial_open(const char *devname, int baud_rate) // // Find a device path by vid/pid. // -static char *find_path(int vid, int pid) +char *find_path(int vid, int pid) { char *result = 0; @@ -671,7 +671,7 @@ void serial_close() // Query and return the device identification string. // On error, return NULL. // -const char *serial_identify() +const char *serial_identify(char* dev_path) { static unsigned char reply[16]; unsigned char ack[3]; diff --git a/src/lib.rs b/src/lib.rs index a59e139..965bcd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ use libc::{c_int, c_char}; use getopts::Options; use std::process::exit; - +mod serial; mod radio; const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ"; diff --git a/src/radio.rs b/src/radio.rs index 33a9798..c29976d 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -45,8 +45,7 @@ extern { fn hid_identify() -> *const c_char; fn hid_close(); - fn serial_init(vid: c_int, pid: c_int) -> c_int; - fn serial_identify() -> *const c_char; + fn serial_identify(s: *const c_char) -> *const c_char; fn serial_close(); } @@ -78,8 +77,10 @@ pub fn connect() -> Radio { if ident.is_null() { // Try AT-D868UV. - if serial_init(0x28e9, 0x018a) >= 0 { - ident = serial_identify(); + let trace_flag = false; //TODO fix + if let Some(device_path) = crate::serial::serial_init(0x28e9, 0x018a, trace_flag) { + let ptr = device_path.as_ptr() as *mut c_char; + ident = serial_identify(ptr); } } diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..7162a76 --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,33 @@ +use std::ffi::{CStr}; +use libc::{c_char}; + +extern { + fn find_path(vid: libc::c_int, pid: libc::c_int) -> *const c_char; +} + +/// Connect to the specified device. +/// Initiate the programming session. +pub fn serial_init(vid: u32, pid: u32, trace_flag: bool) -> Option { + let dev_path = unsafe { find_path(vid as i32, pid as i32) }; + + if dev_path.is_null() { + if trace_flag { + eprintln!("Cannot find USB device: {:#x}{:#x}", vid, pid); + } + return None; + } + + let dev_path = unsafe { CStr::from_ptr(dev_path).to_str().unwrap().to_string() }; + println!("Serial port: {}", dev_path); + Some(dev_path) +} + +/* +/// Query and return the device identification string. +/// On error, return None. +// +pub fn serial_identify(dev_path: &str) -> Option { + + +} +*/ diff --git a/util.h b/util.h index adedad6..7a8b7f0 100644 --- a/util.h +++ b/util.h @@ -93,11 +93,12 @@ void hid_write_finish(void); // // Serial functions. // -int serial_init(int vid, int pid); -const char *serial_identify(void); +//int serial_init(int vid, int pid); +//const char *serial_identify(void); void serial_close(void); void serial_read_region(int addr, unsigned char *data, int nbytes); void serial_write_region(int addr, unsigned char *data, int nbytes); +char *find_path(int vid, int pid); // // Delay in milliseconds.