From cd74a703c1e67f5e9dcf28a205037d4c603fac5a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 28 Feb 2021 02:32:10 -0800 Subject: [PATCH] Modify C code to not use static radio_device_t* Note: this changes program semantics slightly --- radio.c | 11 ++++++----- radio.h | 8 ++++---- src/lib.rs | 19 ++++++++++--------- src/radio.rs | 21 +++++++++++++-------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/radio.c b/radio.c index c5634d4..cef88a0 100644 --- a/radio.c +++ b/radio.c @@ -80,15 +80,15 @@ void radio_disconnect() // // Print a generic information about the device. // -void radio_print_version(FILE *out) +void radio_print_version(radio_device_t* dev, FILE *out) { - device->print_version(device, out); + dev->print_version(dev, out); } // // Connect to the radio and identify the type of device. // -void radio_connect() +radio_device_t* radio_connect() { const char *ident; int i; @@ -122,6 +122,7 @@ void radio_connect() exit(-1); } fprintf(stderr, "Connect to %s.\n", device->name); + return device; } // @@ -140,7 +141,7 @@ void radio_list_c() // // Read firmware image from the device. // -void radio_download() +void radio_download(radio_device_t* dev) { radio_progress = 0; if (! trace_flag) { @@ -148,7 +149,7 @@ void radio_download() fflush(stderr); } - device->download(device); + dev->download(dev); if (! trace_flag) fprintf(stderr, " done.\n"); diff --git a/radio.h b/radio.h index 48334e6..d63223f 100644 --- a/radio.h +++ b/radio.h @@ -30,7 +30,7 @@ // Connect to the radio via the serial port. // Identify the type of device. // -void radio_connect(void); +//void radio_connect(void); // // Close the serial port. @@ -40,7 +40,7 @@ void radio_disconnect(void); // // Read firmware image from the device. // -void radio_download(void); +//void radio_download(void); // // Write firmware image to the device. @@ -50,7 +50,7 @@ void radio_upload(int cont_flag); // // Print a generic information about the device. // -void radio_print_version(FILE *out); +//void radio_print_version(FILE *out); // // Print full information about the device configuration. @@ -85,7 +85,7 @@ void radio_write_csv(const char *filename); // // List all supported radios. // -void radio_list_c(void); +//void radio_list_c(void); // // Check for compatible radio model. diff --git a/src/lib.rs b/src/lib.rs index 5af7bf7..f58deb3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,9 +92,9 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int print_usage(); } unsafe { - radio::connect(); + let device = radio::connect(); radio::read_image(&matches.free[0]); - radio::print_version(); + radio::print_version(device); radio_upload(0); radio::disconnect(); } @@ -111,17 +111,18 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int if let Some(img) = image_filename { // Apply text config to image file. + let device = radio::connect(); //NOTE this changes the semantics of the program radio::read_image(&img); - radio::print_version(); + radio::print_version(device); radio::parse_config(&config_filename); radio::verify_config(); radio::save_image("device.img"); } else { // Update device from text config file. unsafe { - radio::connect(); - radio::download(); - radio::print_version(); + let device = radio::connect(); + radio::download(device); + radio::print_version(device); radio::save_image("device.img"); radio::parse_config(&config_filename); radio::verify_config(); @@ -144,9 +145,9 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int } // Dump device to image file. - radio::connect(); - radio::download(); - radio::print_version(); + let device = radio::connect(); + radio::download(device); + radio::print_version(device); radio::disconnect(); radio::save_image("device.img"); diff --git a/src/radio.rs b/src/radio.rs index 99a926e..3f74eb6 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -2,13 +2,16 @@ use std::ffi::CString; use libc::{c_char, c_int}; use std::os::unix::io::AsRawFd; +#[repr(C)] +pub struct RadioDeviceT { _private: [u8; 0] } + extern { - fn radio_connect(); + fn radio_connect() -> *const RadioDeviceT; fn radio_disconnect(); - fn radio_download(); + fn radio_download(device: *const RadioDeviceT); fn radio_list_c(); fn radio_verify_config(); - fn radio_print_version(stdout: *const libc::FILE); + fn radio_print_version(device: *const RadioDeviceT, stdout: *const libc::FILE); fn radio_print_config(file: *const libc::FILE, verbose: c_int); fn radio_read_image(filename: *const c_char); fn radio_save_image(filename: *const c_char); @@ -16,7 +19,7 @@ extern { fn radio_write_csv(filename: *const c_char); } -pub fn connect() { +pub fn connect() -> *const RadioDeviceT { unsafe { radio_connect() } @@ -28,13 +31,15 @@ pub fn disconnect() { } } -pub fn download() { +pub fn download(device: *const RadioDeviceT) { unsafe { - radio_download() + radio_download(device) } } pub fn list() { + + unsafe { radio_list_c(); } @@ -91,10 +96,10 @@ pub fn print_config_to_stdout() { } } -pub fn print_version() { +pub fn print_version(device: *const RadioDeviceT) { let mode = CString::new("w").unwrap(); unsafe { - radio_print_version(libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr())); + radio_print_version(device, libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr())); } }