/*!
* @file     Det.c
*
* @version  1.0.0
*
* @brief    The source file of the Det driver
*
* @attention
*
* Project Name      : AUTOSAR MCAL
*
* Platform          : Arm
*
* Autosar Standard  : Classic Platform 4.4.0
*
* Compiled Version  : G32A10xx_MCAL_1_0_0_01-Otc-25
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the
* GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
*
* The program is only for reference, which is distributed in the hope
* that it will be usefull and instructional for customers to develop
* their software. Unless required by applicable law or agreed to in
* writing, the program is distributed on an "AS IS" BASIS, WITHOUT
* ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
* See the Geehy Semiconductor Software License Agreement for the governing permissions
* and limitations under the License.
*/

#ifdef __cplusplus
extern "C"{
#endif

#include "Std_Types.h"
#include "Det.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup DET_Driver
  * @brief DET driver modules
  @{
*/

/** @defgroup DET_Macros
  @{
*/
/***********************************************************************************************************************
                                    VENDOR ID & AUTOSAR VERSION INFO
***********************************************************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define DET_VENDOR_ID                          0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DET_AR_RELEASE_MAJOR_VERSION           0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DET_AR_RELEASE_MINOR_VERSION           0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DET_AR_RELEASE_REVISION_VERSION        0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DET_SW_MAJOR_VERSION                   0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DET_SW_MINOR_VERSION                   0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DET_SW_PATCH_VERSION                   0x00U

/**@} end of group DET_Macros */

/**********************************************************************************************************************
                                        VERSION INFOMATION VERIFICATION
***********************************************************************************************************************/
#ifdef ENABLE_INTERMODULE_MCAL_CHECK
    /* Make sure the AUTOSAR major version number is the same between Det.c and Std_Types.h */
    #if (DET_AR_RELEASE_MAJOR_VERSION != STD_TYPES_H_AR_RELEASE_MAJOR_VERSION)
        #error "AUTOSAR Major Version Number mismatch between Det.c and Std_Types.h"
    #endif
    /* Make sure the AUTOSAR minor version number is the same between Det.c and Std_Types.h */
    #if (DET_AR_RELEASE_MINOR_VERSION != STD_TYPES_H_AR_RELEASE_MINOR_VERSION)
        #error "AUTOSAR Minor Version Number mismatch between Det.c and Std_Types.h"
    #endif
#endif

/* Make sure the vendor id is the same between Det.c and Det.h */
#if (DET_VENDOR_ID != DET_H_VENDOR_ID)
    #error "Vendor id mismatch between Det.c and Det.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Det.c and Det.h */
#if (DET_AR_RELEASE_MAJOR_VERSION != DET_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Det.c and Det.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Det.c and Det.h */
#if (DET_AR_RELEASE_MINOR_VERSION != DET_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Det.c and Det.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Det.c and Det.h */
#if (DET_AR_RELEASE_REVISION_VERSION != DET_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Det.c and Det.h"
#endif
/* Make sure the software major version number is the same between Det.c and Det.h */
#if (DET_SW_MAJOR_VERSION != DET_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Det.c and Det.h"
#endif
/* Make sure the software minor version number is the same between Det.c and Det.h */
#if (DET_SW_MINOR_VERSION != DET_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Det.c and Det.h"
#endif
/* Make sure the software patch version number is the same between Det.c and Det.h */
#if (DET_SW_PATCH_VERSION != DET_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Det.c and Det.h"
#endif

/**********************************************************************************************************************
                                             GLOBAL VARIABLE
***********************************************************************************************************************/
/** @defgroup DET_Variable
  @{
*/

/** global variables about overflow error */
VAR(uint8, DET_VAR)    g_detOverflowEvent;
VAR(uint8, DET_VAR)    g_detRuntimeOverflowEvent;
VAR(uint8, DET_VAR)    g_detTransOverflowEvent;

/** global variables to count error */
VAR(uint32, DET_VAR)   g_detEventErrNum;
VAR(uint32, DET_VAR)   g_detRuntimeEventErrNum;
VAR(uint32, DET_VAR)   g_detTransEventErrNum;

/** Variables to store last DET transient error */
VAR(uint8 , DET_VAR)   g_detTransInstanceId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detTransApiId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detTransFaultId[DET_MAX_EVENTS_NUM];
VAR(uint16, DET_VAR)   g_detTransModuleId[DET_MAX_EVENTS_NUM];

/** Variables to store last DET runtime error */
VAR(uint8 , DET_VAR)   g_detRuntimeInstanceId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detRuntimeApiId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detRuntimeErrId[DET_MAX_EVENTS_NUM];
VAR(uint16, DET_VAR)   g_detRuntimeModuleId[DET_MAX_EVENTS_NUM];

/** Variables to store last DET error */
VAR(uint8 , DET_VAR)   g_detInstanceId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detApiId[DET_MAX_EVENTS_NUM];
VAR(uint8 , DET_VAR)   g_detErrId[DET_MAX_EVENTS_NUM];
VAR(uint16, DET_VAR)   g_detModuleId[DET_MAX_EVENTS_NUM];

/**@} end of group DET_Variable */
/**********************************************************************************************************************
                                                    GLOBAL FUNCTION
***********************************************************************************************************************/
/** @defgroup DET_Fuctions
  @{
*/

#ifndef ASR_REL_4_0_REV_0003
/*!
* @brief             The function is used to report the transient errors.
*
* @param[in]         ModuleId      The module id
* @param[in]         InstanceId    The instance id
* @param[in]         ApiId         The API id
* @param[in]         FaultId       The error id
* @param[out]        None
* @param[in,out]     None
*
* @retval            E_OK          The Operation was successful
*                    E_NOT_OK      The Operation was failed
*/
FUNC(Std_ReturnType, DET_CODE) Det_ReportTransientFault(VAR(uint16, AUTOMATIC) ModuleId, VAR(uint8, AUTOMATIC) InstanceId,
                                                        VAR(uint8, AUTOMATIC) ApiId, VAR(uint8, AUTOMATIC) FaultId)
{
    g_detTransOverflowEvent = (DET_MAX_EVENTS_NUM > g_detTransEventErrNum) ? 0U : 1U;
    g_detTransEventErrNum = (0U == g_detTransOverflowEvent) ? g_detTransEventErrNum : (g_detTransEventErrNum - 1U);

    g_detTransModuleId[g_detTransEventErrNum] = ModuleId;
    g_detTransInstanceId[g_detTransEventErrNum] = InstanceId;
    g_detTransApiId[g_detTransEventErrNum] = ApiId;
    g_detTransFaultId[g_detTransEventErrNum] = FaultId;

    if(0U == g_detTransOverflowEvent)
    {
        g_detTransEventErrNum++;

        g_detTransEventErrNum =(DET_MAX_EVENTS_NUM == g_detEventErrNum) ? 0U : g_detTransEventErrNum;
    }
    else
    {
        g_detTransEventErrNum = g_detTransEventErrNum + 2U;
    }

    return E_OK;
}

/*!
* @brief             The function is used to report the runtime errors.
*
* @param[in]         ModuleId      The module id
* @param[in]         InstanceId    The instance id
* @param[in]         ApiId         The API id
* @param[in]         ErrorId       The error id
* @param[out]        None
* @param[in,out]     None
*
* @retval            E_OK          The Operation was successful
*                    E_NOT_OK      The Operation was failed
*/
FUNC(Std_ReturnType, DET_CODE) Det_ReportRuntimeError(VAR(uint16, AUTOMATIC) ModuleId, VAR(uint8, AUTOMATIC) InstanceId,
                                                      VAR(uint8, AUTOMATIC) ApiId, VAR(uint8, AUTOMATIC) ErrorId)
{
    g_detRuntimeOverflowEvent = (DET_MAX_EVENTS_NUM > g_detRuntimeEventErrNum) ? 0U : 1U;
    g_detRuntimeEventErrNum = (0U == g_detRuntimeOverflowEvent) ? g_detRuntimeEventErrNum : (g_detRuntimeEventErrNum - 1U);

    g_detRuntimeModuleId[g_detRuntimeEventErrNum] = ModuleId;
    g_detRuntimeInstanceId[g_detRuntimeEventErrNum] = InstanceId;
    g_detRuntimeApiId[g_detRuntimeEventErrNum] = ApiId;
    g_detRuntimeErrId[g_detRuntimeEventErrNum] = ErrorId;

    g_detRuntimeEventErrNum++;

    if(0U == g_detRuntimeOverflowEvent)
    {
        g_detRuntimeEventErrNum = (g_detRuntimeEventErrNum == DET_MAX_EVENTS_NUM) ? 0U : g_detRuntimeEventErrNum;
    }
    else
    {
        /* Do nothing */
    }

    return E_OK;
}
#endif

/*!
* @brief             The function is used toi report the development errors.
*
* @param[in]         ModuleId      The module id
* @param[in]         InstanceId    The instance id
* @param[in]         ApiId         The API id
* @param[in]         ErrorId       The error id
* @param[out]        None
* @param[in,out]     None
*
* @retval            E_OK          The Operation was successful
*                    E_NOT_OK      The Operation was failed
*/
FUNC(Std_ReturnType, DET_CODE) Det_ReportError(VAR(uint16, AUTOMATIC) ModuleId, VAR(uint8, AUTOMATIC) InstanceId,
                                               VAR(uint8, AUTOMATIC) ApiId, VAR(uint8, AUTOMATIC) ErrorId)
{
    g_detOverflowEvent = (DET_MAX_EVENTS_NUM > g_detEventErrNum) ? 0U : 1U;
    g_detEventErrNum = (0U == g_detOverflowEvent) ? g_detEventErrNum : (g_detEventErrNum - 1U);

    g_detModuleId[g_detEventErrNum] = ModuleId;
    g_detInstanceId[g_detEventErrNum] = InstanceId;
    g_detApiId[g_detEventErrNum] = ApiId;
    g_detErrId[g_detEventErrNum] = ErrorId;

    g_detEventErrNum++;

    if(0U == g_detOverflowEvent)
    {
       g_detEventErrNum = (DET_MAX_EVENTS_NUM == g_detEventErrNum) ? 0U : g_detEventErrNum;
    }
    else
    {
        /* Do nothing */
    }
    return E_OK;
}

/*!
* @brief             The function is used to initialize the DET module.
*
* @param[in]         None
* @param[out]        None
* @param[in,out]     None
*
* @retval            None
*/
FUNC(void, DET_CODE) Det_Init(void)
{

}

/*!
* @brief             The function is used to start the DET module after the Det_Init has been called.
*
* @param[in]         None
* @param[out]        None
* @param[in,out]     None
*
* @retval            None
*/
FUNC(void, DET_CODE) Det_Start(void)
{

}

/**@} end of group DET_Fuctions */

#ifdef __cplusplus
}
#endif

/**@} end of group DET_Driver */
/**@} end of group G32A10xx_StdPeriphDriver */
