]> rtime.felk.cvut.cz Git - arc.git/blob - diagnostic/Dem/Dem.c
Lint-fixes on Dem and Dcm with Peter.
[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 void demZeroPriMemBuffers(void) // 957 PC-Lint (MISRA 8.1): OK. Used only by DemTest\r
190 {\r
191         memset(priMemEventBuffer, 0, sizeof(priMemEventBuffer));\r
192         memset(priMemFreezeFrameBuffer, 0, sizeof(priMemFreezeFrameBuffer));\r
193         memset(priMemExtDataBuffer, 0, sizeof(priMemExtDataBuffer));\r
194 }\r
195 \r
196 \r
197 /*\r
198  * Procedure:   calcChecksum\r
199  * Description: Calculate checksum over *data to *(data+nrOfBytes-1) area\r
200  */\r
201 static ChecksumType calcChecksum(void *data, uint16 nrOfBytes)\r
202 {\r
203         uint16 i;\r
204         uint8 *byte = (uint8*)data;\r
205         ChecksumType sum = 0;\r
206 \r
207         for (i = 0; i < nrOfBytes; i++) {\r
208                 sum += byte[i];\r
209         }\r
210         sum ^= 0xaaaau;\r
211         return sum;\r
212 }\r
213 \r
214 /*\r
215  * Procedure:   checkDtcKind\r
216  * Description: Return TRUE if "dtcKind" match the events DTCKind or "dtcKind"\r
217  *                              is "DEM_DTC_KIND_ALL_DTCS" otherwise FALSE.\r
218  */\r
219 static boolean checkDtcKind(Dem_DTCKindType dtcKind, const Dem_EventParameterType *eventParam)\r
220 {\r
221         boolean result = FALSE;\r
222 \r
223         if (dtcKind == DEM_DTC_KIND_ALL_DTCS) {\r
224                 result = TRUE;\r
225         }\r
226         else {\r
227                 if (eventParam->DTCClassRef != NULL) {\r
228                         if (eventParam->DTCClassRef->DTCKind == dtcKind) {\r
229                                 result = TRUE;\r
230                         }\r
231                 }\r
232         }\r
233         return result;\r
234 }\r
235 \r
236 \r
237 /*\r
238  * Procedure:   checkDtcGroup\r
239  * Description: Return TRUE if "dtc" match the events DTC or "dtc" is\r
240  *                              "DEM_DTC_GROUP_ALL_DTCS" otherwise FALSE.\r
241  */\r
242 static boolean checkDtcGroup(uint32 dtc, const Dem_EventParameterType *eventParam)\r
243 {\r
244         boolean result = FALSE;\r
245 \r
246         if (dtc == DEM_DTC_GROUP_ALL_DTCS) {\r
247                 result = TRUE;\r
248         }\r
249         else {\r
250                 if (eventParam->DTCClassRef != NULL) {\r
251                         if (eventParam->DTCClassRef->DTC == dtc) {\r
252                                 result = TRUE;\r
253                         }\r
254                 }\r
255         }\r
256         return result;\r
257 }\r
258 \r
259 \r
260 /*\r
261  * Procedure:   checkDtcOrigin\r
262  * Description: Return TRUE if "dtcOrigin" match any of the events DTCOrigin otherwise FALSE.\r
263  */\r
264 static boolean checkDtcOrigin(Dem_DTCOriginType dtcOrigin, const Dem_EventParameterType *eventParam)\r
265 {\r
266         boolean result = FALSE;\r
267         boolean dtcOriginFound = FALSE;\r
268         uint16 i;\r
269 \r
270         for (i = 0;(i < DEM_MAX_NR_OF_EVENT_DESTINATION) && (!dtcOriginFound); i++){\r
271                 dtcOriginFound = (eventParam->EventClass->EventDestination[i] == dtcOrigin);\r
272         }\r
273 \r
274         if (dtcOriginFound) {\r
275                 result = TRUE;\r
276         }\r
277 \r
278         return result;\r
279 }\r
280 \r
281 /*\r
282  * Procedure:   checkDtcSeverityMask\r
283  * Description: Return TRUE if "dtcSeverityMask" match any of the events DTC severity otherwise FALSE.\r
284  */\r
285 // PC-Lint (715 etc): Remove errors until function is filled.\r
286 //lint -save -e*\r
287 static boolean checkDtcSeverityMask(Dem_DTCSeverityType dtcSeverityMask, const Dem_EventParameterType *eventParam)\r
288 {\r
289         boolean result = TRUE;\r
290 \r
291         // TODO: This function is optional, may be implemented here.\r
292 \r
293         return result;\r
294 }\r
295 //lint -restore\r
296 \r
297 \r
298 /*\r
299  * Procedure:   checkDtcFaultDetectionCounterMask\r
300  * Description: TBD.\r
301  */\r
302 // PC-Lint (715 etc): Remove errors until function is filled.\r
303 //lint -save -e*\r
304 static boolean checkDtcFaultDetectionCounter(const Dem_EventParameterType *eventParam)\r
305 {\r
306         boolean result = TRUE;\r
307 \r
308         // TODO: Not implemented yet.\r
309 \r
310         return result;\r
311 }\r
312 //lint -restore\r
313 \r
314 /*\r
315  * Procedure:   lookupEventStatusRec\r
316  * Description: Returns the pointer to event id parameters of "eventId" in "*eventStatusBuffer",\r
317  *                              if not found NULL is returned.\r
318  */\r
319 static void lookupEventStatusRec(Dem_EventIdType eventId, EventStatusRecType **const eventStatusRec)\r
320 {\r
321         uint8 i;\r
322         boolean eventIdFound = FALSE;\r
323 \r
324         for (i = 0; (i < DEM_MAX_NUMBER_EVENT) && (!eventIdFound); i++) {\r
325                 eventIdFound = (eventStatusBuffer[i].eventId == eventId);\r
326         }\r
327 \r
328         if (eventIdFound) {\r
329                 *eventStatusRec = &eventStatusBuffer[i-1];\r
330         } else {\r
331                 *eventStatusRec = NULL;\r
332         }\r
333 }\r
334 \r
335 \r
336 /*\r
337  * Procedure:   lookupEventIdParameter\r
338  * Description: Returns the pointer to event id parameters of "eventId" in "*eventIdParam",\r
339  *                              if not found NULL is returned.\r
340  */\r
341 static void lookupEventIdParameter(Dem_EventIdType eventId, const Dem_EventParameterType **const eventIdParam)\r
342 {\r
343         const Dem_EventParameterType *EventIdParamList = configSet->EventParameter;\r
344 \r
345         // Lookup the correct event id parameters\r
346         uint16 i=0;\r
347         while ((EventIdParamList[i].EventID != eventId) && (!EventIdParamList[i].Arc_EOL)) {\r
348                 i++;\r
349         }\r
350 \r
351         if (!EventIdParamList[i].Arc_EOL) {\r
352                 *eventIdParam = &EventIdParamList[i];\r
353         } else {\r
354                 *eventIdParam = NULL;\r
355         }\r
356 }\r
357 \r
358 \r
359 /*\r
360  * Procedure:   preDebounceNone\r
361  * Description: Returns the result of the debouncing.\r
362  */\r
363 static Dem_EventStatusType preDebounceNone(const Dem_EventStatusType reportedStatus, const EventStatusRecType* statusRecord) {\r
364         Dem_EventStatusType returnCode;\r
365         (void)statusRecord;             // Just to get rid of PC-Lint warnings\r
366 \r
367         switch (reportedStatus) {\r
368         case DEM_EVENT_STATUS_FAILED: /** @req DEM091.NoneFailed */\r
369         case DEM_EVENT_STATUS_PASSED: /** @req DEM091.NonePassed */\r
370                 // Already debounced, do nothing.\r
371                 break;\r
372 \r
373         default:\r
374                 // TODO: What to do with PREFAIL and PREPASSED on no debouncing?\r
375                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_PREDEBOUNCE_NONE_ID, DEM_E_PARAM_DATA);\r
376                 break;\r
377         }\r
378 \r
379         returnCode = reportedStatus;\r
380         return returnCode;\r
381 }\r
382 \r
383 \r
384 /*\r
385  * Procedure:   preDebounceCounterBased\r
386  * Description: Returns the result of the debouncing.\r
387  */\r
388 static Dem_EventStatusType preDebounceCounterBased(Dem_EventStatusType reportedStatus, EventStatusRecType* statusRecord) {\r
389         Dem_EventStatusType returnCode;\r
390         const Dem_PreDebounceCounterBasedType* pdVars = statusRecord->eventParamRef->EventClass->PreDebounceAlgorithmClass->PreDebounceAlgorithm.PreDebounceCounterBased;\r
391 \r
392         switch (reportedStatus) {\r
393         case DEM_EVENT_STATUS_PREFAILED:\r
394                 if (statusRecord->faultDetectionCounter < DEBOUNCE_FDC_TEST_FAILED) {\r
395                         if ((pdVars->JumpUp) && (statusRecord->faultDetectionCounter < 0)) {\r
396                                 statusRecord->faultDetectionCounter = 0;\r
397                         } else {\r
398                                 if (((sint16)statusRecord->faultDetectionCounter + (sint8)pdVars->CountInStepSize) < DEBOUNCE_FDC_TEST_FAILED) {\r
399                                         statusRecord->faultDetectionCounter += (sint8)pdVars->CountInStepSize;\r
400                                 } else {\r
401                                         statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_FAILED;\r
402                                 }\r
403                         }\r
404                 }\r
405                 break;\r
406 \r
407         case DEM_EVENT_STATUS_PREPASSED:\r
408                 if (statusRecord->faultDetectionCounter > DEBOUNCE_FDC_TEST_PASSED) {\r
409                         if ((pdVars->JumpDown) && (statusRecord->faultDetectionCounter > 0)) {\r
410                                 statusRecord->faultDetectionCounter = 0;\r
411                         } else {\r
412                                 if (((sint16)statusRecord->faultDetectionCounter - (sint8)pdVars->CountOutStepSize) > DEBOUNCE_FDC_TEST_PASSED) {\r
413                                         statusRecord->faultDetectionCounter -= (sint8)pdVars->CountOutStepSize;\r
414                                 } else {\r
415                                         statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_PASSED;\r
416                                 }\r
417                         }\r
418                 }\r
419                 break;\r
420 \r
421         case DEM_EVENT_STATUS_FAILED:\r
422                 statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_FAILED; /** @req DEM091.CounterFailed */\r
423                 break;\r
424 \r
425         case DEM_EVENT_STATUS_PASSED:\r
426                 statusRecord->faultDetectionCounter = DEBOUNCE_FDC_TEST_PASSED; /** @req DEM091.CounterPassed */\r
427                 break;\r
428 \r
429         default:\r
430                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_PREDEBOUNCE_COUNTER_BASED_ID, DEM_E_PARAM_DATA);\r
431                 break;\r
432 \r
433         }\r
434 \r
435         switch (statusRecord->faultDetectionCounter) {\r
436         case DEBOUNCE_FDC_TEST_FAILED:\r
437                 returnCode = DEM_EVENT_STATUS_FAILED;\r
438                 break;\r
439 \r
440         case DEBOUNCE_FDC_TEST_PASSED:\r
441                 returnCode = DEM_EVENT_STATUS_PASSED;\r
442                 break;\r
443 \r
444         default:\r
445                 returnCode = reportedStatus;\r
446                 break;\r
447         }\r
448 \r
449         return returnCode;\r
450 }\r
451 \r
452 \r
453 /*\r
454  * Procedure:   updateEventStatusRec\r
455  * Description: Update the status of "eventId", if not exist and "createIfNotExist" is\r
456  *                              true a new record is created\r
457  */\r
458 static void updateEventStatusRec(const Dem_EventParameterType *eventParam, Dem_EventStatusType eventStatus, boolean createIfNotExist, EventStatusRecType *eventStatusRec)\r
459 {\r
460         EventStatusRecType *eventStatusRecPtr;\r
461         imask_t state = McuE_EnterCriticalSection();\r
462 \r
463         // Lookup event ID\r
464         lookupEventStatusRec(eventParam->EventID, &eventStatusRecPtr);\r
465 \r
466         if ((eventStatusRecPtr == NULL) && (createIfNotExist)) {\r
467                 // Search for free position\r
468                 lookupEventStatusRec(DEM_EVENT_ID_NULL, &eventStatusRecPtr);\r
469 \r
470                 if (eventStatusRecPtr != NULL) {\r
471                         // Create new event record\r
472                         eventStatusRecPtr->eventId = eventParam->EventID;\r
473                         eventStatusRecPtr->eventParamRef = eventParam;\r
474                         eventStatusRecPtr->faultDetectionCounter = 0;\r
475                         eventStatusRecPtr->occurrence = 0;\r
476                         eventStatusRecPtr->eventStatusExtended = DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE;\r
477                         eventStatusRecPtr->errorStatusChanged = FALSE;\r
478                 }\r
479                 else {\r
480                         // Error: Event status buffer full\r
481                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_UPDATE_EVENT_STATUS_ID, DEM_E_EVENT_STATUS_BUFF_FULL);\r
482                 }\r
483         }\r
484 \r
485 \r
486         if (eventStatusRecPtr != NULL) {\r
487                 // Handle debouncing\r
488                 if (eventParam->EventClass->PreDebounceAlgorithmClass != NULL) {\r
489                         switch (eventParam->EventClass->PreDebounceAlgorithmClass->PreDebounceName) { /** @req DEM004 */ /** @req DEM342 */\r
490                         case DEM_NO_PRE_DEBOUNCE:\r
491                                 eventStatus = preDebounceNone(eventStatus, eventStatusRecPtr);\r
492                                 break;\r
493 \r
494                         case DEM_PRE_DEBOUNCE_COUNTER_BASED:\r
495                                 eventStatus = preDebounceCounterBased(eventStatus, eventStatusRecPtr);\r
496                                 break;\r
497 \r
498                         default:\r
499                                 // Don't know how to handle this.\r
500                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_UPDATE_EVENT_STATUS_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
501                                 break;\r
502                         }\r
503                 }\r
504 \r
505                 eventStatusRecPtr->errorStatusChanged = FALSE;\r
506 \r
507                 // Check test failed\r
508                 if (eventStatus == DEM_EVENT_STATUS_FAILED) {\r
509                         if (!(eventStatusRecPtr->eventStatusExtended & DEM_TEST_FAILED)) {\r
510                                 eventStatusRecPtr->occurrence++;\r
511                                 eventStatusRecPtr->errorStatusChanged = TRUE;\r
512                         }\r
513                         /** @req DEM036 */ /** @req DEM379.PendingSet */\r
514                         eventStatusRecPtr->eventStatusExtended |= (DEM_TEST_FAILED | DEM_TEST_FAILED_THIS_OPERATION_CYCLE | DEM_TEST_FAILED_SINCE_LAST_CLEAR | DEM_PENDING_DTC);\r
515                         eventStatusRecPtr->eventStatusExtended &= (Dem_EventStatusExtendedType)~(DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE);\r
516                 }\r
517 \r
518                 // Check test passed\r
519                 if (eventStatus == DEM_EVENT_STATUS_PASSED) {\r
520                         if (eventStatusRecPtr->eventStatusExtended & DEM_TEST_FAILED) {\r
521                                 eventStatusRecPtr->errorStatusChanged = TRUE;\r
522                         }\r
523                         /** @req DEM036 */\r
524                         eventStatusRecPtr->eventStatusExtended &= (Dem_EventStatusExtendedType)~DEM_TEST_FAILED;\r
525                         eventStatusRecPtr->eventStatusExtended &= (Dem_EventStatusExtendedType)~(DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR | DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE);\r
526                 }\r
527 \r
528                 // Copy the record\r
529                 memcpy(eventStatusRec, eventStatusRecPtr, sizeof(EventStatusRecType));\r
530         }\r
531         else {\r
532                 // Copy an empty record to return data\r
533                 eventStatusRec->eventId = DEM_EVENT_ID_NULL;\r
534                 eventStatusRec->faultDetectionCounter = 0;\r
535                 eventStatusRec->occurrence = 0;\r
536                 eventStatusRec->eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
537                 eventStatusRec->errorStatusChanged = FALSE;\r
538         }\r
539 \r
540         McuE_ExitCriticalSection(state);\r
541 }\r
542 \r
543 \r
544 /*\r
545  * Procedure:   mergeEventStatusRec\r
546  * Description: Update the occurrence counter of status, if not exist a new record is created\r
547  */\r
548 static void mergeEventStatusRec(const EventRecType *eventRec)\r
549 {\r
550         EventStatusRecType *eventStatusRecPtr;\r
551         imask_t state = McuE_EnterCriticalSection();\r
552 \r
553         // Lookup event ID\r
554         lookupEventStatusRec(eventRec->eventId, &eventStatusRecPtr);\r
555 \r
556         if (eventStatusRecPtr != NULL) {\r
557                 // Update occurrence counter, rest of pre init state is kept.\r
558                 eventStatusRecPtr->occurrence += eventRec->occurrence;\r
559 \r
560         }\r
561         else {\r
562                 // Search for free position\r
563                 lookupEventStatusRec(DEM_EVENT_ID_NULL, &eventStatusRecPtr);\r
564 \r
565                 if (eventStatusRecPtr != NULL) {\r
566                         // Create new event, from stored event\r
567                         eventStatusRecPtr->eventId = eventRec->eventId;\r
568                         lookupEventIdParameter(eventRec->eventId, &eventStatusRecPtr->eventParamRef);\r
569                         eventStatusRecPtr->faultDetectionCounter = 0;\r
570                         eventStatusRecPtr->occurrence = eventRec->occurrence;\r
571                         eventStatusRecPtr->eventStatusExtended = DEM_TEST_NOT_COMPLETED_THIS_OPERATION_CYCLE | DEM_TEST_NOT_COMPLETED_SINCE_LAST_CLEAR;\r
572                         eventStatusRecPtr->errorStatusChanged = FALSE;\r
573                 }\r
574                 else {\r
575                         // Error: Event status buffer full\r
576                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_MERGE_EVENT_STATUS_ID, DEM_E_EVENT_STATUS_BUFF_FULL);\r
577                 }\r
578         }\r
579 \r
580         McuE_ExitCriticalSection(state);\r
581 }\r
582 \r
583 \r
584 /*\r
585  * Procedure:   deleteEventStatusRec\r
586  * Description: Delete the status record of "eventParam->eventId" from "eventStatusBuffer".\r
587  */\r
588 static void deleteEventStatusRec(const Dem_EventParameterType *eventParam)\r
589 {\r
590         EventStatusRecType *eventStatusRecPtr;\r
591         imask_t state = McuE_EnterCriticalSection();\r
592 \r
593         // Lookup event ID\r
594         lookupEventStatusRec(eventParam->EventID, &eventStatusRecPtr);\r
595 \r
596         if (eventStatusRecPtr != NULL) {\r
597                 // Delete event record\r
598                 memset(eventStatusRecPtr, 0, sizeof(EventStatusRecType));\r
599         }\r
600 \r
601         McuE_ExitCriticalSection(state);\r
602 }\r
603 \r
604 \r
605 /*\r
606  * Procedure:   getEventStatusRec\r
607  * Description: Returns the status record of "eventId" in "eventStatusRec"\r
608  */\r
609 static void getEventStatusRec(Dem_EventIdType eventId, EventStatusRecType *eventStatusRec)\r
610 {\r
611         EventStatusRecType *eventStatusRecPtr;\r
612 \r
613         // Lookup event ID\r
614         lookupEventStatusRec(eventId, &eventStatusRecPtr);\r
615 \r
616         if (eventStatusRecPtr != NULL) {\r
617                 // Copy the record\r
618                 memcpy(eventStatusRec, eventStatusRecPtr, sizeof(EventStatusRecType));\r
619         }\r
620         else {\r
621                 eventStatusRec->eventId = DEM_EVENT_ID_NULL;\r
622         }\r
623 }\r
624 \r
625 \r
626 /*\r
627  * Procedure:   lookupDtcEvent\r
628  * Description: Returns TRUE if the DTC was found and "eventStatusRec" points\r
629  *                              to the event record found.\r
630  */\r
631 static boolean lookupDtcEvent(uint32 dtc, EventStatusRecType **eventStatusRec)\r
632 {\r
633         boolean dtcFound = FALSE;\r
634         uint16 i;\r
635 \r
636         *eventStatusRec = NULL;\r
637 \r
638         for (i = 0; (i < DEM_MAX_NUMBER_EVENT) && (!dtcFound); i++) {\r
639                 if (eventStatusBuffer[i].eventId != DEM_EVENT_ID_NULL) {\r
640                         if (eventStatusBuffer[i].eventParamRef->DTCClassRef != NULL) {\r
641 \r
642                                 // Check DTC\r
643                                 if (eventStatusBuffer[i].eventParamRef->DTCClassRef->DTC == dtc) {\r
644                                         *eventStatusRec = &eventStatusBuffer[i];\r
645                                         dtcFound = TRUE;\r
646                                 }\r
647                         }\r
648                 }\r
649         }\r
650 \r
651         return dtcFound;\r
652 }\r
653 \r
654 \r
655 /*\r
656  * Procedure:   matchEventWithDtcFilter\r
657  * Description: Returns TRUE if the event pointed by "event" fulfill\r
658  *                              the "dtcFilter" global filter settings.\r
659  */\r
660 static boolean matchEventWithDtcFilter(const EventStatusRecType *eventRec)\r
661 {\r
662         boolean dtcMatch = FALSE;\r
663 \r
664         // Check status\r
665         if ((dtcFilter.dtcStatusMask == DEM_DTC_STATUS_MASK_ALL) || (eventRec->eventStatusExtended & dtcFilter.dtcStatusMask)) {\r
666                 if (eventRec->eventParamRef != NULL) {\r
667 \r
668                         // Check dtcKind\r
669                         if (checkDtcKind(dtcFilter.dtcKind, eventRec->eventParamRef)) {\r
670 \r
671                                 // Check dtcOrigin\r
672                                 if (checkDtcOrigin(dtcFilter.dtcOrigin, eventRec->eventParamRef)) {\r
673 \r
674                                         // Check severity\r
675                                         if ((dtcFilter.filterWithSeverity == DEM_FILTER_WITH_SEVERITY_NO)\r
676                                                 || ((dtcFilter.filterWithSeverity == DEM_FILTER_WITH_SEVERITY_YES) && (checkDtcSeverityMask(dtcFilter.dtcSeverityMask, eventRec->eventParamRef)))) {\r
677 \r
678                                                 // Check fault detection counter\r
679                                                 if ((dtcFilter.filterForFaultDetectionCounter == DEM_FILTER_FOR_FDC_NO)\r
680                                                         || ((dtcFilter.filterWithSeverity == DEM_FILTER_FOR_FDC_YES) && (checkDtcFaultDetectionCounter(eventRec->eventParamRef)))) {\r
681                                                         dtcMatch = TRUE;\r
682                                                 }\r
683                                         }\r
684                                 }\r
685                         }\r
686                 }\r
687         }\r
688 \r
689         return dtcMatch;\r
690 }\r
691 \r
692 \r
693 // PC-Lint (715 etc): Remove errors until function is filled.\r
694 //lint -save -e*\r
695 static void getFreezeFrameData(const Dem_EventParameterType *eventParam, FreezeFrameRecType *freezeFrame)\r
696 {\r
697         // TODO: Fill out\r
698         freezeFrame->eventId = DEM_EVENT_ID_NULL;       // Not supported yet\r
699 }\r
700 //lint -restore\r
701 \r
702 // PC-Lint (715 etc): Remove errors until function is filled.\r
703 //lint -save -e*\r
704 static void storeFreezeFrameDataPreInit(const Dem_EventParameterType *eventParam, const FreezeFrameRecType *freezeFrame)\r
705 {\r
706         // TODO: Fill out\r
707 }\r
708 //lint -restore\r
709 \r
710 // PC-Lint (715 etc): Remove errors until function is filled.\r
711 //lint -save -e*\r
712 static void updateFreezeFrameOccurrencePreInit(const EventRecType *EventBuffer)\r
713 {\r
714         // TODO: Fill out\r
715 }\r
716 //lint -restore  // PC-lint: Restore to original error-setting.\r
717 \r
718 /*\r
719  * Procedure:   getExtendedData\r
720  * Description: Collects the extended data according to "eventParam" and return it in "extData",\r
721  *                              if not found eventId is set to DEM_EVENT_ID_NULL.\r
722  */\r
723 static void getExtendedData(const Dem_EventParameterType *eventParam, ExtDataRecType *extData)\r
724 {\r
725         Std_ReturnType callbackReturnCode;\r
726         uint16 i;\r
727         uint16 storeIndex = 0;\r
728         uint16 recordSize;\r
729 \r
730         // Clear ext data record\r
731         memset(extData, 0, sizeof(ExtDataRecType));\r
732 \r
733         // Check if any pointer to extended data class\r
734         if (eventParam->ExtendedDataClassRef != NULL) {\r
735                 // Request extended data and copy it to the buffer\r
736                 for (i = 0; (i < DEM_MAX_NR_OF_RECORDS_IN_EXTENDED_DATA) && (eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i] != NULL); i++) {\r
737                         recordSize = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i]->DataSize;\r
738                         if ((storeIndex + recordSize) <= DEM_MAX_SIZE_EXT_DATA) {\r
739                                 callbackReturnCode = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef[i]->CallbackGetExtDataRecord(&extData->data[storeIndex]); /** @req DEM282 */\r
740                                 if (callbackReturnCode != E_OK) {\r
741                                         // Callback data currently not available, clear space.\r
742                                         memset(&extData->data[storeIndex], 0xFF, recordSize); // PC-Lint OK\r
743                                 }\r
744                                 storeIndex += recordSize;\r
745                         }\r
746                         else {\r
747                                 // Error: Size of extended data record is bigger than reserved space.\r
748                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GET_EXTENDED_DATA_ID, DEM_E_EXT_DATA_TOO_BIG);\r
749                                 break;  // Break the loop\r
750                         }\r
751                 }\r
752         }\r
753 \r
754         // Check if any data has been stored\r
755         if (storeIndex != 0) {\r
756                 extData->eventId = eventParam->EventID;\r
757                 extData->dataSize = storeIndex;\r
758                 extData->checksum = calcChecksum(extData, sizeof(ExtDataRecType)-sizeof(ChecksumType));\r
759         }\r
760         else {\r
761                 extData->eventId = DEM_EVENT_ID_NULL;\r
762                 extData->dataSize = storeIndex;\r
763                 extData->checksum = 0;\r
764         }\r
765 }\r
766 \r
767 \r
768 /*\r
769  * Procedure:   storeExtendedDataPreInit\r
770  * Description: Store the extended data pointed by "extendedData" to the "preInitExtDataBuffer",\r
771  *                              if non existent a new entry is created.\r
772  */\r
773 static void storeExtendedDataPreInit(const Dem_EventParameterType *eventParam, const ExtDataRecType *extendedData)\r
774 {\r
775         boolean eventIdFound = FALSE;\r
776         boolean eventIdFreePositionFound=FALSE;\r
777         uint16 i;\r
778         imask_t state = McuE_EnterCriticalSection();\r
779 \r
780         // Check if already stored\r
781         for (i = 0; (i<DEM_MAX_NUMBER_EXT_DATA_PRE_INIT) && (!eventIdFound); i++){\r
782                 eventIdFound = (preInitExtDataBuffer[i].eventId == eventParam->EventID);\r
783         }\r
784 \r
785         if(eventIdFound){\r
786                 // Yes, overwrite existing\r
787                 /*lint -esym(669,memcpy) -e826 */ /* supress lintwarning 669 and 826 locally. */\r
788                 memcpy(&preInitExtDataBuffer[i-1], extendedData, sizeof(ExtDataRecType)); // 826,669 PC-Lint: OK. TA BORT ERROR! Lint does not recognise the size of the preInitExtDataBuffer record correctly.\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         // Lookup event ID\r
823         for (i = 0; (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) && (!eventIdFound); i++){\r
824                 eventIdFound = (priMemEventBuffer[i].eventId == eventStatus->eventId);\r
825         }\r
826 \r
827         if (eventIdFound) {\r
828                 // Update event found\r
829                 priMemEventBuffer[i-1].occurrence = eventStatus->occurrence;\r
830                 priMemEventBuffer[i-1].checksum = calcChecksum(&priMemEventBuffer[i-1], sizeof(EventRecType)-sizeof(ChecksumType));\r
831         }\r
832         else {\r
833                 // Search for free position\r
834                 for (i=0; (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) && (!eventIdFreePositionFound); i++){\r
835                         eventIdFreePositionFound = (priMemEventBuffer[i].eventId == DEM_EVENT_ID_NULL);\r
836                 }\r
837 \r
838 \r
839                 if (eventIdFreePositionFound) {\r
840                         priMemEventBuffer[i-1].eventId = eventStatus->eventId;\r
841                         priMemEventBuffer[i-1].occurrence = eventStatus->occurrence;\r
842                         priMemEventBuffer[i-1].checksum = calcChecksum(&priMemEventBuffer[i-1], sizeof(EventRecType)-sizeof(ChecksumType));\r
843                 }\r
844                 else {\r
845                         // Error: Pri mem event buffer full\r
846                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_STORE_EVENT_PRI_MEM_ID, DEM_E_PRI_MEM_EVENT_BUFF_FULL);\r
847                 }\r
848         }\r
849 \r
850         McuE_ExitCriticalSection(state);\r
851 }\r
852 \r
853 /*\r
854  * Procedure:   deleteEventPriMem\r
855  * Description: Delete the event data of "eventParam->eventId" from "priMemEventBuffer".\r
856  */\r
857 static void deleteEventPriMem(const Dem_EventParameterType *eventParam)\r
858 {\r
859         boolean eventIdFound = FALSE;\r
860         uint16 i;\r
861         imask_t state = McuE_EnterCriticalSection();\r
862 \r
863 \r
864         // Lookup event ID\r
865         for (i = 0; (i < DEM_MAX_NUMBER_EVENT_ENTRY_PRI) && (!eventIdFound); i++){\r
866                 eventIdFound = (priMemEventBuffer[i].eventId == eventParam->EventID);\r
867         }\r
868 \r
869         if (eventIdFound) {\r
870                 // Delete event found\r
871                 memset(&priMemEventBuffer[i-1], 0, sizeof(EventRecType));\r
872         }\r
873 \r
874         McuE_ExitCriticalSection(state);\r
875 }\r
876 \r
877 /*\r
878  * Procedure:   storeEventEvtMem\r
879  * Description: Store the event data of "eventStatus->eventId" in event memory according to\r
880  *                              "eventParam" destination option.\r
881  */\r
882 static void storeEventEvtMem(const Dem_EventParameterType *eventParam, const EventStatusRecType *eventStatus)\r
883 {\r
884         uint16 i;\r
885 \r
886         for (i = 0; (i < DEM_MAX_NR_OF_EVENT_DESTINATION)\r
887                                  && (eventParam->EventClass->EventDestination[i] != DEM_EVENT_DESTINATION_END_OF_LIST); i++) {\r
888                 switch (eventParam->EventClass->EventDestination[i])\r
889                 {\r
890                 case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
891                         storeEventPriMem(eventParam, eventStatus);      /** @req DEM010 */\r
892                         break;\r
893 \r
894                 case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
895                 case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
896                 case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
897                         // Not yet supported\r
898                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GLOBAL_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
899                         break;\r
900                 default:\r
901                         break;\r
902                 }\r
903         }\r
904 }\r
905 \r
906 \r
907 /*\r
908  * Procedure:   storeExtendedDataPriMem\r
909  * Description: Store the extended data pointed by "extendedData" to the "priMemExtDataBuffer",\r
910  *                              if non existent a new entry is created.\r
911  */\r
912 static void storeExtendedDataPriMem(const Dem_EventParameterType *eventParam, const ExtDataRecType *extendedData) /** @req DEM041 */\r
913 {\r
914         boolean eventIdFound = FALSE;\r
915         boolean eventIdFreePositionFound=FALSE;\r
916         uint16 i;\r
917         imask_t state = McuE_EnterCriticalSection();\r
918 \r
919         // Check if already stored\r
920         for (i = 0; (i<DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) && (!eventIdFound); i++){\r
921                 eventIdFound = (priMemExtDataBuffer[i].eventId == eventParam->EventID);\r
922         }\r
923 \r
924         if (eventIdFound) {\r
925                 // Yes, overwrite existing\r
926                 memcpy(&priMemExtDataBuffer[i-1], extendedData, sizeof(ExtDataRecType));\r
927         }\r
928         else {\r
929                 // No, lookup first free position\r
930                 for (i = 0; (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) && (!eventIdFreePositionFound); i++){\r
931                         eventIdFreePositionFound =  (priMemExtDataBuffer[i].eventId == DEM_EVENT_ID_NULL);\r
932                 }\r
933                 if (eventIdFreePositionFound) {\r
934                         memcpy(&priMemExtDataBuffer[i-1], extendedData, sizeof(ExtDataRecType));\r
935                 }\r
936                 else {\r
937                         // Error: Pri mem extended data buffer full\r
938                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_STORE_EXT_DATA_PRI_MEM_ID, DEM_E_PRI_MEM_EXT_DATA_BUFF_FULL);\r
939                 }\r
940         }\r
941 \r
942         McuE_ExitCriticalSection(state);\r
943 }\r
944 \r
945 /*\r
946  * Procedure:   deleteExtendedDataPriMem\r
947  * Description: Delete the extended data of "eventParam->eventId" from "priMemExtDataBuffer".\r
948  */\r
949 static void deleteExtendedDataPriMem(const Dem_EventParameterType *eventParam)\r
950 {\r
951         boolean eventIdFound = FALSE;\r
952         uint16 i;\r
953         imask_t state = McuE_EnterCriticalSection();\r
954 \r
955         // Check if already stored\r
956         for (i = 0;(i<DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) && (!eventIdFound); i++){\r
957                 eventIdFound = (priMemExtDataBuffer[i].eventId == eventParam->EventID);\r
958         }\r
959 \r
960         if (eventIdFound) {\r
961                 // Yes, clear record\r
962                 memset(&priMemExtDataBuffer[i-1], 0, sizeof(ExtDataRecType));\r
963         }\r
964 \r
965         McuE_ExitCriticalSection(state);\r
966 }\r
967 \r
968 /*\r
969  * Procedure:   storeExtendedDataEvtMem\r
970  * Description: Store the extended data in event memory according to\r
971  *                              "eventParam" destination option\r
972  */\r
973 static void storeExtendedDataEvtMem(const Dem_EventParameterType *eventParam, const ExtDataRecType *extendedData)\r
974 {\r
975         uint16 i;\r
976 \r
977         for (i = 0; (i < DEM_MAX_NR_OF_EVENT_DESTINATION) && (eventParam->EventClass->EventDestination[i] != DEM_EVENT_DESTINATION_END_OF_LIST); i++) {\r
978                 switch (eventParam->EventClass->EventDestination[i])\r
979                 {\r
980                 case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
981                         storeExtendedDataPriMem(eventParam, extendedData);\r
982                         break;\r
983 \r
984                 case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
985                 case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
986                 case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
987                         // Not yet supported\r
988                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GLOBAL_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
989                         break;\r
990 \r
991                 default:\r
992                         break;\r
993                 }\r
994         }\r
995 }\r
996 \r
997 \r
998 /*\r
999  * Procedure:   lookupExtendedDataRecNumParam\r
1000  * Description: Returns TRUE if the requested extended data number was found among the configured records for the event.\r
1001  *                              "extDataRecClassPtr" returns a pointer to the record class, "posInExtData" returns the position in stored extended data.\r
1002  */\r
1003 static boolean lookupExtendedDataRecNumParam(uint8 extendedDataNumber, const Dem_EventParameterType *eventParam, Dem_ExtendedDataRecordClassType const **extDataRecClassPtr, uint16 *posInExtData)\r
1004 {\r
1005         boolean recNumFound = FALSE;\r
1006 \r
1007         if (eventParam->ExtendedDataClassRef != NULL) {\r
1008                 Dem_ExtendedDataRecordClassType const* const* extDataRecClassRefList = eventParam->ExtendedDataClassRef->ExtendedDataRecordClassRef;\r
1009                 uint16  byteCnt = 0;\r
1010                 uint16 i;\r
1011 \r
1012                 // Request extended data and copy it to the buffer\r
1013                 for (i = 0; (i < DEM_MAX_NR_OF_RECORDS_IN_EXTENDED_DATA) && (extDataRecClassRefList[i] != NULL) && (!recNumFound); i++) {\r
1014                         if (extDataRecClassRefList[i]->RecordNumber == extendedDataNumber) {\r
1015                                 *extDataRecClassPtr =  extDataRecClassRefList[i];\r
1016                                 *posInExtData = byteCnt;\r
1017                                 recNumFound = TRUE;\r
1018                         }\r
1019                         byteCnt += extDataRecClassRefList[i]->DataSize;\r
1020                 }\r
1021         }\r
1022 \r
1023         return recNumFound;\r
1024 }\r
1025 \r
1026 \r
1027 /*\r
1028  * Procedure:   lookupExtendedDataPriMem\r
1029  * Description: Returns TRUE if the requested event id is found, "extData" points to the found data.\r
1030  */\r
1031 static boolean lookupExtendedDataPriMem(Dem_EventIdType eventId, ExtDataRecType **extData)\r
1032 {\r
1033         boolean eventIdFound = FALSE;\r
1034         sint16 i;\r
1035 \r
1036         // Lookup corresponding extended data\r
1037         for (i = 0; (i < DEM_MAX_NUMBER_EXT_DATA_PRI_MEM) && (!eventIdFound); i++) {\r
1038                 eventIdFound = (priMemExtDataBuffer[i].eventId == eventId);\r
1039         }\r
1040 \r
1041         if (eventIdFound) {\r
1042                 // Yes, return pointer\r
1043                 *extData = &priMemExtDataBuffer[i-1];\r
1044         }\r
1045 \r
1046         return eventIdFound;\r
1047 }\r
1048 \r
1049 // PC-Lint (715 etc): Remove errors until function is filled.\r
1050 //lint -save -e*\r
1051 static void storeFreezeFrameDataPriMem(const Dem_EventParameterType *eventParam, const FreezeFrameRecType *freezeFrame)\r
1052 {\r
1053         // TODO: Fill out\r
1054 }\r
1055 //lint -restore\r
1056 \r
1057 // PC-Lint (715 etc): Remove errors until function is filled.\r
1058 //lint -save -e*\r
1059 static void deleteFreezeFrameDataPriMem(const Dem_EventParameterType *eventParam)\r
1060 {\r
1061         // TODO: Fill out\r
1062 }\r
1063 //lint -restore\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                                         if ((DEM_CLEAR_ALL_EVENTS == STD_ON) || (eventParam->DTCClassRef != NULL)) {\r
2029                                                 if (checkDtcKind(dtcKind, eventParam)) {\r
2030                                                         if (checkDtcGroup(dtc, eventParam)) {\r
2031                                                                 boolean dtcOriginFound = FALSE;\r
2032                                                                 for (j = 0; (j < DEM_MAX_NR_OF_EVENT_DESTINATION) && (!dtcOriginFound) ; j++){\r
2033                                                                         dtcOriginFound =(eventParam->EventClass->EventDestination[j] == dtcOrigin);\r
2034                                                                 }\r
2035                                                                 //if (j-1 < DEM_MAX_NR_OF_EVENT_DESTINATION) {\r
2036                                                                 if (dtcOriginFound) {\r
2037                                                                         // Yes! All conditions met.\r
2038                                                                         switch (dtcOrigin)\r
2039                                                                         {\r
2040                                                                         case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
2041                                                                                 /** @req DEM077 */\r
2042                                                                                 deleteEventPriMem(eventParam);\r
2043                                                                                 deleteFreezeFrameDataPriMem(eventParam);\r
2044                                                                                 deleteExtendedDataPriMem(eventParam);\r
2045                                                                                 deleteEventStatusRec(eventParam);               // TODO: Shall this be done or just resetting the status?\r
2046                                                                                 break;\r
2047 \r
2048                                                                         case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
2049                                                                         case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
2050                                                                         case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
2051                                                                                 // Not yet supported\r
2052                                                                                 returnCode = DEM_CLEAR_WRONG_DTCORIGIN;\r
2053                                                                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
2054                                                                                 break;\r
2055                                                                         default:\r
2056                                                                                 returnCode = DEM_CLEAR_WRONG_DTCORIGIN;\r
2057                                                                                 break;\r
2058                                                                         }\r
2059                                                                 }\r
2060                                                         }\r
2061                                                 }\r
2062                                         }\r
2063                                 }\r
2064                                 else {\r
2065                                         // Fatal error, no event parameters found for the stored event!\r
2066                                         DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_UNEXPECTED_EXECUTION);\r
2067                                 }\r
2068                         }\r
2069                 }\r
2070         } else {\r
2071                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_CLEARDTC_ID, DEM_E_UNINIT);\r
2072                 returnCode = DEM_CLEAR_FAILED;\r
2073         }\r
2074 \r
2075         return returnCode;\r
2076 }\r
2077 \r
2078 \r
2079 /*\r
2080  * Procedure:   Dem_DisableDTCStorage\r
2081  * Reentrant:   No\r
2082  */\r
2083 Dem_ReturnControlDTCStorageType Dem_DisableDTCStorage(Dem_DTCGroupType dtcGroup, Dem_DTCKindType dtcKind) /** @req DEM035 */\r
2084 {\r
2085         Dem_ReturnControlDTCStorageType returnCode = DEM_CONTROL_DTC_STORAGE_OK;\r
2086 \r
2087         if (demState == DEM_INITIALIZED) {\r
2088                 // Check dtcGroup parameter\r
2089                 if (dtcGroup == DEM_DTC_GROUP_ALL_DTCS) {\r
2090                         // Check dtcKind parameter\r
2091                         if ((dtcKind == DEM_DTC_KIND_ALL_DTCS) || (dtcKind ==  DEM_DTC_KIND_EMISSION_REL_DTCS)) {\r
2092                                 /** @req DEM079 */\r
2093                                 disableDtcStorage.dtcGroup = dtcGroup;\r
2094                                 disableDtcStorage.dtcKind = dtcKind;\r
2095                                 disableDtcStorage.storageDisabled = TRUE;\r
2096                         } else {\r
2097                                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2098                         }\r
2099                 } else {\r
2100                         returnCode = DEM_CONTROL_DTC_WRONG_DTCGROUP;\r
2101                 }\r
2102         } else {\r
2103                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_DISABLEDTCSTORAGE_ID, DEM_E_UNINIT);\r
2104                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2105         }\r
2106 \r
2107         return returnCode;\r
2108 }\r
2109 \r
2110 \r
2111 /*\r
2112  * Procedure:   Dem_EnableDTCStorage\r
2113  * Reentrant:   No\r
2114  */\r
2115 Dem_ReturnControlDTCStorageType Dem_EnableDTCStorage(Dem_DTCGroupType dtcGroup, Dem_DTCKindType dtcKind)\r
2116 {\r
2117         Dem_ReturnControlDTCStorageType returnCode = DEM_CONTROL_DTC_STORAGE_OK;\r
2118 \r
2119         if (demState == DEM_INITIALIZED) {\r
2120                 // TODO: Behavior is not defined if group or kind do not match active settings, therefore the filter is just switched off.\r
2121                 (void)dtcGroup; (void)dtcKind;  // Just to make get rid of PC-Lint warnings\r
2122                 disableDtcStorage.storageDisabled = FALSE; /** @req DEM080 */\r
2123         } else {\r
2124                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_ENABLEDTCSTORAGE_ID, DEM_E_UNINIT);\r
2125                 returnCode = DEM_CONTROL_DTC_STORAGE_N_OK;\r
2126         }\r
2127 \r
2128         return returnCode;\r
2129 }\r
2130 \r
2131 /*\r
2132  * Procedure:   Dem_GetExtendedDataRecordByDTC\r
2133  * Reentrant:   No\r
2134  */\r
2135 Dem_ReturnGetExtendedDataRecordByDTCType Dem_GetExtendedDataRecordByDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin, uint8 extendedDataNumber, uint8 *destBuffer, uint16 *bufSize)\r
2136 {\r
2137         Dem_ReturnGetExtendedDataRecordByDTCType returnCode = DEM_RECORD_WRONG_DTC;\r
2138         EventStatusRecType *eventRec;\r
2139         Dem_ExtendedDataRecordClassType const *extendedDataRecordClass = NULL;\r
2140         ExtDataRecType *extData;\r
2141         uint16 posInExtData = 0;\r
2142 \r
2143         if (demState == DEM_INITIALIZED) {\r
2144                 if (lookupDtcEvent(dtc, &eventRec)) {\r
2145                         if (checkDtcKind(dtcKind, eventRec->eventParamRef)) {\r
2146                                 if (checkDtcOrigin(dtcOrigin, eventRec->eventParamRef)) {\r
2147                                         if (lookupExtendedDataRecNumParam(extendedDataNumber, eventRec->eventParamRef, &extendedDataRecordClass, &posInExtData)) {\r
2148                                                 if (*bufSize >= extendedDataRecordClass->DataSize) {\r
2149                                                         switch (dtcOrigin)\r
2150                                                         {\r
2151                                                         case DEM_DTC_ORIGIN_PRIMARY_MEMORY:\r
2152                                                                 if (lookupExtendedDataPriMem(eventRec->eventId, &extData)) {\r
2153                                                                         // Yes all conditions met, copy the extended data record to destination buffer.\r
2154                                                                         memcpy(destBuffer, &extData->data[posInExtData], extendedDataRecordClass->DataSize); /** @req DEM075 */ // 960 PC-Lint OK\r
2155                                                                         *bufSize = extendedDataRecordClass->DataSize;\r
2156                                                                         returnCode = DEM_RECORD_OK;\r
2157                                                                 }\r
2158                                                                 else {\r
2159                                                                         // The record number is legal but no record was found for the DTC\r
2160                                                                         *bufSize = 0;\r
2161                                                                         returnCode = DEM_RECORD_OK;\r
2162                                                                 }\r
2163                                                                 break;\r
2164 \r
2165                                                         case DEM_DTC_ORIGIN_SECONDARY_MEMORY:\r
2166                                                         case DEM_DTC_ORIGIN_PERMANENT_MEMORY:\r
2167                                                         case DEM_DTC_ORIGIN_MIRROR_MEMORY:\r
2168                                                                 // Not yet supported\r
2169                                                                 returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2170                                                                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEXTENDEDDATARECORDBYDTC_ID, DEM_E_NOT_IMPLEMENTED_YET);\r
2171                                                                 break;\r
2172                                                         default:\r
2173                                                                 returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2174                                                                 break;\r
2175                                                         }\r
2176                                                 }\r
2177                                                 else {\r
2178                                                         returnCode = DEM_RECORD_BUFFERSIZE;\r
2179                                                 }\r
2180                                         }\r
2181                                         else {\r
2182                                                 returnCode = DEM_RECORD_NUMBER;\r
2183                                         }\r
2184                                 }\r
2185                                 else {\r
2186                                         returnCode = DEM_RECORD_WRONG_DTCORIGIN;\r
2187                                 }\r
2188                         }\r
2189                         else {\r
2190                                 returnCode = DEM_RECORD_DTCKIND;\r
2191                         }\r
2192                 }\r
2193         } else {\r
2194                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETEXTENDEDDATARECORDBYDTC_ID, DEM_E_UNINIT);\r
2195                 returnCode = DEM_RECORD_WRONG_DTC;\r
2196         }\r
2197 \r
2198         return returnCode;\r
2199 }\r
2200 \r
2201 \r
2202 /*\r
2203  * Procedure:   Dem_GetSizeOfExtendedDataRecordByDTC\r
2204  * Reentrant:   No\r
2205  */\r
2206 Dem_ReturnGetSizeOfExtendedDataRecordByDTCType Dem_GetSizeOfExtendedDataRecordByDTC(uint32 dtc, Dem_DTCKindType dtcKind, Dem_DTCOriginType dtcOrigin, uint8 extendedDataNumber, uint16 *sizeOfExtendedDataRecord)\r
2207 {\r
2208         Dem_ReturnGetExtendedDataRecordByDTCType returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTC;\r
2209         EventStatusRecType *eventRec;\r
2210         Dem_ExtendedDataRecordClassType const *extendedDataRecordClass = NULL;\r
2211         uint16 posInExtData;\r
2212 \r
2213         if (demState == DEM_INITIALIZED) {\r
2214                 if (lookupDtcEvent(dtc, &eventRec)) {\r
2215                         if (checkDtcKind(dtcKind, eventRec->eventParamRef)) {\r
2216                                 if (checkDtcOrigin(dtcOrigin, eventRec->eventParamRef)) {\r
2217                                         if (lookupExtendedDataRecNumParam(extendedDataNumber, eventRec->eventParamRef, &extendedDataRecordClass, &posInExtData)) {\r
2218                                                 *sizeOfExtendedDataRecord = extendedDataRecordClass->DataSize; /** @req DEM076 */\r
2219                                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_OK;\r
2220                                         }\r
2221                                         else {\r
2222                                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_RNUM;\r
2223                                         }\r
2224                                 }\r
2225                                 else {\r
2226                                         returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTCOR;\r
2227                                 }\r
2228                         }\r
2229                         else {\r
2230                                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTCKI;\r
2231                         }\r
2232                 }\r
2233         } else {\r
2234                 DET_REPORTERROR(MODULE_ID_DEM, 0, DEM_GETSIZEOFEXTENDEDDATARECORDBYDTC_ID, DEM_E_UNINIT);\r
2235                 returnCode = DEM_GET_SIZEOFEDRBYDTC_W_DTC;\r
2236         }\r
2237 \r
2238         return returnCode;\r
2239 }\r
2240 \r
2241 /***********************************\r
2242  * OBD-specific Interfaces (8.3.6) *\r
2243  ***********************************/\r
2244 \r
2245 \r
2246 \r