stream_buffer.c 74 KB

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