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