/* 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_P44.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_P44_CRC_INIT_VALUE ((uint32)0xFFFFFFFF) #define E2E_P44_HEADER_BYTE_LENGTH ((uint32)12) /*PRS_E2E_00707*/ #define E2E_P44_LENGTH_POS ((uint16)0) #define E2E_P44_COUNTER_POS ((uint16)2) #define E2E_P44_DATAID_POS ((uint32)4) #define E2E_P44_CRC_POS ((uint32)8) #define E2E_P44_CRC_LEN ((uint32)4) #define E2E_P44_MAX_DATA_LENGTH ((uint32)(65535*8)) #define E2E_P44_MIN_DATA_LENGTH ((uint32)(12*8)) #define E2E_P44_DATA_UINT_BIT ((uint32)8) /*PRS_E2E_00219*//*PRS_E2E_00128*/ #define E2E_P44_BYTE_OFFSET_BIT ((uint64)8) #define E2E_P44_TWOBYTE_OFFSET_BIT ((uint64)16) #define E2E_P44_THRESSBYTE_OFFSET_BIT ((uint64)24) #define E2E_P44_INDEX_2 (2) #define E2E_P44_INDEX_3 (3) #define E2E_P44_MAX_COUNTER_VALUE ((uint16)0xFFFF) #define E2E_P44_BYTE_MASK ((uint8)0xFF) #define E2E_P44_BYTE_ALIGNED_MASK ((uint32)0x0007) #define E2E_P44_GET_HEADEROFFSET(config) ((config)->Offset >> (uint32)3) #define E2E_P44_IS_NOT_BYTE_ALIGNED(bit) ((uint32)((bit) & E2E_P44_BYTE_ALIGNED_MASK) != (uint32)0) #define E2E_P44_BETOMCU16(data) (uint16)( (uint16)(((uint16)(data)[0] & (uint16)0x00FF) << (uint16)8)\ | (uint16)(((uint16)(data)[1] & (uint16)0x00FF) << (uint16)0)) #define E2E_P44_BETOMCU32(data) (uint32)( (((uint32)(data)[0] & (uint32)0xFF) << (uint32)24)\ | (((uint32)(data)[1] & (uint32)0xFF) << (uint32)16)\ | (((uint32)(data)[2] & (uint32)0xFF) << (uint32)8)\ | (((uint32)(data)[3] & (uint32)0xFF) << (uint32)0)) #define E2E_START_SEC_CODE #include "E2E_MemMap.h" /****************************************************************************** * Private Functions Declare ******************************************************************************/ STATIC FUNC(void, E2E_CODE)E2E_P44CheckRecvCounter ( uint16 Counter, P2CONST(E2E_P44ConfigType, AUTOMATIC, E2E_APPL_CONST) Config, P2VAR(E2E_P44CheckStateType, AUTOMATIC, E2E_APPL_DATA) State ); STATIC FUNC(uint16, E2E_CODE) E2E_P44GetDeltaCounter ( uint16 Counter, uint16 LastValidCounter ); STATIC FUNC(uint32, E2E_CODE)E2E_P44GetCrc ( uint32 offset, uint16 DataLen, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) Data ); STATIC FUNC(void, E2E_CODE)E2E_P44Mcu16ToBeData ( P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 data ); STATIC FUNC(void, E2E_CODE)E2E_P44Mcu32ToBeData ( P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint32 data ); /****************************************************************************** * Private Functions ******************************************************************************/ /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44CheckRecvCounter * * 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 */ STATIC FUNC(void, E2E_CODE)E2E_P44CheckRecvCounter ( uint16 Counter, P2CONST(E2E_P44ConfigType, AUTOMATIC, E2E_APPL_CONST) Config, P2VAR(E2E_P44CheckStateType, AUTOMATIC, E2E_APPL_DATA) State ) { uint16 DeltaCounter; /*compute delta counter*/ DeltaCounter = E2E_P44GetDeltaCounter(Counter, State->Counter); if((uint16)0 == DeltaCounter) { State->Status = E2E_P44STATUS_REPEATED; } else if((uint16)1 == DeltaCounter) { State->Status = E2E_P44STATUS_OK; } else if(DeltaCounter <= Config->MaxDeltaCounter) { State->Status = E2E_P44STATUS_OKSOMELOST; } else { State->Status = E2E_P44STATUS_WRONGSEQUENCE; } State->Counter = Counter; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44GetDeltaCounter * * 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(uint16, E2E_CODE) E2E_P44GetDeltaCounter ( uint16 Counter, uint16 LastValidCounter ) { uint16 DeltaCounter; if(Counter >= LastValidCounter) { DeltaCounter = Counter - LastValidCounter; } else { DeltaCounter = ((uint16)(E2E_P44_MAX_COUNTER_VALUE - LastValidCounter) + (uint16)1) + Counter; } return DeltaCounter; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44GetCrc * * 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(uint32, E2E_CODE)E2E_P44GetCrc ( uint32 offset, uint16 DataLen, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) Data ) { uint32 crc = E2E_P44_CRC_INIT_VALUE; uint32 Offsetlen; /*PRS_E2E_02367*/ /*compute CRC over bytes that are before CRC.*/ crc = Crc_CalculateCRC32P4(&Data[0], (uint32)offset + E2E_P44_CRC_POS , crc, TRUE); Offsetlen = offset + E2E_P44_CRC_POS + E2E_P44_CRC_LEN; if(Offsetlen < DataLen) { /*Compute CRC over bytes that are after CRC*/ crc = Crc_CalculateCRC32P4(&Data[offset + E2E_P44_CRC_POS + E2E_P44_CRC_LEN], (uint32)(DataLen - offset - E2E_P44_CRC_POS - E2E_P44_CRC_LEN), crc, FALSE); } else { /*Nothing*/ } return crc; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44Mcu16ToBeData * * Description: Converte the 16-bit data to big endian 8-bit data * * Inputs: DataPtr : Pointer to the data. * Data : Data to be converted. * * Outputs: None. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ STATIC FUNC(void, E2E_CODE)E2E_P44Mcu16ToBeData ( P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 data ) { DataPtr[0] = (uint8)((uint8)(data >> E2E_P44_BYTE_OFFSET_BIT) & E2E_P44_BYTE_MASK); DataPtr[1] = (uint8)(uint8)(data & E2E_P44_BYTE_MASK); } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44Mcu32ToBeData * * Description: Converte the 32-bit data to big endian 8-bit data * * Inputs: DataPtr : Pointer to the data. * Data : Data to be converted. * * Outputs: None. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ STATIC FUNC(void, E2E_CODE)E2E_P44Mcu32ToBeData ( P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint32 data ) { DataPtr[0] = (uint8)((uint8)(data >> E2E_P44_THRESSBYTE_OFFSET_BIT) & E2E_P44_BYTE_MASK); DataPtr[1] = (uint8)((uint8)(data >> E2E_P44_TWOBYTE_OFFSET_BIT) & E2E_P44_BYTE_MASK); DataPtr[E2E_P44_INDEX_2] = (uint8)((uint8)(data >> E2E_P44_BYTE_OFFSET_BIT) & E2E_P44_BYTE_MASK); DataPtr[E2E_P44_INDEX_3] = (uint8)((uint8)data & E2E_P44_BYTE_MASK); } /****************************************************************************** * Public Funtions ******************************************************************************/ /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44ProtectInit * * Description: Initializes the protection state. * * Outputs: StatePtr : Pointer to port/data communication state. * * Return value: E2E_E_INPUTERR_NULL, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_91028*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P44ProtectInit ( P2VAR(E2E_P44ProtectStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr ) { Std_ReturnType ret; /*SWS_E2E_10001*/ if(NULL_PTR == StatePtr) { ret = E2E_E_INPUTERR_NULL; } else { ret = E2E_E_OK; /*PRS_E2E_00523*/ StatePtr->Counter = (uint16)0; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44Protect * * Description: Protects the array/buffer to be transmitted using the E2E profile 44. * This includes checksum calculation, handling of counter and Data ID. * * Inputs: ConfigPtr : Pointer to static configuration. * Length : Length of the data in bytes. * * Inout: StatePtr : Pointer to port/data communication state. * DataPtr : Pointer to the data to be protected. * * Return value: E2E_E_INPUTERR_NULL, * E2E_E_INPUTERR_WRONG, * E2E_E_INTERR, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_91027*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P44Protect ( P2CONST(E2E_P44ConfigType, AUTOMATIC, E2E_APPL_CONST) ConfigPtr, P2VAR(E2E_P44ProtectStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr, P2VAR(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 Length ) { uint32 crc; Std_ReturnType ret = E2E_E_OK; uint32 headerOffset; /*PRS_E2E_00712*//*PRS_E2E_02362*/ /*Verify inputs*//*PRS_E2E_00713*//*PRS_E2E_02363*/ if((NULL_PTR == ConfigPtr) || (NULL_PTR == StatePtr) || (NULL_PTR == DataPtr)) { ret = E2E_E_INPUTERR_NULL; } /*PRS_E2E_00735*/ else if (((ConfigPtr->MinDataLength) < E2E_P44_MIN_DATA_LENGTH) || (ConfigPtr->MaxDataLength > E2E_P44_MAX_DATA_LENGTH) || (ConfigPtr->MinDataLength > ConfigPtr->MaxDataLength) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->MinDataLength)) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->MaxDataLength))) { ret = E2E_E_INPUTERR_WRONG; } else if (((Length * E2E_P44_DATA_UINT_BIT) < ConfigPtr->MinDataLength) || ((Length * E2E_P44_DATA_UINT_BIT) > ConfigPtr->MaxDataLength)) { ret = E2E_E_INPUTERR_WRONG; } else if ((ConfigPtr->Offset > ((Length - E2E_P44_HEADER_BYTE_LENGTH) * E2E_P44_DATA_UINT_BIT)) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->Offset))) { ret = E2E_E_INPUTERR_WRONG; } else { /*Compute offset*//*PRS_E2E_00714*//*PRS_E2E_02376*/ headerOffset = E2E_P44_GET_HEADEROFFSET(ConfigPtr); /*Write Length to data in big endian order*//*PRS_E2E_00715*//*PRS_E2E_02364]*/ E2E_P44Mcu16ToBeData(&DataPtr[headerOffset + E2E_P44_LENGTH_POS], Length); /*Write Counter to data in big endian order*//*PRS_E2E_00716*//*PRS_E2E_02365*/ E2E_P44Mcu16ToBeData(&DataPtr[headerOffset + E2E_P44_COUNTER_POS], StatePtr->Counter); /*Write DataID to data in big endian order*//*PRS_E2E_00717*//*PRS_E2E_02366*/ E2E_P44Mcu32ToBeData(&DataPtr[headerOffset + E2E_P44_DATAID_POS], ConfigPtr->DataID); /*calculate crc*//*PRS_E2E_00718*//*PRS_E2E_02367*/ crc = E2E_P44GetCrc(headerOffset, Length, DataPtr); /*Write crc to data in big endian order*//*PRS_E2E_00719*//*SWS_E2E_00368*/ E2E_P44Mcu32ToBeData(&DataPtr[headerOffset + E2E_P44_CRC_POS], crc); /*Increment Counter*//*PRS_E2E_00720*//*PRS_E2E_00494*/ StatePtr->Counter++; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44Check * * Description: Checks the Data received using the E2E profile 44. * This includes CRC calculation,handling of Counter and Data ID. * * Inputs: ConfigPtr : Pointer to static configuration. * DataPtr : Pointer to the data to be cheecked. * Length : Length of the data in bytes. * * Inout: StatePtr : Pointer to port/data communication state. * * Return value: E2E_E_INPUTERR_NULL, * E2E_E_INPUTERR_WRONG, * E2E_E_INTERR, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_91030*/ FUNC(Std_ReturnType, E2E_CODE)E2E_P44Check ( P2CONST(E2E_P44ConfigType, AUTOMATIC, E2E_APPL_CONST) ConfigPtr, P2VAR(E2E_P44CheckStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr, P2CONST(uint8, AUTOMATIC, E2E_APPL_DATA) DataPtr, uint16 Length ) { Std_ReturnType ret = E2E_E_OK; uint32 calCrc; uint32 recvCrc; uint32 headOffset; uint16 recvCounter; uint16 recvLength; uint32 recvDataID; /*PRS_E2E_00725*//*PRS_E2E_02355*/ /*Verify inputs of the check function*//*PRS_E2E_00726*//*PRS_E2E_02356*/ if((NULL_PTR == ConfigPtr) || (NULL_PTR == StatePtr)) { ret = E2E_E_INPUTERR_NULL; } /*check config data length*/ else if (((ConfigPtr->MinDataLength) < E2E_P44_MIN_DATA_LENGTH) || (ConfigPtr->MaxDataLength > E2E_P44_MAX_DATA_LENGTH) || (ConfigPtr->MinDataLength > ConfigPtr->MaxDataLength) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->MinDataLength)) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->MaxDataLength))) { 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 { /*No data has been received*/ StatePtr->Status = E2E_P44STATUS_NONEWDATA; } } /*check the length of received data*/ else if (((Length * E2E_P44_DATA_UINT_BIT) > ConfigPtr->MaxDataLength) || ((Length * E2E_P44_DATA_UINT_BIT) < ConfigPtr->MinDataLength)) { ret = E2E_E_INPUTERR_WRONG; } /*the length of the data received is correct*/ else { if ((ConfigPtr->Offset > ((Length - E2E_P44_HEADER_BYTE_LENGTH) * E2E_P44_DATA_UINT_BIT)) || (E2E_P44_IS_NOT_BYTE_ALIGNED(ConfigPtr->Offset))) { ret = E2E_E_INPUTERR_WRONG; } else { /*compute offset*//*PRS_E2E_02376*/ headOffset = E2E_P44_GET_HEADEROFFSET(ConfigPtr); /*PRQA S 4571 ++*/ /*Allows less than int length data to shift and retrieve other operations, and the department is verified and there is no problem.*/ /*read received length from data*//*PRS_E2E_00727*//*PRS_E2E_02357*/ recvLength = E2E_P44_BETOMCU16(&DataPtr[headOffset + E2E_P44_LENGTH_POS]); /*read received counter from data*//*PRS_E2E_00728*//*PRS_E2E_02358*/ recvCounter = E2E_P44_BETOMCU16(&DataPtr[headOffset + E2E_P44_COUNTER_POS]); /*read data id from data*//*PRS_E2E_00729*//*PRS_E2E_02359*/ recvDataID = E2E_P44_BETOMCU32(&DataPtr[headOffset + E2E_P44_DATAID_POS]); /*PRQA S 4571 --*/ /*read received crc from data*//*PRS_E2E_00730*//*PRS_E2E_02360*/ recvCrc = E2E_P44_BETOMCU32(&DataPtr[headOffset + E2E_P44_CRC_POS]); /*Compute CRC*/ calCrc = E2E_P44GetCrc(headOffset, Length, DataPtr); /*Check if the received crc is equal to the calculated crc*//*PRS_E2E_00731*//*PRS_E2E_02361*/ if((recvCrc == calCrc) && (recvDataID == ConfigPtr->DataID) && (recvLength == Length)) { /*check counter*/ E2E_P44CheckRecvCounter(recvCounter, ConfigPtr, StatePtr); } else { StatePtr->Status = E2E_P44STATUS_ERROR; } } } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44CheckInit * * Description: Initializes the check state. * * Outputs: StatePtr : Pointer to port/data communication state. * * Return vakue: E2E_E_INPUTERR_NULL, * E2E_E_OK * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_91031*/ FUNC(Std_ReturnType, E2E_CODE) E2E_P44CheckInit ( P2VAR(E2E_P44CheckStateType, AUTOMATIC, E2E_APPL_DATA) StatePtr ) { Std_ReturnType ret; /*SWS_E2E_10002*/ if(NULL_PTR == StatePtr) { ret = E2E_E_INPUTERR_NULL; } else { StatePtr->Status = E2E_P44STATUS_ERROR; /*SWS_E2E_00471*/ StatePtr->Counter = E2E_P44_MAX_COUNTER_VALUE; ret = E2E_E_OK; } return ret; } /* BEGIN_FUNCTION_HDR ******************************************************************************* * Function Name: E2E_P44MapStatusToSM * * Description: The function maps the check status of Profile 44 to a generic * check status, which can be used by E2E state machine check function. * The E2E Profile 44 delivers a more fine-granular status, but this * is not relevant for the E2E state machine. * * Inputs: CheckReturn : Return value of the E2E_P44Check function. * Status : Status determined by E2E_P44Check function * * Return value: Profile-independent status of the reception on one single Data in one cycle. * * Limitations: None ******************************************************************************* END_FUNCTION_HDR */ /*SWS_E2E_91032*/ FUNC(E2E_PCheckStatusType, E2E_CODE) E2E_P44MapStatusToSM ( Std_ReturnType CheckReturn, E2E_P44CheckStatusType Status ) { /*SWS_E2E_10003*/ E2E_PCheckStatusType CheckStatus = E2E_P_ERROR; if(E2E_E_OK == CheckReturn) { if((E2E_P44STATUS_OK == Status) || (E2E_P44STATUS_OKSOMELOST == Status)) { CheckStatus = E2E_P_OK; } else if(E2E_P44STATUS_REPEATED == Status) { CheckStatus = E2E_P_REPEATED; } else if(E2E_P44STATUS_NONEWDATA == Status) { CheckStatus = E2E_P_NONEWDATA; } else if(E2E_P44STATUS_WRONGSEQUENCE == Status) { CheckStatus = E2E_P_WRONGSEQUENCE; } else { CheckStatus = E2E_P_ERROR; } } return CheckStatus; } #define E2E_STOP_SEC_CODE #include "E2E_MemMap.h"