feat: add init and close library

This commit is contained in:
theo 2024-08-29 01:15:33 +02:00
parent 9829a189a4
commit 952c8561ba
Signed by: theo
SSH Key Fingerprint: SHA256:IbehMhSwpXrGUj7vj9iVvfdwe3g09IL9KLUz0zFzcXU

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;