kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1 | /* |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 2 | * FreeRTOS Kernel V10.2.1 |
| 3 | * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | * this software and associated documentation files (the "Software"), to deal in |
| 7 | * the Software without restriction, including without limitation the rights to |
| 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 9 | * the Software, and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be included in all |
| 13 | * copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 22 | * http://www.FreeRTOS.org |
| 23 | * http://aws.amazon.com/freertos |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 24 | * |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 25 | * 1 tab == 4 spaces! |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | /* Standard includes. */ |
| 29 | #include <stdint.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 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. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 35 | #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE |
| 36 | |
| 37 | /* FreeRTOS includes. */ |
| 38 | #include "FreeRTOS.h" |
| 39 | #include "task.h" |
| 40 | #include "stream_buffer.h" |
| 41 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 42 | #if( configUSE_TASK_NOTIFICATIONS != 1 ) |
| 43 | #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | /* Lint e961, e9021 and e750 are suppressed as a MISRA exception justified |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 47 | because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined |
| 48 | for the header files above, but not in this file, in order to generate the |
| 49 | correct privileged Vs unprivileged linkage and placement. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 50 | #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */ |
| 51 | |
| 52 | /* If the user has not provided application specific Rx notification macros, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 53 | or #defined the notification macros away, them provide default implementations |
| 54 | that uses task notifications. */ |
| 55 | /*lint -save -e9026 Function like macros allowed and needed here so they can be overidden. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 56 | #ifndef sbRECEIVE_COMPLETED |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 57 | #define sbRECEIVE_COMPLETED( pxStreamBuffer ) \ |
| 58 | vTaskSuspendAll(); \ |
| 59 | { \ |
| 60 | if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \ |
| 61 | { \ |
| 62 | ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \ |
| 63 | ( uint32_t ) 0, \ |
| 64 | eNoAction ); \ |
| 65 | ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \ |
| 66 | } \ |
| 67 | } \ |
| 68 | ( void ) xTaskResumeAll(); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 69 | #endif /* sbRECEIVE_COMPLETED */ |
| 70 | |
| 71 | #ifndef sbRECEIVE_COMPLETED_FROM_ISR |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 72 | #define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \ |
| 73 | pxHigherPriorityTaskWoken ) \ |
| 74 | { \ |
| 75 | UBaseType_t uxSavedInterruptStatus; \ |
| 76 | \ |
| 77 | uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \ |
| 78 | { \ |
| 79 | if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \ |
| 80 | { \ |
| 81 | ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \ |
| 82 | ( uint32_t ) 0, \ |
| 83 | eNoAction, \ |
| 84 | pxHigherPriorityTaskWoken ); \ |
| 85 | ( pxStreamBuffer )->xTaskWaitingToSend = NULL; \ |
| 86 | } \ |
| 87 | } \ |
| 88 | portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \ |
| 89 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 90 | #endif /* sbRECEIVE_COMPLETED_FROM_ISR */ |
| 91 | |
| 92 | /* If the user has not provided an application specific Tx notification macro, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 93 | or #defined the notification macro away, them provide a default implementation |
| 94 | that uses task notifications. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 95 | #ifndef sbSEND_COMPLETED |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 96 | #define sbSEND_COMPLETED( pxStreamBuffer ) \ |
| 97 | vTaskSuspendAll(); \ |
| 98 | { \ |
| 99 | if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \ |
| 100 | { \ |
| 101 | ( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \ |
| 102 | ( uint32_t ) 0, \ |
| 103 | eNoAction ); \ |
| 104 | ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \ |
| 105 | } \ |
| 106 | } \ |
| 107 | ( void ) xTaskResumeAll(); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 108 | #endif /* sbSEND_COMPLETED */ |
| 109 | |
| 110 | #ifndef sbSEND_COMPLETE_FROM_ISR |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 111 | #define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \ |
| 112 | { \ |
| 113 | UBaseType_t uxSavedInterruptStatus; \ |
| 114 | \ |
| 115 | uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \ |
| 116 | { \ |
| 117 | if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \ |
| 118 | { \ |
| 119 | ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \ |
| 120 | ( uint32_t ) 0, \ |
| 121 | eNoAction, \ |
| 122 | pxHigherPriorityTaskWoken ); \ |
| 123 | ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \ |
| 124 | } \ |
| 125 | } \ |
| 126 | portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \ |
| 127 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 128 | #endif /* sbSEND_COMPLETE_FROM_ISR */ |
| 129 | /*lint -restore (9026) */ |
| 130 | |
| 131 | /* The number of bytes used to hold the length of a message in the buffer. */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 132 | #define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 133 | |
| 134 | /* Bits stored in the ucFlags field of the stream buffer. */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 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. */ |
| 136 | #define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 ) /* Set if the stream buffer was created using statically allocated memory. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 137 | |
| 138 | /*-----------------------------------------------------------*/ |
| 139 | |
| 140 | /* Structure that hold state information on the buffer. */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 141 | typedef struct StreamBufferDef_t /*lint !e9058 Style convention uses tag. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 142 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 143 | volatile size_t xTail; /* Index to the next item to read within the buffer. */ |
| 144 | volatile size_t xHead; /* Index to the next item to write within the buffer. */ |
| 145 | size_t xLength; /* The length of the buffer pointed to by pucBuffer. */ |
| 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. */ |
| 147 | volatile TaskHandle_t xTaskWaitingToReceive; /* Holds the handle of a task waiting for data, or NULL if no tasks are waiting. */ |
| 148 | volatile TaskHandle_t xTaskWaitingToSend; /* Holds the handle of a task waiting to send data to a message buffer that is full. */ |
| 149 | uint8_t *pucBuffer; /* Points to the buffer itself - that is - the RAM that stores the data passed through the buffer. */ |
| 150 | uint8_t ucFlags; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 151 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 152 | #if ( configUSE_TRACE_FACILITY == 1 ) |
| 153 | UBaseType_t uxStreamBufferNumber; /* Used for tracing purposes. */ |
| 154 | #endif |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 155 | } StreamBuffer_t; |
| 156 | |
| 157 | /* |
| 158 | * The number of bytes available to be read from the buffer. |
| 159 | */ |
| 160 | static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION; |
| 161 | |
| 162 | /* |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 163 | * Add xCount bytes from pucData into the pxStreamBuffer message buffer. |
| 164 | * Returns the number of bytes written, which will either equal xCount in the |
| 165 | * success case, or 0 if there was not enough space in the buffer (in which case |
| 166 | * no data is written into the buffer). |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 167 | */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 168 | static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer, const uint8_t *pucData, size_t xCount ) PRIVILEGED_FUNCTION; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * If the stream buffer is being used as a message buffer, then reads an entire |
| 172 | * message out of the buffer. If the stream buffer is being used as a stream |
| 173 | * buffer then read as many bytes as possible from the buffer. |
| 174 | * prvReadBytesFromBuffer() is called to actually extract the bytes from the |
| 175 | * buffer's data storage area. |
| 176 | */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 177 | static size_t prvReadMessageFromBuffer( StreamBuffer_t *pxStreamBuffer, |
| 178 | void *pvRxData, |
| 179 | size_t xBufferLengthBytes, |
| 180 | size_t xBytesAvailable, |
| 181 | size_t xBytesToStoreMessageLength ) PRIVILEGED_FUNCTION; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * If the stream buffer is being used as a message buffer, then writes an entire |
| 185 | * message to the buffer. If the stream buffer is being used as a stream |
| 186 | * buffer then write as many bytes as possible to the buffer. |
| 187 | * prvWriteBytestoBuffer() is called to actually send the bytes to the buffer's |
| 188 | * data storage area. |
| 189 | */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 190 | static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer, |
| 191 | const void * pvTxData, |
| 192 | size_t xDataLengthBytes, |
| 193 | size_t xSpace, |
| 194 | size_t xRequiredSpace ) PRIVILEGED_FUNCTION; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 195 | |
| 196 | /* |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 197 | * Read xMaxCount bytes from the pxStreamBuffer message buffer and write them |
| 198 | * to pucData. |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 199 | */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 200 | static size_t prvReadBytesFromBuffer( StreamBuffer_t *pxStreamBuffer, |
| 201 | uint8_t *pucData, |
| 202 | size_t xMaxCount, |
| 203 | size_t xBytesAvailable ) PRIVILEGED_FUNCTION; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Called by both pxStreamBufferCreate() and pxStreamBufferCreateStatic() to |
| 207 | * initialise the members of the newly created stream buffer structure. |
| 208 | */ |
| 209 | static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 210 | uint8_t * const pucBuffer, |
| 211 | size_t xBufferSizeBytes, |
| 212 | size_t xTriggerLevelBytes, |
| 213 | uint8_t ucFlags ) PRIVILEGED_FUNCTION; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 214 | |
| 215 | /*-----------------------------------------------------------*/ |
| 216 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 217 | #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 218 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 219 | StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer ) |
| 220 | { |
| 221 | uint8_t *pucAllocatedMemory; |
| 222 | uint8_t ucFlags; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 223 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 224 | /* In case the stream buffer is going to be used as a message buffer |
| 225 | (that is, it will hold discrete messages with a little meta data that |
| 226 | says how big the next message is) check the buffer will be large enough |
| 227 | to hold at least one message. */ |
| 228 | if( xIsMessageBuffer == pdTRUE ) |
| 229 | { |
| 230 | /* Is a message buffer but not statically allocated. */ |
| 231 | ucFlags = sbFLAGS_IS_MESSAGE_BUFFER; |
| 232 | configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH ); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | /* Not a message buffer and not statically allocated. */ |
| 237 | ucFlags = 0; |
| 238 | configASSERT( xBufferSizeBytes > 0 ); |
| 239 | } |
| 240 | configASSERT( xTriggerLevelBytes <= xBufferSizeBytes ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 241 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 242 | /* A trigger level of 0 would cause a waiting task to unblock even when |
| 243 | the buffer was empty. */ |
| 244 | if( xTriggerLevelBytes == ( size_t ) 0 ) |
| 245 | { |
| 246 | xTriggerLevelBytes = ( size_t ) 1; |
| 247 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 248 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 249 | /* A stream buffer requires a StreamBuffer_t structure and a buffer. |
| 250 | Both are allocated in a single call to pvPortMalloc(). The |
| 251 | StreamBuffer_t structure is placed at the start of the allocated memory |
| 252 | and the buffer follows immediately after. The requested size is |
| 253 | incremented so the free space is returned as the user would expect - |
| 254 | this is a quirk of the implementation that means otherwise the free |
| 255 | space would be reported as one byte smaller than would be logically |
| 256 | expected. */ |
| 257 | xBufferSizeBytes++; |
| 258 | pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 259 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 260 | if( pucAllocatedMemory != NULL ) |
| 261 | { |
| 262 | 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. */ |
| 263 | pucAllocatedMemory + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */ |
| 264 | xBufferSizeBytes, |
| 265 | xTriggerLevelBytes, |
| 266 | ucFlags ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 267 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 268 | traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer ); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer ); |
| 273 | } |
| 274 | |
| 275 | return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */ |
| 276 | } |
| 277 | |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 278 | #endif /* configSUPPORT_DYNAMIC_ALLOCATION */ |
| 279 | /*-----------------------------------------------------------*/ |
| 280 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 281 | #if( configSUPPORT_STATIC_ALLOCATION == 1 ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 282 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 283 | StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes, |
| 284 | size_t xTriggerLevelBytes, |
| 285 | BaseType_t xIsMessageBuffer, |
| 286 | uint8_t * const pucStreamBufferStorageArea, |
| 287 | StaticStreamBuffer_t * const pxStaticStreamBuffer ) |
| 288 | { |
| 289 | StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */ |
| 290 | StreamBufferHandle_t xReturn; |
| 291 | uint8_t ucFlags; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 292 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 293 | configASSERT( pucStreamBufferStorageArea ); |
| 294 | configASSERT( pxStaticStreamBuffer ); |
| 295 | configASSERT( xTriggerLevelBytes <= xBufferSizeBytes ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 296 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 297 | /* A trigger level of 0 would cause a waiting task to unblock even when |
| 298 | the buffer was empty. */ |
| 299 | if( xTriggerLevelBytes == ( size_t ) 0 ) |
| 300 | { |
| 301 | xTriggerLevelBytes = ( size_t ) 1; |
| 302 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 303 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 304 | if( xIsMessageBuffer != pdFALSE ) |
| 305 | { |
| 306 | /* Statically allocated message buffer. */ |
| 307 | ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | /* Statically allocated stream buffer. */ |
| 312 | ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED; |
| 313 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 314 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 315 | /* In case the stream buffer is going to be used as a message buffer |
| 316 | (that is, it will hold discrete messages with a little meta data that |
| 317 | says how big the next message is) check the buffer will be large enough |
| 318 | to hold at least one message. */ |
| 319 | configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 320 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 321 | #if( configASSERT_DEFINED == 1 ) |
| 322 | { |
| 323 | /* Sanity check that the size of the structure used to declare a |
| 324 | variable of type StaticStreamBuffer_t equals the size of the real |
| 325 | message buffer structure. */ |
| 326 | volatile size_t xSize = sizeof( StaticStreamBuffer_t ); |
| 327 | configASSERT( xSize == sizeof( StreamBuffer_t ) ); |
| 328 | } /*lint !e529 xSize is referenced is configASSERT() is defined. */ |
| 329 | #endif /* configASSERT_DEFINED */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 330 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 331 | if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) ) |
| 332 | { |
| 333 | prvInitialiseNewStreamBuffer( pxStreamBuffer, |
| 334 | pucStreamBufferStorageArea, |
| 335 | xBufferSizeBytes, |
| 336 | xTriggerLevelBytes, |
| 337 | ucFlags ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 338 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 339 | /* Remember this was statically allocated in case it is ever deleted |
| 340 | again. */ |
| 341 | pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 342 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 343 | traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 344 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 345 | xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */ |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | xReturn = NULL; |
| 350 | traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer ); |
| 351 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 352 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 353 | return xReturn; |
| 354 | } |
| 355 | |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 356 | #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */ |
| 357 | /*-----------------------------------------------------------*/ |
| 358 | |
| 359 | void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) |
| 360 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 361 | StreamBuffer_t * pxStreamBuffer = xStreamBuffer; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 362 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 363 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 364 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 365 | traceSTREAM_BUFFER_DELETE( xStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 366 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 367 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE ) |
| 368 | { |
| 369 | #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) |
| 370 | { |
| 371 | /* Both the structure and the buffer were allocated using a single call |
| 372 | to pvPortMalloc(), hence only one call to vPortFree() is required. */ |
| 373 | vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */ |
| 374 | } |
| 375 | #else |
| 376 | { |
| 377 | /* Should not be possible to get here, ucFlags must be corrupt. |
| 378 | Force an assert. */ |
| 379 | configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 ); |
| 380 | } |
| 381 | #endif |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | /* The structure and buffer were not allocated dynamically and cannot be |
| 386 | freed - just scrub the structure so future use will assert. */ |
| 387 | ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); |
| 388 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 389 | } |
| 390 | /*-----------------------------------------------------------*/ |
| 391 | |
| 392 | BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ) |
| 393 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 394 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 395 | BaseType_t xReturn = pdFAIL; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 396 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 397 | #if( configUSE_TRACE_FACILITY == 1 ) |
| 398 | UBaseType_t uxStreamBufferNumber; |
| 399 | #endif |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 400 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 401 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 402 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 403 | #if( configUSE_TRACE_FACILITY == 1 ) |
| 404 | { |
| 405 | /* Store the stream buffer number so it can be restored after the |
| 406 | reset. */ |
| 407 | uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber; |
| 408 | } |
| 409 | #endif |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 410 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 411 | /* Can only reset a message buffer if there are no tasks blocked on it. */ |
| 412 | taskENTER_CRITICAL(); |
| 413 | { |
| 414 | if( pxStreamBuffer->xTaskWaitingToReceive == NULL ) |
| 415 | { |
| 416 | if( pxStreamBuffer->xTaskWaitingToSend == NULL ) |
| 417 | { |
| 418 | prvInitialiseNewStreamBuffer( pxStreamBuffer, |
| 419 | pxStreamBuffer->pucBuffer, |
| 420 | pxStreamBuffer->xLength, |
| 421 | pxStreamBuffer->xTriggerLevelBytes, |
| 422 | pxStreamBuffer->ucFlags ); |
| 423 | xReturn = pdPASS; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 424 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 425 | #if( configUSE_TRACE_FACILITY == 1 ) |
| 426 | { |
| 427 | pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber; |
| 428 | } |
| 429 | #endif |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 430 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 431 | traceSTREAM_BUFFER_RESET( xStreamBuffer ); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | taskEXIT_CRITICAL(); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 436 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 437 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 438 | } |
| 439 | /*-----------------------------------------------------------*/ |
| 440 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 441 | BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 442 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 443 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 444 | BaseType_t xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 445 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 446 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 447 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 448 | /* It is not valid for the trigger level to be 0. */ |
| 449 | if( xTriggerLevel == ( size_t ) 0 ) |
| 450 | { |
| 451 | xTriggerLevel = ( size_t ) 1; |
| 452 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 453 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 454 | /* The trigger level is the number of bytes that must be in the stream |
| 455 | buffer before a task that is waiting for data is unblocked. */ |
| 456 | if( xTriggerLevel <= pxStreamBuffer->xLength ) |
| 457 | { |
| 458 | pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel; |
| 459 | xReturn = pdPASS; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | xReturn = pdFALSE; |
| 464 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 465 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 466 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 467 | } |
| 468 | /*-----------------------------------------------------------*/ |
| 469 | |
| 470 | size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) |
| 471 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 472 | const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 473 | size_t xSpace; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 474 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 475 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 476 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 477 | xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail; |
| 478 | xSpace -= pxStreamBuffer->xHead; |
| 479 | xSpace -= ( size_t ) 1; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 480 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 481 | if( xSpace >= pxStreamBuffer->xLength ) |
| 482 | { |
| 483 | xSpace -= pxStreamBuffer->xLength; |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | mtCOVERAGE_TEST_MARKER(); |
| 488 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 489 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 490 | return xSpace; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 491 | } |
| 492 | /*-----------------------------------------------------------*/ |
| 493 | |
| 494 | size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ) |
| 495 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 496 | const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 497 | size_t xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 498 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 499 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 500 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 501 | xReturn = prvBytesInBuffer( pxStreamBuffer ); |
| 502 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 503 | } |
| 504 | /*-----------------------------------------------------------*/ |
| 505 | |
| 506 | size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 507 | const void *pvTxData, |
| 508 | size_t xDataLengthBytes, |
| 509 | TickType_t xTicksToWait ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 510 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 511 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 512 | size_t xReturn, xSpace = 0; |
| 513 | size_t xRequiredSpace = xDataLengthBytes; |
| 514 | TimeOut_t xTimeOut; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 515 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 516 | configASSERT( pvTxData ); |
| 517 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 518 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 519 | /* This send function is used to write to both message buffers and stream |
| 520 | buffers. If this is a message buffer then the space needed must be |
| 521 | increased by the amount of bytes needed to store the length of the |
| 522 | message. */ |
| 523 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 524 | { |
| 525 | xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 526 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 527 | /* Overflow? */ |
| 528 | configASSERT( xRequiredSpace > xDataLengthBytes ); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | mtCOVERAGE_TEST_MARKER(); |
| 533 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 534 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 535 | if( xTicksToWait != ( TickType_t ) 0 ) |
| 536 | { |
| 537 | vTaskSetTimeOutState( &xTimeOut ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 538 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 539 | do |
| 540 | { |
| 541 | /* Wait until the required number of bytes are free in the message |
| 542 | buffer. */ |
| 543 | taskENTER_CRITICAL(); |
| 544 | { |
| 545 | xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 546 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 547 | if( xSpace < xRequiredSpace ) |
| 548 | { |
| 549 | /* Clear notification state as going to wait for space. */ |
| 550 | ( void ) xTaskNotifyStateClear( NULL ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 551 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 552 | /* Should only be one writer. */ |
| 553 | configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL ); |
| 554 | pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle(); |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | taskEXIT_CRITICAL(); |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | taskEXIT_CRITICAL(); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 563 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 564 | traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer ); |
| 565 | ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait ); |
| 566 | pxStreamBuffer->xTaskWaitingToSend = NULL; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 567 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 568 | } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE ); |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | mtCOVERAGE_TEST_MARKER(); |
| 573 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 574 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 575 | if( xSpace == ( size_t ) 0 ) |
| 576 | { |
| 577 | xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer ); |
| 578 | } |
| 579 | else |
| 580 | { |
| 581 | mtCOVERAGE_TEST_MARKER(); |
| 582 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 583 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 584 | xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 585 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 586 | if( xReturn > ( size_t ) 0 ) |
| 587 | { |
| 588 | traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 589 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 590 | /* Was a task waiting for the data? */ |
| 591 | if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes ) |
| 592 | { |
| 593 | sbSEND_COMPLETED( pxStreamBuffer ); |
| 594 | } |
| 595 | else |
| 596 | { |
| 597 | mtCOVERAGE_TEST_MARKER(); |
| 598 | } |
| 599 | } |
| 600 | else |
| 601 | { |
| 602 | mtCOVERAGE_TEST_MARKER(); |
| 603 | traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer ); |
| 604 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 605 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 606 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 607 | } |
| 608 | /*-----------------------------------------------------------*/ |
| 609 | |
| 610 | size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 611 | const void *pvTxData, |
| 612 | size_t xDataLengthBytes, |
| 613 | BaseType_t * const pxHigherPriorityTaskWoken ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 614 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 615 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 616 | size_t xReturn, xSpace; |
| 617 | size_t xRequiredSpace = xDataLengthBytes; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 618 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 619 | configASSERT( pvTxData ); |
| 620 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 621 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 622 | /* This send function is used to write to both message buffers and stream |
| 623 | buffers. If this is a message buffer then the space needed must be |
| 624 | increased by the amount of bytes needed to store the length of the |
| 625 | message. */ |
| 626 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 627 | { |
| 628 | xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH; |
| 629 | } |
| 630 | else |
| 631 | { |
| 632 | mtCOVERAGE_TEST_MARKER(); |
| 633 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 634 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 635 | xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer ); |
| 636 | xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 637 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 638 | if( xReturn > ( size_t ) 0 ) |
| 639 | { |
| 640 | /* Was a task waiting for the data? */ |
| 641 | if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes ) |
| 642 | { |
| 643 | sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ); |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | mtCOVERAGE_TEST_MARKER(); |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | mtCOVERAGE_TEST_MARKER(); |
| 653 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 654 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 655 | traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 656 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 657 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 658 | } |
| 659 | /*-----------------------------------------------------------*/ |
| 660 | |
| 661 | static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 662 | const void * pvTxData, |
| 663 | size_t xDataLengthBytes, |
| 664 | size_t xSpace, |
| 665 | size_t xRequiredSpace ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 666 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 667 | BaseType_t xShouldWrite; |
| 668 | size_t xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 669 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 670 | if( xSpace == ( size_t ) 0 ) |
| 671 | { |
| 672 | /* Doesn't matter if this is a stream buffer or a message buffer, there |
| 673 | is no space to write. */ |
| 674 | xShouldWrite = pdFALSE; |
| 675 | } |
| 676 | else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 ) |
| 677 | { |
| 678 | /* This is a stream buffer, as opposed to a message buffer, so writing a |
| 679 | stream of bytes rather than discrete messages. Write as many bytes as |
| 680 | possible. */ |
| 681 | xShouldWrite = pdTRUE; |
| 682 | xDataLengthBytes = configMIN( xDataLengthBytes, xSpace ); |
| 683 | } |
| 684 | else if( xSpace >= xRequiredSpace ) |
| 685 | { |
| 686 | /* This is a message buffer, as opposed to a stream buffer, and there |
| 687 | is enough space to write both the message length and the message itself |
| 688 | into the buffer. Start by writing the length of the data, the data |
| 689 | itself will be written later in this function. */ |
| 690 | xShouldWrite = pdTRUE; |
| 691 | ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH ); |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | /* There is space available, but not enough space. */ |
| 696 | xShouldWrite = pdFALSE; |
| 697 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 698 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 699 | if( xShouldWrite != pdFALSE ) |
| 700 | { |
| 701 | /* Writes the data itself. */ |
| 702 | xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alighment and access. */ |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | xReturn = 0; |
| 707 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 708 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 709 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 710 | } |
| 711 | /*-----------------------------------------------------------*/ |
| 712 | |
| 713 | size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 714 | void *pvRxData, |
| 715 | size_t xBufferLengthBytes, |
| 716 | TickType_t xTicksToWait ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 717 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 718 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 719 | size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 720 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 721 | configASSERT( pvRxData ); |
| 722 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 723 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 724 | /* This receive function is used by both message buffers, which store |
| 725 | discrete messages, and stream buffers, which store a continuous stream of |
| 726 | bytes. Discrete messages include an additional |
| 727 | sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the |
| 728 | message. */ |
| 729 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 730 | { |
| 731 | xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH; |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | xBytesToStoreMessageLength = 0; |
| 736 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 737 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 738 | if( xTicksToWait != ( TickType_t ) 0 ) |
| 739 | { |
| 740 | /* Checking if there is data and clearing the notification state must be |
| 741 | performed atomically. */ |
| 742 | taskENTER_CRITICAL(); |
| 743 | { |
| 744 | xBytesAvailable = prvBytesInBuffer( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 745 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 746 | /* If this function was invoked by a message buffer read then |
| 747 | xBytesToStoreMessageLength holds the number of bytes used to hold |
| 748 | the length of the next discrete message. If this function was |
| 749 | invoked by a stream buffer read then xBytesToStoreMessageLength will |
| 750 | be 0. */ |
| 751 | if( xBytesAvailable <= xBytesToStoreMessageLength ) |
| 752 | { |
| 753 | /* Clear notification state as going to wait for data. */ |
| 754 | ( void ) xTaskNotifyStateClear( NULL ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 755 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 756 | /* Should only be one reader. */ |
| 757 | configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL ); |
| 758 | pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle(); |
| 759 | } |
| 760 | else |
| 761 | { |
| 762 | mtCOVERAGE_TEST_MARKER(); |
| 763 | } |
| 764 | } |
| 765 | taskEXIT_CRITICAL(); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 766 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 767 | if( xBytesAvailable <= xBytesToStoreMessageLength ) |
| 768 | { |
| 769 | /* Wait for data to be available. */ |
| 770 | traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer ); |
| 771 | ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait ); |
| 772 | pxStreamBuffer->xTaskWaitingToReceive = NULL; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 773 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 774 | /* Recheck the data available after blocking. */ |
| 775 | xBytesAvailable = prvBytesInBuffer( pxStreamBuffer ); |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | mtCOVERAGE_TEST_MARKER(); |
| 780 | } |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | xBytesAvailable = prvBytesInBuffer( pxStreamBuffer ); |
| 785 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 786 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 787 | /* Whether receiving a discrete message (where xBytesToStoreMessageLength |
| 788 | holds the number of bytes used to store the message length) or a stream of |
| 789 | bytes (where xBytesToStoreMessageLength is zero), the number of bytes |
| 790 | available must be greater than xBytesToStoreMessageLength to be able to |
| 791 | read bytes from the buffer. */ |
| 792 | if( xBytesAvailable > xBytesToStoreMessageLength ) |
| 793 | { |
| 794 | xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 795 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 796 | /* Was a task waiting for space in the buffer? */ |
| 797 | if( xReceivedLength != ( size_t ) 0 ) |
| 798 | { |
| 799 | traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength ); |
| 800 | sbRECEIVE_COMPLETED( pxStreamBuffer ); |
| 801 | } |
| 802 | else |
| 803 | { |
| 804 | mtCOVERAGE_TEST_MARKER(); |
| 805 | } |
| 806 | } |
| 807 | else |
| 808 | { |
| 809 | traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer ); |
| 810 | mtCOVERAGE_TEST_MARKER(); |
| 811 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 812 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 813 | return xReceivedLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 814 | } |
| 815 | /*-----------------------------------------------------------*/ |
| 816 | |
| 817 | size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ) |
| 818 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 819 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 820 | size_t xReturn, xBytesAvailable, xOriginalTail; |
| 821 | configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 822 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 823 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 824 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 825 | /* Ensure the stream buffer is being used as a message buffer. */ |
| 826 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 827 | { |
| 828 | xBytesAvailable = prvBytesInBuffer( pxStreamBuffer ); |
| 829 | if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH ) |
| 830 | { |
| 831 | /* The number of bytes available is greater than the number of bytes |
| 832 | required to hold the length of the next message, so another message |
| 833 | is available. Return its length without removing the length bytes |
| 834 | from the buffer. A copy of the tail is stored so the buffer can be |
| 835 | returned to its prior state as the message is not actually being |
| 836 | removed from the buffer. */ |
| 837 | xOriginalTail = pxStreamBuffer->xTail; |
| 838 | ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable ); |
| 839 | xReturn = ( size_t ) xTempReturn; |
| 840 | pxStreamBuffer->xTail = xOriginalTail; |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | /* The minimum amount of bytes in a message buffer is |
| 845 | ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is |
| 846 | less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid |
| 847 | value is 0. */ |
| 848 | configASSERT( xBytesAvailable == 0 ); |
| 849 | xReturn = 0; |
| 850 | } |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | xReturn = 0; |
| 855 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 856 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 857 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 858 | } |
| 859 | /*-----------------------------------------------------------*/ |
| 860 | |
| 861 | size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 862 | void *pvRxData, |
| 863 | size_t xBufferLengthBytes, |
| 864 | BaseType_t * const pxHigherPriorityTaskWoken ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 865 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 866 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 867 | size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 868 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 869 | configASSERT( pvRxData ); |
| 870 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 871 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 872 | /* This receive function is used by both message buffers, which store |
| 873 | discrete messages, and stream buffers, which store a continuous stream of |
| 874 | bytes. Discrete messages include an additional |
| 875 | sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the |
| 876 | message. */ |
| 877 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 878 | { |
| 879 | xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH; |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | xBytesToStoreMessageLength = 0; |
| 884 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 885 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 886 | xBytesAvailable = prvBytesInBuffer( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 887 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 888 | /* Whether receiving a discrete message (where xBytesToStoreMessageLength |
| 889 | holds the number of bytes used to store the message length) or a stream of |
| 890 | bytes (where xBytesToStoreMessageLength is zero), the number of bytes |
| 891 | available must be greater than xBytesToStoreMessageLength to be able to |
| 892 | read bytes from the buffer. */ |
| 893 | if( xBytesAvailable > xBytesToStoreMessageLength ) |
| 894 | { |
| 895 | xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 896 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 897 | /* Was a task waiting for space in the buffer? */ |
| 898 | if( xReceivedLength != ( size_t ) 0 ) |
| 899 | { |
| 900 | sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ); |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | mtCOVERAGE_TEST_MARKER(); |
| 905 | } |
| 906 | } |
| 907 | else |
| 908 | { |
| 909 | mtCOVERAGE_TEST_MARKER(); |
| 910 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 911 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 912 | traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 913 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 914 | return xReceivedLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 915 | } |
| 916 | /*-----------------------------------------------------------*/ |
| 917 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 918 | static size_t prvReadMessageFromBuffer( StreamBuffer_t *pxStreamBuffer, |
| 919 | void *pvRxData, |
| 920 | size_t xBufferLengthBytes, |
| 921 | size_t xBytesAvailable, |
| 922 | size_t xBytesToStoreMessageLength ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 923 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 924 | size_t xOriginalTail, xReceivedLength, xNextMessageLength; |
| 925 | configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 926 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 927 | if( xBytesToStoreMessageLength != ( size_t ) 0 ) |
| 928 | { |
| 929 | /* A discrete message is being received. First receive the length |
| 930 | of the message. A copy of the tail is stored so the buffer can be |
| 931 | returned to its prior state if the length of the message is too |
| 932 | large for the provided buffer. */ |
| 933 | xOriginalTail = pxStreamBuffer->xTail; |
| 934 | ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable ); |
| 935 | xNextMessageLength = ( size_t ) xTempNextMessageLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 936 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 937 | /* Reduce the number of bytes available by the number of bytes just |
| 938 | read out. */ |
| 939 | xBytesAvailable -= xBytesToStoreMessageLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 940 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 941 | /* Check there is enough space in the buffer provided by the |
| 942 | user. */ |
| 943 | if( xNextMessageLength > xBufferLengthBytes ) |
| 944 | { |
| 945 | /* The user has provided insufficient space to read the message |
| 946 | so return the buffer to its previous state (so the length of |
| 947 | the message is in the buffer again). */ |
| 948 | pxStreamBuffer->xTail = xOriginalTail; |
| 949 | xNextMessageLength = 0; |
| 950 | } |
| 951 | else |
| 952 | { |
| 953 | mtCOVERAGE_TEST_MARKER(); |
| 954 | } |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | /* A stream of bytes is being received (as opposed to a discrete |
| 959 | message), so read as many bytes as possible. */ |
| 960 | xNextMessageLength = xBufferLengthBytes; |
| 961 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 962 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 963 | /* Read the actual data. */ |
| 964 | 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. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 965 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 966 | return xReceivedLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 967 | } |
| 968 | /*-----------------------------------------------------------*/ |
| 969 | |
| 970 | BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ) |
| 971 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 972 | const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 973 | BaseType_t xReturn; |
| 974 | size_t xTail; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 975 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 976 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 977 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 978 | /* True if no bytes are available. */ |
| 979 | xTail = pxStreamBuffer->xTail; |
| 980 | if( pxStreamBuffer->xHead == xTail ) |
| 981 | { |
| 982 | xReturn = pdTRUE; |
| 983 | } |
| 984 | else |
| 985 | { |
| 986 | xReturn = pdFALSE; |
| 987 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 988 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 989 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 990 | } |
| 991 | /*-----------------------------------------------------------*/ |
| 992 | |
| 993 | BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ) |
| 994 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 995 | BaseType_t xReturn; |
| 996 | size_t xBytesToStoreMessageLength; |
| 997 | const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 998 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 999 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1000 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1001 | /* This generic version of the receive function is used by both message |
| 1002 | buffers, which store discrete messages, and stream buffers, which store a |
| 1003 | continuous stream of bytes. Discrete messages include an additional |
| 1004 | sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */ |
| 1005 | if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 ) |
| 1006 | { |
| 1007 | xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH; |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | xBytesToStoreMessageLength = 0; |
| 1012 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1013 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1014 | /* True if the available space equals zero. */ |
| 1015 | if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength ) |
| 1016 | { |
| 1017 | xReturn = pdTRUE; |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | xReturn = pdFALSE; |
| 1022 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1023 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1024 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1025 | } |
| 1026 | /*-----------------------------------------------------------*/ |
| 1027 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1028 | BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1029 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1030 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 1031 | BaseType_t xReturn; |
| 1032 | UBaseType_t uxSavedInterruptStatus; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1033 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1034 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1035 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1036 | uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); |
| 1037 | { |
| 1038 | if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) |
| 1039 | { |
| 1040 | ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, |
| 1041 | ( uint32_t ) 0, |
| 1042 | eNoAction, |
| 1043 | pxHigherPriorityTaskWoken ); |
| 1044 | ( pxStreamBuffer )->xTaskWaitingToReceive = NULL; |
| 1045 | xReturn = pdTRUE; |
| 1046 | } |
| 1047 | else |
| 1048 | { |
| 1049 | xReturn = pdFALSE; |
| 1050 | } |
| 1051 | } |
| 1052 | portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1053 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1054 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1055 | } |
| 1056 | /*-----------------------------------------------------------*/ |
| 1057 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1058 | BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1059 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1060 | StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; |
| 1061 | BaseType_t xReturn; |
| 1062 | UBaseType_t uxSavedInterruptStatus; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1063 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1064 | configASSERT( pxStreamBuffer ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1065 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1066 | uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); |
| 1067 | { |
| 1068 | if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) |
| 1069 | { |
| 1070 | ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, |
| 1071 | ( uint32_t ) 0, |
| 1072 | eNoAction, |
| 1073 | pxHigherPriorityTaskWoken ); |
| 1074 | ( pxStreamBuffer )->xTaskWaitingToSend = NULL; |
| 1075 | xReturn = pdTRUE; |
| 1076 | } |
| 1077 | else |
| 1078 | { |
| 1079 | xReturn = pdFALSE; |
| 1080 | } |
| 1081 | } |
| 1082 | portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1083 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1084 | return xReturn; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1085 | } |
| 1086 | /*-----------------------------------------------------------*/ |
| 1087 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1088 | static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer, const uint8_t *pucData, size_t xCount ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1089 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1090 | size_t xNextHead, xFirstLength; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1091 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1092 | configASSERT( xCount > ( size_t ) 0 ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1093 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1094 | xNextHead = pxStreamBuffer->xHead; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1095 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1096 | /* Calculate the number of bytes that can be added in the first write - |
| 1097 | which may be less than the total number of bytes that need to be added if |
| 1098 | the buffer will wrap back to the beginning. */ |
| 1099 | xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1100 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1101 | /* Write as many bytes as can be written in the first write. */ |
| 1102 | configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength ); |
| 1103 | ( void ) memcpy( ( void* ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1104 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1105 | /* If the number of bytes written was less than the number that could be |
| 1106 | written in the first write... */ |
| 1107 | if( xCount > xFirstLength ) |
| 1108 | { |
| 1109 | /* ...then write the remaining bytes to the start of the buffer. */ |
| 1110 | configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength ); |
| 1111 | ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */ |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | mtCOVERAGE_TEST_MARKER(); |
| 1116 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1117 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1118 | xNextHead += xCount; |
| 1119 | if( xNextHead >= pxStreamBuffer->xLength ) |
| 1120 | { |
| 1121 | xNextHead -= pxStreamBuffer->xLength; |
| 1122 | } |
| 1123 | else |
| 1124 | { |
| 1125 | mtCOVERAGE_TEST_MARKER(); |
| 1126 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1127 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1128 | pxStreamBuffer->xHead = xNextHead; |
| 1129 | |
| 1130 | return xCount; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1131 | } |
| 1132 | /*-----------------------------------------------------------*/ |
| 1133 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1134 | static size_t prvReadBytesFromBuffer( StreamBuffer_t *pxStreamBuffer, uint8_t *pucData, size_t xMaxCount, size_t xBytesAvailable ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1135 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1136 | size_t xCount, xFirstLength, xNextTail; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1137 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1138 | /* Use the minimum of the wanted bytes and the available bytes. */ |
| 1139 | xCount = configMIN( xBytesAvailable, xMaxCount ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1140 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1141 | if( xCount > ( size_t ) 0 ) |
| 1142 | { |
| 1143 | xNextTail = pxStreamBuffer->xTail; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1144 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1145 | /* Calculate the number of bytes that can be read - which may be |
| 1146 | less than the number wanted if the data wraps around to the start of |
| 1147 | the buffer. */ |
| 1148 | xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount ); |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1149 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1150 | /* Obtain the number of bytes it is possible to obtain in the first |
| 1151 | read. Asserts check bounds of read and write. */ |
| 1152 | configASSERT( xFirstLength <= xMaxCount ); |
| 1153 | configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength ); |
| 1154 | ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */ |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1155 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1156 | /* If the total number of wanted bytes is greater than the number |
| 1157 | that could be read in the first read... */ |
| 1158 | if( xCount > xFirstLength ) |
| 1159 | { |
| 1160 | /*...then read the remaining bytes from the start of the buffer. */ |
| 1161 | configASSERT( xCount <= xMaxCount ); |
| 1162 | ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */ |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | mtCOVERAGE_TEST_MARKER(); |
| 1167 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1168 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1169 | /* Move the tail pointer to effectively remove the data read from |
| 1170 | the buffer. */ |
| 1171 | xNextTail += xCount; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1172 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1173 | if( xNextTail >= pxStreamBuffer->xLength ) |
| 1174 | { |
| 1175 | xNextTail -= pxStreamBuffer->xLength; |
| 1176 | } |
| 1177 | |
| 1178 | pxStreamBuffer->xTail = xNextTail; |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | mtCOVERAGE_TEST_MARKER(); |
| 1183 | } |
| 1184 | |
| 1185 | return xCount; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1186 | } |
| 1187 | /*-----------------------------------------------------------*/ |
| 1188 | |
| 1189 | static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) |
| 1190 | { |
| 1191 | /* Returns the distance between xTail and xHead. */ |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1192 | size_t xCount; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1193 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1194 | xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead; |
| 1195 | xCount -= pxStreamBuffer->xTail; |
| 1196 | if ( xCount >= pxStreamBuffer->xLength ) |
| 1197 | { |
| 1198 | xCount -= pxStreamBuffer->xLength; |
| 1199 | } |
| 1200 | else |
| 1201 | { |
| 1202 | mtCOVERAGE_TEST_MARKER(); |
| 1203 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1204 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1205 | return xCount; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1206 | } |
| 1207 | /*-----------------------------------------------------------*/ |
| 1208 | |
| 1209 | static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer, |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1210 | uint8_t * const pucBuffer, |
| 1211 | size_t xBufferSizeBytes, |
| 1212 | size_t xTriggerLevelBytes, |
| 1213 | uint8_t ucFlags ) |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1214 | { |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1215 | /* Assert here is deliberately writing to the entire buffer to ensure it can |
| 1216 | be written to without generating exceptions, and is setting the buffer to a |
| 1217 | known value to assist in development/debugging. */ |
| 1218 | #if( configASSERT_DEFINED == 1 ) |
| 1219 | { |
| 1220 | /* The value written just has to be identifiable when looking at the |
| 1221 | memory. Don't use 0xA5 as that is the stack fill value and could |
| 1222 | result in confusion as to what is actually being observed. */ |
| 1223 | const BaseType_t xWriteValue = 0x55; |
| 1224 | configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer ); |
| 1225 | } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */ |
| 1226 | #endif |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1227 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1228 | ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */ |
| 1229 | pxStreamBuffer->pucBuffer = pucBuffer; |
| 1230 | pxStreamBuffer->xLength = xBufferSizeBytes; |
| 1231 | pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes; |
| 1232 | pxStreamBuffer->ucFlags = ucFlags; |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | #if ( configUSE_TRACE_FACILITY == 1 ) |
| 1236 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1237 | UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer ) |
| 1238 | { |
| 1239 | return xStreamBuffer->uxStreamBufferNumber; |
| 1240 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1241 | |
| 1242 | #endif /* configUSE_TRACE_FACILITY */ |
| 1243 | /*-----------------------------------------------------------*/ |
| 1244 | |
| 1245 | #if ( configUSE_TRACE_FACILITY == 1 ) |
| 1246 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1247 | void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer, UBaseType_t uxStreamBufferNumber ) |
| 1248 | { |
| 1249 | xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber; |
| 1250 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1251 | |
| 1252 | #endif /* configUSE_TRACE_FACILITY */ |
| 1253 | /*-----------------------------------------------------------*/ |
| 1254 | |
| 1255 | #if ( configUSE_TRACE_FACILITY == 1 ) |
| 1256 | |
xiaohu.huang | 4f321fb | 2024-03-22 14:50:29 +0800 | [diff] [blame] | 1257 | uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer ) |
| 1258 | { |
| 1259 | return ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ); |
| 1260 | } |
kelvin.zhang | 57fb6ae | 2021-10-15 10:19:42 +0800 | [diff] [blame] | 1261 | |
| 1262 | #endif /* configUSE_TRACE_FACILITY */ |
| 1263 | /*-----------------------------------------------------------*/ |