]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/Common/FreeRTOS_Plus_UDP_Demos/CLICommands/UDPCommandServer.c
8e84c34b4c4a17063b55379c6f08a22060cfc50a
[freertos] / FreeRTOS-Plus / Demo / Common / FreeRTOS_Plus_UDP_Demos / CLICommands / 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      1250\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 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 \r
88         /* Just to prevent compiler warnings. */\r
89         ( void ) pvParameters;\r
90 \r
91         /* Attempt to open the socket.  The port number is passed in the task\r
92         parameter.  The strange casting is to remove compiler warnings on 32-bit\r
93         machines. */\r
94         xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );\r
95 \r
96         if( xSocket != FREERTOS_INVALID_SOCKET )\r
97         {\r
98                 for( ;; )\r
99                 {\r
100                         /* Wait for incoming data on the opened socket. */\r
101                         lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );\r
102 \r
103                         if( lBytes != FREERTOS_SOCKET_ERROR )\r
104                         {\r
105                                 /* Process each received byte in turn. */\r
106                                 lByte = 0;\r
107                                 while( lByte < lBytes )\r
108                                 {\r
109                                         /* The next character in the input buffer. */\r
110                                         cInChar = cLocalBuffer[ lByte ];\r
111                                         lByte++;\r
112 \r
113                                         /* Newline characters are taken as the end of the command\r
114                                         string. */\r
115                                         if( cInChar == '\n' )\r
116                                         {\r
117                                                 /* Process the input string received prior to the\r
118                                                 newline. */\r
119                                                 do\r
120                                                 {\r
121                                                         /* Pass the string to FreeRTOS+CLI. */\r
122                                                         xMoreDataToFollow = FreeRTOS_CLIProcessCommand( cInputString, cOutputString, cmdMAX_OUTPUT_SIZE );\r
123 \r
124                                                         /* Send the output generated by the command's\r
125                                                         implementation. */\r
126                                                         FreeRTOS_sendto( xSocket, cOutputString,  strlen( cOutputString ), 0, &xClient, xClientAddressLength );\r
127 \r
128                                                 } while( xMoreDataToFollow != pdFALSE ); /* Until the command does not generate any more output. */\r
129 \r
130                                                 /* All the strings generated by the command processing\r
131                                                 have been sent.  Clear the input string ready to receive\r
132                                                 the next command. */\r
133                                                 cInputIndex = 0;\r
134                                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
135 \r
136                                                 /* Transmit a spacer, just to make the command console\r
137                                                 easier to read. */\r
138                                                 FreeRTOS_sendto( xSocket, "\r\n",  strlen( "\r\n" ), 0, &xClient, xClientAddressLength );\r
139                                         }\r
140                                         else\r
141                                         {\r
142                                                 if( cInChar == '\r' )\r
143                                                 {\r
144                                                         /* Ignore the character.  Newlines are used to\r
145                                                         detect the end of the input string. */\r
146                                                 }\r
147                                                 else if( cInChar == '\b' )\r
148                                                 {\r
149                                                         /* Backspace was pressed.  Erase the last character\r
150                                                         in the string - if any. */\r
151                                                         if( cInputIndex > 0 )\r
152                                                         {\r
153                                                                 cInputIndex--;\r
154                                                                 cInputString[ cInputIndex ] = '\0';\r
155                                                         }\r
156                                                 }\r
157                                                 else\r
158                                                 {\r
159                                                         /* A character was entered.  Add it to the string\r
160                                                         entered so far.  When a \n is entered the complete\r
161                                                         string will be passed to the command interpreter. */\r
162                                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
163                                                         {\r
164                                                                 cInputString[ cInputIndex ] = cInChar;\r
165                                                                 cInputIndex++;\r
166                                                         }\r
167                                                 }\r
168                                         }\r
169                                 }\r
170                         }\r
171                 }\r
172         }\r
173         else\r
174         {\r
175                 /* The socket could not be opened. */\r
176                 vTaskDelete( NULL );\r
177         }\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 static xSocket_t prvOpenUDPServerSocket( uint16_t usPort )\r
182 {\r
183 struct freertos_sockaddr xServer;\r
184 xSocket_t xSocket = FREERTOS_INVALID_SOCKET;\r
185 \r
186         xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
187         if( xSocket != FREERTOS_INVALID_SOCKET)\r
188         {\r
189                 /* Zero out the server structure. */\r
190                 memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );\r
191 \r
192                 /* Set family and port. */\r
193                 xServer.sin_port = FreeRTOS_htons( usPort );\r
194 \r
195                 /* Bind the address to the socket. */\r
196                 if( FreeRTOS_bind( xSocket, &xServer, sizeof( xServer ) ) == -1 )\r
197                 {\r
198                         FreeRTOS_closesocket( xSocket );\r
199                         xSocket = FREERTOS_INVALID_SOCKET;\r
200                 }\r
201         }\r
202 \r
203         return xSocket;\r
204 }\r
205 \r
206 \r