stream_buffer.c 54 KB

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