stream_buffer.c 57 KB

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