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