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