/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _LIN_NM_H_
#define _LIN_NM_H_

/*! \brief his file exports Lin communication manager
 *
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include <stdint.h>
#include <stdbool.h>

/*! \addtogroup lin_com_manager
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

typedef enum
{
    LINNM_MODE_IDLE = 0u,
    LINNM_MODE_INIT,
    LINNM_MODE_RUNNING,
    LINNM_MODE_SLEEP_PREPARE,
    LINNM_MODE_SLEEP_READY,
    LINNM_MODE_SLEEP,

    LINNM_MODE_NUM
} LinNm_ModeType;

typedef struct
{
    bool transferSuccessful;
    bool responseError;

    uint32_t wakeUpSignalValidTickCnt;
    uint32_t delayCommunicateTickCnt;
    uint32_t busIdleTickCnt;
    uint32_t wakeUpTimeOutTickCnt;
    uint8_t  retryWakeUpCnt;

    bool requestSleep;
    bool sleepIndication;

    bool           activeWakeUpReq;
    bool           passiveWakeUpConfirm;
    LinNm_ModeType currentComMode;
    LinNm_ModeType targetComMode;
} LinNm_StateType;

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/
/*! \brief Initialize the communication manager
 *
 * This function execute to initialize the communication manager
 *
 * \param[in] cpuClockFrequency: cpu lock frequency
 * \return: void
 */
extern void LinNm_Init(uint32_t cpuClockFrequency);

/*! \brief Get the communication mode
 *
 * This function execute to get the communication mode
 *
 * \param[in] void:
 * \return: the communication mode
 */
extern LinNm_ModeType LinNm_GetComMode(void);

/*! \brief Request sleep
 *
 * This function execute to request sleep
 *
 * \param[in] sleepRequest: request sleep
 * \return: void
 */
extern void LinNm_RequestSleep(bool sleepRequest);

/*! \brief Set the active wake up request
 *
 * This function execute to set active wake-up request if the local condition is match to exist from sleep
 *
 * \param[in] void:
 * \return: void
 */
extern void LinNm_SetActiveRequestWakeUp(void);

/*! \brief Check the passive wakeup signal
 *
 * This function execute to get the passive wake-up signal from bus
 *
 * \param[in] uint8_t channelIdx: which physical channel is used
 * \return: true: passive wakeup signal is valid
 *          false: passive wakup signal is invalid
 */
extern bool LinNm_CheckPassiveWakeUpSignal(uint8_t channelIdx);

/*! \brief Process the communication manager
 *
 * This function execute to process the communication manager
 *
 * \param[in] void:
 * \return: void
 */
extern void LinNm_MainFunction(void);

/*! \brief Set the error response bit
 *
 * This function execute to update the response error bit
 *
 * \param[in] errBit: err
 * \return: void
 */
extern void LinNm_SetErrResponseBit(bool errBit);

/*! \brief Get the error response bit
 *
 * This function execute to get the error response bit
 *
 * \param[in] void:
 * \return: true: error is happen
 *          false: error is not happen
 */
extern bool LinNm_GetErrResponseBit(void);

/*! \brief Set the transfer successful bit
 *
 * This function execute
 *
 * \param[in] successfullBit: true or false
 * \return: void
 */
extern void LinNm_SetSuccessfulTransferBit(bool successfulBit);

/*! \brief Get the transfer successful bit
 *
 * This function execute to get the transfer successful bit
 *
 * \param[in] void:
 * \return: true: transfer successfully
 *          false: transfer failed
 */
extern bool LinNm_GetSuccessfulTransferBit(void);

#ifdef __cplusplus
}
#endif /* extern "C" */

/*! @}*/

#endif /* _LIN_COM_MANAGER_H_ */
