1 /*
2 * FreeRTOS+TCP V3.1.0
3 * Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * http://aws.amazon.com/freertos
25 * http://www.FreeRTOS.org
26 */
27
28 /**
29 * @file main.c
30 * @brief Implements the main function.
31 */
32
33 /* FreeRTOS include. */
34 #include <FreeRTOS.h>
35 #include "task.h"
36
37 /* System application includes. */
38 #include "FreeRTOS_IP.h"
39 #include "FreeRTOS_Sockets.h"
40 #include "FreeRTOS_DHCP.h"
41
42 #define mainHOST_NAME "Build Combination"
43
44 /*-----------------------------------------------------------*/
45
46 /* Notes if the trace is running or not. */
47 static BaseType_t xTraceRunning = pdTRUE;
48
49 /* Default MAC address configuration. The demo creates a virtual network
50 * connection that uses this MAC address by accessing the raw Ethernet data
51 * to and from a real network connection on the host PC. See the
52 * configNETWORK_INTERFACE_TO_USE definition for information on how to configure
53 * the real network connection to use. */
54 const uint8_t ucMACAddress[ 6 ] =
55 {
56 configMAC_ADDR0,
57 configMAC_ADDR1,
58 configMAC_ADDR2,
59 configMAC_ADDR3,
60 configMAC_ADDR4,
61 configMAC_ADDR5
62 };
63
64 /* The default IP and MAC address used by the code. It is used as a place holder.
65 */
66 static const uint8_t ucIPAddress[ 4 ] =
67 {
68 configIP_ADDR0,
69 configIP_ADDR1,
70 configIP_ADDR2,
71 configIP_ADDR3
72 };
73 static const uint8_t ucNetMask[ 4 ] =
74 {
75 configNET_MASK0,
76 configNET_MASK1,
77 configNET_MASK2,
78 configNET_MASK3
79 };
80 static const uint8_t ucGatewayAddress[ 4 ] =
81 {
82 configGATEWAY_ADDR0,
83 configGATEWAY_ADDR1,
84 configGATEWAY_ADDR2,
85 configGATEWAY_ADDR3
86 };
87 static const uint8_t ucDNSServerAddress[ 4 ] =
88 {
89 configDNS_SERVER_ADDR0,
90 configDNS_SERVER_ADDR1,
91 configDNS_SERVER_ADDR2,
92 configDNS_SERVER_ADDR3
93 };
94
95 /* Use by the pseudo random number generator. */
96 static UBaseType_t ulNextRand;
97
98 /*-----------------------------------------------------------*/
main(void)99 int main( void )
100 {
101 /* Initialize the network interface.
102 *
103 ***NOTE*** Tasks that use the network are created in the network event hook
104 * when the network is connected and ready for use (see the definition of
105 * vApplicationIPNetworkEventHook() below). The address values passed in here
106 * are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1
107 * but a DHCP server cannot be contacted. */
108 FreeRTOS_printf( ( "FreeRTOS_IPInit\n" ) );
109 FreeRTOS_IPInit(
110 ucIPAddress,
111 ucNetMask,
112 ucGatewayAddress,
113 ucDNSServerAddress,
114 ucMACAddress );
115
116 vTaskStartScheduler();
117
118 return 0;
119 }
120 /*-----------------------------------------------------------*/
121
vApplicationIPNetworkEventHook(eIPCallbackEvent_t eNetworkEvent)122 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
123 {
124 static BaseType_t xTasksAlreadyCreated = pdFALSE;
125
126 /* If the network has just come up...*/
127 if( ( eNetworkEvent == eNetworkUp ) && ( xTasksAlreadyCreated == pdFALSE ) )
128 {
129 /* Do nothing. Just a stub. */
130
131 xTasksAlreadyCreated = pdTRUE;
132 }
133 }
134
135 /*-----------------------------------------------------------*/
136
137 #if ( ( ipconfigUSE_LLMNR != 0 ) || \
138 ( ipconfigUSE_NBNS != 0 ) || \
139 ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) )
140
pcApplicationHostnameHook(void)141 const char * pcApplicationHostnameHook( void )
142 {
143 /* This function will be called during the DHCP: the machine will be registered
144 * with an IP address plus this name. */
145 return mainHOST_NAME;
146 }
147
148 #endif /* if ( ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) ) */
149 /*-----------------------------------------------------------*/
150
151 #if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
152
xApplicationDNSQueryHook(const char * pcName)153 BaseType_t xApplicationDNSQueryHook( const char * pcName )
154 {
155 BaseType_t xReturn;
156
157 /* Determine if a name lookup is for this node. Two names are given
158 * to this node: that returned by pcApplicationHostnameHook() and that set
159 * by mainDEVICE_NICK_NAME. */
160 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
161 {
162 xReturn = pdPASS;
163 }
164 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
165 {
166 xReturn = pdPASS;
167 }
168 else
169 {
170 xReturn = pdFAIL;
171 }
172
173 return xReturn;
174 }
175
176 #endif /* if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) */
177 /*-----------------------------------------------------------*/
178
vApplicationIdleHook(void)179 void vApplicationIdleHook( void )
180 {
181 const uint32_t ulMSToSleep = 1;
182 const TickType_t xKitHitCheckPeriod = pdMS_TO_TICKS( 1000UL );
183 static TickType_t xTimeNow, xLastTimeCheck = 0;
184
185 if( ( xTimeNow - xLastTimeCheck ) > xKitHitCheckPeriod )
186 {
187 xLastTimeCheck = xTimeNow;
188 }
189
190 /* Exit. Just a stub. */
191 }
192 /*-----------------------------------------------------------*/
193
vAssertCalled(const char * pcFile,uint32_t ulLine)194 void vAssertCalled( const char * pcFile,
195 uint32_t ulLine )
196 {
197 const uint32_t ulLongSleep = 1000UL;
198 volatile uint32_t ulBlockVariable = 0UL;
199 volatile char * pcFileName = ( volatile char * ) pcFile;
200 volatile uint32_t ulLineNumber = ulLine;
201
202 ( void ) pcFileName;
203 ( void ) ulLineNumber;
204
205 taskDISABLE_INTERRUPTS();
206 {
207 while( 1 )
208 {
209 }
210 }
211 taskENABLE_INTERRUPTS();
212 }
213 /*-----------------------------------------------------------*/
214
getUserCmd(char * pucUserCmd)215 void getUserCmd( char * pucUserCmd )
216 {
217 /* Provide a stub for this function. */
218 }
219 /*-----------------------------------------------------------*/
220
uxRand(void)221 UBaseType_t uxRand( void )
222 {
223 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
224
225 /* Utility function to generate a pseudo random number. */
226
227 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
228 return( ( int ) ( ulNextRand ) & 0x7fffUL );
229 }
230
xApplicationGetRandomNumber(uint32_t * pulNumber)231 BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
232 {
233 *pulNumber = uxRand();
234
235 return pdTRUE;
236 }
237
vApplicationGetIdleTaskMemory(StaticTask_t ** ppxIdleTaskTCBBuffer,StackType_t ** ppxIdleTaskStackBuffer,uint32_t * pulIdleTaskStackSize)238 void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
239 StackType_t ** ppxIdleTaskStackBuffer,
240 uint32_t * pulIdleTaskStackSize )
241 {
242 /* Provide a stub for this function. */
243 }
244
245
246 /*
247 * Callback that provides the inputs necessary to generate a randomized TCP
248 * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION
249 * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION
250 * SYSTEMS.
251 */
ulApplicationGetNextSequenceNumber(uint32_t ulSourceAddress,uint16_t usSourcePort,uint32_t ulDestinationAddress,uint16_t usDestinationPort)252 extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
253 uint16_t usSourcePort,
254 uint32_t ulDestinationAddress,
255 uint16_t usDestinationPort )
256 {
257 ( void ) ulSourceAddress;
258 ( void ) usSourcePort;
259 ( void ) ulDestinationAddress;
260 ( void ) usDestinationPort;
261
262 return uxRand();
263 }
264
vApplicationGetTimerTaskMemory(StaticTask_t ** ppxTimerTaskTCBBuffer,StackType_t ** ppxTimerTaskStackBuffer,uint32_t * pulTimerTaskStackSize)265 void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
266 StackType_t ** ppxTimerTaskStackBuffer,
267 uint32_t * pulTimerTaskStackSize )
268 {
269 /* Provide a stub for this function. */
270 }
271
vApplicationMallocFailedHook(void)272 void vApplicationMallocFailedHook( void )
273 {
274 /* Provide a stub for this function. */
275 }
276
xNetworkInterfaceOutput(NetworkBufferDescriptor_t * const pxNetworkBuffer,BaseType_t bReleaseAfterSend)277 BaseType_t xNetworkInterfaceOutput( NetworkBufferDescriptor_t * const pxNetworkBuffer,
278 BaseType_t bReleaseAfterSend )
279 {
280 /* Provide a stub for this function. */
281 return pdTRUE;
282 }
283
xNetworkInterfaceInitialise(void)284 BaseType_t xNetworkInterfaceInitialise( void )
285 {
286 /* Provide a stub for this function. */
287 return pdTRUE;
288 }
289
290 #if ( ( ipconfigUSE_TCP == 1 ) && ( ipconfigUSE_DHCP_HOOK != 0 ) )
xApplicationDHCPHook(eDHCPCallbackPhase_t eDHCPPhase,uint32_t ulIPAddress)291 eDHCPCallbackAnswer_t xApplicationDHCPHook( eDHCPCallbackPhase_t eDHCPPhase,
292 uint32_t ulIPAddress )
293 {
294 /* Provide a stub for this function. */
295 return eDHCPContinue;
296 }
297 #endif
298
vApplicationPingReplyHook(ePingReplyStatus_t eStatus,uint16_t usIdentifier)299 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus,
300 uint16_t usIdentifier )
301 {
302 /* Provide a stub for this function. */
303 }
304