stream_buffer.c 57 KB

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