From 7a39fd1ae2ba259cb2d6aea5ada53e39aa2aa1ad Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 1 Mar 2021 03:18:39 -0800 Subject: [PATCH] Move radio_is_compatible to Rust --- radio.c | 20 ++------------------ src/radio.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/radio.c b/radio.c index 43d329d..56799ce 100644 --- a/radio.c +++ b/radio.c @@ -35,6 +35,7 @@ #include "radio.h" #include "util.h" +/* typedef struct { char* ident; radio_device_t* device; } radio_tab_t; static radio_tab_t radio_tab[] = { @@ -56,6 +57,7 @@ static radio_tab_t radio_tab[] = { { "ZN><:", &radio_rt27d }, // Radtel RT-27D { 0, 0 } }; +*/ static radio_device_t *active_device; // Device-dependent interface @@ -280,21 +282,3 @@ void radio_write_csv(radio_device_t* device, const char *filename) device->write_csv(device, csv); fclose(csv); } - -// -// Check for compatible radio model. -// -int radio_is_compatible(const char *name) -{ - radio_device_t* dev = get_active_device(); - int i; - - for (i=0; radio_tab[i].ident; i++) { - // Radio is compatible when it has the same parse routine. - if (dev->parse_parameter == radio_tab[i].device->parse_parameter && - strcasecmp(name, radio_tab[i].device->name) == 0) { - return 1; - } - } - return 0; -} diff --git a/src/radio.rs b/src/radio.rs index 3abd851..eba845b 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -33,6 +33,7 @@ static mut RADIO_TABLE: [(&'static str, &'static radio_device_t); 16] = unsafe { extern { + fn get_active_device() -> *const radio_device_t; fn set_active_device(device: *const radio_device_t); fn radio_print_config(device: *const radio_device_t, file: *const libc::FILE, verbose: c_int); @@ -294,3 +295,25 @@ pub fn write_csv(radio: &Radio, filename: &str) { radio_write_csv(device, filename.as_ptr()); } } + +/// Check for compatible radio model. +#[no_mangle] +pub extern "C" fn radio_is_compatible(name: *const c_char) -> c_int { + unsafe { + let name = CStr::from_ptr(name).to_str().unwrap(); + let dev = get_active_device(); + + for (_, device) in RADIO_TABLE.iter() { + // Radio is compatible when it has the same parse routine. + let same_parse = (*dev).parse_parameter.unwrap() == (*device).parse_parameter.unwrap(); + let name_ptr = (*device).name; + let device_name = CStr::from_ptr(name_ptr).to_str().unwrap(); + let same_name = name.eq_ignore_ascii_case(device_name); + + if same_parse && same_name { + return 1; + } + } + } + return 0; +}