stream_buffer.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. * FreeRTOS Kernel V10.4.3
  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, them 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. /* The maximum amount of space a stream buffer will ever report is its length
  458. * minus 1. */
  459. const size_t xMaxReportedSpace = pxStreamBuffer->xLength - ( size_t ) 1;
  460. configASSERT( pvTxData );
  461. configASSERT( pxStreamBuffer );
  462. /* This send function is used to write to both message buffers and stream
  463. * buffers. If this is a message buffer then the space needed must be
  464. * increased by the amount of bytes needed to store the length of the
  465. * message. */
  466. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  467. {
  468. xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
  469. /* Overflow? */
  470. configASSERT( xRequiredSpace > xDataLengthBytes );
  471. /* If this is a message buffer then it must be possible to write the
  472. * whole message. */
  473. if( xRequiredSpace > xMaxReportedSpace )
  474. {
  475. /* The message would not fit even if the entire buffer was empty,
  476. * so don't wait for space. */
  477. xTicksToWait = ( TickType_t ) 0;
  478. }
  479. else
  480. {
  481. mtCOVERAGE_TEST_MARKER();
  482. }
  483. }
  484. else
  485. {
  486. /* If this is a stream buffer then it is acceptable to write only part
  487. * of the message to the buffer. Cap the length to the total length of
  488. * the buffer. */
  489. if( xRequiredSpace > xMaxReportedSpace )
  490. {
  491. xRequiredSpace = xMaxReportedSpace;
  492. }
  493. else
  494. {
  495. mtCOVERAGE_TEST_MARKER();
  496. }
  497. }
  498. if( xTicksToWait != ( TickType_t ) 0 )
  499. {
  500. vTaskSetTimeOutState( &xTimeOut );
  501. do
  502. {
  503. /* Wait until the required number of bytes are free in the message
  504. * buffer. */
  505. taskENTER_CRITICAL();
  506. {
  507. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  508. if( xSpace < xRequiredSpace )
  509. {
  510. /* Clear notification state as going to wait for space. */
  511. ( void ) xTaskNotifyStateClear( NULL );
  512. /* Should only be one writer. */
  513. configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
  514. pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
  515. }
  516. else
  517. {
  518. taskEXIT_CRITICAL();
  519. break;
  520. }
  521. }
  522. taskEXIT_CRITICAL();
  523. traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
  524. ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
  525. pxStreamBuffer->xTaskWaitingToSend = NULL;
  526. } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
  527. }
  528. else
  529. {
  530. mtCOVERAGE_TEST_MARKER();
  531. }
  532. if( xSpace == ( size_t ) 0 )
  533. {
  534. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  535. }
  536. else
  537. {
  538. mtCOVERAGE_TEST_MARKER();
  539. }
  540. xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
  541. if( xReturn > ( size_t ) 0 )
  542. {
  543. traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
  544. /* Was a task waiting for the data? */
  545. if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
  546. {
  547. sbSEND_COMPLETED( pxStreamBuffer );
  548. }
  549. else
  550. {
  551. mtCOVERAGE_TEST_MARKER();
  552. }
  553. }
  554. else
  555. {
  556. mtCOVERAGE_TEST_MARKER();
  557. traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
  558. }
  559. return xReturn;
  560. }
  561. /*-----------------------------------------------------------*/
  562. size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
  563. const void * pvTxData,
  564. size_t xDataLengthBytes,
  565. BaseType_t * const pxHigherPriorityTaskWoken )
  566. {
  567. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  568. size_t xReturn, xSpace;
  569. size_t xRequiredSpace = xDataLengthBytes;
  570. configASSERT( pvTxData );
  571. configASSERT( pxStreamBuffer );
  572. /* This send function is used to write to both message buffers and stream
  573. * buffers. If this is a message buffer then the space needed must be
  574. * increased by the amount of bytes needed to store the length of the
  575. * message. */
  576. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  577. {
  578. xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
  579. }
  580. else
  581. {
  582. mtCOVERAGE_TEST_MARKER();
  583. }
  584. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  585. xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
  586. if( xReturn > ( size_t ) 0 )
  587. {
  588. /* Was a task waiting for the data? */
  589. if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
  590. {
  591. sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  592. }
  593. else
  594. {
  595. mtCOVERAGE_TEST_MARKER();
  596. }
  597. }
  598. else
  599. {
  600. mtCOVERAGE_TEST_MARKER();
  601. }
  602. traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
  603. return xReturn;
  604. }
  605. /*-----------------------------------------------------------*/
  606. static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
  607. const void * pvTxData,
  608. size_t xDataLengthBytes,
  609. size_t xSpace,
  610. size_t xRequiredSpace )
  611. {
  612. BaseType_t xShouldWrite;
  613. size_t xReturn;
  614. if( xSpace == ( size_t ) 0 )
  615. {
  616. /* Doesn't matter if this is a stream buffer or a message buffer, there
  617. * is no space to write. */
  618. xShouldWrite = pdFALSE;
  619. }
  620. else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
  621. {
  622. /* This is a stream buffer, as opposed to a message buffer, so writing a
  623. * stream of bytes rather than discrete messages. Write as many bytes as
  624. * possible. */
  625. xShouldWrite = pdTRUE;
  626. xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
  627. }
  628. else if( xSpace >= xRequiredSpace )
  629. {
  630. /* This is a message buffer, as opposed to a stream buffer, and there
  631. * is enough space to write both the message length and the message itself
  632. * into the buffer. Start by writing the length of the data, the data
  633. * itself will be written later in this function. */
  634. xShouldWrite = pdTRUE;
  635. ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
  636. }
  637. else
  638. {
  639. /* There is space available, but not enough space. */
  640. xShouldWrite = pdFALSE;
  641. }
  642. if( xShouldWrite != pdFALSE )
  643. {
  644. /* Writes the data itself. */
  645. xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alignment and access. */
  646. }
  647. else
  648. {
  649. xReturn = 0;
  650. }
  651. return xReturn;
  652. }
  653. /*-----------------------------------------------------------*/
  654. size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
  655. void * pvRxData,
  656. size_t xBufferLengthBytes,
  657. TickType_t xTicksToWait )
  658. {
  659. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  660. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  661. configASSERT( pvRxData );
  662. configASSERT( pxStreamBuffer );
  663. /* This receive function is used by both message buffers, which store
  664. * discrete messages, and stream buffers, which store a continuous stream of
  665. * bytes. Discrete messages include an additional
  666. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  667. * message. */
  668. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  669. {
  670. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  671. }
  672. else
  673. {
  674. xBytesToStoreMessageLength = 0;
  675. }
  676. if( xTicksToWait != ( TickType_t ) 0 )
  677. {
  678. /* Checking if there is data and clearing the notification state must be
  679. * performed atomically. */
  680. taskENTER_CRITICAL();
  681. {
  682. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  683. /* If this function was invoked by a message buffer read then
  684. * xBytesToStoreMessageLength holds the number of bytes used to hold
  685. * the length of the next discrete message. If this function was
  686. * invoked by a stream buffer read then xBytesToStoreMessageLength will
  687. * be 0. */
  688. if( xBytesAvailable <= xBytesToStoreMessageLength )
  689. {
  690. /* Clear notification state as going to wait for data. */
  691. ( void ) xTaskNotifyStateClear( NULL );
  692. /* Should only be one reader. */
  693. configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
  694. pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
  695. }
  696. else
  697. {
  698. mtCOVERAGE_TEST_MARKER();
  699. }
  700. }
  701. taskEXIT_CRITICAL();
  702. if( xBytesAvailable <= xBytesToStoreMessageLength )
  703. {
  704. /* Wait for data to be available. */
  705. traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
  706. ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
  707. pxStreamBuffer->xTaskWaitingToReceive = NULL;
  708. /* Recheck the data available after blocking. */
  709. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  710. }
  711. else
  712. {
  713. mtCOVERAGE_TEST_MARKER();
  714. }
  715. }
  716. else
  717. {
  718. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  719. }
  720. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  721. * holds the number of bytes used to store the message length) or a stream of
  722. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  723. * available must be greater than xBytesToStoreMessageLength to be able to
  724. * read bytes from the buffer. */
  725. if( xBytesAvailable > xBytesToStoreMessageLength )
  726. {
  727. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
  728. /* Was a task waiting for space in the buffer? */
  729. if( xReceivedLength != ( size_t ) 0 )
  730. {
  731. traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
  732. sbRECEIVE_COMPLETED( pxStreamBuffer );
  733. }
  734. else
  735. {
  736. mtCOVERAGE_TEST_MARKER();
  737. }
  738. }
  739. else
  740. {
  741. traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
  742. mtCOVERAGE_TEST_MARKER();
  743. }
  744. return xReceivedLength;
  745. }
  746. /*-----------------------------------------------------------*/
  747. size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
  748. {
  749. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  750. size_t xReturn, xBytesAvailable, xOriginalTail;
  751. configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
  752. configASSERT( pxStreamBuffer );
  753. /* Ensure the stream buffer is being used as a message buffer. */
  754. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  755. {
  756. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  757. if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
  758. {
  759. /* The number of bytes available is greater than the number of bytes
  760. * required to hold the length of the next message, so another message
  761. * is available. Return its length without removing the length bytes
  762. * from the buffer. A copy of the tail is stored so the buffer can be
  763. * returned to its prior state as the message is not actually being
  764. * removed from the buffer. */
  765. xOriginalTail = pxStreamBuffer->xTail;
  766. ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
  767. xReturn = ( size_t ) xTempReturn;
  768. pxStreamBuffer->xTail = xOriginalTail;
  769. }
  770. else
  771. {
  772. /* The minimum amount of bytes in a message buffer is
  773. * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
  774. * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
  775. * value is 0. */
  776. configASSERT( xBytesAvailable == 0 );
  777. xReturn = 0;
  778. }
  779. }
  780. else
  781. {
  782. xReturn = 0;
  783. }
  784. return xReturn;
  785. }
  786. /*-----------------------------------------------------------*/
  787. size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
  788. void * pvRxData,
  789. size_t xBufferLengthBytes,
  790. BaseType_t * const pxHigherPriorityTaskWoken )
  791. {
  792. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  793. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  794. configASSERT( pvRxData );
  795. configASSERT( pxStreamBuffer );
  796. /* This receive function is used by both message buffers, which store
  797. * discrete messages, and stream buffers, which store a continuous stream of
  798. * bytes. Discrete messages include an additional
  799. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  800. * message. */
  801. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  802. {
  803. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  804. }
  805. else
  806. {
  807. xBytesToStoreMessageLength = 0;
  808. }
  809. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  810. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  811. * holds the number of bytes used to store the message length) or a stream of
  812. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  813. * available must be greater than xBytesToStoreMessageLength to be able to
  814. * read bytes from the buffer. */
  815. if( xBytesAvailable > xBytesToStoreMessageLength )
  816. {
  817. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
  818. /* Was a task waiting for space in the buffer? */
  819. if( xReceivedLength != ( size_t ) 0 )
  820. {
  821. sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  822. }
  823. else
  824. {
  825. mtCOVERAGE_TEST_MARKER();
  826. }
  827. }
  828. else
  829. {
  830. mtCOVERAGE_TEST_MARKER();
  831. }
  832. traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
  833. return xReceivedLength;
  834. }
  835. /*-----------------------------------------------------------*/
  836. static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
  837. void * pvRxData,
  838. size_t xBufferLengthBytes,
  839. size_t xBytesAvailable,
  840. size_t xBytesToStoreMessageLength )
  841. {
  842. size_t xOriginalTail, xReceivedLength, xNextMessageLength;
  843. configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
  844. if( xBytesToStoreMessageLength != ( size_t ) 0 )
  845. {
  846. /* A discrete message is being received. First receive the length
  847. * of the message. A copy of the tail is stored so the buffer can be
  848. * returned to its prior state if the length of the message is too
  849. * large for the provided buffer. */
  850. xOriginalTail = pxStreamBuffer->xTail;
  851. ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
  852. xNextMessageLength = ( size_t ) xTempNextMessageLength;
  853. /* Reduce the number of bytes available by the number of bytes just
  854. * read out. */
  855. xBytesAvailable -= xBytesToStoreMessageLength;
  856. /* Check there is enough space in the buffer provided by the
  857. * user. */
  858. if( xNextMessageLength > xBufferLengthBytes )
  859. {
  860. /* The user has provided insufficient space to read the message
  861. * so return the buffer to its previous state (so the length of
  862. * the message is in the buffer again). */
  863. pxStreamBuffer->xTail = xOriginalTail;
  864. xNextMessageLength = 0;
  865. }
  866. else
  867. {
  868. mtCOVERAGE_TEST_MARKER();
  869. }
  870. }
  871. else
  872. {
  873. /* A stream of bytes is being received (as opposed to a discrete
  874. * message), so read as many bytes as possible. */
  875. xNextMessageLength = xBufferLengthBytes;
  876. }
  877. /* Read the actual data. */
  878. 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. */
  879. return xReceivedLength;
  880. }
  881. /*-----------------------------------------------------------*/
  882. BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
  883. {
  884. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  885. BaseType_t xReturn;
  886. size_t xTail;
  887. configASSERT( pxStreamBuffer );
  888. /* True if no bytes are available. */
  889. xTail = pxStreamBuffer->xTail;
  890. if( pxStreamBuffer->xHead == xTail )
  891. {
  892. xReturn = pdTRUE;
  893. }
  894. else
  895. {
  896. xReturn = pdFALSE;
  897. }
  898. return xReturn;
  899. }
  900. /*-----------------------------------------------------------*/
  901. BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
  902. {
  903. BaseType_t xReturn;
  904. size_t xBytesToStoreMessageLength;
  905. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  906. configASSERT( pxStreamBuffer );
  907. /* This generic version of the receive function is used by both message
  908. * buffers, which store discrete messages, and stream buffers, which store a
  909. * continuous stream of bytes. Discrete messages include an additional
  910. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
  911. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  912. {
  913. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  914. }
  915. else
  916. {
  917. xBytesToStoreMessageLength = 0;
  918. }
  919. /* True if the available space equals zero. */
  920. if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
  921. {
  922. xReturn = pdTRUE;
  923. }
  924. else
  925. {
  926. xReturn = pdFALSE;
  927. }
  928. return xReturn;
  929. }
  930. /*-----------------------------------------------------------*/
  931. BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  932. BaseType_t * pxHigherPriorityTaskWoken )
  933. {
  934. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  935. BaseType_t xReturn;
  936. UBaseType_t uxSavedInterruptStatus;
  937. configASSERT( pxStreamBuffer );
  938. uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
  939. {
  940. if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
  941. {
  942. ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
  943. ( uint32_t ) 0,
  944. eNoAction,
  945. pxHigherPriorityTaskWoken );
  946. ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
  947. xReturn = pdTRUE;
  948. }
  949. else
  950. {
  951. xReturn = pdFALSE;
  952. }
  953. }
  954. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  955. return xReturn;
  956. }
  957. /*-----------------------------------------------------------*/
  958. BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  959. BaseType_t * pxHigherPriorityTaskWoken )
  960. {
  961. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  962. BaseType_t xReturn;
  963. UBaseType_t uxSavedInterruptStatus;
  964. configASSERT( pxStreamBuffer );
  965. uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
  966. {
  967. if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
  968. {
  969. ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
  970. ( uint32_t ) 0,
  971. eNoAction,
  972. pxHigherPriorityTaskWoken );
  973. ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
  974. xReturn = pdTRUE;
  975. }
  976. else
  977. {
  978. xReturn = pdFALSE;
  979. }
  980. }
  981. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  982. return xReturn;
  983. }
  984. /*-----------------------------------------------------------*/
  985. static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
  986. const uint8_t * pucData,
  987. size_t xCount )
  988. {
  989. size_t xNextHead, xFirstLength;
  990. configASSERT( xCount > ( size_t ) 0 );
  991. xNextHead = pxStreamBuffer->xHead;
  992. /* Calculate the number of bytes that can be added in the first write -
  993. * which may be less than the total number of bytes that need to be added if
  994. * the buffer will wrap back to the beginning. */
  995. xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
  996. /* Write as many bytes as can be written in the first write. */
  997. configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
  998. ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  999. /* If the number of bytes written was less than the number that could be
  1000. * written in the first write... */
  1001. if( xCount > xFirstLength )
  1002. {
  1003. /* ...then write the remaining bytes to the start of the buffer. */
  1004. configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
  1005. ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1006. }
  1007. else
  1008. {
  1009. mtCOVERAGE_TEST_MARKER();
  1010. }
  1011. xNextHead += xCount;
  1012. if( xNextHead >= pxStreamBuffer->xLength )
  1013. {
  1014. xNextHead -= pxStreamBuffer->xLength;
  1015. }
  1016. else
  1017. {
  1018. mtCOVERAGE_TEST_MARKER();
  1019. }
  1020. pxStreamBuffer->xHead = xNextHead;
  1021. return xCount;
  1022. }
  1023. /*-----------------------------------------------------------*/
  1024. static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
  1025. uint8_t * pucData,
  1026. size_t xMaxCount,
  1027. size_t xBytesAvailable )
  1028. {
  1029. size_t xCount, xFirstLength, xNextTail;
  1030. /* Use the minimum of the wanted bytes and the available bytes. */
  1031. xCount = configMIN( xBytesAvailable, xMaxCount );
  1032. if( xCount > ( size_t ) 0 )
  1033. {
  1034. xNextTail = pxStreamBuffer->xTail;
  1035. /* Calculate the number of bytes that can be read - which may be
  1036. * less than the number wanted if the data wraps around to the start of
  1037. * the buffer. */
  1038. xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
  1039. /* Obtain the number of bytes it is possible to obtain in the first
  1040. * read. Asserts check bounds of read and write. */
  1041. configASSERT( xFirstLength <= xMaxCount );
  1042. configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
  1043. ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1044. /* If the total number of wanted bytes is greater than the number
  1045. * that could be read in the first read... */
  1046. if( xCount > xFirstLength )
  1047. {
  1048. /*...then read the remaining bytes from the start of the buffer. */
  1049. configASSERT( xCount <= xMaxCount );
  1050. ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1051. }
  1052. else
  1053. {
  1054. mtCOVERAGE_TEST_MARKER();
  1055. }
  1056. /* Move the tail pointer to effectively remove the data read from
  1057. * the buffer. */
  1058. xNextTail += xCount;
  1059. if( xNextTail >= pxStreamBuffer->xLength )
  1060. {
  1061. xNextTail -= pxStreamBuffer->xLength;
  1062. }
  1063. pxStreamBuffer->xTail = xNextTail;
  1064. }
  1065. else
  1066. {
  1067. mtCOVERAGE_TEST_MARKER();
  1068. }
  1069. return xCount;
  1070. }
  1071. /*-----------------------------------------------------------*/
  1072. static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
  1073. {
  1074. /* Returns the distance between xTail and xHead. */
  1075. size_t xCount;
  1076. xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
  1077. xCount -= pxStreamBuffer->xTail;
  1078. if( xCount >= pxStreamBuffer->xLength )
  1079. {
  1080. xCount -= pxStreamBuffer->xLength;
  1081. }
  1082. else
  1083. {
  1084. mtCOVERAGE_TEST_MARKER();
  1085. }
  1086. return xCount;
  1087. }
  1088. /*-----------------------------------------------------------*/
  1089. static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
  1090. uint8_t * const pucBuffer,
  1091. size_t xBufferSizeBytes,
  1092. size_t xTriggerLevelBytes,
  1093. uint8_t ucFlags )
  1094. {
  1095. /* Assert here is deliberately writing to the entire buffer to ensure it can
  1096. * be written to without generating exceptions, and is setting the buffer to a
  1097. * known value to assist in development/debugging. */
  1098. #if ( configASSERT_DEFINED == 1 )
  1099. {
  1100. /* The value written just has to be identifiable when looking at the
  1101. * memory. Don't use 0xA5 as that is the stack fill value and could
  1102. * result in confusion as to what is actually being observed. */
  1103. const BaseType_t xWriteValue = 0x55;
  1104. configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
  1105. } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */
  1106. #endif
  1107. ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
  1108. pxStreamBuffer->pucBuffer = pucBuffer;
  1109. pxStreamBuffer->xLength = xBufferSizeBytes;
  1110. pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
  1111. pxStreamBuffer->ucFlags = ucFlags;
  1112. }
  1113. #if ( configUSE_TRACE_FACILITY == 1 )
  1114. UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
  1115. {
  1116. return xStreamBuffer->uxStreamBufferNumber;
  1117. }
  1118. #endif /* configUSE_TRACE_FACILITY */
  1119. /*-----------------------------------------------------------*/
  1120. #if ( configUSE_TRACE_FACILITY == 1 )
  1121. void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
  1122. UBaseType_t uxStreamBufferNumber )
  1123. {
  1124. xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
  1125. }
  1126. #endif /* configUSE_TRACE_FACILITY */
  1127. /*-----------------------------------------------------------*/
  1128. #if ( configUSE_TRACE_FACILITY == 1 )
  1129. uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
  1130. {
  1131. return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
  1132. }
  1133. #endif /* configUSE_TRACE_FACILITY */
  1134. /*-----------------------------------------------------------*/