/*
 * Client : tee_test_openmp_example.c
 * TA     : test_TA_openmp_example  (c28775f6-c710-11e8-bcb2-13d6a9fa35e7)
 */

#include <stdio.h>
#include <tee_client_api.h>

TEEC_UUID uuid_openmp = {
    .timeLow = 0xc28775f6,
    .timeMid = 0xc710,
    .timeHiAndVersion = 0x11e8,
    .clockSeqAndNode = {0xbc, 0xb2, 0x13, 0xd6, 0xa9, 0xfa, 0x35, 0xe7},
};

enum { PRIMES_OPENMP };

static
TEEC_Result execute_TA(TEEC_Context * context,
                       int cmd_id,
                       const TEEC_UUID * uuid)
{
    TEEC_Result res;
    TEEC_Session session;
    uint32_t returnOrigin;

    res = TEEC_OpenSession(context, &session, uuid, 0, NULL, NULL,
                           &returnOrigin);
    if (res != TEEC_SUCCESS) {
        printf("TEEC_OpenSession returned %s from %s\n",
               get_error_name(res), get_return_origin(returnOrigin));
        goto out;
    }

    res = TEEC_InvokeCommand(&session, cmd_id, NULL, &returnOrigin);
    if (res != TEEC_SUCCESS) {
        printf("TEEC_InvokeCommand returned %s from %s\n",
               get_error_name(res), get_return_origin(returnOrigin));
    }

    TEEC_CloseSession(&session);

 out:
    return res;
}

int main(int argc, char **argv)
{
    (void)argc;
    (void)argv;

    TEEC_Result res;
    TEEC_Context context;

    res = TEEC_InitializeContext(NULL, &context);
    if (res != TEEC_SUCCESS) {
        printf("InitializeContext failed: %s\n", get_error_name(res));
        goto out;
    }
    TEECS_SetCryptoClk(&context, ENABLE_CRYPTO_CLK);

    res = execute_TA(&context, PRIMES_OPENMP, &uuid_openmp);
    if (res != TEEC_SUCCESS) {
        printf("execute_TA failed: %d\n", res);
    }

    TEECS_SetCryptoClk(&context, DISABLE_CRYPTO_CLK);
    TEEC_FinalizeContext(&context);

 out:
    if (res != TEEC_SUCCESS) {
        printf("FAILURE\n");
        return 1;
    }

    printf("SUCCESS\n");
    return 0;
}
