]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/stream_buffer.c
Fix regressions introduced by introduction of configMESSAGE_BUFFER_LENGTH_TYPE consta...
[freertos] / FreeRTOS / Source / stream_buffer.c
index 598e2b44f878a6c44fd93288c280d22f9e9e5e68..1f053b7dde64fe83867b07d2cf0bfe15719ba038 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
- * FreeRTOS Kernel V10.0.0\r
+ * FreeRTOS Kernel V10.0.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
@@ -10,8 +10,7 @@
  * 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. If you wish to use our Amazon\r
- * FreeRTOS name, please do so in a fair use way that does not cause confusion.\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
@@ -40,6 +39,10 @@ task.h is included from an application file. */
 #include "task.h"\r
 #include "stream_buffer.h"\r
 \r
+#if( configUSE_TASK_NOTIFICATIONS != 1 )\r
+       #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c\r
+#endif\r
+\r
 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
 header files above, but not in this file, in order to generate the correct\r
@@ -126,7 +129,7 @@ that uses task notifications. */
 /*lint -restore (9026) */\r
 \r
 /* The number of bytes used to hold the length of a message in the buffer. */\r
-#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( size_t ) )\r
+#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )\r
 \r
 /* Bits stored in the ucFlags field of the stream buffer. */\r
 #define sbFLAGS_IS_MESSAGE_BUFFER              ( ( uint8_t ) 1 ) /* Set if the stream buffer was created as a message buffer, in which case it holds discrete messages rather than a stream. */\r
@@ -501,6 +504,9 @@ TimeOut_t xTimeOut;
        if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )\r
        {\r
                xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;\r
+\r
+               /* Overflow? */\r
+               configASSERT( xRequiredSpace > xDataLengthBytes );\r
        }\r
        else\r
        {\r
@@ -537,7 +543,7 @@ TimeOut_t xTimeOut;
                        taskEXIT_CRITICAL();\r
 \r
                        traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );\r
-                       ( void ) xTaskNotifyWait( ( uint32_t ) 0, UINT32_MAX, NULL, xTicksToWait );\r
+                       ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );\r
                        pxStreamBuffer->xTaskWaitingToSend = NULL;\r
 \r
                } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );\r
@@ -743,7 +749,7 @@ size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
                {\r
                        /* Wait for data to be available. */\r
                        traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );\r
-                       ( void ) xTaskNotifyWait( ( uint32_t ) 0, UINT32_MAX, NULL, xTicksToWait );\r
+                       ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );\r
                        pxStreamBuffer->xTaskWaitingToReceive = NULL;\r
 \r
                        /* Recheck the data available after blocking. */\r
@@ -789,6 +795,50 @@ size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )\r
+{\r
+StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) xStreamBuffer; /*lint !e9087 !e9079 Safe cast as StreamBufferHandle_t is opaque Streambuffer_t. */\r
+size_t xReturn, xBytesAvailable, xOriginalTail;\r
+configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;\r
+\r
+       configASSERT( pxStreamBuffer );\r
+\r
+       /* Ensure the stream buffer is being used as a message buffer. */\r
+       if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )\r
+       {\r
+               xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );\r
+               if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )\r
+               {\r
+                       /* The number of bytes available is greater than the number of bytes\r
+                       required to hold the length of the next message, so another message\r
+                       is available.  Return its length without removing the length bytes\r
+                       from the buffer.  A copy of the tail is stored so the buffer can be\r
+                       returned to its prior state as the message is not actually being\r
+                       removed from the buffer. */\r
+                       xOriginalTail = pxStreamBuffer->xTail;\r
+                       ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );\r
+                       xReturn = ( size_t ) xTempReturn;\r
+                       pxStreamBuffer->xTail = xOriginalTail;\r
+               }\r
+               else\r
+               {\r
+                       /* The minimum amount of bytes in a message buffer is\r
+                       ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is\r
+                       less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid\r
+                       value is 0. */\r
+                       configASSERT( xBytesAvailable == 0 );\r
+                       xReturn = 0;\r
+               }\r
+       }\r
+       else\r
+       {\r
+               xReturn = 0;\r
+       }\r
+\r
+       return xReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,\r
                                                                        void *pvRxData,\r
                                                                        size_t xBufferLengthBytes,\r
@@ -853,6 +903,7 @@ static size_t prvReadMessageFromBuffer( StreamBuffer_t *pxStreamBuffer,
                                                                                size_t xBytesToStoreMessageLength )\r
 {\r
 size_t xOriginalTail, xReceivedLength, xNextMessageLength;\r
+configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;\r
 \r
        if( xBytesToStoreMessageLength != ( size_t ) 0 )\r
        {\r
@@ -861,7 +912,8 @@ size_t xOriginalTail, xReceivedLength, xNextMessageLength;
                returned to its prior state if the length of the message is too\r
                large for the provided buffer. */\r
                xOriginalTail = pxStreamBuffer->xTail;\r
-               ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );\r
+               ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );\r
+               xNextMessageLength = ( size_t ) xTempNextMessageLength;\r
 \r
                /* Reduce the number of bytes available by the number of bytes just\r
                read out. */\r
@@ -1189,7 +1241,7 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
 \r
        uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )\r
        {\r
-               return ( ( StreamBuffer_t * )xStreamBuffer )->ucFlags | sbFLAGS_IS_MESSAGE_BUFFER;\r
+               return ( ( StreamBuffer_t * )xStreamBuffer )->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER;\r
        }\r
 \r
 #endif /* configUSE_TRACE_FACILITY */\r