Compute radio list in rust

This commit is contained in:
Greg Shuflin 2021-02-28 19:41:21 -08:00
parent e8099fc681
commit 8f544cfa95
2 changed files with 32 additions and 20 deletions

24
radio.c
View File

@ -35,10 +35,9 @@
#include "radio.h"
#include "util.h"
static struct {
char *ident;
radio_device_t *device;
} radio_tab[] = {
typedef struct { char* ident; radio_device_t* device; } radio_tab_t;
static radio_tab_t radio_tab[] = {
{ "DR780", &radio_md380 }, // TYT MD-380, Retevis RT3, RT8
{ "MD390", &radio_md390 }, // TYT MD-390
{ "MD-UV380", &radio_uv380 }, // TYT MD-UV380
@ -58,6 +57,10 @@ static struct {
{ 0, 0 }
};
radio_tab_t* get_radio_tab() {
return radio_tab;
}
static radio_device_t *device; // Device-dependent interface
unsigned char radio_mem [1024*1024*2]; // Radio memory contents, up to 2 Mbytes
@ -114,19 +117,6 @@ radio_device_t* radio_connect()
return dev;
}
//
// List all supported radios.
//
void radio_list_c()
{
int i;
printf("Supported radios:\n");
for (i=0; radio_tab[i].ident; i++) {
printf(" %s\n", radio_tab[i].device->name);
}
}
//
// Read firmware image from the device.
//

View File

@ -1,15 +1,21 @@
use std::ffi::CString;
use std::ffi::{CStr, CString};
use libc::{c_char, c_int};
use std::os::unix::io::AsRawFd;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[repr(C)]
pub struct radio_tab_t {
ident: *const c_char,
device: *const radio_device_t,
}
extern {
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);
fn radio_list_c();
fn radio_verify_config(device: *const radio_device_t);
fn radio_print_version(device: *const radio_device_t, stdout: *const libc::FILE);
fn radio_print_config(device: *const radio_device_t, file: *const libc::FILE, verbose: c_int);
@ -22,6 +28,8 @@ extern {
fn dfu_close();
fn hid_close();
fn serial_close();
fn get_radio_tab() -> *const radio_tab_t;
}
/// Connect to the radio via the serial port.
@ -62,8 +70,22 @@ pub fn upload(device: *const radio_device_t, cont_flag: c_int) {
/// List all supported radios.
pub fn list() {
println!("Supported radios:");
unsafe {
radio_list_c();
let mut ptr = get_radio_tab();
loop {
let ident_ptr = (*ptr).ident;
if ident_ptr.is_null() {
break;
}
let name_ptr = (*(*ptr).device).name;
let name = CStr::from_ptr(name_ptr).to_str().unwrap().to_string();
println!(" {}", name);
ptr = ptr.offset(1);
}
}
}