1 /*
2 * Trace Recorder for Tracealyzer v4.6.6
3 * Copyright 2021 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * The implementation of state machines.
9 */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
14 
15 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
16 
17 #define TRC_STATE_MACHINE_STATE_INDEX 0
18 #define TRC_STATE_MACHINE_INDEX 0
19 
xTraceStateMachineCreate(const char * szName,TraceStateMachineHandle_t * pxStateMachineHandle)20 traceResult xTraceStateMachineCreate(const char *szName, TraceStateMachineHandle_t *pxStateMachineHandle)
21 {
22 	TraceObjectHandle_t xObjectHandle;
23 
24 	/* This should never fail */
25 	TRC_ASSERT(pxStateMachineHandle != 0);
26 
27 	/* We need to check this */
28 	if (xTraceObjectRegister(PSF_EVENT_STATEMACHINE_CREATE , 0, szName, 0, &xObjectHandle) == TRC_FAIL)
29 	{
30 		return TRC_FAIL;
31 	}
32 
33 	/* This should never fail */
34 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetOptions((TraceEntryHandle_t)xObjectHandle, (uint32_t)TRC_ENTRY_OPTION_STATE_MACHINE) == TRC_SUCCESS);
35 
36 	*pxStateMachineHandle = (TraceStateMachineHandle_t)xObjectHandle;
37 
38 	return TRC_SUCCESS;
39 }
40 
xTraceStateMachineStateCreate(TraceStateMachineHandle_t xStateMachineHandle,const char * szName,TraceStateMachineStateHandle_t * pxStateHandle)41 traceResult xTraceStateMachineStateCreate(TraceStateMachineHandle_t xStateMachineHandle, const char* szName, TraceStateMachineStateHandle_t* pxStateHandle)
42 {
43 	TraceObjectHandle_t xObjectHandle;
44 
45 	/* This should never fail */
46 	TRC_ASSERT(xStateMachineHandle != 0);
47 
48 	/* This should never fail */
49 	TRC_ASSERT(pxStateHandle != 0);
50 
51 	/* We need to check this */
52 	if (xTraceObjectRegister(PSF_EVENT_STATEMACHINE_STATE_CREATE, 0, szName, (TraceUnsignedBaseType_t)xStateMachineHandle, &xObjectHandle) == TRC_FAIL)
53 	{
54 		return TRC_FAIL;
55 	}
56 
57 	/* This should never fail */
58 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetOptions((TraceEntryHandle_t)xObjectHandle, (uint32_t)TRC_ENTRY_OPTION_STATE_MACHINE_STATE) == TRC_SUCCESS);
59 
60 	*pxStateHandle = (TraceStateMachineHandle_t)xObjectHandle;
61 
62 	return TRC_SUCCESS;
63 }
64 
xTraceStateMachineSetState(TraceStateMachineHandle_t xStateMachineHandle,TraceStateMachineStateHandle_t xStateHandle)65 traceResult xTraceStateMachineSetState(TraceStateMachineHandle_t xStateMachineHandle, TraceStateMachineStateHandle_t xStateHandle)
66 {
67 	TraceEventHandle_t xEventHandle = 0;
68 
69 	/* This should never fail */
70 	TRC_ASSERT(xStateMachineHandle != 0);
71 
72 	/* This should never fail */
73 	TRC_ASSERT(xStateHandle != 0);
74 
75 	/* Verify that this state machine state was meant to be used with this state machine */
76 	/* This should never fail */
77 	TRC_ASSERT(xStateMachineHandle == (TraceStateMachineHandle_t)xTraceEntryGetStateReturn((TraceEntryHandle_t)xStateHandle, TRC_STATE_MACHINE_INDEX));
78 
79 	/* This should never fail */
80 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState((TraceEntryHandle_t)xStateMachineHandle, TRC_STATE_MACHINE_STATE_INDEX, (TraceUnsignedBaseType_t)xStateHandle) == TRC_SUCCESS);
81 
82 	/* We need to check this */
83 	if (xTraceEventBegin(PSF_EVENT_STATEMACHINE_STATECHANGE, sizeof(void*) + sizeof(void*), &xEventHandle) == TRC_SUCCESS)
84 	{
85 		xTraceEventAddPointer(xEventHandle, (void*)xStateMachineHandle);
86 		xTraceEventAddPointer(xEventHandle, (void*)xStateHandle);
87 		xTraceEventEnd(xEventHandle);
88 	}
89 
90 	return TRC_SUCCESS;
91 }
92 
93 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
94 
95 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
96