]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator/DemoTasks/UDPCommandServer.c
53ab0aa2f08d6ee5582e6f823784790c4f3fad18
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_Windows_Simulator / DemoTasks / UDPCommandServer.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\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.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard includes. */\r
29 #include <stdint.h>\r
30 #include <stdio.h>\r
31 \r
32 /* FreeRTOS includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* FreeRTOS+CLI includes. */\r
37 #include "FreeRTOS_CLI.h"\r
38 \r
39 /* FreeRTOS+UDP includes. */\r
40 #include "FreeRTOS_UDP_IP.h"\r
41 #include "FreeRTOS_Sockets.h"\r
42 \r
43 /* Demo app includes. */\r
44 #include "UDPCommandInterpreter.h"\r
45 \r
46 /* Dimensions the buffer into which input characters are placed. */\r
47 #define cmdMAX_INPUT_SIZE       60\r
48 \r
49 /* Dimensions the buffer into which string outputs can be placed. */\r
50 #define cmdMAX_OUTPUT_SIZE      1024\r
51 \r
52 /* Dimensions the buffer passed to the recvfrom() call. */\r
53 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
54 \r
55 /*\r
56  * The task that runs FreeRTOS+CLI.\r
57  */\r
58 void vUDPCommandInterpreterTask( void *pvParameters );\r
59 \r
60 /*\r
61  * Open and configure the UDP socket.\r
62  */\r
63 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );\r
64 \r
65 /*-----------------------------------------------------------*/\r
66 \r
67 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
68 {\r
69         xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
70 }\r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /*\r
74  * Task that provides the input and output for the FreeRTOS+CLI command\r
75  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
76  * within main.c for the location of the online documentation.\r
77  */\r
78 void vUDPCommandInterpreterTask( void *pvParameters )\r
79 {\r
80 long lBytes, lByte;\r
81 signed char cInChar, cInputIndex = 0;\r
82 static signed char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
83 BaseType_t xMoreDataToFollow;\r
84 struct freertos_sockaddr xClient;\r
85 socklen_t xClientAddressLength = 0; /* This is required as a parameter to maintain the sendto() Berkeley sockets API - but it is not actually used so can take any value. */\r
86 xSocket_t xSocket;\r
87 extern const uint8_t ucIPAddress[ 4 ];\r
88 extern const uint8_t ucMACAddress[ 6 ];\r
89 \r
90         /* Just to prevent compiler warnings. */\r
91         ( void ) pvParameters;\r
92 \r
93         /* Attempt to open the socket.  The port number is passed in the task\r
94         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
95         machines. */\r
96         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
97 \r
98         if( xSocket != FREERTOS_INVALID_SOCKET )\r
99         {\r
100                 for( ;; )\r
101                 {\r
102                         /* Wait for incoming data on the opened socket. */\r
103                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
104 \r
105                         if( lBytes != FREERTOS_SOCKET_ERROR )\r
106                         {\r
107                                 /* Process each received byte in turn. */\r
108                                 lByte = 0;\r
109                                 while( lByte < lBytes )\r
110                                 {\r
111                                         /* The next character in the input buffer. */\r
112                                         cInChar = cLocalBuffer[ lByte ];\r
113                                         lByte++;\r
114 \r
115                                         /* Newline characters are taken as the end of the command\r
116                                         string. */\r
117                                         if( cInChar == '\n' )\r
118                                         {\r
119                                                 /* Process the input string received prior to the\r
120                                                 newline. */\r
121                                                 do\r
122                                                 {\r
123                                                         /* Pass the string to FreeRTOS+CLI. */\r
124                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
125 \r
126                                                         /* Send the output generated by the command's\r
127                                                         implementation. */\r
128                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, &xClient, xClientAddressLength );\r
129 \r
130                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
131 \r
132                                                 /* All the strings generated by the command processing\r
133                                                 have been sent.  Clear the input string ready to receive\r
134                                                 the next command. */\r
135                                                 cInputIndex = 0;\r
136                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
137 \r
138                                                 /* Transmit a spacer, just to make the command console\r
139                                                 easier to read. */\r
140                                                 FreeRTOS_sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, &xClient, xClientAddressLength );\r
141                                         }\r
142                                         else\r
143                                         {\r
144                                                 if( cInChar == '\r' )\r
145                                                 {\r
146                                                         /* Ignore the character.  Newlines are used to\r
147                                                         detect the end of the input string. */\r
148                                                 }\r
149                                                 else if( cInChar == '\b' )\r
150                                                 {\r
151                                                         /* Backspace was pressed.  Erase the last character\r
152                                                         in the string - if any. */\r
153                                                         if( cInputIndex > 0 )\r
154                                                         {\r
155                                                                 cInputIndex--;\r
156                                                                 cInputString[ cInputIndex ] = '\0';\r
157                                                         }\r
158                                                 }\r
159                                                 else\r
160                                                 {\r
161                                                         /* A character was entered.  Add it to the string\r
162                                                         entered so far.  When a \n is entered the complete\r
163                                                         string will be passed to the command interpreter. */\r
164                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
165                                                         {\r
166                                                                 cInputString[ cInputIndex ] = cInChar;\r
167                                                                 cInputIndex++;\r
168                                                         }\r
169                                                 }\r
170                                         }\r
171                                 }\r
172                         }\r
173                 }\r
174         }\r
175         else\r
176         {\r
177                 /* The socket could not be opened. */\r
178                 vTaskDelete( NULL );\r
179         }\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )\r
184 {\r
185 struct freertos_sockaddr xServer;\r
186 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;\r
187 \r
188         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
189         if( xSocket != FREERTOS_INVALID_SOCKET)\r
190         {\r
191                 /* Zero out the server structure. */\r
192                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
193 \r
194                 /* Set family and port. */\r
195                 xServer.sin_port = FreeRTOS_htons( usPort );\r
196 \r
197                 /* Bind the address to the socket. */\r
198                 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )\r
199                 {\r
200                         FreeRTOS_closesocket( xSocket );\r
201                         xSocket = FREERTOS_INVALID_SOCKET;\r
202                 }\r
203         }\r
204 \r
205         return xSocket;\r
206 }\r
207 \r
208 \r