diff --git a/radio.c b/radio.c index b31273a..678c008 100644 --- a/radio.c +++ b/radio.c @@ -157,23 +157,6 @@ radio_device_t* radio_read_image(const char *filename) return dev; } -// -// Save firmware image to the binary file. -// -void radio_save_image(radio_device_t* device, const char *filename) -{ - FILE *img; - - fprintf(stderr, "Write codeplug to file '%s'.\n", filename); - img = fopen(filename, "wb"); - if (! img) { - perror(filename); - exit(-1); - } - device->save_image(device, img); - fclose(img); -} - // // Read the configuration from text file, and modify the firmware. // diff --git a/src/radio.rs b/src/radio.rs index 62e8826..0f5bc0f 100644 --- a/src/radio.rs +++ b/src/radio.rs @@ -20,10 +20,8 @@ extern { fn set_active_device(device: *const radio_device_t); - fn radio_print_version(device: *const radio_device_t, stdout: *const libc::FILE); fn radio_print_config(device: *const radio_device_t, file: *const libc::FILE, verbose: c_int); fn radio_read_image(filename: *const c_char) -> *const radio_device_t; - fn radio_save_image(device: *const radio_device_t, filename: *const c_char); 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); @@ -202,10 +200,19 @@ pub fn read_image(filename: &str) -> Radio { /// Save firmware image to the binary file. pub fn save_image(radio: &Radio, filename: &str) { - let device = radio.ptr; - let filename = CString::new(filename.to_string()).unwrap(); + + eprintln!("Write codeplug to file '{}'.", filename); + + let device = radio.ptr as *mut radio_device_t; + let file = std::fs::File::create(filename).unwrap(); + let fd = file.as_raw_fd(); + let mode = CString::new("wb").unwrap(); + unsafe { - radio_save_image(device, filename.as_ptr()) + let file = libc::fdopen(fd, mode.as_ptr()); + let save_image_fn = (*device).save_image.unwrap(); + save_image_fn(device, file); + libc::fclose(file); } }