Modify C code to not use static radio_device_t*

Note: this changes program semantics slightly
This commit is contained in:
Greg Shuflin 2021-02-28 02:32:10 -08:00
parent 4183e0afc5
commit cd74a703c1
4 changed files with 33 additions and 26 deletions

11
radio.c
View File

@ -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");

View File

@ -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.

View File

@ -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");

View File

@ -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()));
}
}