stream_buffer.c 54 KB

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