Port more serial stuff to Rust

This commit is contained in:
Greg Shuflin 2021-03-04 19:07:51 -08:00
parent 23fc703abd
commit 963dc072c9
2 changed files with 18 additions and 12 deletions

View File

@ -45,7 +45,6 @@ extern {
fn hid_identify() -> *const c_char;
fn hid_close();
fn serial_identify(s: *const c_char) -> *const c_char;
fn serial_close();
}
@ -65,7 +64,7 @@ unsafe fn set_active_device(d: *const radio_device_t) {
/// and identify the type of device.
pub fn connect() -> Radio {
let ident_str = {
let ident_str = (|| {
let mut ident: *const c_char;
// Try TYT MD family.
ident = unsafe { dfu_init(0x0483, 0xdf11) };
@ -79,8 +78,9 @@ pub fn connect() -> Radio {
// Try AT-D868UV.
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 = unsafe { serial_identify(ptr) };
if let Some(identifier) = crate::serial::identify(&device_path) {
return identifier;
}
}
}
@ -91,7 +91,7 @@ pub fn connect() -> Radio {
}
unsafe { CStr::from_ptr(ident).to_str().unwrap().to_string() }
};
})();
unsafe {

View File

@ -1,8 +1,9 @@
use std::ffi::{CStr};
use std::ffi::{CStr, CString};
use libc::{c_char};
extern {
fn find_path(vid: libc::c_int, pid: libc::c_int) -> *const c_char;
fn serial_identify(s: *const c_char) -> *const c_char;
}
/// Connect to the specified device.
@ -22,12 +23,17 @@ pub fn serial_init(vid: u32, pid: u32, trace_flag: bool) -> Option<String> {
Some(dev_path)
}
/*
/// Query and return the device identification string.
/// On error, return None.
//
pub fn serial_identify(dev_path: &str) -> Option<String> {
pub fn identify(dev_path: &str) -> Option<String> {
let dev_path_c = CString::new(dev_path).unwrap();
let ptr = dev_path_c.as_ptr() as *mut c_char;
unsafe {
let output = serial_identify(ptr);
if output.is_null() {
None
} else {
Some(CStr::from_ptr(output).to_str().unwrap().to_string())
}
}
}
*/