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