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