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 interface definitions for trace streaming ("stream ports").
9  * This "stream port" sets up the recorder to stream the trace to file.
10  */
11 
12 #ifndef TRC_STREAM_PORT_H
13 #define TRC_STREAM_PORT_H
14 
15 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
16 
17 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
18 
19 #include <stdint.h>
20 #include <trcTypes.h>
21 #include <trcStreamPortConfig.h>
22 #include <stdio.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define TRC_ALIGNED_STREAM_PORT_BUFFER_SIZE ((((TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_SIZE) + sizeof(TraceUnsignedBaseType_t) - 1) / sizeof(TraceUnsignedBaseType_t)) * sizeof(TraceUnsignedBaseType_t))
29 
30 #define TRC_USE_INTERNAL_BUFFER (TRC_CFG_STREAM_PORT_USE_INTERNAL_BUFFER)
31 
32 #define TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_WRITE_MODE)
33 
34 #define TRC_INTERNAL_EVENT_BUFFER_TRANSFER_MODE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_TRANSFER_MODE)
35 
36 #define TRC_INTERNAL_BUFFER_CHUNK_SIZE (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_SIZE)
37 
38 #define TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT)
39 
40 #define TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT (TRC_CFG_STREAM_PORT_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT)
41 
42 /* Default file name */
43 #ifndef TRC_CFG_STREAM_PORT_TRACE_FILE
44 #define TRC_CFG_STREAM_PORT_TRACE_FILE "trace.psf"
45 #endif
46 
47 typedef struct TraceStreamPortFile	/* Aligned */
48 {
49 	FILE* pxFile;
50 #if (TRC_USE_INTERNAL_BUFFER)
51 	uint8_t buffer[TRC_ALIGNED_STREAM_PORT_BUFFER_SIZE];
52 #endif
53 } TraceStreamPortFile_t;
54 
55 extern TraceStreamPortFile_t* pxStreamPortFile;
56 
57 #define TRC_STREAM_PORT_BUFFER_SIZE (sizeof(TraceStreamPortFile_t))
58 
59 typedef struct TraceStreamPortBuffer
60 {
61 	uint8_t buffer[TRC_STREAM_PORT_BUFFER_SIZE];
62 } TraceStreamPortBuffer_t;
63 
64 /**
65  * @internal Stream port initialize callback.
66  *
67  * This function is called by the recorder as part of its initialization phase.
68  *
69  * @param[in] pxBuffer Buffer
70  *
71  * @retval TRC_FAIL Initialization failed
72  * @retval TRC_SUCCESS Success
73  */
74 traceResult xTraceStreamPortInitialize(TraceStreamPortBuffer_t* pxBuffer);
75 
76 /**
77  * @brief Allocates data from the stream port.
78  *
79  * @param[in] uiSize Allocation size
80  * @param[out] ppvData Allocation data pointer
81  *
82  * @retval TRC_FAIL Allocate failed
83  * @retval TRC_SUCCESS Success
84  */
85 #if (TRC_USE_INTERNAL_BUFFER == 1)
86 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
87 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
88 	#else
89 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceInternalEventBufferAlloc(uiSize, ppvData))
90 	#endif
91 #else
92 	#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
93 #endif
94 
95 /**
96  * @brief Commits data to the stream port, depending on the implementation/configuration of the
97  * stream port this data might be directly written to the stream port interface, buffered, or
98  * something else.
99  *
100  * @param[in] pvData Data to commit
101  * @param[in] uiSize Data to commit size
102  * @param[out] piBytesCommitted Bytes committed
103  *
104  * @retval TRC_FAIL Commit failed
105  * @retval TRC_SUCCESS Success
106  */
107 #if (TRC_USE_INTERNAL_BUFFER == 1)
108 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
109 		#define xTraceStreamPortCommit xTraceInternalEventBufferPush
110 	#else
111 		#define xTraceStreamPortCommit xTraceInternalEventBufferAllocCommit
112 	#endif
113 #else
114 	#define xTraceStreamPortCommit xTraceStreamPortWriteData
115 #endif
116 
117 /**
118  * @brief Writes data through the stream port interface.
119  *
120  * @param[in] pvData Data to write
121  * @param[in] uiSize Data to write size
122  * @param[out] piBytesWritten Bytes written
123  *
124  * @retval TRC_FAIL Write failed
125  * @retval TRC_SUCCESS Success
126  */
127 #define xTraceStreamPortWriteData(pvData, uiSize, piBytesWritten) (*(piBytesWritten) = (int32_t)fwrite(pvData, 1, uiSize, pxStreamPortFile->pxFile), TRC_SUCCESS)
128 
129 /**
130  * @brief Reads data through the stream port interface.
131  *
132  * @param[in] pvData Destination data buffer
133  * @param[in] uiSize Destination data buffer size
134  * @param[out] piBytesRead Bytes read
135  *
136  * @retval TRC_FAIL Read failed
137  * @retval TRC_SUCCESS Success
138  */
139 #define xTraceStreamPortReadData(pvData, uiSize, piBytesRead) ((void)(pvData), (void)(uiSize), (void)(piBytesRead), TRC_SUCCESS)
140 
141 #define xTraceStreamPortOnEnable(uiStartOption) ((void)(uiStartOption), TRC_SUCCESS)
142 
143 #define xTraceStreamPortOnDisable() (TRC_SUCCESS)
144 
145 traceResult xTraceStreamPortOnTraceBegin(void);
146 
147 traceResult xTraceStreamPortOnTraceEnd(void);
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
154 
155 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
156 
157 #endif /* TRC_STREAM_PORT_H */
158