/*
 * Copyright (C) 2019 SAMSUNG S.LSI
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __ESE_API__H
#define __ESE_API__H

/**
 * @defgroup common Common
 * @brief Common header files.
 */

/**
 * @file ese_api.h
 * @brief Basic eSE APIs.
 */

/**
 * @ingroup common
 * @defgroup ese_api eSE_API
 * @brief Basic eSE APIs.
 *
 * This API provides functions to use eSE.
 * @{
 */

#ifdef ESE_U_BOOT_BUILD
#include <linux/types.h>
#else
//#include <unistd.h>
#include <stdint.h>
#endif

/**
 * @brief eSE Status Codes
 *
 * Return values of eSE APIs.
 */
enum ESESTATUS {
	ESESTATUS_SUCCESS = (0x0000), /**< Success */
	ESESTATUS_FAILED = (0x0001), /**< Fail */
	ESESTATUS_IOCTL_FAILED = -1, /**< IOCTL fail */
	ESESTATUS_INVALID_BUFFER = (0x0002), /**< Invalid buffer */
	ESESTATUS_BUFFER_TOO_SMALL = (0x0003), /**< Buffer size is too small */
	ESESTATUS_INVALID_CLA = (0x0004), /**< Invalid CLA */
	ESESTATUS_INVALID_CPDU_TYPE = (0x0005), /**< Invalid CPDU type */
	ESESTATUS_INVALID_LE_TYPE = (0x0007), /**< Invalid Le type */
	ESESTATUS_INVALID_DEVICE = (0x0006), /**< Invalid device */
	ESESTATUS_MORE_FRAME = (0x0008), /**< More frame */
	ESESTATUS_LAST_FRAME = (0x0009), /**< Last frame */
	ESESTATUS_CRC_ERROR = (0x000A), /**< CRC error */
	ESESTATUS_SOF_ERROR = (0x000B), /**< SOF error */
	ESESTATUS_INSUFFICIENT_RESOURCES = (0x000C), /**< Insufficient resources */
	ESESTATUS_PENDING = (0x000D), /**< Pending */
	ESESTATUS_BOARD_COMMUNICATION_ERROR = (0x000F), /**< Board communication
									 error */
	ESESTATUS_INVALID_STATE = (0x0011), /**< Invalid state */
	ESESTATUS_NOT_INITIALISED = (0x0031), /**< Not initialised */
	ESESTATUS_ALREADY_INITIALISED = (0x0032), /**< Already initialised */
	ESESTATUS_FEATURE_NOT_SUPPORTED = (0x0033), /**< Feature not supported */
	ESESTATUS_PARITY_ERROR = (0x0034), /**< Parity error */
	ESESTATUS_ALREADY_REGISTERED = (0x0035), /**< Already registered */
	ESESTATUS_CHAINED_FRAME = (0x0036), /**< Chained frame */
	ESESTATUS_SINGLE_FRAME = (0x0037), /**< Single frame */
	ESESTATUS_DESELECTED = (0x0038), /**< Deselected */
	ESESTATUS_RELEASED = (0x0039), /**< Released */
	ESESTATUS_NOT_ALLOWED = (0x003A), /**< Not allowed */
	ESESTATUS_OTHER_ERROR = (0x003C), /**< Other error */
	ESESTATUS_DWNLD_BUSY = (0x006E), /**< Download is busy status */
	ESESTATUS_BUSY = (0x006F), /**< Busy status */
	ESESTATUS_INVALID_REMOTE_DEVICE = (0x001D), /**< Invalid remote device */
	ESESTATUS_READ_FAILED = (0x0014), /**< Read fail */
	ESESTATUS_WRITE_FAILED = (0x0015), /**< Write fail */
	ESESTATUS_NO_NDEF_SUPPORT = (0x0016), /**< NDEF is not supported */
	ESESTATUS_RESET_SEQ_COUNTER_FRAME_RESEND = (0x001A), /**< Reset sequence
									 counter frame resend */
	ESESTATUS_INVALID_RECEIVE_LENGTH = (0x001B), /**< Invalid receive length */
	ESESTATUS_INVALID_FORMAT = (0x001C), /**< Invalid format */
	ESESTATUS_INSUFFICIENT_STORAGE = (0x001F), /**< Insufficient storage */
	ESESTATUS_FRAME_RESEND = (0x0023), /**< Frame resend */
	ESESTATUS_WRITE_TIMEOUT = (0x0024), /**< Write timeout */
	ESESTATUS_RESPONSE_TIMEOUT = (0x0025), /**< Response timeout */
	ESESTATUS_FRAME_RESEND_R_FRAME = (0x0026), /**< Frame resend R-Frame */
	ESESTATUS_SEND_NEXT_FRAME = (0x0027), /**< Send next frame */
	ESESTATUS_RECOVERY_STARTED = (0x0028), /**< Recovery started */
	ESESTATUS_SEND_R_FRAME = (0x0029), /**< Send R-Frame */
	ESESTATUS_FRAME_RESEND_RNAK = (0x0030), /**< Frame resend R-Nak */
	ESESTATUS_FRAME_SEND_R_FRAME = (0x003B), /**< Frame send R-Frame */
	ESESTATUS_UNKNOWN_ERROR = (0x00FE), /**< Unknown error */
	ESESTATUS_INVALID_PARAMETER = (0x00FF), /**< Invalid parameter */
	ESESTATUS_CMD_ABORTED = (0x0002), /**< Command aborted */
	ESESTATUS_NO_TARGET_FOUND = (0x000A), /**< No target found */
	ESESTATUS_NO_DEVICE_CONNECTED = (0x000B), /**< No device connected */
	ESESTATUS_RESYNCH_REQ = (0x000E), /**< Resynchronization request */
	ESESTATUS_RESYNCH_RES = (0x0010), /**< Resynchronization response */
	ESESTATUS_IFS_REQ = (0x001E), /**< IFS request */
	ESESTATUS_IFS_RES = (0x0017), /**< IFS response */
	ESESTATUS_ABORT_REQ = (0x00F0), /**< Abort request */
	ESESTATUS_ABORT_RES = (0x00F2), /**< Abort response */
	ESESTATUS_WTX_REQ = (0x00F5), /**< WTX request */
	ESESTATUS_WTX_RES = (0x00F6), /**< WTX response */
	ESESTATUS_RESET_REQ = (0x00F7), /**< Reset request */
	ESESTATUS_RESET_RES = (0x00F8), /**< Reset response */
	ESESTATUS_END_APDU_REQ = (0x00F9), /**< End APDU request */
	ESESTATUS_END_APDU_RES = (0x00FA), /**< End APDU response */
	ESESTATUS_SHUTDOWN = (0x0091), /**< Shutdown */
	ESESTATUS_TARGET_LOST = (0x0092), /**< Target lost */
	ESESTATUS_REJECTED = (0x0093), /**< Rejected */
	ESESTATUS_TARGET_NOT_CONNECTED = (0x0094), /**< Target not connected */
	ESESTATUS_INVALID_HANDLE = (0x0095), /**< Invalid handle */
	ESESTATUS_ABORTED = (0x0096), /**< Aborted */
	ESESTATUS_COMMAND_NOT_SUPPORTED = (0x0097), /**< Command not supported */
	ESESTATUS_NON_NDEF_COMPLIANT = (0x0098), /**< Non NDEF compliant */
	ESESTATUS_NOT_ENOUGH_MEMORY = (0x001F), /**< Not enough memory */
	ESESTATUS_INCOMING_CONNECTION = (0x0045), /**< Incoming connection */
	ESESTATUS_CONNECTION_SUCCESS = (0x0046), /**< Connection success */
	ESESTATUS_CONNECTION_FAILED = (0x0047), /**< Connection fail */
	ESESTATUS_ALREADY_OPENED = (0x0048), /**< Open fail */	
	ESESTATUS_ALREADY_CLOSED = (0x0049), /**< Close fail */
};

/**
 * @brief Type definition of ESE_STATUS
 */
typedef uint32_t ESE_STATUS;

/**
 * @brief Open eSE
 *
 * - Initialize eSE for using the API and determine supported API functionality.
 * - Connect to the secure element and initialize parameters.
 *
 * @return Returns ::ESESTATUS_SUCCESS if succeeded and other value if failed.
 * @see ese_close
 * @see ese_reset
 */
ESE_STATUS ese_open(void);
/**
 * @brief Close eSE
 *
 * - Finalize eSE.
 *
 * @return Returns ::ESESTATUS_SUCCESS if succeeded and other value if failed.
 * @see ese_open
 * @see ese_reset
 */
ESE_STATUS ese_close(void);
/**
 * @brief Reset eSE
 *
 * - Reset parameters for eSE.
 *
 * @return Returns ::ESESTATUS_SUCCESS if succeeded and other value if failed.
 * @see ese_open
 * @see ese_close
 */
ESE_STATUS ese_reset(void);
/**
 * @brief Transceive data with eSE
 *
 * - Transmit command to eSE and receive data from eSE.
 *
 * @param[in]  cmd     : Command to transmit to eSE
 * @param[in]  cmd_len : Length of \p cmd
 * @param[out] rsp     : Data received from eSE
 * @param[out] rsp_len : Length of \p rsp
 * @return Returns ::ESESTATUS_SUCCESS if succeeded and other value if failed.
 */
ESE_STATUS ese_transceive(uint8_t *cmd, uint32_t cmd_len, uint8_t **rsp, uint32_t *rsp_len);
/**
 * @}
 */
#endif
