Port serial_init

This commit is contained in:
Greg Shuflin 2021-03-04 00:44:52 -08:00
parent fc15b1d1db
commit fc45f65a52
6 changed files with 46 additions and 11 deletions

View File

@ -347,7 +347,7 @@ static const char *identify()
// Return the first path found (dynamically allocated). // Return the first path found (dynamically allocated).
// Return 0 when no device with such GUID is present. // 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; char *path = 0;
@ -392,7 +392,7 @@ const char *dfu_init(unsigned vid, unsigned pid)
// Find path for device. // Find path for device.
if (vid == 0x0483 && pid == 0xdf11) { if (vid == 0x0483 && pid == 0xdf11) {
path = find_path(&guid_0483_df11); path = find_path_for_guid(&guid_0483_df11);
} else { } else {
fprintf(stderr, "No guid for vid=%04x, pid=%04x!\n", vid, pid); fprintf(stderr, "No guid for vid=%04x, pid=%04x!\n", vid, pid);
exit(-1); exit(-1);

View File

@ -357,7 +357,7 @@ int serial_open(const char *devname, int baud_rate)
// //
// Find a device path by vid/pid. // 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; char *result = 0;
@ -671,7 +671,7 @@ void serial_close()
// Query and return the device identification string. // Query and return the device identification string.
// On error, return NULL. // On error, return NULL.
// //
const char *serial_identify() const char *serial_identify(char* dev_path)
{ {
static unsigned char reply[16]; static unsigned char reply[16];
unsigned char ack[3]; unsigned char ack[3];

View File

@ -6,7 +6,7 @@ use libc::{c_int, c_char};
use getopts::Options; use getopts::Options;
use std::process::exit; use std::process::exit;
mod serial;
mod radio; mod radio;
const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ"; const COPYRIGHT: &'static str = "Copyright (C) 2018 Serge Vakulenko KK6ABQ";

View File

@ -45,8 +45,7 @@ extern {
fn hid_identify() -> *const c_char; fn hid_identify() -> *const c_char;
fn hid_close(); fn hid_close();
fn serial_init(vid: c_int, pid: c_int) -> c_int; fn serial_identify(s: *const c_char) -> *const c_char;
fn serial_identify() -> *const c_char;
fn serial_close(); fn serial_close();
} }
@ -78,8 +77,10 @@ pub fn connect() -> Radio {
if ident.is_null() { if ident.is_null() {
// Try AT-D868UV. // Try AT-D868UV.
if serial_init(0x28e9, 0x018a) >= 0 { let trace_flag = false; //TODO fix
ident = serial_identify(); 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);
} }
} }

33
src/serial.rs Normal file
View File

@ -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<String> {
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<String> {
}
*/

5
util.h
View File

@ -93,11 +93,12 @@ void hid_write_finish(void);
// //
// Serial functions. // Serial functions.
// //
int serial_init(int vid, int pid); //int serial_init(int vid, int pid);
const char *serial_identify(void); //const char *serial_identify(void);
void serial_close(void); void serial_close(void);
void serial_read_region(int addr, unsigned char *data, int nbytes); void serial_read_region(int addr, unsigned char *data, int nbytes);
void serial_write_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. // Delay in milliseconds.