Use Radio wrapper type

This commit is contained in:
Greg Shuflin 2021-03-01 00:54:36 -08:00
parent 61544fd23f
commit 25571023a3
2 changed files with 50 additions and 35 deletions

View File

@ -97,8 +97,8 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
} }
let device = radio::connect(); let device = radio::connect();
radio::read_image(&matches.free[0]); radio::read_image(&matches.free[0]);
radio::print_version(device); radio::print_version(&device);
radio::upload(device, 0); radio::upload(&device, 0);
radio::disconnect(); radio::disconnect();
} else if config_flag { } else if config_flag {
let conf_args = matches.free.len(); let conf_args = matches.free.len();
@ -115,19 +115,19 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
// Apply text config to image file. // Apply text config to image file.
let device = radio::connect(); //NOTE this changes the semantics of the program let device = radio::connect(); //NOTE this changes the semantics of the program
radio::read_image(&img); radio::read_image(&img);
radio::print_version(device); radio::print_version(&device);
radio::parse_config(device, &config_filename); radio::parse_config(&device, &config_filename);
radio::verify_config(device); radio::verify_config(&device);
radio::save_image(device, "device.img"); radio::save_image(&device, "device.img");
} else { } else {
// Update device from text config file. // Update device from text config file.
let device = radio::connect(); let device = radio::connect();
radio::download(device, trace_flag); radio::download(&device, trace_flag);
radio::print_version(device); radio::print_version(&device);
radio::save_image(device, "device.img"); radio::save_image(&device, "device.img");
radio::parse_config(device, &config_filename); radio::parse_config(&device, &config_filename);
radio::verify_config(device); radio::verify_config(&device);
radio::upload(device, 1); radio::upload(&device, 1);
radio::disconnect(); radio::disconnect();
} }
} else if verify_flag { } else if verify_flag {
@ -136,8 +136,8 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
} }
// Verify text config file. // Verify text config file.
let device = radio::connect(); let device = radio::connect();
radio::parse_config(device, &matches.free[0]); radio::parse_config(&device, &matches.free[0]);
radio::verify_config(device); radio::verify_config(&device);
radio::disconnect(); radio::disconnect();
} else if read_flag { } else if read_flag {
if matches.free.len() != 0 { if matches.free.len() != 0 {
@ -146,16 +146,16 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
// Dump device to image file. // Dump device to image file.
let device = radio::connect(); let device = radio::connect();
radio::download(device, trace_flag); radio::download(&device, trace_flag);
radio::print_version(device); radio::print_version(&device);
radio::disconnect(); radio::disconnect();
radio::save_image(device, "device.img"); radio::save_image(&device, "device.img");
// Print configuration to file. // Print configuration to file.
let filename = "device.conf"; let filename = "device.conf";
println!("Print configuration to file '{}.", filename); println!("Print configuration to file '{}.", filename);
radio::print_config(device, filename); radio::print_config(&device, filename);
} else if csv_flag { } else if csv_flag {
if matches.free.len() != 1 { if matches.free.len() != 1 {
@ -163,14 +163,14 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
} }
let device = radio::connect(); let device = radio::connect();
radio::write_csv(device, &matches.free[0]); radio::write_csv(&device, &matches.free[0]);
radio::disconnect(); radio::disconnect();
} else { } else {
if matches.free.len() != 1 { if matches.free.len() != 1 {
print_usage(); print_usage();
} }
let device = radio::read_image(&matches.free[0]); let device = radio::read_image(&matches.free[0]);
radio::print_config_to_stdout(device); radio::print_config_to_stdout(&device);
} }
exit(0); exit(0);

View File

@ -5,6 +5,10 @@ use std::process::exit;
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
pub struct Radio {
ptr: *const radio_device_t
}
#[repr(C)] #[repr(C)]
pub struct radio_tab_t { pub struct radio_tab_t {
ident: *const c_char, ident: *const c_char,
@ -41,7 +45,7 @@ extern {
/// Connect to the radio via the serial port. /// Connect to the radio via the serial port.
/// and identify the type of device. /// and identify the type of device.
pub fn connect() -> *const radio_device_t { pub fn connect() -> Radio {
unsafe { unsafe {
let mut ident: *const c_char; let mut ident: *const c_char;
// Try TYT MD family. // Try TYT MD family.
@ -95,7 +99,7 @@ pub fn connect() -> *const radio_device_t {
println!("Connect to {}", name); println!("Connect to {}", name);
set_active_device(device); set_active_device(device);
return device; return Radio { ptr: device };
} }
} }
@ -113,7 +117,9 @@ pub fn disconnect() {
} }
/// Read firmware image from the device /// Read firmware image from the device
pub fn download(device: *const radio_device_t, trace: bool) { pub fn download(radio: &Radio, trace: bool) {
let device = radio.ptr;
if !trace { if !trace {
eprint!("Read device: "); eprint!("Read device: ");
@ -131,7 +137,8 @@ pub fn download(device: *const radio_device_t, trace: bool) {
} }
/// Write firmware image to the device. /// Write firmware image to the device.
pub fn upload(device: *const radio_device_t, cont_flag: c_int) { pub fn upload(radio: &Radio, cont_flag: c_int) {
let device = radio.ptr;
unsafe { unsafe {
radio_upload(device, cont_flag) radio_upload(device, cont_flag)
} }
@ -160,22 +167,25 @@ pub fn list() {
} }
/// Check the configuration. /// Check the configuration.
pub fn verify_config(device: *const radio_device_t) { pub fn verify_config(radio: &Radio) {
let device = radio.ptr;
unsafe { unsafe {
radio_verify_config(device); radio_verify_config(device);
} }
} }
/// Read firmware image from the binary file. /// Read firmware image from the binary file.
pub fn read_image(filename: &str) -> *const radio_device_t { pub fn read_image(filename: &str) -> Radio {
let filename = CString::new(filename.to_string()).unwrap(); let filename = CString::new(filename.to_string()).unwrap();
unsafe { let ptr = unsafe {
radio_read_image(filename.as_ptr()) radio_read_image(filename.as_ptr())
} };
Radio { ptr }
} }
/// Save firmware image to the binary file. /// Save firmware image to the binary file.
pub fn save_image(device: *const radio_device_t, filename: &str) { pub fn save_image(radio: &Radio, filename: &str) {
let device = radio.ptr;
let filename = CString::new(filename.to_string()).unwrap(); let filename = CString::new(filename.to_string()).unwrap();
unsafe { unsafe {
radio_save_image(device, filename.as_ptr()) radio_save_image(device, filename.as_ptr())
@ -183,7 +193,8 @@ pub fn save_image(device: *const radio_device_t, filename: &str) {
} }
/// Read the configuration from text file, and modify the firmware. /// Read the configuration from text file, and modify the firmware.
pub fn parse_config(device: *const radio_device_t, filename: &str) { pub fn parse_config(radio: &Radio, filename: &str) {
let device = radio.ptr;
let filename = CString::new(filename.to_string()).unwrap(); let filename = CString::new(filename.to_string()).unwrap();
unsafe { unsafe {
radio_parse_config(device, filename.as_ptr()) radio_parse_config(device, filename.as_ptr())
@ -191,7 +202,8 @@ pub fn parse_config(device: *const radio_device_t, filename: &str) {
} }
/// Print full information about the device configuration. /// Print full information about the device configuration.
pub fn print_config(device: *const radio_device_t, filename: &str) { pub fn print_config(radio: &Radio, filename: &str) {
let device = radio.ptr;
let file = std::fs::File::create(filename).unwrap(); let file = std::fs::File::create(filename).unwrap();
let fd = file.as_raw_fd(); let fd = file.as_raw_fd();
let mode = CString::new("w").unwrap(); let mode = CString::new("w").unwrap();
@ -202,7 +214,8 @@ pub fn print_config(device: *const radio_device_t, filename: &str) {
} }
} }
pub fn print_config_to_stdout(device: *const radio_device_t) { pub fn print_config_to_stdout(radio: &Radio) {
let device = radio.ptr;
let mode = CString::new("w").unwrap(); let mode = CString::new("w").unwrap();
unsafe { unsafe {
let stdout = libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr()); let stdout = libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr());
@ -216,7 +229,8 @@ pub fn print_config_to_stdout(device: *const radio_device_t) {
} }
/// Print generic information about the device. /// Print generic information about the device.
pub fn print_version(device: *const radio_device_t) { pub fn print_version(radio: &Radio) {
let device = radio.ptr;
let mode = CString::new("w").unwrap(); let mode = CString::new("w").unwrap();
unsafe { unsafe {
radio_print_version(device, libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr())); radio_print_version(device, libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr()));
@ -224,7 +238,8 @@ pub fn print_version(device: *const radio_device_t) {
} }
/// Update CSV contacts database. /// Update CSV contacts database.
pub fn write_csv(device: *const radio_device_t, filename: &str) { pub fn write_csv(radio: &Radio, filename: &str) {
let device = radio.ptr;
let filename = CString::new(filename.to_string()).unwrap(); let filename = CString::new(filename.to_string()).unwrap();
unsafe { unsafe {
radio_write_csv(device, filename.as_ptr()); radio_write_csv(device, filename.as_ptr());