stream_buffer.c 60 KB

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