1use std::ffi::{c_char, c_void};
19
20use log::{debug, warn};
21
22use super::{
23 context::{ContextManager, initialize_context_impl},
24 safe_ptr,
25 session::{
26 close_session_impl, invoke_command_impl, open_session_impl, request_cancellation_impl,
27 },
28 shared_memory::SharedMemoryManager,
29};
30use crate::{ErrorKind, ErrorOrigin, Result, raw};
31
32fn handle_error(result: Result<u32>, ret_origin: *mut u32) -> raw::TEEC_Result {
38 match result {
39 Ok(code) => code,
40 Err(e) => {
41 if !ret_origin.is_null() {
42 unsafe {
45 *ret_origin = e.origin().unwrap_or(ErrorOrigin::API) as u32;
46 }
47 }
48 e.raw_code()
49 }
50 }
51}
52
53#[unsafe(no_mangle)]
61pub extern "C" fn TEEC_InitializeContext(
62 _name: *const c_char,
63 ctx: *mut raw::TEEC_Context,
64) -> raw::TEEC_Result {
65 debug!("TEEC_InitializeContext");
66
67 match initialize_context_impl(ctx) {
68 Ok(_) => raw::TEEC_SUCCESS,
69 Err(e) => e.raw_code(),
70 }
71}
72
73#[unsafe(no_mangle)]
80pub extern "C" fn TEEC_FinalizeContext(ctx: *mut raw::TEEC_Context) {
81 debug!("[HOST] TEEC_FinalizeContext");
82
83 if let Ok(ctx_nn) = safe_ptr::deref(ctx) {
84 let ctx_ref = unsafe { ctx_nn.as_ref() };
87 let ctx_id = ctx_ref.imp.fd;
88 if ctx_id < 0 {
89 debug!("TEEC_FinalizeContext: context already finalized (fd={ctx_id})");
90 return;
91 }
92 }
93
94 SharedMemoryManager::release_by_context(ctx);
95 ContextManager::remove_context(ctx);
96}
97
98#[unsafe(no_mangle)]
112pub extern "C" fn TEEC_OpenSession(
113 ctx: *mut raw::TEEC_Context,
114 session: *mut raw::TEEC_Session,
115 destination: *const raw::TEEC_UUID,
116 connection_method: u32,
117 _connection_data: *const c_void,
118 operation: *mut raw::TEEC_Operation,
119 ret_origin: *mut u32,
120) -> raw::TEEC_Result {
121 debug!("TEEC_OpenSession");
122
123 let result = open_session_impl(ctx, session, destination, connection_method, operation);
124
125 handle_error(result, ret_origin)
126}
127
128#[unsafe(no_mangle)]
132pub extern "C" fn TEEC_CloseSession(session: *mut raw::TEEC_Session) {
133 debug!("TEEC_CloseSession");
134
135 if let Err(e) = close_session_impl(session) {
136 warn!("TEEC_CloseSession 失败:{e}");
138 }
139}
140
141#[unsafe(no_mangle)]
151pub extern "C" fn TEEC_InvokeCommand(
152 session: *mut raw::TEEC_Session,
153 cmd_id: u32,
154 operation: *mut raw::TEEC_Operation,
155 error_origin: *mut u32,
156) -> raw::TEEC_Result {
157 debug!("TEEC_InvokeCommand");
158
159 let result = invoke_command_impl(session, cmd_id, operation);
160
161 handle_error(result, error_origin)
162}
163
164#[unsafe(no_mangle)]
168pub extern "C" fn TEEC_RequestCancellation(operation: *mut raw::TEEC_Operation) {
169 debug!("TEEC_RequestCancellation");
170
171 if let Err(e) = request_cancellation_impl(operation) {
172 warn!("TEEC_RequestCancellation 失败:{e}");
174 }
175}
176
177#[unsafe(no_mangle)]
187pub extern "C" fn TEEC_RegisterSharedMemory(
188 ctx: *mut raw::TEEC_Context,
189 shm: *mut raw::TEEC_SharedMemory,
190) -> raw::TEEC_Result {
191 debug!("TEEC_RegisterSharedMemory");
192
193 match SharedMemoryManager::allocate(ctx, shm, true) {
194 Ok(_) => raw::TEEC_SUCCESS,
195 Err(e) => e.raw_code(),
196 }
197}
198
199#[unsafe(no_mangle)]
207pub extern "C" fn TEEC_RegisterSharedMemoryFileDescriptor(
208 _ctx: *mut raw::TEEC_Context,
209 _shm: *mut raw::TEEC_SharedMemory,
210 _fd: i32,
211) -> raw::TEEC_Result {
212 ErrorKind::NotImplemented.into()
214}
215
216#[unsafe(no_mangle)]
225pub extern "C" fn TEEC_AllocateSharedMemory(
226 ctx: *mut raw::TEEC_Context,
227 shm: *mut raw::TEEC_SharedMemory,
228) -> raw::TEEC_Result {
229 debug!("TEEC_AllocateSharedMemory");
230
231 match SharedMemoryManager::allocate(ctx, shm, false) {
232 Ok(_) => raw::TEEC_SUCCESS,
233 Err(e) => e.raw_code(),
234 }
235}
236
237#[unsafe(no_mangle)]
241pub extern "C" fn TEEC_ReleaseSharedMemory(shm: *mut raw::TEEC_SharedMemory) {
242 debug!("TEEC_ReleaseSharedMemory");
243 SharedMemoryManager::release(shm);
244}