]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/UDPCommandServer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / UDPCommandServer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 #pragma comment( lib, "ws2_32.lib" )\r
30 \r
31 /* Win32 includes. */\r
32 #include <WinSock2.h>\r
33 #include <stdio.h>\r
34 \r
35 /* FreeRTOS includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 \r
39 /* FreeRTOS+CLI includes. */\r
40 #include "FreeRTOS_CLI.h"\r
41 \r
42 /* Dimensions the buffer into which input characters are placed. */\r
43 #define cmdMAX_INPUT_SIZE       60\r
44 \r
45 /* Dimensions the buffer into which string outputs can be placed. */\r
46 #define cmdMAX_OUTPUT_SIZE      1024\r
47 \r
48 /* Dimensions the buffer passed to the recvfrom() call. */\r
49 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
50 \r
51 /*\r
52  * Open and configure the UDP socket.\r
53  */\r
54 static SOCKET prvOpenUDPSocket( void );\r
55 \r
56 /*-----------------------------------------------------------*/\r
57 \r
58 /*\r
59  * Task that provides the input and output for the FreeRTOS+CLI command\r
60  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
61  * within main.c for the location of the online documentation.\r
62  */\r
63 void vUDPCommandInterpreterTask( void *pvParameters )\r
64 {\r
65 long lBytes, lByte;\r
66 signed char cInChar, cInputIndex = 0;\r
67 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
68 BaseType_t xMoreDataToFollow;\r
69 volatile int iErrorCode = 0;\r
70 struct sockaddr_in xClient;\r
71 int xClientAddressLength = sizeof( struct sockaddr_in );\r
72 SOCKET xSocket;\r
73 \r
74         /* Just to prevent compiler warnings. */\r
75         ( void ) pvParameters;\r
76 \r
77         /* Attempt to open the socket. */\r
78         xSocket = prvOpenUDPSocket();\r
79 \r
80         if( xSocket != INVALID_SOCKET )\r
81         {\r
82                 for( ;; )\r
83                 {\r
84                         /* Wait for incoming data on the opened socket. */\r
85                         lBytes = recvfrom( xSocket, cLocalBuffer, sizeof( cLocalBuffer ), 0, ( struct sockaddr * ) &xClient, &xClientAddressLength );\r
86 \r
87                         if( lBytes == SOCKET_ERROR )\r
88                         {\r
89                                 /* Something went wrong, but it is not handled by this simple\r
90                                 example. */\r
91                                 iErrorCode = WSAGetLastError();\r
92                         }\r
93                         else\r
94                         {\r
95                                 /* Process each received byte in turn. */\r
96                                 lByte = 0;\r
97                                 while( lByte < lBytes )\r
98                                 {\r
99                                         /* The next character in the input buffer. */\r
100                                         cInChar = cLocalBuffer[ lByte ];\r
101                                         lByte++;\r
102 \r
103                                         /* Newline characters are taken as the end of the command\r
104                                         string. */\r
105                                         if( cInChar == '\n' )\r
106                                         {\r
107                                                 /* Process the input string received prior to the\r
108                                                 newline. */\r
109                                                 do\r
110                                                 {\r
111                                                         /* Pass the string to FreeRTOS+CLI. */\r
112                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
113 \r
114                                                         /* Send the output generated by the command's\r
115                                                         implementation. */\r
116                                                         sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
117 \r
118                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
119 \r
120                                                 /* All the strings generated by the command processing\r
121                                                 have been sent.  Clear the input string ready to receive\r
122                                                 the next command. */\r
123                                                 cInputIndex = 0;\r
124                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
125 \r
126                                                 /* Transmit a spacer, just to make the command console\r
127                                                 easier to read. */\r
128                                                 sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, ( SOCKADDR * ) &xClient, xClientAddressLength );\r
129                                         }\r
130                                         else\r
131                                         {\r
132                                                 if( cInChar == '\r' )\r
133                                                 {\r
134                                                         /* Ignore the character.  Newlines are used to\r
135                                                         detect the end of the input string. */\r
136                                                 }\r
137                                                 else if( cInChar == '\b' )\r
138                                                 {\r
139                                                         /* Backspace was pressed.  Erase the last character\r
140                                                         in the string - if any. */\r
141                                                         if( cInputIndex > 0 )\r
142                                                         {\r
143                                                                 cInputIndex--;\r
144                                                                 cInputString[ cInputIndex ] = '\0';\r
145                                                         }\r
146                                                 }\r
147                                                 else\r
148                                                 {\r
149                                                         /* A character was entered.  Add it to the string\r
150                                                         entered so far.  When a \n is entered the complete\r
151                                                         string will be passed to the command interpreter. */\r
152                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
153                                                         {\r
154                                                                 cInputString[ cInputIndex ] = cInChar;\r
155                                                                 cInputIndex++;\r
156                                                         }\r
157                                                 }\r
158                                         }\r
159                                 }\r
160                         }\r
161                 }\r
162         }\r
163         else\r
164         {\r
165                 /* The socket could not be opened. */\r
166                 vTaskDelete( NULL );\r
167         }\r
168 }\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static SOCKET prvOpenUDPSocket( void )\r
172 {\r
173 WSADATA xWSAData;\r
174 WORD wVersionRequested;\r
175 struct sockaddr_in xServer;\r
176 SOCKET xSocket = INVALID_SOCKET;\r
177 \r
178         wVersionRequested = MAKEWORD( 2, 2 );\r
179 \r
180         /* Prepare to use WinSock. */\r
181         if( WSAStartup( wVersionRequested, &xWSAData ) != 0 )\r
182         {\r
183                 fprintf( stderr, "Could not open Windows connection.\n" );\r
184         }\r
185         else\r
186         {\r
187                 xSocket = socket( AF_INET, SOCK_DGRAM, 0 );\r
188                 if( xSocket == INVALID_SOCKET)\r
189                 {\r
190                         fprintf( stderr, "Could not create socket.\n" );\r
191                         WSACleanup();\r
192                 }\r
193                 else\r
194                 {\r
195                         /* Zero out the server structure. */\r
196                         memset( ( void * ) &xServer, 0x00, sizeof( struct sockaddr_in ) );\r
197 \r
198                         /* Set family and port. */\r
199                         xServer.sin_family = AF_INET;\r
200                         xServer.sin_port = htons( configUDP_CLI_PORT_NUMBER );\r
201 \r
202                         /* Assign the loopback address */\r
203                         xServer.sin_addr.S_un.S_un_b.s_b1 = 127;\r
204                         xServer.sin_addr.S_un.S_un_b.s_b2 = 0;\r
205                         xServer.sin_addr.S_un.S_un_b.s_b3 = 0;\r
206                         xServer.sin_addr.S_un.S_un_b.s_b4 = 1;\r
207 \r
208                         /* Bind the address to the socket. */\r
209                         if( bind( xSocket, ( struct sockaddr * ) &xServer, sizeof( struct sockaddr_in ) ) == -1 )\r
210                         {\r
211                                 fprintf( stderr, "Could not socket to port %d.\n", configUDP_CLI_PORT_NUMBER );\r
212                                 closesocket( xSocket );\r
213                                 xSocket = INVALID_SOCKET;\r
214                                 WSACleanup();\r
215                         }\r
216                 }\r
217         }\r
218 \r
219         return xSocket;\r
220 }\r
221 \r
222 \r