feat: add init and close library

This commit is contained in:
theo 2024-08-29 01:15:33 +02:00 committed by Florian Briand
parent 981a5d34c5
commit ecd84bf242
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B

View File

@ -1,8 +1,8 @@
mod bindings; mod bindings;
pub mod types; pub mod types;
use bindings::SSV_LireConfig; use bindings::{SSV_InitLIB2, SSV_LireConfig, SSV_TermLIB};
use std::{fmt, ptr}; use std::{ffi::CString, fmt, path::Path, ptr};
use types::serialization_types::{read_from_buffer, Configuration}; use types::serialization_types::{read_from_buffer, Configuration};
#[derive(Debug)] #[derive(Debug)]
@ -16,6 +16,29 @@ impl fmt::Display for SesamVitaleError {
} }
} }
pub fn init_library(sesam_ini_path: &Path) -> Result<(), SesamVitaleError> {
// TODO: better error handling
let path_str = sesam_ini_path.to_str().unwrap();
let path_ptr = CString::new(path_str).expect("failed to create cstring");
let exit_code: u16 = unsafe { SSV_InitLIB2(path_ptr.as_ptr()) };
if exit_code != 0 {
let error = SesamVitaleError { code: exit_code };
return Err(error);
};
Ok(())
}
pub fn close_library() -> Result<(), SesamVitaleError> {
let exit_code: u16 = unsafe { SSV_TermLIB() };
if exit_code != 0 {
let error = SesamVitaleError { code: exit_code };
return Err(error);
};
Ok(())
}
pub fn read_config() -> Result<Configuration, SesamVitaleError> { pub fn read_config() -> Result<Configuration, SesamVitaleError> {
let mut buffer_ptr: *mut libc::c_void = ptr::null_mut(); let mut buffer_ptr: *mut libc::c_void = ptr::null_mut();
let mut size: libc::size_t = 0; let mut size: libc::size_t = 0;