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