131 lines
3.1 KiB
Rust
131 lines
3.1 KiB
Rust
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() -> *const RadioDeviceT;
|
|
fn radio_disconnect();
|
|
fn radio_download(device: *const RadioDeviceT);
|
|
fn radio_upload(device: *const RadioDeviceT, cont_flag: c_int);
|
|
fn radio_list_c();
|
|
fn radio_verify_config();
|
|
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);
|
|
fn radio_parse_config(filename: *const c_char);
|
|
fn radio_write_csv(filename: *const c_char);
|
|
}
|
|
|
|
/// Connect to the radio via the serial port.
|
|
/// Identify the type of device.
|
|
pub fn connect() -> *const RadioDeviceT {
|
|
unsafe {
|
|
radio_connect()
|
|
}
|
|
}
|
|
|
|
/// Close the serial port.
|
|
pub fn disconnect() {
|
|
unsafe {
|
|
radio_disconnect()
|
|
}
|
|
}
|
|
|
|
/// Read firmware image from the device
|
|
pub fn download(device: *const RadioDeviceT) {
|
|
unsafe {
|
|
radio_download(device)
|
|
}
|
|
}
|
|
|
|
/// Write firmware image to the device.
|
|
pub fn upload(device: *const RadioDeviceT, cont_flag: c_int) {
|
|
unsafe {
|
|
radio_upload(device, cont_flag)
|
|
}
|
|
}
|
|
|
|
/// List all supported radios.
|
|
pub fn list() {
|
|
|
|
unsafe {
|
|
radio_list_c();
|
|
}
|
|
}
|
|
|
|
/// Check the configuration.
|
|
pub fn verify_config() {
|
|
unsafe {
|
|
radio_verify_config();
|
|
}
|
|
}
|
|
|
|
/// Read firmware image from the binary file.
|
|
pub fn read_image(filename: &str) {
|
|
let filename = CString::new(filename.to_string()).unwrap();
|
|
unsafe {
|
|
radio_read_image(filename.as_ptr());
|
|
}
|
|
}
|
|
|
|
/// Save firmware image to the binary file.
|
|
pub fn save_image(filename: &str) {
|
|
let filename = CString::new(filename.to_string()).unwrap();
|
|
unsafe {
|
|
radio_save_image(filename.as_ptr())
|
|
}
|
|
}
|
|
|
|
/// Read the configuration from text file, and modify the firmware.
|
|
pub fn parse_config(filename: &str) {
|
|
let filename = CString::new(filename.to_string()).unwrap();
|
|
unsafe {
|
|
radio_parse_config(filename.as_ptr())
|
|
}
|
|
}
|
|
|
|
/// Print full information about the device configuration.
|
|
pub fn print_config(filename: &str) {
|
|
let file = std::fs::File::create(filename).unwrap();
|
|
let fd = file.as_raw_fd();
|
|
let mode = CString::new("w").unwrap();
|
|
unsafe {
|
|
let file =libc::fdopen(fd, mode.as_ptr());
|
|
radio_print_config(file, 1);
|
|
libc::fclose(file);
|
|
}
|
|
}
|
|
|
|
pub fn print_config_to_stdout() {
|
|
let mode = CString::new("w").unwrap();
|
|
unsafe {
|
|
let stdout = libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr());
|
|
let verbosity = if libc::isatty(libc::STDOUT_FILENO) == 1 {
|
|
0
|
|
} else {
|
|
1
|
|
};
|
|
radio_print_config(stdout, verbosity);
|
|
}
|
|
}
|
|
|
|
/// Print generic information about the device.
|
|
pub fn print_version(device: *const RadioDeviceT) {
|
|
let mode = CString::new("w").unwrap();
|
|
unsafe {
|
|
radio_print_version(device, libc::fdopen(libc::STDOUT_FILENO, mode.as_ptr()));
|
|
}
|
|
}
|
|
|
|
/// Update CSV contacts database.
|
|
pub fn write_csv(filename: &str) {
|
|
let filename = CString::new(filename.to_string()).unwrap();
|
|
unsafe {
|
|
radio_write_csv(filename.as_ptr());
|
|
}
|
|
}
|