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