+++ /dev/null
-/*\r
- * Amazon FreeRTOS Common V1.0.0\r
- * Copyright (C) 2018 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://aws.amazon.com/freertos\r
- * http://www.FreeRTOS.org\r
- */\r
-\r
-/* FreeRTOS includes. */\r
-#include "FreeRTOS.h"\r
-#include "task.h"\r
-#include "queue.h"\r
-\r
-/* Logging includes. */\r
-#include "iot_logging_task.h"\r
-\r
-/* Standard includes. */\r
-#include <stdio.h>\r
-#include <stdarg.h>\r
-#include <string.h>\r
-\r
-/* Sanity check all the definitions required by this file are set. */\r
-#ifndef configPRINT_STRING\r
- #error configPRINT_STRING( x ) must be defined in FreeRTOSConfig.h to use this logging file. Set configPRINT_STRING( x ) to a function that outputs a string, where X is the string. For example, #define configPRINT_STRING( x ) MyUARTWriteString( X )\r
-#endif\r
-\r
-#ifndef configLOGGING_MAX_MESSAGE_LENGTH\r
- #error configLOGGING_MAX_MESSAGE_LENGTH must be defined in FreeRTOSConfig.h to use this logging file. configLOGGING_MAX_MESSAGE_LENGTH sets the size of the buffer into which formatted text is written, so also sets the maximum log message length.\r
-#endif\r
-\r
-#ifndef configLOGGING_INCLUDE_TIME_AND_TASK_NAME\r
- #error configLOGGING_INCLUDE_TIME_AND_TASK_NAME must be defined in FreeRTOSConfig.h to use this logging file. Set configLOGGING_INCLUDE_TIME_AND_TASK_NAME to 1 to prepend a time stamp, message number and the name of the calling task to each logged message. Otherwise set to 0.\r
-#endif\r
-\r
-/* A block time of 0 just means don't block. */\r
-#define loggingDONT_BLOCK 0\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/*\r
- * The task that actually performs the print output. Using a separate task\r
- * enables the use of slow output, such as as a UART, without the task that is\r
- * outputting the log message having to wait for the message to be completely\r
- * written. Using a separate task also serializes access to the output port.\r
- *\r
- * The structure of this task is very simple; it blocks on a queue to wait for\r
- * a pointer to a string, sending any received strings to a macro that performs\r
- * the actual output. The macro is port specific, so implemented outside of\r
- * this file. This version uses dynamic memory, so the buffer that contained\r
- * the log message is freed after it has been output.\r
- */\r
-static void prvLoggingTask( void * pvParameters );\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-/*\r
- * The queue used to pass pointers to log messages from the task that created\r
- * the message to the task that will performs the output.\r
- */\r
-static QueueHandle_t xQueue = NULL;\r
-\r
-/*-----------------------------------------------------------*/\r
-\r
-BaseType_t xLoggingTaskInitialize( uint16_t usStackSize,\r
- UBaseType_t uxPriority,\r
- UBaseType_t uxQueueLength )\r
-{\r
- BaseType_t xReturn = pdFAIL;\r
-\r
- /* Ensure the logging task has not been created already. */\r
- if( xQueue == NULL )\r
- {\r
- /* Create the queue used to pass pointers to strings to the logging task. */\r
- xQueue = xQueueCreate( uxQueueLength, sizeof( char ** ) );\r
-\r
- if( xQueue != NULL )\r
- {\r
- if( xTaskCreate( prvLoggingTask, "Logging", usStackSize, NULL, uxPriority, NULL ) == pdPASS )\r
- {\r
- xReturn = pdPASS;\r
- }\r
- else\r
- {\r
- /* Could not create the task, so delete the queue again. */\r
- vQueueDelete( xQueue );\r
- }\r
- }\r
- }\r
-\r
- return xReturn;\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-static void prvLoggingTask( void * pvParameters )\r
-{\r
- char * pcReceivedString = NULL;\r
-\r
- for( ; ; )\r
- {\r
- /* Block to wait for the next string to print. */\r
- if( xQueueReceive( xQueue, &pcReceivedString, portMAX_DELAY ) == pdPASS )\r
- {\r
- configPRINT_STRING( pcReceivedString );\r
- vPortFree( ( void * ) pcReceivedString );\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-/*!\r
- * \brief Formats a string to be printed and sends it\r
- * to the print queue.\r
- *\r
- * Appends the message number, time (in ticks), and task\r
- * that called vLoggingPrintf to the beginning of each\r
- * print statement.\r
- *\r
- */\r
-void vLoggingPrintf( const char * pcFormat,\r
- ... )\r
-{\r
- size_t xLength = 0;\r
- int32_t xLength2 = 0;\r
- va_list args;\r
- char * pcPrintString = NULL;\r
-\r
- /* The queue is created by xLoggingTaskInitialize(). Check\r
- * xLoggingTaskInitialize() has been called. */\r
- configASSERT( xQueue );\r
-\r
- /* Allocate a buffer to hold the log message. */\r
- pcPrintString = pvPortMalloc( configLOGGING_MAX_MESSAGE_LENGTH );\r
-\r
- if( pcPrintString != NULL )\r
- {\r
- /* There are a variable number of parameters. */\r
- va_start( args, pcFormat );\r
-\r
- if( strcmp( pcFormat, "\n" ) != 0 )\r
- {\r
- #if ( configLOGGING_INCLUDE_TIME_AND_TASK_NAME == 1 )\r
- {\r
- const char * pcTaskName;\r
- const char * pcNoTask = "None";\r
- static BaseType_t xMessageNumber = 0;\r
-\r
- /* Add a time stamp and the name of the calling task to the\r
- * 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
- xLength = snprintf( pcPrintString, configLOGGING_MAX_MESSAGE_LENGTH, "%lu %lu [%s] ",\r
- ( unsigned long ) xMessageNumber++,\r
- ( unsigned long ) xTaskGetTickCount(),\r
- pcTaskName );\r
- }\r
- #else /* if ( configLOGGING_INCLUDE_TIME_AND_TASK_NAME == 1 ) */\r
- {\r
- xLength = 0;\r
- }\r
- #endif /* if ( configLOGGING_INCLUDE_TIME_AND_TASK_NAME == 1 ) */\r
- }\r
-\r
- xLength2 = vsnprintf( pcPrintString + xLength, configLOGGING_MAX_MESSAGE_LENGTH - xLength, pcFormat, args );\r
-\r
- if( xLength2 < 0 )\r
- {\r
- /* vsnprintf() failed. Restore the terminating NULL\r
- * character of the first part. Note that the first\r
- * part of the buffer may be empty if the value of\r
- * configLOGGING_INCLUDE_TIME_AND_TASK_NAME is not\r
- * 1 and as a result, the whole buffer may be empty.\r
- * That's the reason we have a check for xLength > 0\r
- * before sending the buffer to the logging task.\r
- */\r
- xLength2 = 0;\r
- pcPrintString[ xLength ] = '\0';\r
- }\r
-\r
- xLength += ( size_t ) xLength2;\r
- va_end( args );\r
-\r
- /* Only send the buffer to the logging task if it is\r
- * not empty. */\r
- if( xLength > 0 )\r
- {\r
- /* Send the string to the logging task for IO. */\r
- if( xQueueSend( xQueue, &pcPrintString, loggingDONT_BLOCK ) != pdPASS )\r
- {\r
- /* The buffer was not sent so must be freed again. */\r
- vPortFree( ( void * ) pcPrintString );\r
- }\r
- }\r
- else\r
- {\r
- /* The buffer was not sent, so it must be\r
- * freed. */\r
- vPortFree( ( void * ) pcPrintString );\r
- }\r
- }\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
-void vLoggingPrint( const char * pcMessage )\r
-{\r
- char * pcPrintString = NULL;\r
- size_t xLength = 0;\r
-\r
- /* The queue is created by xLoggingTaskInitialize(). Check\r
- * xLoggingTaskInitialize() has been called. */\r
- configASSERT( xQueue );\r
-\r
- xLength = strlen( pcMessage ) + 1;\r
- pcPrintString = pvPortMalloc( xLength );\r
-\r
- if( pcPrintString != NULL )\r
- {\r
- strncpy( pcPrintString, pcMessage, xLength );\r
-\r
- /* Send the string to the logging task for IO. */\r
- if( xQueueSend( xQueue, &pcPrintString, loggingDONT_BLOCK ) != pdPASS )\r
- {\r
- /* The buffer was not sent so must be freed again. */\r
- vPortFree( ( void * ) pcPrintString );\r
- }\r
- }\r
-}\r