
/*
 * =====================================================================================
 *
 *       Filename:  tui_defs.h
 *
 *    Description:  TUI internal definitions
 *
 *        Version:  1.0
 *        Created:  04/02/2020 11:17 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *        Company:  Samsung Electronics
 *        Copyright (c) 2020 by Samsung Electronics, All rights reserved.
 *
 * =====================================================================================
 */

#ifndef _TUI_DEFS_H_
#define _TUI_DEFS_H_

/**
 * Custom QCOM includes
 */
#ifdef CONFIG_QSEE
#include <qsee_log.h>
#endif

/**
 * External includes
 */
#include <stdio.h>
#include "string.h"
#include "tee_internal_api.h"

/**
 * TUI Logging
 */
#define	LOG_ENTRY_SIZE	512
#define	LOG_MSG_SIZE	(LOG_ENTRY_SIZE - sizeof(uint32_t) - sizeof(uint32_t))
#define TAG "TA_TUI : "


/**
 * TUI Fixed Values
 */

/**
 * TUI Commands
 */
/* Command ID list */
#define CMD_MPOSTUI_CHECK_DEVICE_INTEGRITY         0x00000001
#define CMD_MPOSTUI_SEND_CERTIFICATE               0x00000002
#define CMD_MPOSTUI_SEND_PINPAD_IMAGES             0x00000003
#define CMD_MPOSTUI_SEND_PINBOX_IMAGES             0x00000004
#define CMD_MPOSTUI_START_TUI_SESSION              0x00000005
#define CMD_MPOSTUI_CLOSE_TUI_SESSION              0x00000006

/**
 * Possible return codes
 */
typedef enum tui_return_code {
	TUI_STATUS_SUCCESS               = 0x0,
	TUI_STATUS_FAIL                  = 0x1,
	TUI_DEVICE_STATUS_CHECK_FAILED,
	TUI_SEND_CERTIFICATE_FAILED,
	TUI_VERIFY_CERTIFICATE_FAILED,
	TUI_SEND_PINPAD_IMAGES_FAILED,
	TUI_SEND_PINBOX_IMAGES_FAILED,
	TUI_START_TUI_SESSION_FAILED,
	TUI_CLOSE_TUI_SESSION_FAILED,
} tui_return_code_t;

/*
 * Return code
 */
#define TIMA_SUCCESS				0
#define	TIMA_ERROR_OUT_OF_MEMORY		1
#define	TIMA_ERROR_UNKNOWN_ERROR		2
#define TIMA_TRANSFER_NOT_FINISH		3
#define TIMA_TRANSFER_FINISH_ALL		4
#define TIMA_ERROR_TUI				0x5000
#define TIMA_ERROR_TUI_CANCELLED		TIMA_ERROR_TUI
#define	TIMA_ERROR_TUI_NO_SESSION		TIMA_ERROR_TUI + 1 /** The session to TUI driver cannot be found. It was not opened or has been closed*/
#define	TIMA_ERROR_TUI_BUSY			TIMA_ERROR_TUI + 2 /** TUI driver is busy. Another session may be open. */
#define	TIMA_ERROR_TUI_NO_EVENT			TIMA_ERROR_TUI + 3 /** TUI event queue is empty. */
#define	TIMA_ERROR_TUI_OUT_OF_DISPLAY		TIMA_ERROR_TUI + 4 /** TUI driver can't display the image because it is out of screen. */
#define	TIMA_ERROR_TUI_IMG_BAD_FORMAT		TIMA_ERROR_TUI + 5 /** TUI driver can't decode the image. */
#define	TIMA_ERROR_TUI_NO_RESOURCE		TIMA_ERROR_TUI + 6
#define	TIMA_ERROR_TUI_IN_PROGRESS		TIMA_ERROR_TUI + 7
#define	TIMA_ERROR_TUI_NOT_SUPPORTED		TIMA_ERROR_TUI + 8
#define	TIMA_ERROR_TUI_UNKNOWN_CMD		TIMA_ERROR_TUI + 9
#define	TIMA_ERROR_TUI_NOT_READY		TIMA_ERROR_TUI + 10
#define TIMA_ERROR_TUI_SECRET_READ_ERROR	TIMA_ERROR_TUI + 11
#define TIMA_ERROR_TUI_SECRET_WRITE_ERROR	TIMA_ERROR_TUI + 12
#define TIMA_ERROR_TUI_SECRET_NOT_EXIST		TIMA_ERROR_TUI + 13
#define TIMA_ERROR_TUI_DISPLAY_ERROR		TIMA_ERROR_TUI + 14
#define TIMA_ERROR_TUI_IMAGE_TOO_BIG		TIMA_ERROR_TUI + 15
#define TIMA_ERROR_TUI_CERT_BAD_FORMAT		TIMA_ERROR_TUI + 16
#define TIMA_ERROR_TUI_CERT_KEY_ERROR		TIMA_ERROR_TUI + 17
#define TIMA_ERROR_TUI_PRIVATE_KEY_ERROR	TIMA_ERROR_TUI + 18
#define TIMA_ERROR_TUI_SECRET_DECRYPT_ERROR	TIMA_ERROR_TUI + 19
#define TIMA_ERROR_TUI_SECRET_SIGNATURE_ERROR	TIMA_ERROR_TUI + 20
#define TIMA_ERROR_TUI_RES_INVALID_ID		TIMA_ERROR_TUI + 21
#define TIMA_ERROR_TUI_MALLOC_ERROR		TIMA_ERROR_TUI + 22
#define TIMA_ERROR_TUI_WRAP_ERROR		TIMA_ERROR_TUI + 23
#define TIMA_ERROR_TUI_PIN_VERIFY_FAIL		TIMA_ERROR_TUI + 24
#define TIMA_ERROR_INVALID_ARGUMENT		TIMA_ERROR_TUI + 25

/*
 * Return code specific to SPAY TUI
 */
#define MPOS_TPP_ERROR_NO_PIN			0x60000
#define MPOS_TPP_ERROR_INVALID_PIN_SO		0x60001
#define MPOS_TPP_ERROR_PIN_EXIST		0x60002
#define MPOS_TPP_ERROR_NO_FLOW_TO_RESUME	0x60003
#define MPOS_TPP_SETUP_PIN_ENTERED		0x60004
#define MPOS_TPP_SETUP_PIN_VERIFIED		0x60005
#define MPOS_TPP_ERROR_SETUP_PIN_MISMATCH	0x60006
#define MPOS_TPP_PIN_VERIFYED			0x60007
#define MPOS_TPP_ERROR_PIN_MISMATCH		0x60008
#define MPOS_TPP_ERROR_CANCELED			0x60009
#define MPOS_TPP_ERROR_3_SAME_DIGITS		0x6000A	// PIN contains same digits,such as 1113
#define MPOS_TPP_ERROR_CONSECUTIVE_DIGITS	0x6000B	// such as 1234 or 4321
//#define MPOS_TPP_ERROR_TUI_FP_REJECT		0x6000C	// Do not accept FP when PIN exists but FP ID list is empty
#define MPOS_TPP_ERROR_TUI_VERIFIED_TIMEOUT	0x6000D	// TUI verification result is not valid anymore
//#define MPOS_TPP_ERROR_TUI_IRIS_REJECT		0x6000E	// Do not accept FP when PIN exists but FP ID list is empty

#define MPOS_TPP_ERROR_INVALID_NUMBER_OF_PIN	0x61000
#define MPOS_TPP_ERROR_INVALID_STATE		0x61001
#define MPOS_TPP_ERROR_GENERIC			0x62000

#define MPOS_TPP_TUI_SESSION_ON			0x63000
#define MPOS_TPP_TUI_SESSION_OFF		0x63001

#define PIN_SIZE		16	/* Maximum PIN size */

/**
 * Logging
 */
char tui_log_msg[LOG_MSG_SIZE];

#ifdef CONFIG_QSEE
#define TUI_LOG(...)    do {                                     \
	snprintf(tui_log_msg, LOG_MSG_SIZE - 1, TAG __VA_ARGS__); \
	tui_log_msg[LOG_MSG_SIZE - 1] = '\0';                      \
	qsee_log(QSEE_LOG_MSG_ERROR, tui_log_msg);                  \
} while(0);
#else
#define TUI_LOG(...)    do {                                     \
	snprintf(tui_log_msg, LOG_MSG_SIZE - 1, TAG __VA_ARGS__); \
	tui_log_msg[LOG_MSG_SIZE - 1] = '\0';                      \
	printf(tui_log_msg);                                        \
	printf("\n");                                                \
} while(0);
#endif

/**
 * Remove below comment for debugging logs
 */
#define DEBUG_TUI

/**
 * Debugging
 */
#ifdef DEBUG_TUI
#define TUI_LOG_DEBUG(...) TUI_LOG(__VA_ARGS__)
#else
#define TUI_LOG_DEBUG(...)
#endif

#endif /* _TUI_DEFS_H_ */
