stream_buffer.c 53 KB

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