ringbuf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 Jim Mussared
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <string.h>
  27. #include "ringbuf.h"
  28. int ringbuf_get16(ringbuf_t *r) {
  29. int v = ringbuf_peek16(r);
  30. if (v == -1) {
  31. return v;
  32. }
  33. r->iget += 2;
  34. if (r->iget >= r->size) {
  35. r->iget -= r->size;
  36. }
  37. return v;
  38. }
  39. int ringbuf_peek16(ringbuf_t *r) {
  40. if (r->iget == r->iput) {
  41. return -1;
  42. }
  43. uint32_t iget_a = r->iget + 1;
  44. if (iget_a == r->size) {
  45. iget_a = 0;
  46. }
  47. if (iget_a == r->iput) {
  48. return -1;
  49. }
  50. return (r->buf[r->iget] << 8) | (r->buf[iget_a]);
  51. }
  52. int ringbuf_put16(ringbuf_t *r, uint16_t v) {
  53. uint32_t iput_a = r->iput + 1;
  54. if (iput_a == r->size) {
  55. iput_a = 0;
  56. }
  57. if (iput_a == r->iget) {
  58. return -1;
  59. }
  60. uint32_t iput_b = iput_a + 1;
  61. if (iput_b == r->size) {
  62. iput_b = 0;
  63. }
  64. if (iput_b == r->iget) {
  65. return -1;
  66. }
  67. r->buf[r->iput] = (v >> 8) & 0xff;
  68. r->buf[iput_a] = v & 0xff;
  69. r->iput = iput_b;
  70. return 0;
  71. }
  72. // Returns:
  73. // 0: Success
  74. // -1: Not enough data available to complete read (try again later)
  75. // -2: Requested read is larger than buffer - will never succeed
  76. int ringbuf_get_bytes(ringbuf_t *r, uint8_t *data, size_t data_len) {
  77. if (ringbuf_avail(r) < data_len) {
  78. return (r->size <= data_len) ? -2 : -1;
  79. }
  80. uint32_t iget = r->iget;
  81. uint32_t iget_a = (iget + data_len) % r->size;
  82. uint8_t *datap = data;
  83. if (iget_a < iget) {
  84. // Copy part of the data from the space left at the end of the buffer
  85. memcpy(datap, r->buf + iget, r->size - iget);
  86. datap += (r->size - iget);
  87. iget = 0;
  88. }
  89. memcpy(datap, r->buf + iget, iget_a - iget);
  90. r->iget = iget_a;
  91. return 0;
  92. }
  93. // Returns:
  94. // 0: Success
  95. // -1: Not enough free space available to complete write (try again later)
  96. // -2: Requested write is larger than buffer - will never succeed
  97. int ringbuf_put_bytes(ringbuf_t *r, const uint8_t *data, size_t data_len) {
  98. if (ringbuf_free(r) < data_len) {
  99. return (r->size <= data_len) ? -2 : -1;
  100. }
  101. uint32_t iput = r->iput;
  102. uint32_t iput_a = (iput + data_len) % r->size;
  103. const uint8_t *datap = data;
  104. if (iput_a < iput) {
  105. // Copy part of the data to the end of the buffer
  106. memcpy(r->buf + iput, datap, r->size - iput);
  107. datap += (r->size - iput);
  108. iput = 0;
  109. }
  110. memcpy(r->buf + iput, datap, iput_a - iput);
  111. r->iput = iput_a;
  112. return 0;
  113. }