From 8f544cfa95e561373aa51cd728454b69881c173d Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 28 Feb 2021 19:41:21 -0800 Subject: [PATCH] Compute radio list in rust --- radio.c | 24 +++++++----------------- src/radio.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/radio.c b/radio.c index d0723b0..e5c0ba0 100644 --- a/radio.c +++ b/radio.c @@ -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. // diff --git a/src/radio.rs b/src/radio.rs index bc06713..40cddae 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -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); + } } }