stream_buffer.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  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. /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  39. * for the header files above, but not in this file, in order to generate the
  40. * correct privileged Vs unprivileged linkage and placement. */
  41. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  42. /* This entire source file will be skipped if the application is not configured
  43. * to include stream buffer functionality. This #if is closed at the very bottom
  44. * of this file. If you want to include stream buffers then ensure
  45. * configUSE_STREAM_BUFFERS is set to 1 in FreeRTOSConfig.h. */
  46. #if ( configUSE_STREAM_BUFFERS == 1 )
  47. #if ( configUSE_TASK_NOTIFICATIONS != 1 )
  48. #error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
  49. #endif
  50. #if ( INCLUDE_xTaskGetCurrentTaskHandle != 1 )
  51. #error INCLUDE_xTaskGetCurrentTaskHandle must be set to 1 to build stream_buffer.c
  52. #endif
  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. /* Overflow? */
  833. configASSERT( xRequiredSpace > xDataLengthBytes );
  834. }
  835. else
  836. {
  837. mtCOVERAGE_TEST_MARKER();
  838. }
  839. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  840. xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
  841. if( xReturn > ( size_t ) 0 )
  842. {
  843. /* Was a task waiting for the data? */
  844. if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
  845. {
  846. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  847. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  848. /* coverity[misra_c_2012_directive_4_7_violation] */
  849. prvSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  850. }
  851. else
  852. {
  853. mtCOVERAGE_TEST_MARKER();
  854. }
  855. }
  856. else
  857. {
  858. mtCOVERAGE_TEST_MARKER();
  859. }
  860. traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
  861. traceRETURN_xStreamBufferSendFromISR( xReturn );
  862. return xReturn;
  863. }
  864. /*-----------------------------------------------------------*/
  865. static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
  866. const void * pvTxData,
  867. size_t xDataLengthBytes,
  868. size_t xSpace,
  869. size_t xRequiredSpace )
  870. {
  871. size_t xNextHead = pxStreamBuffer->xHead;
  872. configMESSAGE_BUFFER_LENGTH_TYPE xMessageLength;
  873. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  874. {
  875. /* This is a message buffer, as opposed to a stream buffer. */
  876. /* Convert xDataLengthBytes to the message length type. */
  877. xMessageLength = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
  878. /* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
  879. configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
  880. if( xSpace >= xRequiredSpace )
  881. {
  882. /* There is enough space to write both the message length and the message
  883. * itself into the buffer. Start by writing the length of the data, the data
  884. * itself will be written later in this function. */
  885. xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
  886. }
  887. else
  888. {
  889. /* Not enough space, so do not write data to the buffer. */
  890. xDataLengthBytes = 0;
  891. }
  892. }
  893. else
  894. {
  895. /* This is a stream buffer, as opposed to a message buffer, so writing a
  896. * stream of bytes rather than discrete messages. Plan to write as many
  897. * bytes as possible. */
  898. xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
  899. }
  900. if( xDataLengthBytes != ( size_t ) 0 )
  901. {
  902. /* Write the data to the buffer. */
  903. /* MISRA Ref 11.5.5 [Void pointer assignment] */
  904. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  905. /* coverity[misra_c_2012_rule_11_5_violation] */
  906. pxStreamBuffer->xHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes, xNextHead );
  907. }
  908. return xDataLengthBytes;
  909. }
  910. /*-----------------------------------------------------------*/
  911. size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
  912. void * pvRxData,
  913. size_t xBufferLengthBytes,
  914. TickType_t xTicksToWait )
  915. {
  916. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  917. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  918. traceENTER_xStreamBufferReceive( xStreamBuffer, pvRxData, xBufferLengthBytes, xTicksToWait );
  919. configASSERT( pvRxData );
  920. configASSERT( pxStreamBuffer );
  921. /* This receive function is used by both message buffers, which store
  922. * discrete messages, and stream buffers, which store a continuous stream of
  923. * bytes. Discrete messages include an additional
  924. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  925. * message. */
  926. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  927. {
  928. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  929. }
  930. else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_BATCHING_BUFFER ) != ( uint8_t ) 0 )
  931. {
  932. /* Force task to block if the batching buffer contains less bytes than
  933. * the trigger level. */
  934. xBytesToStoreMessageLength = pxStreamBuffer->xTriggerLevelBytes;
  935. }
  936. else
  937. {
  938. xBytesToStoreMessageLength = 0;
  939. }
  940. if( xTicksToWait != ( TickType_t ) 0 )
  941. {
  942. /* Checking if there is data and clearing the notification state must be
  943. * performed atomically. */
  944. taskENTER_CRITICAL();
  945. {
  946. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  947. /* If this function was invoked by a message buffer read then
  948. * xBytesToStoreMessageLength holds the number of bytes used to hold
  949. * the length of the next discrete message. If this function was
  950. * invoked by a stream buffer read then xBytesToStoreMessageLength will
  951. * be 0. If this function was invoked by a stream batch buffer read
  952. * then xBytesToStoreMessageLength will be xTriggerLevelBytes value
  953. * for the buffer.*/
  954. if( xBytesAvailable <= xBytesToStoreMessageLength )
  955. {
  956. /* Clear notification state as going to wait for data. */
  957. ( void ) xTaskNotifyStateClearIndexed( NULL, pxStreamBuffer->uxNotificationIndex );
  958. /* Should only be one reader. */
  959. configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
  960. pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
  961. }
  962. else
  963. {
  964. mtCOVERAGE_TEST_MARKER();
  965. }
  966. }
  967. taskEXIT_CRITICAL();
  968. if( xBytesAvailable <= xBytesToStoreMessageLength )
  969. {
  970. /* Wait for data to be available. */
  971. traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
  972. ( void ) xTaskNotifyWaitIndexed( pxStreamBuffer->uxNotificationIndex, ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
  973. pxStreamBuffer->xTaskWaitingToReceive = NULL;
  974. /* Recheck the data available after blocking. */
  975. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  976. }
  977. else
  978. {
  979. mtCOVERAGE_TEST_MARKER();
  980. }
  981. }
  982. else
  983. {
  984. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  985. }
  986. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  987. * holds the number of bytes used to store the message length) or a stream of
  988. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  989. * available must be greater than xBytesToStoreMessageLength to be able to
  990. * read bytes from the buffer. */
  991. if( xBytesAvailable > xBytesToStoreMessageLength )
  992. {
  993. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
  994. /* Was a task waiting for space in the buffer? */
  995. if( xReceivedLength != ( size_t ) 0 )
  996. {
  997. traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
  998. prvRECEIVE_COMPLETED( xStreamBuffer );
  999. }
  1000. else
  1001. {
  1002. mtCOVERAGE_TEST_MARKER();
  1003. }
  1004. }
  1005. else
  1006. {
  1007. traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
  1008. mtCOVERAGE_TEST_MARKER();
  1009. }
  1010. traceRETURN_xStreamBufferReceive( xReceivedLength );
  1011. return xReceivedLength;
  1012. }
  1013. /*-----------------------------------------------------------*/
  1014. size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
  1015. {
  1016. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1017. size_t xReturn, xBytesAvailable;
  1018. configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
  1019. traceENTER_xStreamBufferNextMessageLengthBytes( xStreamBuffer );
  1020. configASSERT( pxStreamBuffer );
  1021. /* Ensure the stream buffer is being used as a message buffer. */
  1022. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  1023. {
  1024. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  1025. if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
  1026. {
  1027. /* The number of bytes available is greater than the number of bytes
  1028. * required to hold the length of the next message, so another message
  1029. * is available. */
  1030. ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, pxStreamBuffer->xTail );
  1031. xReturn = ( size_t ) xTempReturn;
  1032. }
  1033. else
  1034. {
  1035. /* The minimum amount of bytes in a message buffer is
  1036. * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
  1037. * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
  1038. * value is 0. */
  1039. configASSERT( xBytesAvailable == 0 );
  1040. xReturn = 0;
  1041. }
  1042. }
  1043. else
  1044. {
  1045. xReturn = 0;
  1046. }
  1047. traceRETURN_xStreamBufferNextMessageLengthBytes( xReturn );
  1048. return xReturn;
  1049. }
  1050. /*-----------------------------------------------------------*/
  1051. size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
  1052. void * pvRxData,
  1053. size_t xBufferLengthBytes,
  1054. BaseType_t * const pxHigherPriorityTaskWoken )
  1055. {
  1056. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1057. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  1058. traceENTER_xStreamBufferReceiveFromISR( xStreamBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken );
  1059. configASSERT( pvRxData );
  1060. configASSERT( pxStreamBuffer );
  1061. /* This receive function is used by both message buffers, which store
  1062. * discrete messages, and stream buffers, which store a continuous stream of
  1063. * bytes. Discrete messages include an additional
  1064. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  1065. * message. */
  1066. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  1067. {
  1068. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  1069. }
  1070. else
  1071. {
  1072. xBytesToStoreMessageLength = 0;
  1073. }
  1074. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  1075. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  1076. * holds the number of bytes used to store the message length) or a stream of
  1077. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  1078. * available must be greater than xBytesToStoreMessageLength to be able to
  1079. * read bytes from the buffer. */
  1080. if( xBytesAvailable > xBytesToStoreMessageLength )
  1081. {
  1082. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable );
  1083. /* Was a task waiting for space in the buffer? */
  1084. if( xReceivedLength != ( size_t ) 0 )
  1085. {
  1086. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  1087. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  1088. /* coverity[misra_c_2012_directive_4_7_violation] */
  1089. prvRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  1090. }
  1091. else
  1092. {
  1093. mtCOVERAGE_TEST_MARKER();
  1094. }
  1095. }
  1096. else
  1097. {
  1098. mtCOVERAGE_TEST_MARKER();
  1099. }
  1100. traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
  1101. traceRETURN_xStreamBufferReceiveFromISR( xReceivedLength );
  1102. return xReceivedLength;
  1103. }
  1104. /*-----------------------------------------------------------*/
  1105. static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
  1106. void * pvRxData,
  1107. size_t xBufferLengthBytes,
  1108. size_t xBytesAvailable )
  1109. {
  1110. size_t xCount, xNextMessageLength;
  1111. configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
  1112. size_t xNextTail = pxStreamBuffer->xTail;
  1113. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  1114. {
  1115. /* A discrete message is being received. First receive the length
  1116. * of the message. */
  1117. xNextTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextTail );
  1118. xNextMessageLength = ( size_t ) xTempNextMessageLength;
  1119. /* Reduce the number of bytes available by the number of bytes just
  1120. * read out. */
  1121. xBytesAvailable -= sbBYTES_TO_STORE_MESSAGE_LENGTH;
  1122. /* Check there is enough space in the buffer provided by the
  1123. * user. */
  1124. if( xNextMessageLength > xBufferLengthBytes )
  1125. {
  1126. /* The user has provided insufficient space to read the message. */
  1127. xNextMessageLength = 0;
  1128. }
  1129. else
  1130. {
  1131. mtCOVERAGE_TEST_MARKER();
  1132. }
  1133. }
  1134. else
  1135. {
  1136. /* A stream of bytes is being received (as opposed to a discrete
  1137. * message), so read as many bytes as possible. */
  1138. xNextMessageLength = xBufferLengthBytes;
  1139. }
  1140. /* Use the minimum of the wanted bytes and the available bytes. */
  1141. xCount = configMIN( xNextMessageLength, xBytesAvailable );
  1142. if( xCount != ( size_t ) 0 )
  1143. {
  1144. /* Read the actual data and update the tail to mark the data as officially consumed. */
  1145. /* MISRA Ref 11.5.5 [Void pointer assignment] */
  1146. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1147. /* coverity[misra_c_2012_rule_11_5_violation] */
  1148. pxStreamBuffer->xTail = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xCount, xNextTail );
  1149. }
  1150. return xCount;
  1151. }
  1152. /*-----------------------------------------------------------*/
  1153. BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
  1154. {
  1155. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1156. BaseType_t xReturn;
  1157. size_t xTail;
  1158. traceENTER_xStreamBufferIsEmpty( xStreamBuffer );
  1159. configASSERT( pxStreamBuffer );
  1160. /* True if no bytes are available. */
  1161. xTail = pxStreamBuffer->xTail;
  1162. if( pxStreamBuffer->xHead == xTail )
  1163. {
  1164. xReturn = pdTRUE;
  1165. }
  1166. else
  1167. {
  1168. xReturn = pdFALSE;
  1169. }
  1170. traceRETURN_xStreamBufferIsEmpty( xReturn );
  1171. return xReturn;
  1172. }
  1173. /*-----------------------------------------------------------*/
  1174. BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
  1175. {
  1176. BaseType_t xReturn;
  1177. size_t xBytesToStoreMessageLength;
  1178. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1179. traceENTER_xStreamBufferIsFull( xStreamBuffer );
  1180. configASSERT( pxStreamBuffer );
  1181. /* This generic version of the receive function is used by both message
  1182. * buffers, which store discrete messages, and stream buffers, which store a
  1183. * continuous stream of bytes. Discrete messages include an additional
  1184. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
  1185. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  1186. {
  1187. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  1188. }
  1189. else
  1190. {
  1191. xBytesToStoreMessageLength = 0;
  1192. }
  1193. /* True if the available space equals zero. */
  1194. if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
  1195. {
  1196. xReturn = pdTRUE;
  1197. }
  1198. else
  1199. {
  1200. xReturn = pdFALSE;
  1201. }
  1202. traceRETURN_xStreamBufferIsFull( xReturn );
  1203. return xReturn;
  1204. }
  1205. /*-----------------------------------------------------------*/
  1206. BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  1207. BaseType_t * pxHigherPriorityTaskWoken )
  1208. {
  1209. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1210. BaseType_t xReturn;
  1211. UBaseType_t uxSavedInterruptStatus;
  1212. traceENTER_xStreamBufferSendCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
  1213. configASSERT( pxStreamBuffer );
  1214. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  1215. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  1216. /* coverity[misra_c_2012_directive_4_7_violation] */
  1217. uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
  1218. {
  1219. if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
  1220. {
  1221. ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
  1222. ( pxStreamBuffer )->uxNotificationIndex,
  1223. ( uint32_t ) 0,
  1224. eNoAction,
  1225. pxHigherPriorityTaskWoken );
  1226. ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
  1227. xReturn = pdTRUE;
  1228. }
  1229. else
  1230. {
  1231. xReturn = pdFALSE;
  1232. }
  1233. }
  1234. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  1235. traceRETURN_xStreamBufferSendCompletedFromISR( xReturn );
  1236. return xReturn;
  1237. }
  1238. /*-----------------------------------------------------------*/
  1239. BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  1240. BaseType_t * pxHigherPriorityTaskWoken )
  1241. {
  1242. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1243. BaseType_t xReturn;
  1244. UBaseType_t uxSavedInterruptStatus;
  1245. traceENTER_xStreamBufferReceiveCompletedFromISR( xStreamBuffer, pxHigherPriorityTaskWoken );
  1246. configASSERT( pxStreamBuffer );
  1247. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  1248. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  1249. /* coverity[misra_c_2012_directive_4_7_violation] */
  1250. uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
  1251. {
  1252. if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
  1253. {
  1254. ( void ) xTaskNotifyIndexedFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
  1255. ( pxStreamBuffer )->uxNotificationIndex,
  1256. ( uint32_t ) 0,
  1257. eNoAction,
  1258. pxHigherPriorityTaskWoken );
  1259. ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
  1260. xReturn = pdTRUE;
  1261. }
  1262. else
  1263. {
  1264. xReturn = pdFALSE;
  1265. }
  1266. }
  1267. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  1268. traceRETURN_xStreamBufferReceiveCompletedFromISR( xReturn );
  1269. return xReturn;
  1270. }
  1271. /*-----------------------------------------------------------*/
  1272. static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
  1273. const uint8_t * pucData,
  1274. size_t xCount,
  1275. size_t xHead )
  1276. {
  1277. size_t xFirstLength;
  1278. configASSERT( xCount > ( size_t ) 0 );
  1279. /* Calculate the number of bytes that can be added in the first write -
  1280. * which may be less than the total number of bytes that need to be added if
  1281. * the buffer will wrap back to the beginning. */
  1282. xFirstLength = configMIN( pxStreamBuffer->xLength - xHead, xCount );
  1283. /* Write as many bytes as can be written in the first write. */
  1284. configASSERT( ( xHead + xFirstLength ) <= pxStreamBuffer->xLength );
  1285. ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xHead ] ) ), ( const void * ) pucData, xFirstLength );
  1286. /* If the number of bytes written was less than the number that could be
  1287. * written in the first write... */
  1288. if( xCount > xFirstLength )
  1289. {
  1290. /* ...then write the remaining bytes to the start of the buffer. */
  1291. configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
  1292. ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength );
  1293. }
  1294. else
  1295. {
  1296. mtCOVERAGE_TEST_MARKER();
  1297. }
  1298. xHead += xCount;
  1299. if( xHead >= pxStreamBuffer->xLength )
  1300. {
  1301. xHead -= pxStreamBuffer->xLength;
  1302. }
  1303. else
  1304. {
  1305. mtCOVERAGE_TEST_MARKER();
  1306. }
  1307. return xHead;
  1308. }
  1309. /*-----------------------------------------------------------*/
  1310. static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
  1311. uint8_t * pucData,
  1312. size_t xCount,
  1313. size_t xTail )
  1314. {
  1315. size_t xFirstLength;
  1316. configASSERT( xCount != ( size_t ) 0 );
  1317. /* Calculate the number of bytes that can be read - which may be
  1318. * less than the number wanted if the data wraps around to the start of
  1319. * the buffer. */
  1320. xFirstLength = configMIN( pxStreamBuffer->xLength - xTail, xCount );
  1321. /* Obtain the number of bytes it is possible to obtain in the first
  1322. * read. Asserts check bounds of read and write. */
  1323. configASSERT( xFirstLength <= xCount );
  1324. configASSERT( ( xTail + xFirstLength ) <= pxStreamBuffer->xLength );
  1325. ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xTail ] ), xFirstLength );
  1326. /* If the total number of wanted bytes is greater than the number
  1327. * that could be read in the first read... */
  1328. if( xCount > xFirstLength )
  1329. {
  1330. /* ...then read the remaining bytes from the start of the buffer. */
  1331. ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength );
  1332. }
  1333. else
  1334. {
  1335. mtCOVERAGE_TEST_MARKER();
  1336. }
  1337. /* Move the tail pointer to effectively remove the data read from the buffer. */
  1338. xTail += xCount;
  1339. if( xTail >= pxStreamBuffer->xLength )
  1340. {
  1341. xTail -= pxStreamBuffer->xLength;
  1342. }
  1343. return xTail;
  1344. }
  1345. /*-----------------------------------------------------------*/
  1346. static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
  1347. {
  1348. /* Returns the distance between xTail and xHead. */
  1349. size_t xCount;
  1350. xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
  1351. xCount -= pxStreamBuffer->xTail;
  1352. if( xCount >= pxStreamBuffer->xLength )
  1353. {
  1354. xCount -= pxStreamBuffer->xLength;
  1355. }
  1356. else
  1357. {
  1358. mtCOVERAGE_TEST_MARKER();
  1359. }
  1360. return xCount;
  1361. }
  1362. /*-----------------------------------------------------------*/
  1363. static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
  1364. uint8_t * const pucBuffer,
  1365. size_t xBufferSizeBytes,
  1366. size_t xTriggerLevelBytes,
  1367. uint8_t ucFlags,
  1368. StreamBufferCallbackFunction_t pxSendCompletedCallback,
  1369. StreamBufferCallbackFunction_t pxReceiveCompletedCallback )
  1370. {
  1371. /* Assert here is deliberately writing to the entire buffer to ensure it can
  1372. * be written to without generating exceptions, and is setting the buffer to a
  1373. * known value to assist in development/debugging. */
  1374. #if ( configASSERT_DEFINED == 1 )
  1375. {
  1376. /* The value written just has to be identifiable when looking at the
  1377. * memory. Don't use 0xA5 as that is the stack fill value and could
  1378. * result in confusion as to what is actually being observed. */
  1379. #define STREAM_BUFFER_BUFFER_WRITE_VALUE ( 0x55 )
  1380. configASSERT( memset( pucBuffer, ( int ) STREAM_BUFFER_BUFFER_WRITE_VALUE, xBufferSizeBytes ) == pucBuffer );
  1381. }
  1382. #endif
  1383. ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
  1384. pxStreamBuffer->pucBuffer = pucBuffer;
  1385. pxStreamBuffer->xLength = xBufferSizeBytes;
  1386. pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
  1387. pxStreamBuffer->ucFlags = ucFlags;
  1388. pxStreamBuffer->uxNotificationIndex = tskDEFAULT_INDEX_TO_NOTIFY;
  1389. #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
  1390. {
  1391. pxStreamBuffer->pxSendCompletedCallback = pxSendCompletedCallback;
  1392. pxStreamBuffer->pxReceiveCompletedCallback = pxReceiveCompletedCallback;
  1393. }
  1394. #else
  1395. {
  1396. /* MISRA Ref 11.1.1 [Object type casting] */
  1397. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
  1398. /* coverity[misra_c_2012_rule_11_1_violation] */
  1399. ( void ) pxSendCompletedCallback;
  1400. /* MISRA Ref 11.1.1 [Object type casting] */
  1401. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-111 */
  1402. /* coverity[misra_c_2012_rule_11_1_violation] */
  1403. ( void ) pxReceiveCompletedCallback;
  1404. }
  1405. #endif /* if ( configUSE_SB_COMPLETED_CALLBACK == 1 ) */
  1406. }
  1407. /*-----------------------------------------------------------*/
  1408. UBaseType_t uxStreamBufferGetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer )
  1409. {
  1410. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1411. traceENTER_uxStreamBufferGetStreamBufferNotificationIndex( xStreamBuffer );
  1412. configASSERT( pxStreamBuffer );
  1413. traceRETURN_uxStreamBufferGetStreamBufferNotificationIndex( pxStreamBuffer->uxNotificationIndex );
  1414. return pxStreamBuffer->uxNotificationIndex;
  1415. }
  1416. /*-----------------------------------------------------------*/
  1417. void vStreamBufferSetStreamBufferNotificationIndex( StreamBufferHandle_t xStreamBuffer,
  1418. UBaseType_t uxNotificationIndex )
  1419. {
  1420. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  1421. traceENTER_vStreamBufferSetStreamBufferNotificationIndex( xStreamBuffer, uxNotificationIndex );
  1422. /* There should be no task waiting otherwise we'd never resume them. */
  1423. configASSERT( ( pxStreamBuffer != NULL ) && ( pxStreamBuffer->xTaskWaitingToReceive == NULL ) );
  1424. configASSERT( ( pxStreamBuffer != NULL ) && ( pxStreamBuffer->xTaskWaitingToSend == NULL ) );
  1425. /* Check that the task notification index is valid. */
  1426. configASSERT( uxNotificationIndex < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  1427. pxStreamBuffer->uxNotificationIndex = uxNotificationIndex;
  1428. traceRETURN_vStreamBufferSetStreamBufferNotificationIndex();
  1429. }
  1430. /*-----------------------------------------------------------*/
  1431. #if ( configUSE_TRACE_FACILITY == 1 )
  1432. UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
  1433. {
  1434. traceENTER_uxStreamBufferGetStreamBufferNumber( xStreamBuffer );
  1435. traceRETURN_uxStreamBufferGetStreamBufferNumber( xStreamBuffer->uxStreamBufferNumber );
  1436. return xStreamBuffer->uxStreamBufferNumber;
  1437. }
  1438. #endif /* configUSE_TRACE_FACILITY */
  1439. /*-----------------------------------------------------------*/
  1440. #if ( configUSE_TRACE_FACILITY == 1 )
  1441. void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
  1442. UBaseType_t uxStreamBufferNumber )
  1443. {
  1444. traceENTER_vStreamBufferSetStreamBufferNumber( xStreamBuffer, uxStreamBufferNumber );
  1445. xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
  1446. traceRETURN_vStreamBufferSetStreamBufferNumber();
  1447. }
  1448. #endif /* configUSE_TRACE_FACILITY */
  1449. /*-----------------------------------------------------------*/
  1450. #if ( configUSE_TRACE_FACILITY == 1 )
  1451. uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
  1452. {
  1453. traceENTER_ucStreamBufferGetStreamBufferType( xStreamBuffer );
  1454. traceRETURN_ucStreamBufferGetStreamBufferType( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
  1455. return( ( uint8_t ) ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) );
  1456. }
  1457. #endif /* configUSE_TRACE_FACILITY */
  1458. /*-----------------------------------------------------------*/
  1459. /* This entire source file will be skipped if the application is not configured
  1460. * to include stream buffer functionality. This #if is closed at the very bottom
  1461. * of this file. If you want to include stream buffers then ensure
  1462. * configUSE_STREAM_BUFFERS is set to 1 in FreeRTOSConfig.h. */
  1463. #endif /* configUSE_STREAM_BUFFERS == 1 */