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