move radio_connect to rust
This commit is contained in:
parent
b1116daba2
commit
2a7c32924c
43
radio.c
43
radio.c
@ -82,49 +82,6 @@ void radio_print_version(radio_device_t* dev, FILE *out)
|
||||
dev->print_version(dev, out);
|
||||
}
|
||||
|
||||
//
|
||||
// Connect to the radio and identify the type of device.
|
||||
//
|
||||
radio_device_t* radio_connect()
|
||||
{
|
||||
const char *ident;
|
||||
int i;
|
||||
radio_device_t * dev = 0;
|
||||
|
||||
// Try TYT MD family.
|
||||
ident = dfu_init(0x0483, 0xdf11);
|
||||
if (! ident) {
|
||||
// Try RD-5R, DM-1801 and GD-77.
|
||||
if (hid_init(0x15a2, 0x0073) >= 0)
|
||||
ident = hid_identify();
|
||||
}
|
||||
if (! ident) {
|
||||
// Try AT-D868UV.
|
||||
if (serial_init(0x28e9, 0x018a) >= 0)
|
||||
ident = serial_identify();
|
||||
}
|
||||
if (! ident) {
|
||||
fprintf(stderr, "No radio detected.\n");
|
||||
fprintf(stderr, "Check your USB cable!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
for (i=0; radio_tab[i].ident; i++) {
|
||||
if (strcasecmp(ident, radio_tab[i].ident) == 0) {
|
||||
dev = radio_tab[i].device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! dev) {
|
||||
fprintf(stderr, "Unrecognized radio '%s'.\n", ident);
|
||||
exit(-1);
|
||||
}
|
||||
fprintf(stderr, "Connect to %s.\n", dev->name);
|
||||
|
||||
set_active_device(dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
//
|
||||
// Read firmware image from the device.
|
||||
//
|
||||
|
40
src/radio.rs
40
src/radio.rs
@ -1,7 +1,7 @@
|
||||
use std::ffi::{CStr, CString};
|
||||
use libc::{c_char, c_int, c_uint};
|
||||
use std::os::unix::io::AsRawFd;
|
||||
use sys::process::exit;
|
||||
use std::process::exit;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
|
||||
@ -13,7 +13,9 @@ pub struct radio_tab_t {
|
||||
|
||||
extern {
|
||||
|
||||
fn radio_connect() -> *const radio_device_t;
|
||||
fn set_active_device(device: *const radio_device_t);
|
||||
|
||||
//fn radio_connect() -> *const radio_device_t;
|
||||
|
||||
fn radio_download(device: *const radio_device_t);
|
||||
fn radio_upload(device: *const radio_device_t, cont_flag: c_int);
|
||||
@ -44,21 +46,22 @@ extern {
|
||||
/// and identify the type of device.
|
||||
pub fn connect() -> *const radio_device_t {
|
||||
unsafe {
|
||||
|
||||
let mut ident: *const c_char = std::ptr::null();
|
||||
let mut ident: *const c_char;
|
||||
// Try TYT MD family.
|
||||
ident = dfu_init(0x0483, 0xdf11);
|
||||
if ident.is_null() {
|
||||
// Try RD-5R, DM-1801 and GD-77.
|
||||
if (hid_init(0x15a2, 0x0073) >= 0)
|
||||
if hid_init(0x15a2, 0x0073) >= 0 {
|
||||
ident = hid_identify();
|
||||
}
|
||||
}
|
||||
|
||||
if ident.is_null() {
|
||||
// Try AT-D868UV.
|
||||
if (serial_init(0x28e9, 0x018a) >= 0)
|
||||
if serial_init(0x28e9, 0x018a) >= 0 {
|
||||
ident = serial_identify();
|
||||
}
|
||||
}
|
||||
|
||||
if ident.is_null() {
|
||||
eprintln!("No radio detected.");
|
||||
@ -66,33 +69,36 @@ pub fn connect() -> *const radio_device_t {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
let mut device: *const radio_device_t = std::ptr::null();
|
||||
|
||||
let mut ptr = get_radio_tab();
|
||||
let ident_str = CStr::from_ptr(ident).to_str().unwrap();
|
||||
|
||||
let mut device: *const radio_device_t = std::ptr::null();
|
||||
let mut radio_tab_ptr = get_radio_tab();
|
||||
loop {
|
||||
let ident_ptr = (*ptr).ident;
|
||||
let ident_ptr = (*radio_tab_ptr).ident;
|
||||
if ident_ptr.is_null() {
|
||||
break;
|
||||
}
|
||||
let name_ptr = (*(*ptr).device).name;
|
||||
|
||||
let table_ident = CStr::from_ptr(ident_ptr).to_str().unwrap().to_string();
|
||||
if table_ident.eq_ignore_ascii_case(ident_str) {
|
||||
device = ptr;
|
||||
let table_ident = CStr::from_ptr(ident_ptr).to_str().unwrap();
|
||||
if ident_str.eq_ignore_ascii_case(table_ident) {
|
||||
device = (*radio_tab_ptr).device;
|
||||
break;
|
||||
}
|
||||
|
||||
ptr = ptr.offset(1);
|
||||
radio_tab_ptr = radio_tab_ptr.offset(1);
|
||||
}
|
||||
|
||||
if device.is_null() {
|
||||
eprintln!("Unrecognized radio '{}'.", ident_str);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//TODO need to reset device static
|
||||
|
||||
let name_ptr = (*device).name;
|
||||
let name = CStr::from_ptr(name_ptr).to_str().unwrap();
|
||||
println!("Connect to {}", name);
|
||||
|
||||
set_active_device(device);
|
||||
return device;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user