1 /*******************************************************************************
\r
2 * Trace Recorder Library for Tracealyzer v4.1.5
\r
3 * Percepio AB, www.percepio.com
\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
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
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
42 * Tabs are used for indent in this file (1 tab = 4 spaces)
\r
44 * Copyright Percepio AB, 2018.
\r
46 ******************************************************************************/
\r
48 #include "trcRecorder.h"
\r
50 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
\r
51 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
\r
53 /* TCP/IP includes - for lwIP in this case */
\r
54 #include "lwip/tcpip.h"
\r
55 #include "lwip/sockets.h"
\r
59 #define TRC_TCPIP_PORT 12000
\r
61 int sock = -1, new_sd = -1;
\r
64 struct sockaddr_in address, remote;
\r
66 int32_t trcSocketSend( void* data, int32_t size, int32_t* bytesWritten )
\r
71 if (bytesWritten == NULL)
\r
74 *bytesWritten = send( new_sd, data, size, 0 );
\r
75 if (*bytesWritten < 0)
\r
77 /* EWOULDBLOCK may be expected when buffers are full */
\r
78 if (errno != 0 && errno != EWOULDBLOCK)
\r
80 closesocket(new_sd);
\r
91 int32_t trcSocketReceive( void* data, int32_t size, int32_t* bytesRead )
\r
96 *bytesRead = recv( new_sd, data, size, 0 );
\r
97 if ( *bytesRead < 0 )
\r
99 /* EWOULDBLOCK may be expected when there is no data to receive */
\r
100 if (errno != 0 && errno != EWOULDBLOCK)
\r
102 closesocket(new_sd);
\r
113 int32_t trcSocketInitializeListener()
\r
118 sock = lwip_socket(AF_INET, SOCK_STREAM, 0);
\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
127 if (bind(sock, (struct sockaddr *)&address, sizeof (address)) < 0)
\r
134 if (lwip_listen(sock, 5) < 0)
\r
144 int32_t trcSocketAccept()
\r
152 remoteSize = sizeof( remote );
\r
153 new_sd = accept( sock, (struct sockaddr *)&remote, (socklen_t*)&remoteSize );
\r
155 flags = fcntl( new_sd, F_GETFL, 0 );
\r
156 fcntl( new_sd, F_SETFL, flags | O_NONBLOCK );
\r
160 closesocket(new_sd);
\r
169 /************** MODIFY THE ABOVE PART TO USE YOUR TPC/IP STACK ****************/
\r
171 int32_t trcTcpWrite(void* data, uint32_t size, int32_t *ptrBytesWritten)
\r
173 return trcSocketSend(data, size, ptrBytesWritten);
\r
176 int32_t trcTcpRead(void* data, uint32_t size, int32_t *ptrBytesRead)
\r
178 trcSocketInitializeListener();
\r
182 return trcSocketReceive(data, size, ptrBytesRead);
\r
185 #endif /*(TRC_USE_TRACEALYZER_RECORDER == 1)*/
\r
186 #endif /*(TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)*/
\r