1 /*
2 * Trace Recorder for Tracealyzer v4.9.2
3 * Copyright 2023 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * The implementation of extensions.
9 */
10 #include <trcRecorder.h>
11 
12 #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
13 
14 #define TRC_EXTENSION_BASE_EVENT_ID (TRC_EVENT_LAST_ID + 1UL)
15 
16 #define TRC_EXTENSION_COMBINE_VERSION(_major, _minor, _patch) \
17 		( \
18 			((0x000000FFUL & (TraceUnsignedBaseType_t)(_major)) << 24) | \
19 			((0x000000FFUL & (TraceUnsignedBaseType_t)(_minor)) << 16) | \
20 			((0x0000FFFFUL & (TraceUnsignedBaseType_t)(_patch)) << 0) \
21 		)
22 
23 static TraceExtensionData_t *pxExtensionData TRC_CFG_RECORDER_DATA_ATTRIBUTE;
24 
xTraceExtensionInitialize(TraceExtensionData_t * const pxBuffer)25 traceResult xTraceExtensionInitialize(TraceExtensionData_t* const pxBuffer)
26 {
27 	/* This should never fail */
28 	TRC_ASSERT(pxBuffer != (void*)0);
29 
30 	pxExtensionData = pxBuffer;
31 
32 	pxExtensionData->uxNextFreeExtensionEventId = TRC_EXTENSION_BASE_EVENT_ID;
33 
34 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_EXTENSION);
35 
36 	return TRC_SUCCESS;
37 }
38 
39 /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
xTraceExtensionCreate(const char * szName,uint8_t uiMajor,uint8_t uiMinor,uint16_t uiPatch,uint32_t uiEventCount,TraceExtensionHandle_t * pxExtensionHandle)40 traceResult xTraceExtensionCreate(const char* szName, uint8_t uiMajor, uint8_t uiMinor, uint16_t uiPatch, uint32_t uiEventCount, TraceExtensionHandle_t* pxExtensionHandle)
41 {
42 	TraceObjectHandle_t xObjectHandle;
43 	TraceUnsignedBaseType_t uxStates[3];
44 
45 	/* This should never fail */
46 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_EXTENSION));
47 
48 	/* This should never fail */
49 	TRC_ASSERT(uiEventCount != 0u);
50 
51 	/* This should never fail */
52 	TRC_ASSERT(pxExtensionHandle != (void*)0);
53 
54 	/* This should never fail */
55 	TRC_ASSERT(pxExtensionHandle != (void*)0);
56 
57 	/* This should never fail */
58 	TRC_ASSERT(szName != (void*)0);
59 
60 	/* This should never fail */
61 	TRC_ASSERT(szName[0] != (char)0); /*cstat !MISRAC2004-17.4_b We need to check the first characted*/ /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
62 
63 	uxStates[TRC_EXTENSION_STATE_INDEX_VERSION] = TRC_EXTENSION_COMBINE_VERSION(uiMajor, uiMinor, uiPatch);
64 	uxStates[TRC_EXTENSION_STATE_INDEX_BASE_EVENT_ID] = pxExtensionData->uxNextFreeExtensionEventId;
65 	uxStates[TRC_EXTENSION_STATE_INDEX_EVENT_COUNT] = uiEventCount;
66 
67 	/* We need to check this */
68 	if (xTraceObjectRegisterInternal(PSF_EVENT_EXTENSION_CREATE, (void*)0, szName, 3u, uxStates, TRC_ENTRY_OPTION_EXTENSION, &xObjectHandle) == TRC_FAIL)
69 	{
70 		return TRC_FAIL;
71 	}
72 
73 	pxExtensionData->uxNextFreeExtensionEventId += uiEventCount;
74 
75 	*pxExtensionHandle = (TraceExtensionHandle_t)xObjectHandle;
76 
77 	return TRC_SUCCESS;
78 }
79 
xTraceExtensionGetBaseEventId(TraceExtensionHandle_t xExtensionHandle,uint32_t * puiBaseEventId)80 traceResult xTraceExtensionGetBaseEventId(TraceExtensionHandle_t xExtensionHandle, uint32_t *puiBaseEventId)
81 {
82 	TraceUnsignedBaseType_t uxBaseEventId;
83 
84 	/* This should never fail */
85 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_EXTENSION));
86 
87 	/* This should never fail */
88 	TRC_ASSERT(puiBaseEventId != (void*)0);
89 
90 	/* This should never fail */
91 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntryGetState((TraceEntryHandle_t)xExtensionHandle, TRC_EXTENSION_STATE_INDEX_BASE_EVENT_ID, &uxBaseEventId) == TRC_SUCCESS);
92 
93 	*puiBaseEventId = (uint32_t)uxBaseEventId;
94 
95 	return TRC_SUCCESS;
96 }
97 
98 /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
xTraceExtensionGetConfigName(TraceExtensionHandle_t xExtensionHandle,const char ** pszName)99 traceResult xTraceExtensionGetConfigName(TraceExtensionHandle_t xExtensionHandle, const char **pszName)
100 {
101 	/* This should never fail */
102 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_EXTENSION));
103 
104 	return xTraceEntryGetSymbol((TraceEntryHandle_t)xExtensionHandle, pszName);
105 }
106 
107 #endif
108