]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-Plus-Trace/streamports/TCPIP/trcStreamingPort.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-Plus-Trace / streamports / TCPIP / trcStreamingPort.c
1 /*******************************************************************************\r
2  * Trace Recorder Library for Tracealyzer v4.1.5\r
3  * Percepio AB, www.percepio.com\r
4  *\r
5  * trcStreamingPort.c\r
6  *\r
7  * Supporting functions for trace streaming, used by the "stream ports" \r
8  * for reading and writing data to the interface.\r
9  * Existing ports can easily be modified to fit another setup, e.g., a \r
10  * different TCP/IP stack, or to define your own stream port.\r
11  *\r
12   * Terms of Use\r
13  * This file is part of the trace recorder library (RECORDER), which is the \r
14  * intellectual property of Percepio AB (PERCEPIO) and provided under a\r
15  * license as follows.\r
16  * The RECORDER may be used free of charge for the purpose of recording data\r
17  * intended for analysis in PERCEPIO products. It may not be used or modified\r
18  * for other purposes without explicit permission from PERCEPIO.\r
19  * You may distribute the RECORDER in its original source code form, assuming\r
20  * this text (terms of use, disclaimer, copyright notice) is unchanged. You are\r
21  * allowed to distribute the RECORDER with minor modifications intended for\r
22  * configuration or porting of the RECORDER, e.g., to allow using it on a \r
23  * specific processor, processor family or with a specific communication\r
24  * interface. Any such modifications should be documented directly below\r
25  * this comment block.  \r
26  *\r
27  * Disclaimer\r
28  * The RECORDER is being delivered to you AS IS and PERCEPIO makes no warranty\r
29  * as to its use or performance. PERCEPIO does not and cannot warrant the \r
30  * performance or results you may obtain by using the RECORDER or documentation.\r
31  * PERCEPIO make no warranties, express or implied, as to noninfringement of\r
32  * third party rights, merchantability, or fitness for any particular purpose.\r
33  * In no event will PERCEPIO, its technology partners, or distributors be liable\r
34  * to you for any consequential, incidental or special damages, including any\r
35  * lost profits or lost savings, even if a representative of PERCEPIO has been\r
36  * advised of the possibility of such damages, or for any claim by any third\r
37  * party. Some jurisdictions do not allow the exclusion or limitation of\r
38  * incidental, consequential or special damages, or the exclusion of implied\r
39  * warranties or limitations on how long an implied warranty may last, so the\r
40  * above limitations may not apply to you.\r
41  *\r
42  * Tabs are used for indent in this file (1 tab = 4 spaces)\r
43  *\r
44  * Copyright Percepio AB, 2018.\r
45  * www.percepio.com\r
46  ******************************************************************************/\r
47 \r
48 #include "trcRecorder.h"\r
49 \r
50 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)  \r
51 #if (TRC_USE_TRACEALYZER_RECORDER == 1)\r
52         \r
53 /* TCP/IP includes - for lwIP in this case */\r
54 #include "lwip/tcpip.h"\r
55 #include "lwip/sockets.h"\r
56 \r
57 int errno;\r
58 \r
59 #define TRC_TCPIP_PORT 12000\r
60 \r
61 int sock = -1, new_sd = -1;\r
62 int flags = 0;\r
63 int remoteSize;\r
64 struct sockaddr_in address, remote;\r
65 \r
66 int32_t trcSocketSend( void* data, int32_t size, int32_t* bytesWritten )\r
67 {\r
68   if (new_sd < 0)\r
69     return -1;\r
70   \r
71   if (bytesWritten == NULL)\r
72         return -1;\r
73   \r
74   *bytesWritten = send( new_sd, data, size, 0 );\r
75   if (*bytesWritten < 0)\r
76   {\r
77     /* EWOULDBLOCK may be expected when buffers are full */\r
78     if (errno != 0 && errno != EWOULDBLOCK)\r
79     {\r
80       closesocket(new_sd);\r
81       new_sd = -1;\r
82       return -1;\r
83     }\r
84     else\r
85         *bytesWritten = 0;\r
86   }\r
87   \r
88   return 0;\r
89 }\r
90 \r
91 int32_t trcSocketReceive( void* data, int32_t size, int32_t* bytesRead )\r
92 {\r
93   if (new_sd < 0)\r
94     return -1;\r
95   \r
96   *bytesRead = recv( new_sd, data, size, 0 );\r
97   if ( *bytesRead < 0 )\r
98   {\r
99     /* EWOULDBLOCK may be expected when there is no data to receive */\r
100     if (errno != 0 && errno != EWOULDBLOCK)\r
101     {\r
102       closesocket(new_sd);\r
103       new_sd = -1;\r
104       return -1;\r
105     }\r
106     else\r
107         *bytesRead = 0;\r
108   }\r
109 \r
110   return 0;\r
111 }\r
112 \r
113 int32_t trcSocketInitializeListener()\r
114 {\r
115   if (sock >= 0)\r
116         return 0;\r
117   \r
118   sock = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
119 \r
120   if (sock < 0)\r
121     return -1;\r
122 \r
123   address.sin_family = AF_INET;\r
124   address.sin_port = htons( TRC_TCPIP_PORT );\r
125   address.sin_addr.s_addr = INADDR_ANY;\r
126 \r
127   if (bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0)\r
128   {\r
129     closesocket(sock);\r
130     sock = -1;\r
131     return -1;\r
132   }\r
133 \r
134   if (lwip_listen(sock, 5) < 0)\r
135   {\r
136     closesocket(sock);\r
137     sock = -1;\r
138     return -1;\r
139   }\r
140 \r
141   return 0;\r
142 }\r
143 \r
144 int32_t trcSocketAccept()\r
145 {\r
146   if (sock < 0)\r
147       return -1;\r
148   \r
149   if (new_sd >= 0)\r
150       return 0;\r
151   \r
152   remoteSize = sizeof( remote );\r
153   new_sd = accept( sock, (struct sockaddr *)&remote, (socklen_t*)&remoteSize );\r
154 \r
155   flags = fcntl( new_sd, F_GETFL, 0 );\r
156   fcntl( new_sd, F_SETFL, flags | O_NONBLOCK );\r
157 \r
158   if( new_sd < 0 )\r
159   {\r
160         closesocket(new_sd);\r
161     new_sd = -1;\r
162         closesocket(sock);\r
163     sock = -1;\r
164     return -1;\r
165   }\r
166 \r
167   return 0;\r
168 }\r
169 /************** MODIFY THE ABOVE PART TO USE YOUR TPC/IP STACK ****************/\r
170 \r
171 int32_t trcTcpWrite(void* data, uint32_t size, int32_t *ptrBytesWritten)\r
172 {\r
173     return trcSocketSend(data, size, ptrBytesWritten);\r
174 }\r
175 \r
176 int32_t trcTcpRead(void* data, uint32_t size, int32_t *ptrBytesRead)\r
177 {\r
178     trcSocketInitializeListener();\r
179         \r
180     trcSocketAccept();\r
181       \r
182     return trcSocketReceive(data, size, ptrBytesRead);\r
183 }\r
184 \r
185 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/\r
186 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/\r