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. */
33 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
34 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
36 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
38 /* FreeRTOS includes. */
41 #include "stream_buffer.h"
43 #if ( configUSE_TASK_NOTIFICATIONS != 1 )
44 #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
47 #if ( INCLUDE_xTaskGetCurrentTaskHandle != 1 )
48 #error INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 to build stream_buffer.c
51 /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified
52 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
53 * for the header files above, but not in this file, in order to generate the
54 * correct privileged Vs unprivileged linkage and placement. */
55 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
57 /* If the user has not provided application specific Rx notification macros,
58 * or #defined the notification macros away, then provide default implementations
59 * that uses task notifications. */
60 /*lint -save -e9026 Function like macros allowed and needed here so they can be overridden. */
61 #ifndef sbRECEIVE_COMPLETED
62 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
65 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
67 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
70 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
73 ( 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 = portSET_INTERRUPT_MASK_FROM_ISR(); \
103 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
105 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
108 ( pxHigherPriorityTaskWoken ) ); \
109 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
112 portCLEAR_INTERRUPT_MASK_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 = portSET_INTERRUPT_MASK_FROM_ISR(); \
180 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
182 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
185 ( pxHigherPriorityTaskWoken ) ); \
186 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
189 portCLEAR_INTERRUPT_MASK_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 /*lint -restore (9026) */
213 /* The number of bytes used to hold the length of a message in the buffer. */
214 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
216 /* Bits stored in the ucFlags field of the stream buffer. */
217 #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. */
218 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
220 /*-----------------------------------------------------------*/
222 /* Structure that hold state information on the buffer. */
223 typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
225 volatile size_t xTail; /* Index to the next item to read within the buffer. */
226 volatile size_t xHead; /* Index to the next item to write within the buffer. */
227 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
228 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
229 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
230 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
231 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
234 #if ( configUSE_TRACE_FACILITY == 1 )
235 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
238 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
239 StreamBufferCallbackFunction_t pxSendCompletedCallback; /* Optional callback called on send complete. sbSEND_COMPLETED is called if this is NULL. */
240 StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
245 * The number of bytes available to be read from the buffer.
247 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
250 * Add xCount bytes from pucData into the pxStreamBuffer's data storage area.
251 * This function does not update the buffer's xHead pointer, so multiple writes
252 * may be chained together "atomically". This is useful for Message Buffers where
253 * the length and data bytes are written in two separate chunks, and we don't want
254 * the reader to see the buffer as having grown until after all data is copied over.
255 * This function takes a custom xHead value to indicate where to write to (necessary
256 * for chaining) and returns the the resulting xHead position.
257 * To mark the write as complete, manually set the buffer's xHead field with the
258 * returned xHead from this function.
260 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
261 const uint8_t * pucData,
263 size_t xHead ) PRIVILEGED_FUNCTION;
266 * If the stream buffer is being used as a message buffer, then reads an entire
267 * message out of the buffer. If the stream buffer is being used as a stream
268 * buffer then read as many bytes as possible from the buffer.
269 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
270 * buffer's data storage area.
272 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
274 size_t xBufferLengthBytes,
275 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
278 * If the stream buffer is being used as a message buffer, then writes an entire
279 * message to the buffer. If the stream buffer is being used as a stream
280 * buffer then write as many bytes as possible to the buffer.
281 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
284 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
285 const void * pvTxData,
286 size_t xDataLengthBytes,
288 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
291 * Copies xCount bytes from the pxStreamBuffer's data storage area to pucData.
292 * This function does not update the buffer's xTail pointer, so multiple reads
293 * may be chained together "atomically". This is useful for Message Buffers where
294 * the length and data bytes are read in two separate chunks, and we don't want
295 * the writer to see the buffer as having more free space until after all data is
296 * copied over, especially if we have to abort the read due to insufficient receiving space.
297 * This function takes a custom xTail value to indicate where to read from (necessary
298 * for chaining) and returns the the resulting xTail position.
299 * To mark the read as complete, manually set the buffer's xTail field with the
300 * returned xTail from this function.
302 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
305 size_t xTail ) PRIVILEGED_FUNCTION;
308 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
309 * initialise the members of the newly created stream buffer structure.
311 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
312 uint8_t * const pucBuffer,
313 size_t xBufferSizeBytes,
314 size_t xTriggerLevelBytes,
316 StreamBufferCallbackFunction_t pxSendCompletedCallback,
317 StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
319 /*-----------------------------------------------------------*/
320 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
321 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
322 size_t xTriggerLevelBytes,
323 BaseType_t xIsMessageBuffer,
324 StreamBufferCallbackFunction_t pxSendCompletedCallback,
325 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
327 void * pvAllocatedMemory;
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 + 1 + sizeof( StreamBuffer_t ) ) )
367 pvAllocatedMemory = pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) );
371 pvAllocatedMemory = NULL;
374 if( pvAllocatedMemory != NULL )
376 prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pvAllocatedMemory, /* Structure at the start of the allocated memory. */ /*lint !e9087 Safe cast as allocated memory is aligned. */ /*lint !e826 Area is not too small and alignment is guaranteed provided malloc() behaves as expected and returns aligned buffer. */
377 ( ( uint8_t * ) pvAllocatedMemory ) + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */
381 pxSendCompletedCallback,
382 pxReceiveCompletedCallback );
384 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xIsMessageBuffer );
388 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
391 return ( StreamBufferHandle_t ) pvAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
393 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
394 /*-----------------------------------------------------------*/
396 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
398 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
399 size_t xTriggerLevelBytes,
400 BaseType_t xIsMessageBuffer,
401 uint8_t * const pucStreamBufferStorageArea,
402 StaticStreamBuffer_t * const pxStaticStreamBuffer,
403 StreamBufferCallbackFunction_t pxSendCompletedCallback,
404 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
406 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
407 StreamBufferHandle_t xReturn;
410 configASSERT( pucStreamBufferStorageArea );
411 configASSERT( pxStaticStreamBuffer );
412 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
414 /* A trigger level of 0 would cause a waiting task to unblock even when
415 * the buffer was empty. */
416 if( xTriggerLevelBytes == ( size_t ) 0 )
418 xTriggerLevelBytes = ( size_t ) 1;
421 if( xIsMessageBuffer != pdFALSE )
423 /* Statically allocated message buffer. */
424 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
428 /* Statically allocated stream buffer. */
429 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
432 /* In case the stream buffer is going to be used as a message buffer
433 * (that is, it will hold discrete messages with a little meta data that
434 * says how big the next message is) check the buffer will be large enough
435 * to hold at least one message. */
436 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
438 #if ( configASSERT_DEFINED == 1 )
440 /* Sanity check that the size of the structure used to declare a
441 * variable of type StaticStreamBuffer_t equals the size of the real
442 * message buffer structure. */
443 configASSERT( sizeof( StaticStreamBuffer_t ) == sizeof( StreamBuffer_t ) );
444 #endif /* configASSERT_DEFINED */
446 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
448 prvInitialiseNewStreamBuffer( pxStreamBuffer,
449 pucStreamBufferStorageArea,
453 pxSendCompletedCallback,
454 pxReceiveCompletedCallback );
456 /* Remember this was statically allocated in case it is ever deleted
458 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
460 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
462 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
467 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
472 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
473 /*-----------------------------------------------------------*/
475 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
476 BaseType_t xStreamBufferGetStaticBuffers( StreamBufferHandle_t xStreamBuffer,
477 uint8_t ** ppucStreamBufferStorageArea,
478 StaticStreamBuffer_t ** ppxStaticStreamBuffer )
481 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
483 configASSERT( pxStreamBuffer );
484 configASSERT( ppucStreamBufferStorageArea );
485 configASSERT( ppxStaticStreamBuffer );
487 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) != ( uint8_t ) 0 )
489 *ppucStreamBufferStorageArea = pxStreamBuffer->pucBuffer;
490 *ppxStaticStreamBuffer = ( StaticStreamBuffer_t * ) pxStreamBuffer;
500 #endif /* configSUPPORT_STATIC_ALLOCATION */
501 /*-----------------------------------------------------------*/
503 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
505 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
507 configASSERT( pxStreamBuffer );
509 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
511 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
513 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
515 /* Both the structure and the buffer were allocated using a single call
516 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
517 vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
521 /* Should not be possible to get here, ucFlags must be corrupt.
522 * Force an assert. */
523 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
529 /* The structure and buffer were not allocated dynamically and cannot be
530 * freed - just scrub the structure so future use will assert. */
531 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
534 /*-----------------------------------------------------------*/
536 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
538 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
539 BaseType_t xReturn = pdFAIL;
540 StreamBufferCallbackFunction_t pxSendCallback = NULL, pxReceiveCallback = NULL;
542 #if ( configUSE_TRACE_FACILITY == 1 )
543 UBaseType_t uxStreamBufferNumber;
546 configASSERT( pxStreamBuffer );
548 #if ( configUSE_TRACE_FACILITY == 1 )
550 /* Store the stream buffer number so it can be restored after the
552 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
556 /* Can only reset a message buffer if there are no tasks blocked on it. */
557 taskENTER_CRITICAL();
559 if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
561 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
563 pxSendCallback = pxStreamBuffer->pxSendCompletedCallback;
564 pxReceiveCallback = pxStreamBuffer->pxReceiveCompletedCallback;
568 prvInitialiseNewStreamBuffer( pxStreamBuffer,
569 pxStreamBuffer->pucBuffer,
570 pxStreamBuffer->xLength,
571 pxStreamBuffer->xTriggerLevelBytes,
572 pxStreamBuffer->ucFlags,
576 #if ( configUSE_TRACE_FACILITY == 1 )
578 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
582 traceSTREAM_BUFFER_RESET( xStreamBuffer );
591 /*-----------------------------------------------------------*/
593 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
594 size_t xTriggerLevel )
596 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
599 configASSERT( pxStreamBuffer );
601 /* It is not valid for the trigger level to be 0. */
602 if( xTriggerLevel == ( size_t ) 0 )
604 xTriggerLevel = ( size_t ) 1;
607 /* The trigger level is the number of bytes that must be in the stream
608 * buffer before a task that is waiting for data is unblocked. */
609 if( xTriggerLevel < pxStreamBuffer->xLength )
611 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
621 /*-----------------------------------------------------------*/
623 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
625 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
627 size_t xOriginalTail;
629 configASSERT( pxStreamBuffer );
631 /* The code below reads xTail and then xHead. This is safe if the stream
632 * buffer is updated once between the two reads - but not if the stream buffer
633 * is updated more than once between the two reads - hence the loop. */
636 xOriginalTail = pxStreamBuffer->xTail;
637 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
638 xSpace -= pxStreamBuffer->xHead;
639 } while( xOriginalTail != pxStreamBuffer->xTail );
641 xSpace -= ( size_t ) 1;
643 if( xSpace >= pxStreamBuffer->xLength )
645 xSpace -= pxStreamBuffer->xLength;
649 mtCOVERAGE_TEST_MARKER();
654 /*-----------------------------------------------------------*/
656 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
658 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
661 configASSERT( pxStreamBuffer );
663 xReturn = prvBytesInBuffer( pxStreamBuffer );
666 /*-----------------------------------------------------------*/
668 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
669 const void * pvTxData,
670 size_t xDataLengthBytes,
671 TickType_t xTicksToWait )
673 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
674 size_t xReturn, xSpace = 0;
675 size_t xRequiredSpace = xDataLengthBytes;
677 size_t xMaxReportedSpace = 0;
679 configASSERT( pvTxData );
680 configASSERT( pxStreamBuffer );
682 /* The maximum amount of space a stream buffer will ever report is its length
684 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
686 /* This send function is used to write to both message buffers and stream
687 * buffers. If this is a message buffer then the space needed must be
688 * increased by the amount of bytes needed to store the length of the
690 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
692 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
695 configASSERT( xRequiredSpace > xDataLengthBytes );
697 /* If this is a message buffer then it must be possible to write the
699 if( xRequiredSpace > xMaxReportedSpace )
701 /* The message would not fit even if the entire buffer was empty,
702 * so don't wait for space. */
703 xTicksToWait = ( TickType_t ) 0;
707 mtCOVERAGE_TEST_MARKER();
712 /* If this is a stream buffer then it is acceptable to write only part
713 * of the message to the buffer. Cap the length to the total length of
715 if( xRequiredSpace > xMaxReportedSpace )
717 xRequiredSpace = xMaxReportedSpace;
721 mtCOVERAGE_TEST_MARKER();
725 if( xTicksToWait != ( TickType_t ) 0 )
727 vTaskSetTimeOutState( &xTimeOut );
731 /* Wait until the required number of bytes are free in the message
733 taskENTER_CRITICAL();
735 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
737 if( xSpace < xRequiredSpace )
739 /* Clear notification state as going to wait for space. */
740 ( void ) xTaskNotifyStateClear( NULL );
742 /* Should only be one writer. */
743 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
744 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
754 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
755 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
756 pxStreamBuffer->xTaskWaitingToSend = NULL;
757 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
761 mtCOVERAGE_TEST_MARKER();
764 if( xSpace == ( size_t ) 0 )
766 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
770 mtCOVERAGE_TEST_MARKER();
773 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
775 if( xReturn > ( size_t ) 0 )
777 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
779 /* Was a task waiting for the data? */
780 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
782 prvSEND_COMPLETED( pxStreamBuffer );
786 mtCOVERAGE_TEST_MARKER();
791 mtCOVERAGE_TEST_MARKER();
792 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
797 /*-----------------------------------------------------------*/
799 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
800 const void * pvTxData,
801 size_t xDataLengthBytes,
802 BaseType_t * const pxHigherPriorityTaskWoken )
804 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
805 size_t xReturn, xSpace;
806 size_t xRequiredSpace = xDataLengthBytes;
808 configASSERT( pvTxData );
809 configASSERT( pxStreamBuffer );
811 /* This send function is used to write to both message buffers and stream
812 * buffers. If this is a message buffer then the space needed must be
813 * increased by the amount of bytes needed to store the length of the
815 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
817 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
821 mtCOVERAGE_TEST_MARKER();
824 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
825 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
827 if( xReturn > ( size_t ) 0 )
829 /* Was a task waiting for the data? */
830 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
832 prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
836 mtCOVERAGE_TEST_MARKER();
841 mtCOVERAGE_TEST_MARKER();
844 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
848 /*-----------------------------------------------------------*/
850 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
851 const void * pvTxData,
852 size_t xDataLengthBytes,
854 size_t xRequiredSpace )
856 size_t xNextHead = pxStreamBuffer->xHead;
857 configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
859 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
861 /* This is a message buffer, as opposed to a stream buffer. */
863 /* Convert xDataLengthBytes to the message length type. */
864 xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
866 /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
867 configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
869 if( xSpace >= xRequiredSpace )
871 /* There is enough space to write both the message length and the message
872 * itself into the buffer. Start by writing the length of the data, the data
873 * itself will be written later in this function. */
874 xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
878 /* Not enough space, so do not write data to the buffer. */
879 xDataLengthBytes = 0;
884 /* This is a stream buffer, as opposed to a message buffer, so writing a
885 * stream of bytes rather than discrete messages. Plan to write as many
886 * bytes as possible. */
887 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
890 if( xDataLengthBytes != ( size_t ) 0 )
892 /* Write the data to the buffer. */
893 pxStreamBuffer->xHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes, xNextHead ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alignment and access. */
896 return xDataLengthBytes;
898 /*-----------------------------------------------------------*/
900 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
902 size_t xBufferLengthBytes,
903 TickType_t xTicksToWait )
905 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
906 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
908 configASSERT( pvRxData );
909 configASSERT( pxStreamBuffer );
911 /* This receive function is used by both message buffers, which store
912 * discrete messages, and stream buffers, which store a continuous stream of
913 * bytes. Discrete messages include an additional
914 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
916 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
918 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
922 xBytesToStoreMessageLength = 0;
925 if( xTicksToWait != ( TickType_t ) 0 )
927 /* Checking if there is data and clearing the notification state must be
928 * performed atomically. */
929 taskENTER_CRITICAL();
931 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
933 /* If this function was invoked by a message buffer read then
934 * xBytesToStoreMessageLength holds the number of bytes used to hold
935 * the length of the next discrete message. If this function was
936 * invoked by a stream buffer read then xBytesToStoreMessageLength will
938 if( xBytesAvailable <= xBytesToStoreMessageLength )
940 /* Clear notification state as going to wait for data. */
941 ( void ) xTaskNotifyStateClear( NULL );
943 /* Should only be one reader. */
944 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
945 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
949 mtCOVERAGE_TEST_MARKER();
954 if( xBytesAvailable <= xBytesToStoreMessageLength )
956 /* Wait for data to be available. */
957 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
958 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
959 pxStreamBuffer->xTaskWaitingToReceive = NULL;
961 /* Recheck the data available after blocking. */
962 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
966 mtCOVERAGE_TEST_MARKER();
971 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
974 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
975 * holds the number of bytes used to store the message length) or a stream of
976 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
977 * available must be greater than xBytesToStoreMessageLength to be able to
978 * read bytes from the buffer. */
979 if( xBytesAvailable > xBytesToStoreMessageLength )
981 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
983 /* Was a task waiting for space in the buffer? */
984 if( xReceivedLength != ( size_t ) 0 )
986 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
987 prvRECEIVE_COMPLETED( xStreamBuffer );
991 mtCOVERAGE_TEST_MARKER();
996 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
997 mtCOVERAGE_TEST_MARKER();
1000 return xReceivedLength;
1002 /*-----------------------------------------------------------*/
1004 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
1006 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1007 size_t xReturn, xBytesAvailable;
1008 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
1010 configASSERT( pxStreamBuffer );
1012 /* Ensure the stream buffer is being used as a message buffer. */
1013 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1015 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1017 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
1019 /* The number of bytes available is greater than the number of bytes
1020 * required to hold the length of the next message, so another message
1022 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
1023 xReturn = ( size_t ) xTempReturn;
1027 /* The minimum amount of bytes in a message buffer is
1028 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
1029 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
1031 configASSERT( xBytesAvailable == 0 );
1042 /*-----------------------------------------------------------*/
1044 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
1046 size_t xBufferLengthBytes,
1047 BaseType_t * const pxHigherPriorityTaskWoken )
1049 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1050 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1052 configASSERT( pvRxData );
1053 configASSERT( pxStreamBuffer );
1055 /* This receive function is used by both message buffers, which store
1056 * discrete messages, and stream buffers, which store a continuous stream of
1057 * bytes. Discrete messages include an additional
1058 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1060 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1062 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1066 xBytesToStoreMessageLength = 0;
1069 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1071 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1072 * holds the number of bytes used to store the message length) or a stream of
1073 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1074 * available must be greater than xBytesToStoreMessageLength to be able to
1075 * read bytes from the buffer. */
1076 if( xBytesAvailable > xBytesToStoreMessageLength )
1078 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1080 /* Was a task waiting for space in the buffer? */
1081 if( xReceivedLength != ( size_t ) 0 )
1083 prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
1087 mtCOVERAGE_TEST_MARKER();
1092 mtCOVERAGE_TEST_MARKER();
1095 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
1097 return xReceivedLength;
1099 /*-----------------------------------------------------------*/
1101 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
1103 size_t xBufferLengthBytes,
1104 size_t xBytesAvailable )
1106 size_t xCount, xNextMessageLength;
1107 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
1108 size_t xNextTail = pxStreamBuffer->xTail;
1110 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1112 /* A discrete message is being received. First receive the length
1113 * of the message. */
1114 xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
1115 xNextMessageLength = ( size_t ) xTempNextMessageLength;
1117 /* Reduce the number of bytes available by the number of bytes just
1119 xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
1121 /* Check there is enough space in the buffer provided by the
1123 if( xNextMessageLength > xBufferLengthBytes )
1125 /* The user has provided insufficient space to read the message. */
1126 xNextMessageLength = 0;
1130 mtCOVERAGE_TEST_MARKER();
1135 /* A stream of bytes is being received (as opposed to a discrete
1136 * message), so read as many bytes as possible. */
1137 xNextMessageLength = xBufferLengthBytes;
1140 /* Use the minimum of the wanted bytes and the available bytes. */
1141 xCount = configMIN( xNextMessageLength, xBytesAvailable );
1143 if( xCount != ( size_t ) 0 )
1145 /* Read the actual data and update the tail to mark the data as officially consumed. */
1146 pxStreamBuffer->xTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xCount, xNextTail ); /*lint !e9079 Data storage area is implemented as uint8_t array for ease of sizing, indexing and alignment. */
1151 /*-----------------------------------------------------------*/
1153 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1155 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1159 configASSERT( pxStreamBuffer );
1161 /* True if no bytes are available. */
1162 xTail = pxStreamBuffer->xTail;
1164 if( pxStreamBuffer->xHead == xTail )
1175 /*-----------------------------------------------------------*/
1177 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1180 size_t xBytesToStoreMessageLength;
1181 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1183 configASSERT( pxStreamBuffer );
1185 /* This generic version of the receive function is used by both message
1186 * buffers, which store discrete messages, and stream buffers, which store a
1187 * continuous stream of bytes. Discrete messages include an additional
1188 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1189 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1191 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1195 xBytesToStoreMessageLength = 0;
1198 /* True if the available space equals zero. */
1199 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1210 /*-----------------------------------------------------------*/
1212 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1213 BaseType_t * pxHigherPriorityTaskWoken )
1215 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1217 UBaseType_t uxSavedInterruptStatus;
1219 configASSERT( pxStreamBuffer );
1221 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1223 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1225 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1228 pxHigherPriorityTaskWoken );
1229 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1237 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1241 /*-----------------------------------------------------------*/
1243 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1244 BaseType_t * pxHigherPriorityTaskWoken )
1246 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1248 UBaseType_t uxSavedInterruptStatus;
1250 configASSERT( pxStreamBuffer );
1252 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1254 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1256 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1259 pxHigherPriorityTaskWoken );
1260 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1268 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1272 /*-----------------------------------------------------------*/
1274 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1275 const uint8_t * pucData,
1279 size_t xFirstLength;
1281 configASSERT( xCount > ( size_t ) 0 );
1283 /* Calculate the number of bytes that can be added in the first write -
1284 * which may be less than the total number of bytes that need to be added if
1285 * the buffer will wrap back to the beginning. */
1286 xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
1288 /* Write as many bytes as can be written in the first write. */
1289 configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
1290 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1292 /* If the number of bytes written was less than the number that could be
1293 * written in the first write... */
1294 if( xCount > xFirstLength )
1296 /* ...then write the remaining bytes to the start of the buffer. */
1297 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1298 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1302 mtCOVERAGE_TEST_MARKER();
1307 if( xHead >= pxStreamBuffer->xLength )
1309 xHead -= pxStreamBuffer->xLength;
1313 mtCOVERAGE_TEST_MARKER();
1318 /*-----------------------------------------------------------*/
1320 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1325 size_t xFirstLength;
1327 configASSERT( xCount != ( size_t ) 0 );
1329 /* Calculate the number of bytes that can be read - which may be
1330 * less than the number wanted if the data wraps around to the start of
1332 xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
1334 /* Obtain the number of bytes it is possible to obtain in the first
1335 * read. Asserts check bounds of read and write. */
1336 configASSERT( xFirstLength <= xCount );
1337 configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
1338 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1340 /* If the total number of wanted bytes is greater than the number
1341 * that could be read in the first read... */
1342 if( xCount > xFirstLength )
1344 /* ...then read the remaining bytes from the start of the buffer. */
1345 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1349 mtCOVERAGE_TEST_MARKER();
1352 /* Move the tail pointer to effectively remove the data read from the buffer. */
1355 if( xTail >= pxStreamBuffer->xLength )
1357 xTail -= pxStreamBuffer->xLength;
1362 /*-----------------------------------------------------------*/
1364 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1366 /* Returns the distance between xTail and xHead. */
1369 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1370 xCount -= pxStreamBuffer->xTail;
1372 if( xCount >= pxStreamBuffer->xLength )
1374 xCount -= pxStreamBuffer->xLength;
1378 mtCOVERAGE_TEST_MARKER();
1383 /*-----------------------------------------------------------*/
1385 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1386 uint8_t * const pucBuffer,
1387 size_t xBufferSizeBytes,
1388 size_t xTriggerLevelBytes,
1390 StreamBufferCallbackFunction_t pxSendCompletedCallback,
1391 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
1393 /* Assert here is deliberately writing to the entire buffer to ensure it can
1394 * be written to without generating exceptions, and is setting the buffer to a
1395 * known value to assist in development/debugging. */
1396 #if ( configASSERT_DEFINED == 1 )
1398 /* The value written just has to be identifiable when looking at the
1399 * memory. Don't use 0xA5 as that is the stack fill value and could
1400 * result in confusion as to what is actually being observed. */
1401 #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
1402 configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
1406 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
1407 pxStreamBuffer->pucBuffer = pucBuffer;
1408 pxStreamBuffer->xLength = xBufferSizeBytes;
1409 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1410 pxStreamBuffer->ucFlags = ucFlags;
1411 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
1413 pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
1414 pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
1418 ( void ) pxSendCompletedCallback;
1419 ( void ) pxReceiveCompletedCallback;
1424 #if ( configUSE_TRACE_FACILITY == 1 )
1426 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1428 return xStreamBuffer->uxStreamBufferNumber;
1431 #endif /* configUSE_TRACE_FACILITY */
1432 /*-----------------------------------------------------------*/
1434 #if ( configUSE_TRACE_FACILITY == 1 )
1436 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1437 UBaseType_t uxStreamBufferNumber )
1439 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1442 #endif /* configUSE_TRACE_FACILITY */
1443 /*-----------------------------------------------------------*/
1445 #if ( configUSE_TRACE_FACILITY == 1 )
1447 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1449 return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
1452 #endif /* configUSE_TRACE_FACILITY */
1453 /*-----------------------------------------------------------*/