2 * FreeRTOS Kernel V11.0.1
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 /* If the user has not provided application specific Rx notification macros,
56 * or #defined the notification macros away, then provide default implementations
57 * that uses task notifications. */
58 #ifndef sbRECEIVE_COMPLETED
59 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
64 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
66 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
69 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
72 ( void ) xTaskResumeAll(); \
74 #endif /* sbRECEIVE_COMPLETED */
76 /* If user has provided a per-instance receive complete callback, then
77 * invoke the callback else use the receive complete macro which is provided by default for all instances.
79 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
80 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) \
82 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
84 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
88 sbRECEIVE_COMPLETED( ( pxStreamBuffer ) ); \
91 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
92 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) sbRECEIVE_COMPLETED( ( pxStreamBuffer ) )
93 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
95 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
96 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
97 pxHigherPriorityTaskWoken ) \
99 UBaseType_t uxSavedInterruptStatus; \
101 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
103 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
105 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
108 ( pxHigherPriorityTaskWoken ) ); \
109 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
112 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
114 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
116 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
117 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
118 pxHigherPriorityTaskWoken ) \
120 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
122 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
126 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
129 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
130 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
131 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
132 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
134 /* If the user has not provided an application specific Tx notification macro,
135 * or #defined the notification macro away, then provide a default
136 * implementation that uses task notifications.
138 #ifndef sbSEND_COMPLETED
139 #define sbSEND_COMPLETED( pxStreamBuffer ) \
142 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
144 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
147 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
150 ( void ) xTaskResumeAll()
151 #endif /* sbSEND_COMPLETED */
153 /* If user has provided a per-instance send completed callback, then
154 * invoke the callback else use the send complete macro which is provided by default for all instances.
156 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
157 #define prvSEND_COMPLETED( pxStreamBuffer ) \
159 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
161 ( pxStreamBuffer )->pxSendCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
165 sbSEND_COMPLETED( ( pxStreamBuffer ) ); \
168 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
169 #define prvSEND_COMPLETED( pxStreamBuffer ) sbSEND_COMPLETED( ( pxStreamBuffer ) )
170 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
173 #ifndef sbSEND_COMPLETE_FROM_ISR
174 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
176 UBaseType_t uxSavedInterruptStatus; \
178 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
180 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
182 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
185 ( pxHigherPriorityTaskWoken ) ); \
186 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
189 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
191 #endif /* sbSEND_COMPLETE_FROM_ISR */
194 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
195 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
197 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
199 ( pxStreamBuffer )->pxSendCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
203 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
206 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
207 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
208 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
209 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
211 /* The number of bytes used to hold the length of a message in the buffer. */
212 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
214 /* Bits stored in the ucFlags field of the stream buffer. */
215 #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. */
216 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
218 /*-----------------------------------------------------------*/
220 /* Structure that hold state information on the buffer. */
221 typedef struct StreamBufferDef_t
223 volatile size_t xTail; /* Index to the next item to read within the buffer. */
224 volatile size_t xHead; /* Index to the next item to write within the buffer. */
225 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
226 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
227 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
228 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
229 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
232 #if ( configUSE_TRACE_FACILITY == 1 )
233 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
236 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
237 StreamBufferCallbackFunction_t pxSendCompletedCallback; /* Optional callback called on send complete. sbSEND_COMPLETED is called if this is NULL. */
238 StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
243 * The number of bytes available to be read from the buffer.
245 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
248 * Add xCount bytes from pucData into the pxStreamBuffer's data storage area.
249 * This function does not update the buffer's xHead pointer, so multiple writes
250 * may be chained together "atomically". This is useful for Message Buffers where
251 * the length and data bytes are written in two separate chunks, and we don't want
252 * the reader to see the buffer as having grown until after all data is copied over.
253 * This function takes a custom xHead value to indicate where to write to (necessary
254 * for chaining) and returns the the resulting xHead position.
255 * To mark the write as complete, manually set the buffer's xHead field with the
256 * returned xHead from this function.
258 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
259 const uint8_t * pucData,
261 size_t xHead ) PRIVILEGED_FUNCTION;
264 * If the stream buffer is being used as a message buffer, then reads an entire
265 * message out of the buffer. If the stream buffer is being used as a stream
266 * buffer then read as many bytes as possible from the buffer.
267 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
268 * buffer's data storage area.
270 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
272 size_t xBufferLengthBytes,
273 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
276 * If the stream buffer is being used as a message buffer, then writes an entire
277 * message to the buffer. If the stream buffer is being used as a stream
278 * buffer then write as many bytes as possible to the buffer.
279 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
282 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
283 const void * pvTxData,
284 size_t xDataLengthBytes,
286 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
289 * Copies xCount bytes from the pxStreamBuffer's data storage area to pucData.
290 * This function does not update the buffer's xTail pointer, so multiple reads
291 * may be chained together "atomically". This is useful for Message Buffers where
292 * the length and data bytes are read in two separate chunks, and we don't want
293 * the writer to see the buffer as having more free space until after all data is
294 * copied over, especially if we have to abort the read due to insufficient receiving space.
295 * This function takes a custom xTail value to indicate where to read from (necessary
296 * for chaining) and returns the the resulting xTail position.
297 * To mark the read as complete, manually set the buffer's xTail field with the
298 * returned xTail from this function.
300 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
303 size_t xTail ) PRIVILEGED_FUNCTION;
306 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
307 * initialise the members of the newly created stream buffer structure.
309 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
310 uint8_t * const pucBuffer,
311 size_t xBufferSizeBytes,
312 size_t xTriggerLevelBytes,
314 StreamBufferCallbackFunction_t pxSendCompletedCallback,
315 StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
317 /*-----------------------------------------------------------*/
318 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
319 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
320 size_t xTriggerLevelBytes,
321 BaseType_t xIsMessageBuffer,
322 StreamBufferCallbackFunction_t pxSendCompletedCallback,
323 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
325 void * pvAllocatedMemory;
328 traceENTER_xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
330 /* In case the stream buffer is going to be used as a message buffer
331 * (that is, it will hold discrete messages with a little meta data that
332 * says how big the next message is) check the buffer will be large enough
333 * to hold at least one message. */
334 if( xIsMessageBuffer == pdTRUE )
336 /* Is a message buffer but not statically allocated. */
337 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
338 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
342 /* Not a message buffer and not statically allocated. */
344 configASSERT( xBufferSizeBytes > 0 );
347 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
349 /* A trigger level of 0 would cause a waiting task to unblock even when
350 * the buffer was empty. */
351 if( xTriggerLevelBytes == ( size_t ) 0 )
353 xTriggerLevelBytes = ( size_t ) 1;
356 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
357 * Both are allocated in a single call to pvPortMalloc(). The
358 * StreamBuffer_t structure is placed at the start of the allocated memory
359 * and the buffer follows immediately after. The requested size is
360 * incremented so the free space is returned as the user would expect -
361 * this is a quirk of the implementation that means otherwise the free
362 * space would be reported as one byte smaller than would be logically
364 if( xBufferSizeBytes < ( xBufferSizeBytes + 1U + sizeof( StreamBuffer_t ) ) )
367 pvAllocatedMemory = pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) );
371 pvAllocatedMemory = NULL;
374 if( pvAllocatedMemory != NULL )
376 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
377 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
378 /* coverity[misra_c_2012_rule_11_5_violation] */
379 prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pvAllocatedMemory, /* Structure at the start of the allocated memory. */
380 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
381 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
382 /* coverity[misra_c_2012_rule_11_5_violation] */
383 ( ( uint8_t * ) pvAllocatedMemory ) + sizeof( StreamBuffer_t ), /* Storage area follows. */
387 pxSendCompletedCallback,
388 pxReceiveCompletedCallback );
390 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xIsMessageBuffer );
394 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
397 traceRETURN_xStreamBufferGenericCreate( pvAllocatedMemory );
399 /* MISRA Ref 11.5.1 [Malloc memory assignment] */
400 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
401 /* coverity[misra_c_2012_rule_11_5_violation] */
402 return ( StreamBufferHandle_t ) pvAllocatedMemory;
404 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
405 /*-----------------------------------------------------------*/
407 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
409 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
410 size_t xTriggerLevelBytes,
411 BaseType_t xIsMessageBuffer,
412 uint8_t * const pucStreamBufferStorageArea,
413 StaticStreamBuffer_t * const pxStaticStreamBuffer,
414 StreamBufferCallbackFunction_t pxSendCompletedCallback,
415 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
417 /* MISRA Ref 11.3.1 [Misaligned access] */
418 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
419 /* coverity[misra_c_2012_rule_11_3_violation] */
420 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer;
421 StreamBufferHandle_t xReturn;
424 traceENTER_xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
426 configASSERT( pucStreamBufferStorageArea );
427 configASSERT( pxStaticStreamBuffer );
428 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
430 /* A trigger level of 0 would cause a waiting task to unblock even when
431 * the buffer was empty. */
432 if( xTriggerLevelBytes == ( size_t ) 0 )
434 xTriggerLevelBytes = ( size_t ) 1;
437 /* In case the stream buffer is going to be used as a message buffer
438 * (that is, it will hold discrete messages with a little meta data that
439 * says how big the next message is) check the buffer will be large enough
440 * to hold at least one message. */
442 if( xIsMessageBuffer != pdFALSE )
444 /* Statically allocated message buffer. */
445 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
446 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
450 /* Statically allocated stream buffer. */
451 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
454 #if ( configASSERT_DEFINED == 1 )
456 /* Sanity check that the size of the structure used to declare a
457 * variable of type StaticStreamBuffer_t equals the size of the real
458 * message buffer structure. */
459 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
460 configASSERT( xSize == sizeof( StreamBuffer_t ) );
462 #endif /* configASSERT_DEFINED */
464 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
466 prvInitialiseNewStreamBuffer( pxStreamBuffer,
467 pucStreamBufferStorageArea,
471 pxSendCompletedCallback,
472 pxReceiveCompletedCallback );
474 /* Remember this was statically allocated in case it is ever deleted
476 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
478 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
480 /* MISRA Ref 11.3.1 [Misaligned access] */
481 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
482 /* coverity[misra_c_2012_rule_11_3_violation] */
483 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer;
488 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
491 traceRETURN_xStreamBufferGenericCreateStatic( xReturn );
495 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
496 /*-----------------------------------------------------------*/
498 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
499 BaseType_t xStreamBufferGetStaticBuffers( StreamBufferHandle_t xStreamBuffer,
500 uint8_t ** ppucStreamBufferStorageArea,
501 StaticStreamBuffer_t ** ppxStaticStreamBuffer )
504 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
506 traceENTER_xStreamBufferGetStaticBuffers( xStreamBuffer, ppucStreamBufferStorageArea, ppxStaticStreamBuffer );
508 configASSERT( pxStreamBuffer );
509 configASSERT( ppucStreamBufferStorageArea );
510 configASSERT( ppxStaticStreamBuffer );
512 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) != ( uint8_t ) 0 )
514 *ppucStreamBufferStorageArea = pxStreamBuffer->pucBuffer;
515 /* MISRA Ref 11.3.1 [Misaligned access] */
516 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
517 /* coverity[misra_c_2012_rule_11_3_violation] */
518 *ppxStaticStreamBuffer = ( StaticStreamBuffer_t * ) pxStreamBuffer;
526 traceRETURN_xStreamBufferGetStaticBuffers( xReturn );
530 #endif /* configSUPPORT_STATIC_ALLOCATION */
531 /*-----------------------------------------------------------*/
533 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
535 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
537 traceENTER_vStreamBufferDelete( xStreamBuffer );
539 configASSERT( pxStreamBuffer );
541 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
543 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
545 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
547 /* Both the structure and the buffer were allocated using a single call
548 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
549 vPortFree( ( void * ) pxStreamBuffer );
553 /* Should not be possible to get here, ucFlags must be corrupt.
554 * Force an assert. */
555 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
561 /* The structure and buffer were not allocated dynamically and cannot be
562 * freed - just scrub the structure so future use will assert. */
563 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
566 traceRETURN_vStreamBufferDelete();
568 /*-----------------------------------------------------------*/
570 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
572 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
573 BaseType_t xReturn = pdFAIL;
574 StreamBufferCallbackFunction_t pxSendCallback = NULL, pxReceiveCallback = NULL;
576 #if ( configUSE_TRACE_FACILITY == 1 )
577 UBaseType_t uxStreamBufferNumber;
580 traceENTER_xStreamBufferReset( xStreamBuffer );
582 configASSERT( pxStreamBuffer );
584 #if ( configUSE_TRACE_FACILITY == 1 )
586 /* Store the stream buffer number so it can be restored after the
588 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
592 /* Can only reset a message buffer if there are no tasks blocked on it. */
593 taskENTER_CRITICAL();
595 if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
597 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
599 pxSendCallback = pxStreamBuffer->pxSendCompletedCallback;
600 pxReceiveCallback = pxStreamBuffer->pxReceiveCompletedCallback;
604 prvInitialiseNewStreamBuffer( pxStreamBuffer,
605 pxStreamBuffer->pucBuffer,
606 pxStreamBuffer->xLength,
607 pxStreamBuffer->xTriggerLevelBytes,
608 pxStreamBuffer->ucFlags,
612 #if ( configUSE_TRACE_FACILITY == 1 )
614 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
618 traceSTREAM_BUFFER_RESET( xStreamBuffer );
625 traceRETURN_xStreamBufferReset( xReturn );
629 /*-----------------------------------------------------------*/
631 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
632 size_t xTriggerLevel )
634 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
637 traceENTER_xStreamBufferSetTriggerLevel( xStreamBuffer, xTriggerLevel );
639 configASSERT( pxStreamBuffer );
641 /* It is not valid for the trigger level to be 0. */
642 if( xTriggerLevel == ( size_t ) 0 )
644 xTriggerLevel = ( size_t ) 1;
647 /* The trigger level is the number of bytes that must be in the stream
648 * buffer before a task that is waiting for data is unblocked. */
649 if( xTriggerLevel < pxStreamBuffer->xLength )
651 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
659 traceRETURN_xStreamBufferSetTriggerLevel( xReturn );
663 /*-----------------------------------------------------------*/
665 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
667 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
669 size_t xOriginalTail;
671 traceENTER_xStreamBufferSpacesAvailable( xStreamBuffer );
673 configASSERT( pxStreamBuffer );
675 /* The code below reads xTail and then xHead. This is safe if the stream
676 * buffer is updated once between the two reads - but not if the stream buffer
677 * is updated more than once between the two reads - hence the loop. */
680 xOriginalTail = pxStreamBuffer->xTail;
681 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
682 xSpace -= pxStreamBuffer->xHead;
683 } while( xOriginalTail != pxStreamBuffer->xTail );
685 xSpace -= ( size_t ) 1;
687 if( xSpace >= pxStreamBuffer->xLength )
689 xSpace -= pxStreamBuffer->xLength;
693 mtCOVERAGE_TEST_MARKER();
696 traceRETURN_xStreamBufferSpacesAvailable( xSpace );
700 /*-----------------------------------------------------------*/
702 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
704 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
707 traceENTER_xStreamBufferBytesAvailable( xStreamBuffer );
709 configASSERT( pxStreamBuffer );
711 xReturn = prvBytesInBuffer( pxStreamBuffer );
713 traceRETURN_xStreamBufferBytesAvailable( xReturn );
717 /*-----------------------------------------------------------*/
719 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
720 const void * pvTxData,
721 size_t xDataLengthBytes,
722 TickType_t xTicksToWait )
724 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
725 size_t xReturn, xSpace = 0;
726 size_t xRequiredSpace = xDataLengthBytes;
728 size_t xMaxReportedSpace = 0;
730 traceENTER_xStreamBufferSend( xStreamBuffer, pvTxData, xDataLengthBytes, xTicksToWait );
732 configASSERT( pvTxData );
733 configASSERT( pxStreamBuffer );
735 /* The maximum amount of space a stream buffer will ever report is its length
737 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
739 /* This send function is used to write to both message buffers and stream
740 * buffers. If this is a message buffer then the space needed must be
741 * increased by the amount of bytes needed to store the length of the
743 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
745 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
748 configASSERT( xRequiredSpace > xDataLengthBytes );
750 /* If this is a message buffer then it must be possible to write the
752 if( xRequiredSpace > xMaxReportedSpace )
754 /* The message would not fit even if the entire buffer was empty,
755 * so don't wait for space. */
756 xTicksToWait = ( TickType_t ) 0;
760 mtCOVERAGE_TEST_MARKER();
765 /* If this is a stream buffer then it is acceptable to write only part
766 * of the message to the buffer. Cap the length to the total length of
768 if( xRequiredSpace > xMaxReportedSpace )
770 xRequiredSpace = xMaxReportedSpace;
774 mtCOVERAGE_TEST_MARKER();
778 if( xTicksToWait != ( TickType_t ) 0 )
780 vTaskSetTimeOutState( &xTimeOut );
784 /* Wait until the required number of bytes are free in the message
786 taskENTER_CRITICAL();
788 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
790 if( xSpace < xRequiredSpace )
792 /* Clear notification state as going to wait for space. */
793 ( void ) xTaskNotifyStateClear( NULL );
795 /* Should only be one writer. */
796 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
797 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
807 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
808 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
809 pxStreamBuffer->xTaskWaitingToSend = NULL;
810 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
814 mtCOVERAGE_TEST_MARKER();
817 if( xSpace == ( size_t ) 0 )
819 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
823 mtCOVERAGE_TEST_MARKER();
826 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
828 if( xReturn > ( size_t ) 0 )
830 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
832 /* Was a task waiting for the data? */
833 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
835 prvSEND_COMPLETED( pxStreamBuffer );
839 mtCOVERAGE_TEST_MARKER();
844 mtCOVERAGE_TEST_MARKER();
845 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
848 traceRETURN_xStreamBufferSend( xReturn );
852 /*-----------------------------------------------------------*/
854 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
855 const void * pvTxData,
856 size_t xDataLengthBytes,
857 BaseType_t * const pxHigherPriorityTaskWoken )
859 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
860 size_t xReturn, xSpace;
861 size_t xRequiredSpace = xDataLengthBytes;
863 traceENTER_xStreamBufferSendFromISR( xStreamBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken );
865 configASSERT( pvTxData );
866 configASSERT( pxStreamBuffer );
868 /* This send function is used to write to both message buffers and stream
869 * buffers. If this is a message buffer then the space needed must be
870 * increased by the amount of bytes needed to store the length of the
872 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
874 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
878 mtCOVERAGE_TEST_MARKER();
881 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
882 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
884 if( xReturn > ( size_t ) 0 )
886 /* Was a task waiting for the data? */
887 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
889 prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
893 mtCOVERAGE_TEST_MARKER();
898 mtCOVERAGE_TEST_MARKER();
901 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
902 traceRETURN_xStreamBufferSendFromISR( xReturn );
906 /*-----------------------------------------------------------*/
908 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
909 const void * pvTxData,
910 size_t xDataLengthBytes,
912 size_t xRequiredSpace )
914 size_t xNextHead = pxStreamBuffer->xHead;
915 configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
917 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
919 /* This is a message buffer, as opposed to a stream buffer. */
921 /* Convert xDataLengthBytes to the message length type. */
922 xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
924 /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
925 configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
927 if( xSpace >= xRequiredSpace )
929 /* There is enough space to write both the message length and the message
930 * itself into the buffer. Start by writing the length of the data, the data
931 * itself will be written later in this function. */
932 xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
936 /* Not enough space, so do not write data to the buffer. */
937 xDataLengthBytes = 0;
942 /* This is a stream buffer, as opposed to a message buffer, so writing a
943 * stream of bytes rather than discrete messages. Plan to write as many
944 * bytes as possible. */
945 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
948 if( xDataLengthBytes != ( size_t ) 0 )
950 /* Write the data to the buffer. */
951 /* MISRA Ref 11.5.5 [Void pointer assignment] */
952 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
953 /* coverity[misra_c_2012_rule_11_5_violation] */
954 pxStreamBuffer->xHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes, xNextHead );
957 return xDataLengthBytes;
959 /*-----------------------------------------------------------*/
961 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
963 size_t xBufferLengthBytes,
964 TickType_t xTicksToWait )
966 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
967 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
969 traceENTER_xStreamBufferReceive( xStreamBuffer, pvRxData, xBufferLengthBytes, xTicksToWait );
971 configASSERT( pvRxData );
972 configASSERT( pxStreamBuffer );
974 /* This receive function is used by both message buffers, which store
975 * discrete messages, and stream buffers, which store a continuous stream of
976 * bytes. Discrete messages include an additional
977 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
979 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
981 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
985 xBytesToStoreMessageLength = 0;
988 if( xTicksToWait != ( TickType_t ) 0 )
990 /* Checking if there is data and clearing the notification state must be
991 * performed atomically. */
992 taskENTER_CRITICAL();
994 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
996 /* If this function was invoked by a message buffer read then
997 * xBytesToStoreMessageLength holds the number of bytes used to hold
998 * the length of the next discrete message. If this function was
999 * invoked by a stream buffer read then xBytesToStoreMessageLength will
1001 if( xBytesAvailable <= xBytesToStoreMessageLength )
1003 /* Clear notification state as going to wait for data. */
1004 ( void ) xTaskNotifyStateClear( NULL );
1006 /* Should only be one reader. */
1007 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
1008 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
1012 mtCOVERAGE_TEST_MARKER();
1015 taskEXIT_CRITICAL();
1017 if( xBytesAvailable <= xBytesToStoreMessageLength )
1019 /* Wait for data to be available. */
1020 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
1021 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
1022 pxStreamBuffer->xTaskWaitingToReceive = NULL;
1024 /* Recheck the data available after blocking. */
1025 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1029 mtCOVERAGE_TEST_MARKER();
1034 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1037 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1038 * holds the number of bytes used to store the message length) or a stream of
1039 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1040 * available must be greater than xBytesToStoreMessageLength to be able to
1041 * read bytes from the buffer. */
1042 if( xBytesAvailable > xBytesToStoreMessageLength )
1044 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1046 /* Was a task waiting for space in the buffer? */
1047 if( xReceivedLength != ( size_t ) 0 )
1049 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
1050 prvRECEIVE_COMPLETED( xStreamBuffer );
1054 mtCOVERAGE_TEST_MARKER();
1059 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
1060 mtCOVERAGE_TEST_MARKER();
1063 traceRETURN_xStreamBufferReceive( xReceivedLength );
1065 return xReceivedLength;
1067 /*-----------------------------------------------------------*/
1069 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
1071 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1072 size_t xReturn, xBytesAvailable;
1073 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
1075 traceENTER_xStreamBufferNextMessageLengthBytes( xStreamBuffer );
1077 configASSERT( pxStreamBuffer );
1079 /* Ensure the stream buffer is being used as a message buffer. */
1080 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1082 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1084 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
1086 /* The number of bytes available is greater than the number of bytes
1087 * required to hold the length of the next message, so another message
1089 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
1090 xReturn = ( size_t ) xTempReturn;
1094 /* The minimum amount of bytes in a message buffer is
1095 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
1096 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
1098 configASSERT( xBytesAvailable == 0 );
1107 traceRETURN_xStreamBufferNextMessageLengthBytes( xReturn );
1111 /*-----------------------------------------------------------*/
1113 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
1115 size_t xBufferLengthBytes,
1116 BaseType_t * const pxHigherPriorityTaskWoken )
1118 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1119 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1121 traceENTER_xStreamBufferReceiveFromISR( xStreamBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken );
1123 configASSERT( pvRxData );
1124 configASSERT( pxStreamBuffer );
1126 /* This receive function is used by both message buffers, which store
1127 * discrete messages, and stream buffers, which store a continuous stream of
1128 * bytes. Discrete messages include an additional
1129 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1131 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1133 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1137 xBytesToStoreMessageLength = 0;
1140 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1142 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1143 * holds the number of bytes used to store the message length) or a stream of
1144 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1145 * available must be greater than xBytesToStoreMessageLength to be able to
1146 * read bytes from the buffer. */
1147 if( xBytesAvailable > xBytesToStoreMessageLength )
1149 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1151 /* Was a task waiting for space in the buffer? */
1152 if( xReceivedLength != ( size_t ) 0 )
1154 prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
1158 mtCOVERAGE_TEST_MARKER();
1163 mtCOVERAGE_TEST_MARKER();
1166 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
1167 traceRETURN_xStreamBufferReceiveFromISR( xReceivedLength );
1169 return xReceivedLength;
1171 /*-----------------------------------------------------------*/
1173 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
1175 size_t xBufferLengthBytes,
1176 size_t xBytesAvailable )
1178 size_t xCount, xNextMessageLength;
1179 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
1180 size_t xNextTail = pxStreamBuffer->xTail;
1182 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1184 /* A discrete message is being received. First receive the length
1185 * of the message. */
1186 xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
1187 xNextMessageLength = ( size_t ) xTempNextMessageLength;
1189 /* Reduce the number of bytes available by the number of bytes just
1191 xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
1193 /* Check there is enough space in the buffer provided by the
1195 if( xNextMessageLength > xBufferLengthBytes )
1197 /* The user has provided insufficient space to read the message. */
1198 xNextMessageLength = 0;
1202 mtCOVERAGE_TEST_MARKER();
1207 /* A stream of bytes is being received (as opposed to a discrete
1208 * message), so read as many bytes as possible. */
1209 xNextMessageLength = xBufferLengthBytes;
1212 /* Use the minimum of the wanted bytes and the available bytes. */
1213 xCount = configMIN( xNextMessageLength, xBytesAvailable );
1215 if( xCount != ( size_t ) 0 )
1217 /* Read the actual data and update the tail to mark the data as officially consumed. */
1218 /* MISRA Ref 11.5.5 [Void pointer assignment] */
1219 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1220 /* coverity[misra_c_2012_rule_11_5_violation] */
1221 pxStreamBuffer->xTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xCount, xNextTail );
1226 /*-----------------------------------------------------------*/
1228 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1230 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1234 traceENTER_xStreamBufferIsEmpty( xStreamBuffer );
1236 configASSERT( pxStreamBuffer );
1238 /* True if no bytes are available. */
1239 xTail = pxStreamBuffer->xTail;
1241 if( pxStreamBuffer->xHead == xTail )
1250 traceRETURN_xStreamBufferIsEmpty( xReturn );
1254 /*-----------------------------------------------------------*/
1256 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1259 size_t xBytesToStoreMessageLength;
1260 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1262 traceENTER_xStreamBufferIsFull( xStreamBuffer );
1264 configASSERT( pxStreamBuffer );
1266 /* This generic version of the receive function is used by both message
1267 * buffers, which store discrete messages, and stream buffers, which store a
1268 * continuous stream of bytes. Discrete messages include an additional
1269 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1270 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1272 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1276 xBytesToStoreMessageLength = 0;
1279 /* True if the available space equals zero. */
1280 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1289 traceRETURN_xStreamBufferIsFull( xReturn );
1293 /*-----------------------------------------------------------*/
1295 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1296 BaseType_t * pxHigherPriorityTaskWoken )
1298 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1300 UBaseType_t uxSavedInterruptStatus;
1302 traceENTER_xStreamBufferSendCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1304 configASSERT( pxStreamBuffer );
1306 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1308 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1310 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1313 pxHigherPriorityTaskWoken );
1314 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1322 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1324 traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
1328 /*-----------------------------------------------------------*/
1330 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1331 BaseType_t * pxHigherPriorityTaskWoken )
1333 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1335 UBaseType_t uxSavedInterruptStatus;
1337 traceENTER_xStreamBufferReceiveCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1339 configASSERT( pxStreamBuffer );
1341 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1343 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1345 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1348 pxHigherPriorityTaskWoken );
1349 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1357 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1359 traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
1363 /*-----------------------------------------------------------*/
1365 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1366 const uint8_t * pucData,
1370 size_t xFirstLength;
1372 configASSERT( xCount > ( size_t ) 0 );
1374 /* Calculate the number of bytes that can be added in the first write -
1375 * which may be less than the total number of bytes that need to be added if
1376 * the buffer will wrap back to the beginning. */
1377 xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
1379 /* Write as many bytes as can be written in the first write. */
1380 configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
1381 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength );
1383 /* If the number of bytes written was less than the number that could be
1384 * written in the first write... */
1385 if( xCount > xFirstLength )
1387 /* ...then write the remaining bytes to the start of the buffer. */
1388 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1389 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength );
1393 mtCOVERAGE_TEST_MARKER();
1398 if( xHead >= pxStreamBuffer->xLength )
1400 xHead -= pxStreamBuffer->xLength;
1404 mtCOVERAGE_TEST_MARKER();
1409 /*-----------------------------------------------------------*/
1411 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1416 size_t xFirstLength;
1418 configASSERT( xCount != ( size_t ) 0 );
1420 /* Calculate the number of bytes that can be read - which may be
1421 * less than the number wanted if the data wraps around to the start of
1423 xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
1425 /* Obtain the number of bytes it is possible to obtain in the first
1426 * read. Asserts check bounds of read and write. */
1427 configASSERT( xFirstLength <= xCount );
1428 configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
1429 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength );
1431 /* If the total number of wanted bytes is greater than the number
1432 * that could be read in the first read... */
1433 if( xCount > xFirstLength )
1435 /* ...then read the remaining bytes from the start of the buffer. */
1436 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength );
1440 mtCOVERAGE_TEST_MARKER();
1443 /* Move the tail pointer to effectively remove the data read from the buffer. */
1446 if( xTail >= pxStreamBuffer->xLength )
1448 xTail -= pxStreamBuffer->xLength;
1453 /*-----------------------------------------------------------*/
1455 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1457 /* Returns the distance between xTail and xHead. */
1460 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1461 xCount -= pxStreamBuffer->xTail;
1463 if( xCount >= pxStreamBuffer->xLength )
1465 xCount -= pxStreamBuffer->xLength;
1469 mtCOVERAGE_TEST_MARKER();
1474 /*-----------------------------------------------------------*/
1476 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1477 uint8_t * const pucBuffer,
1478 size_t xBufferSizeBytes,
1479 size_t xTriggerLevelBytes,
1481 StreamBufferCallbackFunction_t pxSendCompletedCallback,
1482 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
1484 /* Assert here is deliberately writing to the entire buffer to ensure it can
1485 * be written to without generating exceptions, and is setting the buffer to a
1486 * known value to assist in development/debugging. */
1487 #if ( configASSERT_DEFINED == 1 )
1489 /* The value written just has to be identifiable when looking at the
1490 * memory. Don't use 0xA5 as that is the stack fill value and could
1491 * result in confusion as to what is actually being observed. */
1492 #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
1493 configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
1497 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
1498 pxStreamBuffer->pucBuffer = pucBuffer;
1499 pxStreamBuffer->xLength = xBufferSizeBytes;
1500 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1501 pxStreamBuffer->ucFlags = ucFlags;
1502 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
1504 pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
1505 pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
1509 /* MISRA Ref 11.1.1 [Object type casting] */
1510 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1511 /* coverity[misra_c_2012_rule_11_1_violation] */
1512 ( void ) pxSendCompletedCallback;
1514 /* MISRA Ref 11.1.1 [Object type casting] */
1515 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1516 /* coverity[misra_c_2012_rule_11_1_violation] */
1517 ( void ) pxReceiveCompletedCallback;
1519 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
1522 #if ( configUSE_TRACE_FACILITY == 1 )
1524 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1526 traceENTER_uxStreamBufferGetStreamBufferNumber( xStreamBuffer );
1528 traceRETURN_uxStreamBufferGetStreamBufferNumber( xStreamBuffer->uxStreamBufferNumber );
1530 return xStreamBuffer->uxStreamBufferNumber;
1533 #endif /* configUSE_TRACE_FACILITY */
1534 /*-----------------------------------------------------------*/
1536 #if ( configUSE_TRACE_FACILITY == 1 )
1538 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1539 UBaseType_t uxStreamBufferNumber )
1541 traceENTER_vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxStreamBufferNumber );
1543 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1545 traceRETURN_vStreamBufferSetStreamBufferNumber();
1548 #endif /* configUSE_TRACE_FACILITY */
1549 /*-----------------------------------------------------------*/
1551 #if ( configUSE_TRACE_FACILITY == 1 )
1553 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1555 traceENTER_ucStreamBufferGetStreamBufferType( xStreamBuffer );
1557 traceRETURN_ucStreamBufferGetStreamBufferType( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1559 return( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1562 #endif /* configUSE_TRACE_FACILITY */
1563 /*-----------------------------------------------------------*/