/*
 * 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_TYPE_H_
#define _LIN_TYPE_H_

/*! \brief his file exports Lin interface Type defnitions
 *
 */

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

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

/*! \addtogroup lin_type
 *  @{
 */

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/
#define LIN_DATA_INVALID              0xFF

/* Master Request Frame */
#define LIN_STACK_PID_MRF             0x3C
/* Slave Response Frame */
#define LIN_STACK_PID_SRF             0x7D

/* DATA[8] + Checksum */
#define LIN_FRAME_RESP_DATA_BYTES_MAX 8u
#define LIN_FRAME_RESP_BYTES_MAX      (LIN_FRAME_RESP_DATA_BYTES_MAX + 1u)

/*******************************************************************************
 * the typedefs
 ******************************************************************************/
typedef enum
{
    LIN_NODE_IS_MASTER = 0u,
    LIN_NODE_IS_SLAVE,

    LIN_NODE_IS_NUM
} Lin_NodeType;

typedef enum
{
    LIN_FRAME_TYPE_UNCONDITIONAL = 0u,
    LIN_FRAME_TYPE_EVENT_TRIGGER,
    LIN_FRAME_TYPE_SPORADIC,
    LIN_FRAME_TYPE_DIAG,
    LIN_FRAME_TYPE_USER_DEFINED,
    LIN_FRAME_TYPE_RESERVED,

    LIN_FRAME_TYPE_NUM
} Lin_FrametypeType;

typedef enum
{
    LIN_FRAME_CTRL_IDLE = 0u,
    LIN_FRAME_CTRL_NO_ACTION,
    LIN_FRAME_CTRL_PUBLISH,
    LIN_FRAME_CTRL_SUBSCRIBER,

    LIN_FRAME_CTRL_NUM
} Lin_FrameCtrlType;

typedef enum
{
    LIN_CHECKSUM_CLASSIC = 0u,
    LIN_CHECKSUM_ENHANCED,

    LIN_CHECKSUM_NUM
} Lin_CheckSumType;

typedef enum
{
    LIN_STATUS_IDLE = 0u,
    LIN_STATUS_READY,
    LIN_STATUS_HEADER_BUSY,
    LIN_STATUS_RX_DATA_BUSY,
    LIN_STATUS_TX_DATA_BUSY,
    LIN_STATUS_TX_OK,
    LIN_STATUS_RX_OK,

    /* The next are all error information */
    LIN_STATUS_ERR_BIT,
    LIN_STATUS_ERR_BREAK,
    LIN_STATUS_ERR_SYNC,
    LIN_STATUS_ERR_PID,
    LIN_STATUS_ERR_DATA,
    LIN_STATUS_ERR_CHKSUM,
    LIN_STATUS_ERR_FRAME,
    LIN_STATUS_ERR_PHY_BUS,
    LIN_STATUS_ERR_TX_CHECK_ERR,
    LIN_STATUS_ERR_RX_ERR,
    LIN_STATUS_ERR_TIMEOUT,

} Lin_OpStatusType;

typedef struct
{
    union
    {
        uint8_t byteData[LIN_FRAME_RESP_DATA_BYTES_MAX];

        struct
        {
            uint8_t data[LIN_FRAME_RESP_DATA_BYTES_MAX];
        } unContionalBytes;

        struct
        {
            uint8_t data[LIN_FRAME_RESP_DATA_BYTES_MAX];
        } sporadicBytes;

        struct
        {
            uint8_t pid;
            uint8_t data[LIN_FRAME_RESP_DATA_BYTES_MAX - 1u];
        } eventBytes;

        struct
        {
            uint8_t nad;
            uint8_t data[LIN_FRAME_RESP_DATA_BYTES_MAX - 1u];
        } diagBytes;
    } poyload;

} Lin_FrameDataType;

typedef enum
{
    LIN_FRAME_ACCESS_STATE_IDLE = 0u,
    LIN_FRAME_ACCESS_STATE_READING_OUT,
    LIN_FRAME_ACCESS_STATE_WRITING_IN,
    LIN_FRAME_ACCESS_STATE_UPDATED,
    LIN_FRAME_ACCESS_STATE_OVER_RECEIVED,
} Lin_FrameAccessStateType;

typedef struct
{
    Lin_FrametypeType frameType;
    Lin_FrameCtrlType frameCtrl;
    Lin_CheckSumType  checkSumType;
    uint8_t           dataLen;
    uint8_t           pid;
    uint8_t           pidIdx;
    Lin_FrameDataType frameData;
    uint8_t           checkSum;
    uint32_t cycleCnt;
} Lin_PduType;

typedef struct
{
    Lin_FrametypeType        frameType;
    Lin_FrameCtrlType        frameCtrl;
    uint8_t                  dataLen;
    uint8_t                  pid;
    const Lin_FrameDataType *pFrameData;
} Lin_FrameInfoType;

typedef struct
{
    uint8_t                   pid;
    uint8_t                   frameLen;
    Lin_FrameDataType        *pFrameDataPtr;
    Lin_FrameAccessStateType *pFrameAccessStatePtr;
} Lin_FrameGeneralityType;

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

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

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

/*! @}*/

#endif /* _LIN_TYPE_H_ */
