2 * FreeRTOS Kernel V10.3.1
\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 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
33 * all the API functions to use the MPU wrappers. That should only be done when
\r
34 * task.h is included from an application file. */
\r
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
37 /* FreeRTOS includes. */
\r
38 #include "FreeRTOS.h"
\r
40 #include "stream_buffer.h"
\r
42 #if ( configUSE_TASK_NOTIFICATIONS != 1 )
\r
43 #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
\r
46 /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified
\r
47 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
\r
48 * for the header files above, but not in this file, in order to generate the
\r
49 * correct privileged Vs unprivileged linkage and placement. */
\r
50 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
\r
52 /* If the user has not provided application specific Rx notification macros,
\r
53 * or #defined the notification macros away, them provide default implementations
\r
54 * that uses task notifications. */
\r
55 /*lint -save -e9026 Function like macros allowed and needed here so they can be overidden. */
\r
56 #ifndef sbRECEIVE_COMPLETED
\r
57 #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
\r
58 vTaskSuspendAll(); \
\r
60 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
\r
62 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
\r
65 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
\r
68 ( void ) xTaskResumeAll();
\r
69 #endif /* sbRECEIVE_COMPLETED */
\r
71 #ifndef sbRECEIVE_COMPLETED_FROM_ISR
\r
72 #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
\r
73 pxHigherPriorityTaskWoken ) \
\r
75 UBaseType_t uxSavedInterruptStatus; \
\r
77 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
\r
79 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
\r
81 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
\r
84 pxHigherPriorityTaskWoken ); \
\r
85 ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
\r
88 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
\r
90 #endif /* sbRECEIVE_COMPLETED_FROM_ISR */
\r
92 /* If the user has not provided an application specific Tx notification macro,
\r
93 * or #defined the notification macro away, them provide a default implementation
\r
94 * that uses task notifications. */
\r
95 #ifndef sbSEND_COMPLETED
\r
96 #define sbSEND_COMPLETED( pxStreamBuffer ) \
\r
97 vTaskSuspendAll(); \
\r
99 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
\r
101 ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
\r
104 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
\r
107 ( void ) xTaskResumeAll();
\r
108 #endif /* sbSEND_COMPLETED */
\r
110 #ifndef sbSEND_COMPLETE_FROM_ISR
\r
111 #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
\r
113 UBaseType_t uxSavedInterruptStatus; \
\r
115 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
\r
117 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
\r
119 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
\r
122 pxHigherPriorityTaskWoken ); \
\r
123 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
\r
126 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
\r
128 #endif /* sbSEND_COMPLETE_FROM_ISR */
\r
129 /*lint -restore (9026) */
\r
131 /* The number of bytes used to hold the length of a message in the buffer. */
\r
132 #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
\r
134 /* Bits stored in the ucFlags field of the stream buffer. */
\r
135 #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
136 #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */
\r
138 /*-----------------------------------------------------------*/
\r
140 /* Structure that hold state information on the buffer. */
\r
141 typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */
\r
143 volatile size_t xTail; /* Index to the next item to read within the buffer. */
\r
144 volatile size_t xHead; /* Index to the next item to write within the buffer. */
\r
145 size_t xLength; /* The length of the buffer pointed to by pucBuffer. */
\r
146 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
147 volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */
\r
148 volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */
\r
149 uint8_t * pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */
\r
152 #if ( configUSE_TRACE_FACILITY == 1 )
\r
153 UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */
\r
158 * The number of bytes available to be read from the buffer.
\r
160 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
\r
163 * Add xCount bytes from pucData into the pxStreamBuffer message buffer.
\r
164 * Returns the number of bytes written, which will either equal xCount in the
\r
165 * success case, or 0 if there was not enough space in the buffer (in which case
\r
166 * no data is written into the buffer).
\r
168 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
169 const uint8_t * pucData,
\r
170 size_t xCount ) PRIVILEGED_FUNCTION;
\r
173 * If the stream buffer is being used as a message buffer, then reads an entire
\r
174 * message out of the buffer. If the stream buffer is being used as a stream
\r
175 * buffer then read as many bytes as possible from the buffer.
\r
176 * prvReadBytesFromBuffer() is called to actually extract the bytes from the
\r
177 * buffer's data storage area.
\r
179 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
181 size_t xBufferLengthBytes,
\r
182 size_t xBytesAvailable,
\r
183 size_t xBytesToStoreMessageLength ) PRIVILEGED_FUNCTION;
\r
186 * If the stream buffer is being used as a message buffer, then writes an entire
\r
187 * message to the buffer. If the stream buffer is being used as a stream
\r
188 * buffer then write as many bytes as possible to the buffer.
\r
189 * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's
\r
190 * data storage area.
\r
192 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
193 const void * pvTxData,
\r
194 size_t xDataLengthBytes,
\r
196 size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
\r
199 * Read xMaxCount bytes from the pxStreamBuffer message buffer and write them
\r
202 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
205 size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
\r
208 * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to
\r
209 * initialise the members of the newly created stream buffer structure.
\r
211 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
212 uint8_t * const pucBuffer,
\r
213 size_t xBufferSizeBytes,
\r
214 size_t xTriggerLevelBytes,
\r
215 uint8_t ucFlags ) PRIVILEGED_FUNCTION;
\r
217 /*-----------------------------------------------------------*/
\r
219 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
221 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
\r
222 size_t xTriggerLevelBytes,
\r
223 BaseType_t xIsMessageBuffer )
\r
225 uint8_t * pucAllocatedMemory;
\r
228 /* In case the stream buffer is going to be used as a message buffer
\r
229 * (that is, it will hold discrete messages with a little meta data that
\r
230 * says how big the next message is) check the buffer will be large enough
\r
231 * to hold at least one message. */
\r
232 if( xIsMessageBuffer == pdTRUE )
\r
234 /* Is a message buffer but not statically allocated. */
\r
235 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
\r
236 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
240 /* Not a message buffer and not statically allocated. */
\r
242 configASSERT( xBufferSizeBytes > 0 );
\r
245 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
\r
247 /* A trigger level of 0 would cause a waiting task to unblock even when
\r
248 * the buffer was empty. */
\r
249 if( xTriggerLevelBytes == ( size_t ) 0 )
\r
251 xTriggerLevelBytes = ( size_t ) 1;
\r
254 /* A stream buffer requires a StreamBuffer_t structure and a buffer.
\r
255 * Both are allocated in a single call to pvPortMalloc(). The
\r
256 * StreamBuffer_t structure is placed at the start of the allocated memory
\r
257 * and the buffer follows immediately after. The requested size is
\r
258 * incremented so the free space is returned as the user would expect -
\r
259 * this is a quirk of the implementation that means otherwise the free
\r
260 * space would be reported as one byte smaller than would be logically
\r
262 xBufferSizeBytes++;
\r
263 pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
\r
265 if( pucAllocatedMemory != NULL )
\r
267 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
268 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
270 xTriggerLevelBytes,
\r
273 traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer );
\r
277 traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
\r
280 return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
\r
283 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
284 /*-----------------------------------------------------------*/
\r
286 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
288 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
\r
289 size_t xTriggerLevelBytes,
\r
290 BaseType_t xIsMessageBuffer,
\r
291 uint8_t * const pucStreamBufferStorageArea,
\r
292 StaticStreamBuffer_t * const pxStaticStreamBuffer )
\r
294 StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
\r
295 StreamBufferHandle_t xReturn;
\r
298 configASSERT( pucStreamBufferStorageArea );
\r
299 configASSERT( pxStaticStreamBuffer );
\r
300 configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
\r
302 /* A trigger level of 0 would cause a waiting task to unblock even when
\r
303 * the buffer was empty. */
\r
304 if( xTriggerLevelBytes == ( size_t ) 0 )
\r
306 xTriggerLevelBytes = ( size_t ) 1;
\r
309 if( xIsMessageBuffer != pdFALSE )
\r
311 /* Statically allocated message buffer. */
\r
312 ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
316 /* Statically allocated stream buffer. */
\r
317 ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
320 /* In case the stream buffer is going to be used as a message buffer
\r
321 * (that is, it will hold discrete messages with a little meta data that
\r
322 * says how big the next message is) check the buffer will be large enough
\r
323 * to hold at least one message. */
\r
324 configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
326 #if ( configASSERT_DEFINED == 1 )
\r
328 /* Sanity check that the size of the structure used to declare a
\r
329 * variable of type StaticStreamBuffer_t equals the size of the real
\r
330 * message buffer structure. */
\r
331 volatile size_t xSize = sizeof( StaticStreamBuffer_t );
\r
332 configASSERT( xSize == sizeof( StreamBuffer_t ) );
\r
333 } /*lint !e529 xSize is referenced is configASSERT() is defined. */
\r
334 #endif /* configASSERT_DEFINED */
\r
336 if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
\r
338 prvInitialiseNewStreamBuffer( pxStreamBuffer,
\r
339 pucStreamBufferStorageArea,
\r
341 xTriggerLevelBytes,
\r
344 /* Remember this was statically allocated in case it is ever deleted
\r
346 pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
\r
348 traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
\r
350 xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
\r
355 traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
\r
361 #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
\r
362 /*-----------------------------------------------------------*/
\r
364 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
\r
366 StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
\r
368 configASSERT( pxStreamBuffer );
\r
370 traceSTREAM_BUFFER_DELETE( xStreamBuffer );
\r
372 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
\r
374 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
376 /* Both the structure and the buffer were allocated using a single call
\r
377 * to pvPortMalloc(), hence only one call to vPortFree() is required. */
\r
378 vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
\r
382 /* Should not be possible to get here, ucFlags must be corrupt.
\r
383 * Force an assert. */
\r
384 configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
\r
390 /* The structure and buffer were not allocated dynamically and cannot be
\r
391 * freed - just scrub the structure so future use will assert. */
\r
392 ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
\r
395 /*-----------------------------------------------------------*/
\r
397 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
\r
399 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
400 BaseType_t xReturn = pdFAIL;
\r
402 #if ( configUSE_TRACE_FACILITY == 1 )
\r
403 UBaseType_t uxStreamBufferNumber;
\r
406 configASSERT( pxStreamBuffer );
\r
408 #if ( configUSE_TRACE_FACILITY == 1 )
\r
410 /* Store the stream buffer number so it can be restored after the
\r
412 uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
\r
416 /* Can only reset a message buffer if there are no tasks blocked on it. */
\r
417 taskENTER_CRITICAL();
\r
419 if( pxStreamBuffer->xTaskWaitingToReceive == NULL )
\r
421 if( pxStreamBuffer->xTaskWaitingToSend == NULL )
\r
423 prvInitialiseNewStreamBuffer( pxStreamBuffer,
\r
424 pxStreamBuffer->pucBuffer,
\r
425 pxStreamBuffer->xLength,
\r
426 pxStreamBuffer->xTriggerLevelBytes,
\r
427 pxStreamBuffer->ucFlags );
\r
430 #if ( configUSE_TRACE_FACILITY == 1 )
\r
432 pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
\r
436 traceSTREAM_BUFFER_RESET( xStreamBuffer );
\r
440 taskEXIT_CRITICAL();
\r
444 /*-----------------------------------------------------------*/
\r
446 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
\r
447 size_t xTriggerLevel )
\r
449 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
450 BaseType_t xReturn;
\r
452 configASSERT( pxStreamBuffer );
\r
454 /* It is not valid for the trigger level to be 0. */
\r
455 if( xTriggerLevel == ( size_t ) 0 )
\r
457 xTriggerLevel = ( size_t ) 1;
\r
460 /* The trigger level is the number of bytes that must be in the stream
\r
461 * buffer before a task that is waiting for data is unblocked. */
\r
462 if( xTriggerLevel <= pxStreamBuffer->xLength )
\r
464 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
\r
474 /*-----------------------------------------------------------*/
\r
476 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
\r
478 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
481 configASSERT( pxStreamBuffer );
\r
483 xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
\r
484 xSpace -= pxStreamBuffer->xHead;
\r
485 xSpace -= ( size_t ) 1;
\r
487 if( xSpace >= pxStreamBuffer->xLength )
\r
489 xSpace -= pxStreamBuffer->xLength;
\r
493 mtCOVERAGE_TEST_MARKER();
\r
498 /*-----------------------------------------------------------*/
\r
500 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
\r
502 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
505 configASSERT( pxStreamBuffer );
\r
507 xReturn = prvBytesInBuffer( pxStreamBuffer );
\r
510 /*-----------------------------------------------------------*/
\r
512 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
513 const void * pvTxData,
\r
514 size_t xDataLengthBytes,
\r
515 TickType_t xTicksToWait )
\r
517 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
518 size_t xReturn, xSpace = 0;
\r
519 size_t xRequiredSpace = xDataLengthBytes;
\r
520 TimeOut_t xTimeOut;
\r
522 /* Having a 'isFeasible' variable allows to respect the convention that there is only a return statement at the end. Othewise, return
\r
523 * could be done as soon as we realise the send cannot happen. We will let the call to 'prvWriteMessageToBuffer' dealing with this scenario. */
\r
524 BaseType_t xIsFeasible;
\r
526 configASSERT( pvTxData );
\r
527 configASSERT( pxStreamBuffer );
\r
529 /* This send function is used to write to both message buffers and stream
\r
530 * buffers. If this is a message buffer then the space needed must be
\r
531 * increased by the amount of bytes needed to store the length of the
\r
533 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
535 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
538 configASSERT( xRequiredSpace > xDataLengthBytes );
\r
540 /* In the case of the message buffer, one has to be able to write the complete message as opposed to
\r
541 * a stream buffer for semantic reasons. Check if it is physically possible to write the message given
\r
542 * the length of the buffer. */
\r
543 if(xRequiredSpace > pxStreamBuffer->xLength)
\r
545 /* The message could never be written because it is greater than the buffer length.
\r
546 * By setting xIsFeasable to FALSE, we skip over the following do..while loop, thus avoiding
\r
547 * a deadlock. The call to 'prvWriteMessageToBuffer' toward the end of this function with
\r
548 * xRequiredSpace greater than xSpace will suffice in not writing anything to the internal buffer.
\r
549 * Now, the function will return 0 because the message could not be written. Should an error code be
\r
550 * returned instead ??? In my opinion, probably.. But the return type doesn't allow for negative
\r
551 * values to be returned. A confusion could exist to the caller. Returning 0 because a timeout occurred
\r
552 * and a subsequent send attempts could eventually succeed, and returning 0 because a write could never
\r
553 * happen because of the size are two scenarios to me :/ */
\r
554 xIsFeasible = FALSE;
\r
558 /* It is possible to write the message completely in the buffer. This is the intended route.
\r
559 * Let's continue with the regular timeout logic. */
\r
560 xIsFeasible = TRUE;
\r
565 /* In the case of the stream buffer, not being able to completely write the message in the buffer
\r
566 * is an acceptable scenario, but it has to be dealt with properly */
\r
567 if(xRequiredSpace > pxStreamBuffer->xLength)
\r
569 /* Not enough buffer space. We will attempt to write as much as we can in this run
\r
570 * so that the caller can send the remaining in subsequent calls. We avoid a deadlock by
\r
571 * offering the possibility to take the 'else' branch in the 'if( xSpace < xRequiredSpace )'
\r
572 * condition inside the following do..while loop */
\r
573 xRequiredSpace = pxStreamBuffer->xLength;
\r
575 /* TODO FIXME: Is there a check we should do with the xTriggerLevelBytes value ? */
\r
577 /* With the adjustment to 'xRequiredSpace', the deadlock is avoided, thus it's now feasible. */
\r
578 xIsFeasible = TRUE;
\r
582 /* It is possible to write the message completely in the buffer. */
\r
583 xIsFeasible = TRUE;
\r
587 /* Added check against xIsFeasible. If it's not feasible, don't even wait for notification, let the call to 'prvWriteMessageToBuffer' do nothing and return 0 */
\r
588 if( xTicksToWait != ( TickType_t ) 0 && xIsFeasible == TRUE )
\r
590 vTaskSetTimeOutState( &xTimeOut );
\r
594 /* Wait until the required number of bytes are free in the message
\r
596 taskENTER_CRITICAL();
\r
598 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
600 if( xSpace < xRequiredSpace )
\r
602 /* Clear notification state as going to wait for space. */
\r
603 ( void ) xTaskNotifyStateClear( NULL );
\r
605 /* Should only be one writer. */
\r
606 configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
\r
607 pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
\r
611 taskEXIT_CRITICAL();
\r
615 taskEXIT_CRITICAL();
\r
617 traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
\r
618 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
\r
619 pxStreamBuffer->xTaskWaitingToSend = NULL;
\r
620 } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
\r
624 mtCOVERAGE_TEST_MARKER();
\r
627 if( xSpace == ( size_t ) 0 )
\r
629 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
633 mtCOVERAGE_TEST_MARKER();
\r
636 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
\r
638 if( xReturn > ( size_t ) 0 )
\r
640 traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
\r
642 /* Was a task waiting for the data? */
\r
643 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
\r
645 sbSEND_COMPLETED( pxStreamBuffer );
\r
649 mtCOVERAGE_TEST_MARKER();
\r
654 mtCOVERAGE_TEST_MARKER();
\r
655 traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
\r
660 /*-----------------------------------------------------------*/
\r
662 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
663 const void * pvTxData,
\r
664 size_t xDataLengthBytes,
\r
665 BaseType_t * const pxHigherPriorityTaskWoken )
\r
667 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
668 size_t xReturn, xSpace;
\r
669 size_t xRequiredSpace = xDataLengthBytes;
\r
671 configASSERT( pvTxData );
\r
672 configASSERT( pxStreamBuffer );
\r
674 /* This send function is used to write to both message buffers and stream
\r
675 * buffers. If this is a message buffer then the space needed must be
\r
676 * increased by the amount of bytes needed to store the length of the
\r
678 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
680 xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
684 mtCOVERAGE_TEST_MARKER();
\r
687 xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
\r
688 xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
\r
690 if( xReturn > ( size_t ) 0 )
\r
692 /* Was a task waiting for the data? */
\r
693 if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
\r
695 sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
\r
699 mtCOVERAGE_TEST_MARKER();
\r
704 mtCOVERAGE_TEST_MARKER();
\r
707 traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
\r
711 /*-----------------------------------------------------------*/
\r
713 static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
714 const void * pvTxData,
\r
715 size_t xDataLengthBytes,
\r
717 size_t xRequiredSpace )
\r
719 BaseType_t xShouldWrite;
\r
722 if( xSpace == ( size_t ) 0 )
\r
724 /* Doesn't matter if this is a stream buffer or a message buffer, there
\r
725 * is no space to write. */
\r
726 xShouldWrite = pdFALSE;
\r
728 else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
\r
730 /* This is a stream buffer, as opposed to a message buffer, so writing a
\r
731 * stream of bytes rather than discrete messages. Write as many bytes as
\r
733 xShouldWrite = pdTRUE;
\r
734 xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
\r
736 else if( xSpace >= xRequiredSpace )
\r
738 /* This is a message buffer, as opposed to a stream buffer, and there
\r
739 * is enough space to write both the message length and the message itself
\r
740 * into the buffer. Start by writing the length of the data, the data
\r
741 * itself will be written later in this function. */
\r
742 xShouldWrite = pdTRUE;
\r
743 ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
\r
747 /* There is space available, but not enough space. */
\r
748 xShouldWrite = pdFALSE;
\r
751 if( xShouldWrite != pdFALSE )
\r
753 /* Writes the data itself. */
\r
754 xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alighment and access. */
\r
763 /*-----------------------------------------------------------*/
\r
765 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
767 size_t xBufferLengthBytes,
\r
768 TickType_t xTicksToWait )
\r
770 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
771 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
\r
773 configASSERT( pvRxData );
\r
774 configASSERT( pxStreamBuffer );
\r
776 /* This receive function is used by both message buffers, which store
\r
777 * discrete messages, and stream buffers, which store a continuous stream of
\r
778 * bytes. Discrete messages include an additional
\r
779 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
\r
781 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
783 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
787 xBytesToStoreMessageLength = 0;
\r
790 if( xTicksToWait != ( TickType_t ) 0 )
\r
792 /* Checking if there is data and clearing the notification state must be
\r
793 * performed atomically. */
\r
794 taskENTER_CRITICAL();
\r
796 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
798 /* If this function was invoked by a message buffer read then
\r
799 * xBytesToStoreMessageLength holds the number of bytes used to hold
\r
800 * the length of the next discrete message. If this function was
\r
801 * invoked by a stream buffer read then xBytesToStoreMessageLength will
\r
803 if( xBytesAvailable <= xBytesToStoreMessageLength )
\r
805 /* Clear notification state as going to wait for data. */
\r
806 ( void ) xTaskNotifyStateClear( NULL );
\r
808 /* Should only be one reader. */
\r
809 configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
\r
810 pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
\r
814 mtCOVERAGE_TEST_MARKER();
\r
817 taskEXIT_CRITICAL();
\r
819 if( xBytesAvailable <= xBytesToStoreMessageLength )
\r
821 /* Wait for data to be available. */
\r
822 traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
\r
823 ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
\r
824 pxStreamBuffer->xTaskWaitingToReceive = NULL;
\r
826 /* Recheck the data available after blocking. */
\r
827 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
831 mtCOVERAGE_TEST_MARKER();
\r
836 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
839 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
\r
840 * holds the number of bytes used to store the message length) or a stream of
\r
841 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
\r
842 * available must be greater than xBytesToStoreMessageLength to be able to
\r
843 * read bytes from the buffer. */
\r
844 if( xBytesAvailable > xBytesToStoreMessageLength )
\r
846 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
\r
848 /* Was a task waiting for space in the buffer? */
\r
849 if( xReceivedLength != ( size_t ) 0 )
\r
851 traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
\r
852 sbRECEIVE_COMPLETED( pxStreamBuffer );
\r
856 mtCOVERAGE_TEST_MARKER();
\r
861 traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
\r
862 mtCOVERAGE_TEST_MARKER();
\r
865 return xReceivedLength;
\r
867 /*-----------------------------------------------------------*/
\r
869 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
\r
871 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
872 size_t xReturn, xBytesAvailable, xOriginalTail;
\r
873 configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
\r
875 configASSERT( pxStreamBuffer );
\r
877 /* Ensure the stream buffer is being used as a message buffer. */
\r
878 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
880 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
882 if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
\r
884 /* The number of bytes available is greater than the number of bytes
\r
885 * required to hold the length of the next message, so another message
\r
886 * is available. Return its length without removing the length bytes
\r
887 * from the buffer. A copy of the tail is stored so the buffer can be
\r
888 * returned to its prior state as the message is not actually being
\r
889 * removed from the buffer. */
\r
890 xOriginalTail = pxStreamBuffer->xTail;
\r
891 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
\r
892 xReturn = ( size_t ) xTempReturn;
\r
893 pxStreamBuffer->xTail = xOriginalTail;
\r
897 /* The minimum amount of bytes in a message buffer is
\r
898 * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
\r
899 * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
\r
901 configASSERT( xBytesAvailable == 0 );
\r
912 /*-----------------------------------------------------------*/
\r
914 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
916 size_t xBufferLengthBytes,
\r
917 BaseType_t * const pxHigherPriorityTaskWoken )
\r
919 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
920 size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
\r
922 configASSERT( pvRxData );
\r
923 configASSERT( pxStreamBuffer );
\r
925 /* This receive function is used by both message buffers, which store
\r
926 * discrete messages, and stream buffers, which store a continuous stream of
\r
927 * bytes. Discrete messages include an additional
\r
928 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
\r
930 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
932 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
936 xBytesToStoreMessageLength = 0;
\r
939 xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
\r
941 /* Whether receiving a discrete message (where xBytesToStoreMessageLength
\r
942 * holds the number of bytes used to store the message length) or a stream of
\r
943 * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
\r
944 * available must be greater than xBytesToStoreMessageLength to be able to
\r
945 * read bytes from the buffer. */
\r
946 if( xBytesAvailable > xBytesToStoreMessageLength )
\r
948 xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
\r
950 /* Was a task waiting for space in the buffer? */
\r
951 if( xReceivedLength != ( size_t ) 0 )
\r
953 sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
\r
957 mtCOVERAGE_TEST_MARKER();
\r
962 mtCOVERAGE_TEST_MARKER();
\r
965 traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
\r
967 return xReceivedLength;
\r
969 /*-----------------------------------------------------------*/
\r
971 static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
973 size_t xBufferLengthBytes,
\r
974 size_t xBytesAvailable,
\r
975 size_t xBytesToStoreMessageLength )
\r
977 size_t xOriginalTail, xReceivedLength, xNextMessageLength;
\r
978 configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
\r
980 if( xBytesToStoreMessageLength != ( size_t ) 0 )
\r
982 /* A discrete message is being received. First receive the length
\r
983 * of the message. A copy of the tail is stored so the buffer can be
\r
984 * returned to its prior state if the length of the message is too
\r
985 * large for the provided buffer. */
\r
986 xOriginalTail = pxStreamBuffer->xTail;
\r
987 ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
\r
988 xNextMessageLength = ( size_t ) xTempNextMessageLength;
\r
990 /* Reduce the number of bytes available by the number of bytes just
\r
992 xBytesAvailable -= xBytesToStoreMessageLength;
\r
994 /* Check there is enough space in the buffer provided by the
\r
996 if( xNextMessageLength > xBufferLengthBytes )
\r
998 /* The user has provided insufficient space to read the message
\r
999 * so return the buffer to its previous state (so the length of
\r
1000 * the message is in the buffer again). */
\r
1001 pxStreamBuffer->xTail = xOriginalTail;
\r
1002 xNextMessageLength = 0;
\r
1006 mtCOVERAGE_TEST_MARKER();
\r
1011 /* A stream of bytes is being received (as opposed to a discrete
\r
1012 * message), so read as many bytes as possible. */
\r
1013 xNextMessageLength = xBufferLengthBytes;
\r
1016 /* Read the actual data. */
\r
1017 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
1019 return xReceivedLength;
\r
1021 /*-----------------------------------------------------------*/
\r
1023 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
\r
1025 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1026 BaseType_t xReturn;
\r
1029 configASSERT( pxStreamBuffer );
\r
1031 /* True if no bytes are available. */
\r
1032 xTail = pxStreamBuffer->xTail;
\r
1034 if( pxStreamBuffer->xHead == xTail )
\r
1040 xReturn = pdFALSE;
\r
1045 /*-----------------------------------------------------------*/
\r
1047 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
\r
1049 BaseType_t xReturn;
\r
1050 size_t xBytesToStoreMessageLength;
\r
1051 const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1053 configASSERT( pxStreamBuffer );
\r
1055 /* This generic version of the receive function is used by both message
\r
1056 * buffers, which store discrete messages, and stream buffers, which store a
\r
1057 * continuous stream of bytes. Discrete messages include an additional
\r
1058 * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
\r
1059 if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
\r
1061 xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
\r
1065 xBytesToStoreMessageLength = 0;
\r
1068 /* True if the available space equals zero. */
\r
1069 if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
\r
1075 xReturn = pdFALSE;
\r
1080 /*-----------------------------------------------------------*/
\r
1082 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
1083 BaseType_t * pxHigherPriorityTaskWoken )
\r
1085 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1086 BaseType_t xReturn;
\r
1087 UBaseType_t uxSavedInterruptStatus;
\r
1089 configASSERT( pxStreamBuffer );
\r
1091 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
1093 if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
\r
1095 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
\r
1098 pxHigherPriorityTaskWoken );
\r
1099 ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
\r
1104 xReturn = pdFALSE;
\r
1107 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1111 /*-----------------------------------------------------------*/
\r
1113 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
1114 BaseType_t * pxHigherPriorityTaskWoken )
\r
1116 StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
\r
1117 BaseType_t xReturn;
\r
1118 UBaseType_t uxSavedInterruptStatus;
\r
1120 configASSERT( pxStreamBuffer );
\r
1122 uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
\r
1124 if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
\r
1126 ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
\r
1129 pxHigherPriorityTaskWoken );
\r
1130 ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
\r
1135 xReturn = pdFALSE;
\r
1138 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1142 /*-----------------------------------------------------------*/
\r
1144 static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
1145 const uint8_t * pucData,
\r
1148 size_t xNextHead, xFirstLength;
\r
1150 configASSERT( xCount > ( size_t ) 0 );
\r
1152 xNextHead = pxStreamBuffer->xHead;
\r
1154 /* Calculate the number of bytes that can be added in the first write -
\r
1155 * which may be less than the total number of bytes that need to be added if
\r
1156 * the buffer will wrap back to the beginning. */
\r
1157 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
\r
1159 /* Write as many bytes as can be written in the first write. */
\r
1160 configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
\r
1161 ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1163 /* If the number of bytes written was less than the number that could be
\r
1164 * written in the first write... */
\r
1165 if( xCount > xFirstLength )
\r
1167 /* ...then write the remaining bytes to the start of the buffer. */
\r
1168 configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
\r
1169 ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1173 mtCOVERAGE_TEST_MARKER();
\r
1176 xNextHead += xCount;
\r
1178 if( xNextHead >= pxStreamBuffer->xLength )
\r
1180 xNextHead -= pxStreamBuffer->xLength;
\r
1184 mtCOVERAGE_TEST_MARKER();
\r
1187 pxStreamBuffer->xHead = xNextHead;
\r
1191 /*-----------------------------------------------------------*/
\r
1193 static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
\r
1194 uint8_t * pucData,
\r
1196 size_t xBytesAvailable )
\r
1198 size_t xCount, xFirstLength, xNextTail;
\r
1200 /* Use the minimum of the wanted bytes and the available bytes. */
\r
1201 xCount = configMIN( xBytesAvailable, xMaxCount );
\r
1203 if( xCount > ( size_t ) 0 )
\r
1205 xNextTail = pxStreamBuffer->xTail;
\r
1207 /* Calculate the number of bytes that can be read - which may be
\r
1208 * less than the number wanted if the data wraps around to the start of
\r
1210 xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
\r
1212 /* Obtain the number of bytes it is possible to obtain in the first
\r
1213 * read. Asserts check bounds of read and write. */
\r
1214 configASSERT( xFirstLength <= xMaxCount );
\r
1215 configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
\r
1216 ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1218 /* If the total number of wanted bytes is greater than the number
\r
1219 * that could be read in the first read... */
\r
1220 if( xCount > xFirstLength )
\r
1222 /*...then read the remaining bytes from the start of the buffer. */
\r
1223 configASSERT( xCount <= xMaxCount );
\r
1224 ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
\r
1228 mtCOVERAGE_TEST_MARKER();
\r
1231 /* Move the tail pointer to effectively remove the data read from
\r
1233 xNextTail += xCount;
\r
1235 if( xNextTail >= pxStreamBuffer->xLength )
\r
1237 xNextTail -= pxStreamBuffer->xLength;
\r
1240 pxStreamBuffer->xTail = xNextTail;
\r
1244 mtCOVERAGE_TEST_MARKER();
\r
1249 /*-----------------------------------------------------------*/
\r
1251 static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
\r
1253 /* Returns the distance between xTail and xHead. */
\r
1256 xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
\r
1257 xCount -= pxStreamBuffer->xTail;
\r
1259 if( xCount >= pxStreamBuffer->xLength )
\r
1261 xCount -= pxStreamBuffer->xLength;
\r
1265 mtCOVERAGE_TEST_MARKER();
\r
1270 /*-----------------------------------------------------------*/
\r
1272 static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
\r
1273 uint8_t * const pucBuffer,
\r
1274 size_t xBufferSizeBytes,
\r
1275 size_t xTriggerLevelBytes,
\r
1278 /* Assert here is deliberately writing to the entire buffer to ensure it can
\r
1279 * be written to without generating exceptions, and is setting the buffer to a
\r
1280 * known value to assist in development/debugging. */
\r
1281 #if ( configASSERT_DEFINED == 1 )
\r
1283 /* The value written just has to be identifiable when looking at the
\r
1284 * memory. Don't use 0xA5 as that is the stack fill value and could
\r
1285 * result in confusion as to what is actually being observed. */
\r
1286 const BaseType_t xWriteValue = 0x55;
\r
1287 configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
\r
1288 } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */
\r
1291 ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
\r
1292 pxStreamBuffer->pucBuffer = pucBuffer;
\r
1293 pxStreamBuffer->xLength = xBufferSizeBytes;
\r
1294 pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
\r
1295 pxStreamBuffer->ucFlags = ucFlags;
\r
1298 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1300 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
\r
1302 return xStreamBuffer->uxStreamBufferNumber;
\r
1305 #endif /* configUSE_TRACE_FACILITY */
\r
1306 /*-----------------------------------------------------------*/
\r
1308 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1310 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
\r
1311 UBaseType_t uxStreamBufferNumber )
\r
1313 xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
\r
1316 #endif /* configUSE_TRACE_FACILITY */
\r
1317 /*-----------------------------------------------------------*/
\r
1319 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1321 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
\r
1323 return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
\r
1326 #endif /* configUSE_TRACE_FACILITY */
\r
1327 /*-----------------------------------------------------------*/
\r