]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_UDP_Demos/CLICommands/UDPCommandServer.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_UDP_Demos / CLICommands / 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 /* Standard includes. */\r
30 #include <stdint.h>\r
31 #include <stdio.h>\r
32 \r
33 /* FreeRTOS includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* FreeRTOS+CLI includes. */\r
38 #include "FreeRTOS_CLI.h"\r
39 \r
40 /* FreeRTOS+UDP includes. */\r
41 #include "FreeRTOS_UDP_IP.h"\r
42 #include "FreeRTOS_Sockets.h"\r
43 \r
44 /* Demo app includes. */\r
45 #include "UDPCommandInterpreter.h"\r
46 \r
47 /* Dimensions the buffer into which input characters are placed. */\r
48 #define cmdMAX_INPUT_SIZE       60\r
49 \r
50 /* Dimensions the buffer into which string outputs can be placed. */\r
51 #define cmdMAX_OUTPUT_SIZE      1250\r
52 \r
53 /* Dimensions the buffer passed to the recvfrom() call. */\r
54 #define cmdSOCKET_INPUT_BUFFER_SIZE 60\r
55 \r
56 /*\r
57  * The task that runs FreeRTOS+CLI.\r
58  */\r
59 void vUDPCommandInterpreterTask( void *pvParameters );\r
60 \r
61 /*\r
62  * Open and configure the UDP socket.\r
63  */\r
64 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort );\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 void vStartUDPCommandInterpreterTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )\r
69 {\r
70         xTaskCreate( vUDPCommandInterpreterTask, "CLI", usStackSize, ( void * ) ulPort, uxPriority, NULL );\r
71 }\r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /*\r
75  * Task that provides the input and output for the FreeRTOS+CLI command\r
76  * interpreter.  In this case a UDP port is used.  See the URL in the comments\r
77  * within main.c for the location of the online documentation.\r
78  */\r
79 void vUDPCommandInterpreterTask( void *pvParameters )\r
80 {\r
81 long lBytes, lByte;\r
82 signed char cInChar, cInputIndex = 0;\r
83 static char cInputString[ cmdMAX_INPUT_SIZE ], cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];\r
84 BaseType_t xMoreDataToFollow;\r
85 struct freertos_sockaddr xClient;\r
86 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
87 xSocket_t xSocket;\r
88 \r
89         /* Just to prevent compiler warnings. */\r
90         ( void ) pvParameters;\r
91 \r
92         /* Attempt to open the socket.  The port number is passed in the task\r
93         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
94         machines. */\r
95         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
96 \r
97         if( xSocket != FREERTOS_INVALID_SOCKET )\r
98         {\r
99                 for( ;; )\r
100                 {\r
101                         /* Wait for incoming data on the opened socket. */\r
102                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
103 \r
104                         if( lBytes != FREERTOS_SOCKET_ERROR )\r
105                         {\r
106                                 /* Process each received byte in turn. */\r
107                                 lByte = 0;\r
108                                 while( lByte < lBytes )\r
109                                 {\r
110                                         /* The next character in the input buffer. */\r
111                                         cInChar = cLocalBuffer[ lByte ];\r
112                                         lByte++;\r
113 \r
114                                         /* Newline characters are taken as the end of the command\r
115                                         string. */\r
116                                         if( cInChar == '\n' )\r
117                                         {\r
118                                                 /* Process the input string received prior to the\r
119                                                 newline. */\r
120                                                 do\r
121                                                 {\r
122                                                         /* Pass the string to FreeRTOS+CLI. */\r
123                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
124 \r
125                                                         /* Send the output generated by the command's\r
126                                                         implementation. */\r
127                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, &xClient, xClientAddressLength );\r
128 \r
129                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
130 \r
131                                                 /* All the strings generated by the command processing\r
132                                                 have been sent.  Clear the input string ready to receive\r
133                                                 the next command. */\r
134                                                 cInputIndex = 0;\r
135                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
136 \r
137                                                 /* Transmit a spacer, just to make the command console\r
138                                                 easier to read. */\r
139                                                 FreeRTOS_sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, &xClient, xClientAddressLength );\r
140                                         }\r
141                                         else\r
142                                         {\r
143                                                 if( cInChar == '\r' )\r
144                                                 {\r
145                                                         /* Ignore the character.  Newlines are used to\r
146                                                         detect the end of the input string. */\r
147                                                 }\r
148                                                 else if( cInChar == '\b' )\r
149                                                 {\r
150                                                         /* Backspace was pressed.  Erase the last character\r
151                                                         in the string - if any. */\r
152                                                         if( cInputIndex > 0 )\r
153                                                         {\r
154                                                                 cInputIndex--;\r
155                                                                 cInputString[ cInputIndex ] = '\0';\r
156                                                         }\r
157                                                 }\r
158                                                 else\r
159                                                 {\r
160                                                         /* A character was entered.  Add it to the string\r
161                                                         entered so far.  When a \n is entered the complete\r
162                                                         string will be passed to the command interpreter. */\r
163                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
164                                                         {\r
165                                                                 cInputString[ cInputIndex ] = cInChar;\r
166                                                                 cInputIndex++;\r
167                                                         }\r
168                                                 }\r
169                                         }\r
170                                 }\r
171                         }\r
172                 }\r
173         }\r
174         else\r
175         {\r
176                 /* The socket could not be opened. */\r
177                 vTaskDelete( NULL );\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )\r
183 {\r
184 struct freertos_sockaddr xServer;\r
185 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;\r
186 \r
187         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
188         if( xSocket != FREERTOS_INVALID_SOCKET)\r
189         {\r
190                 /* Zero out the server structure. */\r
191                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
192 \r
193                 /* Set family and port. */\r
194                 xServer.sin_port = FreeRTOS_htons( usPort );\r
195 \r
196                 /* Bind the address to the socket. */\r
197                 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )\r
198                 {\r
199                         FreeRTOS_closesocket( xSocket );\r
200                         xSocket = FREERTOS_INVALID_SOCKET;\r
201                 }\r
202         }\r
203 \r
204         return xSocket;\r
205 }\r
206 \r
207 \r