+++ /dev/null
-/*\r
- * FreeRTOS Kernel V10.2.1\r
- * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
- *\r
- * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
- * this software and associated documentation files (the "Software"), to deal in\r
- * the Software without restriction, including without limitation the rights to\r
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
- * the Software, and to permit persons to whom the Software is furnished to do so,\r
- * subject to the following conditions:\r
- *\r
- * The above copyright notice and this permission notice shall be included in all\r
- * copies or substantial portions of the Software.\r
- *\r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- *\r
- * http://www.FreeRTOS.org\r
- * http://aws.amazon.com/freertos\r
- *\r
- * 1 tab == 4 spaces!\r
- */\r
-\r
-/*\r
- * Logging utility that allows FreeRTOS tasks to log to a UDP port, stdout, and\r
- * disk file without making any Win32 system calls themselves.\r
- *\r
- * Messages logged to a UDP port are sent directly (using FreeRTOS+TCP), but as\r
- * FreeRTOS tasks cannot make Win32 system calls messages sent to stdout or a\r
- * disk file are sent via a stream buffer to a Win32 thread which then performs\r
- * the actual output.\r
- */\r
-\r
-/* Standard includes. */\r
-#include <stdio.h>\r
-#include <stdint.h>\r
-#include <stdarg.h>\r
-#include <io.h>\r
-#include <ctype.h>\r
-\r
-/* FreeRTOS includes. */\r
-#include <FreeRTOS.h>\r
-#include "task.h"\r
-\r
-/* FreeRTOS+TCP includes. */\r
-#include "FreeRTOS_IP.h"\r
-#include "FreeRTOS_Sockets.h"\r
-#include "FreeRTOS_Stream_Buffer.h"\r
-\r
-/* Demo includes. */\r
-#include "demo_logging.h"\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/* The maximum size to which the log file may grow, before being renamed\r
-to .ful. */\r
-#define dlLOGGING_FILE_SIZE ( 40ul * 1024ul * 1024ul )\r
-\r
-/* Dimensions the arrays into which print messages are created. */\r
-#define dlMAX_PRINT_STRING_LENGTH 255\r
-\r
-/* The size of the stream buffer used to pass messages from FreeRTOS tasks to\r
-the Win32 thread that is responsible for making any Win32 system calls that are\r
-necessary for the selected logging method. */\r
-#define dlLOGGING_STREAM_BUFFER_SIZE 32768\r
-\r
-/* A block time of zero simply means don't block. */\r
-#define dlDONT_BLOCK 0\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/*\r
- * Called from vLoggingInit() to start a new disk log file.\r
- */\r
-static void prvFileLoggingInit( void );\r
-\r
-/*\r
- * Attempt to write a message to the file.\r
- */\r
-static void prvLogToFile( const char *pcMessage, size_t xLength );\r
-\r
-/*\r
- * Simply close the logging file, if it is open.\r
- */\r
-static void prvFileClose( void );\r
-\r
-/*\r
- * Before the scheduler is started this function is called directly. After the\r
- * scheduler has started it is called from the Windows thread dedicated to\r
- * outputting log messages. Only the windows thread actually performs the\r
- * writing so as not to disrupt the simulation by making Windows system calls\r
- * from FreeRTOS tasks.\r
- */\r
-static void prvLoggingFlushBuffer( void );\r
-\r
-/*\r
- * The windows thread that performs the actual writing of messages that require\r
- * Win32 system calls. Only the windows thread can make system calls so as not\r
- * to disrupt the simulation by making Windows calls from FreeRTOS tasks.\r
- */\r
-static DWORD WINAPI prvWin32LoggingThread( void *pvParam );\r
-\r
-/*\r
- * Creates the socket to which UDP messages are sent. This function is not\r
- * called directly to prevent the print socket being created from within the IP\r
- * task - which could result in a deadlock. Instead the function call is\r
- * deferred to run in the RTOS daemon task - hence it prototype.\r
- */\r
-static void prvCreatePrintSocket( void *pvParameter1, uint32_t ulParameter2 );\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/* Windows event used to wake the Win32 thread which performs any logging that\r
-needs Win32 system calls. */\r
-static void *pvLoggingThreadEvent = NULL;\r
-\r
-/* Stores the selected logging targets passed in as parameters to the\r
-vLoggingInit() function. */\r
-BaseType_t xStdoutLoggingUsed = pdFALSE, xDiskFileLoggingUsed = pdFALSE, xUDPLoggingUsed = pdFALSE;\r
-\r
-/* Circular buffer used to pass messages from the FreeRTOS tasks to the Win32\r
-thread that is responsible for making Win32 calls (when stdout or a disk log is\r
-used). */\r
-static StreamBuffer_t *xLogStreamBuffer = NULL;\r
-\r
-/* Handle to the file used for logging. This is left open while there are\r
-messages waiting to be logged, then closed again in between logs. */\r
-static FILE *pxLoggingFileHandle = NULL;\r
-\r
-/* When true prints are performed directly. After start up xDirectPrint is set\r
-to pdFALSE - at which time prints that require Win32 system calls are done by\r
-the Win32 thread responsible for logging. */\r
-BaseType_t xDirectPrint = pdTRUE;\r
-\r
-/* File names for the in use and complete (full) log files. */\r
-static const char *pcLogFileName = "RTOSDemo.log";\r
-static const char *pcFullLogFileName = "RTOSDemo.ful";\r
-\r
-/* Keep the current file size in a variable, as an optimisation. */\r
-static size_t ulSizeOfLoggingFile = 0ul;\r
-\r
-/* The UDP socket and address on/to which print messages are sent. */\r
-Socket_t xPrintSocket = FREERTOS_INVALID_SOCKET;\r
-struct freertos_sockaddr xPrintUDPAddress;\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-void vLoggingInit( BaseType_t xLogToStdout, BaseType_t xLogToFile, BaseType_t xLogToUDP, uint32_t ulRemoteIPAddress, uint16_t usRemotePort )\r
-{\r
- /* Can only be called before the scheduler has started. */\r
- configASSERT( xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED );\r
-\r
- #if( ( ipconfigHAS_DEBUG_PRINTF == 1 ) || ( ipconfigHAS_PRINTF == 1 ) )\r
- {\r
- HANDLE Win32Thread;\r
-\r
- /* Record which output methods are to be used. */\r
- xStdoutLoggingUsed = xLogToStdout;\r
- xDiskFileLoggingUsed = xLogToFile;\r
- xUDPLoggingUsed = xLogToUDP;\r
-\r
- /* If a disk file is used then initialise it now. */\r
- if( xDiskFileLoggingUsed != pdFALSE )\r
- {\r
- prvFileLoggingInit();\r
- }\r
-\r
- /* If UDP logging is used then store the address to which the log data\r
- will be sent - but don't create the socket yet because the network is\r
- not initialised. */\r
- if( xUDPLoggingUsed != pdFALSE )\r
- {\r
- /* Set the address to which the print messages are sent. */\r
- xPrintUDPAddress.sin_port = FreeRTOS_htons( usRemotePort );\r
- xPrintUDPAddress.sin_addr = ulRemoteIPAddress;\r
- }\r
-\r
- /* If a disk file or stdout are to be used then Win32 system calls will\r
- have to be made. Such system calls cannot be made from FreeRTOS tasks\r
- so create a stream buffer to pass the messages to a Win32 thread, then\r
- create the thread itself, along with a Win32 event that can be used to\r
- unblock the thread. */\r
- if( ( xStdoutLoggingUsed != pdFALSE ) || ( xDiskFileLoggingUsed != pdFALSE ) )\r
- {\r
- /* Create the buffer. */\r
- xLogStreamBuffer = ( StreamBuffer_t * ) malloc( sizeof( *xLogStreamBuffer ) - sizeof( xLogStreamBuffer->ucArray ) + dlLOGGING_STREAM_BUFFER_SIZE + 1 );\r
- configASSERT( xLogStreamBuffer );\r
- memset( xLogStreamBuffer, '\0', sizeof( *xLogStreamBuffer ) - sizeof( xLogStreamBuffer->ucArray ) );\r
- xLogStreamBuffer->LENGTH = dlLOGGING_STREAM_BUFFER_SIZE + 1;\r
-\r
- /* Create the Windows event. */\r
- pvLoggingThreadEvent = CreateEvent( NULL, FALSE, TRUE, "StdoutLoggingEvent" );\r
-\r
- /* Create the thread itself. */\r
- Win32Thread = CreateThread(\r
- NULL, /* Pointer to thread security attributes. */\r
- 0, /* Initial thread stack size, in bytes. */\r
- prvWin32LoggingThread, /* Pointer to thread function. */\r
- NULL, /* Argument for new thread. */\r
- 0, /* Creation flags. */\r
- NULL );\r
-\r
- /* Use the cores that are not used by the FreeRTOS tasks. */\r
- SetThreadAffinityMask( Win32Thread, ~0x01u );\r
- SetThreadPriorityBoost( Win32Thread, TRUE );\r
- SetThreadPriority( Win32Thread, THREAD_PRIORITY_IDLE );\r
- }\r
- }\r
- #else\r
- {\r
- /* FreeRTOSIPConfig is set such that no print messages will be output.\r
- Avoid compiler warnings about unused parameters. */\r
- ( void ) xLogToStdout;\r
- ( void ) xLogToFile;\r
- ( void ) xLogToUDP;\r
- ( void ) usRemotePort;\r
- ( void ) ulRemoteIPAddress;\r
- }\r
- #endif /* ( ipconfigHAS_DEBUG_PRINTF == 1 ) || ( ipconfigHAS_PRINTF == 1 ) */\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvCreatePrintSocket( void *pvParameter1, uint32_t ulParameter2 )\r
-{\r
-static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 0 );\r
-Socket_t xSocket;\r
-\r
- /* The function prototype is that of a deferred function, but the parameters\r
- are not actually used. */\r
- ( void ) pvParameter1;\r
- ( void ) ulParameter2;\r
-\r
- xSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );\r
-\r
- if( xSocket != FREERTOS_INVALID_SOCKET )\r
- {\r
- /* FreeRTOS+TCP decides which port to bind to. */\r
- FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );\r
- FreeRTOS_bind( xSocket, NULL, 0 );\r
-\r
- /* Now the socket is bound it can be assigned to the print socket. */\r
- xPrintSocket = xSocket;\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vLoggingPrintf( const char *pcFormat, ... )\r
-{\r
-char cPrintString[ dlMAX_PRINT_STRING_LENGTH ];\r
-char cOutputString[ dlMAX_PRINT_STRING_LENGTH ];\r
-char *pcSource, *pcTarget, *pcBegin;\r
-size_t xLength, xLength2, rc;\r
-static BaseType_t xMessageNumber = 0;\r
-va_list args;\r
-uint32_t ulIPAddress;\r
-const char *pcTaskName;\r
-const char *pcNoTask = "None";\r
-int iOriginalPriority;\r
-HANDLE xCurrentTask;\r
-\r
-\r
- if( ( xStdoutLoggingUsed != pdFALSE ) || ( xDiskFileLoggingUsed != pdFALSE ) || ( xUDPLoggingUsed != pdFALSE ) )\r
- {\r
- /* There are a variable number of parameters. */\r
- va_start( args, pcFormat );\r
-\r
- /* Additional info to place at the start of the log. */\r
- if( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED )\r
- {\r
- pcTaskName = pcTaskGetName( NULL );\r
- }\r
- else\r
- {\r
- pcTaskName = pcNoTask;\r
- }\r
-\r
- if( strcmp( pcFormat, "\n" ) != 0 )\r
- {\r
- xLength = snprintf( cPrintString, dlMAX_PRINT_STRING_LENGTH, "%lu %lu [%s] ",\r
- xMessageNumber++,\r
- ( unsigned long ) xTaskGetTickCount(),\r
- pcTaskName );\r
- }\r
- else\r
- {\r
- xLength = 0;\r
- memset( cPrintString, 0x00, dlMAX_PRINT_STRING_LENGTH );\r
- }\r
-\r
- xLength2 = vsnprintf( cPrintString + xLength, dlMAX_PRINT_STRING_LENGTH - xLength, pcFormat, args );\r
-\r
- if( xLength2 < 0 )\r
- {\r
- /* Clean up. */\r
- xLength2 = dlMAX_PRINT_STRING_LENGTH - 1 - xLength;\r
- cPrintString[ dlMAX_PRINT_STRING_LENGTH - 1 ] = '\0';\r
- }\r
-\r
- xLength += xLength2;\r
- va_end( args );\r
-\r
- /* For ease of viewing, copy the string into another buffer, converting\r
- IP addresses to dot notation on the way. */\r
- pcSource = cPrintString;\r
- pcTarget = cOutputString;\r
-\r
- while( ( *pcSource ) != '\0' )\r
- {\r
- *pcTarget = *pcSource;\r
- pcTarget++;\r
- pcSource++;\r
-\r
- /* Look forward for an IP address denoted by 'ip'. */\r
- if( ( isxdigit( pcSource[ 0 ] ) != pdFALSE ) && ( pcSource[ 1 ] == 'i' ) && ( pcSource[ 2 ] == 'p' ) )\r
- {\r
- *pcTarget = *pcSource;\r
- pcTarget++;\r
- *pcTarget = '\0';\r
- pcBegin = pcTarget - 8;\r
-\r
- while( ( pcTarget > pcBegin ) && ( isxdigit( pcTarget[ -1 ] ) != pdFALSE ) )\r
- {\r
- pcTarget--;\r
- }\r
-\r
- sscanf( pcTarget, "%8X", &ulIPAddress );\r
- rc = sprintf( pcTarget, "%lu.%lu.%lu.%lu",\r
- ( unsigned long ) ( ulIPAddress >> 24UL ),\r
- ( unsigned long ) ( (ulIPAddress >> 16UL) & 0xffUL ),\r
- ( unsigned long ) ( (ulIPAddress >> 8UL) & 0xffUL ),\r
- ( unsigned long ) ( ulIPAddress & 0xffUL ) );\r
- pcTarget += rc;\r
- pcSource += 3; /* skip "<n>ip" */\r
- }\r
- }\r
-\r
- /* How far through the buffer was written? */\r
- xLength = ( BaseType_t ) ( pcTarget - cOutputString );\r
-\r
- /* If the message is to be logged to a UDP port then it can be sent directly\r
- because it only uses FreeRTOS function (not Win32 functions). */\r
- if( xUDPLoggingUsed != pdFALSE )\r
- {\r
- if( ( xPrintSocket == FREERTOS_INVALID_SOCKET ) && ( FreeRTOS_IsNetworkUp() != pdFALSE ) )\r
- {\r
- /* Create and bind the socket to which print messages are sent. The\r
- xTimerPendFunctionCall() function is used even though this is\r
- not an interrupt because this function is called from the IP task\r
- and the IP task cannot itself wait for a socket to bind. The\r
- parameters to prvCreatePrintSocket() are not required so set to\r
- NULL or 0. */\r
- xTimerPendFunctionCall( prvCreatePrintSocket, NULL, 0, dlDONT_BLOCK );\r
- }\r
-\r
- if( xPrintSocket != FREERTOS_INVALID_SOCKET )\r
- {\r
- FreeRTOS_sendto( xPrintSocket, cOutputString, xLength, 0, &xPrintUDPAddress, sizeof( xPrintUDPAddress ) );\r
-\r
- /* Just because the UDP data logger I'm using is dumb. */\r
- FreeRTOS_sendto( xPrintSocket, "\r", sizeof( char ), 0, &xPrintUDPAddress, sizeof( xPrintUDPAddress ) );\r
- }\r
- }\r
-\r
- /* If logging is also to go to either stdout or a disk file then it cannot\r
- be output here - so instead write the message to the stream buffer and wake\r
- the Win32 thread which will read it from the stream buffer and perform the\r
- actual output. */\r
- if( ( xStdoutLoggingUsed != pdFALSE ) || ( xDiskFileLoggingUsed != pdFALSE ) )\r
- {\r
- configASSERT( xLogStreamBuffer );\r
-\r
- /* How much space is in the buffer? */\r
- xLength2 = uxStreamBufferGetSpace( xLogStreamBuffer );\r
-\r
- /* There must be enough space to write both the string and the length of\r
- the string. */\r
- if( xLength2 >= ( xLength + sizeof( xLength ) ) )\r
- {\r
- /* First write in the length of the data, then write in the data\r
- itself. Raising the thread priority is used as a critical section\r
- as there are potentially multiple writers. The stream buffer is\r
- only thread safe when there is a single writer (likewise for\r
- reading from the buffer). */\r
- xCurrentTask = GetCurrentThread();\r
- iOriginalPriority = GetThreadPriority( xCurrentTask );\r
- SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL );\r
- uxStreamBufferAdd( xLogStreamBuffer, 0, ( const uint8_t * ) &( xLength ), sizeof( xLength ) );\r
- uxStreamBufferAdd( xLogStreamBuffer, 0, ( const uint8_t * ) cOutputString, xLength );\r
- SetThreadPriority( GetCurrentThread(), iOriginalPriority );\r
- }\r
-\r
- /* xDirectPrint is initialised to pdTRUE, and while it remains true the\r
- logging output function is called directly. When the system is running\r
- the output function cannot be called directly because it would get\r
- called from both FreeRTOS tasks and Win32 threads - so instead wake the\r
- Win32 thread responsible for the actual output. */\r
- if( xDirectPrint != pdFALSE )\r
- {\r
- /* While starting up, the thread which calls prvWin32LoggingThread()\r
- is not running yet and xDirectPrint will be pdTRUE. */\r
- prvLoggingFlushBuffer();\r
- }\r
- else if( pvLoggingThreadEvent != NULL )\r
- {\r
- /* While running, wake up prvWin32LoggingThread() to send the\r
- logging data. */\r
- SetEvent( pvLoggingThreadEvent );\r
- }\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvLoggingFlushBuffer( void )\r
-{\r
-size_t xLength;\r
-char cPrintString[ dlMAX_PRINT_STRING_LENGTH ];\r
-\r
- /* Is there more than the length value stored in the circular buffer\r
- used to pass data from the FreeRTOS simulator into this Win32 thread? */\r
- while( uxStreamBufferGetSize( xLogStreamBuffer ) > sizeof( xLength ) )\r
- {\r
- memset( cPrintString, 0x00, dlMAX_PRINT_STRING_LENGTH );\r
- uxStreamBufferGet( xLogStreamBuffer, 0, ( uint8_t * ) &xLength, sizeof( xLength ), pdFALSE );\r
- uxStreamBufferGet( xLogStreamBuffer, 0, ( uint8_t * ) cPrintString, xLength, pdFALSE );\r
-\r
- /* Write the message to standard out if requested to do so when\r
- vLoggingInit() was called, or if the network is not yet up. */\r
- if( ( xStdoutLoggingUsed != pdFALSE ) || ( FreeRTOS_IsNetworkUp() == pdFALSE ) )\r
- {\r
- /* Write the message to stdout. */\r
- printf( "%s", cPrintString ); /*_RB_ Replace with _write(). */\r
- }\r
-\r
- /* Write the message to a file if requested to do so when\r
- vLoggingInit() was called. */\r
- if( xDiskFileLoggingUsed != pdFALSE )\r
- {\r
- prvLogToFile( cPrintString, xLength );\r
- }\r
- }\r
-\r
- prvFileClose();\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static DWORD WINAPI prvWin32LoggingThread( void *pvParameter )\r
-{\r
-const DWORD xMaxWait = 1000;\r
-\r
- ( void ) pvParameter;\r
-\r
- /* From now on, prvLoggingFlushBuffer() will only be called from this\r
- Windows thread */\r
- xDirectPrint = pdFALSE;\r
-\r
- for( ;; )\r
- {\r
- /* Wait to be told there are message waiting to be logged. */\r
- WaitForSingleObject( pvLoggingThreadEvent, xMaxWait );\r
-\r
- /* Write out all waiting messages. */\r
- prvLoggingFlushBuffer();\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvFileLoggingInit( void )\r
-{\r
-FILE *pxHandle = fopen( pcLogFileName, "a" );\r
-\r
- if( pxHandle != NULL )\r
- {\r
- fseek( pxHandle, SEEK_END, 0ul );\r
- ulSizeOfLoggingFile = ftell( pxHandle );\r
- fclose( pxHandle );\r
- }\r
- else\r
- {\r
- ulSizeOfLoggingFile = 0ul;\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvFileClose( void )\r
-{\r
- if( pxLoggingFileHandle != NULL )\r
- {\r
- fclose( pxLoggingFileHandle );\r
- pxLoggingFileHandle = NULL;\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvLogToFile( const char *pcMessage, size_t xLength )\r
-{\r
- if( pxLoggingFileHandle == NULL )\r
- {\r
- pxLoggingFileHandle = fopen( pcLogFileName, "a" );\r
- }\r
-\r
- if( pxLoggingFileHandle != NULL )\r
- {\r
- fwrite( pcMessage, 1, xLength, pxLoggingFileHandle );\r
- ulSizeOfLoggingFile += xLength;\r
-\r
- /* If the file has grown to its maximum permissible size then close and\r
- rename it - then start with a new file. */\r
- if( ulSizeOfLoggingFile > ( size_t ) dlLOGGING_FILE_SIZE )\r
- {\r
- prvFileClose();\r
- if( _access( pcFullLogFileName, 00 ) == 0 )\r
- {\r
- remove( pcFullLogFileName );\r
- }\r
- rename( pcLogFileName, pcFullLogFileName );\r
- ulSizeOfLoggingFile = 0;\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r