/*
 * app_main.h
 */

#ifndef _TZ_ICCC_APP_MAIN_H_
#define _TZ_ICCC_APP_MAIN_H_

#include <qsee_log.h>

#include "app_attestation.h"
#include "app_getDeviceStatus.h"

#include "tz_iccc_comdef.h"

#define SECURE_PARAMETERS_LENGTH 0x40 // TO-DO / Max length of secure buffer

// .../tz_common/public/tz_msg.h
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 tz_iccc_req_s {
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
} __attribute__ ((packed)) tz_iccc_req_t;

typedef struct tz_iccc_rsp_s {
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
    int ret;
    char secure_parameters[SECURE_PARAMETERS_LENGTH]; // TO-DO
} __attribute__ ((packed)) tz_iccc_rsp_t;

typedef struct {
    union content_u {
        tz_iccc_req_t iccc_req;
        tz_iccc_rsp_t iccc_rsp;
    } __attribute__ ((packed)) content;
} __attribute__ ((packed)) tz_iccc_generic_payload_t;

// bootloader
typedef struct iccc_bootloader_req_s {
    uint32_t cmd_id;
    union {
        iccc_bl_status_t bl_secure_info;
        iccc_sys_status_t bl_secure_info_sys;
        rot_bl_status_t bl_secure_info_rot;
        iccc_kern_status_t bl_secure_info_kern;
        iccc_max_reserve_t reserved;
    } __attribute__ ((packed)) data;
} __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)) tz_iccc_bootloader_payload_t;

typedef struct {
    tz_msg_header_t header;
    union payload_u {
        tz_iccc_generic_payload_t generic;
        tz_iccc_status_payload_t status;
        tz_iccc_attestation_payload_t attestation;
        tz_iccc_bootloader_payload_t bootloader;
    } __attribute__ ((packed)) payload;
} __attribute__ ((packed)) iccc_message_t;

typedef iccc_message_t tciMessage_t;

// .../tz_common/public/tci.h
/* 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)

#endif // _TZ_ICCC_APP_MAIN_H_
