1 /*
2  * Percepio DFM v2.1.0
3  * Copyright 2023 Percepio AB
4  * www.percepio.com
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * DFM Entry
9  */
10 
11 #include <dfm.h>
12 #include <string.h>
13 
14 #if ((DFM_CFG_ENABLED) >= 1)
15 
16 #define DFM_ENTRY_VERSION 1
17 
18 typedef struct DfmEntryHeader
19 {
20 	uint8_t cStartMarkers[4];
21 	uint16_t usEndianess;
22 	uint16_t usVersion;
23 	uint16_t usType;
24 	uint16_t usEntryId;
25 	uint16_t usChunkIndex;
26 	uint16_t usChunkCount;
27 	uint32_t ulAlertId;
28 	uint16_t usSessionIdSize;
29 	uint16_t usDeviceNameSize;
30 	uint16_t usDescriptionSize;
31 	uint16_t usReserved;
32 	uint32_t ulDataSize;
33 } DfmEntryHeader_t;
34 
35 typedef struct DfmEntryPayloadHeader
36 {
37 	uint8_t ucStartMarkers[4];		/* The DFM Payload start markers, must be 0x50, 0x44, 0x61, 0x50 ("PDaP") */
38 	uint16_t usEndianness;			/* The endianness checker, assign to 0x0FF0 */
39 	uint8_t ucVersion;				/* The version of the DFM subsystem, current version is 3 */
40 	uint8_t ucFilenameSize;			/* The maximum length of cFilenameBuffer */
41 	uint32_t ulFileSize;			/* The size of the file (buffer) */
42 	char cFilenameBuffer[DFM_PAYLOAD_DESCRIPTION_MAX_LEN]; /* Size will always be 4-byte aligned */
43 	uint8_t ucEndMarkers[4];		/* The DFM Payload end markers, must be 0x50, 0x61, 0x44, 0x50 ("PaDP") */
44 	uint32_t ulChecksum;			/* Checksum on the whole thing, 0 for not enabled */
45 } DfmEntryPayloadHeader_t;
46 
47 typedef struct DfmEntryFooter
48 {
49 	uint8_t cEndMarkers[4];
50 } DfmEntryFooter_t;
51 
52 static DfmEntryData_t* pxDfmEntryData = (void*)0;
53 
54 static DfmResult_t prvDfmEntryVerify(DfmEntryHandle_t xEntryHandle);
55 static uint32_t prvDfmEntryGetHeaderSize(void);
56 static DfmResult_t prvDfmEntrySetup(uint16_t usType, uint16_t usEntryId, uint16_t usChunkIndex, uint16_t usChunkCount, uint32_t ulDescriptionSize, const char* szDescription, void* pvData, uint32_t ulDataSize, DfmEntryHandle_t* pxEntryHandle);
57 
58 /* This function is only used to get around constant "if" condition */
prvDfmEntryGetHeaderSize(void)59 static uint32_t prvDfmEntryGetHeaderSize(void)
60 {
61 	return sizeof(DfmEntryHeader_t);
62 }
63 
prvDfmEntryVerify(DfmEntryHandle_t xEntryHandle)64 static DfmResult_t prvDfmEntryVerify(DfmEntryHandle_t xEntryHandle)
65 {
66 	uint32_t ulEntrySize;
67 	DfmEntryHeader_t* pxEntryHeader;
68 	uint8_t* cEndMarkers;
69 
70 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
71 
72 	if (xDfmEntryGetSize(xEntryHandle, &ulEntrySize) == DFM_FAIL)
73 	{
74 		return DFM_FAIL;
75 	}
76 
77 	if (ulEntrySize > sizeof(pxDfmEntryData->buffer))
78 	{
79 		return DFM_FAIL;
80 	}
81 
82 	if ((pxEntryHeader->cStartMarkers[0] != (uint8_t)0xD1) ||
83 		(pxEntryHeader->cStartMarkers[1] != (uint8_t)0xD2) ||
84 		(pxEntryHeader->cStartMarkers[2] != (uint8_t)0xD3) ||
85 		(pxEntryHeader->cStartMarkers[3] != (uint8_t)0xD4))
86 	{
87 		return DFM_FAIL;
88 	}
89 
90 	if (xDfmEntryGetEndMarkers(xEntryHandle, &cEndMarkers) == DFM_FAIL)
91 	{
92 		return DFM_FAIL;
93 	}
94 
95 	if ((cEndMarkers[0] != (uint8_t)0xD4) ||
96 		(cEndMarkers[1] != (uint8_t)0xD3) ||
97 		(cEndMarkers[2] != (uint8_t)0xD2) ||
98 		(cEndMarkers[3] != (uint8_t)0xD1))
99 	{
100 		return DFM_FAIL;
101 	}
102 
103 	if ((pxEntryHeader->usType != (uint16_t)DFM_ENTRY_TYPE_ALERT) &&
104 		(pxEntryHeader->usType != (uint16_t)DFM_ENTRY_TYPE_PAYLOAD) &&
105 		(pxEntryHeader->usType != (uint16_t)DFM_ENTRY_TYPE_PAYLOAD_HEADER))
106 	{
107 		return DFM_FAIL;
108 	}
109 
110 	if (pxEntryHeader->usVersion > (uint16_t)DFM_ENTRY_VERSION)
111 	{
112 		return DFM_FAIL;
113 	}
114 
115 	if (pxEntryHeader->usChunkIndex == (uint16_t)0)
116 	{
117 		return DFM_FAIL;
118 	}
119 
120 	if (pxEntryHeader->usChunkIndex > pxEntryHeader->usChunkCount)
121 	{
122 		return DFM_FAIL;
123 	}
124 
125 	return DFM_SUCCESS;
126 }
127 
prvDfmEntrySetup(uint16_t usType,uint16_t usEntryId,uint16_t usChunkIndex,uint16_t usChunkCount,uint32_t ulDescriptionSize,const char * szDescription,void * pvData,uint32_t ulDataSize,DfmEntryHandle_t * pxEntryHandle)128 static DfmResult_t prvDfmEntrySetup(uint16_t usType, uint16_t usEntryId, uint16_t usChunkIndex, uint16_t usChunkCount, uint32_t ulDescriptionSize, const char* szDescription, void* pvData, uint32_t ulDataSize, DfmEntryHandle_t* pxEntryHandle)
129 {
130 	DfmEntryHeader_t* pxEntryHeader = (DfmEntryHeader_t*)pxDfmEntryData->buffer; /*cstat !MISRAC2012-Rule-11.3 We convert the untyped buffer to something we can work with. We can't use an exact type since the buffer may need to contain bigger Entries that haven been previously stored*/
131 	char* szSessionId = (void*)0;
132 	uint32_t ulOffset = 0UL;
133 	uint32_t ulAlertId = 0UL;
134 	const char* szDeviceName = (void*)0;
135 
136 	if (szDescription == (void*)0)
137 	{
138 		return DFM_FAIL;
139 	}
140 
141 	if (szDescription[0] == (char)0)
142 	{
143 		return DFM_FAIL;
144 	}
145 
146 	if (pvData == (void*)0)
147 	{
148 		return DFM_FAIL;
149 	}
150 
151 	if (ulDataSize == 0UL)
152 	{
153 		return DFM_FAIL;
154 	}
155 
156 	if (pxEntryHandle == (void*)0)
157 	{
158 		return DFM_FAIL;
159 	}
160 
161 	if (usChunkIndex == (uint16_t)0)
162 	{
163 		return DFM_FAIL;
164 	}
165 
166 	if (usChunkIndex > usChunkCount)
167 	{
168 		return DFM_FAIL;
169 	}
170 
171 	if (xDfmSessionGetUniqueSessionId(&szSessionId) == DFM_FAIL)
172 	{
173 		return DFM_FAIL;
174 	}
175 
176 	if (xDfmSessionGetAlertId(&ulAlertId) == DFM_FAIL)
177 	{
178 		return DFM_FAIL;
179 	}
180 
181 	if (xDfmSessionGetDeviceName(&szDeviceName) == DFM_FAIL)
182 	{
183 		return DFM_FAIL;
184 	}
185 
186 	pxEntryHeader->cStartMarkers[0] = 0xD1;
187 	pxEntryHeader->cStartMarkers[1] = 0xD2;
188 	pxEntryHeader->cStartMarkers[2] = 0xD3;
189 	pxEntryHeader->cStartMarkers[3] = 0xD4;
190 
191 	pxEntryHeader->usEndianess = 0x0FF0;
192 	pxEntryHeader->usVersion = DFM_ENTRY_VERSION;
193 	pxEntryHeader->usType = usType;
194 	pxEntryHeader->usEntryId = usEntryId;
195 	pxEntryHeader->usChunkIndex = usChunkIndex;
196 	pxEntryHeader->usChunkCount = usChunkCount;
197 	pxEntryHeader->ulAlertId = ulAlertId;
198 	pxEntryHeader->usSessionIdSize = DFM_SESSION_ID_MAX_LEN;
199 	pxEntryHeader->usDeviceNameSize = DFM_DEVICE_NAME_MAX_LEN;
200 	pxEntryHeader->usDescriptionSize = (uint16_t)ulDescriptionSize;
201 	pxEntryHeader->ulDataSize = ulDataSize;
202 
203 	ulOffset += sizeof(DfmEntryHeader_t);
204 
205 	/* Write SessionId after Header */
206 	(void)memcpy((char*)((uintptr_t)pxDfmEntryData->buffer + ulOffset), szSessionId, DFM_SESSION_ID_MAX_LEN); /*cstat !MISRAC2012-Rule-11.6 We need to write the Session Id to the buffer with an offset*/
207 
208 	ulOffset += (uint32_t)(DFM_SESSION_ID_MAX_LEN);
209 
210 	/* Write DeviceName after SessionId */
211 	(void)memcpy((char*)((uintptr_t)pxDfmEntryData->buffer + ulOffset), szDeviceName, DFM_DEVICE_NAME_MAX_LEN); /*cstat !MISRAC2012-Rule-11.6 We need to write the Device Name to the buffer with an offset*/
212 
213 	ulOffset += (uint32_t)(DFM_DEVICE_NAME_MAX_LEN);
214 
215 	/* Write Description after DeviceName */
216 	(void)memcpy((char*)((uintptr_t)pxDfmEntryData->buffer + ulOffset), szDescription, ulDescriptionSize); /*cstat !MISRAC2012-Rule-11.6 We need to write the Description to the buffer with an offset*/
217 
218 	ulOffset += ulDescriptionSize;
219 
220 	/* Write Data after Description */
221 	(void)memcpy((void*)((uintptr_t)pxDfmEntryData->buffer + ulOffset), pvData, ulDataSize); /*cstat !MISRAC2012-Rule-11.6 We need to write the Entry data to the buffer with an offset*/
222 
223 	ulOffset += ulDataSize;
224 
225 	/* Write Footer after Data */
226 	DfmEntryFooter_t* pxEntryFooter = (DfmEntryFooter_t*)((uintptr_t)pxDfmEntryData->buffer + ulOffset);
227 	pxEntryFooter->cEndMarkers[0] = pxEntryHeader->cStartMarkers[3];
228 	pxEntryFooter->cEndMarkers[1] = pxEntryHeader->cStartMarkers[2];
229 	pxEntryFooter->cEndMarkers[2] = pxEntryHeader->cStartMarkers[1];
230 	pxEntryFooter->cEndMarkers[3] = pxEntryHeader->cStartMarkers[0];
231 
232 	*pxEntryHandle = (DfmEntryHandle_t)pxEntryHeader;
233 
234 	return DFM_SUCCESS;
235 }
236 
237 /*
238  * Since the sizes are dynamic, an Entry will be stored and read by interpreting consecutive parts as:
239  * Alert: DfmEntryHeader_t -> session[ulSessionIdSize] -> description[ulDescriptionSize] -> alert[ulDataSize] -> DfmEntryFooter_t
240  * Payload: DfmEntryHeader_t -> session[ulSessionIdSize] -> description[ulDescriptionSize] -> payload[ulDataSize] -> DfmEntryFooter_t
241  */
xDfmEntryInitialize(DfmEntryData_t * pxBuffer)242 DfmResult_t xDfmEntryInitialize(DfmEntryData_t* pxBuffer)
243 {
244 	if (pxBuffer == (void*)0)
245 	{
246 		return DFM_FAIL;
247 	}
248 
249 	pxDfmEntryData = pxBuffer;
250 
251 	/* Verify that entry buffer can hold alert size. Calls prvDfmEntryGetHeaderSize() to avoid constant "if" condition. */
252 	if ((prvDfmEntryGetHeaderSize() + (uint32_t)(DFM_SESSION_ID_MAX_LEN) + (uint32_t)(DFM_DESCRIPTION_MAX_LEN) + (uint32_t)(DFM_DEVICE_NAME_MAX_LEN) + sizeof(DfmAlert_t) + sizeof(DfmEntryFooter_t)) > sizeof(pxDfmEntryData->buffer))
253 	{
254 		return DFM_FAIL;
255 	}
256 
257 	/* Verify that entry buffer can hold payload size. Calls prvDfmEntryGetHeaderSize() to avoid constant "if" condition. */
258 	if ((prvDfmEntryGetHeaderSize() + (uint32_t)(DFM_SESSION_ID_MAX_LEN) + (uint32_t)(DFM_PAYLOAD_DESCRIPTION_MAX_LEN) + (uint32_t)(DFM_DEVICE_NAME_MAX_LEN) + (uint32_t)(DFM_CFG_MAX_PAYLOAD_CHUNK_SIZE) + sizeof(DfmEntryFooter_t)) > sizeof(pxDfmEntryData->buffer))
259 	{
260 		return DFM_FAIL;
261 	}
262 
263 	pxDfmEntryData->ulInitialized = 1;
264 
265 	return DFM_SUCCESS;
266 }
267 
xDfmEntryGetBuffer(void ** ppvBuffer,uint32_t * pulBufferSize)268 DfmResult_t xDfmEntryGetBuffer(void** ppvBuffer, uint32_t* pulBufferSize)
269 {
270 	if (pxDfmEntryData == (void*)0)
271 	{
272 		return DFM_FAIL;
273 	}
274 
275 	if (pxDfmEntryData->ulInitialized == 0UL)
276 	{
277 		return DFM_FAIL;
278 	}
279 
280 	if (ppvBuffer == (void*)0)
281 	{
282 		return DFM_FAIL;
283 	}
284 
285 	if (pulBufferSize == (void*)0)
286 	{
287 		return DFM_FAIL;
288 	}
289 
290 	*ppvBuffer = pxDfmEntryData->buffer;
291 	*pulBufferSize = sizeof(pxDfmEntryData->buffer);
292 
293 	return DFM_SUCCESS;
294 }
295 
xDfmEntryCreateAlert(DfmAlertHandle_t xAlertHandle,DfmEntryHandle_t * pxEntryHandle)296 DfmResult_t xDfmEntryCreateAlert(DfmAlertHandle_t xAlertHandle, DfmEntryHandle_t *pxEntryHandle)
297 {
298 	const char* szDescription = (void*)0;
299 
300 	if (pxDfmEntryData == (void*)0)
301 	{
302 		return DFM_FAIL;
303 	}
304 
305 	if (pxDfmEntryData->ulInitialized == 0UL)
306 	{
307 		return DFM_FAIL;
308 	}
309 
310 	if (xAlertHandle == 0)
311 	{
312 		return DFM_FAIL;
313 	}
314 
315 	if (xDfmAlertGetDescription(xAlertHandle, &szDescription) == DFM_FAIL)
316 	{
317 		return DFM_FAIL;
318 	}
319 
320 	if (prvDfmEntrySetup((uint16_t)(DFM_ENTRY_TYPE_ALERT), (uint16_t)0, (uint16_t)1, (uint16_t)1, (uint32_t)(DFM_DESCRIPTION_MAX_LEN), szDescription, (void*)xAlertHandle, sizeof(DfmAlert_t), pxEntryHandle) == DFM_FAIL)
321 	{
322 		return DFM_FAIL;
323 	}
324 
325 	return DFM_SUCCESS;
326 }
327 
xDfmEntryCreatePayloadHeader(DfmAlertHandle_t xAlertHandle,uint16_t usEntryId,uint32_t ulPayloadSize,char * szDescription,DfmEntryHandle_t * pxEntryHandle)328 DfmResult_t xDfmEntryCreatePayloadHeader(DfmAlertHandle_t xAlertHandle, uint16_t usEntryId, uint32_t ulPayloadSize, char* szDescription, DfmEntryHandle_t* pxEntryHandle)
329 {
330 	DfmEntryPayloadHeader_t xPayloadHeader = { 0 };
331 	uint32_t i;
332 
333 	if (pxDfmEntryData == (void*)0)
334 	{
335 		return DFM_FAIL;
336 	}
337 
338 	if (pxDfmEntryData->ulInitialized == 0UL)
339 	{
340 		return DFM_FAIL;
341 	}
342 
343 	if (xAlertHandle == 0)
344 	{
345 		return DFM_FAIL;
346 	}
347 
348 	if (ulPayloadSize == 0UL)
349 	{
350 		return DFM_FAIL;
351 	}
352 
353 	if (szDescription == (void*)0)
354 	{
355 		return DFM_FAIL;
356 	}
357 
358 	if (szDescription[0] == (char)0)
359 	{
360 		return DFM_FAIL;
361 	}
362 
363 	if (pxEntryHandle == (void*)0)
364 	{
365 		return DFM_FAIL;
366 	}
367 
368 	xPayloadHeader.ucStartMarkers[0] = 0x50;
369 	xPayloadHeader.ucStartMarkers[1] = 0x44;
370 	xPayloadHeader.ucStartMarkers[2] = 0x61;
371 	xPayloadHeader.ucStartMarkers[3] = 0x50;
372 	xPayloadHeader.usEndianness = 0x0FF0;
373 	xPayloadHeader.ucVersion = DFM_VERSION;
374 	xPayloadHeader.ucFilenameSize = (uint8_t)sizeof(xPayloadHeader.cFilenameBuffer);
375 	xPayloadHeader.ulFileSize = ulPayloadSize;
376 
377 	for (i = 0; i < sizeof(xPayloadHeader.cFilenameBuffer); i++)
378 	{
379 		xPayloadHeader.cFilenameBuffer[i] = szDescription[i];
380 
381 		if (xPayloadHeader.cFilenameBuffer[i] == (char)0)
382 		{
383 			break;
384 		}
385 	}
386 
387 	xPayloadHeader.ucEndMarkers[0] = 0x50;
388 	xPayloadHeader.ucEndMarkers[1] = 0x61;
389 	xPayloadHeader.ucEndMarkers[2] = 0x44;
390 	xPayloadHeader.ucEndMarkers[3] = 0x50;
391 	xPayloadHeader.ulChecksum = 0;
392 
393 	if (prvDfmEntrySetup((uint16_t)(DFM_ENTRY_TYPE_PAYLOAD_HEADER), usEntryId, (uint16_t)1, (uint16_t)1, (uint32_t)(DFM_PAYLOAD_DESCRIPTION_MAX_LEN), szDescription, &xPayloadHeader, sizeof(xPayloadHeader), pxEntryHandle) == DFM_FAIL)
394 	{
395 		return DFM_FAIL;
396 	}
397 
398 	return DFM_SUCCESS;
399 }
400 
xDfmEntryCreatePayloadChunk(DfmAlertHandle_t xAlertHandle,uint16_t usEntryId,uint16_t usChunkIndex,uint16_t usChunkCount,void * pvPayload,uint32_t ulSize,char * szDescription,DfmEntryHandle_t * pxEntryHandle)401 DfmResult_t xDfmEntryCreatePayloadChunk(DfmAlertHandle_t xAlertHandle, uint16_t usEntryId, uint16_t usChunkIndex, uint16_t usChunkCount, void* pvPayload, uint32_t ulSize, char* szDescription, DfmEntryHandle_t* pxEntryHandle)
402 {
403 	if (pxDfmEntryData == (void*)0)
404 	{
405 		return DFM_FAIL;
406 	}
407 
408 	if (pxDfmEntryData->ulInitialized == 0UL)
409 	{
410 		return DFM_FAIL;
411 	}
412 
413 	if (xAlertHandle == 0)
414 	{
415 		return DFM_FAIL;
416 	}
417 
418 	if (prvDfmEntrySetup((uint16_t)(DFM_ENTRY_TYPE_PAYLOAD), usEntryId, usChunkIndex, usChunkCount, (uint32_t)(DFM_PAYLOAD_DESCRIPTION_MAX_LEN), szDescription, pvPayload, ulSize, pxEntryHandle) == DFM_FAIL)
419 	{
420 		return DFM_FAIL;
421 	}
422 
423 	return DFM_SUCCESS;
424 }
425 
xDfmEntryCreateAlertFromBuffer(DfmEntryHandle_t * pxEntryHandle)426 DfmResult_t xDfmEntryCreateAlertFromBuffer(DfmEntryHandle_t* pxEntryHandle)
427 {
428 	DfmEntryHeader_t* pxEntryHeader;
429 
430 	if (pxDfmEntryData == (void*)0)
431 	{
432 		return DFM_FAIL;
433 	}
434 
435 	if (pxDfmEntryData->ulInitialized == 0UL)
436 	{
437 		return DFM_FAIL;
438 	}
439 
440 	if (pxEntryHandle == (void*)0)
441 	{
442 		return DFM_FAIL;
443 	}
444 
445 	/* We assume the data is in the buffer */
446 
447 	pxEntryHeader = (DfmEntryHeader_t*)pxDfmEntryData->buffer; /*cstat !MISRAC2012-Rule-11.3 We convert the untyped buffer to something we can work with. We can't use an exact type since the buffer may need to contain bigger Entries that haven been previously stored*/
448 
449 	if (prvDfmEntryVerify((DfmEntryHandle_t)pxEntryHeader) == DFM_FAIL)
450 	{
451 		return DFM_FAIL;
452 	}
453 
454 	if (pxEntryHeader->usType != (uint16_t)(DFM_ENTRY_TYPE_ALERT))
455 	{
456 		return DFM_FAIL;
457 	}
458 
459 	*pxEntryHandle = (DfmEntryHandle_t)pxEntryHeader;
460 
461 	return DFM_SUCCESS;
462 }
463 
xDfmEntryCreatePayloadChunkFromBuffer(const char * szSessionId,uint32_t ulAlertId,DfmEntryHandle_t * pxEntryHandle)464 DfmResult_t xDfmEntryCreatePayloadChunkFromBuffer(const char* szSessionId, uint32_t ulAlertId, DfmEntryHandle_t* pxEntryHandle)
465 {
466 	DfmEntryHeader_t* pxEntryHeader;
467 	const char* szPayloadSession = (void*)0;
468 	uint32_t i;
469 
470 	if (pxDfmEntryData == (void*)0)
471 	{
472 		return DFM_FAIL;
473 	}
474 
475 	if (pxDfmEntryData->ulInitialized == 0UL)
476 	{
477 		return DFM_FAIL;
478 	}
479 
480 	if (pxEntryHandle == (void*)0)
481 	{
482 		return DFM_FAIL;
483 	}
484 
485 	/* Make sure szSessiondId is not pointing to a string inside the buffer since that means this function is incorrectly used! */
486 	if (((uintptr_t)szSessionId >= (uintptr_t)pxDfmEntryData->buffer) && ((uintptr_t)szSessionId < (uintptr_t)pxDfmEntryData->buffer + sizeof(pxDfmEntryData->buffer)))
487 	{
488 		return DFM_FAIL;
489 	}
490 
491 	/* We assume the data is in the buffer */
492 
493 	pxEntryHeader = (DfmEntryHeader_t*)pxDfmEntryData->buffer; /*cstat !MISRAC2012-Rule-11.3 We convert the untyped buffer to something we can work with. We can't use an exact type since the buffer may need to contain bigger Entries that haven been previously stored*/
494 
495 	if (prvDfmEntryVerify((DfmEntryHandle_t)pxEntryHeader) == DFM_FAIL)
496 	{
497 		return DFM_FAIL;
498 	}
499 
500 	if (pxEntryHeader->usType != (uint16_t)(DFM_ENTRY_TYPE_PAYLOAD) && pxEntryHeader->usType != (uint16_t)(DFM_ENTRY_TYPE_PAYLOAD_HEADER))
501 	{
502 		return DFM_FAIL;
503 	}
504 
505 	/* Make sure stored alert id matches what we asked for */
506 	if (pxEntryHeader->ulAlertId != ulAlertId)
507 	{
508 		return DFM_FAIL;
509 	}
510 
511 	if (xDfmEntryGetSessionId((DfmEntryHandle_t)pxEntryHeader, &szPayloadSession) == DFM_FAIL)
512 	{
513 		return DFM_FAIL;
514 	}
515 
516 	/* Make sure stored session matches what we asked for */
517 	for (i = 0; i < (uint32_t)(DFM_SESSION_ID_MAX_LEN); i++)
518 	{
519 		if (szSessionId[i] != szPayloadSession[i])
520 		{
521 			return DFM_FAIL;
522 		}
523 
524 		if (szSessionId[i] == (char)0)
525 		{
526 			break;
527 		}
528 	}
529 
530 	*pxEntryHandle = (DfmEntryHandle_t)pxEntryHeader;
531 
532 	return DFM_SUCCESS;
533 }
534 
xDfmEntryGetSize(DfmEntryHandle_t xEntryHandle,uint32_t * pulSize)535 DfmResult_t xDfmEntryGetSize(DfmEntryHandle_t xEntryHandle, uint32_t *pulSize)
536 {
537 	DfmEntryHeader_t* pxEntryHeader;
538 
539 	if (pxDfmEntryData == (void*)0)
540 	{
541 		return DFM_FAIL;
542 	}
543 
544 	if (pxDfmEntryData->ulInitialized == 0UL)
545 	{
546 		return DFM_FAIL;
547 	}
548 
549 	if (xEntryHandle == 0)
550 	{
551 		return DFM_FAIL;
552 	}
553 
554 	if (pulSize == (void*)0)
555 	{
556 		return DFM_FAIL;
557 	}
558 
559 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
560 
561 	*pulSize = sizeof(DfmEntryHeader_t) + pxEntryHeader->usSessionIdSize + pxEntryHeader->usDeviceNameSize + pxEntryHeader->usDescriptionSize + pxEntryHeader->ulDataSize + sizeof(DfmEntryFooter_t);
562 
563 	return DFM_SUCCESS;
564 }
565 
xDfmEntryGetStartMarkers(DfmEntryHandle_t xEntryHandle,uint8_t ** pucMarkersBuffer)566 DfmResult_t xDfmEntryGetStartMarkers(DfmEntryHandle_t xEntryHandle, uint8_t** pucMarkersBuffer)
567 {
568 	DfmEntryHeader_t* pxEntryHeader;
569 
570 	if (pxDfmEntryData == (void*)0)
571 	{
572 		return DFM_FAIL;
573 	}
574 
575 	if (pxDfmEntryData->ulInitialized == 0UL)
576 	{
577 		return DFM_FAIL;
578 	}
579 
580 	if (xEntryHandle == 0)
581 	{
582 		return DFM_FAIL;
583 	}
584 
585 	if (pucMarkersBuffer == (void*)0)
586 	{
587 		return DFM_FAIL;
588 	}
589 
590 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
591 
592 	*pucMarkersBuffer = pxEntryHeader->cStartMarkers;
593 
594 	return DFM_SUCCESS;
595 }
596 
xDfmEntryGetEndianess(DfmEntryHandle_t xEntryHandle,uint16_t * pusEndianess)597 DfmResult_t xDfmEntryGetEndianess(DfmEntryHandle_t xEntryHandle, uint16_t* pusEndianess)
598 {
599 	DfmEntryHeader_t* pxEntryHeader;
600 
601 	if (pxDfmEntryData == (void*)0)
602 	{
603 		return DFM_FAIL;
604 	}
605 
606 	if (pxDfmEntryData->ulInitialized == 0UL)
607 	{
608 		return DFM_FAIL;
609 	}
610 
611 	if (xEntryHandle == 0)
612 	{
613 		return DFM_FAIL;
614 	}
615 
616 	if (pusEndianess == (void*)0)
617 	{
618 		return DFM_FAIL;
619 	}
620 
621 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
622 
623 	*pusEndianess = pxEntryHeader->usEndianess;
624 
625 	return DFM_SUCCESS;
626 }
627 
xDfmEntryGetVersion(DfmEntryHandle_t xEntryHandle,uint16_t * pusVersion)628 DfmResult_t xDfmEntryGetVersion(DfmEntryHandle_t xEntryHandle, uint16_t* pusVersion)
629 {
630 	DfmEntryHeader_t* pxEntryHeader;
631 
632 	if (pxDfmEntryData == (void*)0)
633 	{
634 		return DFM_FAIL;
635 	}
636 
637 	if (pxDfmEntryData->ulInitialized == 0UL)
638 	{
639 		return DFM_FAIL;
640 	}
641 
642 	if (xEntryHandle == 0)
643 	{
644 		return DFM_FAIL;
645 	}
646 
647 	if (pusVersion == (void*)0)
648 	{
649 		return DFM_FAIL;
650 	}
651 
652 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
653 
654 	*pusVersion = pxEntryHeader->usVersion;
655 
656 	return DFM_SUCCESS;
657 }
658 
xDfmEntryGetType(DfmEntryHandle_t xEntryHandle,uint16_t * pusType)659 DfmResult_t xDfmEntryGetType(DfmEntryHandle_t xEntryHandle, uint16_t* pusType)
660 {
661 	DfmEntryHeader_t* pxEntryHeader;
662 
663 	if (pxDfmEntryData == (void*)0)
664 	{
665 		return DFM_FAIL;
666 	}
667 
668 	if (pxDfmEntryData->ulInitialized == 0UL)
669 	{
670 		return DFM_FAIL;
671 	}
672 
673 	if (xEntryHandle == 0)
674 	{
675 		return DFM_FAIL;
676 	}
677 
678 	if (pusType == (void*)0)
679 	{
680 		return DFM_FAIL;
681 	}
682 
683 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
684 
685 	*pusType = pxEntryHeader->usType;
686 
687 	return DFM_SUCCESS;
688 }
689 
xDfmEntryGetEntryId(DfmEntryHandle_t xEntryHandle,uint16_t * pusEntryId)690 DfmResult_t xDfmEntryGetEntryId(DfmEntryHandle_t xEntryHandle, uint16_t* pusEntryId)
691 {
692 	DfmEntryHeader_t* pxEntryHeader;
693 
694 	if (pxDfmEntryData == (void*)0)
695 	{
696 		return DFM_FAIL;
697 	}
698 
699 	if (pxDfmEntryData->ulInitialized == 0UL)
700 	{
701 		return DFM_FAIL;
702 	}
703 
704 	if (xEntryHandle == 0)
705 	{
706 		return DFM_FAIL;
707 	}
708 
709 	if (pusEntryId == (void*)0)
710 	{
711 		return DFM_FAIL;
712 	}
713 
714 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
715 
716 	*pusEntryId = pxEntryHeader->usEntryId;
717 
718 	return DFM_SUCCESS;
719 }
720 
xDfmEntryGetChunkIndex(DfmEntryHandle_t xEntryHandle,uint16_t * pusChunkIndex)721 DfmResult_t xDfmEntryGetChunkIndex(DfmEntryHandle_t xEntryHandle, uint16_t* pusChunkIndex)
722 {
723 	DfmEntryHeader_t* pxEntryHeader;
724 
725 	if (pxDfmEntryData == (void*)0)
726 	{
727 		return DFM_FAIL;
728 	}
729 
730 	if (pxDfmEntryData->ulInitialized == 0UL)
731 	{
732 		return DFM_FAIL;
733 	}
734 
735 	if (xEntryHandle == 0)
736 	{
737 		return DFM_FAIL;
738 	}
739 
740 	if (pusChunkIndex == (void*)0)
741 	{
742 		return DFM_FAIL;
743 	}
744 
745 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
746 
747 	*pusChunkIndex = pxEntryHeader->usChunkIndex;
748 
749 	return DFM_SUCCESS;
750 }
751 
xDfmEntryGetChunkCount(DfmEntryHandle_t xEntryHandle,uint16_t * pusChunkCount)752 DfmResult_t xDfmEntryGetChunkCount(DfmEntryHandle_t xEntryHandle, uint16_t* pusChunkCount)
753 {
754 	DfmEntryHeader_t* pxEntryHeader;
755 
756 	if (pxDfmEntryData == (void*)0)
757 	{
758 		return DFM_FAIL;
759 	}
760 
761 	if (pxDfmEntryData->ulInitialized == 0UL)
762 	{
763 		return DFM_FAIL;
764 	}
765 
766 	if (xEntryHandle == 0)
767 	{
768 		return DFM_FAIL;
769 	}
770 
771 	if (pusChunkCount == (void*)0)
772 	{
773 		return DFM_FAIL;
774 	}
775 
776 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
777 
778 	*pusChunkCount = pxEntryHeader->usChunkCount;
779 
780 	return DFM_SUCCESS;
781 }
782 
xDfmEntryGetSessionIdSize(DfmEntryHandle_t xEntryHandle,uint16_t * pusSize)783 DfmResult_t xDfmEntryGetSessionIdSize(DfmEntryHandle_t xEntryHandle, uint16_t* pusSize)
784 {
785 	DfmEntryHeader_t* pxEntryHeader;
786 
787 	if (pxDfmEntryData == (void*)0)
788 	{
789 		return DFM_FAIL;
790 	}
791 
792 	if (pxDfmEntryData->ulInitialized == 0UL)
793 	{
794 		return DFM_FAIL;
795 	}
796 
797 	if (xEntryHandle == 0)
798 	{
799 		return DFM_FAIL;
800 	}
801 
802 	if (pusSize == (void*)0)
803 	{
804 		return DFM_FAIL;
805 	}
806 
807 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
808 
809 	*pusSize = pxEntryHeader->usSessionIdSize;
810 
811 	return DFM_SUCCESS;
812 }
813 
xDfmEntryGetDeviceNameSize(DfmEntryHandle_t xEntryHandle,uint16_t * pusSize)814 DfmResult_t xDfmEntryGetDeviceNameSize(DfmEntryHandle_t xEntryHandle, uint16_t* pusSize)
815 {
816 	DfmEntryHeader_t* pxEntryHeader;
817 
818 	if (pxDfmEntryData == (void*)0)
819 	{
820 		return DFM_FAIL;
821 	}
822 
823 	if (pxDfmEntryData->ulInitialized == 0UL)
824 	{
825 		return DFM_FAIL;
826 	}
827 
828 	if (xEntryHandle == 0)
829 	{
830 		return DFM_FAIL;
831 	}
832 
833 	if (pusSize == (void*)0)
834 	{
835 		return DFM_FAIL;
836 	}
837 
838 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
839 
840 	*pusSize = pxEntryHeader->usDeviceNameSize;
841 
842 	return DFM_SUCCESS;
843 }
844 
xDfmEntryGetDescriptionSize(DfmEntryHandle_t xEntryHandle,uint16_t * pusSize)845 DfmResult_t xDfmEntryGetDescriptionSize(DfmEntryHandle_t xEntryHandle, uint16_t* pusSize)
846 {
847 	DfmEntryHeader_t* pxEntryHeader;
848 
849 	if (pxDfmEntryData == (void*)0)
850 	{
851 		return DFM_FAIL;
852 	}
853 
854 	if (pxDfmEntryData->ulInitialized == 0UL)
855 	{
856 		return DFM_FAIL;
857 	}
858 
859 	if (xEntryHandle == 0)
860 	{
861 		return DFM_FAIL;
862 	}
863 
864 	if (pusSize == (void*)0)
865 	{
866 		return DFM_FAIL;
867 	}
868 
869 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
870 
871 	*pusSize = pxEntryHeader->usDescriptionSize;
872 
873 	return DFM_SUCCESS;
874 }
875 
xDfmEntryGetDataSize(DfmEntryHandle_t xEntryHandle,uint32_t * pulSize)876 DfmResult_t xDfmEntryGetDataSize(DfmEntryHandle_t xEntryHandle, uint32_t* pulSize)
877 {
878 	DfmEntryHeader_t* pxEntryHeader;
879 
880 	if (pxDfmEntryData == (void*)0)
881 	{
882 		return DFM_FAIL;
883 	}
884 
885 	if (pxDfmEntryData->ulInitialized == 0UL)
886 	{
887 		return DFM_FAIL;
888 	}
889 
890 	if (xEntryHandle == 0)
891 	{
892 		return DFM_FAIL;
893 	}
894 
895 	if (pulSize == (void*)0)
896 	{
897 		return DFM_FAIL;
898 	}
899 
900 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
901 
902 	*pulSize = pxEntryHeader->ulDataSize;
903 
904 	return DFM_SUCCESS;
905 }
906 
xDfmEntryGetAlertId(DfmEntryHandle_t xEntryHandle,uint32_t * pulAlertId)907 DfmResult_t xDfmEntryGetAlertId(DfmEntryHandle_t xEntryHandle, uint32_t* pulAlertId)
908 {
909 	DfmEntryHeader_t* pxEntryHeader;
910 
911 	if (pxDfmEntryData == (void*)0)
912 	{
913 		return DFM_FAIL;
914 	}
915 
916 	if (pxDfmEntryData->ulInitialized == 0UL)
917 	{
918 		return DFM_FAIL;
919 	}
920 
921 	if (xEntryHandle == 0)
922 	{
923 		return DFM_FAIL;
924 	}
925 
926 	if (pulAlertId == (void*)0)
927 	{
928 		return DFM_FAIL;
929 	}
930 
931 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
932 
933 	*pulAlertId = pxEntryHeader->ulAlertId;
934 
935 	return DFM_SUCCESS;
936 }
937 
xDfmEntryGetSessionId(DfmEntryHandle_t xEntryHandle,const char ** pszSessionId)938 DfmResult_t xDfmEntryGetSessionId(DfmEntryHandle_t xEntryHandle, const char** pszSessionId)
939 {
940 	DfmEntryHeader_t* pxEntryHeader;
941 
942 	if (pxDfmEntryData == (void*)0)
943 	{
944 		return DFM_FAIL;
945 	}
946 
947 	if (pxDfmEntryData->ulInitialized == 0UL)
948 	{
949 		return DFM_FAIL;
950 	}
951 
952 	if (xEntryHandle == 0)
953 	{
954 		return DFM_FAIL;
955 	}
956 
957 	if (pszSessionId == (void*)0)
958 	{
959 		return DFM_FAIL;
960 	}
961 
962 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
963 
964 	/* SessionId is located right after Header */
965 	*pszSessionId = (char*)((uintptr_t)pxEntryHeader + sizeof(DfmEntryHeader_t)); /*cstat !MISRAC2012-Rule-18.4 The Session Id is stored after the Entry Header*/
966 
967 	return DFM_SUCCESS;
968 }
969 
xDfmEntryGetDeviceName(DfmEntryHandle_t xEntryHandle,const char ** pszDeviceName)970 DfmResult_t xDfmEntryGetDeviceName(DfmEntryHandle_t xEntryHandle, const char** pszDeviceName)
971 {
972 	DfmEntryHeader_t* pxEntryHeader;
973 
974 	if (pxDfmEntryData == (void*)0)
975 	{
976 		return DFM_FAIL;
977 	}
978 
979 	if (pxDfmEntryData->ulInitialized == 0UL)
980 	{
981 		return DFM_FAIL;
982 	}
983 
984 	if (xEntryHandle == 0)
985 	{
986 		return DFM_FAIL;
987 	}
988 
989 	if (pszDeviceName == (void*)0)
990 	{
991 		return DFM_FAIL;
992 	}
993 
994 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
995 
996 	/* Description is located right after SessionId */
997 	*pszDeviceName = (char*)((uintptr_t)pxEntryHeader + sizeof(DfmEntryHeader_t) + (uint32_t)pxEntryHeader->usSessionIdSize); /*cstat !MISRAC2012-Rule-18.4 The Device Name is stored after the Entry Header and Session Id*/
998 
999 	return DFM_SUCCESS;
1000 }
1001 
xDfmEntryGetDescription(DfmEntryHandle_t xEntryHandle,const char ** pszDescription)1002 DfmResult_t xDfmEntryGetDescription(DfmEntryHandle_t xEntryHandle, const char** pszDescription)
1003 {
1004 	DfmEntryHeader_t* pxEntryHeader;
1005 
1006 	if (pxDfmEntryData == (void*)0)
1007 	{
1008 		return DFM_FAIL;
1009 	}
1010 
1011 	if (pxDfmEntryData->ulInitialized == 0UL)
1012 	{
1013 		return DFM_FAIL;
1014 	}
1015 
1016 	if (xEntryHandle == 0)
1017 	{
1018 		return DFM_FAIL;
1019 	}
1020 
1021 	if (pszDescription == (void*)0)
1022 	{
1023 		return DFM_FAIL;
1024 	}
1025 
1026 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
1027 
1028 	/* Description is located right after SessionId */
1029 	*pszDescription = (char*)((uintptr_t)pxEntryHeader + sizeof(DfmEntryHeader_t) + (uint32_t)pxEntryHeader->usSessionIdSize + (uint32_t)pxEntryHeader->usDeviceNameSize); /*cstat !MISRAC2012-Rule-18.4 The Description is stored after the Entry Header, Session Id and Device Name*/
1030 
1031 	return DFM_SUCCESS;
1032 }
1033 
xDfmEntryGetData(DfmEntryHandle_t xEntryHandle,void ** ppvData)1034 DfmResult_t xDfmEntryGetData(DfmEntryHandle_t xEntryHandle, void** ppvData)
1035 {
1036 	DfmEntryHeader_t* pxEntryHeader;
1037 
1038 	if (pxDfmEntryData == (void*)0)
1039 	{
1040 		return DFM_FAIL;
1041 	}
1042 
1043 	if (pxDfmEntryData->ulInitialized == 0UL)
1044 	{
1045 		return DFM_FAIL;
1046 	}
1047 
1048 	if (xEntryHandle == 0)
1049 	{
1050 		return DFM_FAIL;
1051 	}
1052 
1053 	if (ppvData == (void*)0)
1054 	{
1055 		return DFM_FAIL;
1056 	}
1057 
1058 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
1059 
1060 	/* Data is located right after Description */
1061 	*ppvData = (void*)((uintptr_t)pxEntryHeader + sizeof(DfmEntryHeader_t) + (uint32_t)pxEntryHeader->usSessionIdSize + (uint32_t)pxEntryHeader->usDeviceNameSize + (uint32_t)pxEntryHeader->usDescriptionSize); /*cstat !MISRAC2012-Rule-11.6 !MISRAC2012-Rule-18.4 The Entry Data is stored after the Entry Header, Session Id, Device Name and Description*/
1062 
1063 	return DFM_SUCCESS;
1064 }
1065 
xDfmEntryGetEndMarkers(DfmEntryHandle_t xEntryHandle,uint8_t ** pucMarkersBuffer)1066 DfmResult_t xDfmEntryGetEndMarkers(DfmEntryHandle_t xEntryHandle, uint8_t** pucMarkersBuffer)
1067 {
1068 	DfmEntryHeader_t* pxEntryHeader;
1069 
1070 	if (pxDfmEntryData == (void*)0)
1071 	{
1072 		return DFM_FAIL;
1073 	}
1074 
1075 	if (pxDfmEntryData->ulInitialized == 0UL)
1076 	{
1077 		return DFM_FAIL;
1078 	}
1079 
1080 	if (xEntryHandle == 0)
1081 	{
1082 		return DFM_FAIL;
1083 	}
1084 
1085 	if (pucMarkersBuffer == (void*)0)
1086 	{
1087 		return DFM_FAIL;
1088 	}
1089 
1090 	pxEntryHeader = (DfmEntryHeader_t*)xEntryHandle;
1091 
1092 	if ((pxEntryHeader->cStartMarkers[0] != (uint16_t)0xD1) ||
1093 		(pxEntryHeader->cStartMarkers[1] != (uint16_t)0xD2) ||
1094 		(pxEntryHeader->cStartMarkers[2] != (uint16_t)0xD3) ||
1095 		(pxEntryHeader->cStartMarkers[3] != (uint16_t)0xD4))
1096 	{
1097 		return DFM_FAIL;
1098 	}
1099 
1100 	/* Footer is located right after Data */
1101 	*pucMarkersBuffer = (uint8_t*)((uintptr_t)pxEntryHeader + sizeof(DfmEntryHeader_t) + (uint32_t)pxEntryHeader->usSessionIdSize + (uint32_t)pxEntryHeader->usDeviceNameSize + (uint32_t)pxEntryHeader->usDescriptionSize + pxEntryHeader->ulDataSize); /*cstat !MISRAC2012-Rule-18.4 The End Markers are stored after the Entry Header, Session Id, Device Name, Description and Entry Data*/
1102 
1103 	return DFM_SUCCESS;
1104 }
1105 
1106 #endif
1107