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 /* Remove warning about unused parameter */
193 ( void ) ExceptionInfo;
194 /* If this function is called during a test, the test immediately fails. */
195 TEST_FAIL();
196
197 return EXCEPTION_CONTINUE_EXECUTION;
198 }
199
200 /*-----------------------------------------------------------*/
201
202 #if ( ( ipconfigUSE_LLMNR != 0 ) || \
203 ( ipconfigUSE_NBNS != 0 ) || \
204 ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) )
205
pcApplicationHostnameHook(void)206 const char * pcApplicationHostnameHook( void )
207 {
208 /* This function will be called during the DHCP: the machine will be registered
209 * with an IP address plus this name. */
210 return mainHOST_NAME;
211 }
212
213 #endif /* if ( ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) || ( ipconfigDHCP_REGISTER_HOSTNAME == 1 ) ) */
214 /*-----------------------------------------------------------*/
215
216 #if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 )
217
218 #if defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 )
xApplicationDNSQueryHook_Multi(struct xNetworkEndPoint * pxEndPoint,const char * pcName)219 BaseType_t xApplicationDNSQueryHook_Multi( struct xNetworkEndPoint * pxEndPoint,
220 const char * pcName )
221 #else
222 BaseType_t xApplicationDNSQueryHook( const char * pcName )
223 #endif /* defined( ipconfigIPv4_BACKWARD_COMPATIBLE ) && ( ipconfigIPv4_BACKWARD_COMPATIBLE == 0 ) */
224 {
225 BaseType_t xReturn;
226
227 /* Determine if a name lookup is for this node. Two names are given
228 * to this node: that returned by pcApplicationHostnameHook() and that set
229 * by mainDEVICE_NICK_NAME. */
230 if( _stricmp( pcName, pcApplicationHostnameHook() ) == 0 )
231 {
232 xReturn = pdPASS;
233 }
234 else if( _stricmp( pcName, mainDEVICE_NICK_NAME ) == 0 )
235 {
236 xReturn = pdPASS;
237 }
238 else
239 {
240 xReturn = pdFAIL;
241 }
242
243 return xReturn;
244 }
245
246 #endif /* if ( ipconfigUSE_LLMNR != 0 ) || ( ipconfigUSE_NBNS != 0 ) */
247 /*-----------------------------------------------------------*/
248
vApplicationIdleHook(void)249 void vApplicationIdleHook( void )
250 {
251 const uint32_t ulMSToSleep = 1;
252 const TickType_t xKitHitCheckPeriod = pdMS_TO_TICKS( 1000UL );
253 static TickType_t xTimeNow, xLastTimeCheck = 0;
254
255 /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
256 * to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
257 * task. It is essential that code added to this hook function never attempts
258 * to block in any way (for example, call xQueueReceive() with a block time
259 * specified, or call vTaskDelay()). If application tasks make use of the
260 * vTaskDelete() API function to delete themselves then it is also important
261 * that vApplicationIdleHook() is permitted to return to its calling function,
262 * because it is the responsibility of the idle task to clean up memory
263 * allocated by the kernel to any task that has since deleted itself. */
264
265 /* _kbhit() is a Windows system function, and system functions can cause
266 * crashes if they somehow block the FreeRTOS thread. The call to _kbhit()
267 * can be removed if it causes problems. Limiting the frequency of calls to
268 * _kbhit() should minimize the potential for issues. */
269 xTimeNow = xTaskGetTickCount();
270
271 if( ( xTimeNow - xLastTimeCheck ) > xKitHitCheckPeriod )
272 {
273 /* Uncomment the print line to get confirmation that tests are still
274 * running if you suspect a previous run resulted in a crash. */
275 /* configPRINTF( ( "Running...\n" ) ); /**/
276 xLastTimeCheck = xTimeNow;
277 }
278
279 /* This is just a trivial example of an idle hook. It is called on each
280 * cycle of the idle task if configUSE_IDLE_HOOK is set to 1 in
281 * FreeRTOSConfig.h. It must *NOT* attempt to block. In this case the
282 * idle task just sleeps to lower the CPU usage. */
283 Sleep( ulMSToSleep );
284 }
285 /*-----------------------------------------------------------*/
286
vAssertCalled(const char * pcFile,uint32_t ulLine)287 void vAssertCalled( const char * pcFile,
288 uint32_t ulLine )
289 {
290 const uint32_t ulLongSleep = 1000UL;
291 volatile uint32_t ulBlockVariable = 0UL;
292 volatile char * pcFileName = ( volatile char * ) pcFile;
293 volatile uint32_t ulLineNumber = ulLine;
294
295 ( void ) pcFileName;
296 ( void ) ulLineNumber;
297
298 printf( "vAssertCalled %s, %ld\n", pcFile, ( long ) ulLine );
299 fflush( stdout );
300
301 /* Setting ulBlockVariable to a non-zero value in the debugger will allow
302 * this function to be exited. */
303 taskDISABLE_INTERRUPTS();
304 {
305 while( ulBlockVariable == 0UL )
306 {
307 Sleep( ulLongSleep );
308 }
309 }
310 taskENABLE_INTERRUPTS();
311 }
312 /*-----------------------------------------------------------*/
313
getUserCmd(char * pucUserCmd)314 void getUserCmd( char * pucUserCmd )
315 {
316 char cTmp;
317
318 scanf( "%c%c", pucUserCmd, &cTmp );
319 }
320 /*-----------------------------------------------------------*/
321
uxRand(void)322 UBaseType_t uxRand( void )
323 {
324 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
325
326 /* Utility function to generate a pseudo random number. */
327
328 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
329 return( ( int ) ( ulNextRand ) & 0x7fffUL );
330 }
331
xApplicationGetRandomNumber(uint32_t * pulNumber)332 BaseType_t xApplicationGetRandomNumber( uint32_t * pulNumber )
333 {
334 *pulNumber = uxRand();
335
336 return pdTRUE;
337 }
338
339 /*
340 * Callback that provides the inputs necessary to generate a randomized TCP
341 * Initial Sequence Number per RFC 6528. THIS IS ONLY A DUMMY IMPLEMENTATION
342 * THAT RETURNS A PSEUDO RANDOM NUMBER SO IS NOT INTENDED FOR USE IN PRODUCTION
343 * SYSTEMS.
344 */
ulApplicationGetNextSequenceNumber(uint32_t ulSourceAddress,uint16_t usSourcePort,uint32_t ulDestinationAddress,uint16_t usDestinationPort)345 extern uint32_t ulApplicationGetNextSequenceNumber( uint32_t ulSourceAddress,
346 uint16_t usSourcePort,
347 uint32_t ulDestinationAddress,
348 uint16_t usDestinationPort )
349 {
350 ( void ) ulSourceAddress;
351 ( void ) usSourcePort;
352 ( void ) ulDestinationAddress;
353 ( void ) usDestinationPort;
354
355 return uxRand();
356 }
357