diff --git a/radio.c b/radio.c index 4a1cb02..555cf4b 100644 --- a/radio.c +++ b/radio.c @@ -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); -} diff --git a/src/radio.rs b/src/radio.rs index 29eaf36..45ef5c7 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -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); } }