stream_buffer.c 41 KB

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