Skip to content

Commit d6c89bb

Browse files
committed
2 parents 3070037 + 0c0f0d0 commit d6c89bb

File tree

11,733 files changed

+6151998
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

11,733 files changed

+6151998
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[{000214A0-0000-0000-C000-000000000046}]
2+
Prop3=19,2
3+
[InternetShortcut]
4+
URL=http://www.freertos.org/labs
5+
IDList=
Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
/*
2+
* FreeRTOS+TCP V2.0.3
3+
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
* this software and associated documentation files (the "Software"), to deal in
7+
* the Software without restriction, including without limitation the rights to
8+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
* the Software, and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
*
22+
* http://aws.amazon.com/freertos
23+
* http://www.FreeRTOS.org
24+
*/
25+
26+
27+
/* Standard includes. */
28+
#include <stdint.h>
29+
#include <stdio.h>
30+
#include <stdlib.h>
31+
32+
/* FreeRTOS includes. */
33+
#include "FreeRTOS.h"
34+
#include "task.h"
35+
36+
/* FreeRTOS+TCP includes. */
37+
#include "FreeRTOS_IP.h"
38+
#include "FreeRTOS_Sockets.h"
39+
#include "FreeRTOS_TCP_server.h"
40+
#include "FreeRTOS_server_private.h"
41+
42+
/* Remove the entire file if TCP is not being used. */
43+
#if( ipconfigUSE_TCP == 1 ) && ( ( ipconfigUSE_HTTP == 1 ) || ( ipconfigUSE_FTP == 1 ) )
44+
45+
#if !defined( ARRAY_SIZE )
46+
#define ARRAY_SIZE(x) ( BaseType_t ) (sizeof( x ) / sizeof( x )[ 0 ] )
47+
#endif
48+
49+
50+
static void prvReceiveNewClient( TCPServer_t *pxServer, BaseType_t xIndex, Socket_t xNexSocket );
51+
static char *strnew( const char *pcString );
52+
/* Remove slashes at the end of a path. */
53+
static void prvRemoveSlash( char *pcDir );
54+
55+
TCPServer_t *FreeRTOS_CreateTCPServer( const struct xSERVER_CONFIG *pxConfigs, BaseType_t xCount )
56+
{
57+
TCPServer_t *pxServer;
58+
SocketSet_t xSocketSet;
59+
60+
/* Create a new server.
61+
xPort / xPortAlt : Make the service available on 1 or 2 public port numbers. */
62+
xSocketSet = FreeRTOS_CreateSocketSet();
63+
64+
if( xSocketSet != NULL )
65+
{
66+
BaseType_t xSize;
67+
68+
xSize = sizeof( *pxServer ) - sizeof( pxServer->xServers ) + xCount * sizeof( pxServer->xServers[ 0 ] );
69+
70+
pxServer = ( TCPServer_t * ) pvPortMallocLarge( xSize );
71+
if( pxServer != NULL )
72+
{
73+
struct freertos_sockaddr xAddress;
74+
BaseType_t xNoTimeout = 0;
75+
BaseType_t xIndex;
76+
77+
memset( pxServer, '\0', xSize );
78+
pxServer->xServerCount = xCount;
79+
pxServer->xSocketSet = xSocketSet;
80+
81+
for( xIndex = 0; xIndex < xCount; xIndex++ )
82+
{
83+
BaseType_t xPortNumber = pxConfigs[ xIndex ].xPortNumber;
84+
85+
if( xPortNumber > 0 )
86+
{
87+
Socket_t xSocket;
88+
89+
xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP );
90+
FreeRTOS_printf( ( "TCP socket on port %d\n", ( int )xPortNumber ) );
91+
92+
if( xSocket != FREERTOS_NO_SOCKET )
93+
{
94+
xAddress.sin_addr = FreeRTOS_GetIPAddress(); // Single NIC, currently not used
95+
xAddress.sin_port = FreeRTOS_htons( xPortNumber );
96+
97+
FreeRTOS_bind( xSocket, &xAddress, sizeof( xAddress ) );
98+
FreeRTOS_listen( xSocket, pxConfigs[ xIndex ].xBackLog );
99+
100+
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xNoTimeout, sizeof( BaseType_t ) );
101+
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, ( void * ) &xNoTimeout, sizeof( BaseType_t ) );
102+
103+
#if( ipconfigHTTP_RX_BUFSIZE > 0 )
104+
{
105+
if( pxConfigs[ xIndex ].eType == eSERVER_HTTP )
106+
{
107+
WinProperties_t xWinProps;
108+
109+
memset( &xWinProps, '\0', sizeof( xWinProps ) );
110+
/* The parent socket itself won't get connected. The properties below
111+
will be inherited by each new child socket. */
112+
xWinProps.lTxBufSize = ipconfigHTTP_TX_BUFSIZE;
113+
xWinProps.lTxWinSize = ipconfigHTTP_TX_WINSIZE;
114+
xWinProps.lRxBufSize = ipconfigHTTP_RX_BUFSIZE;
115+
xWinProps.lRxWinSize = ipconfigHTTP_RX_WINSIZE;
116+
117+
/* Set the window and buffer sizes. */
118+
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_WIN_PROPERTIES, ( void * ) &xWinProps, sizeof( xWinProps ) );
119+
}
120+
}
121+
#endif
122+
123+
FreeRTOS_FD_SET( xSocket, xSocketSet, eSELECT_READ|eSELECT_EXCEPT );
124+
pxServer->xServers[ xIndex ].xSocket = xSocket;
125+
pxServer->xServers[ xIndex ].eType = pxConfigs[ xIndex ].eType;
126+
pxServer->xServers[ xIndex ].pcRootDir = strnew( pxConfigs[ xIndex ].pcRootDir );
127+
prvRemoveSlash( ( char * ) pxServer->xServers[ xIndex ].pcRootDir );
128+
}
129+
}
130+
}
131+
}
132+
else
133+
{
134+
/* Could not allocate the server, delete the socket set */
135+
FreeRTOS_DeleteSocketSet( xSocketSet );
136+
}
137+
}
138+
else
139+
{
140+
/* Could not create a socket set, return NULL */
141+
pxServer = NULL;
142+
}
143+
144+
return pxServer;
145+
}
146+
/*-----------------------------------------------------------*/
147+
148+
static void prvReceiveNewClient( TCPServer_t *pxServer, BaseType_t xIndex, Socket_t xNexSocket )
149+
{
150+
TCPClient_t *pxClient = NULL;
151+
BaseType_t xSize = 0;
152+
FTCPWorkFunction fWorkFunc = NULL;
153+
FTCPDeleteFunction fDeleteFunc = NULL;
154+
const char *pcType = "Unknown";
155+
156+
/*_RB_ Can the work and delete functions be part of the xSERVER_CONFIG structure
157+
becomes generic, with no pre-processing required? */
158+
#if( ipconfigUSE_HTTP != 0 )
159+
{
160+
if( pxServer->xServers[ xIndex ].eType == eSERVER_HTTP )
161+
{
162+
xSize = sizeof( HTTPClient_t );
163+
fWorkFunc = xHTTPClientWork;
164+
fDeleteFunc = vHTTPClientDelete;
165+
pcType = "HTTP";
166+
}
167+
}
168+
#endif /* ipconfigUSE_HTTP != 0 */
169+
170+
#if( ipconfigUSE_FTP != 0 )
171+
{
172+
if( pxServer->xServers[ xIndex ].eType == eSERVER_FTP )
173+
{
174+
xSize = sizeof( FTPClient_t );
175+
fWorkFunc = xFTPClientWork;
176+
fDeleteFunc = vFTPClientDelete;
177+
pcType = "FTP";
178+
}
179+
}
180+
#endif /* ipconfigUSE_FTP != 0 */
181+
182+
/* Malloc enough space for a new HTTP-client */
183+
if( xSize )
184+
{
185+
pxClient = ( TCPClient_t* ) pvPortMallocLarge( xSize );
186+
}
187+
188+
if( pxClient != NULL )
189+
{
190+
memset( pxClient, '\0', xSize );
191+
192+
/* Put the new client in front of the list. */
193+
pxClient->eType = pxServer->xServers[ xIndex ].eType;
194+
pxClient->pcRootDir = pxServer->xServers[ xIndex ].pcRootDir;
195+
pxClient->pxParent = pxServer;
196+
pxClient->xSocket = xNexSocket;
197+
pxClient->pxNextClient = pxServer->pxClients;
198+
pxClient->fWorkFunction = fWorkFunc;
199+
pxClient->fDeleteFunction = fDeleteFunc;
200+
pxServer->pxClients = pxClient;
201+
202+
FreeRTOS_FD_SET( xNexSocket, pxServer->xSocketSet, eSELECT_READ|eSELECT_EXCEPT );
203+
}
204+
else
205+
{
206+
pcType = "closed";
207+
FreeRTOS_closesocket( xNexSocket );
208+
}
209+
{
210+
struct freertos_sockaddr xRemoteAddress;
211+
FreeRTOS_GetRemoteAddress( pxClient->xSocket, &xRemoteAddress );
212+
FreeRTOS_printf( ( "TPC-server: new %s client %xip\n", pcType, (unsigned)FreeRTOS_ntohl( xRemoteAddress.sin_addr ) ) );
213+
}
214+
215+
/* Remove compiler warnings in case FreeRTOS_printf() is not used. */
216+
( void ) pcType;
217+
}
218+
/*-----------------------------------------------------------*/
219+
220+
void FreeRTOS_TCPServerWork( TCPServer_t *pxServer, TickType_t xBlockingTime )
221+
{
222+
TCPClient_t **ppxClient;
223+
BaseType_t xIndex;
224+
BaseType_t xRc;
225+
226+
/* Let the server do one working cycle */
227+
xRc = FreeRTOS_select( pxServer->xSocketSet, xBlockingTime );
228+
229+
if( xRc != 0 )
230+
{
231+
for( xIndex = 0; xIndex < pxServer->xServerCount; xIndex++ )
232+
{
233+
struct freertos_sockaddr xAddress;
234+
Socket_t xNexSocket;
235+
socklen_t xSocketLength;
236+
237+
if( pxServer->xServers[ xIndex ].xSocket == FREERTOS_NO_SOCKET )
238+
{
239+
continue;
240+
}
241+
242+
xSocketLength = sizeof( xAddress );
243+
xNexSocket = FreeRTOS_accept( pxServer->xServers[ xIndex ].xSocket, &xAddress, &xSocketLength);
244+
245+
if( ( xNexSocket != FREERTOS_NO_SOCKET ) && ( xNexSocket != FREERTOS_INVALID_SOCKET ) )
246+
{
247+
prvReceiveNewClient( pxServer, xIndex, xNexSocket );
248+
}
249+
}
250+
}
251+
252+
ppxClient = &pxServer->pxClients;
253+
254+
while( ( * ppxClient ) != NULL )
255+
{
256+
TCPClient_t *pxThis = *ppxClient;
257+
258+
/* Almost C++ */
259+
xRc = pxThis->fWorkFunction( pxThis );
260+
261+
if (xRc < 0 )
262+
{
263+
*ppxClient = pxThis->pxNextClient;
264+
/* Close handles, resources */
265+
pxThis->fDeleteFunction( pxThis );
266+
/* Free the space */
267+
vPortFreeLarge( pxThis );
268+
}
269+
else
270+
{
271+
ppxClient = &( pxThis->pxNextClient );
272+
}
273+
}
274+
}
275+
/*-----------------------------------------------------------*/
276+
277+
static char *strnew( const char *pcString )
278+
{
279+
BaseType_t xLength;
280+
char *pxBuffer;
281+
282+
xLength = strlen( pcString ) + 1;
283+
pxBuffer = ( char * ) pvPortMalloc( xLength );
284+
if( pxBuffer != NULL )
285+
{
286+
memcpy( pxBuffer, pcString, xLength );
287+
}
288+
289+
return pxBuffer;
290+
}
291+
/*-----------------------------------------------------------*/
292+
293+
static void prvRemoveSlash( char *pcDir )
294+
{
295+
BaseType_t xLength = strlen( pcDir );
296+
297+
while( ( xLength > 0 ) && ( pcDir[ xLength - 1 ] == '/' ) )
298+
{
299+
pcDir[ --xLength ] = '\0';
300+
}
301+
}
302+
/*-----------------------------------------------------------*/
303+
304+
#if( ipconfigSUPPORT_SIGNALS != 0 )
305+
306+
/* FreeRTOS_TCPServerWork() calls select().
307+
The two functions below provide a possibility to interrupt
308+
the call to select(). After the interruption, resume
309+
by calling FreeRTOS_TCPServerWork() again. */
310+
BaseType_t FreeRTOS_TCPServerSignal( TCPServer_t *pxServer )
311+
{
312+
BaseType_t xIndex;
313+
BaseType_t xResult = pdFALSE;
314+
for( xIndex = 0; xIndex < pxServer->xServerCount; xIndex++ )
315+
{
316+
if( pxServer->xServers[ xIndex ].xSocket != FREERTOS_NO_SOCKET )
317+
{
318+
FreeRTOS_SignalSocket( pxServer->xServers[ xIndex ].xSocket );
319+
xResult = pdTRUE;
320+
break;
321+
}
322+
}
323+
324+
return xResult;
325+
}
326+
327+
#endif /* ipconfigSUPPORT_SIGNALS */
328+
/*-----------------------------------------------------------*/
329+
330+
#if( ipconfigSUPPORT_SIGNALS != 0 )
331+
332+
/* Same as above: this function may be called from an ISR,
333+
for instance a GPIO interrupt. */
334+
BaseType_t FreeRTOS_TCPServerSignalFromISR( TCPServer_t *pxServer, BaseType_t *pxHigherPriorityTaskWoken )
335+
{
336+
BaseType_t xIndex;
337+
BaseType_t xResult = pdFALSE;
338+
for( xIndex = 0; xIndex < pxServer->xServerCount; xIndex++ )
339+
{
340+
if( pxServer->xServers[ xIndex ].xSocket != FREERTOS_NO_SOCKET )
341+
{
342+
FreeRTOS_SignalSocketFromISR( pxServer->xServers[ xIndex ].xSocket, pxHigherPriorityTaskWoken );
343+
xResult = pdTRUE;
344+
break;
345+
}
346+
}
347+
348+
return xResult;
349+
}
350+
#endif /* ipconfigSUPPORT_SIGNALS */
351+
/*-----------------------------------------------------------*/
352+
353+
#endif /* ( ipconfigUSE_TCP == 1 ) && ( ( ipconfigUSE_HTTP == 1 ) || ( ipconfigUSE_FTP == 1 ) ) */

0 commit comments

Comments
 (0)