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  * Supporting functions for trace streaming, used by the "stream ports"
9  * for reading and writing data to the interface.
10  * Existing ports can easily be modified to fit another setup, e.g., a
11  * different TCP/IP stack, or to define your own stream port.
12  */
13 
14 #include <trcRecorder.h>
15 #include <stdio.h>
16 
17 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
18 
19 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
20 
21 TraceStreamPortFile_t* pxStreamPortFile TRC_CFG_RECORDER_DATA_ATTRIBUTE;
22 
xTraceStreamPortInitialize(TraceStreamPortBuffer_t * pxBuffer)23 traceResult xTraceStreamPortInitialize(TraceStreamPortBuffer_t* pxBuffer)
24 {
25 	TRC_ASSERT_EQUAL_SIZE(TraceStreamPortBuffer_t, TraceStreamPortFile_t);
26 
27 	TRC_ASSERT(pxBuffer != 0);
28 
29 	pxStreamPortFile = (TraceStreamPortFile_t*)pxBuffer;
30 	pxStreamPortFile->pxFile = 0;
31 
32 #if (TRC_USE_INTERNAL_BUFFER == 1)
33 	return xTraceInternalEventBufferInitialize(pxStreamPortFile->buffer, sizeof(pxStreamPortFile->buffer));
34 #else
35 	return TRC_SUCCESS;
36 #endif
37 }
38 
xTraceStreamPortOnTraceBegin(void)39 traceResult xTraceStreamPortOnTraceBegin(void)
40 {
41 	if (pxStreamPortFile == 0)
42 	{
43 		return TRC_FAIL;
44 	}
45 
46 	if (pxStreamPortFile->pxFile == 0)
47 	{
48 #if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ == 1
49 		errno_t err = fopen_s(&pxStreamPortFile->pxFile, TRC_CFG_STREAM_PORT_TRACE_FILE, "wb");
50 		if (err != 0)
51 		{
52 			printf("Could not open trace file, error code %d.\n", err);
53 
54 			return TRC_FAIL;
55 		}
56 		else
57 		{
58 			printf("Trace file created.\n");
59 		}
60 #else
61 		FILE * file = fopen(TRC_CFG_STREAM_PORT_TRACE_FILE, "wb");
62 		if (file == NULL)
63 		{
64 			printf("Could not open trace file, error code %d.\n", errno);
65 
66 			return TRC_FAIL;
67 		}
68 		else
69 		{
70 			pxStreamPortFile->pxFile = file;
71 			printf("Trace file created.\n");
72 		}
73 #endif
74 	}
75 
76 	return TRC_SUCCESS;
77 }
78 
xTraceStreamPortOnTraceEnd(void)79 traceResult xTraceStreamPortOnTraceEnd(void)
80 {
81 	if (pxStreamPortFile == 0)
82 	{
83 		return TRC_FAIL;
84 	}
85 
86 	if (pxStreamPortFile->pxFile != 0)
87 	{
88 		fclose(pxStreamPortFile->pxFile);
89 		pxStreamPortFile->pxFile = 0;
90 		printf("Trace file closed.\n");
91 	}
92 
93 	return TRC_SUCCESS;
94 }
95 
96 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
97 
98 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
99