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