feat: Gestion des versions multiples de FSV dans le wrapper exposant les fonctions de la librairie

This commit is contained in:
Florian Briand 2024-09-30 23:40:55 +02:00
parent 473212c2e6
commit 59c325024a
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B

View File

@ -2,6 +2,8 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_snake_case)] #![allow(non_snake_case)]
use std::marker::PhantomData;
#[derive(Debug)] #[derive(Debug)]
pub enum SUPPORTED_FSV_VERSIONS { pub enum SUPPORTED_FSV_VERSIONS {
V1_40_14, // 1.40.14 V1_40_14, // 1.40.14
@ -17,32 +19,14 @@ impl SUPPORTED_FSV_VERSIONS {
} }
} }
pub mod BINDINGS { pub mod BINDINGS_V1_40_14 {
include!(concat!(env!("OUT_DIR"), "/bindings_1.40.14.rs"));
}
pub mod BINDINGS_V1_40_13 {
include!(concat!(env!("OUT_DIR"), "/bindings_1.40.13.rs")); include!(concat!(env!("OUT_DIR"), "/bindings_1.40.13.rs"));
} }
// We need to keep the this use statement to get `ssv_function` macro working well
use BINDINGS::*;
/// Macro to generate a function that implements a call to an external function in BINDINGS
macro_rules! ssv_function {
($binding:ty, $func_name:ident, {$($arg_name:ident: $arg_type:ty),*}) => {
/// # Safety
/// This function is unsafe because it calls an external function through FFI.
/// The caller must ensure that the provided arguments are valid and that the
/// external function is safe to call.
pub unsafe fn $func_name(&self, $($arg_name: $arg_type),*) -> Result<u16, Error> {
let func_struct: libloading::Symbol<'_, $binding> =
unsafe { self.library.get(stringify!($binding).as_bytes())? };
let func = match *func_struct {
Some(func) => func,
None => return Err(Error::SymbolMissing(stringify!($binding))),
};
Ok(func($($arg_name),*))
}
};
}
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]
@ -51,15 +35,121 @@ pub enum Error {
SymbolMissing(&'static str), SymbolMissing(&'static str),
} }
/// Macro to generate a function that implements a call to an external function in BINDINGS
macro_rules! ssv_function {
($binding:ty, $func_name:ident, {$($arg_name:ident: $arg_type:ty),*}) => {
/// # Safety
/// This function is unsafe because it calls an external function through FFI.
/// The caller must ensure that the provided arguments are valid and that the
/// external function is safe to call.
pub unsafe fn $func_name(&self, $($arg_name: $arg_type),*) -> Result<u16, Error> {
let symbol_name = match stringify!($binding)
.split(&[' ', ':'])
.last() {
Some(name) => name,
None => return Err(Error::SymbolMissing(stringify!($binding))),
};
let func_struct: libloading::Symbol<'_, $binding> =
unsafe { self.library.get(symbol_name.as_bytes())? };
let func = match *func_struct {
Some(func) => func,
None => return Err(Error::SymbolMissing(stringify!($binding))),
};
Ok(func($($arg_name),*))
}
};
}
/// `sealed::Sealed` trait is used to prevent external crates from implementing the LibVersion trait.
mod sealed { pub trait Sealed {}}
/// Wrapper around the SESAM-VITALE library /// Wrapper around the SESAM-VITALE library
/// This struct is responsible for loading the library and providing an interface to call its functions. /// This struct is responsible for loading the library and providing an interface to call its functions.
/// The library is loaded at creation and kept in memory until the struct is dropped. /// The library is loaded at creation and kept in memory until the struct is dropped.
#[derive(Debug)] pub trait SSVLibrary_Common {
pub struct SSVLibrary { fn new(path: &str) -> Result<Self, Error> where Self: Sized;
version: SUPPORTED_FSV_VERSIONS, }
pub trait SSVLibrary_Version: sealed::Sealed {}
pub struct V1_40_13 {}
impl sealed::Sealed for V1_40_13 {}
impl SSVLibrary_Version for V1_40_13 {}
pub struct V1_40_14 {}
impl sealed::Sealed for V1_40_14 {}
impl SSVLibrary_Version for V1_40_14 {}
pub struct SSVLibrary<Version: SSVLibrary_Version> {
_version: PhantomData<Version>,
library: libloading::Library, library: libloading::Library,
} }
impl<Version: SSVLibrary_Version> SSVLibrary_Common for SSVLibrary<Version> {
fn new(path: &str) -> Result<Self, Error> {
let library = unsafe { libloading::Library::new(path)?};
Ok(Self {
_version: PhantomData,
library
})
}
}
impl SSVLibrary<V1_40_14> {
pub fn library(&self) -> &libloading::Library {
&self.library
}
ssv_function!(BINDINGS_V1_40_14::SSV_InitLIB2, ssv_init_lib2, {
pcFichierSesam: *const i8
});
ssv_function!(BINDINGS_V1_40_14::SSV_LireConfig, ssv_lire_config, {
pZDataOut: *mut *mut libc::c_void,
psTailleDataOut: *mut usize
});
ssv_function!(BINDINGS_V1_40_14::SSV_LireCartePS, ssv_lire_carte_ps, {
NomRessourcePS: *const i8,
NomRessourceLecteur: *const i8,
CodePorteurPS: *const i8,
pZDataOut: *mut *mut libc::c_void,
pTailleZone: *mut usize
});
}
impl SSVLibrary<V1_40_13> {
ssv_function!(BINDINGS_V1_40_13::SSV_InitLIB2, ssv_init_lib2, {
pcFichierSesam: *const i8
});
ssv_function!(BINDINGS_V1_40_13::SSV_LireConfig, ssv_lire_config, {
pZDataOut: *mut *mut libc::c_void,
psTailleDataOut: *mut usize
});
ssv_function!(BINDINGS_V1_40_13::SSV_LireCartePS, ssv_lire_carte_ps, {
NomRessourcePS: *const i8,
NomRessourceLecteur: *const i8,
CodePorteurPS: *const i8,
pZDataOut: *mut *mut libc::c_void,
pTailleZone: *mut usize
});
}
pub fn get_library_path(version: SUPPORTED_FSV_VERSIONS) -> String {
let root_path = get_library_root_path();
let library_name = get_library_name();
let version = version.as_str();
format!("{root_path}/{version}/lib/{library_name}")
}
pub fn sesam_ini_path(version: SUPPORTED_FSV_VERSIONS) -> String {
let root_path = get_sesam_ini_root_path();
let version = version.as_str();
format!("{root_path}/{version}/conf/sesam.ini")
}
fn get_library_name() -> &'static str { fn get_library_name() -> &'static str {
// TODO : Use libloading::library_filename to get platform-specific filename ? // TODO : Use libloading::library_filename to get platform-specific filename ?
"libssvlux64.so" "libssvlux64.so"
@ -73,48 +163,6 @@ fn get_sesam_ini_root_path() -> &'static str {
"/etc/opt/santesocial/fsv" "/etc/opt/santesocial/fsv"
} }
impl SSVLibrary {
pub fn new(version: SUPPORTED_FSV_VERSIONS) -> Result<Self, Error> {
let library_path = Self::get_library_path(&version);
let library = unsafe { libloading::Library::new(library_path)? };
Ok(SSVLibrary { version, library })
}
pub fn library(&self) -> &libloading::Library {
&self.library
}
pub fn sesam_ini_path(&self) -> String {
let root_path = get_sesam_ini_root_path();
let version = self.version.as_str();
format!("{root_path}/{version}/conf/sesam.ini")
}
pub fn get_library_path(version: &SUPPORTED_FSV_VERSIONS) -> String {
let root_path = get_library_root_path();
let library_name = get_library_name();
let version = version.as_str();
format!("{root_path}/{version}/lib/{library_name}")
}
ssv_function!(SSV_InitLIB2, ssv_init_lib2, {
pcFichierSesam: *const i8
});
ssv_function!(SSV_LireConfig, ssv_lire_config, {
pZDataOut: *mut *mut libc::c_void,
psTailleDataOut: *mut usize
});
ssv_function!(SSV_LireCartePS, ssv_lire_carte_ps, {
NomRessourcePS: *const i8,
NomRessourceLecteur: *const i8,
CodePorteurPS: *const i8,
pZDataOut: *mut *mut libc::c_void,
pTailleZone: *mut usize
});
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::{ffi::CString, ptr}; use std::{ffi::CString, ptr};
@ -123,22 +171,22 @@ mod test {
#[test] #[test]
fn test_initlib2() { fn test_initlib2() {
let ssv_library = let lib_path = &get_library_path(SUPPORTED_FSV_VERSIONS::V1_40_13);
SSVLibrary::new(SUPPORTED_FSV_VERSIONS::V1_40_13).expect("SSVLibrary::new failed"); let ssv_library = SSVLibrary::<V1_40_13>::new(lib_path).expect("SSVLibrary::new failed");
let sesam_ini_str = let sesam_ini_str =
CString::new(ssv_library.sesam_ini_path()).expect("CString::new failed"); CString::new(sesam_ini_path(SUPPORTED_FSV_VERSIONS::V1_40_13)).expect("CString::new failed");
let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap(); let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap();
assert_eq!(result, 0); assert_eq!(result, 0);
} }
#[test] #[test]
fn test_lire_config_and_carte_ps() { fn test_lire_config_and_carte_ps() {
let ssv_library = let lib_path = &get_library_path(SUPPORTED_FSV_VERSIONS::V1_40_13);
SSVLibrary::new(SUPPORTED_FSV_VERSIONS::V1_40_13).expect("SSVLibrary::new failed"); let ssv_library = SSVLibrary::<V1_40_13>::new(lib_path).expect("SSVLibrary::new failed");
let sesam_ini_str = let sesam_ini_str =
CString::new(ssv_library.sesam_ini_path()).expect("CString::new failed"); CString::new(sesam_ini_path(SUPPORTED_FSV_VERSIONS::V1_40_13)).expect("CString::new failed");
let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap(); let result = unsafe { ssv_library.ssv_init_lib2(sesam_ini_str.as_ptr()) }.unwrap();
assert_eq!(result, 0); assert_eq!(result, 0);
@ -146,6 +194,7 @@ mod test {
let mut size: libc::size_t = 0; let mut size: libc::size_t = 0;
let result = unsafe { ssv_library.ssv_lire_config(&mut buffer_ptr, &mut size) }.unwrap(); let result = unsafe { ssv_library.ssv_lire_config(&mut buffer_ptr, &mut size) }.unwrap();
assert_eq!(result, 0); assert_eq!(result, 0);
unsafe { libc::free(buffer_ptr) };
let nom_ressource_ps = let nom_ressource_ps =
CString::new("Gemalto PC Twin Reader (645D94C3) 00 00").expect("CString::new failed"); CString::new("Gemalto PC Twin Reader (645D94C3) 00 00").expect("CString::new failed");
@ -165,5 +214,6 @@ mod test {
} }
.unwrap(); .unwrap();
assert_eq!(result, 0); assert_eq!(result, 0);
unsafe { libc::free(buffer_ptr) };
} }
} }