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