#pragma once

#include "TouchApiDefs.h"
//#include "si.h"

// TODO Remove these once HLOS is updated
#define MAX_IMAGE_SIZE                  400*1024    //400KB
#define MAX_ENCAPSULATED_REQ        	256
#define MAX_ENCAPSULATED_INDICATOR  	MAX_IMAGE_SIZE + 200	//Extra 200 bytes for the header

#define SECUREUILIB_OFFSET            0x01000000
#define SECUREUILIB_CMD_STARTTOUCH            ( 1 | SECUREUILIB_OFFSET)
#define SECUREUILIB_CMD_STOPTOUCH             ( 3 | SECUREUILIB_OFFSET)
#define SECUREUILIB_CMD_PREPARESECUREREQ      ( 4 | SECUREUILIB_OFFSET)
#define SECUREUILIB_CMD_STORESECUREINDICATOR  ( 5 | SECUREUILIB_OFFSET)

/** Command indication from the QSEE application to the Android library layer. */
typedef enum {
  SEC_TOUCH_CMD_CONTINUE      = 0,
    /**< Request to proceed and wait for the next event. */
  SEC_TOUCH_CMD_CANCELLED     = 1,
    /**< Request to cancel the current secure touch session because of an error. */
  SEC_TOUCH_CMD_COMPLETED     = 2,
    /**< Request to terminate the current secure touch session since all data
         requested has been acquired. */
  SEC_TOUCH_CMD_MAX           = SEC_TOUCH_CMD_COMPLETED,
  SEC_TOUCH_CMD_INVALID       = 0x7FFFFFFF
} sec_touch_cmd_t;

/** Error indication from the Android library layer to the QSEE application.
    These indications do not necessarily indicate errors. */
typedef enum {
  SEC_TOUCH_ERR_STARTED       = 0,  /**< Secure touch was started. */
  SEC_TOUCH_ERR_STOPPED       = 1,  /**< Secure touch was stopped. */
  SEC_TOUCH_ERR_DATA          = 2,  /**< Touch interrupt fired; data is available. */
  SEC_TOUCH_ERR_TIMEOUT       = 3,  /**< Waiting for a touch event timed out. */
  SEC_TOUCH_ERR_POWER         = 4,  /**< Abort request to service a power event. */
  SEC_TOUCH_ERR_ABORT         = 5,  /**< Abort request. */
  SEC_TOUCH_ERR_MAX           = SEC_TOUCH_ERR_ABORT,
  SEC_TOUCH_ERR_INVALID       = 0x7FFFFFFF /**< @newpage */
} sec_touch_err_t;

typedef uint32_t tciCommandId_t;
typedef uint32_t tciResponseId_t;
typedef uint32_t tciReturnCode_t;
/**
 * TCI command header.
 */
typedef struct{
    tciCommandId_t commandId; /**< Command ID */
    tciReturnCode_t reserved; /**< Reserved */
} tciCommandHeader_t;

/**
 * TCI response header.
 */
typedef struct{
    tciResponseId_t     responseId; /**< Response ID (must be command ID | RSP_ID_MASK )*/
    tciReturnCode_t     returnCode; /**< Return code of command */
} tciResponseHeader_t;

/******************************************************************************/
/** (1) StartTouch */
/******************************************************************************/
/** StartTouch command */
typedef struct {
  uint32_t reserved;
} cmdStartTouch_t;
/** StartTouch response */
typedef struct {
  int32_t timeout; /**< in milliseconds, 0 for no timeout, negative for infinite */
  sec_touch_cmd_t cmd;
} rspStartTouch_t;

/******************************************************************************/
/** (2) ProcessTouch */
/******************************************************************************/
/** ProcessTouch command */
typedef struct {
  sec_touch_err_t err;
} cmdProcessTouch_t;
/** ProcessTouch response */
typedef struct {
  int32_t timeout; /**< in milliseconds, 0 for no timeout, negative for infinite */
  sec_touch_cmd_t cmd;
} rspProcessTouch_t;

/******************************************************************************/
/** (3) StopTouch */
/******************************************************************************/
/** StopTouch command */
typedef struct {
  sec_touch_err_t err;
} cmdStopTouch_t;
/** StopTouch response */
typedef struct {
  sec_touch_cmd_t cmd;
} rspStopTouch_t;

/******************************************************************************/
/** (4) PrepareRequest */
/******************************************************************************/
/** PrepareRequest response */
typedef struct {
  uint8_t encapsulatedReq[MAX_ENCAPSULATED_REQ];
  uint32_t encapsulatedReqLen;
} rspPrepareRequest_t;

/******************************************************************************/
/** (5) StoreIndicator */
/******************************************************************************/
/** StoreIndicator command */
typedef struct {
  uint8_t encapsulatedIndicator[MAX_ENCAPSULATED_INDICATOR];
  uint32_t encapsulatedIndicatorLen;
} cmdStoreIndicator_t;
typedef union {
  cmdStartTouch_t              startTouch;
  cmdProcessTouch_t            processTouch;
  cmdStopTouch_t               stopTouch;
  cmdStoreIndicator_t          storeIndicator;
} SecureUILibCommandPayload_t;

typedef union {
  rspStartTouch_t              startTouch;
  rspProcessTouch_t            processTouch;
  rspStopTouch_t               stopTouch;
  rspPrepareRequest_t          prepareRequest;
} SecureUILibResponsePayload_t;

typedef struct __attribute__ ((aligned (0x40))) {
  tciCommandHeader_t           header;
  SecureUILibCommandPayload_t  payload;
} tciLibCommand_t;

typedef struct __attribute__ ((aligned (0x40))) {
  tciResponseHeader_t           header;
  SecureUILibResponsePayload_t  payload;
} tciLibResponse_t;
