stream_buffer.c 41 KB

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