stream_buffer.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /*
  2. * FreeRTOS Kernel V10.4.1
  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. xBufferSizeBytes++;
  235. pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) ); /*lint !e9079 malloc() only returns void*. */
  236. if( pucAllocatedMemory != NULL )
  237. {
  238. 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. */
  239. pucAllocatedMemory + sizeof( StreamBuffer_t ), /* Storage area follows. */ /*lint !e9016 Indexing past structure valid for uint8_t pointer, also storage area has no alignment requirement. */
  240. xBufferSizeBytes,
  241. xTriggerLevelBytes,
  242. ucFlags );
  243. traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer );
  244. }
  245. else
  246. {
  247. traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
  248. }
  249. return ( StreamBufferHandle_t ) pucAllocatedMemory; /*lint !e9087 !e826 Safe cast as allocated memory is aligned. */
  250. }
  251. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  252. /*-----------------------------------------------------------*/
  253. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  254. StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
  255. size_t xTriggerLevelBytes,
  256. BaseType_t xIsMessageBuffer,
  257. uint8_t * const pucStreamBufferStorageArea,
  258. StaticStreamBuffer_t * const pxStaticStreamBuffer )
  259. {
  260. StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer; /*lint !e740 !e9087 Safe cast as StaticStreamBuffer_t is opaque Streambuffer_t. */
  261. StreamBufferHandle_t xReturn;
  262. uint8_t ucFlags;
  263. configASSERT( pucStreamBufferStorageArea );
  264. configASSERT( pxStaticStreamBuffer );
  265. configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
  266. /* A trigger level of 0 would cause a waiting task to unblock even when
  267. * the buffer was empty. */
  268. if( xTriggerLevelBytes == ( size_t ) 0 )
  269. {
  270. xTriggerLevelBytes = ( size_t ) 1;
  271. }
  272. if( xIsMessageBuffer != pdFALSE )
  273. {
  274. /* Statically allocated message buffer. */
  275. ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
  276. }
  277. else
  278. {
  279. /* Statically allocated stream buffer. */
  280. ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
  281. }
  282. /* In case the stream buffer is going to be used as a message buffer
  283. * (that is, it will hold discrete messages with a little meta data that
  284. * says how big the next message is) check the buffer will be large enough
  285. * to hold at least one message. */
  286. configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
  287. #if ( configASSERT_DEFINED == 1 )
  288. {
  289. /* Sanity check that the size of the structure used to declare a
  290. * variable of type StaticStreamBuffer_t equals the size of the real
  291. * message buffer structure. */
  292. volatile size_t xSize = sizeof( StaticStreamBuffer_t );
  293. configASSERT( xSize == sizeof( StreamBuffer_t ) );
  294. } /*lint !e529 xSize is referenced is configASSERT() is defined. */
  295. #endif /* configASSERT_DEFINED */
  296. if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
  297. {
  298. prvInitialiseNewStreamBuffer( pxStreamBuffer,
  299. pucStreamBufferStorageArea,
  300. xBufferSizeBytes,
  301. xTriggerLevelBytes,
  302. ucFlags );
  303. /* Remember this was statically allocated in case it is ever deleted
  304. * again. */
  305. pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
  306. traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
  307. xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer; /*lint !e9087 Data hiding requires cast to opaque type. */
  308. }
  309. else
  310. {
  311. xReturn = NULL;
  312. traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
  313. }
  314. return xReturn;
  315. }
  316. #endif /* ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  317. /*-----------------------------------------------------------*/
  318. void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
  319. {
  320. StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
  321. configASSERT( pxStreamBuffer );
  322. traceSTREAM_BUFFER_DELETE( xStreamBuffer );
  323. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
  324. {
  325. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  326. {
  327. /* Both the structure and the buffer were allocated using a single call
  328. * to pvPortMalloc(), hence only one call to vPortFree() is required. */
  329. vPortFree( ( void * ) pxStreamBuffer ); /*lint !e9087 Standard free() semantics require void *, plus pxStreamBuffer was allocated by pvPortMalloc(). */
  330. }
  331. #else
  332. {
  333. /* Should not be possible to get here, ucFlags must be corrupt.
  334. * Force an assert. */
  335. configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
  336. }
  337. #endif
  338. }
  339. else
  340. {
  341. /* The structure and buffer were not allocated dynamically and cannot be
  342. * freed - just scrub the structure so future use will assert. */
  343. ( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
  344. }
  345. }
  346. /*-----------------------------------------------------------*/
  347. BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
  348. {
  349. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  350. BaseType_t xReturn = pdFAIL;
  351. #if ( configUSE_TRACE_FACILITY == 1 )
  352. UBaseType_t uxStreamBufferNumber;
  353. #endif
  354. configASSERT( pxStreamBuffer );
  355. #if ( configUSE_TRACE_FACILITY == 1 )
  356. {
  357. /* Store the stream buffer number so it can be restored after the
  358. * reset. */
  359. uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
  360. }
  361. #endif
  362. /* Can only reset a message buffer if there are no tasks blocked on it. */
  363. taskENTER_CRITICAL();
  364. {
  365. if( pxStreamBuffer->xTaskWaitingToReceive == NULL )
  366. {
  367. if( pxStreamBuffer->xTaskWaitingToSend == NULL )
  368. {
  369. prvInitialiseNewStreamBuffer( pxStreamBuffer,
  370. pxStreamBuffer->pucBuffer,
  371. pxStreamBuffer->xLength,
  372. pxStreamBuffer->xTriggerLevelBytes,
  373. pxStreamBuffer->ucFlags );
  374. xReturn = pdPASS;
  375. #if ( configUSE_TRACE_FACILITY == 1 )
  376. {
  377. pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
  378. }
  379. #endif
  380. traceSTREAM_BUFFER_RESET( xStreamBuffer );
  381. }
  382. }
  383. }
  384. taskEXIT_CRITICAL();
  385. return xReturn;
  386. }
  387. /*-----------------------------------------------------------*/
  388. BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
  389. size_t xTriggerLevel )
  390. {
  391. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  392. BaseType_t xReturn;
  393. configASSERT( pxStreamBuffer );
  394. /* It is not valid for the trigger level to be 0. */
  395. if( xTriggerLevel == ( size_t ) 0 )
  396. {
  397. xTriggerLevel = ( size_t ) 1;
  398. }
  399. /* The trigger level is the number of bytes that must be in the stream
  400. * buffer before a task that is waiting for data is unblocked. */
  401. if( xTriggerLevel <= pxStreamBuffer->xLength )
  402. {
  403. pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
  404. xReturn = pdPASS;
  405. }
  406. else
  407. {
  408. xReturn = pdFALSE;
  409. }
  410. return xReturn;
  411. }
  412. /*-----------------------------------------------------------*/
  413. size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
  414. {
  415. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  416. size_t xSpace;
  417. configASSERT( pxStreamBuffer );
  418. xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
  419. xSpace -= pxStreamBuffer->xHead;
  420. xSpace -= ( size_t ) 1;
  421. if( xSpace >= pxStreamBuffer->xLength )
  422. {
  423. xSpace -= pxStreamBuffer->xLength;
  424. }
  425. else
  426. {
  427. mtCOVERAGE_TEST_MARKER();
  428. }
  429. return xSpace;
  430. }
  431. /*-----------------------------------------------------------*/
  432. size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
  433. {
  434. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  435. size_t xReturn;
  436. configASSERT( pxStreamBuffer );
  437. xReturn = prvBytesInBuffer( pxStreamBuffer );
  438. return xReturn;
  439. }
  440. /*-----------------------------------------------------------*/
  441. size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
  442. const void * pvTxData,
  443. size_t xDataLengthBytes,
  444. TickType_t xTicksToWait )
  445. {
  446. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  447. size_t xReturn, xSpace = 0;
  448. size_t xRequiredSpace = xDataLengthBytes;
  449. TimeOut_t xTimeOut;
  450. /* Having a 'isFeasible' variable allows to respect the convention that there is only a return statement at the end. Othewise, return
  451. * could be done as soon as we realise the send cannot happen. We will let the call to 'prvWriteMessageToBuffer' dealing with this scenario. */
  452. BaseType_t xIsFeasible;
  453. configASSERT( pvTxData );
  454. configASSERT( pxStreamBuffer );
  455. /* This send function is used to write to both message buffers and stream
  456. * buffers. If this is a message buffer then the space needed must be
  457. * increased by the amount of bytes needed to store the length of the
  458. * message. */
  459. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  460. {
  461. xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
  462. /* Overflow? */
  463. configASSERT( xRequiredSpace > xDataLengthBytes );
  464. /* In the case of the message buffer, one has to be able to write the complete message as opposed to
  465. * a stream buffer for semantic reasons. Check if it is physically possible to write the message given
  466. * the length of the buffer. */
  467. if( xRequiredSpace > pxStreamBuffer->xLength )
  468. {
  469. /* The message could never be written because it is greater than the buffer length.
  470. * By setting xIsFeasable to FALSE, we skip over the following do..while loop, thus avoiding
  471. * a deadlock. The call to 'prvWriteMessageToBuffer' toward the end of this function with
  472. * xRequiredSpace greater than xSpace will suffice in not writing anything to the internal buffer.
  473. * Now, the function will return 0 because the message could not be written. Should an error code be
  474. * returned instead ??? In my opinion, probably.. But the return type doesn't allow for negative
  475. * values to be returned. A confusion could exist to the caller. Returning 0 because a timeout occurred
  476. * and a subsequent send attempts could eventually succeed, and returning 0 because a write could never
  477. * happen because of the size are two scenarios to me :/ */
  478. xIsFeasible = pdFALSE;
  479. }
  480. else
  481. {
  482. /* It is possible to write the message completely in the buffer. This is the intended route.
  483. * Let's continue with the regular timeout logic. */
  484. xIsFeasible = pdTRUE;
  485. }
  486. }
  487. else
  488. {
  489. /* In the case of the stream buffer, not being able to completely write the message in the buffer
  490. * is an acceptable scenario, but it has to be dealt with properly */
  491. if( xRequiredSpace > pxStreamBuffer->xLength )
  492. {
  493. /* Not enough buffer space. We will attempt to write as much as we can in this run
  494. * so that the caller can send the remaining in subsequent calls. We avoid a deadlock by
  495. * offering the possibility to take the 'else' branch in the 'if( xSpace < xRequiredSpace )'
  496. * condition inside the following do..while loop */
  497. xRequiredSpace = pxStreamBuffer->xLength;
  498. /* TODO FIXME: Is there a check we should do with the xTriggerLevelBytes value ? */
  499. /* With the adjustment to 'xRequiredSpace', the deadlock is avoided, thus it's now feasible. */
  500. xIsFeasible = pdTRUE;
  501. }
  502. else
  503. {
  504. /* It is possible to write the message completely in the buffer. */
  505. xIsFeasible = pdTRUE;
  506. }
  507. }
  508. /* Added check against xIsFeasible. If it's not feasible, don't even wait for notification, let the call to 'prvWriteMessageToBuffer' do nothing and return 0 */
  509. if( ( xTicksToWait != ( TickType_t ) 0 ) && ( xIsFeasible == pdTRUE ) )
  510. {
  511. vTaskSetTimeOutState( &xTimeOut );
  512. do
  513. {
  514. /* Wait until the required number of bytes are free in the message
  515. * buffer. */
  516. taskENTER_CRITICAL();
  517. {
  518. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  519. if( xSpace < xRequiredSpace )
  520. {
  521. /* Clear notification state as going to wait for space. */
  522. ( void ) xTaskNotifyStateClear( NULL );
  523. /* Should only be one writer. */
  524. configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
  525. pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
  526. }
  527. else
  528. {
  529. taskEXIT_CRITICAL();
  530. break;
  531. }
  532. }
  533. taskEXIT_CRITICAL();
  534. traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
  535. ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
  536. pxStreamBuffer->xTaskWaitingToSend = NULL;
  537. } while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
  538. }
  539. else
  540. {
  541. mtCOVERAGE_TEST_MARKER();
  542. }
  543. if( xSpace == ( size_t ) 0 )
  544. {
  545. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  546. }
  547. else
  548. {
  549. mtCOVERAGE_TEST_MARKER();
  550. }
  551. xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
  552. if( xReturn > ( size_t ) 0 )
  553. {
  554. traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
  555. /* Was a task waiting for the data? */
  556. if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
  557. {
  558. sbSEND_COMPLETED( pxStreamBuffer );
  559. }
  560. else
  561. {
  562. mtCOVERAGE_TEST_MARKER();
  563. }
  564. }
  565. else
  566. {
  567. mtCOVERAGE_TEST_MARKER();
  568. traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
  569. }
  570. return xReturn;
  571. }
  572. /*-----------------------------------------------------------*/
  573. size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
  574. const void * pvTxData,
  575. size_t xDataLengthBytes,
  576. BaseType_t * const pxHigherPriorityTaskWoken )
  577. {
  578. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  579. size_t xReturn, xSpace;
  580. size_t xRequiredSpace = xDataLengthBytes;
  581. configASSERT( pvTxData );
  582. configASSERT( pxStreamBuffer );
  583. /* This send function is used to write to both message buffers and stream
  584. * buffers. If this is a message buffer then the space needed must be
  585. * increased by the amount of bytes needed to store the length of the
  586. * message. */
  587. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  588. {
  589. xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
  590. }
  591. else
  592. {
  593. mtCOVERAGE_TEST_MARKER();
  594. }
  595. xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
  596. xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
  597. if( xReturn > ( size_t ) 0 )
  598. {
  599. /* Was a task waiting for the data? */
  600. if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
  601. {
  602. sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  603. }
  604. else
  605. {
  606. mtCOVERAGE_TEST_MARKER();
  607. }
  608. }
  609. else
  610. {
  611. mtCOVERAGE_TEST_MARKER();
  612. }
  613. traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
  614. return xReturn;
  615. }
  616. /*-----------------------------------------------------------*/
  617. static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
  618. const void * pvTxData,
  619. size_t xDataLengthBytes,
  620. size_t xSpace,
  621. size_t xRequiredSpace )
  622. {
  623. BaseType_t xShouldWrite;
  624. size_t xReturn;
  625. if( xSpace == ( size_t ) 0 )
  626. {
  627. /* Doesn't matter if this is a stream buffer or a message buffer, there
  628. * is no space to write. */
  629. xShouldWrite = pdFALSE;
  630. }
  631. else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
  632. {
  633. /* This is a stream buffer, as opposed to a message buffer, so writing a
  634. * stream of bytes rather than discrete messages. Write as many bytes as
  635. * possible. */
  636. xShouldWrite = pdTRUE;
  637. xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
  638. }
  639. else if( xSpace >= xRequiredSpace )
  640. {
  641. /* This is a message buffer, as opposed to a stream buffer, and there
  642. * is enough space to write both the message length and the message itself
  643. * into the buffer. Start by writing the length of the data, the data
  644. * itself will be written later in this function. */
  645. xShouldWrite = pdTRUE;
  646. ( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
  647. }
  648. else
  649. {
  650. /* There is space available, but not enough space. */
  651. xShouldWrite = pdFALSE;
  652. }
  653. if( xShouldWrite != pdFALSE )
  654. {
  655. /* Writes the data itself. */
  656. xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes ); /*lint !e9079 Storage buffer is implemented as uint8_t for ease of sizing, alignment and access. */
  657. }
  658. else
  659. {
  660. xReturn = 0;
  661. }
  662. return xReturn;
  663. }
  664. /*-----------------------------------------------------------*/
  665. size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
  666. void * pvRxData,
  667. size_t xBufferLengthBytes,
  668. TickType_t xTicksToWait )
  669. {
  670. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  671. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  672. configASSERT( pvRxData );
  673. configASSERT( pxStreamBuffer );
  674. /* This receive function is used by both message buffers, which store
  675. * discrete messages, and stream buffers, which store a continuous stream of
  676. * bytes. Discrete messages include an additional
  677. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  678. * message. */
  679. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  680. {
  681. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  682. }
  683. else
  684. {
  685. xBytesToStoreMessageLength = 0;
  686. }
  687. if( xTicksToWait != ( TickType_t ) 0 )
  688. {
  689. /* Checking if there is data and clearing the notification state must be
  690. * performed atomically. */
  691. taskENTER_CRITICAL();
  692. {
  693. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  694. /* If this function was invoked by a message buffer read then
  695. * xBytesToStoreMessageLength holds the number of bytes used to hold
  696. * the length of the next discrete message. If this function was
  697. * invoked by a stream buffer read then xBytesToStoreMessageLength will
  698. * be 0. */
  699. if( xBytesAvailable <= xBytesToStoreMessageLength )
  700. {
  701. /* Clear notification state as going to wait for data. */
  702. ( void ) xTaskNotifyStateClear( NULL );
  703. /* Should only be one reader. */
  704. configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
  705. pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
  706. }
  707. else
  708. {
  709. mtCOVERAGE_TEST_MARKER();
  710. }
  711. }
  712. taskEXIT_CRITICAL();
  713. if( xBytesAvailable <= xBytesToStoreMessageLength )
  714. {
  715. /* Wait for data to be available. */
  716. traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
  717. ( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
  718. pxStreamBuffer->xTaskWaitingToReceive = NULL;
  719. /* Recheck the data available after blocking. */
  720. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  721. }
  722. else
  723. {
  724. mtCOVERAGE_TEST_MARKER();
  725. }
  726. }
  727. else
  728. {
  729. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  730. }
  731. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  732. * holds the number of bytes used to store the message length) or a stream of
  733. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  734. * available must be greater than xBytesToStoreMessageLength to be able to
  735. * read bytes from the buffer. */
  736. if( xBytesAvailable > xBytesToStoreMessageLength )
  737. {
  738. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
  739. /* Was a task waiting for space in the buffer? */
  740. if( xReceivedLength != ( size_t ) 0 )
  741. {
  742. traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
  743. sbRECEIVE_COMPLETED( pxStreamBuffer );
  744. }
  745. else
  746. {
  747. mtCOVERAGE_TEST_MARKER();
  748. }
  749. }
  750. else
  751. {
  752. traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
  753. mtCOVERAGE_TEST_MARKER();
  754. }
  755. return xReceivedLength;
  756. }
  757. /*-----------------------------------------------------------*/
  758. size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
  759. {
  760. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  761. size_t xReturn, xBytesAvailable, xOriginalTail;
  762. configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
  763. configASSERT( pxStreamBuffer );
  764. /* Ensure the stream buffer is being used as a message buffer. */
  765. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  766. {
  767. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  768. if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
  769. {
  770. /* The number of bytes available is greater than the number of bytes
  771. * required to hold the length of the next message, so another message
  772. * is available. Return its length without removing the length bytes
  773. * from the buffer. A copy of the tail is stored so the buffer can be
  774. * returned to its prior state as the message is not actually being
  775. * removed from the buffer. */
  776. xOriginalTail = pxStreamBuffer->xTail;
  777. ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
  778. xReturn = ( size_t ) xTempReturn;
  779. pxStreamBuffer->xTail = xOriginalTail;
  780. }
  781. else
  782. {
  783. /* The minimum amount of bytes in a message buffer is
  784. * ( sbBYTES_TO_STORE_MESSAGE_LENGTH + 1 ), so if xBytesAvailable is
  785. * less than sbBYTES_TO_STORE_MESSAGE_LENGTH the only other valid
  786. * value is 0. */
  787. configASSERT( xBytesAvailable == 0 );
  788. xReturn = 0;
  789. }
  790. }
  791. else
  792. {
  793. xReturn = 0;
  794. }
  795. return xReturn;
  796. }
  797. /*-----------------------------------------------------------*/
  798. size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
  799. void * pvRxData,
  800. size_t xBufferLengthBytes,
  801. BaseType_t * const pxHigherPriorityTaskWoken )
  802. {
  803. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  804. size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
  805. configASSERT( pvRxData );
  806. configASSERT( pxStreamBuffer );
  807. /* This receive function is used by both message buffers, which store
  808. * discrete messages, and stream buffers, which store a continuous stream of
  809. * bytes. Discrete messages include an additional
  810. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the
  811. * message. */
  812. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  813. {
  814. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  815. }
  816. else
  817. {
  818. xBytesToStoreMessageLength = 0;
  819. }
  820. xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
  821. /* Whether receiving a discrete message (where xBytesToStoreMessageLength
  822. * holds the number of bytes used to store the message length) or a stream of
  823. * bytes (where xBytesToStoreMessageLength is zero), the number of bytes
  824. * available must be greater than xBytesToStoreMessageLength to be able to
  825. * read bytes from the buffer. */
  826. if( xBytesAvailable > xBytesToStoreMessageLength )
  827. {
  828. xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
  829. /* Was a task waiting for space in the buffer? */
  830. if( xReceivedLength != ( size_t ) 0 )
  831. {
  832. sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
  833. }
  834. else
  835. {
  836. mtCOVERAGE_TEST_MARKER();
  837. }
  838. }
  839. else
  840. {
  841. mtCOVERAGE_TEST_MARKER();
  842. }
  843. traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
  844. return xReceivedLength;
  845. }
  846. /*-----------------------------------------------------------*/
  847. static size_t prvReadMessageFromBuffer( StreamBuffer_t * pxStreamBuffer,
  848. void * pvRxData,
  849. size_t xBufferLengthBytes,
  850. size_t xBytesAvailable,
  851. size_t xBytesToStoreMessageLength )
  852. {
  853. size_t xOriginalTail, xReceivedLength, xNextMessageLength;
  854. configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
  855. if( xBytesToStoreMessageLength != ( size_t ) 0 )
  856. {
  857. /* A discrete message is being received. First receive the length
  858. * of the message. A copy of the tail is stored so the buffer can be
  859. * returned to its prior state if the length of the message is too
  860. * large for the provided buffer. */
  861. xOriginalTail = pxStreamBuffer->xTail;
  862. ( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
  863. xNextMessageLength = ( size_t ) xTempNextMessageLength;
  864. /* Reduce the number of bytes available by the number of bytes just
  865. * read out. */
  866. xBytesAvailable -= xBytesToStoreMessageLength;
  867. /* Check there is enough space in the buffer provided by the
  868. * user. */
  869. if( xNextMessageLength > xBufferLengthBytes )
  870. {
  871. /* The user has provided insufficient space to read the message
  872. * so return the buffer to its previous state (so the length of
  873. * the message is in the buffer again). */
  874. pxStreamBuffer->xTail = xOriginalTail;
  875. xNextMessageLength = 0;
  876. }
  877. else
  878. {
  879. mtCOVERAGE_TEST_MARKER();
  880. }
  881. }
  882. else
  883. {
  884. /* A stream of bytes is being received (as opposed to a discrete
  885. * message), so read as many bytes as possible. */
  886. xNextMessageLength = xBufferLengthBytes;
  887. }
  888. /* Read the actual data. */
  889. 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. */
  890. return xReceivedLength;
  891. }
  892. /*-----------------------------------------------------------*/
  893. BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
  894. {
  895. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  896. BaseType_t xReturn;
  897. size_t xTail;
  898. configASSERT( pxStreamBuffer );
  899. /* True if no bytes are available. */
  900. xTail = pxStreamBuffer->xTail;
  901. if( pxStreamBuffer->xHead == xTail )
  902. {
  903. xReturn = pdTRUE;
  904. }
  905. else
  906. {
  907. xReturn = pdFALSE;
  908. }
  909. return xReturn;
  910. }
  911. /*-----------------------------------------------------------*/
  912. BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
  913. {
  914. BaseType_t xReturn;
  915. size_t xBytesToStoreMessageLength;
  916. const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  917. configASSERT( pxStreamBuffer );
  918. /* This generic version of the receive function is used by both message
  919. * buffers, which store discrete messages, and stream buffers, which store a
  920. * continuous stream of bytes. Discrete messages include an additional
  921. * sbBYTES_TO_STORE_MESSAGE_LENGTH bytes that hold the length of the message. */
  922. if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
  923. {
  924. xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
  925. }
  926. else
  927. {
  928. xBytesToStoreMessageLength = 0;
  929. }
  930. /* True if the available space equals zero. */
  931. if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
  932. {
  933. xReturn = pdTRUE;
  934. }
  935. else
  936. {
  937. xReturn = pdFALSE;
  938. }
  939. return xReturn;
  940. }
  941. /*-----------------------------------------------------------*/
  942. BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  943. BaseType_t * pxHigherPriorityTaskWoken )
  944. {
  945. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  946. BaseType_t xReturn;
  947. UBaseType_t uxSavedInterruptStatus;
  948. configASSERT( pxStreamBuffer );
  949. uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
  950. {
  951. if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
  952. {
  953. ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
  954. ( uint32_t ) 0,
  955. eNoAction,
  956. pxHigherPriorityTaskWoken );
  957. ( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
  958. xReturn = pdTRUE;
  959. }
  960. else
  961. {
  962. xReturn = pdFALSE;
  963. }
  964. }
  965. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  966. return xReturn;
  967. }
  968. /*-----------------------------------------------------------*/
  969. BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
  970. BaseType_t * pxHigherPriorityTaskWoken )
  971. {
  972. StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
  973. BaseType_t xReturn;
  974. UBaseType_t uxSavedInterruptStatus;
  975. configASSERT( pxStreamBuffer );
  976. uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
  977. {
  978. if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
  979. {
  980. ( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
  981. ( uint32_t ) 0,
  982. eNoAction,
  983. pxHigherPriorityTaskWoken );
  984. ( pxStreamBuffer )->xTaskWaitingToSend = NULL;
  985. xReturn = pdTRUE;
  986. }
  987. else
  988. {
  989. xReturn = pdFALSE;
  990. }
  991. }
  992. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  993. return xReturn;
  994. }
  995. /*-----------------------------------------------------------*/
  996. static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer,
  997. const uint8_t * pucData,
  998. size_t xCount )
  999. {
  1000. size_t xNextHead, xFirstLength;
  1001. configASSERT( xCount > ( size_t ) 0 );
  1002. xNextHead = pxStreamBuffer->xHead;
  1003. /* Calculate the number of bytes that can be added in the first write -
  1004. * which may be less than the total number of bytes that need to be added if
  1005. * the buffer will wrap back to the beginning. */
  1006. xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
  1007. /* Write as many bytes as can be written in the first write. */
  1008. configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
  1009. ( void ) memcpy( ( void * ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1010. /* If the number of bytes written was less than the number that could be
  1011. * written in the first write... */
  1012. if( xCount > xFirstLength )
  1013. {
  1014. /* ...then write the remaining bytes to the start of the buffer. */
  1015. configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
  1016. ( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1017. }
  1018. else
  1019. {
  1020. mtCOVERAGE_TEST_MARKER();
  1021. }
  1022. xNextHead += xCount;
  1023. if( xNextHead >= pxStreamBuffer->xLength )
  1024. {
  1025. xNextHead -= pxStreamBuffer->xLength;
  1026. }
  1027. else
  1028. {
  1029. mtCOVERAGE_TEST_MARKER();
  1030. }
  1031. pxStreamBuffer->xHead = xNextHead;
  1032. return xCount;
  1033. }
  1034. /*-----------------------------------------------------------*/
  1035. static size_t prvReadBytesFromBuffer( StreamBuffer_t * pxStreamBuffer,
  1036. uint8_t * pucData,
  1037. size_t xMaxCount,
  1038. size_t xBytesAvailable )
  1039. {
  1040. size_t xCount, xFirstLength, xNextTail;
  1041. /* Use the minimum of the wanted bytes and the available bytes. */
  1042. xCount = configMIN( xBytesAvailable, xMaxCount );
  1043. if( xCount > ( size_t ) 0 )
  1044. {
  1045. xNextTail = pxStreamBuffer->xTail;
  1046. /* Calculate the number of bytes that can be read - which may be
  1047. * less than the number wanted if the data wraps around to the start of
  1048. * the buffer. */
  1049. xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
  1050. /* Obtain the number of bytes it is possible to obtain in the first
  1051. * read. Asserts check bounds of read and write. */
  1052. configASSERT( xFirstLength <= xMaxCount );
  1053. configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
  1054. ( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1055. /* If the total number of wanted bytes is greater than the number
  1056. * that could be read in the first read... */
  1057. if( xCount > xFirstLength )
  1058. {
  1059. /*...then read the remaining bytes from the start of the buffer. */
  1060. configASSERT( xCount <= xMaxCount );
  1061. ( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength ); /*lint !e9087 memcpy() requires void *. */
  1062. }
  1063. else
  1064. {
  1065. mtCOVERAGE_TEST_MARKER();
  1066. }
  1067. /* Move the tail pointer to effectively remove the data read from
  1068. * the buffer. */
  1069. xNextTail += xCount;
  1070. if( xNextTail >= pxStreamBuffer->xLength )
  1071. {
  1072. xNextTail -= pxStreamBuffer->xLength;
  1073. }
  1074. pxStreamBuffer->xTail = xNextTail;
  1075. }
  1076. else
  1077. {
  1078. mtCOVERAGE_TEST_MARKER();
  1079. }
  1080. return xCount;
  1081. }
  1082. /*-----------------------------------------------------------*/
  1083. static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
  1084. {
  1085. /* Returns the distance between xTail and xHead. */
  1086. size_t xCount;
  1087. xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
  1088. xCount -= pxStreamBuffer->xTail;
  1089. if( xCount >= pxStreamBuffer->xLength )
  1090. {
  1091. xCount -= pxStreamBuffer->xLength;
  1092. }
  1093. else
  1094. {
  1095. mtCOVERAGE_TEST_MARKER();
  1096. }
  1097. return xCount;
  1098. }
  1099. /*-----------------------------------------------------------*/
  1100. static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
  1101. uint8_t * const pucBuffer,
  1102. size_t xBufferSizeBytes,
  1103. size_t xTriggerLevelBytes,
  1104. uint8_t ucFlags )
  1105. {
  1106. /* Assert here is deliberately writing to the entire buffer to ensure it can
  1107. * be written to without generating exceptions, and is setting the buffer to a
  1108. * known value to assist in development/debugging. */
  1109. #if ( configASSERT_DEFINED == 1 )
  1110. {
  1111. /* The value written just has to be identifiable when looking at the
  1112. * memory. Don't use 0xA5 as that is the stack fill value and could
  1113. * result in confusion as to what is actually being observed. */
  1114. const BaseType_t xWriteValue = 0x55;
  1115. configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
  1116. } /*lint !e529 !e438 xWriteValue is only used if configASSERT() is defined. */
  1117. #endif
  1118. ( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) ); /*lint !e9087 memset() requires void *. */
  1119. pxStreamBuffer->pucBuffer = pucBuffer;
  1120. pxStreamBuffer->xLength = xBufferSizeBytes;
  1121. pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
  1122. pxStreamBuffer->ucFlags = ucFlags;
  1123. }
  1124. #if ( configUSE_TRACE_FACILITY == 1 )
  1125. UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
  1126. {
  1127. return xStreamBuffer->uxStreamBufferNumber;
  1128. }
  1129. #endif /* configUSE_TRACE_FACILITY */
  1130. /*-----------------------------------------------------------*/
  1131. #if ( configUSE_TRACE_FACILITY == 1 )
  1132. void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
  1133. UBaseType_t uxStreamBufferNumber )
  1134. {
  1135. xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
  1136. }
  1137. #endif /* configUSE_TRACE_FACILITY */
  1138. /*-----------------------------------------------------------*/
  1139. #if ( configUSE_TRACE_FACILITY == 1 )
  1140. uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
  1141. {
  1142. return( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
  1143. }
  1144. #endif /* configUSE_TRACE_FACILITY */
  1145. /*-----------------------------------------------------------*/