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