2 * FreeRTOS Kernel V10.4.3
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * https://www.FreeRTOS.org
\r
23 * https://github.com/FreeRTOS
\r
27 /* Standard includes. */
\r
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
32 * all the API functions to use the MPU wrappers. That should only be done when
\r
33 * task.h is included from an application file. */
\r
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
36 /* FreeRTOS includes. */
\r
37 #include "FreeRTOS.h"
\r
39 #include "stream_buffer.h"
\r
41 #if ( configUSE_TASK_NOTIFICATIONS != 1 )
\r
42 #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
\r
45 /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified
\r
46 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
\r
47 * for the header files above, but not in this file, in order to generate the
\r
48 * correct privileged Vs unprivileged linkage and placement. */
\r
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
\r
51 /* If the user has not provided application specific Rx notification macros,
\r
52 * or #defined the notification macros away, them provide default implementations
\r
53 * that uses task notifications. */
\r
54 /*lint -save -e9026 Function like macros allowed and needed here so they can be overridden. */
\r
55 #ifndef sbRECEIVE_COMPLETED
\r
56 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
\r
57 vTaskSuspendAll(); \
\r
59 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
\r
61 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
\r
64 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
\r
67 ( void ) xTaskResumeAll();
\r
68 #endif /* sbRECEIVE_COMPLETED */
\r
70 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
\r
71 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
\r
72 pxHigherPriorityTaskWoken ) \
\r
74 UBaseType_t uxSavedInterruptStatus; \
\r
76 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
\r
78 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
\r
80 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
\r
83 pxHigherPriorityTaskWoken ); \
\r
84 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
\r
87 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
\r
89 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
\r
91 /* If the user has not provided an application specific Tx notification macro,
\r
92 * or #defined the notification macro away, them provide a default implementation
\r
93 * that uses task notifications. */
\r
94 #ifndef sbSEND_COMPLETED
\r
95 #define sbSEND_COMPLETED( pxStreamBuffer ) \
\r
96 vTaskSuspendAll(); \
\r
98 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
\r
100 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
\r
103 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
\r
106 ( void ) xTaskResumeAll();
\r
107 #endif /* sbSEND_COMPLETED */
\r
109 #ifndef sbSEND_COMPLETE_FROM_ISR
\r
110 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
\r
112 UBaseType_t uxSavedInterruptStatus; \
\r
114 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
\r
116 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
\r
118 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
\r
121 pxHigherPriorityTaskWoken ); \
\r
122 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
\r
125 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
\r
127 #endif /* sbSEND_COMPLETE_FROM_ISR */
\r
128 /*lint -restore (9026) */
\r
130 /* The number of bytes used to hold the length of a message in the buffer. */
\r
131 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
\r
133 /* Bits stored in the ucFlags field of the stream buffer. */
\r
134 #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. */
\r
135 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
\r
137 /*-----------------------------------------------------------*/
\r
139 /* Structure that hold state information on the buffer. */
\r
140 typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
\r
142 volatile size_t xTail; /* Index to the next item to read within the buffer. */
\r
143 volatile size_t xHead; /* Index to the next item to write within the buffer. */
\r
144 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
\r
145 size_t xTriggerLevelBytes; /* The number of bytes that must be in the stream buffer before a task that is waiting for data is unblocked. */
\r
146 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
\r
147 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
\r
148 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
\r
151 #if ( configUSE_TRACE_FACILITY == 1 )
\r
152 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
\r
157 * The number of bytes available to be read from the buffer.
\r
159 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
\r
162 * Add xCount bytes from pucData into the pxStreamBuffer message buffer.
\r
163 * Returns the number of bytes written, which will either equal xCount in the
\r
164 * success case, or 0 if there was not enough space in the buffer (in which case
\r
165 * no data is written into the buffer).
\r
167 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
168 const uint8_t * pucData,
\r
169 size_t xCount ) PRIVILEGED_FUNCTION;
\r
172 * If the stream buffer is being used as a message buffer, then reads an entire
\r
173 * message out of the buffer. If the stream buffer is being used as a stream
\r
174 * buffer then read as many bytes as possible from the buffer.
\r
175 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
\r
176 * buffer's data storage area.
\r
178 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
180 size_t xBufferLengthBytes,
\r
181 size_t xBytesAvailable,
\r
182 size_t xBytesToStoreMessageLength ) PRIVILEGED_FUNCTION;
\r
185 * If the stream buffer is being used as a message buffer, then writes an entire
\r
186 * message to the buffer. If the stream buffer is being used as a stream
\r
187 * buffer then write as many bytes as possible to the buffer.
\r
188 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
\r
189 * data storage area.
\r
191 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
192 const void * pvTxData,
\r
193 size_t xDataLengthBytes,
\r
195 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
\r
198 * Read xMaxCount bytes from the pxStreamBuffer message buffer and write them
\r
201 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
204 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
\r
207 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
\r
208 * initialise the members of the newly created stream buffer structure.
\r
210 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
211 uint8_t * const pucBuffer,
\r
212 size_t xBufferSizeBytes,
\r
213 size_t xTriggerLevelBytes,
\r
214 uint8_t ucFlags ) PRIVILEGED_FUNCTION;
\r
216 /*-----------------------------------------------------------*/
\r
218 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
220 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
\r
221 size_t xTriggerLevelBytes,
\r
222 BaseType_t xIsMessageBuffer )
\r
224 uint8_t * pucAllocatedMemory;
\r
227 /* In case the stream buffer is going to be used as a message buffer
\r
228 * (that is, it will hold discrete messages with a little meta data that
\r
229 * says how big the next message is) check the buffer will be large enough
\r
230 * to hold at least one message. */
\r
231 if( xIsMessageBuffer == pdTRUE )
\r
233 /* Is a message buffer but not statically allocated. */
\r
234 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
\r
235 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
239 /* Not a message buffer and not statically allocated. */
\r
241 configASSERT( xBufferSizeBytes > 0 );
\r
244 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
\r
246 /* A trigger level of 0 would cause a waiting task to unblock even when
\r
247 * the buffer was empty. */
\r
248 if( xTriggerLevelBytes == ( size_t ) 0 )
\r
250 xTriggerLevelBytes = ( size_t ) 1;
\r
253 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
\r
254 * Both are allocated in a single call to pvPortMalloc(). The
\r
255 * StreamBuffer_t structure is placed at the start of the allocated memory
\r
256 * and the buffer follows immediately after. The requested size is
\r
257 * incremented so the free space is returned as the user would expect -
\r
258 * this is a quirk of the implementation that means otherwise the free
\r
259 * space would be reported as one byte smaller than would be logically
\r
261 if( xBufferSizeBytes < ( xBufferSizeBytes + 1 + sizeof( StreamBuffer_t ) ) )
\r
263 xBufferSizeBytes++;
\r
264 pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
\r
268 pucAllocatedMemory = NULL;
\r
272 if( pucAllocatedMemory != NULL )
\r
274 prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pucAllocatedMemory, /* 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. */
\r
275 pucAllocatedMemory + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */
\r
277 xTriggerLevelBytes,
\r
280 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer );
\r
284 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
\r
287 return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
\r
290 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
291 /*-----------------------------------------------------------*/
\r
293 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
295 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
\r
296 size_t xTriggerLevelBytes,
\r
297 BaseType_t xIsMessageBuffer,
\r
298 uint8_t * const pucStreamBufferStorageArea,
\r
299 StaticStreamBuffer_t * const pxStaticStreamBuffer )
\r
301 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
\r
302 StreamBufferHandle_t xReturn;
\r
305 configASSERT( pucStreamBufferStorageArea );
\r
306 configASSERT( pxStaticStreamBuffer );
\r
307 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
\r
309 /* A trigger level of 0 would cause a waiting task to unblock even when
\r
310 * the buffer was empty. */
\r
311 if( xTriggerLevelBytes == ( size_t ) 0 )
\r
313 xTriggerLevelBytes = ( size_t ) 1;
\r
316 if( xIsMessageBuffer != pdFALSE )
\r
318 /* Statically allocated message buffer. */
\r
319 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
323 /* Statically allocated stream buffer. */
\r
324 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
327 /* In case the stream buffer is going to be used as a message buffer
\r
328 * (that is, it will hold discrete messages with a little meta data that
\r
329 * says how big the next message is) check the buffer will be large enough
\r
330 * to hold at least one message. */
\r
331 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
333 #if ( configASSERT_DEFINED == 1 )
\r
335 /* Sanity check that the size of the structure used to declare a
\r
336 * variable of type StaticStreamBuffer_t equals the size of the real
\r
337 * message buffer structure. */
\r
338 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
\r
339 configASSERT( xSize == sizeof( StreamBuffer_t ) );
\r
340 } /*lint !e529 xSize is referenced is configASSERT() is defined. */
\r
341 #endif /* configASSERT_DEFINED */
\r
343 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
\r
345 prvInitialiseNewStreamBuffer( pxStreamBuffer,
\r
346 pucStreamBufferStorageArea,
\r
348 xTriggerLevelBytes,
\r
351 /* Remember this was statically allocated in case it is ever deleted
\r
353 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
355 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
\r
357 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
\r
362 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
\r
368 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
\r
369 /*-----------------------------------------------------------*/
\r
371 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
\r
373 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
\r
375 configASSERT( pxStreamBuffer );
\r
377 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
\r
379 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
\r
381 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
383 /* Both the structure and the buffer were allocated using a single call
\r
384 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
\r
385 vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
\r
389 /* Should not be possible to get here, ucFlags must be corrupt.
\r
390 * Force an assert. */
\r
391 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
\r
397 /* The structure and buffer were not allocated dynamically and cannot be
\r
398 * freed - just scrub the structure so future use will assert. */
\r
399 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
\r
402 /*-----------------------------------------------------------*/
\r
404 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
\r
406 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
407 BaseType_t xReturn = pdFAIL;
\r
409 #if ( configUSE_TRACE_FACILITY == 1 )
\r
410 UBaseType_t uxStreamBufferNumber;
\r
413 configASSERT( pxStreamBuffer );
\r
415 #if ( configUSE_TRACE_FACILITY == 1 )
\r
417 /* Store the stream buffer number so it can be restored after the
\r
419 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
\r
423 /* Can only reset a message buffer if there are no tasks blocked on it. */
\r
424 taskENTER_CRITICAL();
\r
426 if( pxStreamBuffer->xTaskWaitingToReceive == NULL )
\r
428 if( pxStreamBuffer->xTaskWaitingToSend == NULL )
\r
430 prvInitialiseNewStreamBuffer( pxStreamBuffer,
\r
431 pxStreamBuffer->pucBuffer,
\r
432 pxStreamBuffer->xLength,
\r
433 pxStreamBuffer->xTriggerLevelBytes,
\r
434 pxStreamBuffer->ucFlags );
\r
437 #if ( configUSE_TRACE_FACILITY == 1 )
\r
439 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
\r
443 traceSTREAM_BUFFER_RESET( xStreamBuffer );
\r
447 taskEXIT_CRITICAL();
\r
451 /*-----------------------------------------------------------*/
\r
453 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
\r
454 size_t xTriggerLevel )
\r
456 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
457 BaseType_t xReturn;
\r
459 configASSERT( pxStreamBuffer );
\r
461 /* It is not valid for the trigger level to be 0. */
\r
462 if( xTriggerLevel == ( size_t ) 0 )
\r
464 xTriggerLevel = ( size_t ) 1;
\r
467 /* The trigger level is the number of bytes that must be in the stream
\r
468 * buffer before a task that is waiting for data is unblocked. */
\r
469 if( xTriggerLevel <= pxStreamBuffer->xLength )
\r
471 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
\r
481 /*-----------------------------------------------------------*/
\r
483 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
\r
485 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
488 configASSERT( pxStreamBuffer );
\r
490 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
\r
491 xSpace -= pxStreamBuffer->xHead;
\r
492 xSpace -= ( size_t ) 1;
\r
494 if( xSpace >= pxStreamBuffer->xLength )
\r
496 xSpace -= pxStreamBuffer->xLength;
\r
500 mtCOVERAGE_TEST_MARKER();
\r
505 /*-----------------------------------------------------------*/
\r
507 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
\r
509 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
512 configASSERT( pxStreamBuffer );
\r
514 xReturn = prvBytesInBuffer( pxStreamBuffer );
\r
517 /*-----------------------------------------------------------*/
\r
519 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
520 const void * pvTxData,
\r
521 size_t xDataLengthBytes,
\r
522 TickType_t xTicksToWait )
\r
524 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
525 size_t xReturn, xSpace = 0;
\r
526 size_t xRequiredSpace = xDataLengthBytes;
\r
527 TimeOut_t xTimeOut;
\r
529 /* The maximum amount of space a stream buffer will ever report is its length
\r
531 const size_t xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
\r
533 configASSERT( pvTxData );
\r
534 configASSERT( pxStreamBuffer );
\r
536 /* This send function is used to write to both message buffers and stream
\r
537 * buffers. If this is a message buffer then the space needed must be
\r
538 * increased by the amount of bytes needed to store the length of the
\r
540 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
542 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
545 configASSERT( xRequiredSpace > xDataLengthBytes );
\r
547 /* If this is a message buffer then it must be possible to write the
\r
548 * whole message. */
\r
549 if( xRequiredSpace > xMaxReportedSpace )
\r
551 /* The message would not fit even if the entire buffer was empty,
\r
552 * so don't wait for space. */
\r
553 xTicksToWait = ( TickType_t ) 0;
\r
557 mtCOVERAGE_TEST_MARKER();
\r
562 /* If this is a stream buffer then it is acceptable to write only part
\r
563 * of the message to the buffer. Cap the length to the total length of
\r
565 if( xRequiredSpace > xMaxReportedSpace )
\r
567 xRequiredSpace = xMaxReportedSpace;
\r
571 mtCOVERAGE_TEST_MARKER();
\r
575 if( xTicksToWait != ( TickType_t ) 0 )
\r
577 vTaskSetTimeOutState( &xTimeOut );
\r
581 /* Wait until the required number of bytes are free in the message
\r
583 taskENTER_CRITICAL();
\r
585 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
587 if( xSpace < xRequiredSpace )
\r
589 /* Clear notification state as going to wait for space. */
\r
590 ( void ) xTaskNotifyStateClear( NULL );
\r
592 /* Should only be one writer. */
\r
593 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
\r
594 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
\r
598 taskEXIT_CRITICAL();
\r
602 taskEXIT_CRITICAL();
\r
604 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
\r
605 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
\r
606 pxStreamBuffer->xTaskWaitingToSend = NULL;
\r
607 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
\r
611 mtCOVERAGE_TEST_MARKER();
\r
614 if( xSpace == ( size_t ) 0 )
\r
616 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
620 mtCOVERAGE_TEST_MARKER();
\r
623 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
\r
625 if( xReturn > ( size_t ) 0 )
\r
627 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
\r
629 /* Was a task waiting for the data? */
\r
630 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
\r
632 sbSEND_COMPLETED( pxStreamBuffer );
\r
636 mtCOVERAGE_TEST_MARKER();
\r
641 mtCOVERAGE_TEST_MARKER();
\r
642 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
\r
647 /*-----------------------------------------------------------*/
\r
649 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
650 const void * pvTxData,
\r
651 size_t xDataLengthBytes,
\r
652 BaseType_t * const pxHigherPriorityTaskWoken )
\r
654 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
655 size_t xReturn, xSpace;
\r
656 size_t xRequiredSpace = xDataLengthBytes;
\r
658 configASSERT( pvTxData );
\r
659 configASSERT( pxStreamBuffer );
\r
661 /* This send function is used to write to both message buffers and stream
\r
662 * buffers. If this is a message buffer then the space needed must be
\r
663 * increased by the amount of bytes needed to store the length of the
\r
665 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
667 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
671 mtCOVERAGE_TEST_MARKER();
\r
674 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
675 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
\r
677 if( xReturn > ( size_t ) 0 )
\r
679 /* Was a task waiting for the data? */
\r
680 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
\r
682 sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
\r
686 mtCOVERAGE_TEST_MARKER();
\r
691 mtCOVERAGE_TEST_MARKER();
\r
694 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
\r
698 /*-----------------------------------------------------------*/
\r
700 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
701 const void * pvTxData,
\r
702 size_t xDataLengthBytes,
\r
704 size_t xRequiredSpace )
\r
706 BaseType_t xShouldWrite;
\r
709 if( xSpace == ( size_t ) 0 )
\r
711 /* Doesn't matter if this is a stream buffer or a message buffer, there
\r
712 * is no space to write. */
\r
713 xShouldWrite = pdFALSE;
\r
715 else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
\r
717 /* This is a stream buffer, as opposed to a message buffer, so writing a
\r
718 * stream of bytes rather than discrete messages. Write as many bytes as
\r
720 xShouldWrite = pdTRUE;
\r
721 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
\r
723 else if( xSpace >= xRequiredSpace )
\r
725 /* This is a message buffer, as opposed to a stream buffer, and there
\r
726 * is enough space to write both the message length and the message itself
\r
727 * into the buffer. Start by writing the length of the data, the data
\r
728 * itself will be written later in this function. */
\r
729 xShouldWrite = pdTRUE;
\r
730 ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
734 /* There is space available, but not enough space. */
\r
735 xShouldWrite = pdFALSE;
\r
738 if( xShouldWrite != pdFALSE )
\r
740 /* Writes the data itself. */
\r
741 xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alignment and access. */
\r
750 /*-----------------------------------------------------------*/
\r
752 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
754 size_t xBufferLengthBytes,
\r
755 TickType_t xTicksToWait )
\r
757 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
758 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
\r
760 configASSERT( pvRxData );
\r
761 configASSERT( pxStreamBuffer );
\r
763 /* This receive function is used by both message buffers, which store
\r
764 * discrete messages, and stream buffers, which store a continuous stream of
\r
765 * bytes. Discrete messages include an additional
\r
766 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
\r
768 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
770 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
774 xBytesToStoreMessageLength = 0;
\r
777 if( xTicksToWait != ( TickType_t ) 0 )
\r
779 /* Checking if there is data and clearing the notification state must be
\r
780 * performed atomically. */
\r
781 taskENTER_CRITICAL();
\r
783 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
785 /* If this function was invoked by a message buffer read then
\r
786 * xBytesToStoreMessageLength holds the number of bytes used to hold
\r
787 * the length of the next discrete message. If this function was
\r
788 * invoked by a stream buffer read then xBytesToStoreMessageLength will
\r
790 if( xBytesAvailable <= xBytesToStoreMessageLength )
\r
792 /* Clear notification state as going to wait for data. */
\r
793 ( void ) xTaskNotifyStateClear( NULL );
\r
795 /* Should only be one reader. */
\r
796 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
\r
797 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
\r
801 mtCOVERAGE_TEST_MARKER();
\r
804 taskEXIT_CRITICAL();
\r
806 if( xBytesAvailable <= xBytesToStoreMessageLength )
\r
808 /* Wait for data to be available. */
\r
809 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
\r
810 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
\r
811 pxStreamBuffer->xTaskWaitingToReceive = NULL;
\r
813 /* Recheck the data available after blocking. */
\r
814 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
818 mtCOVERAGE_TEST_MARKER();
\r
823 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
826 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
\r
827 * holds the number of bytes used to store the message length) or a stream of
\r
828 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
\r
829 * available must be greater than xBytesToStoreMessageLength to be able to
\r
830 * read bytes from the buffer. */
\r
831 if( xBytesAvailable > xBytesToStoreMessageLength )
\r
833 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
\r
835 /* Was a task waiting for space in the buffer? */
\r
836 if( xReceivedLength != ( size_t ) 0 )
\r
838 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
\r
839 sbRECEIVE_COMPLETED( pxStreamBuffer );
\r
843 mtCOVERAGE_TEST_MARKER();
\r
848 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
\r
849 mtCOVERAGE_TEST_MARKER();
\r
852 return xReceivedLength;
\r
854 /*-----------------------------------------------------------*/
\r
856 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
\r
858 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
859 size_t xReturn, xBytesAvailable, xOriginalTail;
\r
860 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
\r
862 configASSERT( pxStreamBuffer );
\r
864 /* Ensure the stream buffer is being used as a message buffer. */
\r
865 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
867 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
869 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
\r
871 /* The number of bytes available is greater than the number of bytes
\r
872 * required to hold the length of the next message, so another message
\r
873 * is available. Return its length without removing the length bytes
\r
874 * from the buffer. A copy of the tail is stored so the buffer can be
\r
875 * returned to its prior state as the message is not actually being
\r
876 * removed from the buffer. */
\r
877 xOriginalTail = pxStreamBuffer->xTail;
\r
878 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
\r
879 xReturn = ( size_t ) xTempReturn;
\r
880 pxStreamBuffer->xTail = xOriginalTail;
\r
884 /* The minimum amount of bytes in a message buffer is
\r
885 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
\r
886 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
\r
888 configASSERT( xBytesAvailable == 0 );
\r
899 /*-----------------------------------------------------------*/
\r
901 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
903 size_t xBufferLengthBytes,
\r
904 BaseType_t * const pxHigherPriorityTaskWoken )
\r
906 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
907 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
\r
909 configASSERT( pvRxData );
\r
910 configASSERT( pxStreamBuffer );
\r
912 /* This receive function is used by both message buffers, which store
\r
913 * discrete messages, and stream buffers, which store a continuous stream of
\r
914 * bytes. Discrete messages include an additional
\r
915 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
\r
917 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
919 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
923 xBytesToStoreMessageLength = 0;
\r
926 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
928 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
\r
929 * holds the number of bytes used to store the message length) or a stream of
\r
930 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
\r
931 * available must be greater than xBytesToStoreMessageLength to be able to
\r
932 * read bytes from the buffer. */
\r
933 if( xBytesAvailable > xBytesToStoreMessageLength )
\r
935 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
\r
937 /* Was a task waiting for space in the buffer? */
\r
938 if( xReceivedLength != ( size_t ) 0 )
\r
940 sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
\r
944 mtCOVERAGE_TEST_MARKER();
\r
949 mtCOVERAGE_TEST_MARKER();
\r
952 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
\r
954 return xReceivedLength;
\r
956 /*-----------------------------------------------------------*/
\r
958 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
960 size_t xBufferLengthBytes,
\r
961 size_t xBytesAvailable,
\r
962 size_t xBytesToStoreMessageLength )
\r
964 size_t xOriginalTail, xReceivedLength, xNextMessageLength;
\r
965 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
\r
967 if( xBytesToStoreMessageLength != ( size_t ) 0 )
\r
969 /* A discrete message is being received. First receive the length
\r
970 * of the message. A copy of the tail is stored so the buffer can be
\r
971 * returned to its prior state if the length of the message is too
\r
972 * large for the provided buffer. */
\r
973 xOriginalTail = pxStreamBuffer->xTail;
\r
974 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
\r
975 xNextMessageLength = ( size_t ) xTempNextMessageLength;
\r
977 /* Reduce the number of bytes available by the number of bytes just
\r
979 xBytesAvailable -= xBytesToStoreMessageLength;
\r
981 /* Check there is enough space in the buffer provided by the
\r
983 if( xNextMessageLength > xBufferLengthBytes )
\r
985 /* The user has provided insufficient space to read the message
\r
986 * so return the buffer to its previous state (so the length of
\r
987 * the message is in the buffer again). */
\r
988 pxStreamBuffer->xTail = xOriginalTail;
\r
989 xNextMessageLength = 0;
\r
993 mtCOVERAGE_TEST_MARKER();
\r
998 /* A stream of bytes is being received (as opposed to a discrete
\r
999 * message), so read as many bytes as possible. */
\r
1000 xNextMessageLength = xBufferLengthBytes;
\r
1003 /* Read the actual data. */
\r
1004 xReceivedLength = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xNextMessageLength, xBytesAvailable ); /*lint !e9079 Data storage area is implemented as uint8_t array for ease of sizing, indexing and alignment. */
\r
1006 return xReceivedLength;
\r
1008 /*-----------------------------------------------------------*/
\r
1010 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
\r
1012 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1013 BaseType_t xReturn;
\r
1016 configASSERT( pxStreamBuffer );
\r
1018 /* True if no bytes are available. */
\r
1019 xTail = pxStreamBuffer->xTail;
\r
1021 if( pxStreamBuffer->xHead == xTail )
\r
1027 xReturn = pdFALSE;
\r
1032 /*-----------------------------------------------------------*/
\r
1034 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
\r
1036 BaseType_t xReturn;
\r
1037 size_t xBytesToStoreMessageLength;
\r
1038 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1040 configASSERT( pxStreamBuffer );
\r
1042 /* This generic version of the receive function is used by both message
\r
1043 * buffers, which store discrete messages, and stream buffers, which store a
\r
1044 * continuous stream of bytes. Discrete messages include an additional
\r
1045 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
\r
1046 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
1048 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
1052 xBytesToStoreMessageLength = 0;
\r
1055 /* True if the available space equals zero. */
\r
1056 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
\r
1062 xReturn = pdFALSE;
\r
1067 /*-----------------------------------------------------------*/
\r
1069 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
1070 BaseType_t * pxHigherPriorityTaskWoken )
\r
1072 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1073 BaseType_t xReturn;
\r
1074 UBaseType_t uxSavedInterruptStatus;
\r
1076 configASSERT( pxStreamBuffer );
\r
1078 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
1080 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
\r
1082 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
\r
1085 pxHigherPriorityTaskWoken );
\r
1086 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
\r
1091 xReturn = pdFALSE;
\r
1094 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1098 /*-----------------------------------------------------------*/
\r
1100 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
1101 BaseType_t * pxHigherPriorityTaskWoken )
\r
1103 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1104 BaseType_t xReturn;
\r
1105 UBaseType_t uxSavedInterruptStatus;
\r
1107 configASSERT( pxStreamBuffer );
\r
1109 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
1111 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
\r
1113 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
\r
1116 pxHigherPriorityTaskWoken );
\r
1117 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
\r
1122 xReturn = pdFALSE;
\r
1125 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1129 /*-----------------------------------------------------------*/
\r
1131 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
1132 const uint8_t * pucData,
\r
1135 size_t xNextHead, xFirstLength;
\r
1137 configASSERT( xCount > ( size_t ) 0 );
\r
1139 xNextHead = pxStreamBuffer->xHead;
\r
1141 /* Calculate the number of bytes that can be added in the first write -
\r
1142 * which may be less than the total number of bytes that need to be added if
\r
1143 * the buffer will wrap back to the beginning. */
\r
1144 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
\r
1146 /* Write as many bytes as can be written in the first write. */
\r
1147 configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
\r
1148 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1150 /* If the number of bytes written was less than the number that could be
\r
1151 * written in the first write... */
\r
1152 if( xCount > xFirstLength )
\r
1154 /* ...then write the remaining bytes to the start of the buffer. */
\r
1155 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
\r
1156 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1160 mtCOVERAGE_TEST_MARKER();
\r
1163 xNextHead += xCount;
\r
1165 if( xNextHead >= pxStreamBuffer->xLength )
\r
1167 xNextHead -= pxStreamBuffer->xLength;
\r
1171 mtCOVERAGE_TEST_MARKER();
\r
1174 pxStreamBuffer->xHead = xNextHead;
\r
1178 /*-----------------------------------------------------------*/
\r
1180 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
1181 uint8_t * pucData,
\r
1183 size_t xBytesAvailable )
\r
1185 size_t xCount, xFirstLength, xNextTail;
\r
1187 /* Use the minimum of the wanted bytes and the available bytes. */
\r
1188 xCount = configMIN( xBytesAvailable, xMaxCount );
\r
1190 if( xCount > ( size_t ) 0 )
\r
1192 xNextTail = pxStreamBuffer->xTail;
\r
1194 /* Calculate the number of bytes that can be read - which may be
\r
1195 * less than the number wanted if the data wraps around to the start of
\r
1197 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
\r
1199 /* Obtain the number of bytes it is possible to obtain in the first
\r
1200 * read. Asserts check bounds of read and write. */
\r
1201 configASSERT( xFirstLength <= xMaxCount );
\r
1202 configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
\r
1203 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1205 /* If the total number of wanted bytes is greater than the number
\r
1206 * that could be read in the first read... */
\r
1207 if( xCount > xFirstLength )
\r
1209 /*...then read the remaining bytes from the start of the buffer. */
\r
1210 configASSERT( xCount <= xMaxCount );
\r
1211 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1215 mtCOVERAGE_TEST_MARKER();
\r
1218 /* Move the tail pointer to effectively remove the data read from
\r
1220 xNextTail += xCount;
\r
1222 if( xNextTail >= pxStreamBuffer->xLength )
\r
1224 xNextTail -= pxStreamBuffer->xLength;
\r
1227 pxStreamBuffer->xTail = xNextTail;
\r
1231 mtCOVERAGE_TEST_MARKER();
\r
1236 /*-----------------------------------------------------------*/
\r
1238 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
\r
1240 /* Returns the distance between xTail and xHead. */
\r
1243 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
\r
1244 xCount -= pxStreamBuffer->xTail;
\r
1246 if( xCount >= pxStreamBuffer->xLength )
\r
1248 xCount -= pxStreamBuffer->xLength;
\r
1252 mtCOVERAGE_TEST_MARKER();
\r
1257 /*-----------------------------------------------------------*/
\r
1259 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
1260 uint8_t * const pucBuffer,
\r
1261 size_t xBufferSizeBytes,
\r
1262 size_t xTriggerLevelBytes,
\r
1265 /* Assert here is deliberately writing to the entire buffer to ensure it can
\r
1266 * be written to without generating exceptions, and is setting the buffer to a
\r
1267 * known value to assist in development/debugging. */
\r
1268 #if ( configASSERT_DEFINED == 1 )
\r
1270 /* The value written just has to be identifiable when looking at the
\r
1271 * memory. Don't use 0xA5 as that is the stack fill value and could
\r
1272 * result in confusion as to what is actually being observed. */
\r
1273 const BaseType_t xWriteValue = 0x55;
\r
1274 configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
\r
1275 } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */
\r
1278 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
\r
1279 pxStreamBuffer->pucBuffer = pucBuffer;
\r
1280 pxStreamBuffer->xLength = xBufferSizeBytes;
\r
1281 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
\r
1282 pxStreamBuffer->ucFlags = ucFlags;
\r
1285 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1287 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
\r
1289 return xStreamBuffer->uxStreamBufferNumber;
\r
1292 #endif /* configUSE_TRACE_FACILITY */
\r
1293 /*-----------------------------------------------------------*/
\r
1295 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1297 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
\r
1298 UBaseType_t uxStreamBufferNumber )
\r
1300 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
\r
1303 #endif /* configUSE_TRACE_FACILITY */
\r
1304 /*-----------------------------------------------------------*/
\r
1306 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1308 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
\r
1310 return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
\r
1313 #endif /* configUSE_TRACE_FACILITY */
\r
1314 /*-----------------------------------------------------------*/
\r