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

/*! \brief Contains public interface to various functions related
 *         to the user-defined UDS application
 */

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

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

#ifdef __cplusplus
extern "C" {
#endif

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

#define EMISSION_SYSTEM_GROUP                     (0x00FFFF33)
#define SAFETY_SYSTEM_GROUP                       (0x00FFFFD0)
#define ALL_FUNC_SYSTEM_GROUP                     (0x00FFFFFF)

#define REPORT_DTC_NUMBER_BY_STATUS_MASK          (0x01)
#define REPORT_DTC_BY_STATUS_MASK                 (0x02)
#define REPORT_DTC_SNOPSHOT_IDENTIFICATION        (0x03)
#define REPORT_DTC_SNOPSHOT_RECORD_BY_DTC_NUMBER  (0x04)
#define REPORT_DTC_STORE_DATA_BY_RECORD_NUMBER    (0x05)
#define REPORT_DTC_EXT_DATA_RECORD_BY_DTC_NUMBER  (0x06)
#define REPORT_DTC_NUMBER_BY_SEVERITY_MASK_RECORD (0x07)
#define REPORT_DTC_BY_SEVERITY_MASK_RECORD        (0x08)
#define REPORT_SUPPORTED_DTC                      (0x0a)

#define DTC_FORMAT_15031                          (0x00)
#define DTC_FORMAT_14229                          (0x01)
#define DTC_FORMAT_J1939                          (0x02)
#define DTC_FORMAT_11992                          (0x03)

#define DTC_AVAILABILITY_STATUS_MASK              (0x7F)

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

/*! \brief The DTC Index name of UDS supported
 */
typedef enum _Uds_DtcIndexNameType_
{
    DTC_FUEL_SENSOR_BELOW = 0,
    DTC_FUEL_SENSOR_ABOVE,
    DTC_BATT_VOLTAG_BELOW,
    DTC_BATT_VOLTAG_ABOVE,
    DTC_CAN_BUS_OFF,
    DTC_COM_LOST_EMS,
    DTC_COM_LOST_TCU,
    DTC_COM_LOST_ABS,
    DTC_COM_LOST_TPMS,
    DTC_COM_LOST_EPB,
    DTC_COM_LOST_ESP,
    DTC_COM_LOST_EPS,
    DTC_COM_LOST_BCM,
    DTC_COM_LOST_SRS,
    DTC_COM_LOST_DVD,
    DTC_COM_LOST_PDC,
    DTC_COM_LOST_ALS,
    DTC_COM_LOST_PEPS,
    DTC_COM_LOST_AC,
    DTC_COM_LOST_AVM,
    DTC_MAX_COUNT
} Uds_DtcIndexNameType;

/*! \brief The DTC status definition refers to ISO14229-1 D.2.4
 * \note  Bit 0, testFailed, clear by test pass or ClearDiagnosticInformation
 *        Bit 1, testFailedThisOperationCycle, clear by new OC or ClearDiagnosticInformation
 *        Bit 2, latch, pendingDTC, clear by fault never happened in cur OC or ClearDiagnosticInformation, if test not completed, must hold.
 *        Bit 3, latch, confirmedDTC, clear by ClearDiagnosticInformation, one(only by normal fault) or 40 OC not detect any fault.
 *        Bit 4, latch, testNotCompletedSinceLastClear, set by a new OC start or ClearDiagnosticInformation
 *        Bit 5, latch, testFailedSinceLastClear, clear by ClearDiagnosticInformation
 *        Bit 6, testNotCompletedThisOperationCycle
 *        Bit 7, warningIndicatorRequested(unsupported)
 */
typedef union _Uds_DtcStatusType_
{
    uint8_t all;
    struct
    {
        uint8_t testFailed : 1;
        uint8_t testFailToc : 1;
        uint8_t pending : 1;
        uint8_t confirmed : 1;
        uint8_t testNcmpSlc : 1;
        uint8_t testFailSlc : 1;
        uint8_t testNcmpToc : 1;
        uint8_t wnIndreq : 1;
    } bit;
} Uds_DtcStatusType;

/*! \brief The DTC definition of UDS
 */
typedef struct _Uds_DtcType_
{
    uint32_t          dtcCode;
    Uds_DtcStatusType dtcStatus;
    int16_t           faultDtCnt; /* refers to ISO14229-1 D.6 */
    uint8_t           AgingCnt;   /* refers to ISO14229-1 D.7 */
} Uds_DtcType;

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

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

/*! \brief control DTC status updata stop and start
 */
extern void Uds_SetDtcOffCtrl(bool val);

/*! \brief get control DTC status
 */
extern bool Uds_GetDtcOffCtrlState(void);

/*! \brief clear DTC by dtc index
 */
extern void Uds_ClearDtcByDtcIndex(uint16_t dtcIndex);

/*! \brief load dtc data from EEPROM
 */
extern void Uds_LoadDtc(void);

/*! \brief save dtc data to EEPROM
 */
extern void Uds_SaveDtc(void);

/*! \brief get DTC counter by dtc status mask
 */
extern uint16_t Uds_GetDtcNumberByStatusMask(uint8_t mask);

/*! \brief get DTC data by dtc status mask
 */
extern uint16_t Uds_GetDtcByStatusMask(uint8_t *pDtcBuf, uint16_t bufLen, uint8_t mask);

/*! \brief get all supported DTC data
 */
extern uint16_t Uds_GetSupportedDtc(uint8_t *pDtcBuf, uint16_t bufLen);

/*! \brief  clear DTC by dtc group
 */
extern void Uds_ClearDtcByGroup(uint32_t group);

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

#endif /* _UDS_DTC_H_ */
