]> rtime.felk.cvut.cz Git - arc.git/blobdiff - diagnostic/Dcm/Dcm_Dsl.c
Dcm, Fixed problem in read / write memory functions (refactored) that made it inpossi...
[arc.git] / diagnostic / Dcm / Dcm_Dsl.c
index df0b4bea3ebecb8c479d330cc9a4798693ef2f56..caca1aa53cfb9d8363eec171013ab6ff3aa54dac 100644 (file)
  * for more details.\r
  * -------------------------------- Arctic Core ------------------------------*/\r
 \r
+/*\r
+ *  General requirements\r
+ */\r
+/** @req DCM030 */\r
+\r
+\r
 #include <string.h>\r
-#include "Mcu.h"\r
-#include "Dcm_Cfg.h"\r
-#include "Dcm_Cbk.h"\r
 #include "Dcm.h"\r
 #include "Dcm_Internal.h"\r
-#include "Det.h"\r
 #include "MemMap.h"\r
+#if defined(USE_COMM)\r
 #include "ComM_Dcm.h"\r
+#endif\r
 #include "PduR_Dcm.h"\r
 #include "ComStack_Types.h"\r
+#include "Cpu.h"\r
+//#define USE_DEBUG_PRINTF\r
 #include "debug.h"\r
 \r
-#define TIMER_DECREMENT(timer) \\r
-       if (timer > 0) { \\r
-               timer = timer - 1; \\r
-       } \\r
+#define DECREMENT(timer) { if (timer > 0){timer--;} }\r
+#define DCM_CONVERT_MS_TO_MAIN_CYCLES(x)  ((x)/DCM_MAIN_FUNCTION_PERIOD_TIME_MS)\r
 \r
-#define COUNT_DECREMENT(timer) \\r
-       if (timer > 0) { \\r
-               timer = timer - 1; \\r
-       } \\r
+\r
+#if (DCM_PAGEDBUFFER_ENABLED)\r
+#error "DCM_PAGEDBUFFER_ENABLED is set to STD_ON, this is not supported by the code."\r
+#endif\r
 \r
 /*\r
  * Type definitions.\r
  */\r
-typedef struct {\r
-       const Dcm_DslProtocolRxType *protocolRx;\r
-       const Dcm_DslMainConnectionType *mainConnection;\r
-       const Dcm_DslConnectionType *connection;\r
-       const Dcm_DslProtocolRowType *protocolRow;\r
-} DcmDsl_ProtocolConfigurationType;\r
-\r
-#define MAX_PARALLEL_PROTOCOLS_ALLOWED         1\r
+// #define MAX_PARALLEL_PROTOCOLS_ALLOWED              1\r
 \r
 typedef struct {\r
        boolean initRun;\r
-       const Dcm_DslProtocolRowType *preemptedProtocol;  // Points to the currently active protocol.\r
-       const Dcm_DslProtocolRowType *activeProtocol;  // Points to the currently active protocol.\r
-       Dcm_DslRunTimeProtocolParametersType\r
-                       protocolList[MAX_PARALLEL_PROTOCOLS_ALLOWED];\r
+       //boolean diagnosticRequestPending; // This is a "semaphore" because DSD and DCM can handle multiple/parallel request at the moment.\r
+       const Dcm_DslProtocolRowType *activeProtocol; // Points to the currently active protocol.\r
+//     const Dcm_DslProtocolRowType *preemptedProtocol; // Points to the currently active protocol - reserved for future use\r
+//     Dcm_DslRunTimeProtocolParametersType protocolList[MAX_PARALLEL_PROTOCOLS_ALLOWED];      // Reserved for future use\r
 } DcmDsl_RunTimeDataType;\r
 \r
-DcmDsl_RunTimeDataType DcmDslRunTimeData = {\r
+static DcmDsl_RunTimeDataType DcmDslRunTimeData = {\r
                .initRun = FALSE,\r
-               .preemptedProtocol = NULL,\r
                .activeProtocol = NULL\r
+//             .preemptedProtocol = NULL,\r
+//             .protocolList = {}\r
 };\r
 \r
-\r
-// ################# DUMMIES START #################\r
-\r
-/*\r
- * Local types\r
- */\r
-\r
-// Global service table, set by DSL used by DSD\r
-Dcm_DsdServiceTableType *DslCurrentServiceTable = NULL;\r
-\r
-void ComM_DCM_ActivateDiagnostic() {\r
-       ;\r
-}\r
-\r
-void ComM_DCM_InactivateDiagnostic() {\r
-       ;\r
-}\r
-\r
-void _DsdDslDataIndication(const PduInfoType *pduRxData,\r
-               const Dcm_DsdServiceTableType *protocolSIDTable,\r
-               Dcm_ProtocolAddrTypeType addrType,\r
-               PduIdType rxPduIdRef,\r
-               PduInfoType *pduTxData) {\r
-       DEBUG( DEBUG_MEDIUM, "_DsdDslDataIndication called!\n");\r
-\r
-       DEBUG( DEBUG_MEDIUM, "pduRxData->SduLength = %d\n", pduRxData->SduLength );\r
-       for (int i=0; i < pduRxData->SduLength; i++) {\r
-               DEBUG( DEBUG_MEDIUM, "Data[%d] = %02x\n", i, pduRxData->SduDataPtr[i] );\r
-       }\r
-\r
-       uint8 *p;\r
-\r
-       pduTxData->SduLength = 5;\r
-       p = pduTxData->SduDataPtr;\r
-\r
-\r
-       for (int i=0; i<pduTxData->SduLength; i++) {\r
-               *p = i;\r
-               p++;\r
-       }\r
-\r
-       // Simulate a diagnostic response.\r
-       DslDsdProcessingDone(rxPduIdRef, DSD_TX_RESPONSE_READY);\r
-\r
-}\r
-\r
 // ################# HELPER FUNCTIONS START #################\r
 \r
 //\r
 // This function reset/stars the session (S3) timer. See requirement\r
-// @DCM141 when that action should be taken.\r
+// DCM141 when that action should be taken.\r
 //\r
-void startS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime,\r
-               const Dcm_DslProtocolRowType *protocolRow ) {\r
+static inline void startS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime, const Dcm_DslProtocolRowType *protocolRow) {\r
        const Dcm_DslProtocolTimingRowType *timeParams;\r
        timeParams = protocolRow->DslProtocolTimeLimit;\r
-       runtime->S3ServerTimeoutCount =\r
-                       DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrS3Server);\r
+       runtime->S3ServerTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrS3Server);\r
 }\r
 \r
 // - - - - - - - - - - -\r
 \r
 //\r
 // This function reset/stars the session (S3) timer. See requirement\r
-// @DCM141 when that action should be taken.\r
+// DCM141 when that action should be taken.\r
 //\r
-void stopS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime) {\r
+static inline void stopS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime) {\r
        runtime->S3ServerTimeoutCount = 0;\r
 }\r
 \r
 // - - - - - - - - - - -\r
 \r
 //\r
-//     This function implements the requirement @DCM139 when\r
+//     This function implements the requirement DCM139 when\r
 //     transition from one session to another.\r
-//     qqq, strange observation: If S3 timeout we will not change security mode to\r
-//     locked and that is how I interper the requirement.\r
 //\r
-void changeDiagnosticSession( Dcm_DslRunTimeProtocolParametersType *runtime,\r
-               Dcm_SesCtrlType newSession) {\r
+static void changeDiagnosticSession(Dcm_DslRunTimeProtocolParametersType *runtime, Dcm_SesCtrlType newSession) {\r
+\r
+       /** @req DCM139 */\r
+\r
        switch (runtime->sessionControl) {\r
        case DCM_DEFAULT_SESSION: // "default".\r
-               if ( newSession != DCM_DEFAULT_SESSION ) { // "default"\r
-                       runtime->securityLevel = DCM_SEC_LEV_LOCKED; // "0x00".\r
-                       runtime->sessionControl = newSession;\r
-                       runtime->protocolStarted = FALSE;\r
-                       DcmDslRunTimeData.activeProtocol = NULL;\r
-               }\r
+               /* to set the dsp buffer to default*/\r
+               DspInit();\r
                break;\r
+\r
        case DCM_PROGRAMMING_SESSION:\r
        case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
        case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
        case DCM_ALL_SESSION_LEVEL:\r
                runtime->securityLevel = DCM_SEC_LEV_LOCKED; // "0x00".\r
+               break;\r
+\r
+       default:\r
+               DET_REPORTERROR(MODULE_ID_DCM, 0, DCM_CHANGE_DIAGNOSTIC_SESSION_ID, DCM_E_PARAM);\r
+               //DEBUG(DEBUG_MEDIUM, "Old session invalid");\r
+               break;\r
+       }\r
+\r
+       switch (newSession) {\r
+       case DCM_DEFAULT_SESSION: // "default".\r
+       case DCM_PROGRAMMING_SESSION:\r
+       case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
+       case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
+       case DCM_ALL_SESSION_LEVEL:\r
                runtime->sessionControl = newSession;\r
                break;\r
+\r
        default:\r
-               // qqq: Log this error.\r
+               DET_REPORTERROR(MODULE_ID_DCM, 0, DCM_CHANGE_DIAGNOSTIC_SESSION_ID, DCM_E_PARAM);\r
+               //DEBUG(DEBUG_MEDIUM, "New session invalid");\r
                break;\r
        }\r
 }\r
 \r
-\r
 // - - - - - - - - - - -\r
 \r
-void DslResetSessionTimeoutTimer() {\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
-       Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+void DslResetSessionTimeoutTimer(void) {\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
+       Dcm_DslRunTimeProtocolParametersType *runtime;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        runtime = activeProtocol->DslRunTimeProtocolParameters;\r
-       startS3SessionTimer(runtime, activeProtocol); // @DCM141\r
+       startS3SessionTimer(runtime, activeProtocol); /** @req DCM141 */\r
 }\r
 \r
 // - - - - - - - - - - -\r
 \r
-void DslGetCurrentServiceTable(const Dcm_DsdServiceTableType **currentServiceTable) {\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
+void DslGetCurrentServiceTable(const Dcm_DsdServiceTableType **currentServiceTable) { /** @req DCM195 */\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        if (activeProtocol != NULL) {\r
                *currentServiceTable = activeProtocol->DslProtocolSIDTable;\r
@@ -187,9 +151,10 @@ void DslGetCurrentServiceTable(const Dcm_DsdServiceTableType **currentServiceTab
 \r
 // - - - - - - - - - - -\r
 \r
-Std_ReturnType DslGetActiveProtocol(Dcm_ProtocolType *protocolId) {\r
+Std_ReturnType DslGetActiveProtocol(Dcm_ProtocolType *protocolId) { /** @req DCM340 */\r
        Std_ReturnType ret = E_NOT_OK;\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        if (activeProtocol != NULL) {\r
                *protocolId = activeProtocol->DslProtocolID;\r
@@ -200,9 +165,10 @@ Std_ReturnType DslGetActiveProtocol(Dcm_ProtocolType *protocolId) {
 \r
 // - - - - - - - - - - -\r
 \r
-void DslSetSecurityLevel(Dcm_SecLevelType secLevel) {\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
-       Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+void DslSetSecurityLevel(Dcm_SecLevelType secLevel) { /** @req DCM020 */\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
+       Dcm_DslRunTimeProtocolParametersType *runtime;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        runtime = activeProtocol->DslRunTimeProtocolParameters;\r
        runtime->securityLevel = secLevel;\r
@@ -210,10 +176,11 @@ void DslSetSecurityLevel(Dcm_SecLevelType secLevel) {
 \r
 // - - - - - - - - - - -\r
 \r
-Std_ReturnType DslGetSecurityLevel(Dcm_SecLevelType *secLevel) {\r
+Std_ReturnType DslGetSecurityLevel(Dcm_SecLevelType *secLevel) {  /** @req DCM020 *//** @req DCM338 */\r
        Std_ReturnType ret = E_NOT_OK;\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        if (activeProtocol != NULL) {\r
                runtime = activeProtocol->DslRunTimeProtocolParameters;\r
@@ -225,9 +192,10 @@ Std_ReturnType DslGetSecurityLevel(Dcm_SecLevelType *secLevel) {
 \r
 // - - - - - - - - - - -\r
 \r
-void DslSetSesCtrlType(Dcm_SesCtrlType sesCtrl) {\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
+void DslSetSesCtrlType(Dcm_SesCtrlType sesCtrl) {  /** @req DCM022 */\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        if (activeProtocol != NULL) {\r
                runtime = activeProtocol->DslRunTimeProtocolParameters;\r
@@ -240,10 +208,11 @@ void DslSetSesCtrlType(Dcm_SesCtrlType sesCtrl) {
 \r
 // - - - - - - - - - - -\r
 \r
-Std_ReturnType DslGetSesCtrlType(Dcm_SesCtrlType *sesCtrlType) {\r
+Std_ReturnType DslGetSesCtrlType(Dcm_SesCtrlType *sesCtrlType) { /** @req DCM022 *//** @req DCM339 */\r
        Std_ReturnType ret = E_NOT_OK;\r
-       const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
+       const Dcm_DslProtocolRowType *activeProtocol;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+\r
        activeProtocol = DcmDslRunTimeData.activeProtocol;\r
        if (activeProtocol != NULL) {\r
                runtime = activeProtocol->DslRunTimeProtocolParameters;\r
@@ -255,7 +224,7 @@ Std_ReturnType DslGetSesCtrlType(Dcm_SesCtrlType *sesCtrlType) {
 \r
 // - - - - - - - - - - -\r
 \r
-boolean findParentConfigurationLeafs(PduIdType dcmRxPduId,\r
+static boolean findRxPduIdParentConfigurationLeafs(PduIdType dcmRxPduId,\r
                const Dcm_DslProtocolRxType **protocolRx,\r
                const Dcm_DslMainConnectionType **mainConnection,\r
                const Dcm_DslConnectionType **connection,\r
@@ -263,9 +232,8 @@ boolean findParentConfigurationLeafs(PduIdType dcmRxPduId,
                Dcm_DslRunTimeProtocolParametersType **runtime) {\r
 \r
        boolean ret = FALSE;\r
-       if (dcmRxPduId < DCM_DSL_BUFFER_LIST_LENGTH) {\r
-               *protocolRx\r
-                               = &DCM_Config.Dsl->DslProtocol->DslProtocolRxGlobalList[dcmRxPduId];\r
+       if (dcmRxPduId < DCM_DSL_RX_PDU_ID_LIST_LENGTH) {\r
+               *protocolRx = &DCM_Config.Dsl->DslProtocol->DslProtocolRxGlobalList[dcmRxPduId];\r
                *mainConnection = (*protocolRx)->DslMainConnectionParent;\r
                *connection = (*mainConnection)->DslConnectionParent;\r
                *protocolRow = (*connection)->DslProtocolRow;\r
@@ -277,8 +245,29 @@ boolean findParentConfigurationLeafs(PduIdType dcmRxPduId,
 \r
 // - - - - - - - - - - -\r
 \r
-void releaseExternalRxTxBuffers( const Dcm_DslProtocolRowType *protocolRow,\r
-               Dcm_DslRunTimeProtocolParametersType *runtime ) {\r
+static boolean findTxPduIdParentConfigurationLeafs(PduIdType dcmTxPduId,\r
+               const Dcm_DslProtocolTxType **protocolTx,\r
+               const Dcm_DslMainConnectionType **mainConnection,\r
+               const Dcm_DslConnectionType **connection,\r
+               const Dcm_DslProtocolRowType **protocolRow,\r
+               Dcm_DslRunTimeProtocolParametersType **runtime) {\r
+\r
+       boolean ret = FALSE;\r
+       if (dcmTxPduId < DCM_DSL_TX_PDU_ID_LIST_LENGTH) {\r
+               *protocolTx = &DCM_Config.Dsl->DslProtocol->DslProtocolTxGlobalList[dcmTxPduId];\r
+               *mainConnection = (*protocolTx)->DslMainConnectionParent;\r
+               *connection = (*mainConnection)->DslConnectionParent;\r
+               *protocolRow = (*connection)->DslProtocolRow;\r
+               *runtime = (*protocolRow)->DslRunTimeProtocolParameters;\r
+               ret = TRUE;\r
+       }\r
+       return ret;\r
+}\r
+\r
+// - - - - - - - - - - -\r
+\r
+static inline void releaseExternalRxTxBuffers(const Dcm_DslProtocolRowType *protocolRow,\r
+               Dcm_DslRunTimeProtocolParametersType *runtime) {\r
 \r
        protocolRow->DslProtocolTxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
        protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
@@ -289,19 +278,19 @@ void releaseExternalRxTxBuffers( const Dcm_DslProtocolRowType *protocolRow,
 // - - - - - - - - - - -\r
 \r
 \r
-void releaseExternalRxTxBuffersHelper( PduIdType rxPduIdRef ) {\r
+static inline void releaseExternalRxTxBuffersHelper(PduIdType rxPduIdRef) {\r
        const Dcm_DslProtocolRxType *protocolRx = NULL;\r
        const Dcm_DslMainConnectionType *mainConnection = NULL;\r
        const Dcm_DslConnectionType *connection = NULL;\r
        const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
 \r
-       if (findParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
-               releaseExternalRxTxBuffers( protocolRow, runtime );\r
+       if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
+               releaseExternalRxTxBuffers(protocolRow, runtime);\r
        }\r
 }\r
 \r
+// - - - - - - - - - - -\r
 \r
 /*\r
  *  This function is called from the DSD module to the DSL when\r
@@ -315,62 +304,74 @@ void DslDsdProcessingDone(PduIdType rxPduIdRef, DsdProcessingDoneResultType resp
        const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
 \r
-       DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone!\n");\r
-       if (findParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
-               imask_t state = McuE_EnterCriticalSection();\r
-               switch ( responseResult ) {\r
+//     DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone rxPduIdRef=%d\n", rxPduIdRef);\r
+\r
+       if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
+           imask_t state;\r
+           Irq_Save(state);\r
+               switch (responseResult) {\r
                case DSD_TX_RESPONSE_READY:\r
-                       runtime->externalTxBufferStatus = DSD_PENDING_RESPONSE_SIGNALED;\r
+                       runtime->externalTxBufferStatus = DSD_PENDING_RESPONSE_SIGNALED; /** @req DCM114 */\r
                        break;\r
-               case DSD_TX_RESPONSE_SUPPRESSED:\r
-                       releaseExternalRxTxBuffersHelper( rxPduIdRef );\r
+               case DSD_TX_RESPONSE_SUPPRESSED: /** @req DCM238 */\r
+                       //DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone called with DSD_TX_RESPONSE_SUPPRESSED.\n");\r
+                       releaseExternalRxTxBuffersHelper(rxPduIdRef);\r
                        break;\r
                default:\r
-                       DEBUG( DEBUG_MEDIUM, "Unknown response result from DslDsdProcessingDone!\n");\r
+                       //DEBUG( DEBUG_MEDIUM, "Unknown response result from DslDsdProcessingDone!\n");\r
                        break;\r
                }\r
-               McuE_ExitCriticalSection(state);\r
+           Irq_Restore(state);\r
        }\r
 }\r
 \r
+// - - - - - - - - - - -\r
+\r
 /*\r
  *     This function preparing transmission of response\r
  *     pending message to tester.\r
  */\r
-void sendResponse(const Dcm_DslProtocolRowType *protocol, Dcm_NegativeResponseCodeType responseCode) {\r
+static void sendResponse(const Dcm_DslProtocolRowType *protocol,\r
+               Dcm_NegativeResponseCodeType responseCode) {\r
+       //Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+       const Dcm_DslProtocolRxType *protocolRx = NULL;\r
+       const Dcm_DslMainConnectionType *mainConnection = NULL;\r
+       const Dcm_DslConnectionType *connection = NULL;\r
+       const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
-       const uint32 txPduId =\r
-                       protocol->DslConnection->DslMainConnection->DslProtocolTx->PduR_DcmDslTxPduId;\r
-       runtime = protocol->DslRunTimeProtocolParameters;\r
-       imask_t state = McuE_EnterCriticalSection();\r
-       if (runtime->localTxBuffer.status == NOT_IN_USE) {\r
-               runtime->localTxBuffer.status = PROVIDED_TO_DSD; // For readability (yes, it will later be overwritten).\r
-               runtime->localTxBuffer.buffer[0] = SID_NEGATIVE_RESPONSE;\r
-               runtime->localTxBuffer.buffer[1]\r
-                               = protocol->DslProtocolRxBufferID->pduInfo.SduDataPtr[2];\r
-               runtime->localTxBuffer.buffer[2] = responseCode; // 0x78.\r
-               runtime->localTxBuffer.PduInfo.SduDataPtr\r
-                               = runtime->localTxBuffer.buffer;\r
-               runtime->localTxBuffer.PduInfo.SduLength = 3;\r
-               runtime->localTxBuffer.status = DCM_TRANSMIT_SIGNALED; // In the DslProvideTxBuffer 'callback' this state signals it is the local buffer we are intressted in sending.\r
-               PduR_DcmTransmit(txPduId, &(runtime->localTxBuffer.PduInfo));\r
+       Std_ReturnType transmitResult;\r
+    imask_t state;\r
+\r
+    Irq_Save(state);\r
+       /** @req DCM119 */\r
+       if (findRxPduIdParentConfigurationLeafs(protocol->DslRunTimeProtocolParameters->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
+               if (runtime->localTxBuffer.status == NOT_IN_USE) {\r
+                       runtime->localTxBuffer.status = PROVIDED_TO_DSD;\r
+                       runtime->localTxBuffer.buffer[0] = SID_NEGATIVE_RESPONSE;\r
+                       runtime->localTxBuffer.buffer[1] = protocol->DslProtocolRxBufferID->pduInfo.SduDataPtr[2];\r
+                       runtime->localTxBuffer.buffer[2] = responseCode;\r
+                       runtime->localTxBuffer.PduInfo.SduDataPtr = runtime->localTxBuffer.buffer;\r
+                       runtime->localTxBuffer.PduInfo.SduLength = 3;\r
+                       runtime->localTxBuffer.status = DCM_TRANSMIT_SIGNALED; // In the DslProvideTxBuffer 'callback' this state signals it is the local buffer we are interested in sending.\r
+                       transmitResult = PduR_DcmTransmit(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, &(runtime->localTxBuffer.PduInfo));/** @req DCM115.Partially */ /* The P2ServerMin has not been implemented. */\r
+                       if (transmitResult != E_OK) {\r
+                               // TODO: What to do here?\r
+                       }\r
+               }\r
        }\r
-       McuE_ExitCriticalSection(state);\r
+    Irq_Restore(state);\r
 }\r
 \r
 // - - - - - - - - - - -\r
 \r
-Std_ReturnType StartProtocolHelper(Dcm_ProtocolType protocolId) {\r
+static Std_ReturnType StartProtocolHelper(Dcm_ProtocolType protocolId) {\r
        Std_ReturnType ret = E_NOT_OK;\r
        uint16 i;\r
 \r
        for (i = 0; !DCM_Config.Dsl->DslCallbackDCMRequestService[i].Arc_EOL; i++) {\r
-               if (DCM_Config.Dsl->DslCallbackDCMRequestService[i].StartProtocol\r
-                               != NULL) {\r
-                       ret = DCM_Config.Dsl->DslCallbackDCMRequestService[i].\r
-                                       StartProtocol(protocolId);\r
-                       if (ret != E_OK) { // qqq: eqvivalent to DCM_E_OK?\r
+               if (DCM_Config.Dsl->DslCallbackDCMRequestService[i].StartProtocol != NULL) {\r
+                       ret = DCM_Config.Dsl->DslCallbackDCMRequestService[i]. StartProtocol(protocolId);\r
+                       if (ret != E_OK) {\r
                                break;\r
                        }\r
                }\r
@@ -380,11 +381,10 @@ Std_ReturnType StartProtocolHelper(Dcm_ProtocolType protocolId) {
 \r
 // - - - - - - - - - - -\r
 \r
-boolean isTesterPresentCommand(const PduInfoType *rxPdu) {\r
+static boolean isTesterPresentCommand(const PduInfoType *rxPdu) {\r
        boolean ret = FALSE;\r
-       if ((rxPdu->SduDataPtr[0] == SID_TESTER_PRESENT) && (rxPdu->SduDataPtr[1]\r
-                       & SUPPRESS_POS_RESP_BIT)) {\r
-               return TRUE;\r
+       if ((rxPdu->SduDataPtr[0] == SID_TESTER_PRESENT) && (rxPdu->SduDataPtr[1] & SUPPRESS_POS_RESP_BIT)) {\r
+               ret = TRUE;\r
        }\r
        return ret;\r
 }\r
@@ -393,93 +393,148 @@ boolean isTesterPresentCommand(const PduInfoType *rxPdu) {
 //     Implements 'void Dcm_Init(void)' for DSL.\r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 void DslInit(void) {\r
-       const Dcm_DslProtocolRowType *listEntry = NULL;\r
+       const Dcm_DslProtocolRowType *listEntry;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
 \r
        listEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
        while (listEntry->Arc_EOL == FALSE) {\r
                runtime = listEntry->DslRunTimeProtocolParameters;\r
-               runtime->externalRxBufferStatus = IDLE;\r
-               runtime->externalTxBufferStatus = IDLE;\r
-               runtime->localRxBuffer.status = IDLE;\r
-               runtime->localTxBuffer.status = IDLE;\r
-               runtime->securityLevel = DCM_SEC_LEV_LOCKED;\r
-               runtime->sessionControl = DCM_DEFAULT_SESSION;\r
-               listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status\r
-                               = BUFFER_AVAILABLE;\r
-               listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status\r
-                               = BUFFER_AVAILABLE;\r
+               runtime->externalRxBufferStatus = DCM_IDLE;\r
+               runtime->externalTxBufferStatus = DCM_IDLE;\r
+               runtime->localRxBuffer.status = DCM_IDLE;\r
+               runtime->localTxBuffer.status = DCM_IDLE;\r
+               runtime->securityLevel = DCM_SEC_LEV_LOCKED; /** @req DCM033 */\r
+               runtime->sessionControl = DCM_DEFAULT_SESSION; /** @req DCM034 */\r
+               listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
+               listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
                listEntry++;\r
        };\r
+       //DcmDslRunTimeData.diagnosticRequestPending = FALSE;\r
        DcmDslRunTimeData.initRun = TRUE;\r
 }\r
 \r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
-//     Implements 'void Dcm_MainFunction(void)' for DSL.\r
+//     Implements 'void DslInternal_ResponseOnOneDataByPeriodicId(uint8 PericodID)' for simulator a periodic did data.\r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 \r
+Std_ReturnType DslInternal_ResponseOnOneDataByPeriodicId(uint8 PericodID)\r
+{\r
+       Std_ReturnType ret = E_NOT_OK;\r
+       const Dcm_DslProtocolRowType *protocolRowEntry;\r
+       Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
+    PduInfoType  *pPeriodData;\r
+       protocolRowEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
+       while (protocolRowEntry->Arc_EOL == FALSE) \r
+       {\r
+       runtime = protocolRowEntry->DslRunTimeProtocolParameters;\r
+        if(runtime != NULL)    // find the runtime\r
+        {\r
+               if( BUFREQ_OK == DslProvideRxBufferToPdur(runtime->diagReqestRxPduId, 3, (const PduInfoType **)&pPeriodData)){\r
+                pPeriodData->SduDataPtr[0] = 0x2a;\r
+                pPeriodData->SduDataPtr[1] = 0;\r
+                pPeriodData->SduDataPtr[2] = PericodID;\r
+                pPeriodData->SduLength = 3;\r
+                DslRxIndicationFromPduR(0, NTFRSLT_OK);\r
+                ret = E_OK;\r
+                       break;\r
+               }\r
+               else {\r
+                       ret = E_NOT_OK;\r
+               }\r
+\r
+               }\r
+       protocolRowEntry++;\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+\r
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
+//     Implements 'void Dcm_MainFunction(void)' for DSL.\r
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 void DslMain(void) {\r
-       const Dcm_DslProtocolRowType *protocolRowEntry = NULL;\r
+       const Dcm_DslProtocolRowType *protocolRowEntry;\r
        const Dcm_DslProtocolTimingRowType *timeParams = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
 \r
        protocolRowEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
        while (protocolRowEntry->Arc_EOL == FALSE) {\r
                runtime = protocolRowEntry->DslRunTimeProtocolParameters;\r
-               if ( runtime != NULL ) {\r
+               if (runtime != NULL) {\r
+                       // #### HANDLE THE TESTER PRESENT PRESENCE ####\r
                        if (runtime->sessionControl != DCM_DEFAULT_SESSION) { // Timeout if tester present is lost.\r
-                               TIMER_DECREMENT(runtime->S3ServerTimeoutCount);\r
-                               if ( runtime->S3ServerTimeoutCount == 0 ) {\r
-                                       changeDiagnosticSession( runtime, DCM_DEFAULT_SESSION );\r
+                               DECREMENT(runtime->S3ServerTimeoutCount);\r
+                               if (runtime->S3ServerTimeoutCount == 0) {\r
+                                       changeDiagnosticSession(runtime, DCM_DEFAULT_SESSION); /** @req DCM140 */\r
                                }\r
                        }\r
                        switch (runtime->externalTxBufferStatus) { // #### TX buffer state. ####\r
                        case NOT_IN_USE:\r
                                //DEBUG( DEBUG_MEDIUM, "state NOT_IN_USE!\n");\r
                                break;\r
-                       case PROVIDED_TO_DSD:\r
-                       {\r
-                               TIMER_DECREMENT(runtime->stateTimeoutCount);\r
-                               DEBUG( DEBUG_MEDIUM, "state PROVIDED_TO_DSD!\n");\r
+                       case PROVIDED_TO_DSD: {\r
+                               DECREMENT(runtime->stateTimeoutCount);\r
                                if (runtime->stateTimeoutCount == 0) {\r
                                        timeParams = protocolRowEntry->DslProtocolTimeLimit;\r
-                                       runtime->stateTimeoutCount =\r
-                                                       DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* Reinitiate timer, see 9.2.2. */\r
-                                       if ( DCM_Config.Dsl->DslDiagResp->DslDiagRespForceRespPendEn == TRUE ) {\r
-                                               if ( runtime->responsePendingCount != 0 ) {\r
-                                                       DEBUG( DEBUG_MEDIUM, "No response withing timeout, sending response pending!\n");\r
-                                                       sendResponse(protocolRowEntry, DCM_E_RESPONSEPENDING);\r
-                                                       COUNT_DECREMENT( runtime->responsePendingCount );\r
+                                       runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* Reinitiate timer, see 9.2.2. */\r
+                                       if (DCM_Config.Dsl->DslDiagResp != NULL) {\r
+                                               if (DCM_Config.Dsl->DslDiagResp->DslDiagRespForceRespPendEn == TRUE) {\r
+                                                       if (runtime->responsePendingCount != 0) {\r
+                                                               sendResponse(protocolRowEntry, DCM_E_RESPONSEPENDING);  /** @req DCM024 */\r
+                                                               DECREMENT( runtime->responsePendingCount );\r
+                                                       } else {\r
+                                                               sendResponse(protocolRowEntry, DCM_E_GENERALREJECT); /** @req DCM120 */\r
+                                                               releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
+                                                       }\r
                                                } else {\r
-                                                       DEBUG( DEBUG_MEDIUM, "Sent all response pending, now sending general reject!\n");\r
+                               //                      DEBUG( DEBUG_MEDIUM, "Not configured to send response pending, now sending general reject!\n");\r
                                                        sendResponse(protocolRowEntry, DCM_E_GENERALREJECT);\r
                                                        releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
                                                }\r
-                                       } else {\r
-                                               DEBUG( DEBUG_MEDIUM, "Not configured to send response pending, now sending general reject!\n");\r
-                                               sendResponse(protocolRowEntry, DCM_E_GENERALREJECT);\r
-                                               releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
                                        }\r
                                }\r
                                break;\r
                        }\r
-                       case DSD_PENDING_RESPONSE_SIGNALED: // The DSD has signaled to DSL that the diagnostic response is available in the Tx buffer.\r
+                       case DSD_PENDING_RESPONSE_SIGNALED:\r
+                               // The DSD has signaled to DSL that the diagnostic response is available in the Tx buffer.\r
                                // Make sure that response pending or general reject have not been issued,\r
                                // if so we can not transmit to PduR because we would not know from where\r
                                // the Tx confirmation resides later.\r
-                               DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
-                               if (runtime->localTxBuffer.status == NOT_IN_USE) {\r
-                                       const uint32 txPduId =\r
-                                                       protocolRowEntry->DslConnection->DslMainConnection->DslProtocolTx->PduR_DcmDslTxPduId;\r
-                                       runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
-                                       PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 **//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
+                       //      DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
+                               if (runtime->localTxBuffer.status == NOT_IN_USE) { // Make sure that no TxConfirm could be sent by the local buffer and mixed up with this transmission.\r
+                                       const Dcm_DslProtocolRxType *protocolRx = NULL;\r
+                                       const Dcm_DslMainConnectionType *mainConnection = NULL;\r
+                                       const Dcm_DslConnectionType *connection = NULL;\r
+                                       const Dcm_DslProtocolRowType *protocolRow = NULL;\r
+                                       Std_ReturnType transmitResult;\r
+\r
+                                       if (findRxPduIdParentConfigurationLeafs(runtime->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
+                                               const PduIdType txPduId = mainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
+                                       //      DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DCM_TRANSMIT_SIGNALED.\n" );\r
+                                               runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
+                                               transmitResult = PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
+                                               if (transmitResult != E_OK) {\r
+                                                       // TODO: What to do here?\r
+                                               }\r
+                                       } else {\r
+                                       //      DEBUG( DEBUG_MEDIUM, "***** WARNING, THIS IS UNEXPECTED !!! ********.\n" );\r
+                                               const PduIdType txPduId = protocolRowEntry->DslConnection->DslMainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
+                                       //      DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DSD_PENDING_RESPONSE_SIGNALED.\n", txPduId);\r
+                                               runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
+                                       //      DEBUG( DEBUG_MEDIUM, "Calling PduR_DcmTransmit with txPduId = %d from DslMain\n", txPduId);\r
+                                               transmitResult = PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
+                                               if (transmitResult != E_OK) {\r
+                                                       // TODO: What to do here?\r
+                                               }\r
+                                       }\r
                                }\r
                                break;\r
                        case DCM_TRANSMIT_SIGNALED:\r
-                               DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
+                               //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
                                break;\r
                        case PROVIDED_TO_PDUR: // The valid data is being transmitted by TP-layer.\r
-                               DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
+                               //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
                                break;\r
                        default:\r
                                break;\r
@@ -497,65 +552,57 @@ void DslMain(void) {
 //  received a FF or a single frame and needs to obtain a buffer from the\r
 //  receiver so that received data can be forwarded.\r
 \r
-BufReq_ReturnType DslProvideRxBufferToPdur(PduIdType dcmRxPduId,\r
-               PduLengthType tpSduLength, const PduInfoType **pduInfoPtr) {\r
+BufReq_ReturnType DslProvideRxBufferToPdur(PduIdType dcmRxPduId, PduLengthType tpSduLength, const PduInfoType **pduInfoPtr) {\r
        BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
        const Dcm_DslProtocolRxType *protocolRx = NULL;\r
        const Dcm_DslMainConnectionType *mainConnection = NULL;\r
        const Dcm_DslConnectionType *connection = NULL;\r
        const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
-\r
-  DEBUG( DEBUG_MEDIUM, "DslProvideRxBufferToPdur called!\n");\r
-       imask_t state = McuE_EnterCriticalSection();\r
-       if (findParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
-               const Dcm_DslBufferType *externalRxBuffer =\r
-                               protocolRow->DslProtocolRxBufferID;\r
-               if (externalRxBuffer->pduInfo.SduLength >= tpSduLength) { // First validate that we have a chance receiving the chunk of data.\r
-                       if ((runtime->externalRxBufferStatus == NOT_IN_USE)\r
-                                       && (externalRxBuffer->externalBufferRuntimeData->status\r
-                                                       == BUFFER_AVAILABLE)) {\r
-        DEBUG( DEBUG_MEDIUM, "External buffer available!\n");\r
+    imask_t state;\r
+//     DEBUG( DEBUG_MEDIUM, "DslProvideRxBufferToPdur(dcmRxPduId=%d) called!\n", dcmRxPduId);  \r
+    Irq_Save(state);\r
+       if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
+               const Dcm_DslBufferType *externalRxBuffer = protocolRow->DslProtocolRxBufferID;\r
+               if (externalRxBuffer->pduInfo.SduLength >= tpSduLength) { /** @req DCM443 */\r
+                       if ((runtime->externalRxBufferStatus == NOT_IN_USE) && (externalRxBuffer->externalBufferRuntimeData->status == BUFFER_AVAILABLE)) {\r
+                       //      DEBUG( DEBUG_MEDIUM, "External buffer available!\n");\r
                                // ### EXTERNAL BUFFER IS AVAILABLE; GRAB IT AND REMEBER THAT WE OWN IT! ###\r
-                               externalRxBuffer->externalBufferRuntimeData->status\r
-                                               = BUFFER_BUSY;\r
+                               externalRxBuffer->externalBufferRuntimeData->status = BUFFER_BUSY;\r
                                runtime->diagnosticRequestFromTester.SduDataPtr = externalRxBuffer->pduInfo.SduDataPtr;\r
                                runtime->diagnosticRequestFromTester.SduLength = tpSduLength;\r
-                               //*pduInfoPtr = &(externalRxBuffer->pduInfo);\r
                                *pduInfoPtr = &(runtime->diagnosticRequestFromTester);\r
-                               runtime->externalRxBufferStatus = PROVIDED_TO_PDUR;\r
+                               runtime->externalRxBufferStatus = PROVIDED_TO_PDUR; /** @req DCM342 */\r
                                ret = BUFREQ_OK;\r
                        } else {\r
-        DEBUG( DEBUG_MEDIUM, "Local buffer available!\n");\r
                                if (runtime->externalRxBufferStatus == PROVIDED_TO_DSD) {\r
                                        // ### EXTERNAL BUFFER IS IN USE BY THE DSD, TRY TO USE LOCAL BUFFER! ###\r
                                        if (runtime->localRxBuffer.status == NOT_IN_USE) {\r
-                                               if (tpSduLength < LOCAL_BUFFER_LENGTH) {\r
+                                               if (tpSduLength < DCM_DSL_LOCAL_BUFFER_LENGTH) {\r
                                                        runtime->localRxBuffer.status = PROVIDED_TO_PDUR;\r
-                                                       runtime->localRxBuffer.PduInfo.SduDataPtr\r
-                                                                       = runtime->localRxBuffer.buffer;\r
-                                                       runtime->localRxBuffer.PduInfo.SduLength\r
-                                                                       = tpSduLength;\r
+                                                       runtime->localRxBuffer.PduInfo.SduDataPtr = runtime->localRxBuffer.buffer;\r
+                                                       runtime->localRxBuffer.PduInfo.SduLength = tpSduLength;\r
                                                        *pduInfoPtr = &(runtime->localRxBuffer.PduInfo);\r
                                                        ret = BUFREQ_OK;\r
+                                               } else {\r
+                                                       ret = BUFREQ_BUSY;\r
                                                }\r
                                        }\r
                                } else {\r
                                        // The buffer is in use by the PduR, we can not help this because then\r
                                        // we would have two different Rx-indications with same PduId but we\r
                                        // will not know which buffer the indication should free.\r
-                                       ret = BUFREQ_BUSY;\r
+                                       ret = BUFREQ_BUSY; /** @req DCM445 */\r
                                }\r
                        }\r
                } else {\r
-                       ret = BUFREQ_OVFL; // Required size is too big.\r
+                       ret = BUFREQ_OVFL; /** @req DCM444 */\r
                }\r
-               if ( ret == BUFREQ_OK ) {\r
-                       stopS3SessionTimer( runtime );  /** req: DCM141 **/\r
+               if (ret == BUFREQ_OK) {\r
+                       stopS3SessionTimer(runtime); /** @req DCM141 */\r
                }\r
        }\r
-       McuE_ExitCriticalSection(state);\r
+    Irq_Restore(state);\r
        return ret;\r
 }\r
 \r
@@ -564,7 +611,7 @@ BufReq_ReturnType DslProvideRxBufferToPdur(PduIdType dcmRxPduId,
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 //     This function is called called by the PduR typically when CanTp has\r
 //     received the diagnostic request, copied it to the provided buffer and need to indicate\r
-//     this to the DCM (DSL) module via propritary API.\r
+//     this to the DCM (DSL) module via proprietary API.\r
 \r
 void DslRxIndicationFromPduR(PduIdType dcmRxPduId, NotifResultType result) {\r
        const Dcm_DslProtocolRxType *protocolRx = NULL;\r
@@ -575,74 +622,82 @@ void DslRxIndicationFromPduR(PduIdType dcmRxPduId, NotifResultType result) {
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
        Std_ReturnType higherLayerResp;\r
        imask_t state;\r
-       \r
-       // qqq: handle the actual result code.\r
-       if (findParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
+\r
+       /** @req DCM345, this needs to be verified when connection to CanIf works. */\r
+\r
+       if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
                timeParams = protocolRow->DslProtocolTimeLimit;\r
                // We need to find out in what buffer we can find our Rx data (it can\r
                // be either in the normal RX-buffer or the 'extra' buffer for implementing\r
                // the Concurrent "Test Present" functionality.\r
+           Irq_Save(state);\r
                if (runtime->externalRxBufferStatus == PROVIDED_TO_PDUR) {\r
-                       if (isTesterPresentCommand(\r
-                                       &(protocolRow->DslProtocolRxBufferID->pduInfo))) {\r
-                               state = McuE_EnterCriticalSection();\r
-                               startS3SessionTimer( runtime, protocolRow ); /** @req DCM141 **/ /** @req DCM112 **//** @req DCM113 **/\r
-                               runtime->externalRxBufferStatus = NOT_IN_USE;\r
-                               McuE_ExitCriticalSection(state);\r
-                       } else {\r
-                               if (runtime->protocolStarted == FALSE) {\r
-                                       higherLayerResp = StartProtocolHelper(\r
-                                                       protocolRow->DslProtocolID);\r
-                                       if (higherLayerResp == E_OK) {\r
-                                               runtime->protocolStarted = TRUE;\r
-                                               DcmDslRunTimeData.activeProtocol = protocolRow;\r
+                       if ( result == NTFRSLT_OK ) { /** @req DCM111 */\r
+                               if (isTesterPresentCommand(&(protocolRow->DslProtocolRxBufferID->pduInfo))) {\r
+                                       startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
+                                       runtime->externalRxBufferStatus = NOT_IN_USE;\r
+                                       protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
+                               } else {\r
+                                       if (runtime->protocolStarted == FALSE) {\r
+                                               higherLayerResp = StartProtocolHelper(protocolRow->DslProtocolID); /** @req DCM036 */\r
+                                               if (higherLayerResp == E_OK) {\r
+                                                       runtime->protocolStarted = TRUE;\r
+                                                       DcmDslRunTimeData.activeProtocol = protocolRow;\r
+                                               }\r
                                        }\r
-                               }\r
-                               if (runtime->protocolStarted == TRUE) {\r
-                                       if (runtime->diagnosticActiveComM == FALSE) {\r
-                                               ComM_DCM_ActivateDiagnostic(); /* @DCM163 */\r
-                                               runtime->diagnosticActiveComM = TRUE;\r
+                                       if (runtime->protocolStarted == TRUE) {\r
+                                               if (runtime->diagnosticActiveComM == FALSE) {\r
+#if defined(USE_COMM)\r
+                                                       ComM_DCM_ActiveDiagnostic(); /** @req DCM163 */\r
+#endif\r
+                                                       runtime->diagnosticActiveComM = TRUE;\r
+                                               }\r
+                                               timeParams = protocolRow->DslProtocolTimeLimit;\r
+                                               runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* See 9.2.2. */\r
+                                               runtime->externalRxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
+                                               if (runtime->externalTxBufferStatus == NOT_IN_USE) {\r
+                                               //      DEBUG( DEBUG_MEDIUM, "External Tx buffer available, we can pass it to DSD.\n");\r
+                                               } else {\r
+                                                       //DEBUG( DEBUG_MEDIUM, "External buffer not available, a response is being transmitted?\n");\r
+                                               }\r
+                                               runtime->externalTxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
+                                               runtime->responsePendingCount = DCM_Config.Dsl->DslDiagResp->DslDiagRespMaxNumRespPend;\r
+                                               runtime->diagnosticResponseFromDsd.SduDataPtr = protocolRow->DslProtocolTxBufferID->pduInfo.SduDataPtr;\r
+                                               runtime->diagnosticResponseFromDsd.SduLength = protocolRow->DslProtocolTxBufferID->pduInfo.SduLength;\r
+                                       //      DEBUG( DEBUG_MEDIUM, "DsdDslDataIndication(DcmDslProtocolTxPduId=%d, dcmRxPduId=%d)\n", mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, dcmRxPduId);\r
+                                               runtime->diagReqestRxPduId = dcmRxPduId;\r
+                                       //      DEBUG(DEBUG_MEDIUM,"\n\n runtime->diagnosticRequestFromTester.SduDataPtr[2]  %x\n\n ",runtime->diagnosticRequestFromTester.SduDataPtr[2]);\r
+                                               DsdDslDataIndication(  // qqq: We are inside a critical section.\r
+                                                               &(runtime->diagnosticRequestFromTester),\r
+                                                               protocolRow->DslProtocolSIDTable,       /** @req DCM035 */\r
+                                                               protocolRx->DslProtocolAddrType,\r
+                                                               mainConnection->DslProtocolTx->DcmDslProtocolTxPduId,\r
+                                                               &(runtime->diagnosticResponseFromDsd),\r
+                                                               dcmRxPduId);\r
                                        }\r
-                                       state = McuE_EnterCriticalSection();\r
-                                       runtime->stateTimeoutCount =\r
-                                                       DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* See 9.2.2. */\r
-                                       runtime->externalRxBufferStatus = PROVIDED_TO_DSD;\r
-                                       runtime->externalTxBufferStatus = PROVIDED_TO_DSD; // Used in main.\r
-                                       timeParams = protocolRow->DslProtocolTimeLimit;\r
-                                       runtime->stateTimeoutCount =\r
-                                                       DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* Reinitiate timer, see 9.2.2. */          \r
-                                       McuE_ExitCriticalSection(state);\r
-                                       runtime->diagnosticResponseFromDsd.SduDataPtr = protocolRow->DslProtocolTxBufferID->pduInfo.SduDataPtr;\r
-                                       runtime->diagnosticResponseFromDsd.SduLength = protocolRow->DslProtocolTxBufferID->pduInfo.SduLength;\r
-                                       _DsdDslDataIndication(\r
-                                                       //&protocolRow->DslProtocolRxBufferID->pduInfo,\r
-                                                       &(runtime->diagnosticRequestFromTester),\r
-                                                       protocolRow->DslProtocolSIDTable,\r
-                                                       protocolRx->DslProtocolAddrType,\r
-                                                       mainConnection->DslProtocolTx->PduR_DcmDslTxPduId,\r
-                                                       //&protocolRow->DslProtocolTxBufferID->pduInfo);\r
-                                                       &(runtime->diagnosticResponseFromDsd));\r
-                                    \r
                                }\r
+                       } else { /** @req DCM344 */\r
+                               // The indication was not equal to NTFRSLT_OK, release the resources and no forward to DSD.\r
+                               runtime->externalRxBufferStatus = NOT_IN_USE;\r
+                               protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
                        }\r
                } else {\r
                        // It is the local buffer that was provided to the PduR, that buffer\r
                        // is only used for tester present reception in parallel to diagnostic\r
                        // requests.\r
-                       state = McuE_EnterCriticalSection();\r
                        if (runtime->localRxBuffer.status == PROVIDED_TO_PDUR) {\r
-                               if (isTesterPresentCommand(&(runtime->localRxBuffer.PduInfo))) {\r
-                                       startS3SessionTimer( runtime, protocolRow ); /** @req DCM141 **/ /** @req DCM112 **//** @req DCM113 **/\r
+                               if ( result == NTFRSLT_OK ) { // Make sure that the data in buffer is valid.\r
+                                       if (isTesterPresentCommand(&(runtime->localRxBuffer.PduInfo))) {\r
+                                               startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
+                                       }\r
                                }\r
                                runtime->localRxBuffer.status = NOT_IN_USE;\r
                        }\r
-                       McuE_ExitCriticalSection(state);\r
                }\r
+           Irq_Restore(state);\r
        }\r
 }\r
 \r
-\r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 //     Implements 'BufReq_ReturnType Dcm_ProvideTxBuffer(PduIdType dcmTxPduId,\r
 //  PduInfoType **pduInfoPtr, PduLengthType length)'.\r
@@ -653,41 +708,42 @@ void DslRxIndicationFromPduR(PduIdType dcmRxPduId, NotifResultType result) {
 //  it has detected that the pending request has been answered by DSD\r
 //  (or any other module?).\r
 \r
-BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId,\r
-               const PduInfoType **pduInfoPtr, PduLengthType length) {\r
+BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId, const PduInfoType **pduInfoPtr, PduLengthType length) {\r
        BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
-       const Dcm_DslProtocolRxType *protocolRx = NULL;\r
+       const Dcm_DslProtocolTxType *protocolTx = NULL;\r
        const Dcm_DslMainConnectionType *mainConnection = NULL;\r
        const Dcm_DslConnectionType *connection = NULL;\r
        const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
 \r
-       if (findParentConfigurationLeafs(dcmTxPduId, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
+       (void)length;           // Currently not used, this is only to remove compilation warnings\r
+//     DEBUG( DEBUG_MEDIUM, "DslProvideTxBuffer=%d\n", dcmTxPduId);\r
+       if (findTxPduIdParentConfigurationLeafs(dcmTxPduId, &protocolTx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
                switch (runtime->externalTxBufferStatus) { // ### EXTERNAL TX BUFFER ###\r
                case DCM_TRANSMIT_SIGNALED: {\r
+                       /** @req DCM346 */ /* Length verification is already done if this state is reached. */\r
                        *pduInfoPtr = &(protocolRow->DslProtocolTxBufferID->pduInfo);\r
-                       runtime->externalTxBufferStatus = PROVIDED_TO_PDUR;\r
+                       runtime->externalTxBufferStatus = PROVIDED_TO_PDUR; /** @req DCM349 */\r
                        ret = BUFREQ_OK;\r
                        break;\r
                }\r
                default:\r
+                       //DEBUG( DEBUG_MEDIUM, "DCM_TRANSMIT_SIGNALED was not signaled in the external buffer\n");\r
                        ret = BUFREQ_NOT_OK;\r
                        break;\r
                }\r
                if (ret == BUFREQ_NOT_OK) {\r
                        switch (runtime->localTxBuffer.status) { // ### LOCAL TX BUFFER ###\r
                        case DCM_TRANSMIT_SIGNALED: {\r
-                               runtime->localTxBuffer.PduInfo.SduDataPtr\r
-                                               = runtime->localTxBuffer.buffer;\r
-                               runtime->localTxBuffer.PduInfo.SduLength\r
-                                               = runtime->localTxBuffer.messageLenght;\r
+                               runtime->localTxBuffer.PduInfo.SduDataPtr = runtime->localTxBuffer.buffer;\r
+                               runtime->localTxBuffer.PduInfo.SduLength = runtime->localTxBuffer.messageLenght;\r
                                *pduInfoPtr = &runtime->localTxBuffer.PduInfo;\r
                                runtime->localTxBuffer.status = PROVIDED_TO_PDUR; // Now the DSL should not touch this Tx-buffer anymore.\r
                                ret = BUFREQ_OK;\r
-        break;\r
+                               break;\r
                        }\r
                        default:\r
+                               //DEBUG( DEBUG_MEDIUM, "DCM_TRANSMIT_SIGNALED was not signaled for the local buffer either\n");\r
                                ret = BUFREQ_NOT_OK;\r
                                break;\r
                        }\r
@@ -696,8 +752,6 @@ BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId,
        return ret;\r
 }\r
 \r
-\r
-\r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
 //     Implements 'void Dcm_TxConfirmation(PduIdType dcmTxPduId, NotifResultType result))'.\r
 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
@@ -706,53 +760,46 @@ BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId,
 //     is even stopped.\r
 \r
 void DslTxConfirmation(PduIdType dcmTxPduId, NotifResultType result) {\r
-       const Dcm_DslProtocolRxType *protocolRx = NULL;\r
+       const Dcm_DslProtocolTxType *protocolTx = NULL;\r
        const Dcm_DslMainConnectionType *mainConnection = NULL;\r
        const Dcm_DslConnectionType *connection = NULL;\r
        const Dcm_DslProtocolRowType *protocolRow = NULL;\r
        Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
        imask_t state;\r
 \r
-       DEBUG( DEBUG_MEDIUM, "DslTxConfirmation called!\n");\r
-       if (findParentConfigurationLeafs(dcmTxPduId, &protocolRx, &mainConnection,\r
-                       &connection, &protocolRow, &runtime)) {\r
+//     DEBUG( DEBUG_MEDIUM, "DslTxConfirmation=%d, result=%d\n", dcmTxPduId, result);\r
+       if (findTxPduIdParentConfigurationLeafs(dcmTxPduId, &protocolTx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
                boolean externalBufferReleased = FALSE;\r
 \r
                // Free the buffer and free the Pdu runtime data buffer.\r
-               state = McuE_EnterCriticalSection();\r
+           Irq_Save(state);\r
                switch (runtime->externalTxBufferStatus) { // ### EXTERNAL TX BUFFER ###\r
                case PROVIDED_TO_PDUR: {\r
-                       ComM_DCM_InactivateDiagnostic();\r
-                       startS3SessionTimer(runtime, protocolRow); // @DCM141\r
-                       releaseExternalRxTxBuffers(protocolRow, runtime);\r
- /*\r
-                       protocolRow->DslProtocolTxBufferID->externalBufferRuntimeData->status\r
-                                       = BUFFER_AVAILABLE;\r
-                       protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status\r
-                                       = BUFFER_AVAILABLE;          \r
-                       runtime->externalTxBufferStatus = IDLE;\r
-                       runtime->externalRxBufferStatus = IDLE;\r
-*/\r
-                       DEBUG( DEBUG_MEDIUM, "Released external buffer sucessfully!\n");\r
+#if defined(USE_COMM)\r
+                       ComM_DCM_InactiveDiagnostic(); /** @req DCM164 */\r
+#endif\r
+                       startS3SessionTimer(runtime, protocolRow); /** @req DCM141 */\r
+                       releaseExternalRxTxBuffers(protocolRow, runtime); /** @req DCM118 *//** @req DCM352 *//** @req DCM353 *//** @req DCM354 */\r
                        externalBufferReleased = TRUE;\r
-                       DsdDataConfirmation(dcmTxPduId, result); /** @req DCM117 **//** @req DCM235 **/\r
+               //      DEBUG( DEBUG_MEDIUM, "Released external buffer OK!\n");\r
+                       DsdDataConfirmation(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, result); /** @req DCM117 *//** @req DCM235 */\r
                        break;\r
                }\r
                default:\r
                        break;\r
                }\r
-               if ( externalBufferReleased == FALSE ) {\r
+               if (!externalBufferReleased) {\r
                        switch (runtime->localTxBuffer.status) { // ### LOCAL TX BUFFER ###\r
                        case PROVIDED_TO_PDUR:\r
-                               DEBUG( DEBUG_MEDIUM, "Released local buffer buffer!\n");\r
-                               runtime->localTxBuffer.status = IDLE;\r
+                       //      DEBUG( DEBUG_MEDIUM, "Released local buffer buffer OK!\n");\r
+                               runtime->localTxBuffer.status = DCM_IDLE;\r
                                break;\r
                        default:\r
-                               DEBUG( DEBUG_MEDIUM, "WARNING! DslTxConfirmation could not release any buffer!\n");\r
+                       //      DEBUG( DEBUG_MEDIUM, "WARNING! DslTxConfirmation could not release external or local buffer!\n");\r
                                break;\r
                        }\r
                }\r
-               McuE_ExitCriticalSection(state);\r
+           Irq_Restore(state);\r
        }\r
 }\r
 \r