link_ring_buffer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2019-04-29 10:34 zhangqianfu The first version
  37. *
  38. */
  39. #include <stdint.h>
  40. #include <stddef.h>
  41. #include <string.h>
  42. #include <link_misc.h>
  43. int ring_buffer_init(tag_ring_buffer_t *ring,unsigned char *buf, int buflen,int offset,int datalen)
  44. {
  45. int ret = -1;
  46. if((NULL == ring))
  47. {
  48. return ret;
  49. }
  50. ring->buf = buf;
  51. ring->buflen = buflen;
  52. ring->datalen = datalen;
  53. ring->dataoff = offset;
  54. ret = 0;
  55. return ret;
  56. }
  57. int ring_buffer_write(tag_ring_buffer_t *ring,unsigned char *buf, int len)
  58. {
  59. int ret = -1;
  60. int cpylen; //the current time we should move
  61. int lenleft; //and how many data still left to move
  62. int offset;
  63. unsigned char *src;
  64. unsigned char *dst;
  65. if((NULL == ring)||(NULL == buf)||(0 == len))
  66. {
  67. return ret;//which means parameters error
  68. }
  69. if((ring->datalen == ring->buflen)|| (ring->buflen <= 0))
  70. {
  71. ret = 0;
  72. return ret;//which means you could copy nothing here
  73. }
  74. ret = len > (ring->buflen-ring->datalen)?(ring->buflen-ring->datalen):len;
  75. //now let us think the method to fill the data,take care of the roll back
  76. lenleft = ret;
  77. src = buf;
  78. if((ring->dataoff+ring->datalen)>ring->buflen) //which means the data has roll back
  79. {
  80. offset = (ring->dataoff+ring->datalen)%ring->buflen; //we could move it one time
  81. cpylen = lenleft;
  82. dst = ring->buf + offset;
  83. if(cpylen > 0)
  84. {
  85. (void) memcpy(dst,src,cpylen);
  86. ring->datalen += cpylen;
  87. lenleft -= cpylen;
  88. }
  89. }
  90. else if((ring->dataoff+ring->datalen + lenleft)>ring->buflen) //which means the data will be roll back
  91. {
  92. //which means roll back,we should copy some here to the tail
  93. offset = ring->dataoff + ring->datalen;
  94. cpylen = ring->buflen - offset;
  95. dst = ring->buf + offset;
  96. (void) memcpy(dst,src,cpylen);
  97. src += cpylen;
  98. ring->datalen += cpylen;
  99. lenleft -= cpylen;
  100. }
  101. //here means we could move it by one time
  102. if(lenleft > 0)
  103. {
  104. offset = (ring->dataoff+ring->datalen)%ring->buflen; //we could move it one time
  105. cpylen = lenleft;
  106. dst = ring->buf + offset;
  107. (void) memcpy(dst,src,cpylen);
  108. ring->datalen += cpylen;
  109. }
  110. return ret;
  111. }
  112. int ring_buffer_read(tag_ring_buffer_t *ring,unsigned char *buf, int len)
  113. {
  114. int ret = -1;
  115. int cpylen; //the current time we should move
  116. int lenleft; //and how many data still left to move
  117. int offset;
  118. unsigned char *src;
  119. unsigned char *dst;
  120. if((NULL == ring)||(NULL == buf)||(0 == len))
  121. {
  122. return ret;//which means parameters error
  123. }
  124. if((ring->datalen == 0) || (ring->buflen <= 0))
  125. {
  126. ret = 0;
  127. return ret;//which means you could copy nothing here
  128. }
  129. ret = len > ring->datalen?ring->datalen:len;
  130. //now let us think the method to fill the data,take care of the roll back
  131. lenleft = ret;
  132. dst = buf;
  133. if(ring->dataoff >= (ring->buflen - lenleft)) //which means the data has roll back
  134. {
  135. offset =ring->dataoff; //we cpy part
  136. cpylen = ring->buflen - ring->dataoff;
  137. src = ring->buf + offset;
  138. if(cpylen > 0)
  139. {
  140. (void) memcpy(dst,src,cpylen);
  141. ring->dataoff = (ring->dataoff + cpylen)%ring->buflen;
  142. ring->datalen -= cpylen;
  143. lenleft -= cpylen;
  144. dst += cpylen;
  145. }
  146. }
  147. //here means we could move it by one time
  148. if(lenleft > 0)
  149. {
  150. offset =ring->dataoff; //we cpy part
  151. cpylen = lenleft;
  152. src = ring->buf + offset;
  153. (void) memcpy(dst,src,cpylen);
  154. ring->dataoff = ring->dataoff + cpylen;
  155. ring->datalen -= cpylen;
  156. }
  157. return ret;
  158. }
  159. int ring_buffer_datalen(tag_ring_buffer_t *ring)
  160. {
  161. int ret = -1;
  162. if(NULL != ring)
  163. {
  164. ret = ring->datalen;
  165. }
  166. return ret;
  167. }
  168. int ring_buffer_freespace(tag_ring_buffer_t *ring)
  169. {
  170. int ret = -1;
  171. if(NULL != ring)
  172. {
  173. ret = ring->buflen-ring->datalen;
  174. }
  175. return ret;
  176. }
  177. int ring_buffer_reset(tag_ring_buffer_t *ring)
  178. {
  179. int ret = -1;
  180. if(NULL != ring)
  181. {
  182. ring->datalen = 0;
  183. ring->dataoff = 0;
  184. ret = 0;
  185. }
  186. return ret;
  187. }
  188. int ring_buffer_deinit(tag_ring_buffer_t *ring)
  189. {
  190. int ret = -1;
  191. if(NULL != ring)
  192. {
  193. (void) memset(ring,0,sizeof(tag_ring_buffer_t));
  194. ret = 0;
  195. }
  196. return ret;
  197. }