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