/*
 * Copyright (C) 2010-2014 NXP Semiconductors
 *
 * 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.
 */

/**
 * \addtogroup eSe_PAL
 * \brief PAL implementation
 * @{ */

#ifndef _PHNXPESE_PAL_H
#define _PHNXPESE_PAL_H

/* Basic type definitions */
#include <phEseTypes.h>
#include <errno.h>
/*!
 * \brief Value indicates to reset device
 */
#define PH_PALESE_RESETDEVICE               (0x00008001)

/*!
 * \ingroup eSe_PAL
 *
 * \brief Enum definition contains supported ioctl control codes.
 *
 * phPalEse_IoCtl
 */
typedef enum
{
    phPalEse_e_Invalid = 0, /*!< Invalid control code */
    phPalEse_e_ResetDevice = PH_PALESE_RESETDEVICE, /*!< Reset the device */
    phPalEse_e_EnableLog, /*!< Enable the spi driver logs */
    phPalEse_e_EnablePollMode, /*!< Enable the polling for SPI */
    phPalEse_e_ExchangeSpiData, /* Transmit data to the device and retrieve data from it simultaneously.*/
    phPalEse_e_GetEseAccess, /*!< get the bus access in specified timeout */
    phPalEse_e_ChipRst,      /*!< eSE Chip reset using ISO RST pin*/
    phPalEse_e_SetPowerScheme, /*!< Set power scheme */
    phPalEse_e_GetSPMStatus    /*!< Get SPM(power mgt) status */
#if(NXP_ESE_JCOP_DWNLD_PROTECTION == TRUE)
	,phPalEse_e_SetClientUpdateState /*!< Set Jcop Download state */
#endif
} phPalEse_ControlCode_t ;  /*!< Control code for IOCTL call */

/*!
 * \ingroup eSe_PAL
 *
 * \brief PAL Configuration exposed to upper layer.
 */
typedef struct phPalEse_Config
{
    int8_t *pDevName;
    /*!< Port name connected to ESE
      *
      * Platform specific canonical device name to which ESE is connected.
      *
      * e.g. On Linux based systems this would be /dev/p73
      */

    uint32_t dwBaudRate;
    /*!< Communication speed between DH and ESE
      *
      * This is the baudrate of the bus for communication between DH and ESE
      */

    //void *pDevHandle;
    /*!< Device handle output */
} phPalEse_Config_t,*pphPalEse_Config_t;    /* pointer to phPalEse_Config_t */

/**
 * \ingroup eSe_PAL
 * \brief Reads requested number of bytes from ESE into given buffer
 *
 * \param[in]    pDevHandle       - valid device handle
**\param[in]    pBuffer           - buffer for read data
**\param[in]    nNbBytesToRead    - number of bytes requested to be read
 *
 * \retval   numRead      - number of successfully read bytes.
 * \retval      -1             - read operation failure
 *
*/
int phPalEse_read(uint8_t * pBuffer, int nNbBytesToRead);

/**
 * \ingroup eSe_PAL
 * \brief Writes requested number of bytes from given buffer into pn547 device
 *
 * \param[in]    pBuffer                    - buffer to write
 * \param[in]    nNbBytesToWrite            - number of bytes to write
 *
 * \retval  numWrote   - number of successfully written bytes
 * \retval      -1         - write operation failure
 *
 */
int phPalEse_write(uint8_t * pBuffer, int nNbBytesToWrite);

/**
 * \ingroup eSe_PAL
 * \brief Print packet data
 *
 * \param[in]    pString            - String to be printed
 * \param[in]    p_data             - data to be printed
 * \param[in]    len                - Length of data to be printed
 *
 * \retval   void
 *
 */
void phPalEse_print_packet(const char *pString, const uint8_t *p_data, uint16_t len);

/**
 * \ingroup eSe_PAL
 * \brief This function  suspends execution of the calling thread for
 *                  (at least) usec microseconds
 *
 * \param[in]    usec                - number of micro seconds to sleep
 *
 * \retval   void
 *
 */
void phPalEse_sleep(uint32_t usec);

/**
 * \ingroup eSe_PAL
 * \brief This function updates destination buffer with val
 *                 data in len size
 *
 * \param[in]    buff                - Array to be udpated
 * \param[in]    val                 - value to be updated
 * \param[in]    len                 - length of array to be updated
 *
 * \retval   void
 *
 */
void* phPalEse_memset(void *buff, int val, size_t len);

/**
 * \ingroup eSe_PAL
 * \brief This function copies source buffer to  destination buffer
 *                 data in len size
 *
 * \param[in]    dest                - Destination array to be updated
 * \param[in]    src                 - Source array to be updated
 * \param[in]    len                 - length of array to be updated
 *
 * \retval   void
 *
 */
void* phPalEse_memcpy(void *dest, const void *src, size_t len);

/**
 * \ingroup eSe_PAL
 * \brief This is utility function for runtime heap memory allocation
 *
 * \param[in]    size                 - number of bytes to be allocated
 *
 * \retval   void
 *
 */
void *phPalEse_memalloc(uint32_t size);

/**
 * \ingroup eSe_PAL
 * \brief This is utility function for freeeing heap memory allocated
 *
 * \param[in]    ptr                 - Address pointer to previous allocation
 *
 * \retval   void
 *
 */
void phPalEse_free(void* ptr);

/** @} */
#endif  /*  _PHNXPESE_PAL_H    */
