From ad5b0547e4e8143ab667f78f8fab9dd0782828cd Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 1 Mar 2021 03:03:54 -0800 Subject: [PATCH] Put radio table into rust --- radio.c | 4 ---- src/radio.rs | 25 ++++--------------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/radio.c b/radio.c index 678c008..43d329d 100644 --- a/radio.c +++ b/radio.c @@ -57,10 +57,6 @@ static radio_tab_t radio_tab[] = { { 0, 0 } }; -radio_tab_t* get_radio_tab() { - return radio_tab; -} - static radio_device_t *active_device; // Device-dependent interface radio_device_t* get_active_device() { diff --git a/src/radio.rs b/src/radio.rs index 381c4d2..3abd851 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -10,13 +10,6 @@ pub struct Radio { ptr: *const radio_device_t } -#[repr(C)] -pub struct radio_tab_t { - ident: *const c_char, - device: *mut radio_device_t, -} - - static mut RADIO_TABLE: [(&'static str, &'static radio_device_t); 16] = unsafe { [ ("DR780", &radio_md380), // TYT MD-380, Retevis RT3, RT8 @@ -58,8 +51,6 @@ extern { fn serial_init(vid: c_int, pid: c_int) -> c_int; fn serial_identify() -> *const c_char; fn serial_close(); - - fn get_radio_tab() -> *const radio_tab_t; } /// Connect to the radio via the serial port. @@ -92,20 +83,12 @@ pub fn connect() -> Radio { 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 = (*radio_tab_ptr).ident; - if ident_ptr.is_null() { + + for (table_ident, device_ptr) in RADIO_TABLE.iter() { + if ident_str == *table_ident { + device = *device_ptr as *const radio_device_t; break; } - - 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; - } - - radio_tab_ptr = radio_tab_ptr.offset(1); } if device.is_null() {