stream_buffer.c 52 KB

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