]> rtime.felk.cvut.cz Git - arc.git/blob - diagnostic/Dcm/Dcm_Dsl.c
Merge with default
[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,\r
107                 Dcm_SesCtrlType newSession) {\r
108 \r
109         /** @req DCM139 */\r
110 \r
111         switch (runtime->sessionControl) {\r
112         case DCM_DEFAULT_SESSION: // "default".\r
113                 break;\r
114 \r
115         case DCM_PROGRAMMING_SESSION:\r
116         case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
117         case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
118         case DCM_ALL_SESSION_LEVEL:\r
119                 runtime->securityLevel = DCM_SEC_LEV_LOCKED; // "0x00".\r
120                 break;\r
121 \r
122         default:\r
123                 // TODO: Log this error.\r
124                 DEBUG(DEBUG_MEDIUM, "Old session invalid");\r
125                 break;\r
126         }\r
127 \r
128         switch (newSession) {\r
129         case DCM_DEFAULT_SESSION: // "default".\r
130         case DCM_PROGRAMMING_SESSION:\r
131         case DCM_EXTENDED_DIAGNOSTIC_SESSION:\r
132         case DCM_SAFTEY_SYSTEM_DIAGNOSTIC_SESSION:\r
133         case DCM_ALL_SESSION_LEVEL:\r
134                 runtime->sessionControl = newSession;\r
135                 break;\r
136 \r
137         default:\r
138                 // TODO: Log this error.\r
139                 DEBUG(DEBUG_MEDIUM, "New session invalid");\r
140                 break;\r
141         }\r
142 }\r
143 \r
144 // - - - - - - - - - - -\r
145 \r
146 void DslResetSessionTimeoutTimer() {\r
147         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
148         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
149         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
150         runtime = activeProtocol->DslRunTimeProtocolParameters;\r
151         startS3SessionTimer(runtime, activeProtocol); /** @req DCM141 */\r
152 }\r
153 \r
154 // - - - - - - - - - - -\r
155 \r
156 void DslGetCurrentServiceTable(const Dcm_DsdServiceTableType **currentServiceTable) { /** @req DCM195 */\r
157         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
158         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
159         if (activeProtocol != NULL) {\r
160                 *currentServiceTable = activeProtocol->DslProtocolSIDTable;\r
161         }\r
162 }\r
163 \r
164 // - - - - - - - - - - -\r
165 \r
166 Std_ReturnType DslGetActiveProtocol(Dcm_ProtocolType *protocolId) { /** @req DCM340 */\r
167         Std_ReturnType ret = E_NOT_OK;\r
168         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
169         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
170         if (activeProtocol != NULL) {\r
171                 *protocolId = activeProtocol->DslProtocolID;\r
172                 ret = E_OK;\r
173         }\r
174         return ret;\r
175 }\r
176 \r
177 // - - - - - - - - - - -\r
178 \r
179 void DslSetSecurityLevel(Dcm_SecLevelType secLevel) { /** @req DCM020 */\r
180         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
181         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
182         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
183         runtime = activeProtocol->DslRunTimeProtocolParameters;\r
184         runtime->securityLevel = secLevel;\r
185 }\r
186 \r
187 // - - - - - - - - - - -\r
188 \r
189 Std_ReturnType DslGetSecurityLevel(Dcm_SecLevelType *secLevel) {  /** @req DCM020 *//** @req DCM338 */\r
190         Std_ReturnType ret = E_NOT_OK;\r
191         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
192         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
193         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
194         if (activeProtocol != NULL) {\r
195                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
196                 *secLevel = runtime->securityLevel;\r
197                 ret = E_OK;\r
198         }\r
199         return ret;\r
200 }\r
201 \r
202 // - - - - - - - - - - -\r
203 \r
204 void DslSetSesCtrlType(Dcm_SesCtrlType sesCtrl) {  /** @req DCM022 */\r
205         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
206         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
207         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
208         if (activeProtocol != NULL) {\r
209                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
210                 if (runtime->sessionControl != sesCtrl) {\r
211                         changeDiagnosticSession(runtime, sesCtrl);\r
212                         DslResetSessionTimeoutTimer();\r
213                 }\r
214         }\r
215 }\r
216 \r
217 // - - - - - - - - - - -\r
218 \r
219 Std_ReturnType DslGetSesCtrlType(Dcm_SesCtrlType *sesCtrlType) { /** @req DCM022 *//** @req DCM339 */\r
220         Std_ReturnType ret = E_NOT_OK;\r
221         const Dcm_DslProtocolRowType *activeProtocol = NULL;\r
222         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
223         activeProtocol = DcmDslRunTimeData.activeProtocol;\r
224         if (activeProtocol != NULL) {\r
225                 runtime = activeProtocol->DslRunTimeProtocolParameters;\r
226                 *sesCtrlType = runtime->sessionControl;\r
227                 ret = E_OK;\r
228         }\r
229         return ret;\r
230 }\r
231 \r
232 // - - - - - - - - - - -\r
233 \r
234 static boolean findRxPduIdParentConfigurationLeafs(PduIdType dcmRxPduId,\r
235                 const Dcm_DslProtocolRxType **protocolRx,\r
236                 const Dcm_DslMainConnectionType **mainConnection,\r
237                 const Dcm_DslConnectionType **connection,\r
238                 const Dcm_DslProtocolRowType **protocolRow,\r
239                 Dcm_DslRunTimeProtocolParametersType **runtime) {\r
240 \r
241         boolean ret = FALSE;\r
242         if (dcmRxPduId < DCM_DSL_RX_PDU_ID_LIST_LENGTH) {\r
243                 *protocolRx = &DCM_Config.Dsl->DslProtocol->DslProtocolRxGlobalList[dcmRxPduId];\r
244                 *mainConnection = (*protocolRx)->DslMainConnectionParent;\r
245                 *connection = (*mainConnection)->DslConnectionParent;\r
246                 *protocolRow = (*connection)->DslProtocolRow;\r
247                 *runtime = (*protocolRow)->DslRunTimeProtocolParameters;\r
248                 ret = TRUE;\r
249         }\r
250         return ret;\r
251 }\r
252 \r
253 // - - - - - - - - - - -\r
254 \r
255 static boolean findTxPduIdParentConfigurationLeafs(PduIdType dcmTxPduId,\r
256                 const Dcm_DslProtocolTxType **protocolTx,\r
257                 const Dcm_DslMainConnectionType **mainConnection,\r
258                 const Dcm_DslConnectionType **connection,\r
259                 const Dcm_DslProtocolRowType **protocolRow,\r
260                 Dcm_DslRunTimeProtocolParametersType **runtime) {\r
261 \r
262         boolean ret = FALSE;\r
263         if (dcmTxPduId < DCM_DSL_TX_PDU_ID_LIST_LENGTH) {\r
264                 *protocolTx = &DCM_Config.Dsl->DslProtocol->DslProtocolTxGlobalList[dcmTxPduId];\r
265                 *mainConnection = (*protocolTx)->DslMainConnectionParent;\r
266                 *connection = (*mainConnection)->DslConnectionParent;\r
267                 *protocolRow = (*connection)->DslProtocolRow;\r
268                 *runtime = (*protocolRow)->DslRunTimeProtocolParameters;\r
269                 ret = TRUE;\r
270         }\r
271         return ret;\r
272 }\r
273 \r
274 // - - - - - - - - - - -\r
275 \r
276 static inline void releaseExternalRxTxBuffers(const Dcm_DslProtocolRowType *protocolRow,\r
277                 Dcm_DslRunTimeProtocolParametersType *runtime) {\r
278 \r
279         protocolRow->DslProtocolTxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
280         protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
281         runtime->externalTxBufferStatus = NOT_IN_USE; // We are waiting for DSD to return the buffer. qqq.\r
282         runtime->externalRxBufferStatus = NOT_IN_USE; // We are waiting for DSD to return the buffer. qqq.\r
283 }\r
284 \r
285 // - - - - - - - - - - -\r
286 \r
287 \r
288 static inline void releaseExternalRxTxBuffersHelper(PduIdType rxPduIdRef) {\r
289         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
290         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
291         const Dcm_DslConnectionType *connection = NULL;\r
292         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
293         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
294 \r
295         if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
296                 releaseExternalRxTxBuffers(protocolRow, runtime);\r
297         }\r
298 }\r
299 \r
300 // - - - - - - - - - - -\r
301 \r
302 /*\r
303  *  This function is called from the DSD module to the DSL when\r
304  *  a response to a diagnostic request has been copied into the\r
305  *  given TX-buffer and is ready for transmission.\r
306  */\r
307 void DslDsdProcessingDone(PduIdType rxPduIdRef, DsdProcessingDoneResultType responseResult) {\r
308         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
309         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
310         const Dcm_DslConnectionType *connection = NULL;\r
311         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
312         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
313 \r
314         DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone rxPduIdRef=%d\n", rxPduIdRef);\r
315 \r
316         if (findRxPduIdParentConfigurationLeafs(rxPduIdRef, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
317                 imask_t state = McuE_EnterCriticalSection();\r
318                 switch (responseResult) {\r
319                 case DSD_TX_RESPONSE_READY:\r
320                         runtime->externalTxBufferStatus = DSD_PENDING_RESPONSE_SIGNALED; /** @req DCM114 */\r
321                         break;\r
322                 case DSD_TX_RESPONSE_SUPPRESSED: /** @req DCM238 */\r
323                         DEBUG( DEBUG_MEDIUM, "DslDsdProcessingDone called with DSD_TX_RESPONSE_SUPPRESSED.\n");\r
324                         releaseExternalRxTxBuffersHelper(rxPduIdRef);\r
325                         break;\r
326                 default:\r
327                         DEBUG( DEBUG_MEDIUM, "Unknown response result from DslDsdProcessingDone!\n");\r
328                         break;\r
329                 }\r
330                 McuE_ExitCriticalSection(state);\r
331         }\r
332 }\r
333 \r
334 // - - - - - - - - - - -\r
335 \r
336 /*\r
337  *      This function preparing transmission of response\r
338  *      pending message to tester.\r
339  */\r
340 static void sendResponse(const Dcm_DslProtocolRowType *protocol,\r
341                 Dcm_NegativeResponseCodeType responseCode) {\r
342         //Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
343         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
344         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
345         const Dcm_DslConnectionType *connection = NULL;\r
346         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
347         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
348 \r
349         /** @req DCM119 */\r
350         imask_t state = McuE_EnterCriticalSection();\r
351         if (findRxPduIdParentConfigurationLeafs(protocol->DslRunTimeProtocolParameters->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
352                 if (runtime->localTxBuffer.status == NOT_IN_USE) {\r
353                         runtime->localTxBuffer.status = PROVIDED_TO_DSD;\r
354                         runtime->localTxBuffer.buffer[0] = SID_NEGATIVE_RESPONSE;\r
355                         runtime->localTxBuffer.buffer[1] = protocol->DslProtocolRxBufferID->pduInfo.SduDataPtr[2];\r
356                         runtime->localTxBuffer.buffer[2] = responseCode;\r
357                         runtime->localTxBuffer.PduInfo.SduDataPtr = runtime->localTxBuffer.buffer;\r
358                         runtime->localTxBuffer.PduInfo.SduLength = 3;\r
359                         runtime->localTxBuffer.status = DCM_TRANSMIT_SIGNALED; // In the DslProvideTxBuffer 'callback' this state signals it is the local buffer we are interested in sending.\r
360                         PduR_DcmTransmit(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, &(runtime->localTxBuffer.PduInfo));/** @req DCM115.Partially */ /* The P2ServerMin has not been implemented. */\r
361                 }\r
362         }\r
363         McuE_ExitCriticalSection(state);\r
364 }\r
365 \r
366 // - - - - - - - - - - -\r
367 \r
368 static Std_ReturnType StartProtocolHelper(Dcm_ProtocolType protocolId) {\r
369         Std_ReturnType ret = E_NOT_OK;\r
370         uint16 i;\r
371 \r
372         for (i = 0; !DCM_Config.Dsl->DslCallbackDCMRequestService[i].Arc_EOL; i++) {\r
373                 if (DCM_Config.Dsl->DslCallbackDCMRequestService[i].StartProtocol != NULL) {\r
374                         ret = DCM_Config.Dsl->DslCallbackDCMRequestService[i]. StartProtocol(protocolId);\r
375                         if (ret != E_OK) {\r
376                                 break;\r
377                         }\r
378                 }\r
379         }\r
380         return ret;\r
381 }\r
382 \r
383 // - - - - - - - - - - -\r
384 \r
385 static boolean isTesterPresentCommand(const PduInfoType *rxPdu) {\r
386         boolean ret = FALSE;\r
387         if ((rxPdu->SduDataPtr[0] == SID_TESTER_PRESENT) && (rxPdu->SduDataPtr[1] & SUPPRESS_POS_RESP_BIT)) {\r
388                 return TRUE;\r
389         }\r
390         return ret;\r
391 }\r
392 \r
393 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
394 //      Implements 'void Dcm_Init(void)' for DSL.\r
395 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
396 void DslInit(void) {\r
397         const Dcm_DslProtocolRowType *listEntry = NULL;\r
398         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
399 \r
400         listEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
401         while (listEntry->Arc_EOL == FALSE) {\r
402                 runtime = listEntry->DslRunTimeProtocolParameters;\r
403                 runtime->externalRxBufferStatus = DCM_IDLE;\r
404                 runtime->externalTxBufferStatus = DCM_IDLE;\r
405                 runtime->localRxBuffer.status = DCM_IDLE;\r
406                 runtime->localTxBuffer.status = DCM_IDLE;\r
407                 runtime->securityLevel = DCM_SEC_LEV_LOCKED; /** @req DCM033 */\r
408                 runtime->sessionControl = DCM_DEFAULT_SESSION; /** @req DCM034 */\r
409                 listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
410                 listEntry->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
411                 listEntry++;\r
412         };\r
413         //DcmDslRunTimeData.diagnosticRequestPending = FALSE;\r
414         DcmDslRunTimeData.initRun = TRUE;\r
415 }\r
416 \r
417 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
418 //      Implements 'void Dcm_MainFunction(void)' for DSL.\r
419 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
420 \r
421 void DslMain(void) {\r
422         const Dcm_DslProtocolRowType *protocolRowEntry = NULL;\r
423         const Dcm_DslProtocolTimingRowType *timeParams = NULL;\r
424         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
425         int debug_count = 0;\r
426 \r
427         protocolRowEntry = DCM_Config.Dsl->DslProtocol->DslProtocolRowList;\r
428         while (protocolRowEntry->Arc_EOL == FALSE) {\r
429                 runtime = protocolRowEntry->DslRunTimeProtocolParameters;\r
430                 if (runtime != NULL) {\r
431                         // #### HANDLE THE TESTER PRESENT PRESENCE ####\r
432                         if (runtime->sessionControl != DCM_DEFAULT_SESSION) { // Timeout if tester present is lost.\r
433                                 DECREMENT(runtime->S3ServerTimeoutCount);\r
434                                 if (runtime->S3ServerTimeoutCount == 0) {\r
435                                         changeDiagnosticSession(runtime, DCM_DEFAULT_SESSION); /** @req DCM140 */\r
436                                 }\r
437                         }\r
438                         switch (runtime->externalTxBufferStatus) { // #### TX buffer state. ####\r
439                         case NOT_IN_USE:\r
440                                 //DEBUG( DEBUG_MEDIUM, "state NOT_IN_USE!\n");\r
441                                 break;\r
442                         case PROVIDED_TO_DSD: {\r
443                                 DEBUG( DEBUG_MEDIUM, "debug_count=%d\n", debug_count);\r
444                                 DECREMENT(runtime->stateTimeoutCount);\r
445                                 if (runtime->stateTimeoutCount == 0) {\r
446                                         DEBUG( DEBUG_MEDIUM, "State PROVIDED_TO_DSD timed out!", debug_count);\r
447                                         timeParams = protocolRowEntry->DslProtocolTimeLimit;\r
448                                         runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* Reinitiate timer, see 9.2.2. */\r
449                                         if (DCM_Config.Dsl->DslDiagResp != NULL) {\r
450                                                 if (DCM_Config.Dsl->DslDiagResp->DslDiagRespForceRespPendEn == TRUE) {\r
451                                                         if (runtime->responsePendingCount != 0) {\r
452                                                                 sendResponse(protocolRowEntry, DCM_E_RESPONSEPENDING);  /** @req DCM024 */\r
453                                                                 DECREMENT( runtime->responsePendingCount );\r
454                                                         } else {\r
455                                                                 sendResponse(protocolRowEntry, DCM_E_GENERALREJECT); /** @req DCM120 */\r
456                                                                 releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
457                                                         }\r
458                                                 } else {\r
459                                                         DEBUG( DEBUG_MEDIUM, "Not configured to send response pending, now sending general reject!\n");\r
460                                                         sendResponse(protocolRowEntry, DCM_E_GENERALREJECT);\r
461                                                         releaseExternalRxTxBuffers(protocolRowEntry, runtime);\r
462                                                 }\r
463                                         }\r
464                                 }\r
465                                 break;\r
466                         }\r
467                         case DSD_PENDING_RESPONSE_SIGNALED:\r
468                                 // The DSD has signaled to DSL that the diagnostic response is available in the Tx buffer.\r
469                                 // Make sure that response pending or general reject have not been issued,\r
470                                 // if so we can not transmit to PduR because we would not know from where\r
471                                 // the Tx confirmation resides later.\r
472                                 DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
473                                 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
474                                         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
475                                         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
476                                         const Dcm_DslConnectionType *connection = NULL;\r
477                                         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
478                                         if (findRxPduIdParentConfigurationLeafs(runtime->diagReqestRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
479                                                 const uint32 txPduId = mainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
480                                                 DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DCM_TRANSMIT_SIGNALED.\n" );\r
481                                                 runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
482                                                 PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
483                                         } else {\r
484                                                 DEBUG( DEBUG_MEDIUM, "***** WARNING, THIS IS UNEXPECTED !!! ********.\n" );\r
485                                                 const uint32 txPduId = protocolRowEntry->DslConnection->DslMainConnection->DslProtocolTx->DcmDslProtocolTxPduId;\r
486                                                 DEBUG( DEBUG_MEDIUM, "runtime->externalTxBufferStatus enter state DSD_PENDING_RESPONSE_SIGNALED.\n", txPduId);\r
487                                                 runtime->externalTxBufferStatus = DCM_TRANSMIT_SIGNALED;\r
488                                                 DEBUG( DEBUG_MEDIUM, "Calling PduR_DcmTransmit with txPduId = %d from DslMain\n", txPduId);\r
489                                                 PduR_DcmTransmit(txPduId, &runtime->diagnosticResponseFromDsd); /** @req DCM237 *//* Will trigger PduR (CanTP) to call DslProvideTxBuffer(). */\r
490                                         }\r
491                                 }\r
492                                 break;\r
493                         case DCM_TRANSMIT_SIGNALED:\r
494                                 //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
495                                 break;\r
496                         case PROVIDED_TO_PDUR: // The valid data is being transmitted by TP-layer.\r
497                                 //DEBUG( DEBUG_MEDIUM, "state DSD_PENDING_RESPONSE_SIGNALED!\n");\r
498                                 break;\r
499                         default:\r
500                                 break;\r
501                         }\r
502                 }\r
503                 protocolRowEntry++;\r
504                 debug_count++;\r
505         }\r
506 }\r
507 \r
508 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
509 //      Implements 'BufReq_ReturnType Dcm_ProvideRxBuffer(PduIdType dcmRxPduId,\r
510 //  PduLengthType tpSduLength, PduInfoType **pduInfoPtr)'.\r
511 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
512 //  This function is called called by the PduR typically when CanTp has\r
513 //  received a FF or a single frame and needs to obtain a buffer from the\r
514 //  receiver so that received data can be forwarded.\r
515 \r
516 BufReq_ReturnType DslProvideRxBufferToPdur(PduIdType dcmRxPduId,\r
517                 PduLengthType tpSduLength, const PduInfoType **pduInfoPtr) {\r
518         BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
519         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
520         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
521         const Dcm_DslConnectionType *connection = NULL;\r
522         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
523         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
524 \r
525         DEBUG( DEBUG_MEDIUM, "DslProvideRxBufferToPdur(dcmRxPduId=%d) called!\n", dcmRxPduId);\r
526         imask_t state = McuE_EnterCriticalSection();\r
527         if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
528                 const Dcm_DslBufferType *externalRxBuffer = protocolRow->DslProtocolRxBufferID;\r
529                 if (externalRxBuffer->pduInfo.SduLength >= tpSduLength) { /** @req DCM443 */\r
530                         if ((runtime->externalRxBufferStatus == NOT_IN_USE) && (externalRxBuffer->externalBufferRuntimeData->status == BUFFER_AVAILABLE)) {\r
531                                 DEBUG( DEBUG_MEDIUM, "External buffer available!\n");\r
532                                 // ### EXTERNAL BUFFER IS AVAILABLE; GRAB IT AND REMEBER THAT WE OWN IT! ###\r
533                                 externalRxBuffer->externalBufferRuntimeData->status = BUFFER_BUSY;\r
534                                 runtime->diagnosticRequestFromTester.SduDataPtr = externalRxBuffer->pduInfo.SduDataPtr;\r
535                                 runtime->diagnosticRequestFromTester.SduLength = tpSduLength;\r
536                                 *pduInfoPtr = &(runtime->diagnosticRequestFromTester);\r
537                                 runtime->externalRxBufferStatus = PROVIDED_TO_PDUR; /** @req DCM342 */\r
538                                 ret = BUFREQ_OK;\r
539                         } else {\r
540                                 DEBUG( DEBUG_MEDIUM, "Local buffer available!\n");\r
541                                 if (runtime->externalRxBufferStatus == PROVIDED_TO_DSD) {\r
542                                         // ### EXTERNAL BUFFER IS IN USE BY THE DSD, TRY TO USE LOCAL BUFFER! ###\r
543                                         if (runtime->localRxBuffer.status == NOT_IN_USE) {\r
544                                                 if (tpSduLength < DCM_DSL_LOCAL_BUFFER_LENGTH) {\r
545                                                         runtime->localRxBuffer.status = PROVIDED_TO_PDUR;\r
546                                                         runtime->localRxBuffer.PduInfo.SduDataPtr = runtime->localRxBuffer.buffer;\r
547                                                         runtime->localRxBuffer.PduInfo.SduLength = tpSduLength;\r
548                                                         *pduInfoPtr = &(runtime->localRxBuffer.PduInfo);\r
549                                                         ret = BUFREQ_OK;\r
550                                                 } else {\r
551                                                         ret = BUFREQ_BUSY;\r
552                                                 }\r
553                                         }\r
554                                 } else {\r
555                                         // The buffer is in use by the PduR, we can not help this because then\r
556                                         // we would have two different Rx-indications with same PduId but we\r
557                                         // will not know which buffer the indication should free.\r
558                                         ret = BUFREQ_BUSY; /** @req DCM445 */\r
559                                 }\r
560                         }\r
561                 } else {\r
562                         ret = BUFREQ_OVFL; /** @req DCM444 */\r
563                 }\r
564                 if (ret == BUFREQ_OK) {\r
565                         stopS3SessionTimer(runtime); /** @req DCM141 */\r
566                 }\r
567         }\r
568         McuE_ExitCriticalSection(state);\r
569         return ret;\r
570 }\r
571 \r
572 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
573 //      Implements 'void Dcm_RxIndication(PduIdType dcmRxPduId, NotifResultType result)'.\r
574 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
575 //      This function is called called by the PduR typically when CanTp has\r
576 //      received the diagnostic request, copied it to the provided buffer and need to indicate\r
577 //      this to the DCM (DSL) module via proprietary API.\r
578 \r
579 void DslRxIndicationFromPduR(PduIdType dcmRxPduId, NotifResultType result) {\r
580         const Dcm_DslProtocolRxType *protocolRx = NULL;\r
581         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
582         const Dcm_DslConnectionType *connection = NULL;\r
583         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
584         const Dcm_DslProtocolTimingRowType *timeParams = NULL;\r
585         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
586         Std_ReturnType higherLayerResp;\r
587         imask_t state;\r
588 \r
589         /** @req DCM345, this needs to be verified when connection to CanIf works. */\r
590 \r
591         if (findRxPduIdParentConfigurationLeafs(dcmRxPduId, &protocolRx, &mainConnection, &connection, &protocolRow, &runtime)) {\r
592                 timeParams = protocolRow->DslProtocolTimeLimit;\r
593                 // We need to find out in what buffer we can find our Rx data (it can\r
594                 // be either in the normal RX-buffer or the 'extra' buffer for implementing\r
595                 // the Concurrent "Test Present" functionality.\r
596                 state = McuE_EnterCriticalSection();\r
597                 if (runtime->externalRxBufferStatus == PROVIDED_TO_PDUR) {\r
598                         if ( result == NTFRSLT_OK ) { /** @req DCM111 */\r
599                                 if (isTesterPresentCommand(&(protocolRow->DslProtocolRxBufferID->pduInfo))) {\r
600                                         startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
601                                         runtime->externalRxBufferStatus = NOT_IN_USE;\r
602                                         protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
603                                 } else {\r
604                                         if (runtime->protocolStarted == FALSE) {\r
605                                                 higherLayerResp = StartProtocolHelper(protocolRow->DslProtocolID); /** @req DCM036 */\r
606                                                 if (higherLayerResp == E_OK) {\r
607                                                         runtime->protocolStarted = TRUE;\r
608                                                         DcmDslRunTimeData.activeProtocol = protocolRow;\r
609                                                 }\r
610                                         }\r
611                                         if (runtime->protocolStarted == TRUE) {\r
612                                                 if (runtime->diagnosticActiveComM == FALSE) {\r
613                                                         ComM_DCM_ActivateDiagnostic(); /** @req DCM163 */\r
614                                                         runtime->diagnosticActiveComM = TRUE;\r
615                                                 }\r
616                                                 timeParams = protocolRow->DslProtocolTimeLimit;\r
617                                                 runtime->stateTimeoutCount = DCM_CONVERT_MS_TO_MAIN_CYCLES(timeParams->TimStrP2ServerMax); /* See 9.2.2. */\r
618                                                 runtime->externalRxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
619                                                 if (runtime->externalTxBufferStatus == NOT_IN_USE) {\r
620                                                         DEBUG( DEBUG_MEDIUM, "External Tx buffer available, we can pass it to DSD.\n");\r
621                                                 } else {\r
622                                                         DEBUG( DEBUG_MEDIUM, "External buffer not available, a response is being transmitted?\n");\r
623                                                 }\r
624                                                 runtime->externalTxBufferStatus = PROVIDED_TO_DSD; /** @req DCM241 */\r
625                                                 runtime->responsePendingCount = DCM_Config.Dsl->DslDiagResp->DslDiagRespMaxNumRespPend;\r
626                                                 runtime->diagnosticResponseFromDsd.SduDataPtr = protocolRow->DslProtocolTxBufferID->pduInfo.SduDataPtr;\r
627                                                 runtime->diagnosticResponseFromDsd.SduLength = protocolRow->DslProtocolTxBufferID->pduInfo.SduLength;\r
628                                                 DEBUG( DEBUG_MEDIUM, "DsdDslDataIndication(DcmDslProtocolTxPduId=%d, dcmRxPduId=%d)\n", mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, dcmRxPduId);\r
629                                                 runtime->diagReqestRxPduId = dcmRxPduId;\r
630                                                 DsdDslDataIndication(  // qqq: We are inside a critical section.\r
631                                                                 &(runtime->diagnosticRequestFromTester),\r
632                                                                 protocolRow->DslProtocolSIDTable,       /** @req DCM035 */\r
633                                                                 protocolRx->DslProtocolAddrType,\r
634                                                                 mainConnection->DslProtocolTx->DcmDslProtocolTxPduId,\r
635                                                                 &(runtime->diagnosticResponseFromDsd),\r
636                                                                 dcmRxPduId);\r
637                                         }\r
638                                 }\r
639                         } else { /** @req DCM344 */\r
640                                 // The indication was not equal to NTFRSLT_OK, release the resources and no forward to DSD.\r
641                                 runtime->externalRxBufferStatus = NOT_IN_USE;\r
642                                 protocolRow->DslProtocolRxBufferID->externalBufferRuntimeData->status = BUFFER_AVAILABLE;\r
643                         }\r
644                 } else {\r
645                         // It is the local buffer that was provided to the PduR, that buffer\r
646                         // is only used for tester present reception in parallel to diagnostic\r
647                         // requests.\r
648                         if (runtime->localRxBuffer.status == PROVIDED_TO_PDUR) {\r
649                                 if ( result == NTFRSLT_OK ) { // Make sure that the data in buffer is valid.\r
650                                         if (isTesterPresentCommand(&(runtime->localRxBuffer.PduInfo))) {\r
651                                                 startS3SessionTimer(runtime, protocolRow); /** @req DCM141 *//** @req DCM112 *//** @req DCM113 */\r
652                                         }\r
653                                 }\r
654                                 runtime->localRxBuffer.status = NOT_IN_USE;\r
655                         }\r
656                 }\r
657                 McuE_ExitCriticalSection(state);\r
658         }\r
659 }\r
660 \r
661 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
662 //      Implements 'BufReq_ReturnType Dcm_ProvideTxBuffer(PduIdType dcmTxPduId,\r
663 //  PduInfoType **pduInfoPtr, PduLengthType length)'.\r
664 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r
665 //  This TX-buffer request is likely triggered by the transport layer (i.e. CanTp)\r
666 //  after PduR_DcmTransmit() has been called (via PduR to CanTp) indicating that something\r
667 //  is to be sent. The PduR_DcmTransmit() call is done from the DSL main function when\r
668 //  it has detected that the pending request has been answered by DSD\r
669 //  (or any other module?).\r
670 \r
671 BufReq_ReturnType DslProvideTxBuffer(PduIdType dcmTxPduId, const PduInfoType **pduInfoPtr, PduLengthType length) {\r
672         BufReq_ReturnType ret = BUFREQ_NOT_OK;\r
673         const Dcm_DslProtocolTxType *protocolTx = NULL;\r
674         const Dcm_DslMainConnectionType *mainConnection = NULL;\r
675         const Dcm_DslConnectionType *connection = NULL;\r
676         const Dcm_DslProtocolRowType *protocolRow = NULL;\r
677         Dcm_DslRunTimeProtocolParametersType *runtime = NULL;\r
678 \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                         ComM_DCM_InactivateDiagnostic(); /** @req DCM164 */\r
738                         startS3SessionTimer(runtime, protocolRow); /** @req DCM141 */\r
739                         releaseExternalRxTxBuffers(protocolRow, runtime); /** @req DCM118 *//** @req DCM352 *//** @req DCM353 *//** @req DCM354 */\r
740                         externalBufferReleased = TRUE;\r
741                         DEBUG( DEBUG_MEDIUM, "Released external buffer OK!\n");\r
742                         DsdDataConfirmation(mainConnection->DslProtocolTx->DcmDslProtocolTxPduId, result); /** @req DCM117 *//** @req DCM235 */\r
743                         break;\r
744                 }\r
745                 default:\r
746                         break;\r
747                 }\r
748                 if (externalBufferReleased == FALSE) {\r
749                         switch (runtime->localTxBuffer.status) { // ### LOCAL TX BUFFER ###\r
750                         case PROVIDED_TO_PDUR:\r
751                                 DEBUG( DEBUG_MEDIUM, "Released local buffer buffer OK!\n");\r
752                                 runtime->localTxBuffer.status = DCM_IDLE;\r
753                                 break;\r
754                         default:\r
755                                 DEBUG( DEBUG_MEDIUM, "WARNING! DslTxConfirmation could not release external or local buffer!\n");\r
756                                 break;\r
757                         }\r
758                 }\r
759                 McuE_ExitCriticalSection(state);\r
760         }\r
761 }\r
762 \r