#ifndef _APP_MAIN_H_
#define _APP_MAIN_H_

#include <comdef.h>

#include <qsee_heap.h>
#include <qsee_log.h>

#define DEBUG_ICCC 0

#define TAG "[ICCC_v1.0]"

typedef enum {
    NORMAL,
    ABNORMAL,
    RESTRICTED,
    FAILED,
    UNINITIALIZED,
    UNDEFINED,
    UNKNOWN_STATUS                    = 0xFFFFFFFF
} tz_grdm_state_type;

typedef enum {
    CMD_ICCC_GRDM_ATTESTATION         = 0x00000001,
    CMD_BOOT_GRDM_SET_GRDM_STATUS     = 0x00000010, // Bootloader CMD
    CMD_ICCC_GRDM_UNKNOWN             = 0x7FFFFFFF
} tz_iccc_cmd_type;

#define MIN_PADDING_NEEDED     9
/* Enhanced Attestation */
#define ICCC_ATN_BLOB_MAX_SIZE 4096

typedef uint32_t tciCommandId_t;
typedef uint32_t tciResponseId_t;
typedef uint32_t tciReturnCode_t;

/* 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;

// bootloader
typedef struct iccc_bootloader_req_s {
    tz_iccc_cmd_type cmd_id;
    tz_grdm_state_type grdm_status;
    uint32_t dev_rot_status;
} __attribute__ ((packed)) iccc_bootloader_req_t;

typedef struct iccc_bootloader_rsp_s {
    uint32_t status;
} __attribute__ ((packed)) iccc_bootloader_rsp_t;

typedef struct {
    union bootloader_content_u {
        iccc_bootloader_req_t iccc_req;
        iccc_bootloader_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) iccc_bootloader_payload_t;

typedef struct {
    tz_msg_header_t header;
    union payload_u {
        iccc_generic_payload_t generic;
        iccc_attestation_payload_t attestation;
        iccc_bootloader_payload_t bootloader;
    } __attribute__ ((packed)) payload;
} __attribute__ ((packed)) iccc_message_t;

typedef iccc_message_t tciMessage_t;

#endif // _APP_MAIN_H_
