stream_buffer.c 56 KB

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