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