/* BEGIN_FILE_HDR ******************************************************************************* * NOTICE * This software is the property of HiRain Technologies. Any information * contained in this doc should not be reproduced, or used, or disclosed * without the written authorization from HiRain Technologies. ******************************************************************************* * File Name : E2E_P22.c ******************************************************************************* * Project/Product : AUTOSAR R21-11 * Title : End-to-End protect module * Author : Hirain ******************************************************************************* * Description : The source file of the End-to-End module. * ******************************************************************************* * Limitations : None * ******************************************************************************* * ******************************************************************************* * Revision History: * * Version Date Initials CR# Descriptions * --------- ---------- ------------ ---------- --------------- * 04.00.00 11/08/2022 Wenbo.Cui N/A Original ******************************************************************************* * END_FILE_HDR*/ /****************************************************************************** * Includes ******************************************************************************/ #include "E2E_P22.h" /******************************************************************************* * General QAC Suppression *******************************************************************************/ /*PRQA S 3214,0862,3614 EOF*/ /* According to the requirements of AUTOSAR Specification,when locating address segments in the files of each module,#include "E2E_MemMap.h" is required.Defined XXX_START_SEC_ will undefine in E2E_MemMap.h */ /*PRQA S 3432 EOF*/ /* P2VAR and other macros in the Compiler conforms to AUTOSAR standard.There's no need to revise. */ /***************************************************************************** * E2E QAC Suppression *****************************************************************************/ /*PRQA S 4640,4641 EOF*/ /* Function, macrodefinition is named with a fixed requirement and does not make changes */ /*PRQA S 3429,3453 EOF*/ /* Allow the use of macrofunction access definitions, and the department is verified and there is no problem. */ /*PRQA S 3435,3456 EOF*/ /* Allow multiple use parameters in macros */ /*PRQA S 3383,3384 EOF*/ /* The previously logical judgment of the data is guaranteed that the data value will not overflow */ /****************************************************************************** * Macro ******************************************************************************/ #define E2E_P22_CRC_INIT_VALUE ((uint8)0xFF) /*PRS_E2E_00122*/ #define E2E_P22_CRC_OFFSET ((uint16)0) /*PRS_E2E_00121 */ #define E2E_P22_MAX_DATA_LENGTH ((uint16)2048)/*in bits*/ #define E2E_P22_Min_DATA_LENGTH ((uint16)16)/*in bits*/ #define E2E_P22_DATA_UINT_BIT ((uint16)8) /*PRS_E2E_00219*//*PRS_E2E_00128*/ #define E2E_P22_MAX_COUNTER_VALUE ((uint8)15) /*in bytes*/ #define E2E_P22_BYTE_ALIGNED_MASK ((uint16)0x0007) #define E2E_P22_LOW_NIBBLE_MASK ((uint8)0x0F) #define E2E_P22_HIGH_NIBBLE_MASK ((uint8)0xF0) #define E2E_P22_GET_HEADEROFFSET(config) ((config)->Offset >> (uint16)3) #define E2E_P22_IS_NOT_BYTE_ALIGNED(bit) ((uint16)((bit) & E2E_P22_BYTE_ALIGNED_MASK) != (uint16)0) #define E2E_START_SEC_CODE #include "E2E_MemMap.h" /****************************************************************************** * Private Functions Declare ******************************************************************************/ STATIC FUNC(void, E2E_CODE)E2E_P22CheckRecvCounter ( uint8 Counter, P2CONST(E2E_P22ConfigType, AUTOMATIC, E2E_APPL_CONST) Config, P2VAR(E2E_P22CheckStateType, AUTOMATIC, E2E_APPL_DATA) State ); STATIC FUNC(uint8, E2E_CODE) E2E_P22GetDeltaCounter ( uint8 Counter, uint8 LastValidCounter ); STATIC FUNC(uint8, E2E_CODE)E2E_P22GetCrc ( uint8 DataId, uint16 offset, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) Data, uint16 DataLen ); /****************************************************************************** * Private Functions ******************************************************************************/ /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22CheckRecvCounter * * Description: Check the received counter. * * Inputs: Counter : Received counter.. * Config : Pointer to static configuration. * State : Pointer to port/data communication state. * * Outputs: None. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*PRS_E2E_00539*/ STATIC FUNC(void, E2E_CODE)E2E_P22CheckRecvCounter ( uint8 Counter, P2CONST(E2E_P22ConfigType, AUTOMATIC, E2E_APPL_CONST) Config, P2VAR(E2E_P22CheckStateType, AUTOMATIC, E2E_APPL_DATA) State ) { uint8 DeltaCounter; /*get delta counter*/ DeltaCounter = E2E_P22GetDeltaCounter(Counter, State->Counter); if ((uint8)0 == DeltaCounter) { State->Status = E2E_P22STATUS_REPEATED; } else if ((uint8)1 == DeltaCounter) { State->Status = E2E_P22STATUS_OK; } else if (DeltaCounter <= Config->MaxDeltaCounter) { State->Status = E2E_P22STATUS_OKSOMELOST; } else { State->Status = E2E_P22STATUS_WRONGSEQUENCE; } State->Counter = Counter; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22GetDeltaCounter * * Description: Calculate the delta counter. * * Inputs: Counter : Received counter.. * LastValidCounter : The last valid counter value. * * Outputs: The delta counter value. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00135*/ STATIC FUNC(uint8, E2E_CODE) E2E_P22GetDeltaCounter ( uint8 Counter, uint8 LastValidCounter ) { uint8 DeltaCounter; if(Counter >= LastValidCounter) { DeltaCounter = Counter - LastValidCounter; } else { DeltaCounter = ((uint8)(E2E_P22_MAX_COUNTER_VALUE - LastValidCounter) + (uint8)1) + Counter; } return DeltaCounter; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22GetCrc * * Description: Calculate the crc value. * * Inputs: DataId : Data ID. * Offset : Protect header offset in bytes. * Data : Pointer to the data to be protected. * DataLen : The length of the data to be protected, except crc. * * Outputs: The crc value. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ STATIC FUNC(uint8, E2E_CODE)E2E_P22GetCrc ( uint8 DataId, uint16 offset, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) Data, uint16 DataLen ) { uint8 CrcData = DataId; uint16 CrcCopyLength; /*SWS_E2E_00118*/ uint8 crc = E2E_P22_CRC_INIT_VALUE; if(offset > (uint16)0) { CrcCopyLength = (uint16)(DataLen - offset) - (uint16)1; /*SWS_E2E_00117*//*SWS_E2E_00221*//*SWS_E2E_00219*//*SWS_E2E_00110*/ crc = Crc_CalculateCRC8H2F(&Data[0], (uint32)offset, crc, TRUE); /*SWS_E2E_00117*//*SWS_E2E_00221*//*SWS_E2E_00219*//*SWS_E2E_00110*/ crc = Crc_CalculateCRC8H2F(&Data[offset + (uint16)1], (uint32)CrcCopyLength, crc, FALSE); } else { CrcCopyLength = (uint16)(DataLen - (uint16)1); /*SWS_E2E_00117*//*SWS_E2E_00221*//*SWS_E2E_00219*//*SWS_E2E_00110*/ crc = Crc_CalculateCRC8H2F(&Data[offset + (uint16)1], (uint32)CrcCopyLength, crc, TRUE); } crc = Crc_CalculateCRC8H2F(&CrcData, (uint32)1, crc, FALSE); return crc; } /****************************************************************************** * Public Funtions ******************************************************************************/ /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22ProtectInit * * Description: Initializes the protection state. * * Inputs: StatePtr : Pointer to port/data communication state. * * Outputs: E2E_E_INPUTERR_NULL, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00387*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P22ProtectInit ( P2VAR(E2E_P22ProtectStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr ) { Std_ReturnType ret; /*SWS_E2E_00559*//*SWS_E2E_00011*/ if(NULL_PTR == StatePtr) { ret = E2E_E_INPUTERR_NULL; } else { ret = E2E_E_OK; /*PRS_E2E_00523*/ StatePtr->Counter = (uint8)0; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22Protect * * Description: Protects the array/buffer to be transmitted using the E2E profile 22. * This includes checksum calculation, handling of sequence counter * and Data ID. * * Inputs: ConfigPtr : Pointer to static configuration. * StatePtr : Pointer to port/data communication state. * DataPtr : Pointer to the data to be protected. * Length : Length of the data in bytes. * * Outputs: E2E_E_INPUTERR_NULL, * E2E_E_INPUTERR_WRONG, * E2E_E_INTERR, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00160*//*SWS_E2E_00124*//*SWS_E2E_00126*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P22Protect ( P2CONST(E2E_P22ConfigType, AUTOMATIC, E2E_APPL_CONST) ConfigPtr, P2VAR(E2E_P22ProtectStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr, P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 Length ) { uint8 dataId; uint8 crc; uint8 counter; Std_ReturnType ret = E2E_E_OK; uint16 headOffset; /*SWS_E2E_00011*/ if((NULL_PTR == ConfigPtr) || (NULL_PTR == StatePtr) || (NULL_PTR == DataPtr)) { ret = E2E_E_INPUTERR_NULL; } else if ((E2E_P22_MAX_DATA_LENGTH < (ConfigPtr->DataLength)) || (E2E_P22_Min_DATA_LENGTH > (ConfigPtr->DataLength)) || (E2E_P22_IS_NOT_BYTE_ALIGNED(ConfigPtr->DataLength))) { ret = E2E_E_INPUTERR_WRONG; } else if(E2E_P22_CRC_OFFSET != ConfigPtr->Offset) { ret = E2E_E_INPUTERR_WRONG; } else if((Length * E2E_P22_DATA_UINT_BIT) != ConfigPtr->DataLength) { ret = E2E_E_INPUTERR_WRONG; } else { /*increase counter*//*SWS_E2E_00221*//*SWS_E2E_00219*//*SWS_E2E_00127*//*SWS_E2E_00129*/ /*SWS_E2E_00130*/ counter = (uint8)(StatePtr->Counter + (uint8)1) & E2E_P22_LOW_NIBBLE_MASK; StatePtr->Counter = counter; headOffset = E2E_P22_GET_HEADEROFFSET(ConfigPtr); /*record the counter*//*SWS_E2E_00123*//*SWS_E2E_00124*/ DataPtr[headOffset + (uint16)1] = (DataPtr[headOffset + (uint16)1] & E2E_P22_HIGH_NIBBLE_MASK) | (counter); /*get data id*//*SWS_E2E_00120*/ dataId = ConfigPtr->DataIDList[counter]; /*calculate crc*//*SWS_E2E_00132*/ crc = E2E_P22GetCrc(dataId, headOffset, DataPtr, Length); /*SWS_E2E_00122*//*SWS_E2E_00124*//*SWS_E2E_00133*/ DataPtr[headOffset] = crc; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22Check * * Description: Check the array/buffer using the E2E profile 22. This includes * checksum calculation, handling of sequence counter and Data ID. * * Inputs: ConfigPtr : Pointer to static configuration. * StatePtr : Pointer to port/data communication state. * DataPtr : Pointer to the data to be cheecked. * Length : Length of the data in bytes. * * Outputs: E2E_E_INPUTERR_NULL, * E2E_E_INPUTERR_WRONG, * E2E_E_INTERR, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00161,SWS_E2E_00134*//*SWS_E2E_00125*/ FUNC(Std_ReturnType, E2E_CODE)E2E_P22Check ( P2CONST(E2E_P22ConfigType, AUTOMATIC, E2E_APPL_CONST) ConfigPtr, P2VAR(E2E_P22CheckStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 Length ) { uint8 recvCounter; uint8 dataId; uint8 crc; uint16 headOffset; Std_ReturnType ret = E2E_E_OK; /*PRS_E2E_00517*/ if((NULL_PTR == ConfigPtr) || (NULL_PTR == StatePtr)) { ret = E2E_E_INPUTERR_NULL; } /*check config data length*/ else if ((E2E_P22_MAX_DATA_LENGTH < (ConfigPtr->DataLength)) || (E2E_P22_Min_DATA_LENGTH > (ConfigPtr->DataLength)) || (E2E_P22_IS_NOT_BYTE_ALIGNED(ConfigPtr->DataLength))) { ret = E2E_E_INPUTERR_WRONG; } else if (DataPtr == NULL_PTR) { /*check the length of received data*//*PRS_E2E_00496*/ if(Length != (uint16)0) { ret = E2E_E_INPUTERR_WRONG; } else { StatePtr->Status = E2E_P22STATUS_NONEWDATA; } } /*check the length of received data*//*PRS_E2E_00496*/ else if ((Length * E2E_P22_DATA_UINT_BIT) != ConfigPtr->DataLength) { ret = E2E_E_INPUTERR_WRONG; } else { headOffset = E2E_P22_GET_HEADEROFFSET(ConfigPtr); /*get received counter*//*SWS_E2E_00123*/ recvCounter = DataPtr[headOffset + (uint16)1] & E2E_P22_LOW_NIBBLE_MASK; /*get data id SWS_E2E_00139*//*SWS_E2E_00120*/ dataId = ConfigPtr->DataIDList[recvCounter]; /*calculate crc, SWS_E2E_00140*/ crc = E2E_P22GetCrc(dataId, headOffset, DataPtr, Length); if (crc == DataPtr[headOffset]) { /*check counter*/ E2E_P22CheckRecvCounter(recvCounter, ConfigPtr, StatePtr); } else { /*SWS_E2E_00141*/ StatePtr->Status = E2E_P22STATUS_ERROR; } } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22CheckInit * * Description: Initializes the check state. * * Inputs: StatePtr : Pointer to port/data communication state. * * Outputs: E2E_E_INPUTERR_NULL, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00391*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P22CheckInit ( P2VAR(E2E_P22CheckStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr ) { Std_ReturnType ret; /*SWS_E2E_00392*//*SWS_E2E_00011*/ if(NULL_PTR == StatePtr) { ret = E2E_E_INPUTERR_NULL; } else { StatePtr->Status = E2E_P22STATUS_ERROR; /*SWS_E2E_00471*/ StatePtr->Counter = E2E_P22_MAX_COUNTER_VALUE; ret = E2E_E_OK; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P22MapStatusToSM * * Description: The function maps the check status of Profile 22 to a generic * check status, which can be used by E2E state machine check function. * The E2E Profile 22 delivers a more fine-granular status, but this * is not relevant for the E2E state machine. * * Inputs: CheckReturn : Return value of the E2E_P22Check function. * Status : Status determined by E2E_P22Check function * * Outputs: Profile-independent status of the reception on one single Data in one cycle. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_00379*/ FUNC(E2E_PCheckStatusType, E2E_CODE) E2E_P22MapStatusToSM ( Std_ReturnType CheckReturn, E2E_P22CheckStatusType Status ) { /*SWS_E2E_00381*/ E2E_PCheckStatusType CheckStatus = E2E_P_ERROR; /*SWS_E2E_00380,SWS_E2E_00477*/ if(E2E_E_OK == CheckReturn) { if((E2E_P22STATUS_OK == Status) || (E2E_P22STATUS_OKSOMELOST == Status)) { CheckStatus = E2E_P_OK; } else if(E2E_P22STATUS_REPEATED == Status) { CheckStatus = E2E_P_REPEATED; } else if(E2E_P22STATUS_NONEWDATA == Status) { CheckStatus = E2E_P_NONEWDATA; } else if(E2E_P22STATUS_WRONGSEQUENCE == Status) { CheckStatus = E2E_P_WRONGSEQUENCE; } else { CheckStatus = E2E_P_ERROR; } } return CheckStatus; } #define E2E_STOP_SEC_CODE #include "E2E_MemMap.h"