/*
 * app_main.h
 */

#ifndef _TZ_ICCC_APP_MAIN_H_
#define _TZ_ICCC_APP_MAIN_H_

#include "app_attestation.h"
#include "app_getDeviceStatus.h"

#include "icccOperations_v4.h"

#define MIN_PADDING_NEEDED 9 // TO-DO

// .../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;
    uint32_t padding[MIN_PADDING_NEEDED]; // TO-DO / only padding, just to make tciMessage_t's size >= 64
} __attribute__ ((packed)) tz_iccc_req_t;

typedef struct tz_iccc_rsp_s {
    uint32_t cmd_id;
    uint32_t type;
    uint32_t value;
    int ret;
} __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;

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;
    } __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_
