#ifndef _ICCCGRD_APP_MAIN_H_
#define _ICCCGRD_APP_MAIN_H_

#include <tee_internal_api.h>

#include "icccOperations_grdm.h"

#define DEBUG_ICCC 0 // For debug

#define TAG "[ICCC_v1.0]"

typedef enum {
    NORMAL,
    ABNORMAL,
    RESTRICTED,
    FAILED,
    UNINITIALIZED,
    UNDEFINED,
    UNKNOWN_STATUS                    = 0xFFFFFFFF
} tz_grdm_state_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 {
    union content_u {
        iccc_req_t iccc_req;
        iccc_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) iccc_generic_payload_t;

typedef struct iccc_attestation_req_s {
    tz_iccc_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_iccc_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;
    } __attribute__ ((packed)) payload;
} __attribute__ ((packed)) iccc_message_t;

typedef iccc_message_t tciMessage_t;

#endif // _ICCCGRD_APP_MAIN_H_
