SimpleStreamBuffer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file SimpleStreamBuffer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stddef.h>
  31. #include <misc/balloc.h>
  32. #include <misc/minmax.h>
  33. #include "SimpleStreamBuffer.h"
  34. static void try_output (SimpleStreamBuffer *o)
  35. {
  36. ASSERT(o->output_data_len > 0)
  37. // calculate number of bytes to output
  38. int bytes = bmin_int(o->output_data_len, o->buf_used);
  39. if (bytes == 0) {
  40. return;
  41. }
  42. // copy bytes to output
  43. memcpy(o->output_data, o->buf, bytes);
  44. // shift buffer
  45. memmove(o->buf, o->buf + bytes, o->buf_used - bytes);
  46. o->buf_used -= bytes;
  47. // forget data
  48. o->output_data_len = -1;
  49. // done
  50. StreamRecvInterface_Done(&o->output, bytes);
  51. }
  52. static void output_handler_recv (SimpleStreamBuffer *o, uint8_t *data, int data_len)
  53. {
  54. DebugObject_Access(&o->d_obj);
  55. ASSERT(o->output_data_len == -1)
  56. ASSERT(data)
  57. ASSERT(data_len > 0)
  58. // remember data
  59. o->output_data = data;
  60. o->output_data_len = data_len;
  61. try_output(o);
  62. }
  63. int SimpleStreamBuffer_Init (SimpleStreamBuffer *o, int buf_size, BPendingGroup *pg)
  64. {
  65. ASSERT(buf_size > 0)
  66. // init arguments
  67. o->buf_size = buf_size;
  68. // init output
  69. StreamRecvInterface_Init(&o->output, (StreamRecvInterface_handler_recv)output_handler_recv, o, pg);
  70. // allocate buffer
  71. if (!(o->buf = BAlloc(buf_size))) {
  72. goto fail1;
  73. }
  74. // init buffer state
  75. o->buf_used = 0;
  76. // set no output data
  77. o->output_data_len = -1;
  78. DebugObject_Init(&o->d_obj);
  79. return 1;
  80. fail1:
  81. StreamRecvInterface_Free(&o->output);
  82. return 0;
  83. }
  84. void SimpleStreamBuffer_Free (SimpleStreamBuffer *o)
  85. {
  86. DebugObject_Free(&o->d_obj);
  87. // free buffer
  88. BFree(o->buf);
  89. // free output
  90. StreamRecvInterface_Free(&o->output);
  91. }
  92. StreamRecvInterface * SimpleStreamBuffer_GetOutput (SimpleStreamBuffer *o)
  93. {
  94. DebugObject_Access(&o->d_obj);
  95. return &o->output;
  96. }
  97. int SimpleStreamBuffer_Write (SimpleStreamBuffer *o, uint8_t *data, int data_len)
  98. {
  99. DebugObject_Access(&o->d_obj);
  100. ASSERT(data_len >= 0)
  101. if (data_len > o->buf_size - o->buf_used) {
  102. return 0;
  103. }
  104. // copy to buffer
  105. memcpy(o->buf + o->buf_used, data, data_len);
  106. // update buffer state
  107. o->buf_used += data_len;
  108. // continue outputting
  109. if (o->output_data_len > 0) {
  110. try_output(o);
  111. }
  112. return 1;
  113. }