Port radio_write_csv

This commit is contained in:
Greg Shuflin 2021-03-01 22:20:28 -08:00
parent f7aa60df21
commit 8430209828
2 changed files with 21 additions and 29 deletions

23
radio.c
View File

@ -153,26 +153,3 @@ badline: fprintf(stderr, "Invalid line: '%s'\n", line);
fclose(conf);
device->update_timestamp(device);
}
//
// Update contacts database on the device.
//
void radio_write_csv(radio_device_t* device, const char *filename)
{
FILE *csv;
if (!device->write_csv) {
fprintf(stderr, "%s does not support CSV database.\n", device->name);
return;
}
csv = fopen(filename, "r");
if (! csv) {
perror(filename);
return;
}
fprintf(stderr, "Read file '%s'.\n", filename);
device->write_csv(device, csv);
fclose(csv);
}

View File

@ -12,7 +12,7 @@ pub struct Radio {
static mut RADIO_TABLE: [(&'static str, &'static radio_device_t); 16] = unsafe {
[
("DR780", &radio_md380), // TYT MD-380, Retevis RT3, RT8
("DR780", &radio_md380), // TYT MD-380, Retevis RT3, RT8
("MD390", &radio_md390), // TYT MD-390
("MD-UV380", &radio_uv380), // TYT MD-UV380
("MD-UV390", &radio_uv390), // TYT MD-UV390, Retevis RT3S
@ -37,7 +37,6 @@ extern {
fn set_active_device(device: *const radio_device_t);
fn radio_parse_config(device: *const radio_device_t, filename: *const c_char);
fn radio_write_csv(device: *const radio_device_t, filename: *const c_char);
fn dfu_init(vid: c_uint, pid: c_uint) -> *const c_char;
fn dfu_reboot();
@ -361,12 +360,28 @@ pub fn print_version(radio: &Radio) {
}
}
/// Update CSV contacts database.
/// Update contacts database on the device.
pub fn write_csv(radio: &Radio, filename: &str) {
let device = radio.ptr;
let filename = CString::new(filename.to_string()).unwrap();
let device = radio.ptr as *mut radio_device_t;
unsafe {
radio_write_csv(device, filename.as_ptr());
let write_csv_fn = match (*device).write_csv {
Some(func) => func,
None => {
let name_ptr = (*device).name;
let name = CStr::from_ptr(name_ptr).to_str().unwrap();
eprintln!("{} does not support CSV database.", name);
exit(-1);
}
};
let file = std::fs::File::open(filename).unwrap();
eprintln!("Read file '{}.", filename);
let fd = file.as_raw_fd();
let mode = CString::new("r").unwrap();
let file = libc::fdopen(fd, mode.as_ptr());
write_csv_fn(device, file);
libc::fclose(file);
}
}