1 /*
2  * Trace Recorder for Tracealyzer v4.10.3
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 use TCP/IP as streaming channel.
10  * The example is for Windows sockets (Winsock), for use with Windows ports.
11  */
12 
13 #ifndef TRC_STREAM_PORT_H
14 #define TRC_STREAM_PORT_H
15 
16 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
17 
18 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
19 
20 #include <stdint.h>
21 #include <trcTypes.h>
22 #include <trcStreamPortConfig.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 typedef struct TraceStreamPortBuffer	/* Aligned */
43 {
44 #if (TRC_USE_INTERNAL_BUFFER)
45 	uint8_t buffer[(TRC_ALIGNED_STREAM_PORT_BUFFER_SIZE)];
46 #else
47 	TraceUnsignedBaseType_t buffer[1];
48 #endif
49 } TraceStreamPortBuffer_t;
50 
51 int32_t prvTraceWriteToSocket(void* data, uint32_t size, int32_t* ptrBytesWritten);
52 int32_t prvTraceReadFromSocket(void* data, uint32_t bufsize, int32_t* ptrBytesRead);
53 
54 traceResult xTraceStreamPortInitialize(TraceStreamPortBuffer_t* pxBuffer);
55 
56 /**
57  * @brief Allocates data from the stream port.
58  *
59  * @param[in] uiSize Allocation size
60  * @param[out] ppvData Allocation data pointer
61  *
62  * @retval TRC_FAIL Allocate failed
63  * @retval TRC_SUCCESS Success
64  */
65 #if (TRC_USE_INTERNAL_BUFFER == 1)
66 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
67 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
68 	#else
69 		#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceInternalEventBufferAlloc(uiSize, ppvData))
70 	#endif
71 #else
72 	#define xTraceStreamPortAllocate(uiSize, ppvData) ((void)(uiSize), xTraceStaticBufferGet(ppvData))
73 #endif
74 
75 /**
76  * @brief Commits data to the stream port, depending on the implementation/configuration of the
77  * stream port this data might be directly written to the stream port interface, buffered, or
78  * something else.
79  *
80  * @param[in] pvData Data to commit
81  * @param[in] uiSize Data to commit size
82  * @param[out] piBytesCommitted Bytes committed
83  *
84  * @retval TRC_FAIL Commit failed
85  * @retval TRC_SUCCESS Success
86  */
87 #if (TRC_USE_INTERNAL_BUFFER == 1)
88 	#if (TRC_INTERNAL_EVENT_BUFFER_WRITE_MODE == TRC_INTERNAL_EVENT_BUFFER_OPTION_WRITE_MODE_COPY)
89 		#define xTraceStreamPortCommit xTraceInternalEventBufferPush
90 	#else
91 		#define xTraceStreamPortCommit xTraceInternalEventBufferAllocCommit
92 	#endif
93 #else
94 	#define xTraceStreamPortCommit xTraceStreamPortWriteData
95 #endif
96 
97 #define xTraceStreamPortWriteData(pvData, uiSize, piBytesWritten) (prvTraceWriteToSocket(pvData, uiSize, piBytesWritten) == 0 ? TRC_SUCCESS : TRC_FAIL)
98 
99 #define xTraceStreamPortReadData(pvData, uiSize, piBytesRead) (prvTraceReadFromSocket(pvData, uiSize, piBytesRead) == 0 ? TRC_SUCCESS : TRC_FAIL)
100 
101 #define xTraceStreamPortOnEnable(uiStartOption) ((void)(uiStartOption), TRC_SUCCESS)
102 
103 #define xTraceStreamPortOnDisable() (TRC_SUCCESS)
104 
105 #define xTraceStreamPortOnTraceBegin() (TRC_SUCCESS)
106 
107 traceResult xTraceStreamPortOnTraceEnd(void);
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
114 
115 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
116 
117 #endif /* TRC_STREAM_PORT_H */
118