]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-Trace/streamports/TCPIP/trcStreamingPort.c
487aede75e5e7648741f797a5534bd74460a0da0
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-Trace / streamports / TCPIP / trcStreamingPort.c
1 /*******************************************************************************\r
2  * Trace Recorder Library for Tracealyzer v3.1.2\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, 2017.\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 */\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   *bytesWritten = send( new_sd, data, size, 0 );\r
72   if (*bytesWritten < 0)\r
73   {\r
74     /* EWOULDBLOCK may be expected when buffers are full */\r
75     if (errno != EWOULDBLOCK)\r
76     {\r
77       closesocket(new_sd);\r
78       new_sd = -1;\r
79       return -1;\r
80     }\r
81     else\r
82         *bytesWritten = 0;\r
83   }\r
84   \r
85   return 0;\r
86 }\r
87 \r
88 int32_t trcSocketReceive( void* data, int32_t size, int32_t* bytesRead )\r
89 {\r
90   if (new_sd < 0)\r
91     return -1;\r
92   \r
93   *bytesRead = recv( new_sd, data, size, 0 );\r
94   if ( *bytesRead < 0 )\r
95   {\r
96     /* EWOULDBLOCK may be expected when there is no data to receive */\r
97     if (errno != EWOULDBLOCK)\r
98     {\r
99       closesocket(new_sd);\r
100       new_sd = -1;\r
101       return -1;\r
102     }\r
103     else\r
104         *bytesRead = 0;\r
105   }\r
106 \r
107   return 0;\r
108 }\r
109 \r
110 int32_t trcSocketInitializeListener()\r
111 {\r
112   if (sock >= 0)\r
113         return 0;\r
114   \r
115   sock = lwip_socket(AF_INET, SOCK_STREAM, 0);\r
116 \r
117   if (sock < 0)\r
118     return -1;\r
119 \r
120   address.sin_family = AF_INET;\r
121   address.sin_port = htons( TRC_TCPIP_PORT );\r
122   address.sin_addr.s_addr = INADDR_ANY;\r
123 \r
124   if (bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0)\r
125   {\r
126     closesocket(sock);\r
127     sock = -1;\r
128     return -1;\r
129   }\r
130 \r
131   if (lwip_listen(sock, 5) < 0)\r
132   {\r
133     closesocket(sock);\r
134     sock = -1;\r
135     return -1;\r
136   }\r
137 \r
138   return 0;\r
139 }\r
140 \r
141 int32_t trcSocketAccept()\r
142 {\r
143   if (sock < 0)\r
144       return -1;\r
145   \r
146   if (new_sd >= 0)\r
147       return 0;\r
148   \r
149   remoteSize = sizeof( remote );\r
150   new_sd = accept( sock, (struct sockaddr *)&remote, (socklen_t*)&remoteSize );\r
151 \r
152   flags = fcntl( new_sd, F_GETFL, 0 );\r
153   fcntl( new_sd, F_SETFL, flags | O_NONBLOCK );\r
154 \r
155   if( new_sd < 0 )\r
156   {\r
157         closesocket(new_sd);\r
158     new_sd = -1;\r
159         closesocket(sock);\r
160     sock = -1;\r
161     return -1;\r
162   }\r
163 \r
164   return 0;\r
165 }\r
166 /************** MODIFY THE ABOVE PART TO USE YOUR TPC/IP STACK ****************/\r
167 \r
168 int32_t trcTcpWrite(void* data, uint32_t size, int32_t *ptrBytesWritten)\r
169 {\r
170     return trcSocketSend(data, size, ptrBytesWritten);\r
171 }\r
172 \r
173 int32_t trcTcpRead(void* data, uint32_t size, int32_t *ptrBytesRead)\r
174 {\r
175     trcSocketInitializeListener();\r
176         \r
177     trcSocketAccept();\r
178       \r
179     return trcSocketReceive(data, size, ptrBytesRead);\r
180 }\r
181 \r
182 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/\r
183 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/\r