stream_buffer.c 57 KB

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