]> rtime.felk.cvut.cz Git - arc.git/blob - diagnostic/Dem/Dem.c
Merge with 16ca58066fba6a270838b7833397c094ec097f43
[arc.git] / diagnostic / Dem / Dem.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 \r
18 \r
19 \r
20 /*\r
21  *  General requirements\r
22  */\r
23 /** @req DEM126 */\r
24 /** @req DEM151.partially */\r
25 /** @req DEM152 */\r
26 /** @req DEM013.14229-1 */\r
27 /** @req DEM277 */\r
28 /** @req DEM363 */\r
29 /** @req DEM113 */ /** @req DEM174 */\r
30 /** @req DEM286 */\r
31 /** @req DEM267 */\r
32 /** @req DEM364 */\r
33 /** @req DEM114 */\r
34 /** @req DEM124 */\r
35 /** @req DEM370 */\r
36 \r
37 \r
38 \r
39 #include <string.h>\r
40 #include "Dem.h"\r
41 //#include "Fim.h"\r
42 //#include "Nvm.h"\r
43 //#include "SchM_Dem.h"\r
44 #include "MemMap.h"\r
45 #include "McuExtensions.h"\r
46 \r
47 /*\r
48  * Local defines\r
49  */\r
50 #define DEBOUNCE_FDC_TEST_FAILED  127\r
51 #define DEBOUNCE_FDC_TEST_PASSED -128\r
52 \r
53 #if  ( DEM_DEV_ERROR_DETECT == STD_ON )\r
54 #include "Det.h"\r
55 /** @req DEM117 */\r
56 #define VALIDATE(_exp,_api,_err ) \\r
57         if( !(_exp) ) { \\r
58           Det_ReportError(MODULE_ID_DEM, 0, _api, _err); \\r
59           return E_NOT_OK; \\r
60         }\r
61 \r
62 #define VALIDATE_RV(_exp,_api,_err,_rv ) \\r
63         if( !(_exp) ) { \\r
64           Det_ReportError(MODULE_ID_DEM, 0, _api, _err); \\r
65           return _rv; \\r
66         }\r
67 \r
68 #define VALIDATE_NO_RV(_exp,_api,_err ) \\r
69   if( !(_exp) ) { \\r
70           Det_ReportError(MODULE_ID_DEM, 0, _api, _err); \\r
71           return; \\r
72         }\r
73 #define DET_REPORTERROR(_x,_y,_z,_q) Det_ReportError(_x, _y, _z, _q)\r
74 \r
75 #else\r
76 #define VALIDATE(_exp,_api,_err )\r
77 #define VALIDATE_RV(_exp,_api,_err,_rv )\r
78 #define VALIDATE_NO_RV(_exp,_api,_err )\r
79 #define DET_REPORTERROR(_x,_y,_z,_q)\r
80 #endif\r
81 \r
82 \r
83 /*\r
84  * Local types\r
85  */\r
86 \r
87 typedef uint16 ChecksumType;\r
88 \r
89 // DtcFilterType\r
90 typedef struct {\r
91         Dem_EventStatusExtendedType dtcStatusMask;\r
92         Dem_DTCKindType                         dtcKind;\r
93         Dem_DTCOriginType                       dtcOrigin;\r
94         Dem_FilterWithSeverityType      filterWithSeverity;\r
95         Dem_DTCSeverityType                     dtcSeverityMask;\r
96         Dem_FilterForFDCType            filterForFaultDetectionCounter;\r
97         uint16                                          faultIndex;\r
98 } DtcFilterType;\r
99 \r
100 // DisableDtcStorageType\r
101 typedef struct {\r
102         boolean                                         storageDisabled;\r
103         Dem_DTCGroupType                        dtcGroup;\r
104         Dem_DTCKindType                         dtcKind;\r
105 } DisableDtcStorageType;\r
106 \r
107 // For keeping track of the events status\r
108 typedef struct {\r
109         Dem_EventIdType                         eventId;\r
110         const Dem_EventParameterType *eventParamRef;\r
111         sint8                                           faultDetectionCounter;\r
112         uint16                                          occurrence;                             /** @req DEM011 */\r
113         Dem_EventStatusExtendedType     eventStatusExtended;    /** @req DEM006 */\r
114         boolean                                         errorStatusChanged;\r
115 } EventStatusRecType;\r
116 \r
117 \r
118 // Types for storing different event data on event memory\r
119 typedef struct {\r
120         Dem_EventIdType         eventId;\r
121         uint16                          occurrence;\r
122         ChecksumType            checksum;\r
123 } EventRecType;\r
124 \r
125 typedef struct {\r
126         Dem_EventIdType         eventId;\r
127         uint16                          occurrence;\r
128         uint16                          dataSize;\r
129         sint8                           data[DEM_MAX_SIZE_FF_DATA];\r
130         ChecksumType            checksum;\r
131 } FreezeFrameRecType;\r
132 \r
133 typedef struct {\r
134         Dem_EventIdType         eventId;\r
135         uint16                          dataSize;\r
136         uint8                           data[DEM_MAX_SIZE_EXT_DATA];\r
137         ChecksumType            checksum;\r
138 } ExtDataRecType;\r
139 \r
140 \r
141 // State variable\r
142 typedef enum\r
143 {\r
144   DEM_UNINITIALIZED = 0,\r
145   DEM_PREINITIALIZED,\r
146   DEM_INITIALIZED\r
147 } Dem_StateType; /** @req DEM169 */\r
148 \r
149 \r
150 static Dem_StateType demState = DEM_UNINITIALIZED;\r
151 \r
152 // Help pointer to configuration set\r
153 static const Dem_ConfigSetType *configSet;\r
154 \r
155 /*\r
156  * Allocation of DTC filter parameters\r
157  */\r
158 static DtcFilterType dtcFilter;\r
159 \r
160 /*\r
161  * Allocation of Disable/Enable DTC storage parameters\r
162  */\r
163 static DisableDtcStorageType disableDtcStorage;\r
164 \r
165 /*\r
166  * Allocation of operation cycle state list\r
167  */\r
168 static Dem_OperationCycleStateType operationCycleStateList[DEM_OPERATION_CYCLE_ID_ENDMARK];\r
169 \r
170 /*\r
171  * Allocation of local event status buffer\r
172  */\r
173 static EventStatusRecType       eventStatusBuffer[DEM_MAX_NUMBER_EVENT];\r
174 \r
175 /*\r
176  * Allocation of pre-init event memory (used between pre-init and init)\r
177  */\r
178 /** @req DEM207 */\r
179 static FreezeFrameRecType       preInitFreezeFrameBuffer[DEM_MAX_NUMBER_FF_DATA_PRE_INIT];\r
180 static ExtDataRecType           preInitExtDataBuffer[DEM_MAX_NUMBER_EXT_DATA_PRE_INIT];\r
181 \r
182 /*\r
183  * Allocation of primary event memory ramlog (after init) in uninitialized memory\r
184  */\r
185 /** @req DEM162 */\r
186 static EventRecType             priMemEventBuffer[DEM_MAX_NUMBER_EVENT_PRI_MEM] __attribute__ ((section (".dem_eventmemory_pri")));\r
187 static FreezeFrameRecType       priMemFreezeFrameBuffer[DEM_MAX_NUMBER_FF_DATA_PRI_MEM] __attribute__ ((section (".dem_eventmemory_pri")));\r
188 static ExtDataRecType           priMemExtDataBuffer[DEM_MAX_NUMBER_EXT_DATA_PRI_MEM] __attribute__ ((section (".dem_eventmemory_pri")));\r
189 \r
190 \r
191 /*\r
192  * Procedure:   zeroPriMemBuffers\r
193  * Description: Fill the primary buffers with zeroes\r
194  */\r
195 void demZeroPriMemBuffers(void)\r
196 {\r
197         memset(priMemEventBuffer, 0, sizeof(priMemEventBuffer));\r
198         memset(priMemFreezeFrameBuffer, 0, sizeof(priMemFreezeFrameBuffer));\r
199         memset(priMemExtDataBuffer, 0, sizeof(priMemExtDataBuffer));\r
200 }\r
201 \r
202 \r
203 /*\r
204  * Procedure:   calcChecksum\r
205  * Description: Calculate checksum over *data to *(data+nrOfBytes-1) area\r
206  */\r
207 static ChecksumType calcChecksum(void *data, uint16 nrOfBytes)\r
208 {\r
209         uint16 i;\r
210         uint8 *ptr = (uint8*)data;\r
211         ChecksumType sum = 0;\r
212 \r
213         for (i = 0; i < nrOfBytes; i++)\r
214                 sum += *ptr++;\r
215         sum ^= 0xaaaa;\r
216         return sum;\r
217 }\r
218 \r
219 \r
220 /*\r
221  * Procedure:   checkDtcKind\r
222  * Description: Return TRUE if "dtcKind" match the events DTCKind or "dtcKind"\r
223  *                              is "DEM_DTC_KIND_ALL_DTCS" otherwise FALSE.\r
224  */\r
225 static boolean checkDtcKind(Dem_DTCKindType dtcKind, const Dem_EventParameterType *eventParam)\r
226 {\r
227         boolean result = FALSE;\r
228 \r
229         if (dtcKind == DEM_DTC_KIND_ALL_DTCS) {\r
230                 result = TRUE;\r
231         }\r
232         else {\r
233                 if (eventParam->DTCClassRef != NULL) {\r
234                         if (eventParam->DTCClassRef->DTCKind == dtcKind) {\r
235                                 result = TRUE;\r
236                         }\r
237                 }\r
238         }\r
239         return result;\r
240 }\r
241 \r
242 \r
243 /*\r
244  * Procedure:   checkDtcGroup\r
245  * Description: Return TRUE if "dtc" match the events DTC or "dtc" is\r
246  *                              "DEM_DTC_GROUP_ALL_DTCS" otherwise FALSE.\r
247  */\r
248 static boolean checkDtcGroup(uint32 dtc, const Dem_EventParameterType *eventParam)\r
249 {\r
250         boolean result = FALSE;\r
251 \r
252         if (dtc == DEM_DTC_GROUP_ALL_DTCS) {\r
253                 result = TRUE;\r
254         }\r
255         else {\r
256                 if (eventParam->DTCClassRef != NULL) {\r
257                         if (eventParam->DTCClassRef->DTC == dtc) {\r
258                                 result = TRUE;\r
259                         }\r
260                 }\r
261         }\r
262         return result;\r
263 }\r
264 \r
265 \r
266 /*\r
267  * Procedure:   checkDtcOrigin\r
268  * Description: Return TRUE if "dtcOrigin" match any of the events DTCOrigin otherwise FALSE.\r
269  */\r
270 static boolean checkDtcOrigin(Dem_DTCOriginType dtcOrigin, const Dem_EventParameterType *eventParam)\r
271 {\r
272         boolean result = FALSE;\r
273         uint16 i;\r
274 \r
275         for (i = 0; (eventParam->EventClass->EventDestination[i] != dtcOrigin) && (i < DEM_MAX_NR_OF_EVENT_DESTINATION); i++);\r
276 \r
277         if (i < DEM_MAX_NR_OF_EVENT_DESTINATION) {\r
278                 result = TRUE;\r
279         }\r
280 \r
281         return result;\r
282 }\r
283 \r
284 \r
285 /*\r
286  * Procedure:   checkDtcSeverityMask\r
287  * Description: Return TRUE if "dtcSeverityMask" match any of the events DTC severity otherwise FALSE.\r
288  */\r
289 static boolean checkDtcSeverityMask(Dem_DTCSeverityType dtcSeverityMask, const Dem_EventParameterType *eventParam)\r
290 {\r
291         boolean result = TRUE;\r
292 \r
293         // TODO: This function is optional, may be implemented here.\r
294 \r
295         return result;\r
296 }\r
297 \r
298 \r
299 /*\r
300  * Procedure:   checkDtcFaultDetectionCounterMask\r
301  * Description: TBD.\r
302  */\r
303 static boolean checkDtcFaultDetectionCounter(const Dem_EventParameterType *eventParam)\r
304 {\r
305         boolean result = TRUE;\r
306 \r
307         // TODO: Not implemented yet.\r
308 \r
309         return result;\r
310 }\r
311 \r
312 \r
313 /*\r
314  * Procedure:   lookupEventStatusRec\r
315  * Description: Returns the pointer to event id parameters of "eventId" in "*eventStatusBuffer",\r
316  *                              if not found NULL is returned.\r
317  */\r
318 static void lookupEventStatusRec(Dem_EventIdType eventId, EventStatusRecType **const eventStatusRec)\r
319 {\r
320         EventStatusRecType *eventStatusRecPtr = eventStatusBuffer;\r
321 \r
322         while ((eventStatusRecPtr->eventId != eventId) && (eventStatusRecPtr < &eventStatusBuffer[DEM_MAX_NUMBER_EVENT])) {\r
323                 eventStatusRecPtr++;\r
324         }\r
325 \r
326         if (eventStatusRecPtr < &eventStatusBuffer[DEM_MAX_NUMBER_EVENT]) {\r
327                 *eventStatusRec = eventStatusRecPtr;\r
328         } else {\r
329                 *eventStatusRec = NULL;\r
330         }\r
331 }\r
332 \r
333 \r
334 /*\r
335  * Procedure:   lookupEventIdParameter\r
336  * Description: Returns the pointer to event id parameters of "eventId" in "*eventIdParam",\r
337  *                              if not found NULL is returned.\r
338  */\r
339 static void lookupEventIdParameter(Dem_EventIdType eventId, const Dem_EventParameterType **const eventIdParam)\r
340 {\r
341         const Dem_EventParameterType *EventIdParamPtr = configSet->EventParameter;\r
342 \r
343         // Lookup the correct event id parameters\r
344         while ((EventIdParamPtr->EventID != eventId) && !EventIdParamPtr->Arc_EOL) {\r
345                 EventIdParamPtr++;\r
346         }\r
347 \r
348         if (!EventIdParamPtr->Arc_EOL) {\r
349                 *eventIdParam = EventIdParamPtr;\r
350         } else {\r
351                 *eventIdParam = NULL;\r
352         }\r
353 }\r
354 \r
355 \r
356 /*\r
357  * Procedure:   preDebounceNone\r
358  * Description: Returns the result of the debouncing.\r
359  */\r
360 static Dem_EventStatusType preDebounceNone(Dem_EventStatusType reportedStatus, EventStatusRecType* statusRecord) {\r
361         Dem_EventStatusType returnCode;\r
362 \r
363         switch (reportedStatus) {\r
364         case DEM_EVENT_STATUS_FAILED: /** @req DEM091.NoneFailed */\r
365         case DEM_EVENT_STATUS_PASSED: /** @req DEM091.NonePassed */\r
366                 // Already debounced, do nothing.\r
367                 break;\r
368 \r
369         default:\r
370                 // TODO: What to do with PREFAIL and PREPASSED on no debouncing?\r
371                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_PREDEBOUNCE_NONE_ID, DEM_E_PARAM_DATA);\r
372                 break;\r
373         }\r
374 \r
375         returnCode = reportedStatus;\r
376         return returnCode;\r
377 }\r
378 \r
379 \r
380 /*\r
381  * Procedure:   preDebounceCounterBased\r
382  * Description: Returns the result of the debouncing.\r
383  */\r
384 static Dem_EventStatusType preDebounceCounterBased(Dem_EventStatusType reportedStatus, EventStatusRecType* statusRecord) {\r
385         Dem_EventStatusType returnCode;\r
386         const Dem_PreDebounceCounterBasedType* pdVars = statusRecord->eventParamRef->EventClass->PreDebounceAlgorithmClass->PreDebounceAlgorithm.PreDebounceCounterBased;\r
387 \r
388         switch (reportedStatus) {\r
389         case DEM_EVENT_STATUS_PREFAILED:\r
390                 if (statusRecord->faultDetectionCounter < DEBOUNCE_FDC_TEST_FAILED) {\r
391                         if (pdVars->JumpUp && (statusRecord->faultDetectionCounter < 0)) {\r
392                                 statusRecord->faultDetectionCounter = 0;\r
393                         } else {\r
394                                 if (((sint16)statusRecord->faultDetectionCounter + pdVars->CountInStepSize) < DEBOUNCE_FDC_TEST_FAILED) {\r
395                                         statusRecord->faultDetectionCounter += pdVars->CountInStepSize;\r
396                                 } else {\r
397                                         statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_FAILED;\r
398                                 }\r
399                         }\r
400                 }\r
401                 break;\r
402 \r
403         case DEM_EVENT_STATUS_PREPASSED:\r
404                 if (statusRecord->faultDetectionCounter > DEBOUNCE_FDC_TEST_PASSED) {\r
405                         if (pdVars->JumpDown && (statusRecord->faultDetectionCounter > 0)) {\r
406                                 statusRecord->faultDetectionCounter = 0;\r
407                         } else {\r
408                                 if (((sint16)statusRecord->faultDetectionCounter - pdVars->CountOutStepSize) > DEBOUNCE_FDC_TEST_PASSED) {\r
409                                         statusRecord->faultDetectionCounter -= pdVars->CountOutStepSize;\r
410                                 } else {\r
411                                         statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_PASSED;\r
412                                 }\r
413                         }\r
414                 }\r
415                 break;\r
416 \r
417         case DEM_EVENT_STATUS_FAILED:\r
418                 statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_FAILED; /** @req DEM091.CounterFailed */\r
419                 break;\r
420 \r
421         case DEM_EVENT_STATUS_PASSED:\r
422                 statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_PASSED; /** @req DEM091.CounterPassed */\r
423                 break;\r
424 \r
425         default:\r
426                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_PREDEBOUNCE_COUNTER_BASED_ID, DEM_E_PARAM_DATA);\r
427                 break;\r
428 \r
429         }\r
430 \r
431         switch (statusRecord->faultDetectionCounter) {\r
432         case DEBOUNCE_FDC_TEST_FAILED:\r
433                 returnCode = DEM_EVENT_STATUS_FAILED;\r
434                 break;\r
435 \r
436         case DEBOUNCE_FDC_TEST_PASSED:\r
437                 returnCode = DEM_EVENT_STATUS_PASSED;\r
438                 break;\r
439 \r
440         default:\r
441                 returnCode = reportedStatus;\r
442                 break;\r
443         }\r
444 \r
445         return returnCode;\r
446 }\r
447 \r
448 \r
449 /*\r
450  * Procedure:   updateEventStatusRec\r
451  * Description: Update the status of "eventId", if not exist and "createIfNotExist" is\r
452  *                              true a new record is created\r
453  */\r
454 static void updateEventStatusRec(const Dem_EventParameterType *eventParam, Dem_EventStatusType eventStatus, boolean createIfNotExist, EventStatusRecType *eventStatusRec)\r
455 {\r
456         EventStatusRecType *eventStatusRecPtr;\r
457         imask_t state = McuE_EnterCriticalSection();\r
458 \r
459         // Lookup event ID\r
460         lookupEventStatusRec(eventParam->EventID, &eventStatusRecPtr);\r
461 \r
462         if ((eventStatusRecPtr == NULL) && (createIfNotExist)) {\r
463                 // Search for free position\r
464                 lookupEventStatusRec(DEM_EVENT_ID_NULL, &eventStatusRecPtr);\r
465 \r
466                 if (eventStatusRecPtr != NULL) {\r
467                         // Create new event record\r
468                         eventStatusRecPtr->eventId = eventParam->EventID;\r
469                         eventStatusRecPtr->eventParamRef = eventParam;\r
470                         eventStatusRecPtr->faultDetectionCounter = 0;\r
471                         eventStatusRecPtr->occurrence = 0;\r
472                         eventStatusRecPtr->eventStatusExtended = DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE;\r
473                         eventStatusRecPtr->errorStatusChanged = FALSE;\r
474                 }\r
475                 else {\r
476                         // Error: Event status buffer full\r
477                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_UPDATE_EVENT_STATUS_ID, DEM_E_EVENT_STATUS_BUFF_FULL);\r
478                 }\r
479         }\r
480 \r
481 \r
482         if (eventStatusRecPtr != NULL) {\r
483                 // Handle debouncing\r
484                 if (eventParam->EventClass->PreDebounceAlgorithmClass != NULL) {\r
485                         switch (eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceName) { /** @req DEM004 */ /** @req DEM342 */\r
486                         case DEM_NO_PRE_DEBOUNCE:\r
487                                 eventStatus = preDebounceNone(eventStatus, eventStatusRecPtr);\r
488                                 break;\r
489 \r
490                         case DEM_PRE_DEBOUNCE_COUNTER_BASED:\r
491                                 eventStatus = preDebounceCounterBased(eventStatus, eventStatusRecPtr);\r
492                                 break;\r
493 \r
494                         default:\r
495                                 // Don't know how to handle this.\r
496                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_UPDATE_EVENT_STATUS_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
497                                 break;\r
498                         }\r
499                 }\r
500 \r
501                 eventStatusRecPtr->errorStatusChanged = FALSE;\r
502 \r
503                 // Check test failed\r
504                 if (eventStatus == DEM_EVENT_STATUS_FAILED) {\r
505                         if (!(eventStatusRecPtr->eventStatusExtended & DEM_TEST_FAILED)) {\r
506                                 eventStatusRecPtr->occurrence++;\r
507                                 eventStatusRecPtr->errorStatusChanged = TRUE;\r
508                         }\r
509                         /** @req DEM036 */ /** @req DEM379.PendingSet */\r
510                         eventStatusRecPtr->eventStatusExtended |= (DEM_TEST_FAILED | DEM_TEST_FAILED_THIS_OPERATION_CYCLE | DEM_TEST_FAILED_SINCE_LAST_CLEAR | DEM_PENDING_DTC);\r
511                         eventStatusRecPtr->eventStatusExtended &= ~(DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE);\r
512                 }\r
513 \r
514                 // Check test passed\r
515                 if (eventStatus == DEM_EVENT_STATUS_PASSED) {\r
516                         if (eventStatusRecPtr->eventStatusExtended & DEM_TEST_FAILED) {\r
517                                 eventStatusRecPtr->errorStatusChanged = TRUE;\r
518                         }\r
519                         /** @req DEM036 */\r
520                         eventStatusRecPtr->eventStatusExtended &= ~DEM_TEST_FAILED;\r
521                         eventStatusRecPtr->eventStatusExtended &= ~(DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE);\r
522                 }\r
523 \r
524                 // Copy the record\r
525                 memcpy(eventStatusRec, eventStatusRecPtr, sizeof(EventStatusRecType));\r
526         }\r
527         else {\r
528                 // Copy an empty record to return data\r
529                 eventStatusRec->eventId = DEM_EVENT_ID_NULL;\r
530                 eventStatusRecPtr->faultDetectionCounter = 0;\r
531                 eventStatusRec->occurrence = 0;\r
532                 eventStatusRec->eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
533                 eventStatusRec->errorStatusChanged = FALSE;\r
534         }\r
535 \r
536         McuE_ExitCriticalSection(state);\r
537 }\r
538 \r
539 \r
540 /*\r
541  * Procedure:   mergeEventStatusRec\r
542  * Description: Update the occurrence counter of status, if not exist a new record is created\r
543  */\r
544 static void mergeEventStatusRec(EventRecType *eventRec)\r
545 {\r
546         EventStatusRecType *eventStatusRecPtr;\r
547         imask_t state = McuE_EnterCriticalSection();\r
548 \r
549         // Lookup event ID\r
550         lookupEventStatusRec(eventRec->eventId, &eventStatusRecPtr);\r
551 \r
552         if (eventStatusRecPtr != NULL) {\r
553                 // Update occurrence counter, rest of pre init state is kept.\r
554                 eventStatusRecPtr->occurrence += eventRec->occurrence;\r
555 \r
556         }\r
557         else {\r
558                 // Search for free position\r
559                 lookupEventStatusRec(DEM_EVENT_ID_NULL, &eventStatusRecPtr);\r
560 \r
561                 if (eventStatusRecPtr != NULL) {\r
562                         // Create new event, from stored event\r
563                         eventStatusRecPtr->eventId = eventRec->eventId;\r
564                         lookupEventIdParameter(eventRec->eventId, &eventStatusRecPtr->eventParamRef);\r
565                         eventStatusRecPtr->faultDetectionCounter = 0;\r
566                         eventStatusRecPtr->occurrence = eventRec->occurrence;\r
567                         eventStatusRecPtr->eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
568                         eventStatusRecPtr->errorStatusChanged = FALSE;\r
569                 }\r
570                 else {\r
571                         // Error: Event status buffer full\r
572                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_MERGE_EVENT_STATUS_ID, DEM_E_EVENT_STATUS_BUFF_FULL);\r
573                 }\r
574         }\r
575 \r
576         McuE_ExitCriticalSection(state);\r
577 }\r
578 \r
579 \r
580 /*\r
581  * Procedure:   deleteEventStatusRec\r
582  * Description: Delete the status record of "eventParam->eventId" from "eventStatusBuffer".\r
583  */\r
584 static void deleteEventStatusRec(const Dem_EventParameterType *eventParam)\r
585 {\r
586         EventStatusRecType *eventStatusRecPtr;\r
587         imask_t state = McuE_EnterCriticalSection();\r
588 \r
589         // Lookup event ID\r
590         lookupEventStatusRec(eventParam->EventID, &eventStatusRecPtr);\r
591 \r
592         if (eventStatusRecPtr != NULL) {\r
593                 // Delete event record\r
594                 memset(eventStatusRecPtr, 0, sizeof(EventStatusRecType));\r
595         }\r
596 \r
597         McuE_ExitCriticalSection(state);\r
598 }\r
599 \r
600 \r
601 /*\r
602  * Procedure:   getEventStatusRec\r
603  * Description: Returns the status record of "eventId" in "eventStatusRec"\r
604  */\r
605 static void getEventStatusRec(Dem_EventIdType eventId, EventStatusRecType *eventStatusRec)\r
606 {\r
607         EventStatusRecType *eventStatusRecPtr;\r
608 \r
609         // Lookup event ID\r
610         lookupEventStatusRec(eventId, &eventStatusRecPtr);\r
611 \r
612         if (eventStatusRecPtr != NULL) {\r
613                 // Copy the record\r
614                 memcpy(eventStatusRec, eventStatusRecPtr, sizeof(EventStatusRecType));\r
615         }\r
616         else {\r
617                 eventStatusRec->eventId = DEM_EVENT_ID_NULL;\r
618         }\r
619 }\r
620 \r
621 \r
622 /*\r
623  * Procedure:   lookupDtcEvent\r
624  * Description: Returns TRUE if the DTC was found and "eventStatusRec" points\r
625  *                              to the event record found.\r
626  */\r
627 static boolean lookupDtcEvent(uint32 dtc, EventStatusRecType **eventStatusRec)\r
628 {\r
629         boolean dtcFound = FALSE;\r
630         uint16 i;\r
631 \r
632         *eventStatusRec = NULL;\r
633 \r
634         for (i = 0; (i < DEM_MAX_NUMBER_EVENT) && !dtcFound; i++) {\r
635                 if (eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) {\r
636                         if (eventStatusBuffer[i].eventParamRef->DTCClassRef != NULL) {\r
637 \r
638                                 // Check DTC\r
639                                 if (eventStatusBuffer[i].eventParamRef->DTCClassRef->DTC == dtc) {\r
640                                         *eventStatusRec = &eventStatusBuffer[i];\r
641                                         dtcFound = TRUE;\r
642                                 }\r
643                         }\r
644                 }\r
645         }\r
646 \r
647         return dtcFound;\r
648 }\r
649 \r
650 \r
651 /*\r
652  * Procedure:   matchEventWithDtcFilter\r
653  * Description: Returns TRUE if the event pointed by "event" fulfill\r
654  *                              the "dtcFilter" global filter settings.\r
655  */\r
656 static boolean matchEventWithDtcFilter(const EventStatusRecType *eventRec)\r
657 {\r
658         boolean dtcMatch = FALSE;\r
659 \r
660         // Check status\r
661         if ((dtcFilter.dtcStatusMask == DEM_DTC_STATUS_MASK_ALL) || (eventRec->eventStatusExtended & dtcFilter.dtcStatusMask)) {\r
662                 if (eventRec->eventParamRef != NULL) {\r
663 \r
664                         // Check dtcKind\r
665                         if (checkDtcKind(dtcFilter.dtcKind, eventRec->eventParamRef)) {\r
666 \r
667                                 // Check dtcOrigin\r
668                                 if (checkDtcOrigin(dtcFilter.dtcOrigin, eventRec->eventParamRef)) {\r
669 \r
670                                         // Check severity\r
671                                         if ((dtcFilter.filterWithSeverity == DEM_FILTER_WITH_SEVERITY_NO)\r
672                                                 || ((dtcFilter.filterWithSeverity == DEM_FILTER_WITH_SEVERITY_YES) && checkDtcSeverityMask(dtcFilter.dtcSeverityMask, eventRec->eventParamRef))) {\r
673 \r
674                                                 // Check fault detection counter\r
675                                                 if ((dtcFilter.filterForFaultDetectionCounter == DEM_FILTER_FOR_FDC_NO)\r
676                                                         || ((dtcFilter.filterWithSeverity == DEM_FILTER_FOR_FDC_YES) && checkDtcFaultDetectionCounter(eventRec->eventParamRef))) {\r
677                                                         dtcMatch = TRUE;\r
678                                                 }\r
679                                         }\r
680                                 }\r
681                         }\r
682                 }\r
683         }\r
684 \r
685         return dtcMatch;\r
686 }\r
687 \r
688 \r
689 static void getFreezeFrameData(const Dem_EventParameterType *eventParam, FreezeFrameRecType *freezeFrame)\r
690 {\r
691         // TODO: Fill out\r
692         freezeFrame->eventId = DEM_EVENT_ID_NULL;       // Not supported yet\r
693 }\r
694 \r
695 \r
696 static void storeFreezeFrameDataPreInit(const Dem_EventParameterType *eventParam, FreezeFrameRecType *freezeFrame)\r
697 {\r
698         // TODO: Fill out\r
699 }\r
700 \r
701 \r
702 static void updateFreezeFrameOccurrencePreInit(EventRecType *EventBuffer)\r
703 {\r
704         // TODO: Fill out\r
705 }\r
706 \r
707 \r
708 /*\r
709  * Procedure:   getExtendedData\r
710  * Description: Collects the extended data according to "eventParam" and return it in "extData",\r
711  *                              if not found eventId is set to DEM_EVENT_ID_NULL.\r
712  */\r
713 static void getExtendedData(const Dem_EventParameterType *eventParam, ExtDataRecType *extData)\r
714 {\r
715         Std_ReturnType callbackReturnCode;\r
716         uint16 i;\r
717         uint16 storeIndex = 0;\r
718         uint16 recordSize;\r
719 \r
720         // Clear ext data record\r
721         memset(extData, 0, sizeof(ExtDataRecType));\r
722 \r
723         // Check if any pointer to extended data class\r
724         if (eventParam->ExtendedDataClassRef != NULL) {\r
725                 // Request extended data and copy it to the buffer\r
726                 for (i = 0; (i < DEM_MAX_NR_OF_RECORDS_IN_EXTENDED_DATA) && (eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i] != NULL); i++) {\r
727                         recordSize = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i]->DataSize;\r
728                         if ((storeIndex + recordSize) <= DEM_MAX_SIZE_EXT_DATA) {\r
729                                 callbackReturnCode = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i]->CallbackGetExtDataRecord(&extData->data[storeIndex]); /** @req DEM282 */\r
730                                 if (callbackReturnCode != E_OK) {\r
731                                         // Callback data currently not available, clear space.\r
732                                         memset(&extData->data[storeIndex], 0xFF, recordSize);\r
733                                 }\r
734                                 storeIndex += recordSize;\r
735                         }\r
736                         else {\r
737                                 // Error: Size of extended data record is bigger than reserved space.\r
738                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GET_EXTENDED_DATA_ID, DEM_E_EXT_DATA_TOO_BIG);\r
739                                 break;  // Break the loop\r
740                         }\r
741                 }\r
742         }\r
743 \r
744         // Check if any data has been stored\r
745         if (storeIndex != 0) {\r
746                 extData->eventId = eventParam->EventID;\r
747                 extData->dataSize = storeIndex;\r
748                 extData->checksum = calcChecksum(extData, sizeof(ExtDataRecType)-sizeof(ChecksumType));\r
749         }\r
750         else {\r
751                 extData->eventId = DEM_EVENT_ID_NULL;\r
752                 extData->dataSize = storeIndex;\r
753                 extData->checksum = 0;\r
754         }\r
755 }\r
756 \r
757 \r
758 /*\r
759  * Procedure:   storeExtendedDataPreInit\r
760  * Description: Store the extended data pointed by "extendedData" to the "preInitExtDataBuffer",\r
761  *                              if non existent a new entry is created.\r
762  */\r
763 static void storeExtendedDataPreInit(const Dem_EventParameterType *eventParam, ExtDataRecType *extendedData)\r
764 {\r
765         uint16 i;\r
766         imask_t state = McuE_EnterCriticalSection();\r
767 \r
768         // Check if already stored\r
769         for (i = 0; (preInitExtDataBuffer[i].eventId != eventParam->EventID) && (i<DEM_MAX_NUMBER_EXT_DATA_PRE_INIT); i++);\r
770 \r
771         if (i < DEM_MAX_NUMBER_EXT_DATA_PRE_INIT) {\r
772                 // Yes, overwrite existing\r
773                 memcpy(&preInitExtDataBuffer[i], extendedData, sizeof(ExtDataRecType));\r
774         }\r
775         else {\r
776                 // No, lookup first free position\r
777                 for (i = 0; (preInitExtDataBuffer[i].eventId !=0) && (i<DEM_MAX_NUMBER_EXT_DATA_PRE_INIT); i++);\r
778 \r
779                 if (i < DEM_MAX_NUMBER_EXT_DATA_PRE_INIT) {\r
780                         memcpy(&preInitExtDataBuffer[i], extendedData, sizeof(ExtDataRecType));\r
781                 }\r
782                 else {\r
783                         // Error: Pre init extended data buffer full\r
784                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_STORE_EXT_DATA_PRE_INIT_ID, DEM_E_PRE_INIT_EXT_DATA_BUFF_FULL);\r
785                 }\r
786         }\r
787 \r
788         McuE_ExitCriticalSection(state);\r
789 }\r
790 \r
791 \r
792 /*\r
793  * Procedure:   storeEventPriMem\r
794  * Description: Store the event data of "eventStatus->eventId" in "priMemEventBuffer",\r
795  *                              if non existent a new entry is created.\r
796  */\r
797 static void storeEventPriMem(const Dem_EventParameterType *eventParam, EventStatusRecType *eventStatus)\r
798 {\r
799         uint16 i;\r
800         imask_t state = McuE_EnterCriticalSection();\r
801 \r
802 \r
803         // Lookup event ID\r
804         for (i = 0; (priMemEventBuffer[i].eventId != eventStatus->eventId) && (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI); i++);\r
805 \r
806         if (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) {\r
807                 // Update event found\r
808                 priMemEventBuffer[i].occurrence = eventStatus->occurrence;\r
809                 priMemEventBuffer[i].checksum = calcChecksum(&priMemEventBuffer[i], sizeof(EventRecType)-sizeof(ChecksumType));\r
810         }\r
811         else {\r
812                 // Search for free position\r
813                 for (i=0; (priMemEventBuffer[i].eventId != DEM_EVENT_ID_NULL) && (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI); i++);\r
814 \r
815                 if (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) {\r
816                         priMemEventBuffer[i].eventId = eventStatus->eventId;\r
817                         priMemEventBuffer[i].occurrence = eventStatus->occurrence;\r
818                         priMemEventBuffer[i].checksum = calcChecksum(&priMemEventBuffer[i], sizeof(EventRecType)-sizeof(ChecksumType));\r
819                 }\r
820                 else {\r
821                         // Error: Pri mem event buffer full\r
822                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_STORE_EVENT_PRI_MEM_ID, DEM_E_PRI_MEM_EVENT_BUFF_FULL);\r
823                 }\r
824         }\r
825 \r
826         McuE_ExitCriticalSection(state);\r
827 }\r
828 \r
829 \r
830 /*\r
831  * Procedure:   deleteEventPriMem\r
832  * Description: Delete the event data of "eventParam->eventId" from "priMemEventBuffer".\r
833  */\r
834 static void deleteEventPriMem(const Dem_EventParameterType *eventParam)\r
835 {\r
836         uint16 i;\r
837         imask_t state = McuE_EnterCriticalSection();\r
838 \r
839 \r
840         // Lookup event ID\r
841         for (i = 0; (priMemEventBuffer[i].eventId != eventParam->EventID) && (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI); i++);\r
842 \r
843         if (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) {\r
844                 // Delete event found\r
845                 memset(&priMemEventBuffer[i], 0, sizeof(EventRecType));\r
846         }\r
847 \r
848         McuE_ExitCriticalSection(state);\r
849 }\r
850 \r
851 \r
852 /*\r
853  * Procedure:   storeEventEvtMem\r
854  * Description: Store the event data of "eventStatus->eventId" in event memory according to\r
855  *                              "eventParam" destination option.\r
856  */\r
857 static void storeEventEvtMem(const Dem_EventParameterType *eventParam, EventStatusRecType *eventStatus)\r
858 {\r
859         uint16 i;\r
860 \r
861         for (i = 0; (i < DEM_MAX_NR_OF_EVENT_DESTINATION)\r
862                                  && (eventParam->EventClass->EventDestination[i] != DEM_EVENT_DESTINATION_END_OF_LIST); i++) {\r
863                 switch (eventParam->EventClass->EventDestination[i])\r
864                 {\r
865                 case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
866                         storeEventPriMem(eventParam, eventStatus);      /** @req DEM010 */\r
867                         break;\r
868 \r
869                 case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
870                 case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
871                 case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
872                         // Not yet supported\r
873                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GLOBAL_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
874                         break;\r
875                 default:\r
876                         break;\r
877                 }\r
878         }\r
879 }\r
880 \r
881 \r
882 /*\r
883  * Procedure:   storeExtendedDataPriMem\r
884  * Description: Store the extended data pointed by "extendedData" to the "priMemExtDataBuffer",\r
885  *                              if non existent a new entry is created.\r
886  */\r
887 static void storeExtendedDataPriMem(const Dem_EventParameterType *eventParam, ExtDataRecType *extendedData) /** @req DEM041 */\r
888 {\r
889         uint16 i;\r
890         imask_t state = McuE_EnterCriticalSection();\r
891 \r
892         // Check if already stored\r
893         for (i = 0; (priMemExtDataBuffer[i].eventId != eventParam->EventID) && (i<DEM_MAX_NUMBER_EXT_DATA_PRI_MEM); i++);\r
894 \r
895         if (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) {\r
896                 // Yes, overwrite existing\r
897                 memcpy(&priMemExtDataBuffer[i], extendedData, sizeof(ExtDataRecType));\r
898         }\r
899         else {\r
900                 // No, lookup first free position\r
901                 for (i = 0; (priMemExtDataBuffer[i].eventId != DEM_EVENT_ID_NULL) && (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM); i++);\r
902                 if (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) {\r
903                         memcpy(&priMemExtDataBuffer[i], extendedData, sizeof(ExtDataRecType));\r
904                 }\r
905                 else {\r
906                         // Error: Pri mem extended data buffer full\r
907                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_STORE_EXT_DATA_PRI_MEM_ID, DEM_E_PRI_MEM_EXT_DATA_BUFF_FULL);\r
908                 }\r
909         }\r
910 \r
911         McuE_ExitCriticalSection(state);\r
912 }\r
913 \r
914 \r
915 /*\r
916  * Procedure:   deleteExtendedDataPriMem\r
917  * Description: Delete the extended data of "eventParam->eventId" from "priMemExtDataBuffer".\r
918  */\r
919 static void deleteExtendedDataPriMem(const Dem_EventParameterType *eventParam)\r
920 {\r
921         uint16 i;\r
922         imask_t state = McuE_EnterCriticalSection();\r
923 \r
924         // Check if already stored\r
925         for (i = 0; (priMemExtDataBuffer[i].eventId != eventParam->EventID) && (i<DEM_MAX_NUMBER_EXT_DATA_PRI_MEM); i++);\r
926 \r
927         if (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) {\r
928                 // Yes, clear record\r
929                 memset(&priMemExtDataBuffer[i], 0, sizeof(ExtDataRecType));\r
930         }\r
931 \r
932         McuE_ExitCriticalSection(state);\r
933 }\r
934 \r
935 \r
936 /*\r
937  * Procedure:   storeExtendedDataEvtMem\r
938  * Description: Store the extended data in event memory according to\r
939  *                              "eventParam" destination option\r
940  */\r
941 static void storeExtendedDataEvtMem(const Dem_EventParameterType *eventParam, ExtDataRecType *extendedData)\r
942 {\r
943         uint16 i;\r
944 \r
945         for (i = 0; (i < DEM_MAX_NR_OF_EVENT_DESTINATION) && (eventParam->EventClass->EventDestination[i] != DEM_EVENT_DESTINATION_END_OF_LIST); i++) {\r
946                 switch (eventParam->EventClass->EventDestination[i])\r
947                 {\r
948                 case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
949                         storeExtendedDataPriMem(eventParam, extendedData);\r
950                         break;\r
951 \r
952                 case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
953                 case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
954                 case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
955                         // Not yet supported\r
956                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GLOBAL_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
957                         break;\r
958 \r
959                 default:\r
960                         break;\r
961                 }\r
962         }\r
963 }\r
964 \r
965 \r
966 /*\r
967  * Procedure:   lookupExtendedDataRecNumParam\r
968  * Description: Returns TRUE if the requested extended data number was found among the configured records for the event.\r
969  *                              "extDataRecClassPtr" returns a pointer to the record class, "posInExtData" returns the position in stored extended data.\r
970  */\r
971 static boolean lookupExtendedDataRecNumParam(uint8 extendedDataNumber, const Dem_EventParameterType *eventParam, Dem_ExtendedDataRecordClassType const **extDataRecClassPtr, uint8 *posInExtData)\r
972 {\r
973         boolean recNumFound = FALSE;\r
974 \r
975         if (eventParam->ExtendedDataClassRef != NULL) {\r
976                 Dem_ExtendedDataRecordClassType const* const* extDataRecClassRefList = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef;\r
977                 uint16  byteCnt = 0;\r
978                 uint16 i;\r
979 \r
980                 // Request extended data and copy it to the buffer\r
981                 for (i = 0; (i < DEM_MAX_NR_OF_RECORDS_IN_EXTENDED_DATA) && (extDataRecClassRefList[i] != NULL) && !recNumFound; i++) {\r
982                         if (extDataRecClassRefList[i]->RecordNumber == extendedDataNumber) {\r
983                                 *extDataRecClassPtr =  extDataRecClassRefList[i];\r
984                                 *posInExtData = byteCnt;\r
985                                 recNumFound = TRUE;\r
986                         }\r
987                         byteCnt += extDataRecClassRefList[i]->DataSize;\r
988                 }\r
989         }\r
990 \r
991         return recNumFound;\r
992 }\r
993 \r
994 \r
995 /*\r
996  * Procedure:   lookupExtendedDataPriMem\r
997  * Description: Returns TRUE if the requested event id is found, "extData" points to the found data.\r
998  */\r
999 static boolean lookupExtendedDataPriMem(Dem_EventIdType eventId, ExtDataRecType **extData)\r
1000 {\r
1001         boolean eventIdFound = FALSE;\r
1002         uint16 i;\r
1003 \r
1004         // Lookup corresponding extended data\r
1005         for (i = 0; (priMemExtDataBuffer[i].eventId != eventId) && (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM); i++);\r
1006 \r
1007         if (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) {\r
1008                 // Yes, return pointer\r
1009                 *extData = &priMemExtDataBuffer[i];\r
1010                 eventIdFound = TRUE;\r
1011         }\r
1012 \r
1013         return eventIdFound;\r
1014 }\r
1015 \r
1016 \r
1017 static void storeFreezeFrameDataPriMem(const Dem_EventParameterType *eventParam, FreezeFrameRecType *freezeFrame)\r
1018 {\r
1019         // TODO: Fill out\r
1020 }\r
1021 \r
1022 \r
1023 static void deleteFreezeFrameDataPriMem(const Dem_EventParameterType *eventParam)\r
1024 {\r
1025         // TODO: Fill out\r
1026 }\r
1027 \r
1028 \r
1029 /*\r
1030  * Procedure:   storeFreezeFrameDataEvtMem\r
1031  * Description: Store the freeze frame data in event memory according to\r
1032  *                              "eventParam" destination option\r
1033  */\r
1034 static void storeFreezeFrameDataEvtMem(const Dem_EventParameterType *eventParam, FreezeFrameRecType *freezeFrame)\r
1035 {\r
1036         uint16 i;\r
1037 \r
1038         for (i = 0; (i < DEM_MAX_NR_OF_EVENT_DESTINATION) && (eventParam->EventClass->EventDestination[i] != DEM_EVENT_DESTINATION_END_OF_LIST); i++) {\r
1039                 switch (eventParam->EventClass->EventDestination[i])\r
1040                 {\r
1041                 case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
1042                         storeFreezeFrameDataPriMem(eventParam, freezeFrame);\r
1043                         break;\r
1044 \r
1045                 case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
1046                 case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
1047                 case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
1048                         // Not yet supported\r
1049                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GLOBAL_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
1050                         break;\r
1051                 default:\r
1052                         break;\r
1053                 }\r
1054         }\r
1055 }\r
1056 \r
1057 \r
1058 /*\r
1059  * Procedure:   handlePreInitEvent\r
1060  * Description: Handle the updating of event status and storing of\r
1061  *                              event related data in preInit buffers.\r
1062  */\r
1063 static void handlePreInitEvent(Dem_EventIdType eventId, Dem_EventStatusType eventStatus)\r
1064 {\r
1065         const Dem_EventParameterType *eventParam;\r
1066         EventStatusRecType eventStatusLocal;\r
1067         FreezeFrameRecType freezeFrameLocal;\r
1068         ExtDataRecType extendedDataLocal;\r
1069 \r
1070         // Find configuration for this event\r
1071         lookupEventIdParameter(eventId, &eventParam);\r
1072         if (eventParam != NULL) {\r
1073                 if (eventParam->EventClass->OperationCycleRef < DEM_OPERATION_CYCLE_ID_ENDMARK) {\r
1074                         if (operationCycleStateList[eventParam->EventClass->OperationCycleRef] == DEM_CYCLE_STATE_START) {\r
1075                                 if (eventStatus == DEM_EVENT_STATUS_FAILED) {\r
1076                                         updateEventStatusRec(eventParam, eventStatus, TRUE, &eventStatusLocal);\r
1077                                 }\r
1078                                 else {\r
1079                                         updateEventStatusRec(eventParam, eventStatus, FALSE, &eventStatusLocal);\r
1080                                 }\r
1081 \r
1082                                 if (eventStatusLocal.errorStatusChanged) {\r
1083 \r
1084                                         if (eventStatusLocal.eventStatusExtended & DEM_TEST_FAILED) {\r
1085                                                 // Collect freeze frame data\r
1086                                                 getFreezeFrameData(eventParam, &freezeFrameLocal);\r
1087                                                 if (freezeFrameLocal.eventId != DEM_EVENT_ID_NULL) {\r
1088                                                         storeFreezeFrameDataPreInit(eventParam, &freezeFrameLocal);\r
1089                                                 }\r
1090 \r
1091                                                 // Collect extended data\r
1092                                                 getExtendedData(eventParam, &extendedDataLocal);\r
1093                                                 if (extendedDataLocal.eventId != DEM_EVENT_ID_NULL) {\r
1094                                                         storeExtendedDataPreInit(eventParam, &extendedDataLocal);\r
1095                                                 }\r
1096                                         }\r
1097                                 }\r
1098                         }\r
1099                         else {\r
1100                                 // Operation cycle not started\r
1101                                 // TODO: Report error?\r
1102                         }\r
1103                 }\r
1104                 else {\r
1105                         // Operation cycle not set\r
1106                         // TODO: Report error?\r
1107                 }\r
1108         }\r
1109         else {\r
1110                 // Event ID not configured\r
1111                 // TODO: Report error?\r
1112         }\r
1113 }\r
1114 \r
1115 \r
1116 /*\r
1117  * Procedure:   handleEvent\r
1118  * Description: Handle the updating of event status and storing of\r
1119  *                              event related data in event memory.\r
1120  */\r
1121 static Std_ReturnType handleEvent(Dem_EventIdType eventId, Dem_EventStatusType eventStatus)\r
1122 {\r
1123         Std_ReturnType returnCode = E_OK;\r
1124         const Dem_EventParameterType *eventParam;\r
1125         EventStatusRecType eventStatusLocal;\r
1126         FreezeFrameRecType freezeFrameLocal;\r
1127         ExtDataRecType extendedDataLocal;\r
1128 \r
1129         // Find configuration for this event\r
1130         lookupEventIdParameter(eventId, &eventParam);\r
1131         if (eventParam != NULL) {\r
1132                 if (eventParam->EventClass->OperationCycleRef < DEM_OPERATION_CYCLE_ID_ENDMARK) {\r
1133                         if (operationCycleStateList[eventParam->EventClass->OperationCycleRef] == DEM_CYCLE_STATE_START) {\r
1134                                 if (!(disableDtcStorage.storageDisabled && checkDtcGroup(disableDtcStorage.dtcGroup, eventParam) && checkDtcKind(disableDtcStorage.dtcKind, eventParam)))  {\r
1135                                         updateEventStatusRec(eventParam, eventStatus, TRUE, &eventStatusLocal);\r
1136                                         if (eventStatusLocal.errorStatusChanged) {\r
1137                                                 if (eventStatusLocal.eventStatusExtended & DEM_TEST_FAILED) {\r
1138                                                         storeEventEvtMem(eventParam, &eventStatusLocal); /** @req DEM184 */\r
1139                                                         // Collect freeze frame data\r
1140                                                         getFreezeFrameData(eventParam, &freezeFrameLocal);\r
1141                                                         if (freezeFrameLocal.eventId != DEM_EVENT_ID_NULL) {\r
1142                                                                 storeFreezeFrameDataEvtMem(eventParam, &freezeFrameLocal); /** @req DEM190 */\r
1143                                                         }\r
1144 \r
1145                                                         // Collect extended data\r
1146                                                         getExtendedData(eventParam, &extendedDataLocal);\r
1147                                                         if (extendedDataLocal.eventId != DEM_EVENT_ID_NULL)\r
1148                                                         {\r
1149                                                                 storeExtendedDataEvtMem(eventParam, &extendedDataLocal);\r
1150                                                         }\r
1151                                                 }\r
1152                                         }\r
1153                                 }\r
1154                         }\r
1155                         else {\r
1156                                 // Operation cycle not started\r
1157                                 returnCode = E_NOT_OK;\r
1158                         }\r
1159                 }\r
1160                 else {\r
1161                         // Operation cycle not set\r
1162                         returnCode = E_NOT_OK;\r
1163                 }\r
1164         }\r
1165         else {\r
1166                 // Event ID not configured\r
1167                 returnCode = E_NOT_OK;\r
1168         }\r
1169 \r
1170         return returnCode;\r
1171 }\r
1172 \r
1173 \r
1174 /*\r
1175  * Procedure:   resetEventStatus\r
1176  * Description: Resets the events status of eventId.\r
1177  */\r
1178 static void resetEventStatus(Dem_EventIdType eventId)\r
1179 {\r
1180         imask_t state = McuE_EnterCriticalSection();\r
1181         EventStatusRecType *eventStatusRecPtr;\r
1182 \r
1183         lookupEventStatusRec(eventId, &eventStatusRecPtr);\r
1184         if (eventStatusRecPtr != NULL) {\r
1185                 eventStatusRecPtr->eventStatusExtended &= ~DEM_TEST_FAILED; /** @req DEM187 */\r
1186         }\r
1187 \r
1188         McuE_ExitCriticalSection(state);\r
1189 }\r
1190 \r
1191 \r
1192 /*\r
1193  * Procedure:   getEventStatus\r
1194  * Description: Returns the extended event status bitmask of eventId in "eventStatusExtended".\r
1195  */\r
1196 static void getEventStatus(Dem_EventIdType eventId, Dem_EventStatusExtendedType *eventStatusExtended)\r
1197 {\r
1198         EventStatusRecType eventStatusLocal;\r
1199 \r
1200         // Get recorded status\r
1201         getEventStatusRec(eventId, &eventStatusLocal);\r
1202         if (eventStatusLocal.eventId == eventId) {\r
1203                 *eventStatusExtended = eventStatusLocal.eventStatusExtended; /** @req DEM051 */\r
1204         }\r
1205         else {\r
1206                 // Event Id not found, no report received.\r
1207                 *eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
1208         }\r
1209 }\r
1210 \r
1211 \r
1212 /*\r
1213  * Procedure:   getEventFailed\r
1214  * Description: Returns the TRUE or FALSE of "eventId" in "eventFailed" depending on current status.\r
1215  */\r
1216 static void getEventFailed(Dem_EventIdType eventId, boolean *eventFailed)\r
1217 {\r
1218         EventStatusRecType eventStatusLocal;\r
1219 \r
1220         // Get recorded status\r
1221         getEventStatusRec(eventId, &eventStatusLocal);\r
1222         if (eventStatusLocal.eventId == eventId) {\r
1223                 if (eventStatusLocal.eventStatusExtended & DEM_TEST_FAILED) { /** @req DEM052 */\r
1224                         *eventFailed = TRUE;\r
1225                 }\r
1226                 else {\r
1227                         *eventFailed = FALSE;\r
1228                 }\r
1229         }\r
1230         else {\r
1231                 // Event Id not found, assume ok.\r
1232                 *eventFailed = FALSE;\r
1233         }\r
1234 }\r
1235 \r
1236 \r
1237 /*\r
1238  * Procedure:   getEventTested\r
1239  * Description: Returns the TRUE or FALSE of "eventId" in "eventTested" depending on\r
1240  *                              current status the "test not completed this operation cycle" bit.\r
1241  */\r
1242 static void getEventTested(Dem_EventIdType eventId, boolean *eventTested)\r
1243 {\r
1244         EventStatusRecType eventStatusLocal;\r
1245 \r
1246         // Get recorded status\r
1247         getEventStatusRec(eventId, &eventStatusLocal);\r
1248         if (eventStatusLocal.eventId == eventId) {\r
1249                 if ( !(eventStatusLocal.eventStatusExtended & DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE)) { /** @req DEM053 */\r
1250                         *eventTested = TRUE;\r
1251                 }\r
1252                 else {\r
1253                         *eventTested = FALSE;\r
1254                 }\r
1255         }\r
1256         else {\r
1257                 // Event Id not found, not tested.\r
1258                 *eventTested = FALSE;\r
1259         }\r
1260 }\r
1261 \r
1262 \r
1263 /*\r
1264  * Procedure:   getFaultDetectionCounter\r
1265  * Description: Returns pre debounce counter of "eventId" in "counter" and return value E_OK if\r
1266  *                              the counter was available else E_NOT_OK.\r
1267  */\r
1268 static Std_ReturnType getFaultDetectionCounter(Dem_EventIdType eventId, sint8 *counter)\r
1269 {\r
1270         Std_ReturnType returnCode = E_NOT_OK;\r
1271         const Dem_EventParameterType *eventParam;\r
1272 \r
1273         lookupEventIdParameter(eventId, &eventParam);\r
1274         if (eventParam != NULL) {\r
1275                 if (eventParam->EventClass->PreDebounceAlgorithmClass != NULL) {\r
1276                         switch (eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceName)\r
1277                         {\r
1278                         case DEM_NO_PRE_DEBOUNCE:\r
1279                                 if (eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceAlgorithm.PreDebounceMonitorInternal != NULL) {\r
1280                                         if (eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceAlgorithm.PreDebounceMonitorInternal->CallbackGetFDCntFnc != NULL) {\r
1281                                                 returnCode = eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceAlgorithm.PreDebounceMonitorInternal->CallbackGetFDCntFnc(counter); /** @req DEM204.None */ /** @req DEM264 */ /** @req DEM265 */\r
1282                                         }\r
1283                                 }\r
1284                                 break;\r
1285 \r
1286                         case DEM_PRE_DEBOUNCE_COUNTER_BASED:\r
1287                                 {\r
1288                                         EventStatusRecType *eventStatusRec;\r
1289 \r
1290                                         lookupEventStatusRec(eventId, &eventStatusRec);\r
1291                                         if (eventStatusRec != NULL) {\r
1292                                                 *counter = eventStatusRec->faultDetectionCounter; /** @req DEM204.Counter */\r
1293                                         } else {\r
1294                                                 *counter = 0;\r
1295                                         }\r
1296                                         returnCode = E_OK;\r
1297                                 }\r
1298                                 break;\r
1299 \r
1300                         case DEM_PRE_DEBOUNCE_FREQUENCY_BASED:\r
1301                         case DEM_PRE_DEBOUNCE_TIME_BASED:\r
1302                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETFAULTDETECTIONCOUNTER_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
1303                                 break;\r
1304 \r
1305                         default:\r
1306                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETFAULTDETECTIONCOUNTER_ID, DEM_E_PARAM_DATA);\r
1307                                 break;\r
1308                         }\r
1309                 }\r
1310         }\r
1311 \r
1312         return returnCode;\r
1313 }\r
1314 \r
1315 \r
1316 /*\r
1317  * Procedure:   setOperationCycleState\r
1318  * Description: Change the operation state of "operationCycleId" to "cycleState" and updates stored\r
1319  *                              event connected to this cycle id.\r
1320  *                              Returns E_OK if operation was successful else E_NOT_OK.\r
1321  */\r
1322 static Std_ReturnType setOperationCycleState(Dem_OperationCycleIdType operationCycleId, Dem_OperationCycleStateType cycleState) /** @req DEM338 */\r
1323 {\r
1324         uint16 i;\r
1325         Std_ReturnType returnCode = E_OK;\r
1326 \r
1327         if (operationCycleId < DEM_OPERATION_CYCLE_ID_ENDMARK) {\r
1328                 switch (cycleState)\r
1329                 {\r
1330                 case DEM_CYCLE_STATE_START:\r
1331                         operationCycleStateList[operationCycleId] = cycleState;\r
1332                         // Lookup event ID\r
1333                         for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1334                                 if ((eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) && (eventStatusBuffer[i].eventParamRef->EventClass->OperationCycleRef == operationCycleId)) {\r
1335                                         eventStatusBuffer[i].eventStatusExtended &= ~DEM_TEST_FAILED_THIS_OPERATION_CYCLE;\r
1336                                         eventStatusBuffer[i].eventStatusExtended |= DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE;\r
1337                                 }\r
1338                         }\r
1339                         break;\r
1340 \r
1341                 case DEM_CYCLE_STATE_END:\r
1342                         operationCycleStateList[operationCycleId] = cycleState;\r
1343                         // Lookup event ID\r
1344                         for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1345                                 if ((eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) && (eventStatusBuffer[i].eventParamRef->EventClass->OperationCycleRef == operationCycleId)) {\r
1346                                         if (!(eventStatusBuffer[i].eventStatusExtended & DEM_TEST_FAILED_THIS_OPERATION_CYCLE) && !(eventStatusBuffer[i].eventStatusExtended & DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE)) {\r
1347                                                 eventStatusBuffer[i].eventStatusExtended &= ~DEM_PENDING_DTC;           // Clear pendingDTC bit /** @req DEM379.PendingClear\r
1348                                         }\r
1349                                 }\r
1350                         }\r
1351                         break;\r
1352                 default:\r
1353                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_SETOPERATIONCYCLESTATE_ID, DEM_E_PARAM_DATA);\r
1354                         returnCode = E_NOT_OK;\r
1355                         break;\r
1356                 }\r
1357         }\r
1358         else {\r
1359                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_SETOPERATIONCYCLESTATE_ID, DEM_E_PARAM_DATA);\r
1360                 returnCode = E_NOT_OK;\r
1361                 }\r
1362 \r
1363         return returnCode;\r
1364 }\r
1365 \r
1366 \r
1367 //==============================================================================//\r
1368 //                                                                                                                                                              //\r
1369 //                                        E X T E R N A L   F U N C T I O N S                                           //\r
1370 //                                                                                                                                                              //\r
1371 //==============================================================================//\r
1372 \r
1373 /*********************************************\r
1374  * Interface for upper layer modules (8.3.1) *\r
1375  *********************************************/\r
1376 \r
1377 /*\r
1378  * Procedure:   Dem_GetVersionInfo\r
1379  * Reentrant:   Yes\r
1380  */\r
1381 // Defined in Dem.h\r
1382 \r
1383 \r
1384 /***********************************************\r
1385  * Interface ECU State Manager <-> DEM (8.3.2) *\r
1386  ***********************************************/\r
1387 \r
1388 /*\r
1389  * Procedure:   Dem_PreInit\r
1390  * Reentrant:   No\r
1391  */\r
1392 void Dem_PreInit(void)\r
1393 {\r
1394         /** @req DEM180 */\r
1395         int i, j;\r
1396 \r
1397         VALIDATE_NO_RV(DEM_Config.ConfigSet != NULL, DEM_PREINIT_ID, DEM_E_CONFIG_PTR_INVALID);\r
1398 \r
1399         configSet = DEM_Config.ConfigSet;\r
1400 \r
1401         // Initializion of operation cycle states.\r
1402         for (i = 0; i < DEM_OPERATION_CYCLE_ID_ENDMARK; i++) {\r
1403                 operationCycleStateList[i] = DEM_CYCLE_STATE_END;\r
1404         }\r
1405 \r
1406         // Initialize the event status buffer\r
1407         for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1408                 eventStatusBuffer[i].eventId = DEM_EVENT_ID_NULL;\r
1409                 eventStatusBuffer[i].eventParamRef = NULL;\r
1410                 eventStatusBuffer[i].faultDetectionCounter = 0;\r
1411                 eventStatusBuffer[i].occurrence = 0;\r
1412                 eventStatusBuffer[i].eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
1413                 eventStatusBuffer[i].errorStatusChanged = FALSE;\r
1414         }\r
1415 \r
1416         // Initialize the pre init buffers\r
1417         for (i = 0; i < DEM_MAX_NUMBER_FF_DATA_PRE_INIT; i++) {\r
1418                 preInitFreezeFrameBuffer[i].checksum = 0;\r
1419                 preInitFreezeFrameBuffer[i].eventId = DEM_EVENT_ID_NULL;\r
1420                 preInitFreezeFrameBuffer[i].occurrence = 0;\r
1421                 preInitFreezeFrameBuffer[i].dataSize = 0;\r
1422                 for (j = 0; j < DEM_MAX_SIZE_FF_DATA;j++)\r
1423                         preInitFreezeFrameBuffer[i].data[j] = 0;\r
1424         }\r
1425 \r
1426         for (i = 0; i < DEM_MAX_NUMBER_EXT_DATA_PRE_INIT; i++) {\r
1427                 preInitExtDataBuffer[i].checksum = 0;\r
1428                 preInitExtDataBuffer[i].eventId = DEM_EVENT_ID_NULL;\r
1429                 preInitExtDataBuffer[i].dataSize = 0;\r
1430                 for (j = 0; j < DEM_MAX_SIZE_EXT_DATA;j++)\r
1431                         preInitExtDataBuffer[i].data[j] = 0;\r
1432         }\r
1433 \r
1434         disableDtcStorage.storageDisabled = FALSE;\r
1435 \r
1436         setOperationCycleState(DEM_ACTIVE, DEM_CYCLE_STATE_START); /** @req DEM047 */\r
1437 \r
1438         demState = DEM_PREINITIALIZED;\r
1439 }\r
1440 \r
1441 \r
1442 /*\r
1443  * Procedure:   Dem_Init\r
1444  * Reentrant:   No\r
1445  */\r
1446 void Dem_Init(void)\r
1447 {\r
1448         uint16 i;\r
1449         ChecksumType cSum;\r
1450         const Dem_EventParameterType *eventParam;\r
1451 \r
1452         /*\r
1453          *  Validate and read out saved error log from non volatile memory\r
1454          */\r
1455 \r
1456         // Validate event records stored in primary memory\r
1457         for (i = 0; i < DEM_MAX_NUMBER_EVENT_PRI_MEM; i++) {\r
1458                 cSum = calcChecksum(&priMemEventBuffer[i], sizeof(EventRecType)-sizeof(ChecksumType));\r
1459                 if ((cSum != priMemEventBuffer[i].checksum) || priMemEventBuffer[i].eventId == DEM_EVENT_ID_NULL) {\r
1460                         // Unlegal record, clear the record\r
1461                         memset(&priMemEventBuffer[i], 0, sizeof(EventRecType));\r
1462                 }\r
1463                 else {\r
1464                         // Valid, update current status\r
1465                         mergeEventStatusRec(&priMemEventBuffer[i]);\r
1466 \r
1467                         // Update occurrence counter on pre init stored freeze frames\r
1468                         updateFreezeFrameOccurrencePreInit(&priMemEventBuffer[i]);\r
1469                 }\r
1470         }\r
1471 \r
1472         // Validate extended data records stored in primary memory\r
1473         for (i = 0; i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM; i++) {\r
1474                 cSum = calcChecksum(&priMemExtDataBuffer[i], sizeof(ExtDataRecType)-sizeof(ChecksumType));\r
1475                 if ((cSum != priMemExtDataBuffer[i].checksum) || priMemExtDataBuffer[i].eventId == DEM_EVENT_ID_NULL) {\r
1476                         // Unlegal record, clear the record\r
1477                         memset(&priMemExtDataBuffer[i], 0, sizeof(ExtDataRecType));\r
1478                 }\r
1479         }\r
1480 \r
1481         // Validate freeze frame records stored in primary memory\r
1482         for (i = 0; i < DEM_MAX_NUMBER_FF_DATA_PRI_MEM; i++) {\r
1483                 cSum = calcChecksum(&priMemFreezeFrameBuffer[i], sizeof(FreezeFrameRecType)-sizeof(ChecksumType));\r
1484                 if ((cSum != priMemFreezeFrameBuffer[i].checksum) || (priMemFreezeFrameBuffer[i].eventId == DEM_EVENT_ID_NULL)) {\r
1485                         // Unlegal record, clear the record\r
1486                         memset(&priMemFreezeFrameBuffer[i], 0, sizeof(FreezeFrameRecType));\r
1487                 }\r
1488         }\r
1489 \r
1490         /*\r
1491          *  Handle errors stored in temporary buffer (if any)\r
1492          */\r
1493 \r
1494         // Transfer updated event data to event memory\r
1495         for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1496                 if (eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) {\r
1497                         // Update the event memory\r
1498                         lookupEventIdParameter(eventStatusBuffer[i].eventId, &eventParam);\r
1499                         storeEventEvtMem(eventParam, &eventStatusBuffer[i]);\r
1500                 }\r
1501         }\r
1502 \r
1503         // Transfer extended data to event memory if necessary\r
1504         for (i = 0; i < DEM_MAX_NUMBER_EXT_DATA_PRE_INIT; i++) {\r
1505                 if (preInitExtDataBuffer[i].eventId !=  DEM_EVENT_ID_NULL) {\r
1506                         lookupEventIdParameter(preInitExtDataBuffer[i].eventId, &eventParam);\r
1507                         storeExtendedDataEvtMem(eventParam, &preInitExtDataBuffer[i]);\r
1508                 }\r
1509         }\r
1510 \r
1511         // Transfer freeze frames to event memory\r
1512         for (i = 0; i < DEM_MAX_NUMBER_FF_DATA_PRE_INIT; i++) {\r
1513                 if (preInitFreezeFrameBuffer[i].eventId != DEM_EVENT_ID_NULL) {\r
1514                         lookupEventIdParameter(preInitFreezeFrameBuffer[i].eventId, &eventParam);\r
1515                         storeFreezeFrameDataEvtMem(eventParam, &preInitFreezeFrameBuffer[i]);\r
1516                 }\r
1517         }\r
1518 \r
1519         // Init the dtc filter\r
1520         dtcFilter.dtcStatusMask = DEM_DTC_STATUS_MASK_ALL;                                      // All allowed\r
1521         dtcFilter.dtcKind = DEM_DTC_KIND_ALL_DTCS;                                                      // All kinds of DTCs\r
1522         dtcFilter.dtcOrigin = DEM_DTC_ORIGIN_PRIMARY_MEMORY;                            // Primary memory\r
1523         dtcFilter.filterWithSeverity = DEM_FILTER_WITH_SEVERITY_NO;                     // No Severity filtering\r
1524         dtcFilter.dtcSeverityMask = DEM_SEVERITY_NO_SEVERITY;                           // Not used when filterWithSeverity is FALSE\r
1525         dtcFilter.filterForFaultDetectionCounter = DEM_FILTER_FOR_FDC_NO;       // No fault detection counter filtering\r
1526 \r
1527         dtcFilter.faultIndex = DEM_MAX_NUMBER_EVENT;\r
1528 \r
1529         disableDtcStorage.storageDisabled = FALSE;\r
1530 \r
1531         demState = DEM_INITIALIZED;\r
1532 }\r
1533 \r
1534 \r
1535 /*\r
1536  * Procedure:   Dem_shutdown\r
1537  * Reentrant:   No\r
1538  */\r
1539 void Dem_Shutdown(void)\r
1540 {\r
1541         setOperationCycleState(DEM_ACTIVE, DEM_CYCLE_STATE_END); /** @req DEM047 */\r
1542 \r
1543         demState = DEM_UNINITIALIZED; /** @req DEM368 */\r
1544 }\r
1545 \r
1546 \r
1547 /*\r
1548  * Interface for basic software scheduler\r
1549  */\r
1550 void Dem_MainFunction(void)\r
1551 {\r
1552         /** @req DEM125 */\r
1553 \r
1554 }\r
1555 \r
1556 \r
1557 /***************************************************\r
1558  * Interface SW-Components via RTE <-> DEM (8.3.3) *\r
1559  ***************************************************/\r
1560 \r
1561 /*\r
1562  * Procedure:   Dem_SetEventStatus\r
1563  * Reentrant:   Yes\r
1564  */\r
1565 Std_ReturnType Dem_SetEventStatus(Dem_EventIdType eventId, Dem_EventStatusType eventStatus) /** @req DEM330 */\r
1566 {\r
1567         Std_ReturnType returnCode = E_OK;\r
1568 \r
1569         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1570         {\r
1571                 returnCode = handleEvent(eventId, eventStatus);\r
1572         }\r
1573         else\r
1574         {\r
1575                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_SETEVENTSTATUS_ID, DEM_E_UNINIT);\r
1576                 returnCode = E_NOT_OK;\r
1577         }\r
1578 \r
1579         return returnCode;\r
1580 }\r
1581 \r
1582 \r
1583 /*\r
1584  * Procedure:   Dem_ResetEventStatus\r
1585  * Reentrant:   Yes\r
1586  */\r
1587 Std_ReturnType Dem_ResetEventStatus(Dem_EventIdType eventId) /** @req DEM331 */\r
1588 {\r
1589         Std_ReturnType returnCode = E_OK;\r
1590 \r
1591         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1592         {\r
1593                 resetEventStatus(eventId); /** @req DEM186 */\r
1594         }\r
1595         else\r
1596         {\r
1597                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_RESETEVENTSTATUS_ID, DEM_E_UNINIT);\r
1598                 returnCode = E_NOT_OK;\r
1599         }\r
1600 \r
1601         return returnCode;\r
1602 }\r
1603 \r
1604 \r
1605 /*\r
1606  * Procedure:   Dem_GetEventStatus\r
1607  * Reentrant:   Yes\r
1608  */\r
1609 Std_ReturnType Dem_GetEventStatus(Dem_EventIdType eventId, Dem_EventStatusExtendedType *eventStatusExtended) /** @req DEM332 */\r
1610 {\r
1611         Std_ReturnType returnCode = E_OK;\r
1612 \r
1613         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1614         {\r
1615                 getEventStatus(eventId, eventStatusExtended);\r
1616         }\r
1617         else\r
1618         {\r
1619                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEVENTSTATUS_ID, DEM_E_UNINIT);\r
1620                 returnCode = E_NOT_OK;\r
1621         }\r
1622 \r
1623         return returnCode;\r
1624 }\r
1625 \r
1626 \r
1627 /*\r
1628  * Procedure:   Dem_GetEventFailed\r
1629  * Reentrant:   Yes\r
1630  */\r
1631 Std_ReturnType Dem_GetEventFailed(Dem_EventIdType eventId, boolean *eventFailed) /** @req DEM333 */\r
1632 {\r
1633         Std_ReturnType returnCode = E_OK;\r
1634 \r
1635         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1636         {\r
1637                 getEventFailed(eventId, eventFailed);\r
1638         }\r
1639         else\r
1640         {\r
1641                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEVENTFAILED_ID, DEM_E_UNINIT);\r
1642                 returnCode = E_NOT_OK;\r
1643         }\r
1644 \r
1645         return returnCode;\r
1646 }\r
1647 \r
1648 \r
1649 /*\r
1650  * Procedure:   Dem_GetEventTested\r
1651  * Reentrant:   Yes\r
1652  */\r
1653 Std_ReturnType Dem_GetEventTested(Dem_EventIdType eventId, boolean *eventTested)\r
1654 {\r
1655         Std_ReturnType returnCode = E_OK;\r
1656 \r
1657         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1658         {\r
1659                 getEventTested(eventId, eventTested);\r
1660         }\r
1661         else\r
1662         {\r
1663                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEVENTTESTED_ID, DEM_E_UNINIT);\r
1664                 returnCode = E_NOT_OK;\r
1665         }\r
1666 \r
1667         return returnCode;\r
1668 }\r
1669 \r
1670 \r
1671 /*\r
1672  * Procedure:   Dem_GetFaultDetectionCounter\r
1673  * Reentrant:   No\r
1674  */\r
1675 Std_ReturnType Dem_GetFaultDetectionCounter(Dem_EventIdType eventId, sint8 *counter)\r
1676 {\r
1677         Std_ReturnType returnCode = E_OK;\r
1678 \r
1679         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1680         {\r
1681                 returnCode = getFaultDetectionCounter(eventId, counter);\r
1682         }\r
1683         else\r
1684         {\r
1685                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETFAULTDETECTIONCOUNTER_ID, DEM_E_UNINIT);\r
1686                 returnCode = E_NOT_OK;\r
1687         }\r
1688 \r
1689         return returnCode;\r
1690 }\r
1691 \r
1692 \r
1693 /*\r
1694  * Procedure:   Dem_SetOperationCycleState\r
1695  * Reentrant:   No\r
1696  */\r
1697 Std_ReturnType Dem_SetOperationCycleState(Dem_OperationCycleIdType operationCycleId, Dem_OperationCycleStateType cycleState)\r
1698 {\r
1699         Std_ReturnType returnCode = E_OK;\r
1700 \r
1701         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1702         {\r
1703                 returnCode = setOperationCycleState(operationCycleId, cycleState);\r
1704 \r
1705         }\r
1706         else\r
1707         {\r
1708                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_SETOPERATIONCYCLESTATE_ID, DEM_E_UNINIT);\r
1709                 returnCode = E_NOT_OK;\r
1710         }\r
1711 \r
1712         return returnCode;\r
1713 }\r
1714 \r
1715 \r
1716 /*\r
1717  * Procedure:   Dem_GetDTCOfEvent\r
1718  * Reentrant:   Yes\r
1719  */\r
1720 Std_ReturnType Dem_GetDTCOfEvent(Dem_EventIdType eventId, Dem_DTCKindType dtcKind, uint32* dtcOfEvent)\r
1721 {\r
1722         Std_ReturnType returnCode = E_NO_DTC_AVAILABLE;\r
1723         const Dem_EventParameterType *eventParam;\r
1724 \r
1725         if (demState == DEM_INITIALIZED) // No action is taken if the module is not started\r
1726         {\r
1727                 lookupEventIdParameter(eventId, &eventParam);\r
1728 \r
1729                 if (eventParam != NULL) {\r
1730                         if (checkDtcKind(dtcKind, eventParam)) {\r
1731                                 if (eventParam->DTCClassRef != NULL) {\r
1732                                         *dtcOfEvent = eventParam->DTCClassRef->DTC; /** @req DEM269 */\r
1733                                         returnCode = E_OK;\r
1734                                 }\r
1735                         }\r
1736                 }\r
1737                 else {\r
1738                         // Event Id not found\r
1739                         returnCode = E_NOT_OK;\r
1740                 }\r
1741         }\r
1742         else\r
1743         {\r
1744                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETDTCOFEVENT_ID, DEM_UNINITIALIZED);\r
1745                 returnCode = E_NOT_OK;\r
1746         }\r
1747 \r
1748         return returnCode;\r
1749 }\r
1750 \r
1751 \r
1752 /********************************************\r
1753  * Interface BSW-Components <-> DEM (8.3.4) *\r
1754  ********************************************/\r
1755 \r
1756 /*\r
1757  * Procedure:   Dem_ReportErrorStatus\r
1758  * Reentrant:   Yes\r
1759  */\r
1760 void Dem_ReportErrorStatus( Dem_EventIdType eventId, Dem_EventStatusType eventStatus ) /** @req DEM107 */\r
1761 {\r
1762 \r
1763         switch (demState) {\r
1764                 case DEM_PREINITIALIZED:\r
1765                         // Update status and check if is to be stored\r
1766                         if ((eventStatus == DEM_EVENT_STATUS_PASSED) || (eventStatus == DEM_EVENT_STATUS_FAILED)) {\r
1767                                 handlePreInitEvent(eventId, eventStatus); /** @req DEM168 */\r
1768                         }\r
1769                         break;\r
1770 \r
1771                 case DEM_INITIALIZED:\r
1772                         (void)handleEvent(eventId, eventStatus);        /** @req DEM167 */\r
1773                         break;\r
1774 \r
1775                 case DEM_UNINITIALIZED:\r
1776                 default:\r
1777                         // Uninitialized can not do anything\r
1778                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_REPORTERRORSTATUS_ID, DEM_E_UNINIT);\r
1779 \r
1780                         break;\r
1781 \r
1782         } // switch (demState)\r
1783 }\r
1784 \r
1785 /*********************************\r
1786  * Interface DCM <-> DEM (8.3.5) *\r
1787  *********************************/\r
1788 /*\r
1789  * Procedure:   Dem_GetDTCStatusAvailabilityMask\r
1790  * Reentrant:   No\r
1791  */\r
1792 Std_ReturnType Dem_GetDTCStatusAvailabilityMask(uint8 *dtcStatusMask) /** @req DEM014 */\r
1793 {\r
1794         *dtcStatusMask =        DEM_DTC_STATUS_AVAILABILITY_MASK;               // User configuration mask\r
1795         *dtcStatusMask &=       DEM_TEST_FAILED                                                 // Mask with supported bits /** @req DEM060 */\r
1796                                                 | DEM_TEST_FAILED_THIS_OPERATION_CYCLE\r
1797                                                 | DEM_PENDING_DTC\r
1798 //                                              | DEM_CONFIRMED_DTC                                     TODO: Add support for this bit\r
1799                                                 | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR\r
1800                                                 | DEM_TEST_FAILED_SINCE_LAST_CLEAR\r
1801                                                 | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE\r
1802 //                                              | DEM_WARNING_INDICATOR_REQUESTED       TODO: Add support for this bit\r
1803                                                 ;\r
1804 \r
1805         return E_OK;\r
1806 }\r
1807 \r
1808 \r
1809 /*\r
1810  * Procedure:   Dem_SetDTCFilter\r
1811  * Reentrant:   No\r
1812  */\r
1813 Dem_ReturnSetDTCFilterType Dem_SetDTCFilter(uint8 dtcStatusMask,\r
1814                 Dem_DTCKindType dtcKind,\r
1815                 Dem_DTCOriginType dtcOrigin,\r
1816                 Dem_FilterWithSeverityType filterWithSeverity,\r
1817                 Dem_DTCSeverityType dtcSeverityMask,\r
1818                 Dem_FilterForFDCType filterForFaultDetectionCounter)\r
1819 {\r
1820         Dem_ReturnSetDTCFilterType returnCode = DEM_FILTER_ACCEPTED;\r
1821 \r
1822         if (demState == DEM_INITIALIZED) {\r
1823                 // Check dtcKind parameter\r
1824                 VALIDATE_RV((dtcKind == DEM_DTC_KIND_ALL_DTCS) || (dtcKind == DEM_DTC_KIND_EMISSION_REL_DTCS), DEM_SETDTCFILTER_ID, DEM_E_PARAM_DATA, DEM_WRONG_FILTER);\r
1825 \r
1826                 // Check dtcOrigin parameter\r
1827                 VALIDATE_RV((dtcOrigin == DEM_DTC_ORIGIN_SECONDARY_MEMORY) || (dtcOrigin == DEM_DTC_ORIGIN_PRIMARY_MEMORY)\r
1828                                         || (dtcOrigin == DEM_DTC_ORIGIN_PERMANENT_MEMORY) || (dtcOrigin == DEM_DTC_ORIGIN_MIRROR_MEMORY), DEM_SETDTCFILTER_ID, DEM_E_PARAM_DATA, DEM_WRONG_FILTER);\r
1829 \r
1830                 // Check filterWithSeverity and dtcSeverityMask parameter\r
1831                 VALIDATE_RV(((filterWithSeverity == DEM_FILTER_WITH_SEVERITY_NO)\r
1832                                         || ((filterWithSeverity == DEM_FILTER_WITH_SEVERITY_YES)\r
1833                                                 && !(dtcSeverityMask & ~(DEM_SEVERITY_MAINTENANCE_ONLY | DEM_SEVERITY_CHECK_AT_NEXT_FALT | DEM_SEVERITY_CHECK_IMMEDIATELY)))), DEM_SETDTCFILTER_ID, DEM_E_PARAM_DATA, DEM_WRONG_FILTER);\r
1834 \r
1835                 // Check filterForFaultDetectionCounter parameter\r
1836                 VALIDATE_RV((filterForFaultDetectionCounter == DEM_FILTER_FOR_FDC_YES) || (filterForFaultDetectionCounter ==  DEM_FILTER_FOR_FDC_NO), DEM_SETDTCFILTER_ID, DEM_E_PARAM_DATA, DEM_WRONG_FILTER);\r
1837 \r
1838                 // Yes all parameters correct, set the new filters.  /** @req DEM057 */\r
1839                 dtcFilter.dtcStatusMask = dtcStatusMask;\r
1840                 dtcFilter.dtcKind = dtcKind;\r
1841                 dtcFilter.dtcOrigin = dtcOrigin;\r
1842                 dtcFilter.filterWithSeverity = filterWithSeverity;\r
1843                 dtcFilter.dtcSeverityMask = dtcSeverityMask;\r
1844                 dtcFilter.filterForFaultDetectionCounter = filterForFaultDetectionCounter;\r
1845                 dtcFilter.faultIndex = DEM_MAX_NUMBER_EVENT;\r
1846         } else {\r
1847                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_SETDTCFILTER_ID, DEM_E_UNINIT);\r
1848                 returnCode = DEM_WRONG_FILTER;\r
1849         }\r
1850 \r
1851         return returnCode;\r
1852 }\r
1853 \r
1854 \r
1855 /*\r
1856  * Procedure:   Dem_GetStatusOfDTC\r
1857  * Reentrant:   No\r
1858  */\r
1859 Dem_ReturnGetStatusOfDTCType Dem_GetStatusOfDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin, Dem_EventStatusExtendedType* status) {\r
1860         Dem_ReturnGetStatusOfDTCType returnCode = DEM_STATUS_FAILED;\r
1861         EventStatusRecType *eventRec;\r
1862 \r
1863         if (demState == DEM_INITIALIZED) {\r
1864                 if (lookupDtcEvent(dtc, &eventRec)) {\r
1865                         if (checkDtcKind(dtcKind, eventRec->eventParamRef)) {\r
1866                                 if (checkDtcOrigin(dtcOrigin,eventRec->eventParamRef)) {\r
1867                                         *status = eventRec->eventStatusExtended; /** @req DEM059 */\r
1868                                         returnCode = DEM_STATUS_OK;\r
1869                                 }\r
1870                                 else {\r
1871                                         returnCode = DEM_STATUS_WRONG_DTCORIGIN; /** @req DEM171 */\r
1872                                 }\r
1873                         }\r
1874                         else {\r
1875                                 returnCode = DEM_STATUS_WRONG_DTCKIND;\r
1876                         }\r
1877                 }\r
1878                 else {\r
1879                         returnCode = DEM_STATUS_WRONG_DTC; /** @req DEM172 */\r
1880                 }\r
1881         } else {\r
1882                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETSTATUSOFDTC_ID, DEM_E_UNINIT);\r
1883                 returnCode = DEM_STATUS_FAILED;\r
1884         }\r
1885 \r
1886         return returnCode;\r
1887 }\r
1888 \r
1889 \r
1890 /*\r
1891  * Procedure:   Dem_GetNumberOfFilteredDtc\r
1892  * Reentrant:   No\r
1893  */\r
1894 Dem_ReturnGetNumberOfFilteredDTCType Dem_GetNumberOfFilteredDtc(uint16 *numberOfFilteredDTC) {\r
1895         uint16 i;\r
1896         uint16 numberOfFaults = 0;\r
1897         Dem_ReturnGetNumberOfFilteredDTCType returnCode = DEM_NUMBER_OK;\r
1898 \r
1899         if (demState == DEM_INITIALIZED) {\r
1900                 //Dem_DisableEventStatusUpdate();\r
1901 \r
1902                 for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1903                         if (eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) {\r
1904                                 if (matchEventWithDtcFilter(&eventStatusBuffer[i])) {\r
1905                                         if (eventStatusBuffer[i].eventParamRef->DTCClassRef != NULL) {\r
1906                                                 numberOfFaults++;\r
1907                                         }\r
1908                                 }\r
1909                         }\r
1910                 }\r
1911 \r
1912                 //Dem_EnableEventStatusUpdate();\r
1913 \r
1914                 *numberOfFilteredDTC = numberOfFaults; /** @req DEM061 */\r
1915         } else {\r
1916                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETNUMBEROFFILTEREDDTC_ID, DEM_E_UNINIT);\r
1917                 returnCode = DEM_NUMBER_FAILED;\r
1918         }\r
1919 \r
1920         return returnCode;\r
1921 }\r
1922 \r
1923 \r
1924 /*\r
1925  * Procedure:   Dem_GetNextFilteredDTC\r
1926  * Reentrant:   No\r
1927  */\r
1928 Dem_ReturnGetNextFilteredDTCType Dem_GetNextFilteredDTC(uint32 *dtc, Dem_EventStatusExtendedType *dtcStatus)\r
1929 {\r
1930         Dem_ReturnGetNextFilteredDTCType returnCode = DEM_FILTERED_OK;\r
1931         boolean dtcFound = FALSE;\r
1932 \r
1933         if (demState == DEM_INITIALIZED) {\r
1934                 // TODO: This job should be done in an more advanced way according to Dem217\r
1935                 while (!dtcFound && (dtcFilter.faultIndex != 0)) {\r
1936                         dtcFilter.faultIndex--;\r
1937                         if (eventStatusBuffer[dtcFilter.faultIndex].eventId != DEM_EVENT_ID_NULL) {\r
1938                                 if (matchEventWithDtcFilter(&eventStatusBuffer[dtcFilter.faultIndex])) {\r
1939                                         if (eventStatusBuffer[dtcFilter.faultIndex].eventParamRef->DTCClassRef != NULL) {\r
1940                                                 *dtc = eventStatusBuffer[dtcFilter.faultIndex].eventParamRef->DTCClassRef->DTC; /** @req DEM216 */\r
1941                                                 *dtcStatus = eventStatusBuffer[dtcFilter.faultIndex].eventStatusExtended;\r
1942                                                 dtcFound = TRUE;\r
1943                                         }\r
1944                                 }\r
1945                         }\r
1946                 }\r
1947 \r
1948                 if (!dtcFound) {\r
1949                         dtcFilter.faultIndex = DEM_MAX_NUMBER_EVENT;\r
1950                         returnCode = DEM_FILTERED_NO_MATCHING_DTC;\r
1951                 }\r
1952         } else {\r
1953                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETNEXTFILTEREDDTC_ID, DEM_E_UNINIT);\r
1954                 returnCode = DEM_FILTERED_NO_MATCHING_DTC;\r
1955         }\r
1956 \r
1957         return returnCode;\r
1958 }\r
1959 \r
1960 \r
1961 /*\r
1962  * Procedure:   Dem_GetTranslationType\r
1963  * Reentrant:   No\r
1964  */\r
1965 Dem_ReturnTypeOfDtcSupportedType Dem_GetTranslationType(void)\r
1966 {\r
1967         return DEM_TYPE_OF_DTC_SUPPORTED; /** @req DEM231 */\r
1968 }\r
1969 \r
1970 \r
1971 /*\r
1972  * Procedure:   Dem_ClearDTC\r
1973  * Reentrant:   No\r
1974  */\r
1975 Dem_ReturnClearDTCType Dem_ClearDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin) /** @req DEM009 */\r
1976 {\r
1977         Dem_ReturnClearDTCType returnCode = DEM_CLEAR_OK;\r
1978         Dem_EventIdType eventId;\r
1979         const Dem_EventParameterType *eventParam;\r
1980         uint16 i, j;\r
1981 \r
1982         if (demState == DEM_INITIALIZED) {\r
1983                 // Loop through the event buffer\r
1984                 for (i = 0; i < DEM_MAX_NUMBER_EVENT; i++) {\r
1985                         eventId = eventStatusBuffer[i].eventId;\r
1986                         if (eventId != DEM_EVENT_ID_NULL) {\r
1987                                 eventParam = eventStatusBuffer[i].eventParamRef;\r
1988                                 if (eventParam != NULL) {\r
1989                                         if (DEM_CLEAR_ALL_EVENTS | (eventParam->DTCClassRef != NULL)) {\r
1990                                                 if (checkDtcKind(dtcKind, eventParam)) {\r
1991                                                         if (checkDtcGroup(dtc, eventParam)) {\r
1992                                                                 for (j = 0; (j < DEM_MAX_NR_OF_EVENT_DESTINATION) && (eventParam->EventClass->EventDestination[j] != dtcOrigin); j++);\r
1993                                                                 if (j < DEM_MAX_NR_OF_EVENT_DESTINATION) {\r
1994                                                                         // Yes! All conditions met.\r
1995                                                                         switch (dtcOrigin)\r
1996                                                                         {\r
1997                                                                         case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
1998                                                                                 /** @req DEM077 */\r
1999                                                                                 deleteEventPriMem(eventParam);\r
2000                                                                                 deleteFreezeFrameDataPriMem(eventParam);\r
2001                                                                                 deleteExtendedDataPriMem(eventParam);\r
2002                                                                                 deleteEventStatusRec(eventParam);               // TODO: Shall this be done or just resetting the status?\r
2003                                                                                 break;\r
2004 \r
2005                                                                         case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
2006                                                                         case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
2007                                                                         case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
2008                                                                                 // Not yet supported\r
2009                                                                                 returnCode = DEM_CLEAR_WRONG_DTCORIGIN;\r
2010                                                                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
2011                                                                                 break;\r
2012                                                                         default:\r
2013                                                                                 returnCode = DEM_CLEAR_WRONG_DTCORIGIN;\r
2014                                                                                 break;\r
2015                                                                         }\r
2016                                                                 }\r
2017                                                         }\r
2018                                                 }\r
2019                                         }\r
2020                                 }\r
2021                                 else {\r
2022                                         // Fatal error, no event parameters found for the stored event!\r
2023                                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_UNEXPECTED_EXECUTION);\r
2024                                 }\r
2025                         }\r
2026                 }\r
2027         } else {\r
2028                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_UNINIT);\r
2029                 returnCode = DEM_CLEAR_FAILED;\r
2030         }\r
2031 \r
2032         return returnCode;\r
2033 }\r
2034 \r
2035 \r
2036 /*\r
2037  * Procedure:   Dem_DisableDTCStorage\r
2038  * Reentrant:   No\r
2039  */\r
2040 Dem_ReturnControlDTCStorageType Dem_DisableDTCStorage(Dem_DTCGroupType dtcGroup, Dem_DTCKindType dtcKind) /** @req DEM035 */\r
2041 {\r
2042         Dem_ReturnControlDTCStorageType returnCode = DEM_CONTROL_DTC_STORAGE_OK;\r
2043 \r
2044         if (demState == DEM_INITIALIZED) {\r
2045                 // Check dtcGroup parameter\r
2046                 if (dtcGroup == DEM_DTC_GROUP_ALL_DTCS) {\r
2047                         // Check dtcKind parameter\r
2048                         if ((dtcKind == DEM_DTC_KIND_ALL_DTCS) || (dtcKind ==  DEM_DTC_KIND_EMISSION_REL_DTCS)) {\r
2049                                 /** @req DEM079 */\r
2050                                 disableDtcStorage.dtcGroup = dtcGroup;\r
2051                                 disableDtcStorage.dtcKind = dtcKind;\r
2052                                 disableDtcStorage.storageDisabled = TRUE;\r
2053                         } else {\r
2054                                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2055                         }\r
2056                 } else {\r
2057                         returnCode = DEM_CONTROL_DTC_WRONG_DTCGROUP;\r
2058                 }\r
2059         } else {\r
2060                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_DISABLEDTCSTORAGE_ID, DEM_E_UNINIT);\r
2061                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2062         }\r
2063 \r
2064         return returnCode;\r
2065 }\r
2066 \r
2067 \r
2068 /*\r
2069  * Procedure:   Dem_EnableDTCStorage\r
2070  * Reentrant:   No\r
2071  */\r
2072 Dem_ReturnControlDTCStorageType Dem_EnableDTCStorage(Dem_DTCGroupType dtcGroup, Dem_DTCKindType dtcKind)\r
2073 {\r
2074         Dem_ReturnControlDTCStorageType returnCode = DEM_CONTROL_DTC_STORAGE_OK;\r
2075 \r
2076         if (demState == DEM_INITIALIZED) {\r
2077                 // TODO: Behavior is not defined if group or kind do not match active settings, therefore the filter is just switched off.\r
2078                 disableDtcStorage.storageDisabled = FALSE; /** @req DEM080 */\r
2079         } else {\r
2080                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_ENABLEDTCSTORAGE_ID, DEM_E_UNINIT);\r
2081                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2082         }\r
2083 \r
2084         return returnCode;\r
2085 }\r
2086 \r
2087 /*\r
2088  * Procedure:   Dem_GetExtendedDataRecordByDTC\r
2089  * Reentrant:   No\r
2090  */\r
2091 Dem_ReturnGetExtendedDataRecordByDTCType Dem_GetExtendedDataRecordByDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin, uint8 extendedDataNumber, uint8 *destBuffer, uint8 *bufSize)\r
2092 {\r
2093         Dem_ReturnGetExtendedDataRecordByDTCType returnCode = DEM_RECORD_WRONG_DTC;\r
2094         EventStatusRecType *eventRec;\r
2095         Dem_ExtendedDataRecordClassType const *extendedDataRecordClass = NULL;\r
2096         ExtDataRecType *extData;\r
2097         uint8 posInExtData = 0;\r
2098 \r
2099         if (demState == DEM_INITIALIZED) {\r
2100                 if (lookupDtcEvent(dtc, &eventRec)) {\r
2101                         if (checkDtcKind(dtcKind, eventRec->eventParamRef)) {\r
2102                                 if (checkDtcOrigin(dtcOrigin, eventRec->eventParamRef)) {\r
2103                                         if (lookupExtendedDataRecNumParam(extendedDataNumber, eventRec->eventParamRef, &extendedDataRecordClass, &posInExtData)) {\r
2104                                                 if (*bufSize >= extendedDataRecordClass->DataSize) {\r
2105                                                         switch (dtcOrigin)\r
2106                                                         {\r
2107                                                         case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
2108                                                                 if (lookupExtendedDataPriMem(eventRec->eventId, &extData)) {\r
2109                                                                         // Yes all conditions met, copy the extended data record to destination buffer.\r
2110                                                                         memcpy(destBuffer, &extData->data[posInExtData], extendedDataRecordClass->DataSize); /** @req DEM075 */\r
2111                                                                         *bufSize = extendedDataRecordClass->DataSize;\r
2112                                                                         returnCode = DEM_RECORD_OK;\r
2113                                                                 }\r
2114                                                                 else {\r
2115                                                                         // The record number is legal but no record was found for the DTC\r
2116                                                                         *bufSize = 0;\r
2117                                                                         returnCode = DEM_RECORD_OK;\r
2118                                                                 }\r
2119                                                                 break;\r
2120 \r
2121                                                         case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
2122                                                         case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
2123                                                         case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
2124                                                                 // Not yet supported\r
2125                                                                 returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2126                                                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEXTENDEDDATARECORDBYDTC_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
2127                                                                 break;\r
2128                                                         default:\r
2129                                                                 returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2130                                                                 break;\r
2131                                                         }\r
2132                                                 }\r
2133                                                 else {\r
2134                                                         returnCode = DEM_RECORD_BUFFERSIZE;\r
2135                                                 }\r
2136                                         }\r
2137                                         else {\r
2138                                                 returnCode = DEM_RECORD_NUMBER;\r
2139                                         }\r
2140                                 }\r
2141                                 else {\r
2142                                         returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2143                                 }\r
2144                         }\r
2145                         else {\r
2146                                 returnCode = DEM_RECORD_DTCKIND;\r
2147                         }\r
2148                 }\r
2149         } else {\r
2150                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEXTENDEDDATARECORDBYDTC_ID, DEM_E_UNINIT);\r
2151                 returnCode = DEM_RECORD_WRONG_DTC;\r
2152         }\r
2153 \r
2154         return returnCode;\r
2155 }\r
2156 \r
2157 \r
2158 /*\r
2159  * Procedure:   Dem_GetSizeOfExtendedDataRecordByDTC\r
2160  * Reentrant:   No\r
2161  */\r
2162 Dem_ReturnGetSizeOfExtendedDataRecordByDTCType Dem_GetSizeOfExtendedDataRecordByDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin, uint8 extendedDataNumber, uint16 *sizeOfExtendedDataRecord)\r
2163 {\r
2164         Dem_ReturnGetExtendedDataRecordByDTCType returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTC;\r
2165         EventStatusRecType *eventRec;\r
2166         Dem_ExtendedDataRecordClassType const *extendedDataRecordClass = NULL;\r
2167         uint8 posInExtData;\r
2168 \r
2169         if (demState == DEM_INITIALIZED) {\r
2170                 if (lookupDtcEvent(dtc, &eventRec)) {\r
2171                         if (checkDtcKind(dtcKind, eventRec->eventParamRef)) {\r
2172                                 if (checkDtcOrigin(dtcOrigin, eventRec->eventParamRef)) {\r
2173                                         if (lookupExtendedDataRecNumParam(extendedDataNumber, eventRec->eventParamRef, &extendedDataRecordClass, &posInExtData)) {\r
2174                                                 *sizeOfExtendedDataRecord = extendedDataRecordClass->DataSize; /** @req DEM076 */\r
2175                                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_OK;\r
2176                                         }\r
2177                                         else {\r
2178                                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_RNUM;\r
2179                                         }\r
2180                                 }\r
2181                                 else {\r
2182                                         returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTCOR;\r
2183                                 }\r
2184                         }\r
2185                         else {\r
2186                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTCKI;\r
2187                         }\r
2188                 }\r
2189         } else {\r
2190                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETSIZEOFEXTENDEDDATARECORDBYDTC_ID, DEM_E_UNINIT);\r
2191                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTC;\r
2192         }\r
2193 \r
2194         return returnCode;\r
2195 }\r
2196 \r
2197 /***********************************\r
2198  * OBD-specific Interfaces (8.3.6) *\r
2199  ***********************************/\r
2200 \r
2201 \r
2202 \r