/*
 * Copyright (c) 2019-2020, 2022-2023 Qualcomm Technologies, Inc.
 * All Rights Reserved.
 * Confidential and Proprietary - Qualcomm Technologies, Inc.
 */

#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "gp_client_imp.h"
#include "qtee_init.h"
#include "run_gp_basic.h"
#include "run_gp_time.h"
#include "stringl.h"
#include "tee_client_api.h"

char const ta_install_path[] = TA_PATH_DEFINE;

/**
 * Example App UUID this must match to the UUID that TA defined in metadata.
 * You can find the corresponding TA metadata definition in 'GPTA/src/SConscript'.
 */
const TEEC_UUID example_gpapp_ta_uuid = {0x11111111,
                                         0x1111,
                                         0x1111,
                                         {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}};

int main(void)
{
  int error = 1;
  TEEC_Result result = TEEC_SUCCESS;
  bool test_failed = false;

  /*
   * Initialize QTEEEmu.
   *
   * A null pointer can be passed to use the default settings. Alternatively, a path to a JSON file
   * specifying desired settings can be provided. See 'qtee_init.h'.
   */
  error = qtee_emu_init(NULL);
  if (error) {
    printf("qtee_emu_init() failed\n");
    // If this call fails no point to continue further so return the error
    return error;
  }

  /* Create Buffer Loader/RequestTABuffer/gpAppClient/waitCBO Object*/
  qtee_gp_init();

  result = run_invoke_multiplyHLOSBuffer();
  if (result != TEEC_SUCCESS) {
    printf("Error running multiply HLOS buffer example, ret = 0x%X.\n", result);
    test_failed = true;
  }

  result = run_tee_wait();
  if (result != TEEC_SUCCESS) {
    printf("Error running tee wait example, ret = 0x%X.\n", result);
    test_failed = true;
  }

  result = run_tee_wait_cancel();
  if (result == TEEC_ERROR_CANCEL) {
    printf("The TEE_Wait request has been cancelled, ret = 0x%X\n", result);
  } else if (result == TEEC_SUCCESS) {  // Regarded as fail as TEEC_ERROR_CANCEL is expected
    printf("Error timed out before it is cancelled, ret = 0x%X\n", result);
    test_failed = true;
  } else {
    printf("Error running tee wait cancel example failed, ret = 0x%X\n", result);
    test_failed = true;
  }

  if (test_failed) {
    error = 1;
  }

  /* Release Buffer Loader/RequestTABuffer/gpAppClient/waitCBO Object and unload files */
  qtee_gp_deinit();
  qtee_emu_deinit();
  return error;
}
