2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /* Standard includes. */
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
33 * all the API functions to use the MPU wrappers. That should only be done when
34 * task.h is included from an application file. */
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37 /* FreeRTOS includes. */
40 #include "stream_buffer.h"
42 #if ( configUSE_TASK_NOTIFICATIONS != 1 )
43 #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
46 #if ( INCLUDE_xTaskGetCurrentTaskHandle != 1 )
47 #error INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 to build stream_buffer.c
50 /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
51 * for the header files above, but not in this file, in order to generate the
52 * correct privileged Vs unprivileged linkage and placement. */
53 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
55 /* This entire source file will be skipped if the application is not configured
56 * to include stream buffer functionality. This #if is closed at the very bottom
57 * of this file. If you want to include stream buffers then ensure
58 * configUSE_STREAM_BUFFERS is set to 1 in FreeRTOSConfig.h. */
59 #if ( configUSE_STREAM_BUFFERS == 1 )
61 /* If the user has not provided application specific Rx notification macros,
62 * or #defined the notification macros away, then provide default implementations
63 * that uses task notifications. */
64 #ifndef sbRECEIVE_COMPLETED
65 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
70 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
72 ( void ) xTaskNotifyIndexed( ( pxStreamBuffer )->xTaskWaitingToSend, \
73 ( pxStreamBuffer )->uxNotificationIndex, \
76 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
79 ( void ) xTaskResumeAll(); \
81 #endif /* sbRECEIVE_COMPLETED */
83 /* If user has provided a per-instance receive complete callback, then
84 * invoke the callback else use the receive complete macro which is provided by default for all instances.
86 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
87 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) \
89 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
91 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
95 sbRECEIVE_COMPLETED( ( pxStreamBuffer ) ); \
98 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
99 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) sbRECEIVE_COMPLETED( ( pxStreamBuffer ) )
100 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
102 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
103 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
104 pxHigherPriorityTaskWoken ) \
106 UBaseType_t uxSavedInterruptStatus; \
108 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
110 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
112 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
113 ( pxStreamBuffer )->uxNotificationIndex, \
116 ( pxHigherPriorityTaskWoken ) ); \
117 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
120 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
122 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
124 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
125 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
126 pxHigherPriorityTaskWoken ) \
128 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
130 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
134 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
137 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
138 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
139 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
140 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
142 /* If the user has not provided an application specific Tx notification macro,
143 * or #defined the notification macro away, then provide a default
144 * implementation that uses task notifications.
146 #ifndef sbSEND_COMPLETED
147 #define sbSEND_COMPLETED( pxStreamBuffer ) \
150 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
152 ( void ) xTaskNotifyIndexed( ( pxStreamBuffer )->xTaskWaitingToReceive, \
153 ( pxStreamBuffer )->uxNotificationIndex, \
156 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
159 ( void ) xTaskResumeAll()
160 #endif /* sbSEND_COMPLETED */
162 /* If user has provided a per-instance send completed callback, then
163 * invoke the callback else use the send complete macro which is provided by default for all instances.
165 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
166 #define prvSEND_COMPLETED( pxStreamBuffer ) \
168 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
170 ( pxStreamBuffer )->pxSendCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
174 sbSEND_COMPLETED( ( pxStreamBuffer ) ); \
177 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
178 #define prvSEND_COMPLETED( pxStreamBuffer ) sbSEND_COMPLETED( ( pxStreamBuffer ) )
179 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
182 #ifndef sbSEND_COMPLETE_FROM_ISR
183 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
185 UBaseType_t uxSavedInterruptStatus; \
187 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
189 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
191 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
192 ( pxStreamBuffer )->uxNotificationIndex, \
195 ( pxHigherPriorityTaskWoken ) ); \
196 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
199 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
201 #endif /* sbSEND_COMPLETE_FROM_ISR */
204 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
205 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
207 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
209 ( pxStreamBuffer )->pxSendCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
213 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
216 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
217 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
218 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
219 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
221 /* The number of bytes used to hold the length of a message in the buffer. */
222 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
224 /* Bits stored in the ucFlags field of the stream buffer. */
225 #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. */
226 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
228 /*-----------------------------------------------------------*/
230 /* Structure that hold state information on the buffer. */
231 typedef struct StreamBufferDef_t
233 volatile size_t xTail; /* Index to the next item to read within the buffer. */
234 volatile size_t xHead; /* Index to the next item to write within the buffer. */
235 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
236 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
237 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
238 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
239 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
242 #if ( configUSE_TRACE_FACILITY == 1 )
243 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
246 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
247 StreamBufferCallbackFunction_t pxSendCompletedCallback; /* Optional callback called on send complete. sbSEND_COMPLETED is called if this is NULL. */
248 StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
250 UBaseType_t uxNotificationIndex; /* The index we are using for notification, by default tskDEFAULT_INDEX_TO_NOTIFY. */
254 * The number of bytes available to be read from the buffer.
256 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
259 * Add xCount bytes from pucData into the pxStreamBuffer's data storage area.
260 * This function does not update the buffer's xHead pointer, so multiple writes
261 * may be chained together "atomically". This is useful for Message Buffers where
262 * the length and data bytes are written in two separate chunks, and we don't want
263 * the reader to see the buffer as having grown until after all data is copied over.
264 * This function takes a custom xHead value to indicate where to write to (necessary
265 * for chaining) and returns the the resulting xHead position.
266 * To mark the write as complete, manually set the buffer's xHead field with the
267 * returned xHead from this function.
269 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
270 const uint8_t * pucData,
272 size_t xHead ) PRIVILEGED_FUNCTION;
275 * If the stream buffer is being used as a message buffer, then reads an entire
276 * message out of the buffer. If the stream buffer is being used as a stream
277 * buffer then read as many bytes as possible from the buffer.
278 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
279 * buffer's data storage area.
281 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
283 size_t xBufferLengthBytes,
284 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
287 * If the stream buffer is being used as a message buffer, then writes an entire
288 * message to the buffer. If the stream buffer is being used as a stream
289 * buffer then write as many bytes as possible to the buffer.
290 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
293 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
294 const void * pvTxData,
295 size_t xDataLengthBytes,
297 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
300 * Copies xCount bytes from the pxStreamBuffer's data storage area to pucData.
301 * This function does not update the buffer's xTail pointer, so multiple reads
302 * may be chained together "atomically". This is useful for Message Buffers where
303 * the length and data bytes are read in two separate chunks, and we don't want
304 * the writer to see the buffer as having more free space until after all data is
305 * copied over, especially if we have to abort the read due to insufficient receiving space.
306 * This function takes a custom xTail value to indicate where to read from (necessary
307 * for chaining) and returns the the resulting xTail position.
308 * To mark the read as complete, manually set the buffer's xTail field with the
309 * returned xTail from this function.
311 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
314 size_t xTail ) PRIVILEGED_FUNCTION;
317 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
318 * initialise the members of the newly created stream buffer structure.
320 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
321 uint8_t * const pucBuffer,
322 size_t xBufferSizeBytes,
323 size_t xTriggerLevelBytes,
325 StreamBufferCallbackFunction_t pxSendCompletedCallback,
326 StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
328 /*-----------------------------------------------------------*/
329 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
330 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
331 size_t xTriggerLevelBytes,
332 BaseType_t xIsMessageBuffer,
333 StreamBufferCallbackFunction_t pxSendCompletedCallback,
334 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
336 void * pvAllocatedMemory;
339 traceENTER_xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
341 /* In case the stream buffer is going to be used as a message buffer
342 * (that is, it will hold discrete messages with a little meta data that
343 * says how big the next message is) check the buffer will be large enough
344 * to hold at least one message. */
345 if( xIsMessageBuffer == pdTRUE )
347 /* Is a message buffer but not statically allocated. */
348 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
349 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
353 /* Not a message buffer and not statically allocated. */
355 configASSERT( xBufferSizeBytes > 0 );
358 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
360 /* A trigger level of 0 would cause a waiting task to unblock even when
361 * the buffer was empty. */
362 if( xTriggerLevelBytes == ( size_t ) 0 )
364 xTriggerLevelBytes = ( size_t ) 1;
367 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
368 * Both are allocated in a single call to pvPortMalloc(). The
369 * StreamBuffer_t structure is placed at the start of the allocated memory
370 * and the buffer follows immediately after. The requested size is
371 * incremented so the free space is returned as the user would expect -
372 * this is a quirk of the implementation that means otherwise the free
373 * space would be reported as one byte smaller than would be logically
375 if( xBufferSizeBytes < ( xBufferSizeBytes + 1U + sizeof( StreamBuffer_t ) ) )
378 pvAllocatedMemory = pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) );
382 pvAllocatedMemory = NULL;
385 if( pvAllocatedMemory != NULL )
387 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
388 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
389 /* coverity[misra_c_2012_rule_11_5_violation] */
390 prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pvAllocatedMemory, /* Structure at the start of the allocated memory. */
391 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
392 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
393 /* coverity[misra_c_2012_rule_11_5_violation] */
394 ( ( uint8_t * ) pvAllocatedMemory ) + sizeof( StreamBuffer_t ), /* Storage area follows. */
398 pxSendCompletedCallback,
399 pxReceiveCompletedCallback );
401 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xIsMessageBuffer );
405 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
408 traceRETURN_xStreamBufferGenericCreate( pvAllocatedMemory );
410 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
411 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
412 /* coverity[misra_c_2012_rule_11_5_violation] */
413 return ( StreamBufferHandle_t ) pvAllocatedMemory;
415 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
416 /*-----------------------------------------------------------*/
418 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
420 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
421 size_t xTriggerLevelBytes,
422 BaseType_t xIsMessageBuffer,
423 uint8_t * const pucStreamBufferStorageArea,
424 StaticStreamBuffer_t * const pxStaticStreamBuffer,
425 StreamBufferCallbackFunction_t pxSendCompletedCallback,
426 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
428 /* MISRA Ref 11.3.1 [Misaligned access] */
429 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
430 /* coverity[misra_c_2012_rule_11_3_violation] */
431 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer;
432 StreamBufferHandle_t xReturn;
435 traceENTER_xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
437 configASSERT( pucStreamBufferStorageArea );
438 configASSERT( pxStaticStreamBuffer );
439 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
441 /* A trigger level of 0 would cause a waiting task to unblock even when
442 * the buffer was empty. */
443 if( xTriggerLevelBytes == ( size_t ) 0 )
445 xTriggerLevelBytes = ( size_t ) 1;
448 /* In case the stream buffer is going to be used as a message buffer
449 * (that is, it will hold discrete messages with a little meta data that
450 * says how big the next message is) check the buffer will be large enough
451 * to hold at least one message. */
453 if( xIsMessageBuffer != pdFALSE )
455 /* Statically allocated message buffer. */
456 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
457 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
461 /* Statically allocated stream buffer. */
462 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
465 #if ( configASSERT_DEFINED == 1 )
467 /* Sanity check that the size of the structure used to declare a
468 * variable of type StaticStreamBuffer_t equals the size of the real
469 * message buffer structure. */
470 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
471 configASSERT( xSize == sizeof( StreamBuffer_t ) );
473 #endif /* configASSERT_DEFINED */
475 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
477 prvInitialiseNewStreamBuffer( pxStreamBuffer,
478 pucStreamBufferStorageArea,
482 pxSendCompletedCallback,
483 pxReceiveCompletedCallback );
485 /* Remember this was statically allocated in case it is ever deleted
487 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
489 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
491 /* MISRA Ref 11.3.1 [Misaligned access] */
492 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
493 /* coverity[misra_c_2012_rule_11_3_violation] */
494 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer;
499 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
502 traceRETURN_xStreamBufferGenericCreateStatic( xReturn );
506 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
507 /*-----------------------------------------------------------*/
509 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
510 BaseType_t xStreamBufferGetStaticBuffers( StreamBufferHandle_t xStreamBuffer,
511 uint8_t ** ppucStreamBufferStorageArea,
512 StaticStreamBuffer_t ** ppxStaticStreamBuffer )
515 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
517 traceENTER_xStreamBufferGetStaticBuffers( xStreamBuffer, ppucStreamBufferStorageArea, ppxStaticStreamBuffer );
519 configASSERT( pxStreamBuffer );
520 configASSERT( ppucStreamBufferStorageArea );
521 configASSERT( ppxStaticStreamBuffer );
523 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) != ( uint8_t ) 0 )
525 *ppucStreamBufferStorageArea = pxStreamBuffer->pucBuffer;
526 /* MISRA Ref 11.3.1 [Misaligned access] */
527 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
528 /* coverity[misra_c_2012_rule_11_3_violation] */
529 *ppxStaticStreamBuffer = ( StaticStreamBuffer_t * ) pxStreamBuffer;
537 traceRETURN_xStreamBufferGetStaticBuffers( xReturn );
541 #endif /* configSUPPORT_STATIC_ALLOCATION */
542 /*-----------------------------------------------------------*/
544 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
546 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
548 traceENTER_vStreamBufferDelete( xStreamBuffer );
550 configASSERT( pxStreamBuffer );
552 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
554 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
556 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
558 /* Both the structure and the buffer were allocated using a single call
559 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
560 vPortFree( ( void * ) pxStreamBuffer );
564 /* Should not be possible to get here, ucFlags must be corrupt.
565 * Force an assert. */
566 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
572 /* The structure and buffer were not allocated dynamically and cannot be
573 * freed - just scrub the structure so future use will assert. */
574 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
577 traceRETURN_vStreamBufferDelete();
579 /*-----------------------------------------------------------*/
581 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
583 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
584 BaseType_t xReturn = pdFAIL;
585 StreamBufferCallbackFunction_t pxSendCallback = NULL, pxReceiveCallback = NULL;
587 #if ( configUSE_TRACE_FACILITY == 1 )
588 UBaseType_t uxStreamBufferNumber;
591 traceENTER_xStreamBufferReset( xStreamBuffer );
593 configASSERT( pxStreamBuffer );
595 #if ( configUSE_TRACE_FACILITY == 1 )
597 /* Store the stream buffer number so it can be restored after the
599 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
603 /* Can only reset a message buffer if there are no tasks blocked on it. */
604 taskENTER_CRITICAL();
606 if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
608 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
610 pxSendCallback = pxStreamBuffer->pxSendCompletedCallback;
611 pxReceiveCallback = pxStreamBuffer->pxReceiveCompletedCallback;
615 prvInitialiseNewStreamBuffer( pxStreamBuffer,
616 pxStreamBuffer->pucBuffer,
617 pxStreamBuffer->xLength,
618 pxStreamBuffer->xTriggerLevelBytes,
619 pxStreamBuffer->ucFlags,
623 #if ( configUSE_TRACE_FACILITY == 1 )
625 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
629 traceSTREAM_BUFFER_RESET( xStreamBuffer );
636 traceRETURN_xStreamBufferReset( xReturn );
640 /*-----------------------------------------------------------*/
642 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
643 size_t xTriggerLevel )
645 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
648 traceENTER_xStreamBufferSetTriggerLevel( xStreamBuffer, xTriggerLevel );
650 configASSERT( pxStreamBuffer );
652 /* It is not valid for the trigger level to be 0. */
653 if( xTriggerLevel == ( size_t ) 0 )
655 xTriggerLevel = ( size_t ) 1;
658 /* The trigger level is the number of bytes that must be in the stream
659 * buffer before a task that is waiting for data is unblocked. */
660 if( xTriggerLevel < pxStreamBuffer->xLength )
662 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
670 traceRETURN_xStreamBufferSetTriggerLevel( xReturn );
674 /*-----------------------------------------------------------*/
676 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
678 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
680 size_t xOriginalTail;
682 traceENTER_xStreamBufferSpacesAvailable( xStreamBuffer );
684 configASSERT( pxStreamBuffer );
686 /* The code below reads xTail and then xHead. This is safe if the stream
687 * buffer is updated once between the two reads - but not if the stream buffer
688 * is updated more than once between the two reads - hence the loop. */
691 xOriginalTail = pxStreamBuffer->xTail;
692 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
693 xSpace -= pxStreamBuffer->xHead;
694 } while( xOriginalTail != pxStreamBuffer->xTail );
696 xSpace -= ( size_t ) 1;
698 if( xSpace >= pxStreamBuffer->xLength )
700 xSpace -= pxStreamBuffer->xLength;
704 mtCOVERAGE_TEST_MARKER();
707 traceRETURN_xStreamBufferSpacesAvailable( xSpace );
711 /*-----------------------------------------------------------*/
713 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
715 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
718 traceENTER_xStreamBufferBytesAvailable( xStreamBuffer );
720 configASSERT( pxStreamBuffer );
722 xReturn = prvBytesInBuffer( pxStreamBuffer );
724 traceRETURN_xStreamBufferBytesAvailable( xReturn );
728 /*-----------------------------------------------------------*/
730 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
731 const void * pvTxData,
732 size_t xDataLengthBytes,
733 TickType_t xTicksToWait )
735 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
736 size_t xReturn, xSpace = 0;
737 size_t xRequiredSpace = xDataLengthBytes;
739 size_t xMaxReportedSpace = 0;
741 traceENTER_xStreamBufferSend( xStreamBuffer, pvTxData, xDataLengthBytes, xTicksToWait );
743 configASSERT( pvTxData );
744 configASSERT( pxStreamBuffer );
746 /* The maximum amount of space a stream buffer will ever report is its length
748 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
750 /* This send function is used to write to both message buffers and stream
751 * buffers. If this is a message buffer then the space needed must be
752 * increased by the amount of bytes needed to store the length of the
754 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
756 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
759 configASSERT( xRequiredSpace > xDataLengthBytes );
761 /* If this is a message buffer then it must be possible to write the
763 if( xRequiredSpace > xMaxReportedSpace )
765 /* The message would not fit even if the entire buffer was empty,
766 * so don't wait for space. */
767 xTicksToWait = ( TickType_t ) 0;
771 mtCOVERAGE_TEST_MARKER();
776 /* If this is a stream buffer then it is acceptable to write only part
777 * of the message to the buffer. Cap the length to the total length of
779 if( xRequiredSpace > xMaxReportedSpace )
781 xRequiredSpace = xMaxReportedSpace;
785 mtCOVERAGE_TEST_MARKER();
789 if( xTicksToWait != ( TickType_t ) 0 )
791 vTaskSetTimeOutState( &xTimeOut );
795 /* Wait until the required number of bytes are free in the message
797 taskENTER_CRITICAL();
799 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
801 if( xSpace < xRequiredSpace )
803 /* Clear notification state as going to wait for space. */
804 ( void ) xTaskNotifyStateClearIndexed( NULL, pxStreamBuffer->uxNotificationIndex );
806 /* Should only be one writer. */
807 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
808 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
818 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
819 ( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
820 pxStreamBuffer->xTaskWaitingToSend = NULL;
821 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
825 mtCOVERAGE_TEST_MARKER();
828 if( xSpace == ( size_t ) 0 )
830 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
834 mtCOVERAGE_TEST_MARKER();
837 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
839 if( xReturn > ( size_t ) 0 )
841 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
843 /* Was a task waiting for the data? */
844 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
846 prvSEND_COMPLETED( pxStreamBuffer );
850 mtCOVERAGE_TEST_MARKER();
855 mtCOVERAGE_TEST_MARKER();
856 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
859 traceRETURN_xStreamBufferSend( xReturn );
863 /*-----------------------------------------------------------*/
865 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
866 const void * pvTxData,
867 size_t xDataLengthBytes,
868 BaseType_t * const pxHigherPriorityTaskWoken )
870 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
871 size_t xReturn, xSpace;
872 size_t xRequiredSpace = xDataLengthBytes;
874 traceENTER_xStreamBufferSendFromISR( xStreamBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken );
876 configASSERT( pvTxData );
877 configASSERT( pxStreamBuffer );
879 /* This send function is used to write to both message buffers and stream
880 * buffers. If this is a message buffer then the space needed must be
881 * increased by the amount of bytes needed to store the length of the
883 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
885 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
889 mtCOVERAGE_TEST_MARKER();
892 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
893 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
895 if( xReturn > ( size_t ) 0 )
897 /* Was a task waiting for the data? */
898 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
900 prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
904 mtCOVERAGE_TEST_MARKER();
909 mtCOVERAGE_TEST_MARKER();
912 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
913 traceRETURN_xStreamBufferSendFromISR( xReturn );
917 /*-----------------------------------------------------------*/
919 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
920 const void * pvTxData,
921 size_t xDataLengthBytes,
923 size_t xRequiredSpace )
925 size_t xNextHead = pxStreamBuffer->xHead;
926 configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
928 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
930 /* This is a message buffer, as opposed to a stream buffer. */
932 /* Convert xDataLengthBytes to the message length type. */
933 xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
935 /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
936 configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
938 if( xSpace >= xRequiredSpace )
940 /* There is enough space to write both the message length and the message
941 * itself into the buffer. Start by writing the length of the data, the data
942 * itself will be written later in this function. */
943 xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
947 /* Not enough space, so do not write data to the buffer. */
948 xDataLengthBytes = 0;
953 /* This is a stream buffer, as opposed to a message buffer, so writing a
954 * stream of bytes rather than discrete messages. Plan to write as many
955 * bytes as possible. */
956 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
959 if( xDataLengthBytes != ( size_t ) 0 )
961 /* Write the data to the buffer. */
962 /* MISRA Ref 11.5.5 [Void pointer assignment] */
963 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
964 /* coverity[misra_c_2012_rule_11_5_violation] */
965 pxStreamBuffer->xHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes, xNextHead );
968 return xDataLengthBytes;
970 /*-----------------------------------------------------------*/
972 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
974 size_t xBufferLengthBytes,
975 TickType_t xTicksToWait )
977 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
978 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
980 traceENTER_xStreamBufferReceive( xStreamBuffer, pvRxData, xBufferLengthBytes, xTicksToWait );
982 configASSERT( pvRxData );
983 configASSERT( pxStreamBuffer );
985 /* This receive function is used by both message buffers, which store
986 * discrete messages, and stream buffers, which store a continuous stream of
987 * bytes. Discrete messages include an additional
988 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
990 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
992 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
996 xBytesToStoreMessageLength = 0;
999 if( xTicksToWait != ( TickType_t ) 0 )
1001 /* Checking if there is data and clearing the notification state must be
1002 * performed atomically. */
1003 taskENTER_CRITICAL();
1005 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1007 /* If this function was invoked by a message buffer read then
1008 * xBytesToStoreMessageLength holds the number of bytes used to hold
1009 * the length of the next discrete message. If this function was
1010 * invoked by a stream buffer read then xBytesToStoreMessageLength will
1012 if( xBytesAvailable <= xBytesToStoreMessageLength )
1014 /* Clear notification state as going to wait for data. */
1015 ( void ) xTaskNotifyStateClearIndexed( NULL, pxStreamBuffer->uxNotificationIndex );
1017 /* Should only be one reader. */
1018 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
1019 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
1023 mtCOVERAGE_TEST_MARKER();
1026 taskEXIT_CRITICAL();
1028 if( xBytesAvailable <= xBytesToStoreMessageLength )
1030 /* Wait for data to be available. */
1031 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
1032 ( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
1033 pxStreamBuffer->xTaskWaitingToReceive = NULL;
1035 /* Recheck the data available after blocking. */
1036 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1040 mtCOVERAGE_TEST_MARKER();
1045 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1048 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1049 * holds the number of bytes used to store the message length) or a stream of
1050 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1051 * available must be greater than xBytesToStoreMessageLength to be able to
1052 * read bytes from the buffer. */
1053 if( xBytesAvailable > xBytesToStoreMessageLength )
1055 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1057 /* Was a task waiting for space in the buffer? */
1058 if( xReceivedLength != ( size_t ) 0 )
1060 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
1061 prvRECEIVE_COMPLETED( xStreamBuffer );
1065 mtCOVERAGE_TEST_MARKER();
1070 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
1071 mtCOVERAGE_TEST_MARKER();
1074 traceRETURN_xStreamBufferReceive( xReceivedLength );
1076 return xReceivedLength;
1078 /*-----------------------------------------------------------*/
1080 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
1082 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1083 size_t xReturn, xBytesAvailable;
1084 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
1086 traceENTER_xStreamBufferNextMessageLengthBytes( xStreamBuffer );
1088 configASSERT( pxStreamBuffer );
1090 /* Ensure the stream buffer is being used as a message buffer. */
1091 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1093 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1095 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
1097 /* The number of bytes available is greater than the number of bytes
1098 * required to hold the length of the next message, so another message
1100 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
1101 xReturn = ( size_t ) xTempReturn;
1105 /* The minimum amount of bytes in a message buffer is
1106 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
1107 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
1109 configASSERT( xBytesAvailable == 0 );
1118 traceRETURN_xStreamBufferNextMessageLengthBytes( xReturn );
1122 /*-----------------------------------------------------------*/
1124 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
1126 size_t xBufferLengthBytes,
1127 BaseType_t * const pxHigherPriorityTaskWoken )
1129 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1130 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1132 traceENTER_xStreamBufferReceiveFromISR( xStreamBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken );
1134 configASSERT( pvRxData );
1135 configASSERT( pxStreamBuffer );
1137 /* This receive function is used by both message buffers, which store
1138 * discrete messages, and stream buffers, which store a continuous stream of
1139 * bytes. Discrete messages include an additional
1140 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1142 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1144 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1148 xBytesToStoreMessageLength = 0;
1151 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1153 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1154 * holds the number of bytes used to store the message length) or a stream of
1155 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1156 * available must be greater than xBytesToStoreMessageLength to be able to
1157 * read bytes from the buffer. */
1158 if( xBytesAvailable > xBytesToStoreMessageLength )
1160 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1162 /* Was a task waiting for space in the buffer? */
1163 if( xReceivedLength != ( size_t ) 0 )
1165 prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
1169 mtCOVERAGE_TEST_MARKER();
1174 mtCOVERAGE_TEST_MARKER();
1177 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
1178 traceRETURN_xStreamBufferReceiveFromISR( xReceivedLength );
1180 return xReceivedLength;
1182 /*-----------------------------------------------------------*/
1184 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
1186 size_t xBufferLengthBytes,
1187 size_t xBytesAvailable )
1189 size_t xCount, xNextMessageLength;
1190 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
1191 size_t xNextTail = pxStreamBuffer->xTail;
1193 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1195 /* A discrete message is being received. First receive the length
1196 * of the message. */
1197 xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
1198 xNextMessageLength = ( size_t ) xTempNextMessageLength;
1200 /* Reduce the number of bytes available by the number of bytes just
1202 xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
1204 /* Check there is enough space in the buffer provided by the
1206 if( xNextMessageLength > xBufferLengthBytes )
1208 /* The user has provided insufficient space to read the message. */
1209 xNextMessageLength = 0;
1213 mtCOVERAGE_TEST_MARKER();
1218 /* A stream of bytes is being received (as opposed to a discrete
1219 * message), so read as many bytes as possible. */
1220 xNextMessageLength = xBufferLengthBytes;
1223 /* Use the minimum of the wanted bytes and the available bytes. */
1224 xCount = configMIN( xNextMessageLength, xBytesAvailable );
1226 if( xCount != ( size_t ) 0 )
1228 /* Read the actual data and update the tail to mark the data as officially consumed. */
1229 /* MISRA Ref 11.5.5 [Void pointer assignment] */
1230 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1231 /* coverity[misra_c_2012_rule_11_5_violation] */
1232 pxStreamBuffer->xTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xCount, xNextTail );
1237 /*-----------------------------------------------------------*/
1239 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1241 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1245 traceENTER_xStreamBufferIsEmpty( xStreamBuffer );
1247 configASSERT( pxStreamBuffer );
1249 /* True if no bytes are available. */
1250 xTail = pxStreamBuffer->xTail;
1252 if( pxStreamBuffer->xHead == xTail )
1261 traceRETURN_xStreamBufferIsEmpty( xReturn );
1265 /*-----------------------------------------------------------*/
1267 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1270 size_t xBytesToStoreMessageLength;
1271 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1273 traceENTER_xStreamBufferIsFull( xStreamBuffer );
1275 configASSERT( pxStreamBuffer );
1277 /* This generic version of the receive function is used by both message
1278 * buffers, which store discrete messages, and stream buffers, which store a
1279 * continuous stream of bytes. Discrete messages include an additional
1280 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1281 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1283 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1287 xBytesToStoreMessageLength = 0;
1290 /* True if the available space equals zero. */
1291 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1300 traceRETURN_xStreamBufferIsFull( xReturn );
1304 /*-----------------------------------------------------------*/
1306 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1307 BaseType_t * pxHigherPriorityTaskWoken )
1309 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1311 UBaseType_t uxSavedInterruptStatus;
1313 traceENTER_xStreamBufferSendCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1315 configASSERT( pxStreamBuffer );
1317 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1319 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1321 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1322 ( pxStreamBuffer )->uxNotificationIndex,
1325 pxHigherPriorityTaskWoken );
1326 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1334 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1336 traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
1340 /*-----------------------------------------------------------*/
1342 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1343 BaseType_t * pxHigherPriorityTaskWoken )
1345 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1347 UBaseType_t uxSavedInterruptStatus;
1349 traceENTER_xStreamBufferReceiveCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1351 configASSERT( pxStreamBuffer );
1353 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1355 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1357 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1358 ( pxStreamBuffer )->uxNotificationIndex,
1361 pxHigherPriorityTaskWoken );
1362 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1370 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1372 traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
1376 /*-----------------------------------------------------------*/
1378 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1379 const uint8_t * pucData,
1383 size_t xFirstLength;
1385 configASSERT( xCount > ( size_t ) 0 );
1387 /* Calculate the number of bytes that can be added in the first write -
1388 * which may be less than the total number of bytes that need to be added if
1389 * the buffer will wrap back to the beginning. */
1390 xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
1392 /* Write as many bytes as can be written in the first write. */
1393 configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
1394 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength );
1396 /* If the number of bytes written was less than the number that could be
1397 * written in the first write... */
1398 if( xCount > xFirstLength )
1400 /* ...then write the remaining bytes to the start of the buffer. */
1401 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1402 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength );
1406 mtCOVERAGE_TEST_MARKER();
1411 if( xHead >= pxStreamBuffer->xLength )
1413 xHead -= pxStreamBuffer->xLength;
1417 mtCOVERAGE_TEST_MARKER();
1422 /*-----------------------------------------------------------*/
1424 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1429 size_t xFirstLength;
1431 configASSERT( xCount != ( size_t ) 0 );
1433 /* Calculate the number of bytes that can be read - which may be
1434 * less than the number wanted if the data wraps around to the start of
1436 xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
1438 /* Obtain the number of bytes it is possible to obtain in the first
1439 * read. Asserts check bounds of read and write. */
1440 configASSERT( xFirstLength <= xCount );
1441 configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
1442 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength );
1444 /* If the total number of wanted bytes is greater than the number
1445 * that could be read in the first read... */
1446 if( xCount > xFirstLength )
1448 /* ...then read the remaining bytes from the start of the buffer. */
1449 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength );
1453 mtCOVERAGE_TEST_MARKER();
1456 /* Move the tail pointer to effectively remove the data read from the buffer. */
1459 if( xTail >= pxStreamBuffer->xLength )
1461 xTail -= pxStreamBuffer->xLength;
1466 /*-----------------------------------------------------------*/
1468 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1470 /* Returns the distance between xTail and xHead. */
1473 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1474 xCount -= pxStreamBuffer->xTail;
1476 if( xCount >= pxStreamBuffer->xLength )
1478 xCount -= pxStreamBuffer->xLength;
1482 mtCOVERAGE_TEST_MARKER();
1487 /*-----------------------------------------------------------*/
1489 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1490 uint8_t * const pucBuffer,
1491 size_t xBufferSizeBytes,
1492 size_t xTriggerLevelBytes,
1494 StreamBufferCallbackFunction_t pxSendCompletedCallback,
1495 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
1497 /* Assert here is deliberately writing to the entire buffer to ensure it can
1498 * be written to without generating exceptions, and is setting the buffer to a
1499 * known value to assist in development/debugging. */
1500 #if ( configASSERT_DEFINED == 1 )
1502 /* The value written just has to be identifiable when looking at the
1503 * memory. Don't use 0xA5 as that is the stack fill value and could
1504 * result in confusion as to what is actually being observed. */
1505 #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
1506 configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
1510 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
1511 pxStreamBuffer->pucBuffer = pucBuffer;
1512 pxStreamBuffer->xLength = xBufferSizeBytes;
1513 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1514 pxStreamBuffer->ucFlags = ucFlags;
1515 pxStreamBuffer->uxNotificationIndex = tskDEFAULT_INDEX_TO_NOTIFY;
1516 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
1518 pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
1519 pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
1523 /* MISRA Ref 11.1.1 [Object type casting] */
1524 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1525 /* coverity[misra_c_2012_rule_11_1_violation] */
1526 ( void ) pxSendCompletedCallback;
1528 /* MISRA Ref 11.1.1 [Object type casting] */
1529 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1530 /* coverity[misra_c_2012_rule_11_1_violation] */
1531 ( void ) pxReceiveCompletedCallback;
1533 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
1535 /*-----------------------------------------------------------*/
1537 UBaseType_t uxStreamBufferGetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer )
1539 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1541 traceENTER_uxStreamBufferGetStreamBufferNotificationIndex( xStreamBuffer );
1543 configASSERT( pxStreamBuffer );
1545 traceRETURN_uxStreamBufferGetStreamBufferNotificationIndex( pxStreamBuffer->uxNotificationIndex );
1547 return pxStreamBuffer->uxNotificationIndex;
1549 /*-----------------------------------------------------------*/
1551 void vStreamBufferSetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer,
1552 UBaseType_t uxNotificationIndex )
1554 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1556 traceENTER_vStreamBufferSetStreamBufferNotificationIndex( xStreamBuffer, uxNotificationIndex );
1558 configASSERT( pxStreamBuffer );
1560 /* There should be no task waiting otherwise we'd never resume them. */
1561 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
1562 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
1564 /* Check that the task notification index is valid. */
1565 configASSERT( uxNotificationIndex < configTASK_NOTIFICATION_ARRAY_ENTRIES );
1567 pxStreamBuffer->uxNotificationIndex = uxNotificationIndex;
1569 traceRETURN_vStreamBufferSetStreamBufferNotificationIndex();
1571 /*-----------------------------------------------------------*/
1573 #if ( configUSE_TRACE_FACILITY == 1 )
1575 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1577 traceENTER_uxStreamBufferGetStreamBufferNumber( xStreamBuffer );
1579 traceRETURN_uxStreamBufferGetStreamBufferNumber( xStreamBuffer->uxStreamBufferNumber );
1581 return xStreamBuffer->uxStreamBufferNumber;
1584 #endif /* configUSE_TRACE_FACILITY */
1585 /*-----------------------------------------------------------*/
1587 #if ( configUSE_TRACE_FACILITY == 1 )
1589 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1590 UBaseType_t uxStreamBufferNumber )
1592 traceENTER_vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxStreamBufferNumber );
1594 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1596 traceRETURN_vStreamBufferSetStreamBufferNumber();
1599 #endif /* configUSE_TRACE_FACILITY */
1600 /*-----------------------------------------------------------*/
1602 #if ( configUSE_TRACE_FACILITY == 1 )
1604 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1606 traceENTER_ucStreamBufferGetStreamBufferType( xStreamBuffer );
1608 traceRETURN_ucStreamBufferGetStreamBufferType( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1610 return( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1613 #endif /* configUSE_TRACE_FACILITY */
1614 /*-----------------------------------------------------------*/
1616 /* This entire source file will be skipped if the application is not configured
1617 * to include stream buffer functionality. This #if is closed at the very bottom
1618 * of this file. If you want to include stream buffers then ensure
1619 * configUSE_STREAM_BUFFERS is set to 1 in FreeRTOSConfig.h. */
1620 #endif /* configUSE_STREAM_BUFFERS == 1 */