stream_buffer.c 41 KB

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