2024-08-02 22:58:32 +02:00
|
|
|
/// High level API for the SSV library,
|
|
|
|
/// based on the low level bindings in libssv.rs.
|
2024-07-29 20:36:14 +02:00
|
|
|
extern crate dotenv;
|
2024-08-02 00:08:49 +02:00
|
|
|
use libc::{c_void, size_t};
|
2024-08-02 22:58:32 +02:00
|
|
|
use std::env;
|
2024-07-23 14:15:06 +02:00
|
|
|
use std::ffi::CString;
|
2024-08-02 22:58:32 +02:00
|
|
|
use std::path::PathBuf;
|
2024-07-23 14:15:06 +02:00
|
|
|
use std::ptr;
|
2024-08-15 19:27:57 +02:00
|
|
|
use thiserror::Error;
|
2024-07-23 14:15:06 +02:00
|
|
|
|
2024-08-02 00:08:49 +02:00
|
|
|
use crate::cps::lire_carte;
|
|
|
|
use crate::libssv::{SSV_InitLIB2, SSV_LireConfig};
|
2024-07-23 14:15:06 +02:00
|
|
|
|
2024-08-15 19:27:57 +02:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum SSVDemoError {
|
|
|
|
#[error(transparent)]
|
|
|
|
CartePSReading(#[from] crate::cps::CartePSError),
|
|
|
|
#[error(transparent)]
|
|
|
|
SSVLibErrorCode(#[from] crate::libssv::LibSSVError),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ssv_init_lib_2() -> Result<(), SSVDemoError> {
|
2024-07-23 14:15:06 +02:00
|
|
|
let ini_str = env::var("SESAM_INI_PATH").expect("SESAM_INI_PATH must be set");
|
|
|
|
let ini = CString::new(ini_str).expect("CString::new failed");
|
|
|
|
unsafe {
|
|
|
|
let result = SSV_InitLIB2(ini.as_ptr());
|
|
|
|
println!("SSV_InitLIB2 result: {}", result);
|
2024-08-15 19:27:57 +02:00
|
|
|
if result != 0 {
|
|
|
|
return Err(crate::libssv::LibSSVError::StandardErrorCode {
|
|
|
|
code: result,
|
|
|
|
function: "SSV_InitLIB2",
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
2024-07-23 14:15:06 +02:00
|
|
|
}
|
2024-08-15 19:27:57 +02:00
|
|
|
Ok(())
|
2024-07-23 14:15:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-15 19:27:57 +02:00
|
|
|
fn ssv_lire_config() -> Result<(), SSVDemoError> {
|
2024-07-23 14:15:06 +02:00
|
|
|
let mut buffer: *mut c_void = ptr::null_mut();
|
|
|
|
let mut size: size_t = 0;
|
|
|
|
unsafe {
|
|
|
|
let result = SSV_LireConfig(&mut buffer, &mut size);
|
|
|
|
println!("SSV_LireConfig result: {}", result);
|
2024-08-15 19:27:57 +02:00
|
|
|
if result != 0 {
|
|
|
|
return Err(crate::libssv::LibSSVError::StandardErrorCode {
|
|
|
|
code: result,
|
|
|
|
function: "SSV_LireConfig",
|
|
|
|
}
|
|
|
|
.into());
|
|
|
|
}
|
2024-07-23 14:15:06 +02:00
|
|
|
|
|
|
|
if !buffer.is_null() {
|
|
|
|
let hex_values = std::slice::from_raw_parts(buffer as *const u8, size);
|
|
|
|
for &byte in hex_values {
|
|
|
|
print!("{:02X} ", byte);
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
|
|
|
|
libc::free(buffer);
|
|
|
|
}
|
|
|
|
}
|
2024-08-15 19:27:57 +02:00
|
|
|
Ok(())
|
2024-07-23 14:15:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-15 19:27:57 +02:00
|
|
|
pub fn demo() -> Result<(), SSVDemoError> {
|
2024-08-02 22:58:32 +02:00
|
|
|
// TODO : this is probably not working on release, because I'm not sure it exists a CARGO_MANIFEST_DIR and so it can find the `.env`
|
|
|
|
// Maybe we could use a system standard config path to store a config file
|
2024-08-15 19:27:57 +02:00
|
|
|
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
|
2024-07-27 00:31:01 +02:00
|
|
|
let manifest_path = PathBuf::from(manifest_dir);
|
|
|
|
dotenv::from_path(manifest_path.join(".env")).ok();
|
2024-07-23 14:15:06 +02:00
|
|
|
|
2024-08-02 22:58:32 +02:00
|
|
|
println!("------- Demo for the SSV library --------");
|
2024-07-23 14:15:06 +02:00
|
|
|
|
2024-08-15 19:27:57 +02:00
|
|
|
ssv_init_lib_2()?;
|
2024-08-02 22:58:32 +02:00
|
|
|
|
2024-08-01 10:46:58 +02:00
|
|
|
let code_pin = "1234";
|
|
|
|
let lecteur = "HID Global OMNIKEY 3x21 Smart Card Reader 0";
|
2024-08-15 19:27:57 +02:00
|
|
|
let carte_ps = lire_carte(code_pin, lecteur)?;
|
2024-08-01 10:46:58 +02:00
|
|
|
println!("CartePS: {:#?}", carte_ps);
|
|
|
|
|
2024-08-15 19:27:57 +02:00
|
|
|
ssv_lire_config()?;
|
2024-08-01 10:46:58 +02:00
|
|
|
|
2024-08-02 00:08:49 +02:00
|
|
|
println!("-----------------------------------------");
|
2024-08-15 19:27:57 +02:00
|
|
|
Ok(())
|
2024-08-01 10:46:58 +02:00
|
|
|
}
|