FreeRTOS_Stream_Buffer.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * FreeRTOS+TCP V2.3.2 LTS Patch 1
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file FreeRTOS_Stream_Buffer.c
  27. * @brief Provides the API for managing/creating the stream buffers in the FreeRTOS+TCP network stack.
  28. */
  29. /* Standard includes. */
  30. #include <stdint.h>
  31. /* FreeRTOS includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. #include "semphr.h"
  35. /* FreeRTOS+TCP includes. */
  36. #include "FreeRTOS_UDP_IP.h"
  37. #include "FreeRTOS_IP.h"
  38. #include "FreeRTOS_Sockets.h"
  39. #include "FreeRTOS_IP_Private.h"
  40. /**
  41. * @brief Adds data to a stream buffer.
  42. *
  43. * @param[in,out] pxBuffer: The buffer to which the bytes will be added.
  44. * @param[in] uxOffset: If uxOffset > 0, data will be written at an offset from uxHead
  45. * while uxHead will not be moved yet.
  46. * @param[in] pucData: A pointer to the data to be added.
  47. * @param[in] uxByteCount: The number of bytes to add.
  48. *
  49. * @return The number of bytes added to the buffer.
  50. */
  51. size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer,
  52. size_t uxOffset,
  53. const uint8_t * pucData,
  54. size_t uxByteCount )
  55. {
  56. size_t uxSpace, uxNextHead, uxFirst;
  57. size_t uxCount = uxByteCount;
  58. uxSpace = uxStreamBufferGetSpace( pxBuffer );
  59. /* If uxOffset > 0, items can be placed in front of uxHead */
  60. if( uxSpace > uxOffset )
  61. {
  62. uxSpace -= uxOffset;
  63. }
  64. else
  65. {
  66. uxSpace = 0U;
  67. }
  68. /* The number of bytes that can be written is the minimum of the number of
  69. * bytes requested and the number available. */
  70. uxCount = FreeRTOS_min_uint32( uxSpace, uxCount );
  71. if( uxCount != 0U )
  72. {
  73. uxNextHead = pxBuffer->uxHead;
  74. if( uxOffset != 0U )
  75. {
  76. /* ( uxOffset > 0 ) means: write in front if the uxHead marker */
  77. uxNextHead += uxOffset;
  78. if( uxNextHead >= pxBuffer->LENGTH )
  79. {
  80. uxNextHead -= pxBuffer->LENGTH;
  81. }
  82. }
  83. if( pucData != NULL )
  84. {
  85. /* Calculate the number of bytes that can be added in the first
  86. * write - which may be less than the total number of bytes that need
  87. * to be added if the buffer will wrap back to the beginning. */
  88. uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextHead, uxCount );
  89. /* Write as many bytes as can be written in the first write. */
  90. ( void ) memcpy( &( pxBuffer->ucArray[ uxNextHead ] ), pucData, uxFirst );
  91. /* If the number of bytes written was less than the number that
  92. * could be written in the first write... */
  93. if( uxCount > uxFirst )
  94. {
  95. /* ...then write the remaining bytes to the start of the
  96. * buffer. */
  97. ( void ) memcpy( pxBuffer->ucArray, &( pucData[ uxFirst ] ), uxCount - uxFirst );
  98. }
  99. }
  100. if( uxOffset == 0U )
  101. {
  102. /* ( uxOffset == 0 ) means: write at uxHead position */
  103. uxNextHead += uxCount;
  104. if( uxNextHead >= pxBuffer->LENGTH )
  105. {
  106. uxNextHead -= pxBuffer->LENGTH;
  107. }
  108. pxBuffer->uxHead = uxNextHead;
  109. }
  110. if( xStreamBufferLessThenEqual( pxBuffer, pxBuffer->uxFront, uxNextHead ) != pdFALSE )
  111. {
  112. /* Advance the front pointer */
  113. pxBuffer->uxFront = uxNextHead;
  114. }
  115. }
  116. return uxCount;
  117. }
  118. /*-----------------------------------------------------------*/
  119. /**
  120. * @brief Read bytes from stream buffer.
  121. *
  122. * @param[in] pxBuffer: The buffer from which the bytes will be read.
  123. * @param[in] uxOffset: can be used to read data located at a certain offset from 'lTail'.
  124. * @param[in,out] pucData: If 'pucData' equals NULL, the function is called to advance 'lTail' only.
  125. * @param[in] uxMaxCount: The number of bytes to read.
  126. * @param[in] xPeek: if 'xPeek' is pdTRUE, or if 'uxOffset' is non-zero, the 'lTail' pointer will
  127. * not be advanced.
  128. *
  129. * @return The count of the bytes read.
  130. */
  131. size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer,
  132. size_t uxOffset,
  133. uint8_t * pucData,
  134. size_t uxMaxCount,
  135. BaseType_t xPeek )
  136. {
  137. size_t uxSize, uxCount, uxFirst, uxNextTail;
  138. /* How much data is available? */
  139. uxSize = uxStreamBufferGetSize( pxBuffer );
  140. if( uxSize > uxOffset )
  141. {
  142. uxSize -= uxOffset;
  143. }
  144. else
  145. {
  146. uxSize = 0U;
  147. }
  148. /* Use the minimum of the wanted bytes and the available bytes. */
  149. uxCount = FreeRTOS_min_uint32( uxSize, uxMaxCount );
  150. if( uxCount > 0U )
  151. {
  152. uxNextTail = pxBuffer->uxTail;
  153. if( uxOffset != 0U )
  154. {
  155. uxNextTail += uxOffset;
  156. if( uxNextTail >= pxBuffer->LENGTH )
  157. {
  158. uxNextTail -= pxBuffer->LENGTH;
  159. }
  160. }
  161. if( pucData != NULL )
  162. {
  163. /* Calculate the number of bytes that can be read - which may be
  164. * less than the number wanted if the data wraps around to the start of
  165. * the buffer. */
  166. uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextTail, uxCount );
  167. /* Obtain the number of bytes it is possible to obtain in the first
  168. * read. */
  169. ( void ) memcpy( pucData, &( pxBuffer->ucArray[ uxNextTail ] ), uxFirst );
  170. /* If the total number of wanted bytes is greater than the number
  171. * that could be read in the first read... */
  172. if( uxCount > uxFirst )
  173. {
  174. /*...then read the remaining bytes from the start of the buffer. */
  175. ( void ) memcpy( &( pucData[ uxFirst ] ), pxBuffer->ucArray, uxCount - uxFirst );
  176. }
  177. }
  178. if( ( xPeek == pdFALSE ) && ( uxOffset == 0UL ) )
  179. {
  180. /* Move the tail pointer to effectively remove the data read from
  181. * the buffer. */
  182. uxNextTail += uxCount;
  183. if( uxNextTail >= pxBuffer->LENGTH )
  184. {
  185. uxNextTail -= pxBuffer->LENGTH;
  186. }
  187. pxBuffer->uxTail = uxNextTail;
  188. }
  189. }
  190. return uxCount;
  191. }