/*!
* @file     Dem.c
*
* @version  1.0.0
*
* @brief    The source file of the Dem 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 "Dem.h"

/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup DEM_Driver
  * @brief DEM driver modules
  @{
*/

/** @defgroup DEM_Macros
  @{
*/
/***********************************************************************************************************************
                                    VENDOR ID & AUTOSAR VERSION INFO
***********************************************************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define DEM_VENDOR_ID                          0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DEM_AR_RELEASE_MAJOR_VERSION           0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DEM_AR_RELEASE_MINOR_VERSION           0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define DEM_AR_RELEASE_REVISION_VERSION        0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DEM_SW_MAJOR_VERSION                   0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DEM_SW_MINOR_VERSION                   0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define DEM_SW_PATCH_VERSION                   0x00U

/**@} end of group DEM_Macros */

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

/* Make sure the vendor id is the same between Dem.c and Dem.h */
#if (DEM_VENDOR_ID != DEM_H_VENDOR_ID)
    #error "Vendor id mismatch between Dem.c and Dem.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Dem.c and Dem.h */
#if (DEM_AR_RELEASE_MAJOR_VERSION != DEM_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR Major Version Number mismatch between Dem.c and Dem.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Dem.c and Dem.h */
#if (DEM_AR_RELEASE_MINOR_VERSION != DEM_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR Minor Version Number mismatch between Dem.c and Dem.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Dem.c and Dem.h */
#if (DEM_AR_RELEASE_REVISION_VERSION != DEM_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR Revision Version Number mismatch between Dem.c and Dem.h"
#endif
/* Make sure the software major version number is the same between Dem.c and Dem.h */
#if (DEM_SW_MAJOR_VERSION != DEM_H_SW_MAJOR_VERSION)
    #error "Software Major Version Number mismatch between Dem.c and Dem.h"
#endif
/* Make sure the software minor version number is the same between Dem.c and Dem.h */
#if (DEM_SW_MINOR_VERSION != DEM_H_SW_MINOR_VERSION)
    #error "Software Minor Version Number mismatch between Dem.c and Dem.h"
#endif
/* Make sure the software patch version number is the same between Dem.c and Dem.h */
#if (DEM_SW_PATCH_VERSION != DEM_H_SW_PATCH_VERSION)
    #error "Software Patch Version Number mismatch between Dem.c and Dem.h"
#endif

/**********************************************************************************************************************
                                             GLOBAL VARIABLE
***********************************************************************************************************************/
/** @defgroup DEM_Variable
  @{
*/

VAR(uint8, DEM_VAR)  g_demOverflowEvent;
VAR(Dem_EventStatusType, DEM_VAR)  g_demExtendData[DEM_MAX_EXTEND_DATA_LEN];
VAR(Dem_EventIdType, DEM_VAR) g_demEventId[DEM_MAX_EVENTS_NUM];
VAR(Dem_EventStatusType, DEM_VAR) g_demEventStatus[DEM_MAX_EVENTS_NUM];
VAR(uint32, DEM_VAR) g_demEventErrNum = (uint32)0;

/**@} end of group DEM_Variable */

/**********************************************************************************************************************
                                                    GLOBAL FUNCTION
***********************************************************************************************************************/
/** @defgroup DEM_Fuctions
  @{
*/

/*!
* @brief            The function sets the error status.
*
* @param[in]        EventId               The id of the event
* @param[in]        EventStatus           The status of the event
* @param[out]       None
* @param[in,out]    None
*
* @retval           Std_ReturnType        The function is always successful
*/ 
FUNC(Std_ReturnType, DEM_CODE) Dem_SetEventStatus(VAR(Dem_EventIdType, AUTOMATIC) EventId,
                                                  VAR(Dem_EventStatusType, AUTOMATIC) EventStatus)
{
    g_demOverflowEvent = (DEM_MAX_EVENTS_NUM > g_demEventErrNum) ? 0U : 1U;

    g_demEventErrNum = (0U == g_demOverflowEvent) ? g_demEventErrNum : (g_demEventErrNum - 1U);

    g_demEventId[g_demEventErrNum] = EventId;

    g_demEventStatus[g_demEventErrNum] = EventStatus;

    g_demEventErrNum++;

    return  E_OK;
}

/*!
* @brief            The function has to be used if preextended data shall be logged at the time 
*                   the event has been reported as "Failed".
*                   In case thar the EvTestResult is "Passed" the parameters pPreExtData and 
*                   PreExtDataSize will be ignored.
*
* @param[in]        EventIdRaw        The event identification
* @param[in]        EventStatusRaw    The status of the event(DEM_EVENT_STATUS_PASSED/DEM_EVENT_STATUS_FAILED...)
* @param[in]        PreExtDataPtr     The pointer of the pre-extended data
* @param[in]        PreExtDataLen     The length of the pre-extended data
* @param[out]       None
* @param[in,out]    None
*
* @retval           E_OK        The Operation was successful
*                   E_NOT_OK    The Operation was failed
*/ 
FUNC(Std_ReturnType, DEM_CODE) Dem_RepErrStatusPreExtData(VAR(Dem_EventIdType, AUTOMATIC) EventIdRaw,
                                                          VAR(Dem_EventStatusType, AUTOMATIC) EventStatusRaw,
                                                          CONSTP2CONST(uint8, DEM_CONST, DEM_APPL_CONST) PreExtDataPtr,
                                                          VAR(uint8, AUTOMATIC) PreExtDataLen)
{
    VAR(uint8, DEM_VAR) cnter = 0;
    VAR(Std_ReturnType, DEM_VAR) returnVal = E_NOT_OK;

    (void)Dem_SetEventStatus(EventIdRaw, EventStatusRaw);

    /* If the g_demEventStatus is DEM_EVENT_STATUS_FAILED, store the extended data */
    if ((Dem_EventStatusType)DEM_EVENT_STATUS_FAILED == EventStatusRaw)
    {
        returnVal = (Std_ReturnType)((PreExtDataLen <= DEM_MAX_EXTEND_DATA_LEN) ? E_OK : E_NOT_OK );

        if((Std_ReturnType)E_OK == returnVal)
        {
            while( cnter < PreExtDataLen )
            {
                g_demExtendData[cnter] = PreExtDataPtr[cnter];
                cnter++;
            }
        }
        else
        {
            /* Intentionally Empty */
        }
    }
    else
    {
        /* Intentionally Empty */
    }

    return returnVal;
}

/**@} end of group DEM_Fuctions */

#ifdef __cplusplus
}
#endif

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