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 /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified
51 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
52 * for the header files above, but not in this file, in order to generate the
53 * correct privileged Vs unprivileged linkage and placement. */
54 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
56 /* If the user has not provided application specific Rx notification macros,
57 * or #defined the notification macros away, then provide default implementations
58 * that uses task notifications. */
59 /*lint -save -e9026 Function like macros allowed and needed here so they can be overridden. */
60 #ifndef sbRECEIVE_COMPLETED
61 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
64 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
66 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
69 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
72 ( void ) xTaskResumeAll()
73 #endif /* sbRECEIVE_COMPLETED */
75 /* If user has provided a per-instance receive complete callback, then
76 * invoke the callback else use the receive complete macro which is provided by default for all instances.
78 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
79 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) \
81 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
83 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
87 sbRECEIVE_COMPLETED( ( pxStreamBuffer ) ); \
90 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
91 #define prvRECEIVE_COMPLETED( pxStreamBuffer ) sbRECEIVE_COMPLETED( ( pxStreamBuffer ) )
92 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
94 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
95 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
96 pxHigherPriorityTaskWoken ) \
98 UBaseType_t uxSavedInterruptStatus; \
100 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
102 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
104 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
107 ( pxHigherPriorityTaskWoken ) ); \
108 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
111 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
113 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
115 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
116 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
117 pxHigherPriorityTaskWoken ) \
119 if( ( pxStreamBuffer )->pxReceiveCompletedCallback != NULL ) \
121 ( pxStreamBuffer )->pxReceiveCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
125 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
128 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
129 #define prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
130 sbRECEIVE_COMPLETED_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
131 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
133 /* If the user has not provided an application specific Tx notification macro,
134 * or #defined the notification macro away, then provide a default
135 * implementation that uses task notifications.
137 #ifndef sbSEND_COMPLETED
138 #define sbSEND_COMPLETED( pxStreamBuffer ) \
141 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
143 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
146 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
149 ( void ) xTaskResumeAll()
150 #endif /* sbSEND_COMPLETED */
152 /* If user has provided a per-instance send completed callback, then
153 * invoke the callback else use the send complete macro which is provided by default for all instances.
155 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
156 #define prvSEND_COMPLETED( pxStreamBuffer ) \
158 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
160 pxStreamBuffer->pxSendCompletedCallback( ( pxStreamBuffer ), pdFALSE, NULL ); \
164 sbSEND_COMPLETED( ( pxStreamBuffer ) ); \
167 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
168 #define prvSEND_COMPLETED( pxStreamBuffer ) sbSEND_COMPLETED( ( pxStreamBuffer ) )
169 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
172 #ifndef sbSEND_COMPLETE_FROM_ISR
173 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
175 UBaseType_t uxSavedInterruptStatus; \
177 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR(); \
179 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
181 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
184 ( pxHigherPriorityTaskWoken ) ); \
185 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
188 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus ); \
190 #endif /* sbSEND_COMPLETE_FROM_ISR */
193 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
194 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
196 if( ( pxStreamBuffer )->pxSendCompletedCallback != NULL ) \
198 ( pxStreamBuffer )->pxSendCompletedCallback( ( pxStreamBuffer ), pdTRUE, ( pxHigherPriorityTaskWoken ) ); \
202 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) ); \
205 #else /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
206 #define prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
207 sbSEND_COMPLETE_FROM_ISR( ( pxStreamBuffer ), ( pxHigherPriorityTaskWoken ) )
208 #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
210 /*lint -restore (9026) */
212 /* The number of bytes used to hold the length of a message in the buffer. */
213 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
215 /* Bits stored in the ucFlags field of the stream buffer. */
216 #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. */
217 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
219 /*-----------------------------------------------------------*/
221 /* Structure that hold state information on the buffer. */
222 typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
224 volatile size_t xTail; /* Index to the next item to read within the buffer. */
225 volatile size_t xHead; /* Index to the next item to write within the buffer. */
226 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
227 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
228 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
229 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
230 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
233 #if ( configUSE_TRACE_FACILITY == 1 )
234 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
237 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
238 StreamBufferCallbackFunction_t pxSendCompletedCallback; /* Optional callback called on send complete. sbSEND_COMPLETED is called if this is NULL. */
239 StreamBufferCallbackFunction_t pxReceiveCompletedCallback; /* Optional callback called on receive complete. sbRECEIVE_COMPLETED is called if this is NULL. */
244 * The number of bytes available to be read from the buffer.
246 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
249 * Add xCount bytes from pucData into the pxStreamBuffer's data storage area.
250 * This function does not update the buffer's xHead pointer, so multiple writes
251 * may be chained together "atomically". This is useful for Message Buffers where
252 * the length and data bytes are written in two separate chunks, and we don't want
253 * the reader to see the buffer as having grown until after all data is copied over.
254 * This function takes a custom xHead value to indicate where to write to (necessary
255 * for chaining) and returns the the resulting xHead position.
256 * To mark the write as complete, manually set the buffer's xHead field with the
257 * returned xHead from this function.
259 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
260 const uint8_t * pucData,
262 size_t xHead ) PRIVILEGED_FUNCTION;
265 * If the stream buffer is being used as a message buffer, then reads an entire
266 * message out of the buffer. If the stream buffer is being used as a stream
267 * buffer then read as many bytes as possible from the buffer.
268 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
269 * buffer's data storage area.
271 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
273 size_t xBufferLengthBytes,
274 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
277 * If the stream buffer is being used as a message buffer, then writes an entire
278 * message to the buffer. If the stream buffer is being used as a stream
279 * buffer then write as many bytes as possible to the buffer.
280 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
283 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
284 const void * pvTxData,
285 size_t xDataLengthBytes,
287 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
290 * Copies xCount bytes from the pxStreamBuffer's data storage area to pucData.
291 * This function does not update the buffer's xTail pointer, so multiple reads
292 * may be chained together "atomically". This is useful for Message Buffers where
293 * the length and data bytes are read in two separate chunks, and we don't want
294 * the writer to see the buffer as having more free space until after all data is
295 * copied over, especially if we have to abort the read due to insufficient receiving space.
296 * This function takes a custom xTail value to indicate where to read from (necessary
297 * for chaining) and returns the the resulting xTail position.
298 * To mark the read as complete, manually set the buffer's xTail field with the
299 * returned xTail from this function.
301 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
304 size_t xTail ) PRIVILEGED_FUNCTION;
307 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
308 * initialise the members of the newly created stream buffer structure.
310 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
311 uint8_t * const pucBuffer,
312 size_t xBufferSizeBytes,
313 size_t xTriggerLevelBytes,
315 StreamBufferCallbackFunction_t pxSendCompletedCallback,
316 StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
318 /*-----------------------------------------------------------*/
319 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
320 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
321 size_t xTriggerLevelBytes,
322 BaseType_t xIsMessageBuffer,
323 StreamBufferCallbackFunction_t pxSendCompletedCallback,
324 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
326 void * pvAllocatedMemory;
329 traceENTER_xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
331 /* In case the stream buffer is going to be used as a message buffer
332 * (that is, it will hold discrete messages with a little meta data that
333 * says how big the next message is) check the buffer will be large enough
334 * to hold at least one message. */
335 if( xIsMessageBuffer == pdTRUE )
337 /* Is a message buffer but not statically allocated. */
338 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
339 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
343 /* Not a message buffer and not statically allocated. */
345 configASSERT( xBufferSizeBytes > 0 );
348 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
350 /* A trigger level of 0 would cause a waiting task to unblock even when
351 * the buffer was empty. */
352 if( xTriggerLevelBytes == ( size_t ) 0 )
354 xTriggerLevelBytes = ( size_t ) 1;
357 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
358 * Both are allocated in a single call to pvPortMalloc(). The
359 * StreamBuffer_t structure is placed at the start of the allocated memory
360 * and the buffer follows immediately after. The requested size is
361 * incremented so the free space is returned as the user would expect -
362 * this is a quirk of the implementation that means otherwise the free
363 * space would be reported as one byte smaller than would be logically
365 if( xBufferSizeBytes < ( xBufferSizeBytes + 1 + sizeof( StreamBuffer_t ) ) )
368 pvAllocatedMemory = pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) );
372 pvAllocatedMemory = NULL;
375 if( pvAllocatedMemory != NULL )
377 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. */
378 ( ( 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. */
382 pxSendCompletedCallback,
383 pxReceiveCompletedCallback );
385 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pvAllocatedMemory ), xIsMessageBuffer );
389 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
392 traceRETURN_xStreamBufferGenericCreate( pvAllocatedMemory );
394 return ( StreamBufferHandle_t ) pvAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
396 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
397 /*-----------------------------------------------------------*/
399 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
401 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
402 size_t xTriggerLevelBytes,
403 BaseType_t xIsMessageBuffer,
404 uint8_t * const pucStreamBufferStorageArea,
405 StaticStreamBuffer_t * const pxStaticStreamBuffer,
406 StreamBufferCallbackFunction_t pxSendCompletedCallback,
407 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
409 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
410 StreamBufferHandle_t xReturn;
413 traceENTER_xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, xIsMessageBuffer, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback );
415 configASSERT( pucStreamBufferStorageArea );
416 configASSERT( pxStaticStreamBuffer );
417 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
419 /* A trigger level of 0 would cause a waiting task to unblock even when
420 * the buffer was empty. */
421 if( xTriggerLevelBytes == ( size_t ) 0 )
423 xTriggerLevelBytes = ( size_t ) 1;
426 /* In case the stream buffer is going to be used as a message buffer
427 * (that is, it will hold discrete messages with a little meta data that
428 * says how big the next message is) check the buffer will be large enough
429 * to hold at least one message. */
431 if( xIsMessageBuffer != pdFALSE )
433 /* Statically allocated message buffer. */
434 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
435 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
439 /* Statically allocated stream buffer. */
440 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
443 #if ( configASSERT_DEFINED == 1 )
445 /* Sanity check that the size of the structure used to declare a
446 * variable of type StaticStreamBuffer_t equals the size of the real
447 * message buffer structure. */
448 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
449 configASSERT( xSize == sizeof( StreamBuffer_t ) );
450 } /*lint !e529 xSize is referenced is configASSERT() is defined. */
451 #endif /* configASSERT_DEFINED */
453 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
455 prvInitialiseNewStreamBuffer( pxStreamBuffer,
456 pucStreamBufferStorageArea,
460 pxSendCompletedCallback,
461 pxReceiveCompletedCallback );
463 /* Remember this was statically allocated in case it is ever deleted
465 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
467 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
469 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
474 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
477 traceRETURN_xStreamBufferGenericCreateStatic( xReturn );
481 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
482 /*-----------------------------------------------------------*/
484 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
485 BaseType_t xStreamBufferGetStaticBuffers( StreamBufferHandle_t xStreamBuffer,
486 uint8_t ** ppucStreamBufferStorageArea,
487 StaticStreamBuffer_t ** ppxStaticStreamBuffer )
490 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
492 traceENTER_xStreamBufferGetStaticBuffers( xStreamBuffer, ppucStreamBufferStorageArea, ppxStaticStreamBuffer );
494 configASSERT( pxStreamBuffer );
495 configASSERT( ppucStreamBufferStorageArea );
496 configASSERT( ppxStaticStreamBuffer );
498 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) != ( uint8_t ) 0 )
500 *ppucStreamBufferStorageArea = pxStreamBuffer->pucBuffer;
501 *ppxStaticStreamBuffer = ( StaticStreamBuffer_t * ) pxStreamBuffer;
509 traceRETURN_xStreamBufferGetStaticBuffers( xReturn );
513 #endif /* configSUPPORT_STATIC_ALLOCATION */
514 /*-----------------------------------------------------------*/
516 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
518 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
520 traceENTER_vStreamBufferDelete( xStreamBuffer );
522 configASSERT( pxStreamBuffer );
524 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
526 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
528 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
530 /* Both the structure and the buffer were allocated using a single call
531 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
532 vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
536 /* Should not be possible to get here, ucFlags must be corrupt.
537 * Force an assert. */
538 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
544 /* The structure and buffer were not allocated dynamically and cannot be
545 * freed - just scrub the structure so future use will assert. */
546 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
549 traceRETURN_vStreamBufferDelete();
551 /*-----------------------------------------------------------*/
553 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
555 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
556 BaseType_t xReturn = pdFAIL;
557 StreamBufferCallbackFunction_t pxSendCallback = NULL, pxReceiveCallback = NULL;
559 #if ( configUSE_TRACE_FACILITY == 1 )
560 UBaseType_t uxStreamBufferNumber;
563 traceENTER_xStreamBufferReset( xStreamBuffer );
565 configASSERT( pxStreamBuffer );
567 #if ( configUSE_TRACE_FACILITY == 1 )
569 /* Store the stream buffer number so it can be restored after the
571 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
575 /* Can only reset a message buffer if there are no tasks blocked on it. */
576 taskENTER_CRITICAL();
578 if( ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) )
580 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
582 pxSendCallback = pxStreamBuffer->pxSendCompletedCallback;
583 pxReceiveCallback = pxStreamBuffer->pxReceiveCompletedCallback;
587 prvInitialiseNewStreamBuffer( pxStreamBuffer,
588 pxStreamBuffer->pucBuffer,
589 pxStreamBuffer->xLength,
590 pxStreamBuffer->xTriggerLevelBytes,
591 pxStreamBuffer->ucFlags,
595 #if ( configUSE_TRACE_FACILITY == 1 )
597 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
601 traceSTREAM_BUFFER_RESET( xStreamBuffer );
608 traceRETURN_xStreamBufferReset( xReturn );
612 /*-----------------------------------------------------------*/
614 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
615 size_t xTriggerLevel )
617 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
620 traceENTER_xStreamBufferSetTriggerLevel( xStreamBuffer, xTriggerLevel );
622 configASSERT( pxStreamBuffer );
624 /* It is not valid for the trigger level to be 0. */
625 if( xTriggerLevel == ( size_t ) 0 )
627 xTriggerLevel = ( size_t ) 1;
630 /* The trigger level is the number of bytes that must be in the stream
631 * buffer before a task that is waiting for data is unblocked. */
632 if( xTriggerLevel < pxStreamBuffer->xLength )
634 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
642 traceRETURN_xStreamBufferSetTriggerLevel( xReturn );
646 /*-----------------------------------------------------------*/
648 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
650 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
652 size_t xOriginalTail;
654 traceENTER_xStreamBufferSpacesAvailable( xStreamBuffer );
656 configASSERT( pxStreamBuffer );
658 /* The code below reads xTail and then xHead. This is safe if the stream
659 * buffer is updated once between the two reads - but not if the stream buffer
660 * is updated more than once between the two reads - hence the loop. */
663 xOriginalTail = pxStreamBuffer->xTail;
664 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
665 xSpace -= pxStreamBuffer->xHead;
666 } while( xOriginalTail != pxStreamBuffer->xTail );
668 xSpace -= ( size_t ) 1;
670 if( xSpace >= pxStreamBuffer->xLength )
672 xSpace -= pxStreamBuffer->xLength;
676 mtCOVERAGE_TEST_MARKER();
679 traceRETURN_xStreamBufferSpacesAvailable( xSpace );
683 /*-----------------------------------------------------------*/
685 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
687 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
690 traceENTER_xStreamBufferBytesAvailable( xStreamBuffer );
692 configASSERT( pxStreamBuffer );
694 xReturn = prvBytesInBuffer( pxStreamBuffer );
696 traceRETURN_xStreamBufferBytesAvailable( xReturn );
700 /*-----------------------------------------------------------*/
702 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
703 const void * pvTxData,
704 size_t xDataLengthBytes,
705 TickType_t xTicksToWait )
707 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
708 size_t xReturn, xSpace = 0;
709 size_t xRequiredSpace = xDataLengthBytes;
711 size_t xMaxReportedSpace = 0;
713 traceENTER_xStreamBufferSend( xStreamBuffer, pvTxData, xDataLengthBytes, xTicksToWait );
715 configASSERT( pvTxData );
716 configASSERT( pxStreamBuffer );
718 /* The maximum amount of space a stream buffer will ever report is its length
720 xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
722 /* This send function is used to write to both message buffers and stream
723 * buffers. If this is a message buffer then the space needed must be
724 * increased by the amount of bytes needed to store the length of the
726 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
728 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
731 configASSERT( xRequiredSpace > xDataLengthBytes );
733 /* If this is a message buffer then it must be possible to write the
735 if( xRequiredSpace > xMaxReportedSpace )
737 /* The message would not fit even if the entire buffer was empty,
738 * so don't wait for space. */
739 xTicksToWait = ( TickType_t ) 0;
743 mtCOVERAGE_TEST_MARKER();
748 /* If this is a stream buffer then it is acceptable to write only part
749 * of the message to the buffer. Cap the length to the total length of
751 if( xRequiredSpace > xMaxReportedSpace )
753 xRequiredSpace = xMaxReportedSpace;
757 mtCOVERAGE_TEST_MARKER();
761 if( xTicksToWait != ( TickType_t ) 0 )
763 vTaskSetTimeOutState( &xTimeOut );
767 /* Wait until the required number of bytes are free in the message
769 taskENTER_CRITICAL();
771 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
773 if( xSpace < xRequiredSpace )
775 /* Clear notification state as going to wait for space. */
776 ( void ) xTaskNotifyStateClear( NULL );
778 /* Should only be one writer. */
779 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
780 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
790 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
791 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
792 pxStreamBuffer->xTaskWaitingToSend = NULL;
793 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
797 mtCOVERAGE_TEST_MARKER();
800 if( xSpace == ( size_t ) 0 )
802 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
806 mtCOVERAGE_TEST_MARKER();
809 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
811 if( xReturn > ( size_t ) 0 )
813 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
815 /* Was a task waiting for the data? */
816 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
818 prvSEND_COMPLETED( pxStreamBuffer );
822 mtCOVERAGE_TEST_MARKER();
827 mtCOVERAGE_TEST_MARKER();
828 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
831 traceRETURN_xStreamBufferSend( xReturn );
835 /*-----------------------------------------------------------*/
837 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
838 const void * pvTxData,
839 size_t xDataLengthBytes,
840 BaseType_t * const pxHigherPriorityTaskWoken )
842 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
843 size_t xReturn, xSpace;
844 size_t xRequiredSpace = xDataLengthBytes;
846 traceENTER_xStreamBufferSendFromISR( xStreamBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken );
848 configASSERT( pvTxData );
849 configASSERT( pxStreamBuffer );
851 /* This send function is used to write to both message buffers and stream
852 * buffers. If this is a message buffer then the space needed must be
853 * increased by the amount of bytes needed to store the length of the
855 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
857 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
861 mtCOVERAGE_TEST_MARKER();
864 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
865 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
867 if( xReturn > ( size_t ) 0 )
869 /* Was a task waiting for the data? */
870 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
872 prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
876 mtCOVERAGE_TEST_MARKER();
881 mtCOVERAGE_TEST_MARKER();
884 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
885 traceRETURN_xStreamBufferSendFromISR( xReturn );
889 /*-----------------------------------------------------------*/
891 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
892 const void * pvTxData,
893 size_t xDataLengthBytes,
895 size_t xRequiredSpace )
897 size_t xNextHead = pxStreamBuffer->xHead;
898 configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
900 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
902 /* This is a message buffer, as opposed to a stream buffer. */
904 /* Convert xDataLengthBytes to the message length type. */
905 xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
907 /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
908 configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
910 if( xSpace >= xRequiredSpace )
912 /* There is enough space to write both the message length and the message
913 * itself into the buffer. Start by writing the length of the data, the data
914 * itself will be written later in this function. */
915 xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
919 /* Not enough space, so do not write data to the buffer. */
920 xDataLengthBytes = 0;
925 /* This is a stream buffer, as opposed to a message buffer, so writing a
926 * stream of bytes rather than discrete messages. Plan to write as many
927 * bytes as possible. */
928 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
931 if( xDataLengthBytes != ( size_t ) 0 )
933 /* Write the data to the buffer. */
934 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. */
937 return xDataLengthBytes;
939 /*-----------------------------------------------------------*/
941 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
943 size_t xBufferLengthBytes,
944 TickType_t xTicksToWait )
946 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
947 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
949 traceENTER_xStreamBufferReceive( xStreamBuffer, pvRxData, xBufferLengthBytes, xTicksToWait );
951 configASSERT( pvRxData );
952 configASSERT( pxStreamBuffer );
954 /* This receive function is used by both message buffers, which store
955 * discrete messages, and stream buffers, which store a continuous stream of
956 * bytes. Discrete messages include an additional
957 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
959 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
961 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
965 xBytesToStoreMessageLength = 0;
968 if( xTicksToWait != ( TickType_t ) 0 )
970 /* Checking if there is data and clearing the notification state must be
971 * performed atomically. */
972 taskENTER_CRITICAL();
974 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
976 /* If this function was invoked by a message buffer read then
977 * xBytesToStoreMessageLength holds the number of bytes used to hold
978 * the length of the next discrete message. If this function was
979 * invoked by a stream buffer read then xBytesToStoreMessageLength will
981 if( xBytesAvailable <= xBytesToStoreMessageLength )
983 /* Clear notification state as going to wait for data. */
984 ( void ) xTaskNotifyStateClear( NULL );
986 /* Should only be one reader. */
987 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
988 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
992 mtCOVERAGE_TEST_MARKER();
997 if( xBytesAvailable <= xBytesToStoreMessageLength )
999 /* Wait for data to be available. */
1000 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
1001 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
1002 pxStreamBuffer->xTaskWaitingToReceive = NULL;
1004 /* Recheck the data available after blocking. */
1005 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1009 mtCOVERAGE_TEST_MARKER();
1014 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1017 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1018 * holds the number of bytes used to store the message length) or a stream of
1019 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1020 * available must be greater than xBytesToStoreMessageLength to be able to
1021 * read bytes from the buffer. */
1022 if( xBytesAvailable > xBytesToStoreMessageLength )
1024 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1026 /* Was a task waiting for space in the buffer? */
1027 if( xReceivedLength != ( size_t ) 0 )
1029 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
1030 prvRECEIVE_COMPLETED( xStreamBuffer );
1034 mtCOVERAGE_TEST_MARKER();
1039 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
1040 mtCOVERAGE_TEST_MARKER();
1043 traceRETURN_xStreamBufferReceive( xReceivedLength );
1045 return xReceivedLength;
1047 /*-----------------------------------------------------------*/
1049 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
1051 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1052 size_t xReturn, xBytesAvailable;
1053 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
1055 traceENTER_xStreamBufferNextMessageLengthBytes( xStreamBuffer );
1057 configASSERT( pxStreamBuffer );
1059 /* Ensure the stream buffer is being used as a message buffer. */
1060 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1062 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1064 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
1066 /* The number of bytes available is greater than the number of bytes
1067 * required to hold the length of the next message, so another message
1069 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
1070 xReturn = ( size_t ) xTempReturn;
1074 /* The minimum amount of bytes in a message buffer is
1075 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
1076 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
1078 configASSERT( xBytesAvailable == 0 );
1087 traceRETURN_xStreamBufferNextMessageLengthBytes( xReturn );
1091 /*-----------------------------------------------------------*/
1093 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
1095 size_t xBufferLengthBytes,
1096 BaseType_t * const pxHigherPriorityTaskWoken )
1098 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1099 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
1101 traceENTER_xStreamBufferReceiveFromISR( xStreamBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken );
1103 configASSERT( pvRxData );
1104 configASSERT( pxStreamBuffer );
1106 /* This receive function is used by both message buffers, which store
1107 * discrete messages, and stream buffers, which store a continuous stream of
1108 * bytes. Discrete messages include an additional
1109 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
1111 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1113 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1117 xBytesToStoreMessageLength = 0;
1120 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
1122 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
1123 * holds the number of bytes used to store the message length) or a stream of
1124 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
1125 * available must be greater than xBytesToStoreMessageLength to be able to
1126 * read bytes from the buffer. */
1127 if( xBytesAvailable > xBytesToStoreMessageLength )
1129 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
1131 /* Was a task waiting for space in the buffer? */
1132 if( xReceivedLength != ( size_t ) 0 )
1134 prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
1138 mtCOVERAGE_TEST_MARKER();
1143 mtCOVERAGE_TEST_MARKER();
1146 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
1147 traceRETURN_xStreamBufferReceiveFromISR( xReceivedLength );
1149 return xReceivedLength;
1151 /*-----------------------------------------------------------*/
1153 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
1155 size_t xBufferLengthBytes,
1156 size_t xBytesAvailable )
1158 size_t xCount, xNextMessageLength;
1159 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
1160 size_t xNextTail = pxStreamBuffer->xTail;
1162 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1164 /* A discrete message is being received. First receive the length
1165 * of the message. */
1166 xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
1167 xNextMessageLength = ( size_t ) xTempNextMessageLength;
1169 /* Reduce the number of bytes available by the number of bytes just
1171 xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
1173 /* Check there is enough space in the buffer provided by the
1175 if( xNextMessageLength > xBufferLengthBytes )
1177 /* The user has provided insufficient space to read the message. */
1178 xNextMessageLength = 0;
1182 mtCOVERAGE_TEST_MARKER();
1187 /* A stream of bytes is being received (as opposed to a discrete
1188 * message), so read as many bytes as possible. */
1189 xNextMessageLength = xBufferLengthBytes;
1192 /* Use the minimum of the wanted bytes and the available bytes. */
1193 xCount = configMIN( xNextMessageLength, xBytesAvailable );
1195 if( xCount != ( size_t ) 0 )
1197 /* Read the actual data and update the tail to mark the data as officially consumed. */
1198 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. */
1203 /*-----------------------------------------------------------*/
1205 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
1207 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1211 traceENTER_xStreamBufferIsEmpty( xStreamBuffer );
1213 configASSERT( pxStreamBuffer );
1215 /* True if no bytes are available. */
1216 xTail = pxStreamBuffer->xTail;
1218 if( pxStreamBuffer->xHead == xTail )
1227 traceRETURN_xStreamBufferIsEmpty( xReturn );
1231 /*-----------------------------------------------------------*/
1233 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
1236 size_t xBytesToStoreMessageLength;
1237 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1239 traceENTER_xStreamBufferIsFull( xStreamBuffer );
1241 configASSERT( pxStreamBuffer );
1243 /* This generic version of the receive function is used by both message
1244 * buffers, which store discrete messages, and stream buffers, which store a
1245 * continuous stream of bytes. Discrete messages include an additional
1246 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
1247 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
1249 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
1253 xBytesToStoreMessageLength = 0;
1256 /* True if the available space equals zero. */
1257 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
1266 traceRETURN_xStreamBufferIsFull( xReturn );
1270 /*-----------------------------------------------------------*/
1272 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1273 BaseType_t * pxHigherPriorityTaskWoken )
1275 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1277 UBaseType_t uxSavedInterruptStatus;
1279 traceENTER_xStreamBufferSendCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1281 configASSERT( pxStreamBuffer );
1283 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1285 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
1287 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
1290 pxHigherPriorityTaskWoken );
1291 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
1299 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1301 traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
1305 /*-----------------------------------------------------------*/
1307 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
1308 BaseType_t * pxHigherPriorityTaskWoken )
1310 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
1312 UBaseType_t uxSavedInterruptStatus;
1314 traceENTER_xStreamBufferReceiveCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
1316 configASSERT( pxStreamBuffer );
1318 uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
1320 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
1322 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
1325 pxHigherPriorityTaskWoken );
1326 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
1334 taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
1336 traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
1340 /*-----------------------------------------------------------*/
1342 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
1343 const uint8_t * pucData,
1347 size_t xFirstLength;
1349 configASSERT( xCount > ( size_t ) 0 );
1351 /* Calculate the number of bytes that can be added in the first write -
1352 * which may be less than the total number of bytes that need to be added if
1353 * the buffer will wrap back to the beginning. */
1354 xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
1356 /* Write as many bytes as can be written in the first write. */
1357 configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
1358 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1360 /* If the number of bytes written was less than the number that could be
1361 * written in the first write... */
1362 if( xCount > xFirstLength )
1364 /* ...then write the remaining bytes to the start of the buffer. */
1365 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
1366 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1370 mtCOVERAGE_TEST_MARKER();
1375 if( xHead >= pxStreamBuffer->xLength )
1377 xHead -= pxStreamBuffer->xLength;
1381 mtCOVERAGE_TEST_MARKER();
1386 /*-----------------------------------------------------------*/
1388 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
1393 size_t xFirstLength;
1395 configASSERT( xCount != ( size_t ) 0 );
1397 /* Calculate the number of bytes that can be read - which may be
1398 * less than the number wanted if the data wraps around to the start of
1400 xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
1402 /* Obtain the number of bytes it is possible to obtain in the first
1403 * read. Asserts check bounds of read and write. */
1404 configASSERT( xFirstLength <= xCount );
1405 configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
1406 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1408 /* If the total number of wanted bytes is greater than the number
1409 * that could be read in the first read... */
1410 if( xCount > xFirstLength )
1412 /* ...then read the remaining bytes from the start of the buffer. */
1413 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
1417 mtCOVERAGE_TEST_MARKER();
1420 /* Move the tail pointer to effectively remove the data read from the buffer. */
1423 if( xTail >= pxStreamBuffer->xLength )
1425 xTail -= pxStreamBuffer->xLength;
1430 /*-----------------------------------------------------------*/
1432 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
1434 /* Returns the distance between xTail and xHead. */
1437 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
1438 xCount -= pxStreamBuffer->xTail;
1440 if( xCount >= pxStreamBuffer->xLength )
1442 xCount -= pxStreamBuffer->xLength;
1446 mtCOVERAGE_TEST_MARKER();
1451 /*-----------------------------------------------------------*/
1453 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
1454 uint8_t * const pucBuffer,
1455 size_t xBufferSizeBytes,
1456 size_t xTriggerLevelBytes,
1458 StreamBufferCallbackFunction_t pxSendCompletedCallback,
1459 StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
1461 /* Assert here is deliberately writing to the entire buffer to ensure it can
1462 * be written to without generating exceptions, and is setting the buffer to a
1463 * known value to assist in development/debugging. */
1464 #if ( configASSERT_DEFINED == 1 )
1466 /* The value written just has to be identifiable when looking at the
1467 * memory. Don't use 0xA5 as that is the stack fill value and could
1468 * result in confusion as to what is actually being observed. */
1469 #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
1470 configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
1474 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
1475 pxStreamBuffer->pucBuffer = pucBuffer;
1476 pxStreamBuffer->xLength = xBufferSizeBytes;
1477 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
1478 pxStreamBuffer->ucFlags = ucFlags;
1479 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
1481 pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
1482 pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
1486 ( void ) pxSendCompletedCallback;
1487 ( void ) pxReceiveCompletedCallback;
1492 #if ( configUSE_TRACE_FACILITY == 1 )
1494 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
1496 traceENTER_uxStreamBufferGetStreamBufferNumber( xStreamBuffer );
1498 traceRETURN_uxStreamBufferGetStreamBufferNumber( xStreamBuffer->uxStreamBufferNumber );
1500 return xStreamBuffer->uxStreamBufferNumber;
1503 #endif /* configUSE_TRACE_FACILITY */
1504 /*-----------------------------------------------------------*/
1506 #if ( configUSE_TRACE_FACILITY == 1 )
1508 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
1509 UBaseType_t uxStreamBufferNumber )
1511 traceENTER_vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxStreamBufferNumber );
1513 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
1515 traceRETURN_vStreamBufferSetStreamBufferNumber();
1518 #endif /* configUSE_TRACE_FACILITY */
1519 /*-----------------------------------------------------------*/
1521 #if ( configUSE_TRACE_FACILITY == 1 )
1523 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
1525 traceENTER_ucStreamBufferGetStreamBufferType( xStreamBuffer );
1527 traceRETURN_ucStreamBufferGetStreamBufferType( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1529 return( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
1532 #endif /* configUSE_TRACE_FACILITY */
1533 /*-----------------------------------------------------------*/