stream_buffer.c 63 KB

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