osi_fifo.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_FIFO_H_
  13. #define _OSI_FIFO_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * \brief OSI FIFO data structure
  22. *
  23. * Don't access the field members directly. Rather FIFO APIs should be used.
  24. */
  25. typedef struct osiFifo
  26. {
  27. void *data; ///< FIFO buffer
  28. size_t size; ///< FIFO buffer size
  29. size_t rd; ///< FIFO read pointer
  30. size_t wr; ///< FIFO write pointer
  31. } osiFifo_t;
  32. /**
  33. * \brief initialize FIFO
  34. *
  35. * \param fifo the FIFO pointer
  36. * \param data FIFO buffer
  37. * \param size FIFO buffer size
  38. * \return
  39. * - true on success
  40. * - false on invalid parameter
  41. */
  42. bool osiFifoInit(osiFifo_t *fifo, void *data, size_t size);
  43. /**
  44. * \brief reset FIFO
  45. *
  46. * After reset, the internal state indicates there are no data in the
  47. * FIFO.
  48. *
  49. * \param fifo the FIFO pointer
  50. */
  51. void osiFifoReset(osiFifo_t *fifo);
  52. /**
  53. * \brief put data into FIFO
  54. *
  55. * The returned actual put size may be less than \a size.
  56. *
  57. * \param fifo the FIFO pointer
  58. * \param data data to be put into FIFO
  59. * \param size data size
  60. * \return actually put size
  61. */
  62. int osiFifoPut(osiFifo_t *fifo, const void *data, size_t size);
  63. /**
  64. * \brief get data from FIFO
  65. *
  66. * The returned actual get size may be less than \a size.
  67. *
  68. * \param fifo the FIFO pointer
  69. * \param data data buffer for get
  70. * \param size data buffer size
  71. * \return actually get size
  72. */
  73. int osiFifoGet(osiFifo_t *fifo, void *data, size_t size);
  74. /**
  75. * \brief peek data from FIFO
  76. *
  77. * On peek, the read position of FIFO won't be updated.
  78. *
  79. * \param fifo the FIFO pointer
  80. * \param data data buffer for peek
  81. * \param size data buffer size
  82. * \return actually peek size
  83. */
  84. int osiFifoPeek(osiFifo_t *fifo, void *data, size_t size);
  85. /**
  86. * \brief update read position to skip bytes in FIFO
  87. *
  88. * When \a size is larger than byte count in FIFO, only available bytes will
  89. * be skipped.
  90. *
  91. * \param fifo the FIFO pointer
  92. * \param size byte count to be skipped
  93. */
  94. void osiFifoSkipBytes(osiFifo_t *fifo, size_t size);
  95. /**
  96. * \brief search a byte in FIFO
  97. *
  98. * At search, the non-matching bytes will be dropped from the FIFO.
  99. * When \a byte is found, it is configurable to keep the byte or
  100. * drop the byte.
  101. *
  102. * \param fifo the FIFO pointer
  103. * \param byte the byte to be searched
  104. * \param keep true to keep the found byte, false to drop the found byte
  105. * \return
  106. * - true if \a byte is found
  107. * - false if \a byte is not found
  108. */
  109. bool osiFifoSearch(osiFifo_t *fifo, uint8_t byte, bool keep);
  110. /**
  111. * \brief byte count in the FIFO
  112. *
  113. * \param fifo the FIFO pointer
  114. * \return the byte count of the FIFO
  115. */
  116. static inline size_t osiFifoBytes(osiFifo_t *fifo) { return fifo->wr - fifo->rd; }
  117. /**
  118. * \brief available space in the FIFO
  119. *
  120. * \param fifo the FIFO pointer
  121. * \return the available space byte count of the FIFO
  122. */
  123. static inline size_t osiFifoSpace(osiFifo_t *fifo) { return fifo->size - osiFifoBytes(fifo); }
  124. /**
  125. * \brief chech whether the FIFO is full
  126. *
  127. * \param fifo the FIFO pointer
  128. * \return
  129. * - true if the FIFO is full
  130. * - false if the FIFO is not full
  131. */
  132. static inline bool osiFifoIsFull(osiFifo_t *fifo) { return osiFifoSpace(fifo) == 0; }
  133. /**
  134. * \brief chech whether the FIFO is empty
  135. *
  136. * \param fifo the FIFO pointer
  137. * \return
  138. * - true if the FIFO is empty
  139. * - false if the FIFO is not empty
  140. */
  141. static inline bool osiFifoIsEmpty(osiFifo_t *fifo) { return osiFifoBytes(fifo) == 0; }
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif