]> rtime.felk.cvut.cz Git - arc.git/blob - diagnostic/Dcm/Dcm_Dsl.c
Updated Dem and Dcm after Lint check.
[arc.git] / diagnostic / Dcm / Dcm_Dsl.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 /*\r
17  *  General requirements\r
18  */\r
19 /** @req DCM030 */\r
20 \r
21 \r
22 #include <string.h>\r
23 #include "McuExtensions.h"\r
24 #include "Dcm.h"\r
25 #include "Dcm_Internal.h"\r
26 #include "MemMap.h"\r
27 #if defined(USE_COMM)\r
28 #include "ComM_Dcm.h"\r
29 #endif\r
30 #include "PduR_Dcm.h"\r
31 #include "ComStack_Types.h"\r
32 //#define USE_DEBUG_PRINTF\r
33 #include "debug.h"\r
34 \r
35 #define DECREMENT(timer) { if (timer > 0){timer--;} }\r
36 #define DCM_CONVERT_MS_TO_MAIN_CYCLES(x)  ((x)/DCM_MAIN_FUNCTION_PERIOD_TIME_MS)\r
37 \r
38 \r
39 /*\r
40  * Type definitions.\r
41  */\r
42 // #define MAX_PARALLEL_PROTOCOLS_ALLOWED               1\r
43 \r
44 typedef struct {\r
45         boolean initRun;\r
46         //boolean diagnosticRequestPending; // This is a "semaphore" because DSD and DCM can handle multiple/parallel request at the moment.\r
47         const Dcm_DslProtocolRowType *activeProtocol; // Points to the currently active protocol.\r
48 //      const Dcm_DslProtocolRowType *preemptedProtocol; // Points to the currently active protocol - reserved for future use\r
49 //      Dcm_DslRunTimeProtocolParametersType protocolList[MAX_PARALLEL_PROTOCOLS_ALLOWED];      // Reserved for future use\r
50 } DcmDsl_RunTimeDataType;\r
51 \r
52 static DcmDsl_RunTimeDataType DcmDslRunTimeData = {\r
53                 .initRun = FALSE,\r
54                 .activeProtocol = NULL\r
55 //              .preemptedProtocol = NULL,\r
56 //              .protocolList = {}\r
57 };\r
58 \r
59 \r
60 // ################# HELPER FUNCTIONS START #################\r
61 \r
62 //\r
63 // This function reset/stars the session (S3) timer. See requirement\r
64 // DCM141 when that action should be taken.\r
65 //\r
66 static inline void startS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime, const Dcm_DslProtocolRowType *protocolRow) {\r
67         const Dcm_DslProtocolTimingRowType *timeParams;\r
68         timeParams = protocolRow->DslProtocolTimeLimit;\r
69         runtime->S3ServerTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrS3Server);\r
70 }\r
71 \r
72 // - - - - - - - - - - -\r
73 \r
74 //\r
75 // This function reset/stars the session (S3) timer. See requirement\r
76 // DCM141 when that action should be taken.\r
77 //\r
78 static inline void stopS3SessionTimer(Dcm_DslRunTimeProtocolParametersType *runtime) {\r
79         runtime->S3ServerTimeoutCount = 0;\r
80 }\r
81 \r
82 // - - - - - - - - - - -\r
83 \r
84 //\r
85 //      This function implements the requirement DCM139 when\r
86 //      transition from one session to another.\r
87 //\r
88 static void changeDiagnosticSession(Dcm_DslRunTimeProtocolParametersType *runtime, Dcm_SesCtrlType newSession) {\r
89 \r
90         /** @req DCM139 */\r
91 \r
92         switch (runtime->sessionControl) {\r
93         case DCM_DEFAULT_SESSION: // "default".\r
94                 break;\r
95 \r
96         case DCM_PROGRAMMING_SESSION:\r
97         case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
98         case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
99         case DCM_ALL_SESSION_LEVEL:\r
100                 runtime->securityLevel = DCM_SEC_LEV_LOCKED; // "0x00".\r
101                 break;\r
102 \r
103         default:\r
104                 DET_REPORTERROR(MODULE_ID_DCM, 0, DCM_CHANGE_DIAGNOSTIC_SESSION_ID, DCM_E_PARAM);\r
105                 DEBUG(DEBUG_MEDIUM, "Old session invalid");\r
106                 break;\r
107         }\r
108 \r
109         switch (newSession) {\r
110         case DCM_DEFAULT_SESSION: // "default".\r
111         case DCM_PROGRAMMING_SESSION:\r
112         case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
113         case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
114         case DCM_ALL_SESSION_LEVEL:\r
115                 runtime->sessionControl = newSession;\r
116                 break;\r
117 \r
118         default:\r
119                 DET_REPORTERROR(MODULE_ID_DCM, 0, DCM_CHANGE_DIAGNOSTIC_SESSION_ID, DCM_E_PARAM);\r
120                 DEBUG(DEBUG_MEDIUM, "New session invalid");\r
121                 break;\r
122         }\r
123 }\r
124 \r
125 // - - - - - - - - - - -\r
126 \r
127 void DslResetSessionTimeoutTimer(void) {\r
128         const Dcm_DslProtocolRowType *activeProtocol;\r
129         Dcm_DslRunTimeProtocolParametersType *runtime;\r
130 \r
131         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
132         runtime = activeProtocol->DslRunTimeProtocolParameters;\r
133         startS3SessionTimer(runtime, activeProtocol); /** @req DCM141 */\r
134 }\r
135 \r
136 // - - - - - - - - - - -\r
137 \r
138 void DslGetCurrentServiceTable(const Dcm_DsdServiceTableType **currentServiceTable) { /** @req DCM195 */\r
139         const Dcm_DslProtocolRowType *activeProtocol;\r
140 \r
141         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
142         if (activeProtocol != NULL) {\r
143                 *currentServiceTable = activeProtocol->DslProtocolSIDTable;\r
144         }\r
145 }\r
146 \r
147 // - - - - - - - - - - -\r
148 \r
149 Std_ReturnType DslGetActiveProtocol(Dcm_ProtocolType *protocolId) { /** @req DCM340 */\r
150         Std_ReturnType ret = E_NOT_OK;\r
151         const Dcm_DslProtocolRowType *activeProtocol;\r
152 \r
153         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
154         if (activeProtocol != NULL) {\r
155                 *protocolId = activeProtocol->DslProtocolID;\r
156                 ret = E_OK;\r
157         }\r
158         return ret;\r
159 }\r
160 \r
161 // - - - - - - - - - - -\r
162 \r
163 void DslSetSecurityLevel(Dcm_SecLevelType secLevel) { /** @req DCM020 */\r
164         const Dcm_DslProtocolRowType *activeProtocol;\r
165         Dcm_DslRunTimeProtocolParametersType *runtime;\r
166 \r
167         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
168         runtime = activeProtocol->DslRunTimeProtocolParameters;\r
169         runtime->securityLevel = secLevel;\r
170 }\r
171 \r
172 // - - - - - - - - - - -\r
173 \r
174 Std_ReturnType DslGetSecurityLevel(Dcm_SecLevelType *secLevel) {  /** @req DCM020 *//** @req DCM338 */\r
175         Std_ReturnType ret = E_NOT_OK;\r
176         const Dcm_DslProtocolRowType *activeProtocol;\r
177         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
178 \r
179         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
180         if (activeProtocol != NULL) {\r
181                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
182                 *secLevel = runtime->securityLevel;\r
183                 ret = E_OK;\r
184         }\r
185         return ret;\r
186 }\r
187 \r
188 // - - - - - - - - - - -\r
189 \r
190 void DslSetSesCtrlType(Dcm_SesCtrlType sesCtrl) {  /** @req DCM022 */\r
191         const Dcm_DslProtocolRowType *activeProtocol;\r
192         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
193 \r
194         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
195         if (activeProtocol != NULL) {\r
196                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
197                 if (runtime->sessionControl != sesCtrl) {\r
198                         changeDiagnosticSession(runtime, sesCtrl);\r
199                         DslResetSessionTimeoutTimer();\r
200                 }\r
201         }\r
202 }\r
203 \r
204 // - - - - - - - - - - -\r
205 \r
206 Std_ReturnType DslGetSesCtrlType(Dcm_SesCtrlType *sesCtrlType) { /** @req DCM022 *//** @req DCM339 */\r
207         Std_ReturnType ret = E_NOT_OK;\r
208         const Dcm_DslProtocolRowType *activeProtocol;\r
209         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
210 \r
211         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
212         if (activeProtocol != NULL) {\r
213                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
214                 *sesCtrlType = runtime->sessionControl;\r
215                 ret = E_OK;\r
216         }\r
217         return ret;\r
218 }\r
219 \r
220 // - - - - - - - - - - -\r
221 \r
222 static boolean findRxPduIdParentConfigurationLeafs(PduIdType dcmRxPduId,\r
223                 const Dcm_DslProtocolRxType **protocolRx,\r
224                 const Dcm_DslMainConnectionType **mainConnection,\r
225                 const Dcm_DslConnectionType **connection,\r
226                 const Dcm_DslProtocolRowType **protocolRow,\r
227                 Dcm_DslRunTimeProtocolParametersType **runtime) {\r
228 \r
229         boolean ret = FALSE;\r
230         if (dcmRxPduId < DCM_DSL_RX_PDU_ID_LIST_LENGTH) {\r
231                 *protocolRx = &DCM_Config.Dsl->DslProtocol->DslProtocolRxGlobalList[dcmRxPduId];\r
232                 *mainConnection = (*protocolRx)->DslMainConnectionParent;\r
233                 *connection = (*mainConnection)->DslConnectionParent;\r
234                 *protocolRow = (*connection)->DslProtocolRow;\r
235                 *runtime = (*protocolRow)->DslRunTimeProtocolParameters;\r
236                 ret = TRUE;\r
237         }\r
238         return ret;\r
239 }\r
240 \r
241 // - - - - - - - - - - -\r
242 \r
243 static boolean findTxPduIdParentConfigurationLeafs(PduIdType dcmTxPduId,\r
244                 const Dcm_DslProtocolTxType **protocolTx,\r
245                 const Dcm_DslMainConnectionType **mainConnection,\r
246                 const Dcm_DslConnectionType **connection,\r
247                 const Dcm_DslProtocolRowType **protocolRow,\r
248                 Dcm_DslRunTimeProtocolParametersType **runtime) {\r
249 \r
250         boolean ret = FALSE;\r
251         if (dcmTxPduId < DCM_DSL_TX_PDU_ID_LIST_LENGTH) {\r
252                 *protocolTx = &DCM_Config.Dsl->DslProtocol->DslProtocolTxGlobalList[dcmTxPduId];\r
253                 *mainConnection = (*protocolTx)->DslMainConnectionParent;\r
254                 *connection = (*mainConnection)->DslConnectionParent;\r
255                 *protocolRow = (*connection)->DslProtocolRow;\r
256                 *runtime = (*protocolRow)->DslRunTimeProtocolParameters;\r
257                 ret = TRUE;\r
258         }\r
259         return ret;\r
260 }\r
261 \r
262 // - - - - - - - - - - -\r
263 \r
264 static inline void releaseExternalRxTxBuffers(const Dcm_DslProtocolRowType *protocolRow,\r
265                 Dcm_DslRunTimeProtocolParametersType *runtime) {\r
266 \r
267         protocolRow->DslProtocolTxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
268         protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
269         runtime->externalTxBufferStatus = NOT_IN_USE; // We are waiting for DSD to return the buffer. qqq.\r
270         runtime->externalRxBufferStatus = NOT_IN_USE; // We are waiting for DSD to return the buffer. qqq.\r
271 }\r
272 \r
273 // - - - - - - - - - - -\r
274 \r
275 \r
276 static inline void releaseExternalRxTxBuffersHelper(PduIdType rxPduIdRef) {\r
277         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
278         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
279         const Dcm_DslConnectionType *connection = NULL;\r
280         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
281         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
282 \r
283         if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
284                 releaseExternalRxTxBuffers(protocolRow, runtime);\r
285         }\r
286 }\r
287 \r
288 // - - - - - - - - - - -\r
289 \r
290 /*\r
291  *  This function is called from the DSD module to the DSL when\r
292  *  a response to a diagnostic request has been copied into the\r
293  *  given TX-buffer and is ready for transmission.\r
294  */\r
295 void DslDsdProcessingDone(PduIdType rxPduIdRef, DsdProcessingDoneResultType responseResult) {\r
296         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
297         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
298         const Dcm_DslConnectionType *connection = NULL;\r
299         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
300         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
301 \r
302         DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone rxPduIdRef=%d\n", rxPduIdRef);\r
303 \r
304         if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
305                 imask_t state = McuE_EnterCriticalSection();\r
306                 switch (responseResult) {\r
307                 case DSD_TX_RESPONSE_READY:\r
308                         runtime->externalTxBufferStatus = DSD_PENDING_RESPONSE_SIGNALED; /** @req DCM114 */\r
309                         break;\r
310                 case DSD_TX_RESPONSE_SUPPRESSED: /** @req DCM238 */\r
311                         DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone called with DSD_TX_RESPONSE_SUPPRESSED.\n");\r
312                         releaseExternalRxTxBuffersHelper(rxPduIdRef);\r
313                         break;\r
314                 default:\r
315                         DEBUG( DEBUG_MEDIUM, "Unknown response result from DslDsdProcessingDone!\n");\r
316                         break;\r
317                 }\r
318                 McuE_ExitCriticalSection(state);\r
319         }\r
320 }\r
321 \r
322 // - - - - - - - - - - -\r
323 \r
324 /*\r
325  *      This function preparing transmission of response\r
326  *      pending message to tester.\r
327  */\r
328 static void sendResponse(const Dcm_DslProtocolRowType *protocol,\r
329                 Dcm_NegativeResponseCodeType responseCode) {\r
330         //Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
331         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
332         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
333         const Dcm_DslConnectionType *connection = NULL;\r
334         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
335         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
336         Std_ReturnType transmitResult;\r
337 \r
338         /** @req DCM119 */\r
339         imask_t state = McuE_EnterCriticalSection();\r
340         if (findRxPduIdParentConfigurationLeafs(protocol->DslRunTimeProtocolParameters->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
341                 if (runtime->localTxBuffer.status == NOT_IN_USE) {\r
342                         runtime->localTxBuffer.status = PROVIDED_TO_DSD;\r
343                         runtime->localTxBuffer.buffer[0] = SID_NEGATIVE_RESPONSE;\r
344                         runtime->localTxBuffer.buffer[1] = protocol->DslProtocolRxBufferID->pduInfo.SduDataPtr[2];\r
345                         runtime->localTxBuffer.buffer[2] = responseCode;\r
346                         runtime->localTxBuffer.PduInfo.SduDataPtr = runtime->localTxBuffer.buffer;\r
347                         runtime->localTxBuffer.PduInfo.SduLength = 3;\r
348                         runtime->localTxBuffer.status = DCM_TRANSMIT_SIGNALED; // In the DslProvideTxBuffer 'callback' this state signals it is the local buffer we are interested in sending.\r
349                         transmitResult = PduR_DcmTransmit(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, &(runtime->localTxBuffer.PduInfo));/** @req DCM115.Partially */ /* The P2ServerMin has not been implemented. */\r
350                         if (transmitResult != E_OK) {\r
351                                 // TODO: What to do here?\r
352                         }\r
353                 }\r
354         }\r
355         McuE_ExitCriticalSection(state);\r
356 }\r
357 \r
358 // - - - - - - - - - - -\r
359 \r
360 static Std_ReturnType StartProtocolHelper(Dcm_ProtocolType protocolId) {\r
361         Std_ReturnType ret = E_NOT_OK;\r
362         uint16 i;\r
363 \r
364         for (i = 0; !DCM_Config.Dsl->DslCallbackDCMRequestService[i].Arc_EOL; i++) {\r
365                 if (DCM_Config.Dsl->DslCallbackDCMRequestService[i].StartProtocol != NULL) {\r
366                         ret = DCM_Config.Dsl->DslCallbackDCMRequestService[i]. StartProtocol(protocolId);\r
367                         if (ret != E_OK) {\r
368                                 break;\r
369                         }\r
370                 }\r
371         }\r
372         return ret;\r
373 }\r
374 \r
375 // - - - - - - - - - - -\r
376 \r
377 static boolean isTesterPresentCommand(const PduInfoType *rxPdu) {\r
378         boolean ret = FALSE;\r
379         if ((rxPdu->SduDataPtr[0] == SID_TESTER_PRESENT) && (rxPdu->SduDataPtr[1] & SUPPRESS_POS_RESP_BIT)) {\r
380                 ret = TRUE;\r
381         }\r
382         return ret;\r
383 }\r
384 \r
385 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
386 //      Implements 'void Dcm_Init(void)' for DSL.\r
387 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
388 void DslInit(void) {\r
389         const Dcm_DslProtocolRowType *listEntry;\r
390         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
391 \r
392         listEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
393         while (listEntry->Arc_EOL == FALSE) {\r
394                 runtime = listEntry->DslRunTimeProtocolParameters;\r
395                 runtime->externalRxBufferStatus = DCM_IDLE;\r
396                 runtime->externalTxBufferStatus = DCM_IDLE;\r
397                 runtime->localRxBuffer.status = DCM_IDLE;\r
398                 runtime->localTxBuffer.status = DCM_IDLE;\r
399                 runtime->securityLevel = DCM_SEC_LEV_LOCKED; /** @req DCM033 */\r
400                 runtime->sessionControl = DCM_DEFAULT_SESSION; /** @req DCM034 */\r
401                 listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
402                 listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
403                 listEntry++;\r
404         };\r
405         //DcmDslRunTimeData.diagnosticRequestPending = FALSE;\r
406         DcmDslRunTimeData.initRun = TRUE;\r
407 }\r
408 \r
409 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
410 //      Implements 'void Dcm_MainFunction(void)' for DSL.\r
411 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
412 \r
413 void DslMain(void) {\r
414         const Dcm_DslProtocolRowType *protocolRowEntry;\r
415         const Dcm_DslProtocolTimingRowType *timeParams = NULL;\r
416         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
417 \r
418         protocolRowEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
419         while (protocolRowEntry->Arc_EOL == FALSE) {\r
420                 runtime = protocolRowEntry->DslRunTimeProtocolParameters;\r
421                 if (runtime != NULL) {\r
422                         // #### HANDLE THE TESTER PRESENT PRESENCE ####\r
423                         if (runtime->sessionControl != DCM_DEFAULT_SESSION) { // Timeout if tester present is lost.\r
424                                 DECREMENT(runtime->S3ServerTimeoutCount);\r
425                                 if (runtime->S3ServerTimeoutCount == 0) {\r
426                                         changeDiagnosticSession(runtime, DCM_DEFAULT_SESSION); /** @req DCM140 */\r
427                                 }\r
428                         }\r
429                         switch (runtime->externalTxBufferStatus) { // #### TX buffer state. ####\r
430                         case NOT_IN_USE:\r
431                                 //DEBUG( DEBUG_MEDIUM, "state NOT_IN_USE!\n");\r
432                                 break;\r
433                         case PROVIDED_TO_DSD: {\r
434                                 DEBUG( DEBUG_MEDIUM, "debug_count=%d\n", debug_count);\r
435                                 DECREMENT(runtime->stateTimeoutCount);\r
436                                 if (runtime->stateTimeoutCount == 0) {\r
437                                         DEBUG( DEBUG_MEDIUM, "State PROVIDED_TO_DSD timed out!", debug_count);\r
438                                         timeParams = protocolRowEntry->DslProtocolTimeLimit;\r
439                                         runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* Reinitiate timer, see 9.2.2. */\r
440                                         if (DCM_Config.Dsl->DslDiagResp != NULL) {\r
441                                                 if (DCM_Config.Dsl->DslDiagResp->DslDiagRespForceRespPendEn == TRUE) {\r
442                                                         if (runtime->responsePendingCount != 0) {\r
443                                                                 sendResponse(protocolRowEntry, DCM_E_RESPONSEPENDING);  /** @req DCM024 */\r
444                                                                 DECREMENT( runtime->responsePendingCount );\r
445                                                         } else {\r
446                                                                 sendResponse(protocolRowEntry, DCM_E_GENERALREJECT); /** @req DCM120 */\r
447                                                                 releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
448                                                         }\r
449                                                 } else {\r
450                                                         DEBUG( DEBUG_MEDIUM, "Not configured to send response pending, now sending general reject!\n");\r
451                                                         sendResponse(protocolRowEntry, DCM_E_GENERALREJECT);\r
452                                                         releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
453                                                 }\r
454                                         }\r
455                                 }\r
456                                 break;\r
457                         }\r
458                         case DSD_PENDING_RESPONSE_SIGNALED:\r
459                                 // The DSD has signaled to DSL that the diagnostic response is available in the Tx buffer.\r
460                                 // Make sure that response pending or general reject have not been issued,\r
461                                 // if so we can not transmit to PduR because we would not know from where\r
462                                 // the Tx confirmation resides later.\r
463                                 DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
464                                 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
465                                         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
466                                         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
467                                         const Dcm_DslConnectionType *connection = NULL;\r
468                                         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
469                                         Std_ReturnType transmitResult;\r
470 \r
471                                         if (findRxPduIdParentConfigurationLeafs(runtime->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
472                                                 const PduIdType txPduId = mainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
473                                                 DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DCM_TRANSMIT_SIGNALED.\n" );\r
474                                                 runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
475                                                 transmitResult = PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
476                                                 if (transmitResult != E_OK) {\r
477                                                         // TODO: What to do here?\r
478                                                 }\r
479                                         } else {\r
480                                                 DEBUG( DEBUG_MEDIUM, "***** WARNING, THIS IS UNEXPECTED !!! ********.\n" );\r
481                                                 const PduIdType txPduId = protocolRowEntry->DslConnection->DslMainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
482                                                 DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DSD_PENDING_RESPONSE_SIGNALED.\n", txPduId);\r
483                                                 runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
484                                                 DEBUG( DEBUG_MEDIUM, "Calling PduR_DcmTransmit with txPduId = %d from DslMain\n", txPduId);\r
485                                                 transmitResult = PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
486                                                 if (transmitResult != E_OK) {\r
487                                                         // TODO: What to do here?\r
488                                                 }\r
489                                         }\r
490                                 }\r
491                                 break;\r
492                         case DCM_TRANSMIT_SIGNALED:\r
493                                 //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
494                                 break;\r
495                         case PROVIDED_TO_PDUR: // The valid data is being transmitted by TP-layer.\r
496                                 //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
497                                 break;\r
498                         default:\r
499                                 break;\r
500                         }\r
501                 }\r
502                 protocolRowEntry++;\r
503         }\r
504 }\r
505 \r
506 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
507 //      Implements 'BufReq_ReturnType Dcm_ProvideRxBuffer(PduIdType dcmRxPduId,\r
508 //  PduLengthType tpSduLength, PduInfoType **pduInfoPtr)'.\r
509 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
510 //  This function is called called by the PduR typically when CanTp has\r
511 //  received a FF or a single frame and needs to obtain a buffer from the\r
512 //  receiver so that received data can be forwarded.\r
513 \r
514 BufReq_ReturnType DslProvideRxBufferToPdur(PduIdType dcmRxPduId, PduLengthType tpSduLength, const PduInfoType **pduInfoPtr) {\r
515         BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
516         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
517         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
518         const Dcm_DslConnectionType *connection = NULL;\r
519         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
520         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
521 \r
522         DEBUG( DEBUG_MEDIUM, "DslProvideRxBufferToPdur(dcmRxPduId=%d) called!\n", dcmRxPduId);\r
523         imask_t state = McuE_EnterCriticalSection();\r
524         if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
525                 const Dcm_DslBufferType *externalRxBuffer = protocolRow->DslProtocolRxBufferID;\r
526                 if (externalRxBuffer->pduInfo.SduLength >= tpSduLength) { /** @req DCM443 */\r
527                         if ((runtime->externalRxBufferStatus == NOT_IN_USE) && (externalRxBuffer->externalBufferRuntimeData->status == BUFFER_AVAILABLE)) {\r
528                                 DEBUG( DEBUG_MEDIUM, "External buffer available!\n");\r
529                                 // ### EXTERNAL BUFFER IS AVAILABLE; GRAB IT AND REMEBER THAT WE OWN IT! ###\r
530                                 externalRxBuffer->externalBufferRuntimeData->status = BUFFER_BUSY;\r
531                                 runtime->diagnosticRequestFromTester.SduDataPtr = externalRxBuffer->pduInfo.SduDataPtr;\r
532                                 runtime->diagnosticRequestFromTester.SduLength = tpSduLength;\r
533                                 *pduInfoPtr = &(runtime->diagnosticRequestFromTester);\r
534                                 runtime->externalRxBufferStatus = PROVIDED_TO_PDUR; /** @req DCM342 */\r
535                                 ret = BUFREQ_OK;\r
536                         } else {\r
537                                 DEBUG( DEBUG_MEDIUM, "Local buffer available!\n");\r
538                                 if (runtime->externalRxBufferStatus == PROVIDED_TO_DSD) {\r
539                                         // ### EXTERNAL BUFFER IS IN USE BY THE DSD, TRY TO USE LOCAL BUFFER! ###\r
540                                         if (runtime->localRxBuffer.status == NOT_IN_USE) {\r
541                                                 if (tpSduLength < DCM_DSL_LOCAL_BUFFER_LENGTH) {\r
542                                                         runtime->localRxBuffer.status = PROVIDED_TO_PDUR;\r
543                                                         runtime->localRxBuffer.PduInfo.SduDataPtr = runtime->localRxBuffer.buffer;\r
544                                                         runtime->localRxBuffer.PduInfo.SduLength = tpSduLength;\r
545                                                         *pduInfoPtr = &(runtime->localRxBuffer.PduInfo);\r
546                                                         ret = BUFREQ_OK;\r
547                                                 } else {\r
548                                                         ret = BUFREQ_BUSY;\r
549                                                 }\r
550                                         }\r
551                                 } else {\r
552                                         // The buffer is in use by the PduR, we can not help this because then\r
553                                         // we would have two different Rx-indications with same PduId but we\r
554                                         // will not know which buffer the indication should free.\r
555                                         ret = BUFREQ_BUSY; /** @req DCM445 */\r
556                                 }\r
557                         }\r
558                 } else {\r
559                         ret = BUFREQ_OVFL; /** @req DCM444 */\r
560                 }\r
561                 if (ret == BUFREQ_OK) {\r
562                         stopS3SessionTimer(runtime); /** @req DCM141 */\r
563                 }\r
564         }\r
565         McuE_ExitCriticalSection(state);\r
566         return ret;\r
567 }\r
568 \r
569 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
570 //      Implements 'void Dcm_RxIndication(PduIdType dcmRxPduId, NotifResultType result)'.\r
571 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
572 //      This function is called called by the PduR typically when CanTp has\r
573 //      received the diagnostic request, copied it to the provided buffer and need to indicate\r
574 //      this to the DCM (DSL) module via proprietary API.\r
575 \r
576 void DslRxIndicationFromPduR(PduIdType dcmRxPduId, NotifResultType result) {\r
577         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
578         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
579         const Dcm_DslConnectionType *connection = NULL;\r
580         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
581         const Dcm_DslProtocolTimingRowType *timeParams = NULL;\r
582         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
583         Std_ReturnType higherLayerResp;\r
584         imask_t state;\r
585 \r
586         /** @req DCM345, this needs to be verified when connection to CanIf works. */\r
587 \r
588         if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
589                 timeParams = protocolRow->DslProtocolTimeLimit;\r
590                 // We need to find out in what buffer we can find our Rx data (it can\r
591                 // be either in the normal RX-buffer or the 'extra' buffer for implementing\r
592                 // the Concurrent "Test Present" functionality.\r
593                 state = McuE_EnterCriticalSection();\r
594                 if (runtime->externalRxBufferStatus == PROVIDED_TO_PDUR) {\r
595                         if ( result == NTFRSLT_OK ) { /** @req DCM111 */\r
596                                 if (isTesterPresentCommand(&(protocolRow->DslProtocolRxBufferID->pduInfo))) {\r
597                                         startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
598                                         runtime->externalRxBufferStatus = NOT_IN_USE;\r
599                                         protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
600                                 } else {\r
601                                         if (runtime->protocolStarted == FALSE) {\r
602                                                 higherLayerResp = StartProtocolHelper(protocolRow->DslProtocolID); /** @req DCM036 */\r
603                                                 if (higherLayerResp == E_OK) {\r
604                                                         runtime->protocolStarted = TRUE;\r
605                                                         DcmDslRunTimeData.activeProtocol = protocolRow;\r
606                                                 }\r
607                                         }\r
608                                         if (runtime->protocolStarted == TRUE) {\r
609                                                 if (runtime->diagnosticActiveComM == FALSE) {\r
610 #if defined(USE_COMM)\r
611                                                         ComM_DCM_ActivateDiagnostic(); /** @req DCM163 */\r
612 #endif\r
613                                                         runtime->diagnosticActiveComM = TRUE;\r
614                                                 }\r
615                                                 timeParams = protocolRow->DslProtocolTimeLimit;\r
616                                                 runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* See 9.2.2. */\r
617                                                 runtime->externalRxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
618                                                 if (runtime->externalTxBufferStatus == NOT_IN_USE) {\r
619                                                         DEBUG( DEBUG_MEDIUM, "External Tx buffer available, we can pass it to DSD.\n");\r
620                                                 } else {\r
621                                                         DEBUG( DEBUG_MEDIUM, "External buffer not available, a response is being transmitted?\n");\r
622                                                 }\r
623                                                 runtime->externalTxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
624                                                 runtime->responsePendingCount = DCM_Config.Dsl->DslDiagResp->DslDiagRespMaxNumRespPend;\r
625                                                 runtime->diagnosticResponseFromDsd.SduDataPtr = protocolRow->DslProtocolTxBufferID->pduInfo.SduDataPtr;\r
626                                                 runtime->diagnosticResponseFromDsd.SduLength = protocolRow->DslProtocolTxBufferID->pduInfo.SduLength;\r
627                                                 DEBUG( DEBUG_MEDIUM, "DsdDslDataIndication(DcmDslProtocolTxPduId=%d, dcmRxPduId=%d)\n", mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, dcmRxPduId);\r
628                                                 runtime->diagReqestRxPduId = dcmRxPduId;\r
629                                                 DsdDslDataIndication(  // qqq: We are inside a critical section.\r
630                                                                 &(runtime->diagnosticRequestFromTester),\r
631                                                                 protocolRow->DslProtocolSIDTable,       /** @req DCM035 */\r
632                                                                 protocolRx->DslProtocolAddrType,\r
633                                                                 mainConnection->DslProtocolTx->DcmDslProtocolTxPduId,\r
634                                                                 &(runtime->diagnosticResponseFromDsd),\r
635                                                                 dcmRxPduId);\r
636                                         }\r
637                                 }\r
638                         } else { /** @req DCM344 */\r
639                                 // The indication was not equal to NTFRSLT_OK, release the resources and no forward to DSD.\r
640                                 runtime->externalRxBufferStatus = NOT_IN_USE;\r
641                                 protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
642                         }\r
643                 } else {\r
644                         // It is the local buffer that was provided to the PduR, that buffer\r
645                         // is only used for tester present reception in parallel to diagnostic\r
646                         // requests.\r
647                         if (runtime->localRxBuffer.status == PROVIDED_TO_PDUR) {\r
648                                 if ( result == NTFRSLT_OK ) { // Make sure that the data in buffer is valid.\r
649                                         if (isTesterPresentCommand(&(runtime->localRxBuffer.PduInfo))) {\r
650                                                 startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
651                                         }\r
652                                 }\r
653                                 runtime->localRxBuffer.status = NOT_IN_USE;\r
654                         }\r
655                 }\r
656                 McuE_ExitCriticalSection(state);\r
657         }\r
658 }\r
659 \r
660 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
661 //      Implements 'BufReq_ReturnType Dcm_ProvideTxBuffer(PduIdType dcmTxPduId,\r
662 //  PduInfoType **pduInfoPtr, PduLengthType length)'.\r
663 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
664 //  This TX-buffer request is likely triggered by the transport layer (i.e. CanTp)\r
665 //  after PduR_DcmTransmit() has been called (via PduR to CanTp) indicating that something\r
666 //  is to be sent. The PduR_DcmTransmit() call is done from the DSL main function when\r
667 //  it has detected that the pending request has been answered by DSD\r
668 //  (or any other module?).\r
669 \r
670 BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId, const PduInfoType **pduInfoPtr, PduLengthType length) {\r
671         BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
672         const Dcm_DslProtocolTxType *protocolTx = NULL;\r
673         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
674         const Dcm_DslConnectionType *connection = NULL;\r
675         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
676         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
677 \r
678         (void)length;           // Currently not used, this is only to remove compilation warnings\r
679         DEBUG( DEBUG_MEDIUM, "DslProvideTxBuffer=%d\n", dcmTxPduId);\r
680         if (findTxPduIdParentConfigurationLeafs(dcmTxPduId, &protocolTx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
681                 switch (runtime->externalTxBufferStatus) { // ### EXTERNAL TX BUFFER ###\r
682                 case DCM_TRANSMIT_SIGNALED: {\r
683                         /** @req DCM346 */ /* Length verification is already done if this state is reached. */\r
684                         *pduInfoPtr = &(protocolRow->DslProtocolTxBufferID->pduInfo);\r
685                         runtime->externalTxBufferStatus = PROVIDED_TO_PDUR; /** @req DCM349 */\r
686                         ret = BUFREQ_OK;\r
687                         break;\r
688                 }\r
689                 default:\r
690                         DEBUG( DEBUG_MEDIUM, "DCM_TRANSMIT_SIGNALED was not signaled in the external buffer\n");\r
691                         ret = BUFREQ_NOT_OK;\r
692                         break;\r
693                 }\r
694                 if (ret == BUFREQ_NOT_OK) {\r
695                         switch (runtime->localTxBuffer.status) { // ### LOCAL TX BUFFER ###\r
696                         case DCM_TRANSMIT_SIGNALED: {\r
697                                 runtime->localTxBuffer.PduInfo.SduDataPtr = runtime->localTxBuffer.buffer;\r
698                                 runtime->localTxBuffer.PduInfo.SduLength = runtime->localTxBuffer.messageLenght;\r
699                                 *pduInfoPtr = &runtime->localTxBuffer.PduInfo;\r
700                                 runtime->localTxBuffer.status = PROVIDED_TO_PDUR; // Now the DSL should not touch this Tx-buffer anymore.\r
701                                 ret = BUFREQ_OK;\r
702                                 break;\r
703                         }\r
704                         default:\r
705                                 DEBUG( DEBUG_MEDIUM, "DCM_TRANSMIT_SIGNALED was not signaled for the local buffer either\n");\r
706                                 ret = BUFREQ_NOT_OK;\r
707                                 break;\r
708                         }\r
709                 }\r
710         }\r
711         return ret;\r
712 }\r
713 \r
714 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
715 //      Implements 'void Dcm_TxConfirmation(PduIdType dcmTxPduId, NotifResultType result))'.\r
716 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
717 //      This function is called by the PduR (which has been trigged by i.e. CanTp)\r
718 //      when a transmission has been successfully finished, have had errors or\r
719 //      is even stopped.\r
720 \r
721 void DslTxConfirmation(PduIdType dcmTxPduId, NotifResultType result) {\r
722         const Dcm_DslProtocolTxType *protocolTx = NULL;\r
723         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
724         const Dcm_DslConnectionType *connection = NULL;\r
725         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
726         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
727         imask_t state;\r
728 \r
729         DEBUG( DEBUG_MEDIUM, "DslTxConfirmation=%d, result=%d\n", dcmTxPduId, result);\r
730         if (findTxPduIdParentConfigurationLeafs(dcmTxPduId, &protocolTx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
731                 boolean externalBufferReleased = FALSE;\r
732 \r
733                 // Free the buffer and free the Pdu runtime data buffer.\r
734                 state = McuE_EnterCriticalSection();\r
735                 switch (runtime->externalTxBufferStatus) { // ### EXTERNAL TX BUFFER ###\r
736                 case PROVIDED_TO_PDUR: {\r
737 #if defined(USE_COMM)\r
738                         ComM_DCM_InactivateDiagnostic(); /** @req DCM164 */\r
739 #endif\r
740                         startS3SessionTimer(runtime, protocolRow); /** @req DCM141 */\r
741                         releaseExternalRxTxBuffers(protocolRow, runtime); /** @req DCM118 *//** @req DCM352 *//** @req DCM353 *//** @req DCM354 */\r
742                         externalBufferReleased = TRUE;\r
743                         DEBUG( DEBUG_MEDIUM, "Released external buffer OK!\n");\r
744                         DsdDataConfirmation(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, result); /** @req DCM117 *//** @req DCM235 */\r
745                         break;\r
746                 }\r
747                 default:\r
748                         break;\r
749                 }\r
750                 if (!externalBufferReleased) {\r
751                         switch (runtime->localTxBuffer.status) { // ### LOCAL TX BUFFER ###\r
752                         case PROVIDED_TO_PDUR:\r
753                                 DEBUG( DEBUG_MEDIUM, "Released local buffer buffer OK!\n");\r
754                                 runtime->localTxBuffer.status = DCM_IDLE;\r
755                                 break;\r
756                         default:\r
757                                 DEBUG( DEBUG_MEDIUM, "WARNING! DslTxConfirmation could not release external or local buffer!\n");\r
758                                 break;\r
759                         }\r
760                 }\r
761                 McuE_ExitCriticalSection(state);\r
762         }\r
763 }\r
764 \r