Use Radio wrapper type
This commit is contained in:
parent
61544fd23f
commit
25571023a3
40
src/lib.rs
40
src/lib.rs
@ -97,8 +97,8 @@ pub extern "C" fn rust_main(_argc: c_int, _argv: *const *const c_char) -> c_int
|
||||
}
|
||||
let device = radio::connect();
|
||||
radio::read_image(&matches.free[0]);
|
||||
radio::print_version(device);
|
||||
radio::upload(device, 0);
|
||||
radio::print_version(&device);
|
||||
radio::upload(&device, 0);
|
||||
radio::disconnect();
|
||||
} else if config_flag {
|
||||
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.
|
||||
let device = radio::connect(); //NOTE this changes the semantics of the program
|
||||
radio::read_image(&img);
|
||||
radio::print_version(device);
|
||||
radio::parse_config(device, &config_filename);
|
||||
radio::verify_config(device);
|
||||
radio::save_image(device, "device.img");
|
||||
radio::print_version(&device);
|
||||
radio::parse_config(&device, &config_filename);
|
||||
radio::verify_config(&device);
|
||||
radio::save_image(&device, "device.img");
|
||||
} else {
|
||||
// Update device from text config file.
|
||||
let device = radio::connect();
|
||||
radio::download(device, trace_flag);
|
||||
radio::print_version(device);
|
||||
radio::save_image(device, "device.img");
|
||||
radio::parse_config(device, &config_filename);
|
||||
radio::verify_config(device);
|
||||
radio::upload(device, 1);
|
||||
radio::download(&device, trace_flag);
|
||||
radio::print_version(&device);
|
||||
radio::save_image(&device, "device.img");
|
||||
radio::parse_config(&device, &config_filename);
|
||||
radio::verify_config(&device);
|
||||
radio::upload(&device, 1);
|
||||
radio::disconnect();
|
||||
}
|
||||
} 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.
|
||||
let device = radio::connect();
|
||||
radio::parse_config(device, &matches.free[0]);
|
||||
radio::verify_config(device);
|
||||
radio::parse_config(&device, &matches.free[0]);
|
||||
radio::verify_config(&device);
|
||||
radio::disconnect();
|
||||
} else if read_flag {
|
||||
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.
|
||||
let device = radio::connect();
|
||||
radio::download(device, trace_flag);
|
||||
radio::print_version(device);
|
||||
radio::download(&device, trace_flag);
|
||||
radio::print_version(&device);
|
||||
radio::disconnect();
|
||||
radio::save_image(device, "device.img");
|
||||
radio::save_image(&device, "device.img");
|
||||
|
||||
// Print configuration to file.
|
||||
let filename = "device.conf";
|
||||
println!("Print configuration to file '{}.", filename);
|
||||
|
||||
radio::print_config(device, filename);
|
||||
radio::print_config(&device, filename);
|
||||
|
||||
} else if csv_flag {
|
||||
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();
|
||||
radio::write_csv(device, &matches.free[0]);
|
||||
radio::write_csv(&device, &matches.free[0]);
|
||||
radio::disconnect();
|
||||
} else {
|
||||
if matches.free.len() != 1 {
|
||||
print_usage();
|
||||
}
|
||||
let device = radio::read_image(&matches.free[0]);
|
||||
radio::print_config_to_stdout(device);
|
||||
radio::print_config_to_stdout(&device);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
|
43
src/radio.rs
43
src/radio.rs
@ -5,6 +5,10 @@ use std::process::exit;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||
|
||||
pub struct Radio {
|
||||
ptr: *const radio_device_t
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct radio_tab_t {
|
||||
ident: *const c_char,
|
||||
@ -41,7 +45,7 @@ extern {
|
||||
|
||||
/// Connect to the radio via the serial port.
|
||||
/// and identify the type of device.
|
||||
pub fn connect() -> *const radio_device_t {
|
||||
pub fn connect() -> Radio {
|
||||
unsafe {
|
||||
let mut ident: *const c_char;
|
||||
// Try TYT MD family.
|
||||
@ -95,7 +99,7 @@ pub fn connect() -> *const radio_device_t {
|
||||
println!("Connect to {}", name);
|
||||
|
||||
set_active_device(device);
|
||||
return device;
|
||||
return Radio { ptr: device };
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,7 +117,9 @@ pub fn disconnect() {
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
eprint!("Read device: ");
|
||||
@ -131,7 +137,8 @@ pub fn download(device: *const radio_device_t, trace: bool) {
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
radio_upload(device, cont_flag)
|
||||
}
|
||||
@ -160,22 +167,25 @@ pub fn list() {
|
||||
}
|
||||
|
||||
/// Check the configuration.
|
||||
pub fn verify_config(device: *const radio_device_t) {
|
||||
pub fn verify_config(radio: &Radio) {
|
||||
let device = radio.ptr;
|
||||
unsafe {
|
||||
radio_verify_config(device);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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();
|
||||
unsafe {
|
||||
let ptr = unsafe {
|
||||
radio_read_image(filename.as_ptr())
|
||||
}
|
||||
};
|
||||
Radio { ptr }
|
||||
}
|
||||
|
||||
/// 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();
|
||||
unsafe {
|
||||
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.
|
||||
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();
|
||||
unsafe {
|
||||
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.
|
||||
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 fd = file.as_raw_fd();
|
||||
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();
|
||||
unsafe {
|
||||
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.
|
||||
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();
|
||||
unsafe {
|
||||
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.
|
||||
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();
|
||||
unsafe {
|
||||
radio_write_csv(device, filename.as_ptr());
|
||||
|
Loading…
Reference in New Issue
Block a user