1 /*
2 * FreeRTOS V202212.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
24 *
25 */
26
27 /**
28 * @file main.c
29 * @brief Implements the main function.
30 */
31
32 /* FreeRTOS include. */
33 #include <FreeRTOS.h>
34 #include "task.h"
35
36 /* Standard includes. */
37 #include <signal.h>
38 #include <conio.h>
39 #include <setjmp.h>
40 #include <time.h>
41 #include <windows.h>
42
43 /* Test runner includes. */
44 #include "test_runner.h"
45
46 /* System application includes. */
47 #include "FreeRTOS_IP.h"
48 #include "FreeRTOS_Sockets.h"
49 #include "FreeRTOS_DHCP.h"
50 #include "logging.h"
51 #include "errhandlingapi.h"
52 /*#include "iot_system_init.h" */
53
54 /*#include "aws_dev_mode_key_provisioning.h" */
55
56 /* Unity includes. */
57 #include "unity.h"
58
59 /* Define a name that will be used for LLMNR and NBNS searches. Once running,
60 * you can "ping RTOSDemo" instead of pinging the IP address, which is useful when
61 * using DHCP. */
62 #define mainHOST_NAME "TestRunner"
63 #define mainDEVICE_NICK_NAME "windows_TestRunner"
64
65
66 #define TEST_RUNNER_TASK_STACK_SIZE 10000
67 #define FIRST_EXCEPTION_HANDLER 1
68 /* Windows-NT VectoredHandler callback function. */
69 static LONG CALLBACK prvExceptionHandler( _In_ PEXCEPTION_POINTERS ExceptionInfo );
70 jmp_buf xMark; /* Address for long jump to jump to. */
71
72 /*-----------------------------------------------------------*/
73
74 /* Notes if the trace is running or not. */
75 static BaseType_t xTraceRunning = pdTRUE;
76
77 /* Default MAC address configuration. The demo creates a virtual network
78 * connection that uses this MAC address by accessing the raw Ethernet data
79 * to and from a real network connection on the host PC. See the
80 * configNETWORK_INTERFACE_TO_USE definition for information on how to configure
81 * the real network connection to use. */
82 const uint8_t ucMACAddress[ 6 ] =
83 {
84 configMAC_ADDR0,
85 configMAC_ADDR1,
86 configMAC_ADDR2,
87 configMAC_ADDR3,
88 configMAC_ADDR4,
89 configMAC_ADDR5
90 };
91
92 /* The default IP and MAC address used by the demo. The address configuration
93 * defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is
94 * 1 but a DHCP server could not be contacted. See the online documentation for
95 * more information. In both cases the node can be discovered using
96 * "ping RTOSDemo". */
97 static const uint8_t ucIPAddress[ 4 ] =
98 {
99 configIP_ADDR0,
100 configIP_ADDR1,
101 configIP_ADDR2,
102 configIP_ADDR3
103 };
104 static const uint8_t ucNetMask[ 4 ] =
105 {
106 configNET_MASK0,
107 configNET_MASK1,
108 configNET_MASK2,
109 configNET_MASK3
110 };
111 static const uint8_t ucGatewayAddress[ 4 ] =
112 {
113 configGATEWAY_ADDR0,
114 configGATEWAY_ADDR1,
115 configGATEWAY_ADDR2,
116 configGATEWAY_ADDR3
117 };
118 static const uint8_t ucDNSServerAddress[ 4 ] =
119 {
120 configDNS_SERVER_ADDR0,
121 configDNS_SERVER_ADDR1,
122 configDNS_SERVER_ADDR2,
123 configDNS_SERVER_ADDR3
124 };
125
126 /* Use by the pseudo random number generator. */
127 static UBaseType_t ulNextRand;
128
129 /*-----------------------------------------------------------*/
main(void)130 int main( void )
131 {
132 /* Register the Windows VEH for exceptions. */
133 /*AddVectoredExceptionHandler(FIRST_EXCEPTION_HANDLER, prvExceptionHandler); */
134
135 /* Initialize logging for libraries that depend on it. */
136 vLoggingInit(
137 pdTRUE,
138 pdFALSE,
139 pdFALSE,
140 0,
141 0 );
142
143 /* Initialize the network interface.
144 *
145 ***NOTE*** Tasks that use the network are created in the network event hook
146 * when the network is connected and ready for use (see the definition of
147 * vApplicationIPNetworkEventHook() below). The address values passed in here
148 * are used if ipconfigUSE_DHCP is set to 0, or if ipconfigUSE_DHCP is set to 1
149 * but a DHCP server cannot be contacted. */
150 FreeRTOS_printf( ( "FreeRTOS_IPInit\n" ) );
151 FreeRTOS_IPInit(
152 ucIPAddress,
153 ucNetMask,
154 ucGatewayAddress,
155 ucDNSServerAddress,
156 ucMACAddress );
157
158 vTaskStartScheduler();
159
160 return 0;
161 }
162 /*-----------------------------------------------------------*/
163
164 /* *INDENT-OFF* */
165 #if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 )
vApplicationIPNetworkEventHook_Multi(eIPCallbackEvent_t eNetworkEvent,struct xNetworkEndPoint * pxEndPoint)166 void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent,
167 struct xNetworkEndPoint * pxEndPoint )
168 #else
169 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
170 #endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */
171 /* *INDENT-ON* */
172 {
173 static BaseType_t xTasksAlreadyCreated = pdFALSE;
174
175 /* If the network has just come up...*/
176 if( ( eNetworkEvent == eNetworkUp ) && ( xTasksAlreadyCreated == pdFALSE ) )
177 {
178 xTaskCreate( TEST_RUNNER_RunTests_task,
179 "TestRunner",
180 TEST_RUNNER_TASK_STACK_SIZE,
181 NULL,
182 tskIDLE_PRIORITY, NULL );
183
184 xTasksAlreadyCreated = pdTRUE;
185 }
186 }
187
188 /*-----------------------------------------------------------*/
189
prvExceptionHandler(_In_ PEXCEPTION_POINTERS ExceptionInfo)190 static LONG CALLBACK prvExceptionHandler( _In_ PEXCEPTION_POINTERS ExceptionInfo )
191 {
192 /* If this function is called during a test, the test immediately fails. */
193 TEST_FAIL();
194
195 return EXCEPTION_CONTINUE_EXECUTION;
196 }
197
198 /*-----------------------------------------------------------*/
199
200 #if ( ( ipconfigUSE_LLMNR != 0 ) || \
201 ( ipconfigUSE_NBNS != 0 ) || \
202 ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) )
203
pcApplicationHostnameHook(void)204 const char * pcApplicationHostnameHook( void )
205 {
206 /* This function will be called during the DHCP: the machine will be registered
207 * with an IP address plus this name. */
208 return mainHOST_NAME;
209 }
210
211 #endif /* if ( ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) ) */
212 /*-----------------------------------------------------------*/
213
214 #if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
215
216 #if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 )
xApplicationDNSQueryHook_Multi(struct xNetworkEndPoint * pxEndPoint,const char * pcName)217 BaseType_t xApplicationDNSQueryHook_Multi( struct xNetworkEndPoint * pxEndPoint,
218 const char * pcName )
219 #else
220 BaseType_t xApplicationDNSQueryHook( const char * pcName )
221 #endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */
222 {
223 BaseType_t xReturn;
224
225 /* Determine if a name lookup is for this node. Two names are given
226 * to this node: that returned by pcApplicationHostnameHook() and that set
227 * by mainDEVICE_NICK_NAME. */
228 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
229 {
230 xReturn = pdPASS;
231 }
232 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
233 {
234 xReturn = pdPASS;
235 }
236 else
237 {
238 xReturn = pdFAIL;
239 }
240
241 return xReturn;
242 }
243
244 #endif /* if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) */
245 /*-----------------------------------------------------------*/
246
vApplicationIdleHook(void)247 void vApplicationIdleHook( void )
248 {
249 const uint32_t ulMSToSleep = 1;
250 const TickType_t xKitHitCheckPeriod = pdMS_TO_TICKS( 1000UL );
251 static TickType_t xTimeNow, xLastTimeCheck = 0;
252
253 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
254 * to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
255 * task. It is essential that code added to this hook function never attempts
256 * to block in any way (for example, call xQueueReceive() with a block time
257 * specified, or call vTaskDelay()). If application tasks make use of the
258 * vTaskDelete() API function to delete themselves then it is also important
259 * that vApplicationIdleHook() is permitted to return to its calling function,
260 * because it is the responsibility of the idle task to clean up memory
261 * allocated by the kernel to any task that has since deleted itself. */
262
263 /* _kbhit() is a Windows system function, and system functions can cause
264 * crashes if they somehow block the FreeRTOS thread. The call to _kbhit()
265 * can be removed if it causes problems. Limiting the frequency of calls to
266 * _kbhit() should minimize the potential for issues. */
267 xTimeNow = xTaskGetTickCount();
268
269 if( ( xTimeNow - xLastTimeCheck ) > xKitHitCheckPeriod )
270 {
271 /* Uncomment the print line to get confirmation that tests are still
272 * running if you suspect a previous run resulted in a crash. */
273 /* configPRINTF( ( "Running...\n" ) ); /**/
274 xLastTimeCheck = xTimeNow;
275 }
276
277 /* This is just a trivial example of an idle hook. It is called on each
278 * cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in
279 * FreeRTOSConfig.h. It must *NOT* attempt to block. In this case the
280 * idle task just sleeps to lower the CPU usage. */
281 Sleep( ulMSToSleep );
282 }
283 /*-----------------------------------------------------------*/
284
vAssertCalled(const char * pcFile,uint32_t ulLine)285 void vAssertCalled( const char * pcFile,
286 uint32_t ulLine )
287 {
288 const uint32_t ulLongSleep = 1000UL;
289 volatile uint32_t ulBlockVariable = 0UL;
290 volatile char * pcFileName = ( volatile char * ) pcFile;
291 volatile uint32_t ulLineNumber = ulLine;
292
293 ( void ) pcFileName;
294 ( void ) ulLineNumber;
295
296 printf( "vAssertCalled %s, %ld\n", pcFile, ( long ) ulLine );
297 fflush( stdout );
298
299 /* Setting ulBlockVariable to a non-zero value in the debugger will allow
300 * this function to be exited. */
301 taskDISABLE_INTERRUPTS();
302 {
303 while( ulBlockVariable == 0UL )
304 {
305 Sleep( ulLongSleep );
306 }
307 }
308 taskENABLE_INTERRUPTS();
309 }
310 /*-----------------------------------------------------------*/
311
getUserCmd(char * pucUserCmd)312 void getUserCmd( char * pucUserCmd )
313 {
314 char cTmp;
315
316 scanf( "%c%c", pucUserCmd, &cTmp );
317 }
318 /*-----------------------------------------------------------*/
319
uxRand(void)320 UBaseType_t uxRand( void )
321 {
322 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
323
324 /* Utility function to generate a pseudo random number. */
325
326 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
327 return( ( int ) ( ulNextRand >> 16UL ) & 0x7fffUL );
328 }
329
xApplicationGetRandomNumber()330 BaseType_t xApplicationGetRandomNumber()
331 {
332 return uxRand();
333 }
334
335 /*
336 * Callback that provides the inputs necessary to generate a randomized TCP
337 * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION
338 * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION
339 * SYSTEMS.
340 */
ulApplicationGetNextSequenceNumber(uint32_t ulSourceAddress,uint16_t usSourcePort,uint32_t ulDestinationAddress,uint16_t usDestinationPort)341 extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
342 uint16_t usSourcePort,
343 uint32_t ulDestinationAddress,
344 uint16_t usDestinationPort )
345 {
346 ( void ) ulSourceAddress;
347 ( void ) usSourcePort;
348 ( void ) ulDestinationAddress;
349 ( void ) usDestinationPort;
350
351 return uxRand();
352 }
353