#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <tee_client_api.h>

TEEC_UUID uuid = {
    .timeLow = 0x10000000,
    .timeMid = 0,
    .timeHiAndVersion = 0,
    .clockSeqAndNode = {0, 0, 0, 0, 0, 0x0, 0x0, 0x0},
};

int main(void)
{
    TEEC_Context context;
    TEEC_Session session;
    TEEC_Operation operation;
    uint32_t returnOrigin;
    TEEC_Result res = TEEC_InitializeContext(NULL, &context);

    if (res != TEEC_SUCCESS) {
        fprintf(stderr,"InitializeContext failed: %x\n", res);
        return 1;
    }

    res = TEEC_OpenSession(&context, &session, &uuid, 0, NULL, NULL, \
                           &returnOrigin);
    if (res != TEEC_SUCCESS) {
        fprintf(stderr,"TEEC_OpenSession returned %x from %x\n", res,
                returnOrigin);
        return 1;
    }
    char greering_to_SWd[] = "Hello from NWd";
    char response_from_SWd[0x100] = {'\0'};
    operation.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                                            TEEC_MEMREF_TEMP_OUTPUT,
                                            TEEC_NONE,
                                            TEEC_NONE);
    operation.params[0].tmpref.buffer = &greering_to_SWd;
    operation.params[0].tmpref.size = sizeof(greering_to_SWd);
    operation.params[1].tmpref.buffer = &response_from_SWd;
    operation.params[1].tmpref.size = sizeof(response_from_SWd);

    res = TEEC_InvokeCommand(&session, 0x0, &operation, &returnOrigin);
    if (res != TEEC_SUCCESS) {
        fprintf(stderr,"TEEC_InvokeCommand returned %x from %x\n", res,
                returnOrigin);
        return 1;
    }

    fprintf(stderr,"SWd responded by : %s\n", response_from_SWd);
    TEEC_CloseSession(&session);
    TEEC_FinalizeContext(&context);
    return (res != TEEC_SUCCESS);
}
