/*
 * app_main.h
 */

#ifndef _TESTGRD_APP_MAIN_H_
#define _TESTGRD_APP_MAIN_H_

#include <tee_internal_api.h>

#include "icccOperations_grdm.h"

#define DEBUG_TEST 1
#ifndef DEBUG_TEST
#define DEBUG_TEST 0
#endif

#if defined(TEST1)
#define TA_TAG "[TEST_v1.0]"
#elif defined(TEST2)
#define TA_TAG "[TEST2_v1.0]"
#elif defined(TEST3)
#define TA_TAG "[TEST3_v1.0]"
#else
#define TA_TAG "NA : "
#endif

typedef enum {
    CMD_ICCC_GRDM_GET_ROT_CRED_TEST   = 0x000000F1,
    CMD_ICCC_GRDM_GET_BL_CRED_TEST    = 0x000000F2,
    CMD_ICCC_GRDM_GET_ICCC_CRED_TEST  = 0x000000F3,
    CMD_ICCC_GRDM_SET_ICCC_CRED_TEST  = 0x000000F4,
    CMD_ICCC_GRDM_DEL_ICCC_CRED_TEST  = 0x000000F5,
    CMD_ICCC_GRDM_TEST_NEGATIVE       = 0x000000F6,
} tz_test_cmd_type;

#define MIN_PADDING_NEEDED     9
/* Enhanced Attestation */
#define ICCC_ATN_BLOB_MAX_SIZE 4096

/* Responses have bit 31 set */
#define RSP_ID_MASK (1U << 31)
#define RSP_ID(cmdId) (((uint32_t)(cmdId)) | RSP_ID_MASK)
#define IS_CMD(cmdId) ((((uint32_t)(cmdId)) & RSP_ID_MASK) == 0)
#define IS_RSP(cmdId) ((((uint32_t)(cmdId)) & RSP_ID_MASK) == RSP_ID_MASK)

typedef struct tz_msg_header
{
    uint32_t id; // First 4 bytes should always be id: either cmd_id or rsp_id
    uint32_t content_id;
    uint32_t len;
    uint32_t status;
} __attribute__ ((packed)) tz_msg_header_t;

typedef struct iccc_req_s
{
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
    uint32_t padding[MIN_PADDING_NEEDED]; // TO-DO / only padding, just to make tciMessage_t's size >= 64
} __attribute__ ((packed)) iccc_req_t;

typedef struct iccc_rsp_s
{
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
    int ret;
} __attribute__ ((packed)) iccc_rsp_t;

typedef struct iccc_test_rsp_s
{
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
    int ret;
    grdm_rot_status_t grdm_rot_cred;
    grdm_bl_status_t grdm_bl_cred;
    grdm_iccc_credential_1_t grdm_iccc_cred1;
    grdm_iccc_credential_2_t grdm_iccc_cred2;
} __attribute__ ((packed)) iccc_test_rsp_t;

typedef struct {
    union content_u {
        iccc_req_t iccc_req;
        iccc_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) iccc_generic_payload_t;

typedef struct {
    union test_content_u {
        iccc_req_t iccc_req;
        iccc_test_rsp_t iccc_test_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) iccc_test_payload_t;

typedef struct iccc_attestation_req_s {
    tz_test_cmd_type cmd_id;
    uint8_t blob[ICCC_ATN_BLOB_MAX_SIZE];
    uint32_t blob_len;
} __attribute__ ((packed)) iccc_attestation_req_t;

typedef struct iccc_attestation_rsp_s {
    tz_test_cmd_type cmd_id;
    int ret;
    uint8_t blob[ICCC_ATN_BLOB_MAX_SIZE];
    uint32_t blob_len;
} __attribute__ ((packed)) iccc_attestation_rsp_t;

typedef struct {
    union attestation_content_u {
        iccc_attestation_req_t iccc_req;
        iccc_attestation_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) iccc_attestation_payload_t;

typedef struct {
    tz_msg_header_t header;
    union payload_u {
        iccc_generic_payload_t generic;
        iccc_attestation_payload_t attestation;
        iccc_test_payload_t test;
    } __attribute__ ((packed)) payload;
} __attribute__ ((packed)) iccc_message_t;

typedef iccc_message_t tciMessage_t;

#endif // _TESTGRD_APP_MAIN_H_
