2 * FreeRTOS Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
27 /* Standard includes. */
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
32 * all the API functions to use the MPU wrappers. That should only be done when
33 * task.h is included from an application file. */
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
36 /* FreeRTOS includes. */
39 #include "stream_buffer.h"
41 #if ( configUSE_TASK_NOTIFICATIONS != 1 )
42 #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
45 /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified
46 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
47 * for the header files above, but not in this file, in order to generate the
48 * correct privileged Vs unprivileged linkage and placement. */
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
51 /* If the user has not provided application specific Rx notification macros,
52 * or #defined the notification macros away, then provide default implementations
53 * that uses task notifications. */
54 /*lint -save -e9026 Function like macros allowed and needed here so they can be overridden. */
55 #ifndef sbRECEIVE_COMPLETED
56 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
59 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
61 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
64 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
67 ( void ) xTaskResumeAll();
68 #endif /* sbRECEIVE_COMPLETED */
70 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
71 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
72 pxHigherPriorityTaskWoken ) \
74 UBaseType_t uxSavedInterruptStatus; \
76 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
78 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
80 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
83 pxHigherPriorityTaskWoken ); \
84 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
87 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
89 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
91 /* If the user has not provided an application specific Tx notification macro,
92 * or #defined the notification macro away, them provide a default implementation
93 * that uses task notifications. */
94 #ifndef sbSEND_COMPLETED
95 #define sbSEND_COMPLETED( pxStreamBuffer ) \
98 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
100 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
103 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
106 ( void ) xTaskResumeAll();
107 #endif /* sbSEND_COMPLETED */
109 #ifndef sbSEND_COMPLETE_FROM_ISR
110 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
112 UBaseType_t uxSavedInterruptStatus; \
114 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
116 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
118 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
121 pxHigherPriorityTaskWoken ); \
122 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
125 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
127 #endif /* sbSEND_COMPLETE_FROM_ISR */
128 /*lint -restore (9026) */
130 /* The number of bytes used to hold the length of a message in the buffer. */
131 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
133 /* Bits stored in the ucFlags field of the stream buffer. */
134 #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. */
135 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
137 /*-----------------------------------------------------------*/
139 /* Structure that hold state information on the buffer. */
140 typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
142 volatile size_t xTail; /* Index to the next item to read within the buffer. */
143 volatile size_t xHead; /* Index to the next item to write within the buffer. */
144 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
145 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
146 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
147 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
148 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
151 #if ( configUSE_TRACE_FACILITY == 1 )
152 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
157 * The number of bytes available to be read from the buffer.
159 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
162 * Add xCount bytes from pucData into the pxStreamBuffer message buffer.
163 * Returns the number of bytes written, which will either equal xCount in the
164 * success case, or 0 if there was not enough space in the buffer (in which case
165 * no data is written into the buffer).
167 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
168 const uint8_t * pucData,
169 size_t xCount ) PRIVILEGED_FUNCTION;
172 * If the stream buffer is being used as a message buffer, then reads an entire
173 * message out of the buffer. If the stream buffer is being used as a stream
174 * buffer then read as many bytes as possible from the buffer.
175 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
176 * buffer's data storage area.
178 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
180 size_t xBufferLengthBytes,
181 size_t xBytesAvailable,
182 size_t xBytesToStoreMessageLength ) PRIVILEGED_FUNCTION;
185 * If the stream buffer is being used as a message buffer, then writes an entire
186 * message to the buffer. If the stream buffer is being used as a stream
187 * buffer then write as many bytes as possible to the buffer.
188 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
191 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
192 const void * pvTxData,
193 size_t xDataLengthBytes,
195 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
198 * Read xMaxCount bytes from the pxStreamBuffer message buffer and write them
201 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
204 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
207 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
208 * initialise the members of the newly created stream buffer structure.
210 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
211 uint8_t * const pucBuffer,
212 size_t xBufferSizeBytes,
213 size_t xTriggerLevelBytes,
214 uint8_t ucFlags ) PRIVILEGED_FUNCTION;
216 /*-----------------------------------------------------------*/
218 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
220 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
221 size_t xTriggerLevelBytes,
222 BaseType_t xIsMessageBuffer )
224 uint8_t * pucAllocatedMemory;
227 /* In case the stream buffer is going to be used as a message buffer
228 * (that is, it will hold discrete messages with a little meta data that
229 * says how big the next message is) check the buffer will be large enough
230 * to hold at least one message. */
231 if( xIsMessageBuffer == pdTRUE )
233 /* Is a message buffer but not statically allocated. */
234 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
235 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
239 /* Not a message buffer and not statically allocated. */
241 configASSERT( xBufferSizeBytes > 0 );
244 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
246 /* A trigger level of 0 would cause a waiting task to unblock even when
247 * the buffer was empty. */
248 if( xTriggerLevelBytes == ( size_t ) 0 )
250 xTriggerLevelBytes = ( size_t ) 1;
253 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
254 * Both are allocated in a single call to pvPortMalloc(). The
255 * StreamBuffer_t structure is placed at the start of the allocated memory
256 * and the buffer follows immediately after. The requested size is
257 * incremented so the free space is returned as the user would expect -
258 * this is a quirk of the implementation that means otherwise the free
259 * space would be reported as one byte smaller than would be logically
261 if( xBufferSizeBytes < ( xBufferSizeBytes + 1 + sizeof( StreamBuffer_t ) ) )
264 pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
268 pucAllocatedMemory = NULL;
271 if( pucAllocatedMemory != NULL )
273 prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pucAllocatedMemory, /* Structure at the start of the allocated memory. */ /*lint !e9087 Safe cast as allocated memory is aligned. */ /*lint !e826 Area is not too small and alignment is guaranteed provided malloc() behaves as expected and returns aligned buffer. */
274 pucAllocatedMemory + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */
279 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer );
283 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
286 return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
289 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
290 /*-----------------------------------------------------------*/
292 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
294 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
295 size_t xTriggerLevelBytes,
296 BaseType_t xIsMessageBuffer,
297 uint8_t * const pucStreamBufferStorageArea,
298 StaticStreamBuffer_t * const pxStaticStreamBuffer )
300 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
301 StreamBufferHandle_t xReturn;
304 configASSERT( pucStreamBufferStorageArea );
305 configASSERT( pxStaticStreamBuffer );
306 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
308 /* A trigger level of 0 would cause a waiting task to unblock even when
309 * the buffer was empty. */
310 if( xTriggerLevelBytes == ( size_t ) 0 )
312 xTriggerLevelBytes = ( size_t ) 1;
315 if( xIsMessageBuffer != pdFALSE )
317 /* Statically allocated message buffer. */
318 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
322 /* Statically allocated stream buffer. */
323 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
326 /* In case the stream buffer is going to be used as a message buffer
327 * (that is, it will hold discrete messages with a little meta data that
328 * says how big the next message is) check the buffer will be large enough
329 * to hold at least one message. */
330 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
332 #if ( configASSERT_DEFINED == 1 )
334 /* Sanity check that the size of the structure used to declare a
335 * variable of type StaticStreamBuffer_t equals the size of the real
336 * message buffer structure. */
337 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
338 configASSERT( xSize == sizeof( StreamBuffer_t ) );
339 } /*lint !e529 xSize is referenced is configASSERT() is defined. */
340 #endif /* configASSERT_DEFINED */
342 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
344 prvInitialiseNewStreamBuffer( pxStreamBuffer,
345 pucStreamBufferStorageArea,
350 /* Remember this was statically allocated in case it is ever deleted
352 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
354 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
356 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
361 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
367 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
368 /*-----------------------------------------------------------*/
370 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
372 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
374 configASSERT( pxStreamBuffer );
376 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
378 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
380 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
382 /* Both the structure and the buffer were allocated using a single call
383 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
384 vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
388 /* Should not be possible to get here, ucFlags must be corrupt.
389 * Force an assert. */
390 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
396 /* The structure and buffer were not allocated dynamically and cannot be
397 * freed - just scrub the structure so future use will assert. */
398 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
401 /*-----------------------------------------------------------*/
403 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
405 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
406 BaseType_t xReturn = pdFAIL;
408 #if ( configUSE_TRACE_FACILITY == 1 )
409 UBaseType_t uxStreamBufferNumber;
412 configASSERT( pxStreamBuffer );
414 #if ( configUSE_TRACE_FACILITY == 1 )
416 /* Store the stream buffer number so it can be restored after the
418 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
422 /* Can only reset a message buffer if there are no tasks blocked on it. */
423 taskENTER_CRITICAL();
425 if( pxStreamBuffer->xTaskWaitingToReceive == NULL )
427 if( pxStreamBuffer->xTaskWaitingToSend == NULL )
429 prvInitialiseNewStreamBuffer( pxStreamBuffer,
430 pxStreamBuffer->pucBuffer,
431 pxStreamBuffer->xLength,
432 pxStreamBuffer->xTriggerLevelBytes,
433 pxStreamBuffer->ucFlags );
436 #if ( configUSE_TRACE_FACILITY == 1 )
438 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
442 traceSTREAM_BUFFER_RESET( xStreamBuffer );
450 /*-----------------------------------------------------------*/
452 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
453 size_t xTriggerLevel )
455 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
458 configASSERT( pxStreamBuffer );
460 /* It is not valid for the trigger level to be 0. */
461 if( xTriggerLevel == ( size_t ) 0 )
463 xTriggerLevel = ( size_t ) 1;
466 /* The trigger level is the number of bytes that must be in the stream
467 * buffer before a task that is waiting for data is unblocked. */
468 if( xTriggerLevel < pxStreamBuffer->xLength )
470 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
480 /*-----------------------------------------------------------*/
482 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
484 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
487 configASSERT( pxStreamBuffer );
489 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
490 xSpace -= pxStreamBuffer->xHead;
491 xSpace -= ( size_t ) 1;
493 if( xSpace >= pxStreamBuffer->xLength )
495 xSpace -= pxStreamBuffer->xLength;
499 mtCOVERAGE_TEST_MARKER();
504 /*-----------------------------------------------------------*/
506 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
508 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
511 configASSERT( pxStreamBuffer );
513 xReturn = prvBytesInBuffer( pxStreamBuffer );
516 /*-----------------------------------------------------------*/
518 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
519 const void * pvTxData,
520 size_t xDataLengthBytes,
521 TickType_t xTicksToWait )
523 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
524 size_t xReturn, xSpace = 0;
525 size_t xRequiredSpace = xDataLengthBytes;
527 size_t xMaxReportedSpace = 0;
529 configASSERT( pvTxData );
530 configASSERT( pxStreamBuffer );
532 /* The maximum amount of space a stream buffer will ever report is its length
534 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
536 /* This send function is used to write to both message buffers and stream
537 * buffers. If this is a message buffer then the space needed must be
538 * increased by the amount of bytes needed to store the length of the
540 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
542 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
545 configASSERT( xRequiredSpace > xDataLengthBytes );
547 /* If this is a message buffer then it must be possible to write the
549 if( xRequiredSpace > xMaxReportedSpace )
551 /* The message would not fit even if the entire buffer was empty,
552 * so don't wait for space. */
553 xTicksToWait = ( TickType_t ) 0;
557 mtCOVERAGE_TEST_MARKER();
562 /* If this is a stream buffer then it is acceptable to write only part
563 * of the message to the buffer. Cap the length to the total length of
565 if( xRequiredSpace > xMaxReportedSpace )
567 xRequiredSpace = xMaxReportedSpace;
571 mtCOVERAGE_TEST_MARKER();
575 if( xTicksToWait != ( TickType_t ) 0 )
577 vTaskSetTimeOutState( &xTimeOut );
581 /* Wait until the required number of bytes are free in the message
583 taskENTER_CRITICAL();
585 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
587 if( xSpace < xRequiredSpace )
589 /* Clear notification state as going to wait for space. */
590 ( void ) xTaskNotifyStateClear( NULL );
592 /* Should only be one writer. */
593 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
594 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
604 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
605 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
606 pxStreamBuffer->xTaskWaitingToSend = NULL;
607 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
611 mtCOVERAGE_TEST_MARKER();
614 if( xSpace == ( size_t ) 0 )
616 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
620 mtCOVERAGE_TEST_MARKER();
623 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
625 if( xReturn > ( size_t ) 0 )
627 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
629 /* Was a task waiting for the data? */
630 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
632 sbSEND_COMPLETED( pxStreamBuffer );
636 mtCOVERAGE_TEST_MARKER();
641 mtCOVERAGE_TEST_MARKER();
642 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
647 /*-----------------------------------------------------------*/
649 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
650 const void * pvTxData,
651 size_t xDataLengthBytes,
652 BaseType_t * const pxHigherPriorityTaskWoken )
654 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
655 size_t xReturn, xSpace;
656 size_t xRequiredSpace = xDataLengthBytes;
658 configASSERT( pvTxData );
659 configASSERT( pxStreamBuffer );
661 /* This send function is used to write to both message buffers and stream
662 * buffers. If this is a message buffer then the space needed must be
663 * increased by the amount of bytes needed to store the length of the
665 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
667 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
671 mtCOVERAGE_TEST_MARKER();
674 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
675 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
677 if( xReturn > ( size_t ) 0 )
679 /* Was a task waiting for the data? */
680 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
682 sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
686 mtCOVERAGE_TEST_MARKER();
691 mtCOVERAGE_TEST_MARKER();
694 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
698 /*-----------------------------------------------------------*/
700 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
701 const void * pvTxData,
702 size_t xDataLengthBytes,
704 size_t xRequiredSpace )
706 BaseType_t xShouldWrite;
709 if( xSpace == ( size_t ) 0 )
711 /* Doesn't matter if this is a stream buffer or a message buffer, there
712 * is no space to write. */
713 xShouldWrite = pdFALSE;
715 else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
717 /* This is a stream buffer, as opposed to a message buffer, so writing a
718 * stream of bytes rather than discrete messages. Write as many bytes as
720 xShouldWrite = pdTRUE;
721 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
723 else if( xSpace >= xRequiredSpace )
725 /* This is a message buffer, as opposed to a stream buffer, and there
726 * is enough space to write both the message length and the message itself
727 * into the buffer. Start by writing the length of the data, the data
728 * itself will be written later in this function. */
729 xShouldWrite = pdTRUE;
730 ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
734 /* There is space available, but not enough space. */
735 xShouldWrite = pdFALSE;
738 if( xShouldWrite != pdFALSE )
740 /* Writes the data itself. */
741 xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alignment and access. */
750 /*-----------------------------------------------------------*/
752 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
754 size_t xBufferLengthBytes,
755 TickType_t xTicksToWait )
757 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
758 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
760 configASSERT( pvRxData );
761 configASSERT( pxStreamBuffer );
763 /* This receive function is used by both message buffers, which store
764 * discrete messages, and stream buffers, which store a continuous stream of
765 * bytes. Discrete messages include an additional
766 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
768 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
770 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
774 xBytesToStoreMessageLength = 0;
777 if( xTicksToWait != ( TickType_t ) 0 )
779 /* Checking if there is data and clearing the notification state must be
780 * performed atomically. */
781 taskENTER_CRITICAL();
783 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
785 /* If this function was invoked by a message buffer read then
786 * xBytesToStoreMessageLength holds the number of bytes used to hold
787 * the length of the next discrete message. If this function was
788 * invoked by a stream buffer read then xBytesToStoreMessageLength will
790 if( xBytesAvailable <= xBytesToStoreMessageLength )
792 /* Clear notification state as going to wait for data. */
793 ( void ) xTaskNotifyStateClear( NULL );
795 /* Should only be one reader. */
796 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
797 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
801 mtCOVERAGE_TEST_MARKER();
806 if( xBytesAvailable <= xBytesToStoreMessageLength )
808 /* Wait for data to be available. */
809 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
810 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
811 pxStreamBuffer->xTaskWaitingToReceive = NULL;
813 /* Recheck the data available after blocking. */
814 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
818 mtCOVERAGE_TEST_MARKER();
823 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
826 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
827 * holds the number of bytes used to store the message length) or a stream of
828 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
829 * available must be greater than xBytesToStoreMessageLength to be able to
830 * read bytes from the buffer. */
831 if( xBytesAvailable > xBytesToStoreMessageLength )
833 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
835 /* Was a task waiting for space in the buffer? */
836 if( xReceivedLength != ( size_t ) 0 )
838 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
839 sbRECEIVE_COMPLETED( pxStreamBuffer );
843 mtCOVERAGE_TEST_MARKER();
848 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
849 mtCOVERAGE_TEST_MARKER();
852 return xReceivedLength;
854 /*-----------------------------------------------------------*/
856 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
858 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
859 size_t xReturn, xBytesAvailable, xOriginalTail;
860 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
862 configASSERT( pxStreamBuffer );
864 /* Ensure the stream buffer is being used as a message buffer. */
865 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
867 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
869 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
871 /* The number of bytes available is greater than the number of bytes
872 * required to hold the length of the next message, so another message
873 * is available. Return its length without removing the length bytes
874 * from the buffer. A copy of the tail is stored so the buffer can be
875 * returned to its prior state as the message is not actually being
876 * removed from the buffer. */
877 xOriginalTail = pxStreamBuffer->xTail;
878 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
879 xReturn = ( size_t ) xTempReturn;
880 pxStreamBuffer->xTail = xOriginalTail;
884 /* The minimum amount of bytes in a message buffer is
885 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
886 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
888 configASSERT( xBytesAvailable == 0 );
899 /*-----------------------------------------------------------*/
901 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
903 size_t xBufferLengthBytes,
904 BaseType_t * const pxHigherPriorityTaskWoken )
906 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
907 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
909 configASSERT( pvRxData );
910 configASSERT( pxStreamBuffer );
912 /* This receive function is used by both message buffers, which store
913 * discrete messages, and stream buffers, which store a continuous stream of
914 * bytes. Discrete messages include an additional
915 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
917 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
919 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
923 xBytesToStoreMessageLength = 0;
926 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
928 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
929 * holds the number of bytes used to store the message length) or a stream of
930 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
931 * available must be greater than xBytesToStoreMessageLength to be able to
932 * read bytes from the buffer. */
933 if( xBytesAvailable > xBytesToStoreMessageLength )
935 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
937 /* Was a task waiting for space in the buffer? */
938 if( xReceivedLength != ( size_t ) 0 )
940 sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
944 mtCOVERAGE_TEST_MARKER();
949 mtCOVERAGE_TEST_MARKER();
952 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
954 return xReceivedLength;
956 /*-----------------------------------------------------------*/
958 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
960 size_t xBufferLengthBytes,
961 size_t xBytesAvailable,
962 size_t xBytesToStoreMessageLength )
964 size_t xOriginalTail, xReceivedLength, xNextMessageLength;
965 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
967 if( xBytesToStoreMessageLength != ( size_t ) 0 )
969 /* A discrete message is being received. First receive the length
970 * of the message. A copy of the tail is stored so the buffer can be
971 * returned to its prior state if the length of the message is too
972 * large for the provided buffer. */
973 xOriginalTail = pxStreamBuffer->xTail;
974 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
975 xNextMessageLength = ( size_t ) xTempNextMessageLength;
977 /* Reduce the number of bytes available by the number of bytes just
979 xBytesAvailable -= xBytesToStoreMessageLength;
981 /* Check there is enough space in the buffer provided by the
983 if( xNextMessageLength > xBufferLengthBytes )
985 /* The user has provided insufficient space to read the message
986 * so return the buffer to its previous state (so the length of
987 * the message is in the buffer again). */
988 pxStreamBuffer->xTail = xOriginalTail;
989 xNextMessageLength = 0;
993 mtCOVERAGE_TEST_MARKER();
998 /* A stream of bytes is being received (as opposed to a discrete
999 * message), so read as many bytes as possible. */
1000 xNextMessageLength = xBufferLengthBytes;
1003 /* Read the actual data. */
1004 xReceivedLength = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xNextMessageLength, xBytesAvailable ); /*lint !e9079 Data storage area is implemented as uint8_t array for ease of sizing, indexing and alignment. */
1006 return xReceivedLength;
1008 /*-----------------------------------------------------------*/
1010 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1012 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1016 configASSERT( pxStreamBuffer );
1018 /* True if no bytes are available. */
1019 xTail = pxStreamBuffer->xTail;
1021 if( pxStreamBuffer->xHead == xTail )
1032 /*-----------------------------------------------------------*/
1034 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1037 size_t xBytesToStoreMessageLength;
1038 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1040 configASSERT( pxStreamBuffer );
1042 /* This generic version of the receive function is used by both message
1043 * buffers, which store discrete messages, and stream buffers, which store a
1044 * continuous stream of bytes. Discrete messages include an additional
1045 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1046 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1048 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1052 xBytesToStoreMessageLength = 0;
1055 /* True if the available space equals zero. */
1056 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1067 /*-----------------------------------------------------------*/
1069 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1070 BaseType_t * pxHigherPriorityTaskWoken )
1072 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1074 UBaseType_t uxSavedInterruptStatus;
1076 configASSERT( pxStreamBuffer );
1078 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
1080 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1082 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1085 pxHigherPriorityTaskWoken );
1086 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1094 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1098 /*-----------------------------------------------------------*/
1100 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1101 BaseType_t * pxHigherPriorityTaskWoken )
1103 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1105 UBaseType_t uxSavedInterruptStatus;
1107 configASSERT( pxStreamBuffer );
1109 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
1111 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1113 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1116 pxHigherPriorityTaskWoken );
1117 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1125 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1129 /*-----------------------------------------------------------*/
1131 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1132 const uint8_t * pucData,
1135 size_t xNextHead, xFirstLength;
1137 configASSERT( xCount > ( size_t ) 0 );
1139 xNextHead = pxStreamBuffer->xHead;
1141 /* Calculate the number of bytes that can be added in the first write -
1142 * which may be less than the total number of bytes that need to be added if
1143 * the buffer will wrap back to the beginning. */
1144 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
1146 /* Write as many bytes as can be written in the first write. */
1147 configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
1148 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1150 /* If the number of bytes written was less than the number that could be
1151 * written in the first write... */
1152 if( xCount > xFirstLength )
1154 /* ...then write the remaining bytes to the start of the buffer. */
1155 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1156 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1160 mtCOVERAGE_TEST_MARKER();
1163 xNextHead += xCount;
1165 if( xNextHead >= pxStreamBuffer->xLength )
1167 xNextHead -= pxStreamBuffer->xLength;
1171 mtCOVERAGE_TEST_MARKER();
1174 pxStreamBuffer->xHead = xNextHead;
1178 /*-----------------------------------------------------------*/
1180 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1183 size_t xBytesAvailable )
1185 size_t xCount, xFirstLength, xNextTail;
1187 /* Use the minimum of the wanted bytes and the available bytes. */
1188 xCount = configMIN( xBytesAvailable, xMaxCount );
1190 if( xCount > ( size_t ) 0 )
1192 xNextTail = pxStreamBuffer->xTail;
1194 /* Calculate the number of bytes that can be read - which may be
1195 * less than the number wanted if the data wraps around to the start of
1197 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
1199 /* Obtain the number of bytes it is possible to obtain in the first
1200 * read. Asserts check bounds of read and write. */
1201 configASSERT( xFirstLength <= xMaxCount );
1202 configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
1203 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1205 /* If the total number of wanted bytes is greater than the number
1206 * that could be read in the first read... */
1207 if( xCount > xFirstLength )
1209 /*...then read the remaining bytes from the start of the buffer. */
1210 configASSERT( xCount <= xMaxCount );
1211 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1215 mtCOVERAGE_TEST_MARKER();
1218 /* Move the tail pointer to effectively remove the data read from
1220 xNextTail += xCount;
1222 if( xNextTail >= pxStreamBuffer->xLength )
1224 xNextTail -= pxStreamBuffer->xLength;
1227 pxStreamBuffer->xTail = xNextTail;
1231 mtCOVERAGE_TEST_MARKER();
1236 /*-----------------------------------------------------------*/
1238 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1240 /* Returns the distance between xTail and xHead. */
1243 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1244 xCount -= pxStreamBuffer->xTail;
1246 if( xCount >= pxStreamBuffer->xLength )
1248 xCount -= pxStreamBuffer->xLength;
1252 mtCOVERAGE_TEST_MARKER();
1257 /*-----------------------------------------------------------*/
1259 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1260 uint8_t * const pucBuffer,
1261 size_t xBufferSizeBytes,
1262 size_t xTriggerLevelBytes,
1265 /* Assert here is deliberately writing to the entire buffer to ensure it can
1266 * be written to without generating exceptions, and is setting the buffer to a
1267 * known value to assist in development/debugging. */
1268 #if ( configASSERT_DEFINED == 1 )
1270 /* The value written just has to be identifiable when looking at the
1271 * memory. Don't use 0xA5 as that is the stack fill value and could
1272 * result in confusion as to what is actually being observed. */
1273 const BaseType_t xWriteValue = 0x55;
1274 configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
1275 } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */
1278 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
1279 pxStreamBuffer->pucBuffer = pucBuffer;
1280 pxStreamBuffer->xLength = xBufferSizeBytes;
1281 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1282 pxStreamBuffer->ucFlags = ucFlags;
1285 #if ( configUSE_TRACE_FACILITY == 1 )
1287 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1289 return xStreamBuffer->uxStreamBufferNumber;
1292 #endif /* configUSE_TRACE_FACILITY */
1293 /*-----------------------------------------------------------*/
1295 #if ( configUSE_TRACE_FACILITY == 1 )
1297 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1298 UBaseType_t uxStreamBufferNumber )
1300 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1303 #endif /* configUSE_TRACE_FACILITY */
1304 /*-----------------------------------------------------------*/
1306 #if ( configUSE_TRACE_FACILITY == 1 )
1308 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1310 return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
1313 #endif /* configUSE_TRACE_FACILITY */
1314 /*-----------------------------------------------------------*/