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 xStreamBufferResetFromISR( StreamBufferHandle_t xStreamBuffer )
644 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
645 BaseType_t xReturn = pdFAIL;
646 StreamBufferCallbackFunction_t pxSendCallback = NULL, pxReceiveCallback = NULL;
647 UBaseType_t uxSavedInterruptStatus;
649 #if ( configUSE_TRACE_FACILITY == 1 )
650 UBaseType_t uxStreamBufferNumber;
653 traceENTER_xStreamBufferResetFromISR( xStreamBuffer );
655 configASSERT( pxStreamBuffer );
657 #if ( configUSE_TRACE_FACILITY == 1 )
659 /* Store the stream buffer number so it can be restored after the
661 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
665 /* Can only reset a message buffer if there are no tasks blocked on it. */
666 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
668 if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
670 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
672 pxSendCallback = pxStreamBuffer->pxSendCompletedCallback;
673 pxReceiveCallback = pxStreamBuffer->pxReceiveCompletedCallback;
677 prvInitialiseNewStreamBuffer( pxStreamBuffer,
678 pxStreamBuffer->pucBuffer,
679 pxStreamBuffer->xLength,
680 pxStreamBuffer->xTriggerLevelBytes,
681 pxStreamBuffer->ucFlags,
685 #if ( configUSE_TRACE_FACILITY == 1 )
687 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
691 traceSTREAM_BUFFER_RESET_FROM_ISR( xStreamBuffer );
696 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
698 traceRETURN_xStreamBufferResetFromISR( xReturn );
702 /*-----------------------------------------------------------*/
704 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
705 size_t xTriggerLevel )
707 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
710 traceENTER_xStreamBufferSetTriggerLevel( xStreamBuffer, xTriggerLevel );
712 configASSERT( pxStreamBuffer );
714 /* It is not valid for the trigger level to be 0. */
715 if( xTriggerLevel == ( size_t ) 0 )
717 xTriggerLevel = ( size_t ) 1;
720 /* The trigger level is the number of bytes that must be in the stream
721 * buffer before a task that is waiting for data is unblocked. */
722 if( xTriggerLevel < pxStreamBuffer->xLength )
724 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
732 traceRETURN_xStreamBufferSetTriggerLevel( xReturn );
736 /*-----------------------------------------------------------*/
738 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
740 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
742 size_t xOriginalTail;
744 traceENTER_xStreamBufferSpacesAvailable( xStreamBuffer );
746 configASSERT( pxStreamBuffer );
748 /* The code below reads xTail and then xHead. This is safe if the stream
749 * buffer is updated once between the two reads - but not if the stream buffer
750 * is updated more than once between the two reads - hence the loop. */
753 xOriginalTail = pxStreamBuffer->xTail;
754 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
755 xSpace -= pxStreamBuffer->xHead;
756 } while( xOriginalTail != pxStreamBuffer->xTail );
758 xSpace -= ( size_t ) 1;
760 if( xSpace >= pxStreamBuffer->xLength )
762 xSpace -= pxStreamBuffer->xLength;
766 mtCOVERAGE_TEST_MARKER();
769 traceRETURN_xStreamBufferSpacesAvailable( xSpace );
773 /*-----------------------------------------------------------*/
775 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
777 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
780 traceENTER_xStreamBufferBytesAvailable( xStreamBuffer );
782 configASSERT( pxStreamBuffer );
784 xReturn = prvBytesInBuffer( pxStreamBuffer );
786 traceRETURN_xStreamBufferBytesAvailable( xReturn );
790 /*-----------------------------------------------------------*/
792 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
793 const void * pvTxData,
794 size_t xDataLengthBytes,
795 TickType_t xTicksToWait )
797 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
798 size_t xReturn, xSpace = 0;
799 size_t xRequiredSpace = xDataLengthBytes;
801 size_t xMaxReportedSpace = 0;
803 traceENTER_xStreamBufferSend( xStreamBuffer, pvTxData, xDataLengthBytes, xTicksToWait );
805 configASSERT( pvTxData );
806 configASSERT( pxStreamBuffer );
808 /* The maximum amount of space a stream buffer will ever report is its length
810 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
812 /* This send function is used to write to both message buffers and stream
813 * buffers. If this is a message buffer then the space needed must be
814 * increased by the amount of bytes needed to store the length of the
816 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
818 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
821 configASSERT( xRequiredSpace > xDataLengthBytes );
823 /* If this is a message buffer then it must be possible to write the
825 if( xRequiredSpace > xMaxReportedSpace )
827 /* The message would not fit even if the entire buffer was empty,
828 * so don't wait for space. */
829 xTicksToWait = ( TickType_t ) 0;
833 mtCOVERAGE_TEST_MARKER();
838 /* If this is a stream buffer then it is acceptable to write only part
839 * of the message to the buffer. Cap the length to the total length of
841 if( xRequiredSpace > xMaxReportedSpace )
843 xRequiredSpace = xMaxReportedSpace;
847 mtCOVERAGE_TEST_MARKER();
851 if( xTicksToWait != ( TickType_t ) 0 )
853 vTaskSetTimeOutState( &xTimeOut );
857 /* Wait until the required number of bytes are free in the message
859 taskENTER_CRITICAL();
861 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
863 if( xSpace < xRequiredSpace )
865 /* Clear notification state as going to wait for space. */
866 ( void ) xTaskNotifyStateClearIndexed( NULL, pxStreamBuffer->uxNotificationIndex );
868 /* Should only be one writer. */
869 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
870 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
880 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
881 ( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
882 pxStreamBuffer->xTaskWaitingToSend = NULL;
883 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
887 mtCOVERAGE_TEST_MARKER();
890 if( xSpace == ( size_t ) 0 )
892 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
896 mtCOVERAGE_TEST_MARKER();
899 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
901 if( xReturn > ( size_t ) 0 )
903 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
905 /* Was a task waiting for the data? */
906 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
908 prvSEND_COMPLETED( pxStreamBuffer );
912 mtCOVERAGE_TEST_MARKER();
917 mtCOVERAGE_TEST_MARKER();
918 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
921 traceRETURN_xStreamBufferSend( xReturn );
925 /*-----------------------------------------------------------*/
927 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
928 const void * pvTxData,
929 size_t xDataLengthBytes,
930 BaseType_t * const pxHigherPriorityTaskWoken )
932 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
933 size_t xReturn, xSpace;
934 size_t xRequiredSpace = xDataLengthBytes;
936 traceENTER_xStreamBufferSendFromISR( xStreamBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken );
938 configASSERT( pvTxData );
939 configASSERT( pxStreamBuffer );
941 /* This send function is used to write to both message buffers and stream
942 * buffers. If this is a message buffer then the space needed must be
943 * increased by the amount of bytes needed to store the length of the
945 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
947 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
951 mtCOVERAGE_TEST_MARKER();
954 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
955 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
957 if( xReturn > ( size_t ) 0 )
959 /* Was a task waiting for the data? */
960 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
962 prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
966 mtCOVERAGE_TEST_MARKER();
971 mtCOVERAGE_TEST_MARKER();
974 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
975 traceRETURN_xStreamBufferSendFromISR( xReturn );
979 /*-----------------------------------------------------------*/
981 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
982 const void * pvTxData,
983 size_t xDataLengthBytes,
985 size_t xRequiredSpace )
987 size_t xNextHead = pxStreamBuffer->xHead;
988 configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
990 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
992 /* This is a message buffer, as opposed to a stream buffer. */
994 /* Convert xDataLengthBytes to the message length type. */
995 xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
997 /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
998 configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
1000 if( xSpace >= xRequiredSpace )
1002 /* There is enough space to write both the message length and the message
1003 * itself into the buffer. Start by writing the length of the data, the data
1004 * itself will be written later in this function. */
1005 xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
1009 /* Not enough space, so do not write data to the buffer. */
1010 xDataLengthBytes = 0;
1015 /* This is a stream buffer, as opposed to a message buffer, so writing a
1016 * stream of bytes rather than discrete messages. Plan to write as many
1017 * bytes as possible. */
1018 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
1021 if( xDataLengthBytes != ( size_t ) 0 )
1023 /* Write the data to the buffer. */
1024 /* MISRA Ref 11.5.5 [Void pointer assignment] */
1025 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1026 /* coverity[misra_c_2012_rule_11_5_violation] */
1027 pxStreamBuffer->xHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes, xNextHead );
1030 return xDataLengthBytes;
1032 /*-----------------------------------------------------------*/
1034 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
1036 size_t xBufferLengthBytes,
1037 TickType_t xTicksToWait )
1039 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1040 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1042 traceENTER_xStreamBufferReceive( xStreamBuffer, pvRxData, xBufferLengthBytes, xTicksToWait );
1044 configASSERT( pvRxData );
1045 configASSERT( pxStreamBuffer );
1047 /* This receive function is used by both message buffers, which store
1048 * discrete messages, and stream buffers, which store a continuous stream of
1049 * bytes. Discrete messages include an additional
1050 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1052 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1054 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1058 xBytesToStoreMessageLength = 0;
1061 if( xTicksToWait != ( TickType_t ) 0 )
1063 /* Checking if there is data and clearing the notification state must be
1064 * performed atomically. */
1065 taskENTER_CRITICAL();
1067 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1069 /* If this function was invoked by a message buffer read then
1070 * xBytesToStoreMessageLength holds the number of bytes used to hold
1071 * the length of the next discrete message. If this function was
1072 * invoked by a stream buffer read then xBytesToStoreMessageLength will
1074 if( xBytesAvailable <= xBytesToStoreMessageLength )
1076 /* Clear notification state as going to wait for data. */
1077 ( void ) xTaskNotifyStateClearIndexed( NULL, pxStreamBuffer->uxNotificationIndex );
1079 /* Should only be one reader. */
1080 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
1081 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
1085 mtCOVERAGE_TEST_MARKER();
1088 taskEXIT_CRITICAL();
1090 if( xBytesAvailable <= xBytesToStoreMessageLength )
1092 /* Wait for data to be available. */
1093 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
1094 ( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
1095 pxStreamBuffer->xTaskWaitingToReceive = NULL;
1097 /* Recheck the data available after blocking. */
1098 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1102 mtCOVERAGE_TEST_MARKER();
1107 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1110 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1111 * holds the number of bytes used to store the message length) or a stream of
1112 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1113 * available must be greater than xBytesToStoreMessageLength to be able to
1114 * read bytes from the buffer. */
1115 if( xBytesAvailable > xBytesToStoreMessageLength )
1117 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1119 /* Was a task waiting for space in the buffer? */
1120 if( xReceivedLength != ( size_t ) 0 )
1122 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
1123 prvRECEIVE_COMPLETED( xStreamBuffer );
1127 mtCOVERAGE_TEST_MARKER();
1132 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
1133 mtCOVERAGE_TEST_MARKER();
1136 traceRETURN_xStreamBufferReceive( xReceivedLength );
1138 return xReceivedLength;
1140 /*-----------------------------------------------------------*/
1142 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
1144 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1145 size_t xReturn, xBytesAvailable;
1146 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
1148 traceENTER_xStreamBufferNextMessageLengthBytes( xStreamBuffer );
1150 configASSERT( pxStreamBuffer );
1152 /* Ensure the stream buffer is being used as a message buffer. */
1153 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1155 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1157 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
1159 /* The number of bytes available is greater than the number of bytes
1160 * required to hold the length of the next message, so another message
1162 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
1163 xReturn = ( size_t ) xTempReturn;
1167 /* The minimum amount of bytes in a message buffer is
1168 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
1169 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
1171 configASSERT( xBytesAvailable == 0 );
1180 traceRETURN_xStreamBufferNextMessageLengthBytes( xReturn );
1184 /*-----------------------------------------------------------*/
1186 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
1188 size_t xBufferLengthBytes,
1189 BaseType_t * const pxHigherPriorityTaskWoken )
1191 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1192 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1194 traceENTER_xStreamBufferReceiveFromISR( xStreamBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken );
1196 configASSERT( pvRxData );
1197 configASSERT( pxStreamBuffer );
1199 /* This receive function is used by both message buffers, which store
1200 * discrete messages, and stream buffers, which store a continuous stream of
1201 * bytes. Discrete messages include an additional
1202 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1204 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1206 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1210 xBytesToStoreMessageLength = 0;
1213 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1215 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1216 * holds the number of bytes used to store the message length) or a stream of
1217 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1218 * available must be greater than xBytesToStoreMessageLength to be able to
1219 * read bytes from the buffer. */
1220 if( xBytesAvailable > xBytesToStoreMessageLength )
1222 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1224 /* Was a task waiting for space in the buffer? */
1225 if( xReceivedLength != ( size_t ) 0 )
1227 prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
1231 mtCOVERAGE_TEST_MARKER();
1236 mtCOVERAGE_TEST_MARKER();
1239 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
1240 traceRETURN_xStreamBufferReceiveFromISR( xReceivedLength );
1242 return xReceivedLength;
1244 /*-----------------------------------------------------------*/
1246 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
1248 size_t xBufferLengthBytes,
1249 size_t xBytesAvailable )
1251 size_t xCount, xNextMessageLength;
1252 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
1253 size_t xNextTail = pxStreamBuffer->xTail;
1255 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1257 /* A discrete message is being received. First receive the length
1258 * of the message. */
1259 xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
1260 xNextMessageLength = ( size_t ) xTempNextMessageLength;
1262 /* Reduce the number of bytes available by the number of bytes just
1264 xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
1266 /* Check there is enough space in the buffer provided by the
1268 if( xNextMessageLength > xBufferLengthBytes )
1270 /* The user has provided insufficient space to read the message. */
1271 xNextMessageLength = 0;
1275 mtCOVERAGE_TEST_MARKER();
1280 /* A stream of bytes is being received (as opposed to a discrete
1281 * message), so read as many bytes as possible. */
1282 xNextMessageLength = xBufferLengthBytes;
1285 /* Use the minimum of the wanted bytes and the available bytes. */
1286 xCount = configMIN( xNextMessageLength, xBytesAvailable );
1288 if( xCount != ( size_t ) 0 )
1290 /* Read the actual data and update the tail to mark the data as officially consumed. */
1291 /* MISRA Ref 11.5.5 [Void pointer assignment] */
1292 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
1293 /* coverity[misra_c_2012_rule_11_5_violation] */
1294 pxStreamBuffer->xTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xCount, xNextTail );
1299 /*-----------------------------------------------------------*/
1301 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1303 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1307 traceENTER_xStreamBufferIsEmpty( xStreamBuffer );
1309 configASSERT( pxStreamBuffer );
1311 /* True if no bytes are available. */
1312 xTail = pxStreamBuffer->xTail;
1314 if( pxStreamBuffer->xHead == xTail )
1323 traceRETURN_xStreamBufferIsEmpty( xReturn );
1327 /*-----------------------------------------------------------*/
1329 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1332 size_t xBytesToStoreMessageLength;
1333 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1335 traceENTER_xStreamBufferIsFull( xStreamBuffer );
1337 configASSERT( pxStreamBuffer );
1339 /* This generic version of the receive function is used by both message
1340 * buffers, which store discrete messages, and stream buffers, which store a
1341 * continuous stream of bytes. Discrete messages include an additional
1342 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1343 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1345 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1349 xBytesToStoreMessageLength = 0;
1352 /* True if the available space equals zero. */
1353 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1362 traceRETURN_xStreamBufferIsFull( xReturn );
1366 /*-----------------------------------------------------------*/
1368 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1369 BaseType_t * pxHigherPriorityTaskWoken )
1371 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1373 UBaseType_t uxSavedInterruptStatus;
1375 traceENTER_xStreamBufferSendCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1377 configASSERT( pxStreamBuffer );
1379 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1381 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1383 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1384 ( pxStreamBuffer )->uxNotificationIndex,
1387 pxHigherPriorityTaskWoken );
1388 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1396 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1398 traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
1402 /*-----------------------------------------------------------*/
1404 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1405 BaseType_t * pxHigherPriorityTaskWoken )
1407 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1409 UBaseType_t uxSavedInterruptStatus;
1411 traceENTER_xStreamBufferReceiveCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1413 configASSERT( pxStreamBuffer );
1415 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1417 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1419 ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1420 ( pxStreamBuffer )->uxNotificationIndex,
1423 pxHigherPriorityTaskWoken );
1424 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1432 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1434 traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
1438 /*-----------------------------------------------------------*/
1440 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1441 const uint8_t * pucData,
1445 size_t xFirstLength;
1447 configASSERT( xCount > ( size_t ) 0 );
1449 /* Calculate the number of bytes that can be added in the first write -
1450 * which may be less than the total number of bytes that need to be added if
1451 * the buffer will wrap back to the beginning. */
1452 xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
1454 /* Write as many bytes as can be written in the first write. */
1455 configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
1456 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength );
1458 /* If the number of bytes written was less than the number that could be
1459 * written in the first write... */
1460 if( xCount > xFirstLength )
1462 /* ...then write the remaining bytes to the start of the buffer. */
1463 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1464 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength );
1468 mtCOVERAGE_TEST_MARKER();
1473 if( xHead >= pxStreamBuffer->xLength )
1475 xHead -= pxStreamBuffer->xLength;
1479 mtCOVERAGE_TEST_MARKER();
1484 /*-----------------------------------------------------------*/
1486 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1491 size_t xFirstLength;
1493 configASSERT( xCount != ( size_t ) 0 );
1495 /* Calculate the number of bytes that can be read - which may be
1496 * less than the number wanted if the data wraps around to the start of
1498 xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
1500 /* Obtain the number of bytes it is possible to obtain in the first
1501 * read. Asserts check bounds of read and write. */
1502 configASSERT( xFirstLength <= xCount );
1503 configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
1504 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength );
1506 /* If the total number of wanted bytes is greater than the number
1507 * that could be read in the first read... */
1508 if( xCount > xFirstLength )
1510 /* ...then read the remaining bytes from the start of the buffer. */
1511 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength );
1515 mtCOVERAGE_TEST_MARKER();
1518 /* Move the tail pointer to effectively remove the data read from the buffer. */
1521 if( xTail >= pxStreamBuffer->xLength )
1523 xTail -= pxStreamBuffer->xLength;
1528 /*-----------------------------------------------------------*/
1530 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1532 /* Returns the distance between xTail and xHead. */
1535 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1536 xCount -= pxStreamBuffer->xTail;
1538 if( xCount >= pxStreamBuffer->xLength )
1540 xCount -= pxStreamBuffer->xLength;
1544 mtCOVERAGE_TEST_MARKER();
1549 /*-----------------------------------------------------------*/
1551 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1552 uint8_t * const pucBuffer,
1553 size_t xBufferSizeBytes,
1554 size_t xTriggerLevelBytes,
1556 StreamBufferCallbackFunction_t pxSendCompletedCallback,
1557 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
1559 /* Assert here is deliberately writing to the entire buffer to ensure it can
1560 * be written to without generating exceptions, and is setting the buffer to a
1561 * known value to assist in development/debugging. */
1562 #if ( configASSERT_DEFINED == 1 )
1564 /* The value written just has to be identifiable when looking at the
1565 * memory. Don't use 0xA5 as that is the stack fill value and could
1566 * result in confusion as to what is actually being observed. */
1567 #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
1568 configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
1572 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
1573 pxStreamBuffer->pucBuffer = pucBuffer;
1574 pxStreamBuffer->xLength = xBufferSizeBytes;
1575 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1576 pxStreamBuffer->ucFlags = ucFlags;
1577 pxStreamBuffer->uxNotificationIndex = tskDEFAULT_INDEX_TO_NOTIFY;
1578 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
1580 pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
1581 pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
1585 /* MISRA Ref 11.1.1 [Object type casting] */
1586 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1587 /* coverity[misra_c_2012_rule_11_1_violation] */
1588 ( void ) pxSendCompletedCallback;
1590 /* MISRA Ref 11.1.1 [Object type casting] */
1591 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
1592 /* coverity[misra_c_2012_rule_11_1_violation] */
1593 ( void ) pxReceiveCompletedCallback;
1595 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
1597 /*-----------------------------------------------------------*/
1599 UBaseType_t uxStreamBufferGetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer )
1601 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1603 traceENTER_uxStreamBufferGetStreamBufferNotificationIndex( xStreamBuffer );
1605 configASSERT( pxStreamBuffer );
1607 traceRETURN_uxStreamBufferGetStreamBufferNotificationIndex( pxStreamBuffer->uxNotificationIndex );
1609 return pxStreamBuffer->uxNotificationIndex;
1611 /*-----------------------------------------------------------*/
1613 void vStreamBufferSetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer,
1614 UBaseType_t uxNotificationIndex )
1616 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1618 traceENTER_vStreamBufferSetStreamBufferNotificationIndex( xStreamBuffer, uxNotificationIndex );
1620 configASSERT( pxStreamBuffer );
1622 /* There should be no task waiting otherwise we'd never resume them. */
1623 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
1624 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
1626 /* Check that the task notification index is valid. */
1627 configASSERT( uxNotificationIndex < configTASK_NOTIFICATION_ARRAY_ENTRIES );
1629 pxStreamBuffer->uxNotificationIndex = uxNotificationIndex;
1631 traceRETURN_vStreamBufferSetStreamBufferNotificationIndex();
1633 /*-----------------------------------------------------------*/
1635 #if ( configUSE_TRACE_FACILITY == 1 )
1637 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1639 traceENTER_uxStreamBufferGetStreamBufferNumber( xStreamBuffer );
1641 traceRETURN_uxStreamBufferGetStreamBufferNumber( xStreamBuffer->uxStreamBufferNumber );
1643 return xStreamBuffer->uxStreamBufferNumber;
1646 #endif /* configUSE_TRACE_FACILITY */
1647 /*-----------------------------------------------------------*/
1649 #if ( configUSE_TRACE_FACILITY == 1 )
1651 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1652 UBaseType_t uxStreamBufferNumber )
1654 traceENTER_vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxStreamBufferNumber );
1656 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1658 traceRETURN_vStreamBufferSetStreamBufferNumber();
1661 #endif /* configUSE_TRACE_FACILITY */
1662 /*-----------------------------------------------------------*/
1664 #if ( configUSE_TRACE_FACILITY == 1 )
1666 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1668 traceENTER_ucStreamBufferGetStreamBufferType( xStreamBuffer );
1670 traceRETURN_ucStreamBufferGetStreamBufferType( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1672 return( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1675 #endif /* configUSE_TRACE_FACILITY */
1676 /*-----------------------------------------------------------*/
1678 /* This entire source file will be skipped if the application is not configured
1679 * to include stream buffer functionality. This #if is closed at the very bottom
1680 * of this file. If you want to include stream buffers then ensure
1681 * configUSE_STREAM_BUFFERS is set to 1 in FreeRTOSConfig.h. */
1682 #endif /* configUSE_STREAM_BUFFERS == 1 */