stream_buffer.c 57 KB

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