1 /*
2  * Percepio DFM v2.0.0
3  * Copyright 2023 Percepio AB
4  * www.percepio.com
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * DFM Alert
9  */
10 
11 #include <dfm.h>
12 
13 #if ((DFM_CFG_ENABLED) >= 1)
14 
15 static DfmResult_t prvDfmAlertInitialize(DfmAlertHandle_t xAlertHandle, uint8_t ucDfmVersion, uint32_t ulProduct, const char* szFirmwareVersion);
16 static uint32_t prvDfmAlertCalculateChecksum(uint8_t* pxData, uint32_t ulSize);
17 static void prvDfmAlertReset(DfmAlert_t* pxAlert);
18 static DfmResult_t prvDfmProcessAlert(DfmAlertEntryCallback_t xAlertCallback, DfmAlertEntryCallback_t xPayloadCallback);
19 static DfmResult_t prvDfmGetAll(DfmAlertEntryCallback_t xAlertCallback, DfmAlertEntryCallback_t xPayloadCallback);
20 
21 static DfmResult_t prvStoreAlert(DfmEntryHandle_t xEntryHandle);
22 static DfmResult_t prvStorePayloadChunk(DfmEntryHandle_t xEntryHandle);
23 static DfmResult_t prvSendAlert(DfmEntryHandle_t xEntryHandle);
24 static DfmResult_t prvSendPayloadChunk(DfmEntryHandle_t xEntryHandle);
25 
26 DfmAlertData_t* pxDfmAlertData = (void*)0;
27 
prvStoreAlert(DfmEntryHandle_t xEntryHandle)28 static DfmResult_t prvStoreAlert(DfmEntryHandle_t xEntryHandle)
29 {
30 	return xDfmStorageStoreAlert(xEntryHandle);
31 }
32 
prvStorePayloadChunk(DfmEntryHandle_t xEntryHandle)33 static DfmResult_t prvStorePayloadChunk(DfmEntryHandle_t xEntryHandle)
34 {
35 	/* We don't care if payload stuff fails */
36 	(void)xDfmStorageStorePayloadChunk(xEntryHandle);
37 
38 	return DFM_SUCCESS;
39 }
40 
prvSendAlert(DfmEntryHandle_t xEntryHandle)41 static DfmResult_t prvSendAlert(DfmEntryHandle_t xEntryHandle)
42 {
43 	return xDfmCloudSendAlert(xEntryHandle);
44 }
45 
prvSendPayloadChunk(DfmEntryHandle_t xEntryHandle)46 static DfmResult_t prvSendPayloadChunk(DfmEntryHandle_t xEntryHandle)
47 {
48 	/* We don't care if payload stuff fails */
49 	(void)xDfmCloudSendPayloadChunk(xEntryHandle);
50 
51 	return DFM_SUCCESS;
52 }
53 
xDfmAlertInitialize(DfmAlertData_t * pxBuffer)54 DfmResult_t xDfmAlertInitialize(DfmAlertData_t *pxBuffer)
55 {
56 	if (pxBuffer == (void*)0)
57 	{
58 		return DFM_FAIL;
59 	}
60 
61 	pxDfmAlertData = pxBuffer;
62 
63 	pxDfmAlertData->ulPayloadCount = 0;
64 	pxDfmAlertData->ulInitialized = 1;
65 
66 	return prvDfmAlertInitialize((DfmAlertHandle_t)&pxDfmAlertData->xAlert, DFM_VERSION, DFM_CFG_PRODUCTID, DFM_CFG_FIRMWARE_VERSION);
67 }
68 
xDfmAlertGetVersion(DfmAlertHandle_t xAlertHandle,uint8_t * pucVersion)69 DfmResult_t xDfmAlertGetVersion(DfmAlertHandle_t xAlertHandle, uint8_t* pucVersion)
70 {
71 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
72 
73 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
74 	{
75 		return DFM_FAIL;
76 	}
77 
78 	if (pxAlert == (void*)0)
79 	{
80 		return DFM_FAIL;
81 	}
82 
83 	if (pucVersion == (void*)0)
84 	{
85 		return DFM_FAIL;
86 	}
87 
88 	*pucVersion = pxAlert->ucVersion;
89 
90 	return DFM_SUCCESS;
91 }
92 
xDfmAlertGetProduct(DfmAlertHandle_t xAlertHandle,uint32_t * pulProduct)93 DfmResult_t xDfmAlertGetProduct(DfmAlertHandle_t xAlertHandle, uint32_t* pulProduct)
94 {
95 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
96 
97 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
98 	{
99 		return DFM_FAIL;
100 	}
101 
102 	if (pxAlert == (void*)0)
103 	{
104 		return DFM_FAIL;
105 	}
106 
107 	*pulProduct = pxAlert->ulProduct;
108 
109 	return DFM_SUCCESS;
110 }
111 
xDfmAlertGetFirmwareVersion(DfmAlertHandle_t xAlertHandle,const char ** pszFirmwareVersion)112 DfmResult_t xDfmAlertGetFirmwareVersion(DfmAlertHandle_t xAlertHandle, const char** pszFirmwareVersion)
113 {
114 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
115 
116 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
117 	{
118 		return DFM_FAIL;
119 	}
120 
121 	if (pxAlert == (void*)0)
122 	{
123 		return DFM_FAIL;
124 	}
125 
126 	*pszFirmwareVersion = pxAlert->cFirmwareVersionBuffer;
127 
128 	return DFM_SUCCESS;
129 }
130 
xDfmAlertReset(DfmAlertHandle_t xAlertHandle)131 DfmResult_t xDfmAlertReset(DfmAlertHandle_t xAlertHandle)
132 {
133 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
134 
135 	if (pxDfmAlertData == (void*)0)
136 	{
137 		return DFM_FAIL;
138 	}
139 
140 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
141 	{
142 		return DFM_FAIL;
143 	}
144 
145 	if (pxAlert == (void*)0)
146 	{
147 		return DFM_FAIL;
148 	}
149 
150 	prvDfmAlertReset(pxAlert);
151 
152 	return DFM_SUCCESS;
153 }
154 
xDfmAlertBegin(uint32_t ulAlertType,const char * szAlertDescription,DfmAlertHandle_t * pxAlertHandle)155 DfmResult_t xDfmAlertBegin(uint32_t ulAlertType, const char* szAlertDescription, DfmAlertHandle_t* pxAlertHandle)
156 {
157 	DfmAlert_t* pxAlert;
158 	uint32_t i;
159 
160 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
161 	{
162 		return DFM_FAIL;
163 	}
164 
165 	if (pxDfmAlertData == (void*)0)
166 	{
167 		return DFM_FAIL;
168 	}
169 
170 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
171 	{
172 		return DFM_FAIL;
173 	}
174 
175 	if (pxAlertHandle == (void*)0)
176 	{
177 		return DFM_FAIL;
178 	}
179 
180 	if (szAlertDescription == (void*)0)
181 	{
182 		return DFM_FAIL;
183 	}
184 
185 	if (szAlertDescription[0] == (char)0)
186 	{
187 		return DFM_FAIL;
188 	}
189 
190 	pxAlert = &pxDfmAlertData->xAlert;
191 
192 	prvDfmAlertReset(pxAlert);
193 
194 	pxAlert->ucVersion = DFM_VERSION;
195 
196 	pxAlert->ulAlertType = ulAlertType;
197 
198 	if (xDfmSessionGenerateNewAlertId() == DFM_FAIL)
199 	{
200 		return DFM_FAIL;
201 	}
202 
203 	for (i = (uint32_t)0; i < (uint32_t)(DFM_DESCRIPTION_MAX_LEN); i++)
204 	{
205 		pxAlert->cAlertDescription[i] = szAlertDescription[i];
206 
207 		if (szAlertDescription[i] == (char)0)
208 		{
209 			break;
210 		}
211 	}
212 
213 	*pxAlertHandle = (DfmAlertHandle_t)pxAlert;
214 
215 	return DFM_SUCCESS;
216 }
217 
xDfmAlertAddSymptom(DfmAlertHandle_t xAlertHandle,uint32_t ulSymptomId,uint32_t ulValue)218 DfmResult_t xDfmAlertAddSymptom(DfmAlertHandle_t xAlertHandle, uint32_t ulSymptomId, uint32_t ulValue)
219 {
220 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
221 
222 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
223 	{
224 		return DFM_FAIL;
225 	}
226 
227 	if (pxDfmAlertData == (void*)0)
228 	{
229 		return DFM_FAIL;
230 	}
231 
232 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
233 	{
234 		return DFM_FAIL;
235 	}
236 
237 	if (pxAlert == (void*)0)
238 	{
239 		return DFM_FAIL;
240 	}
241 
242 	if (pxDfmAlertData->xAlert.ucSymptomCount >= (uint8_t)(DFM_CFG_MAX_SYMPTOMS))
243 	{
244 		(void)xDfmSessionSetStatus(DFM_STATUS_CODE_MAX_SYMPTOMS_EXCEEDED);
245 		return DFM_FAIL;
246 	}
247 
248 	pxAlert->xSymptoms[pxDfmAlertData->xAlert.ucSymptomCount].ulId = ulSymptomId;
249 	pxAlert->xSymptoms[pxDfmAlertData->xAlert.ucSymptomCount].ulValue = ulValue;
250 
251 	pxDfmAlertData->xAlert.ucSymptomCount++;
252 
253 	return DFM_SUCCESS;
254 }
255 
xDfmAlertGetSymptom(DfmAlertHandle_t xAlertHandle,uint32_t ulIndex,uint32_t * pulSymptomId,uint32_t * pulValue)256 DfmResult_t xDfmAlertGetSymptom(DfmAlertHandle_t xAlertHandle, uint32_t ulIndex, uint32_t* pulSymptomId, uint32_t* pulValue)
257 {
258 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
259 
260 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
261 	{
262 		return DFM_FAIL;
263 	}
264 
265 	if (pxDfmAlertData == (void*)0)
266 	{
267 		return DFM_FAIL;
268 	}
269 
270 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
271 	{
272 		return DFM_FAIL;
273 	}
274 
275 	if (pxAlert == (void*)0)
276 	{
277 		return DFM_FAIL;
278 	}
279 
280 	if (ulIndex >= (uint32_t)(DFM_CFG_MAX_SYMPTOMS))
281 	{
282 		return DFM_FAIL;
283 	}
284 
285 	if (ulIndex >= (uint32_t)pxAlert->ucSymptomCount)
286 	{
287 		return DFM_FAIL;
288 	}
289 
290 	*pulSymptomId = pxAlert->xSymptoms[ulIndex].ulId;
291 	*pulValue = pxAlert->xSymptoms[ulIndex].ulValue;
292 
293 	return DFM_SUCCESS;
294 }
295 
xDfmAlertAddPayload(DfmAlertHandle_t xAlertHandle,void * pvData,uint32_t ulSize,const char * szDescription)296 DfmResult_t xDfmAlertAddPayload(DfmAlertHandle_t xAlertHandle, void* pvData, uint32_t ulSize, const char* szDescription)
297 {
298 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
299 	uint32_t i;
300 
301 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
302 	{
303 		return DFM_FAIL;
304 	}
305 
306 	if (pxDfmAlertData == (void*)0)
307 	{
308 		return DFM_FAIL;
309 	}
310 
311 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
312 	{
313 		return DFM_FAIL;
314 	}
315 
316 	if (pxAlert == (void*)0)
317 	{
318 		return DFM_FAIL;
319 	}
320 
321 	if (pvData == (void*)0)
322 	{
323 		return DFM_FAIL;
324 	}
325 
326 	if (ulSize == (uint32_t)0)
327 	{
328 		return DFM_FAIL;
329 	}
330 
331 	if (szDescription == (void*)0)
332 	{
333 		return DFM_FAIL;
334 	}
335 
336 	if (szDescription[0] == (char)0)
337 	{
338 		return DFM_FAIL;
339 	}
340 
341 	if (pxDfmAlertData->ulPayloadCount >= (uint32_t)(DFM_CFG_MAX_PAYLOADS))
342 	{
343 		return DFM_FAIL;
344 	}
345 
346 	pxDfmAlertData->xPayloads[pxDfmAlertData->ulPayloadCount].pvData = pvData;
347 
348 	pxDfmAlertData->xPayloads[pxDfmAlertData->ulPayloadCount].ulSize = ulSize;
349 
350 	for (i = (uint32_t)0; i < (uint32_t)(DFM_PAYLOAD_DESCRIPTION_MAX_LEN); i++)
351 	{
352 		pxDfmAlertData->xPayloads[pxDfmAlertData->ulPayloadCount].cDescriptionBuffer[i] = szDescription[i];
353 
354 		if (szDescription[i] == (char)0)
355 		{
356 			break;
357 		}
358 	}
359 
360 	pxDfmAlertData->ulPayloadCount++;
361 
362 	return DFM_SUCCESS;
363 }
364 
xDfmAlertGetPayload(DfmAlertHandle_t xAlertHandle,uint32_t ulIndex,void ** ppvData,uint32_t * pulSize,char ** pszDescription)365 DfmResult_t xDfmAlertGetPayload(DfmAlertHandle_t xAlertHandle, uint32_t ulIndex, void** ppvData, uint32_t* pulSize, char** pszDescription)
366 {
367 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
368 
369 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
370 	{
371 		return DFM_FAIL;
372 	}
373 
374 	if (pxDfmAlertData == (void*)0)
375 	{
376 		return DFM_FAIL;
377 	}
378 
379 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
380 	{
381 		return DFM_FAIL;
382 	}
383 
384 	if (pxAlert == (void*)0)
385 	{
386 		return DFM_FAIL;
387 	}
388 
389 	if (ppvData == (void*)0)
390 	{
391 		return DFM_FAIL;
392 	}
393 
394 	if (pulSize == (void*)0)
395 	{
396 		return DFM_FAIL;
397 	}
398 
399 	if (pszDescription == (void*)0)
400 	{
401 		return DFM_FAIL;
402 	}
403 
404 	if (ulIndex >= (uint32_t)(DFM_CFG_MAX_PAYLOADS))
405 	{
406 		return DFM_FAIL;
407 	}
408 
409 	if (ulIndex >= pxDfmAlertData->ulPayloadCount)
410 	{
411 		return DFM_FAIL;
412 	}
413 
414 	*ppvData = pxDfmAlertData->xPayloads[ulIndex].pvData;
415 
416 	*pulSize = pxDfmAlertData->xPayloads[ulIndex].ulSize;
417 
418 	*pszDescription = pxDfmAlertData->xPayloads[ulIndex].cDescriptionBuffer;
419 
420 	return DFM_SUCCESS;
421 }
422 
xDfmAlertGetType(DfmAlertHandle_t xAlertHandle,uint32_t * pulAlertType)423 DfmResult_t xDfmAlertGetType(DfmAlertHandle_t xAlertHandle, uint32_t* pulAlertType)
424 {
425 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
426 
427 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
428 	{
429 		return DFM_FAIL;
430 	}
431 
432 	if (pxDfmAlertData == (void*)0)
433 	{
434 		return DFM_FAIL;
435 	}
436 
437 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
438 	{
439 		return DFM_FAIL;
440 	}
441 
442 	if (pxAlert == (void*)0)
443 	{
444 		return DFM_FAIL;
445 	}
446 
447 	if (pulAlertType == (void*)0)
448 	{
449 		return DFM_FAIL;
450 	}
451 
452 	*pulAlertType = pxAlert->ulAlertType;
453 
454 	return DFM_SUCCESS;
455 }
456 
xDfmAlertGetDescription(DfmAlertHandle_t xAlertHandle,const char ** pszAlertDescription)457 DfmResult_t xDfmAlertGetDescription(DfmAlertHandle_t xAlertHandle, const char** pszAlertDescription)
458 {
459 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
460 
461 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
462 	{
463 		return DFM_FAIL;
464 	}
465 
466 	if (pxDfmAlertData == (void*)0)
467 	{
468 		return DFM_FAIL;
469 	}
470 
471 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
472 	{
473 		return DFM_FAIL;
474 	}
475 
476 	if (pxAlert == (void*)0)
477 	{
478 		return DFM_FAIL;
479 	}
480 
481 	if (pszAlertDescription == (void*)0)
482 	{
483 		return DFM_FAIL;
484 	}
485 
486 	*pszAlertDescription = pxAlert->cAlertDescription;
487 
488 	return DFM_SUCCESS;
489 }
490 
xDfmAlertEnd(DfmAlertHandle_t xAlertHandle)491 DfmResult_t xDfmAlertEnd(DfmAlertHandle_t xAlertHandle)
492 {
493 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
494 
495 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
496 	{
497 		return DFM_FAIL;
498 	}
499 
500 	if (pxDfmAlertData == (void*)0)
501 	{
502 		return DFM_FAIL;
503 	}
504 
505 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
506 	{
507 		return DFM_FAIL;
508 	}
509 
510 	if (pxAlert == (void*)0)
511 	{
512 		return DFM_FAIL;
513 	}
514 
515 	pxAlert->ulChecksum = prvDfmAlertCalculateChecksum((uint8_t*)pxAlert, sizeof(DfmAlert_t) - sizeof(uint32_t));
516 
517 	/* Try to send */
518 	if (prvDfmProcessAlert(prvSendAlert, prvSendPayloadChunk) == DFM_SUCCESS)
519 	{
520 		prvDfmAlertReset(pxAlert);
521 
522 		return DFM_SUCCESS;
523 	}
524 
525 	/* Try to store */
526 	if (prvDfmProcessAlert(prvStoreAlert, prvStorePayloadChunk) == DFM_SUCCESS)
527 	{
528 		prvDfmAlertReset(pxAlert);
529 
530 		return DFM_SUCCESS;
531 	}
532 
533 	/* Could not send or store */
534 	prvDfmAlertReset(pxAlert);
535 
536 	return DFM_FAIL;
537 }
538 
xDfmAlertEndOffline(DfmAlertHandle_t xAlertHandle)539 DfmResult_t xDfmAlertEndOffline(DfmAlertHandle_t xAlertHandle)
540 {
541 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
542 
543 	if (ulDfmSessionIsEnabled() == (uint32_t)0)
544 	{
545 		return DFM_FAIL;
546 	}
547 
548 	if (pxDfmAlertData == (void*)0)
549 	{
550 		return DFM_FAIL;
551 	}
552 
553 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
554 	{
555 		return DFM_FAIL;
556 	}
557 
558 	if (pxAlert == (void*)0)
559 	{
560 		return DFM_FAIL;
561 	}
562 
563 	pxAlert->ulChecksum = prvDfmAlertCalculateChecksum((uint8_t*)pxAlert, sizeof(DfmAlert_t) - sizeof(uint32_t));
564 
565 	/* Try to store */
566 	if (prvDfmProcessAlert(prvStoreAlert, prvStorePayloadChunk) == DFM_SUCCESS)
567 	{
568 		prvDfmAlertReset(pxAlert);
569 
570 		return DFM_SUCCESS;
571 	}
572 
573 	/* Could not store */
574 	prvDfmAlertReset(pxAlert);
575 
576 	return DFM_FAIL;
577 }
578 
prvDfmAlertInitialize(DfmAlertHandle_t xAlertHandle,uint8_t ucDfmVersion,uint32_t ulProduct,const char * szFirmwareVersion)579 static DfmResult_t prvDfmAlertInitialize(DfmAlertHandle_t xAlertHandle, uint8_t ucDfmVersion, uint32_t ulProduct, const char* szFirmwareVersion)
580 {
581 	uint32_t i;
582 	DfmAlert_t* pxAlert = (DfmAlert_t*)xAlertHandle;
583 
584 	if (pxDfmAlertData == (void*)0)
585 	{
586 		return DFM_FAIL;
587 	}
588 
589 	if (pxAlert == (void*)0)
590 	{
591 		return DFM_FAIL;
592 	}
593 
594 	if (szFirmwareVersion == (void*)0) /*cstat !MISRAC2012-Rule-14.3_b C-STAT complains because DFM_CFG_FIRMWARE_VERSION refers to a static string that is never null in this project. In user projects, this is not guaranteed and must therefor be checked.*/
595 	{
596 		return DFM_FAIL;
597 	}
598 
599 	pxDfmAlertData->xAlert.ucStartMarkers[0] = 0x50; /* 'P' */
600 	pxDfmAlertData->xAlert.ucStartMarkers[1] = 0x44; /* 'D' */
601 	pxDfmAlertData->xAlert.ucStartMarkers[2] = 0x66; /* 'f' */
602 	pxDfmAlertData->xAlert.ucStartMarkers[3] = 0x6D; /* 'm' */
603 
604 	pxDfmAlertData->xAlert.usEndianness = 0x0FF0;
605 	pxDfmAlertData->xAlert.ucVersion = ucDfmVersion;
606 	pxDfmAlertData->xAlert.ucMaxSymptoms = (DFM_CFG_MAX_SYMPTOMS);
607 	pxDfmAlertData->xAlert.ucFirmwareVersionSize = (DFM_FIRMWARE_VERSION_MAX_LEN);
608 	pxDfmAlertData->xAlert.ucDescriptionSize = (DFM_DESCRIPTION_MAX_LEN);
609 	pxDfmAlertData->xAlert.ulProduct = ulProduct;
610 
611 	for (i = (uint32_t)0; i < (uint32_t)(DFM_FIRMWARE_VERSION_MAX_LEN); i++)
612 	{
613 		pxAlert->cFirmwareVersionBuffer[i] = szFirmwareVersion[i];
614 		if (szFirmwareVersion[i] == (char)0)
615 		{
616 			break;
617 		}
618 	}
619 
620 	prvDfmAlertReset(pxAlert);
621 
622 	pxAlert->ucEndMarkers[0] = 0x6D; /* 'm' */
623 	pxAlert->ucEndMarkers[1] = 0x66; /* 'f' */
624 	pxAlert->ucEndMarkers[2] = 0x44; /* 'D' */
625 	pxAlert->ucEndMarkers[3] = 0x50; /* 'P' */
626 
627 	return DFM_SUCCESS;
628 }
629 
xDfmAlertSendAll(void)630 DfmResult_t xDfmAlertSendAll(void)
631 {
632 	return prvDfmGetAll(xDfmCloudSendAlert, xDfmCloudSendPayloadChunk);
633 }
634 
xDfmAlertGetAll(DfmAlertEntryCallback_t xCallback)635 DfmResult_t xDfmAlertGetAll(DfmAlertEntryCallback_t xCallback)
636 {
637 	return prvDfmGetAll(xCallback, xCallback);
638 }
639 
prvDfmGetAll(DfmAlertEntryCallback_t xAlertCallback,DfmAlertEntryCallback_t xPayloadCallback)640 static DfmResult_t prvDfmGetAll(DfmAlertEntryCallback_t xAlertCallback, DfmAlertEntryCallback_t xPayloadCallback)
641 {
642 	DfmEntryHandle_t xEntryHandle = 0;
643 	uint32_t i;
644 	const char* szSessionId = (void*)0;
645 	char cSessionIdBuffer[DFM_SESSION_ID_MAX_LEN] = { 0 };
646 	uint32_t ulAlertId = 0;
647 	void* pvBuffer = (void*)0;
648 	uint32_t ulBufferSize = 0;
649 
650 	if (pxDfmAlertData == (void*)0)
651 	{
652 		return DFM_FAIL;
653 	}
654 
655 	if (pxDfmAlertData->ulInitialized == (uint32_t)0)
656 	{
657 		return DFM_FAIL;
658 	}
659 
660 	if (xAlertCallback == 0)
661 	{
662 		return DFM_FAIL;
663 	}
664 
665 	if (xPayloadCallback == 0)
666 	{
667 		return DFM_FAIL;
668 	}
669 
670 	if (xDfmEntryGetBuffer(&pvBuffer, &ulBufferSize) == DFM_FAIL)
671 	{
672 		return DFM_FAIL;
673 	}
674 
675 	memset(pvBuffer, 0, ulBufferSize);
676 
677 	while (xDfmStorageGetAlert(pvBuffer, ulBufferSize) == DFM_SUCCESS)
678 	{
679 		if (xDfmEntryCreateAlertFromBuffer(&xEntryHandle) == DFM_FAIL)
680 		{
681 			return DFM_FAIL;
682 		}
683 
684 		if (xAlertCallback(xEntryHandle) == DFM_FAIL)
685 		{
686 			return DFM_FAIL;
687 		}
688 
689 		if (xDfmEntryGetSessionId(xEntryHandle, &szSessionId) == DFM_FAIL)
690 		{
691 			return DFM_FAIL;
692 		}
693 
694 		if (xDfmEntryGetAlertId(xEntryHandle, &ulAlertId) == DFM_FAIL)
695 		{
696 			return DFM_FAIL;
697 		}
698 
699 		/* Create local copy of sessionId since the buffer containing it WILL be overwritten! */
700 		for (i = 0; i < sizeof(cSessionIdBuffer); i++)
701 		{
702 			cSessionIdBuffer[i] = szSessionId[i];
703 
704 			if (cSessionIdBuffer[i] == (char)0)
705 			{
706 				break;
707 			}
708 		}
709 
710 		memset(pvBuffer, 0, ulBufferSize);
711 
712 		while (xDfmStorageGetPayloadChunk(cSessionIdBuffer, ulAlertId, pvBuffer, ulBufferSize) == DFM_SUCCESS)
713 		{
714 			if (xDfmEntryCreatePayloadChunkFromBuffer(cSessionIdBuffer , ulAlertId, &xEntryHandle) == DFM_FAIL)
715 			{
716 				return DFM_FAIL;
717 			}
718 
719 			if (xPayloadCallback(xEntryHandle) == DFM_FAIL)
720 			{
721 				return DFM_FAIL;
722 			}
723 			memset(pvBuffer, 0, ulBufferSize);
724 		}
725 	}
726 
727 	return DFM_SUCCESS;
728 }
729 
prvDfmAlertReset(DfmAlert_t * pxAlert)730 static void prvDfmAlertReset(DfmAlert_t* pxAlert)
731 {
732 	uint32_t i;
733 
734 	pxAlert->ucVersion = 0;
735 
736 	pxAlert->ulAlertType = 0;
737 
738 	for (i = (uint32_t)0; i < (uint32_t)(DFM_CFG_MAX_SYMPTOMS); i++)
739 	{
740 		pxAlert->xSymptoms[i].ulId = 0;
741 		pxAlert->xSymptoms[i].ulValue = 0;
742 	}
743 	pxAlert->ucSymptomCount = 0;
744 
745 	pxAlert->cAlertDescription[0] = (char)0;
746 
747 	for (i = (uint32_t)0; i < (uint32_t)(DFM_CFG_MAX_PAYLOADS); i++)
748 	{
749 		pxDfmAlertData->xPayloads[i].pvData = (void*)0;
750 		pxDfmAlertData->xPayloads[i].ulSize = 0;
751 		pxDfmAlertData->xPayloads[i].cDescriptionBuffer[0] = (char)0;
752 	}
753 	pxDfmAlertData->ulPayloadCount = 0;
754 
755 	pxAlert->ulChecksum = 0;
756 }
757 
prvDfmProcessAlert(DfmAlertEntryCallback_t xAlertCallback,DfmAlertEntryCallback_t xPayloadCallback)758 static DfmResult_t prvDfmProcessAlert(DfmAlertEntryCallback_t xAlertCallback, DfmAlertEntryCallback_t xPayloadCallback)
759 {
760 	uint16_t j, usChunkCount;
761 	uint32_t i, ulOffset, ulChunkSize;
762 	DfmEntryHandle_t xEntryHandle = 0;
763 	DfmAlert_t* pxAlert = &pxDfmAlertData->xAlert;
764 
765 	if (xAlertCallback == 0) /*cstat !MISRAC2012-Rule-14.3_b This is a sanity check. It should never fail, but it will crash hard if someoone has made a mistake and we remove this check.*/
766 	{
767 		return DFM_FAIL;
768 	}
769 
770 	if (xPayloadCallback == 0) /*cstat !MISRAC2012-Rule-14.3_b This is a sanity check. It should never fail, but it will crash hard if someoone has made a mistake and we remove this check.*/
771 	{
772 		return DFM_FAIL;
773 	}
774 
775 	if (xDfmEntryCreateAlert((DfmAlertHandle_t)pxAlert, &xEntryHandle) == DFM_FAIL)
776 	{
777 		return DFM_FAIL;
778 	}
779 
780 	if (xAlertCallback(xEntryHandle) == DFM_FAIL)
781 	{
782 		return DFM_FAIL;
783 	}
784 
785 	for (i = 0; i < pxDfmAlertData->ulPayloadCount; i++)
786 	{
787 		/* We attempt to store the payloads, but if they fail there's not much we can do about it */
788 		usChunkCount = (uint16_t)(((pxDfmAlertData->xPayloads[i].ulSize - 1UL) / (uint32_t)(DFM_CFG_MAX_PAYLOAD_CHUNK_SIZE)) + 1UL);
789 		ulOffset = 0;
790 
791 		/* First we create the payload header */
792 		if (xDfmEntryCreatePayloadHeader((DfmAlertHandle_t)pxAlert, (uint16_t)(i + 1UL), pxDfmAlertData->xPayloads[i].ulSize, pxDfmAlertData->xPayloads[i].cDescriptionBuffer, &xEntryHandle) == DFM_FAIL)
793 		{
794 			/* Couldn't create header for this payload, continue to next */
795 			continue;
796 		}
797 
798 		/* Send payload header */
799 		if (xPayloadCallback(xEntryHandle) == DFM_FAIL)
800 		{
801 			/* Payload header wasn't handled, skip the rest */
802 			return DFM_FAIL;
803 		}
804 
805 		for (j = 0; j < usChunkCount; j++)
806 		{
807 			ulChunkSize = DFM_CFG_MAX_PAYLOAD_CHUNK_SIZE;
808 			if (ulChunkSize > pxDfmAlertData->xPayloads[i].ulSize - ulOffset)
809 			{
810 				ulChunkSize = pxDfmAlertData->xPayloads[i].ulSize - ulOffset;
811 			}
812 
813 			/* TODO: 64-bit compatible */
814 			if (xDfmEntryCreatePayloadChunk((DfmAlertHandle_t)pxAlert, (uint16_t)(i + 1UL), j + (uint16_t)1, usChunkCount, (void*)((uint32_t)pxDfmAlertData->xPayloads[i].pvData + ulOffset), ulChunkSize, pxDfmAlertData->xPayloads[i].cDescriptionBuffer, &xEntryHandle) == DFM_FAIL) /*cstat !MISRAC2012-Rule-11.6 We need to modify the address by an offset in order to get next payload chunk*/
815 			{
816 				/* Couldn't create entry for this payload chunk, continue to next */
817 				continue;
818 			}
819 
820 			if (xPayloadCallback(xEntryHandle) == DFM_FAIL)
821 			{
822 				/* Payload chunk wasn't handled, skip the rest */
823 				return DFM_FAIL;
824 			}
825 
826 			ulOffset += ulChunkSize;
827 		}
828 	}
829 
830 	return DFM_SUCCESS;
831 }
832 
prvDfmAlertCalculateChecksum(uint8_t * pxData,uint32_t ulSize)833 static uint32_t prvDfmAlertCalculateChecksum(uint8_t* pxData, uint32_t ulSize)
834 {
835 	(void)pxData;
836 	(void)ulSize;
837 
838 	return 0;
839 }
840 
841 #endif
842