stream_buffer.c 52 KB

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