stream_buffer.c 42 KB

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