#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/queue.h>
#include <errno.h>
#include <error.h>
#include "tee_client_api.h"
#include "teec_trace.h"
static void format_uuid_string(TEEC_UUID *uuid, char *buf, size_t buf_size)
{
if (!uuid || !buf)
return;
snprintf(buf, buf_size,
"UUID: { 0x%.8x 0x%.4x 0x%.4x { 0x%.2x 0x%.2x 0x%.2x 0x%.2x "
"0x%.2x 0x%.2x 0x%.2x 0x%.2x }}",
uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
}
TEEC_Result init_tee_context(TEEC_Context *ctx)
{
TEEC_Result res = TEEC_InitializeContext(NULL, ctx);
if (res != TEEC_SUCCESS) {
EMSG("TEEC_InitializeContext failed with code 0x%x", res);
}
return res;
}
TEEC_Result open_tee_session(TEEC_Context *ctx,
TEEC_Session *session,
TEEC_UUID *uuid,
uint32_t *ret_orig)
{
TEEC_Result res = TEEC_OpenSession(ctx, session, uuid,
TEEC_LOGIN_PUBLIC,
NULL, NULL, ret_orig);
if (res != TEEC_SUCCESS) {
char buf[128] = {0};
format_uuid_string(uuid, buf, sizeof(buf));
EMSG("%s TEEC_Opensession failed with code 0x%x origin 0x%x",
buf, res, *ret_orig);
}
return res;
}
TEEC_Result init_tee_session(TEEC_Context *ctx,
TEEC_Session *session,
TEEC_UUID *uuid)
{
if (!ctx || !session || !uuid) {
EMSG("Invalid parameters for session initialization");
return TEEC_ERROR_BAD_PARAMETERS;
}
TEEC_Result res;
uint32_t origin;
char uuid_str[128] = {0};
res = TEEC_InitializeContext(NULL, ctx);
if (res != TEEC_SUCCESS) {
EMSG("TEEC_InitializeContext failed: 0x%x", res);
return res;
}
res = TEEC_OpenSession(ctx, session, uuid, TEEC_LOGIN_PUBLIC,
NULL, NULL, &origin);
if (res != TEEC_SUCCESS) {
format_uuid_string(uuid, uuid_str, sizeof(uuid_str));
EMSG("%s TEEC_OpenSession failed: 0x%x (origin: 0x%x)",
uuid_str, res, origin);
TEEC_FinalizeContext(ctx);
return res;
}
return TEEC_SUCCESS;
}
void terminate_tee_session(TEEC_Context *ctx, TEEC_Session *session)
{
if (!ctx || !session)
return;
TEEC_CloseSession(session);
TEEC_FinalizeContext(ctx);
}
TEEC_Result invoke_inited_tee_command(TEEC_Session *session,
uint32_t cmd_id,
TEEC_Operation *operation)
{
if (!session) {
EMSG("Session is NULL");
return TEEC_ERROR_BAD_PARAMETERS;
}
uint32_t origin;
TEEC_Result res = TEEC_InvokeCommand(session, cmd_id, operation, &origin);
if (res != TEEC_SUCCESS) {
EMSG("TEEC_InvokeCommand [0x%x] failed: 0x%x (origin: %u)",
cmd_id, res, origin);
}
return res;
}
TEEC_Result invoke_tee_command(TEEC_UUID *uuid,
uint32_t cmd_id,
TEEC_Operation *operation)
{
if (!uuid || !operation) {
EMSG("Invalid parameters for command invocation");
return TEEC_ERROR_BAD_PARAMETERS;
}
TEEC_Context ctx = {0};
TEEC_Session session = {0};
TEEC_Result res;
char uuid_str[128] = {0};
res = init_tee_session(&ctx, &session, uuid);
if (res != TEEC_SUCCESS) {
return res;
}
res = invoke_inited_tee_command(&session, cmd_id, operation);
if (res != TEEC_SUCCESS) {
format_uuid_string(uuid, uuid_str, sizeof(uuid_str));
EMSG("%s Command 0x%x failed", uuid_str, cmd_id);
}
terminate_tee_session(&ctx, &session);
return res;
}