use std::ffi::{c_char, c_void};
use log::{debug, warn};
use super::{
context::{ContextManager, initialize_context_impl},
safe_ptr,
session::{
close_session_impl, invoke_command_impl, open_session_impl, request_cancellation_impl,
},
shared_memory::SharedMemoryManager,
};
use crate::{ErrorKind, ErrorOrigin, Result, raw};
fn handle_error(result: Result<u32>, ret_origin: *mut u32) -> raw::TEEC_Result {
match result {
Ok(code) => code,
Err(e) => {
if !ret_origin.is_null() {
unsafe {
*ret_origin = e.origin().unwrap_or(ErrorOrigin::API) as u32;
}
}
e.raw_code()
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_InitializeContext(
_name: *const c_char,
ctx: *mut raw::TEEC_Context,
) -> raw::TEEC_Result {
debug!("TEEC_InitializeContext");
match initialize_context_impl(ctx) {
Ok(_) => raw::TEEC_SUCCESS,
Err(e) => e.raw_code(),
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_FinalizeContext(ctx: *mut raw::TEEC_Context) {
debug!("TEEC_FinalizeContext");
if let Ok(ctx_nn) = safe_ptr::deref(ctx) {
let ctx_ref = unsafe { ctx_nn.as_ref() };
let ctx_id = ctx_ref.imp.fd;
if ctx_id < 0 {
debug!("TEEC_FinalizeContext: context already finalized (fd={ctx_id})");
return;
}
}
SharedMemoryManager::release_by_context(ctx);
ContextManager::remove_context(ctx);
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_OpenSession(
ctx: *mut raw::TEEC_Context,
session: *mut raw::TEEC_Session,
destination: *const raw::TEEC_UUID,
connection_method: u32,
_connection_data: *const c_void,
operation: *mut raw::TEEC_Operation,
ret_origin: *mut u32,
) -> raw::TEEC_Result {
debug!("TEEC_OpenSession");
let result = open_session_impl(ctx, session, destination, connection_method, operation);
handle_error(result, ret_origin)
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_CloseSession(session: *mut raw::TEEC_Session) {
debug!("TEEC_CloseSession");
if let Err(e) = close_session_impl(session) {
warn!("TEEC_CloseSession 失败:{e}");
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_InvokeCommand(
session: *mut raw::TEEC_Session,
cmd_id: u32,
operation: *mut raw::TEEC_Operation,
error_origin: *mut u32,
) -> raw::TEEC_Result {
debug!("TEEC_InvokeCommand");
let result = invoke_command_impl(session, cmd_id, operation);
handle_error(result, error_origin)
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_RequestCancellation(operation: *mut raw::TEEC_Operation) {
debug!("TEEC_RequestCancellation");
if let Err(e) = request_cancellation_impl(operation) {
warn!("TEEC_RequestCancellation 失败:{e}");
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_RegisterSharedMemory(
ctx: *mut raw::TEEC_Context,
shm: *mut raw::TEEC_SharedMemory,
) -> raw::TEEC_Result {
debug!("TEEC_RegisterSharedMemory");
match SharedMemoryManager::allocate(ctx, shm, true) {
Ok(_) => raw::TEEC_SUCCESS,
Err(e) => e.raw_code(),
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_RegisterSharedMemoryFileDescriptor(
_ctx: *mut raw::TEEC_Context,
_shm: *mut raw::TEEC_SharedMemory,
_fd: i32,
) -> raw::TEEC_Result {
ErrorKind::NotImplemented.into()
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_AllocateSharedMemory(
ctx: *mut raw::TEEC_Context,
shm: *mut raw::TEEC_SharedMemory,
) -> raw::TEEC_Result {
debug!("TEEC_AllocateSharedMemory");
match SharedMemoryManager::allocate(ctx, shm, false) {
Ok(_) => raw::TEEC_SUCCESS,
Err(e) => e.raw_code(),
}
}
#[unsafe(no_mangle)]
pub extern "C" fn TEEC_ReleaseSharedMemory(shm: *mut raw::TEEC_SharedMemory) {
debug!("TEEC_ReleaseSharedMemory");
SharedMemoryManager::release(shm);
}