osi_pipe.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_PIPE_H_
  13. #define _OSI_PIPE_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * \brief opaque data structure for pipe
  22. */
  23. typedef struct osiPipe osiPipe_t;
  24. /**
  25. * \brief pipe event type
  26. */
  27. typedef enum
  28. {
  29. OSI_PIPE_EVENT_RX_ARRIVED = (1 << 0), ///< received new data
  30. OSI_PIPE_EVENT_TX_COMPLETE = (1 << 2), ///< all data had been sent
  31. } osiPipeEvent_t;
  32. /**
  33. * \brief pipe callback function prototype
  34. *
  35. * Reader callback will be invoked in writer thread, and writer
  36. * callback will be invoked in reader thread.
  37. */
  38. typedef void (*osiPipeEventCallback_t)(void *param, unsigned event);
  39. /**
  40. * \brief create a pipe
  41. *
  42. * Pipe buffer will be dynamic allocated.
  43. *
  44. * \param size pipe buffer size, can't be 0
  45. * \return
  46. * - the created pipe
  47. * - NULL if parameter is invalid, or out of memory
  48. */
  49. osiPipe_t *osiPipeCreate(unsigned size);
  50. /**
  51. * \brief delete a pipe
  52. *
  53. * \param pipe the pipe, must be valid
  54. */
  55. void osiPipeDelete(osiPipe_t *pipe);
  56. /**
  57. * \brief reset the pipe
  58. *
  59. * After reset, the pipe is empty, and running.
  60. *
  61. * \param pipe the pipe, must be valid
  62. */
  63. void osiPipeReset(osiPipe_t *pipe);
  64. /**
  65. * \brief set writer callback
  66. *
  67. * \param pipe the pipe, must be valid
  68. * \param mask event mask, only masked event will trigger callback
  69. * \param cb writer callback
  70. * \param ctx writer callback context
  71. */
  72. void osiPipeSetWriterCallback(osiPipe_t *pipe, unsigned mask, osiPipeEventCallback_t cb, void *ctx);
  73. /**
  74. * \brief set reader callback
  75. *
  76. * \param pipe the pipe, must be valid
  77. * \param mask event mask, only masked event will trigger callback
  78. * \param cb reader callback
  79. * \param ctx reader callback context
  80. */
  81. void osiPipeSetReaderCallback(osiPipe_t *pipe, unsigned mask, osiPipeEventCallback_t cb, void *ctx);
  82. /**
  83. * \brief stop the pipe
  84. *
  85. * After stop, both read and write will return -1.
  86. *
  87. * \param pipe the pipe, must be valid
  88. */
  89. void osiPipeStop(osiPipe_t *pipe);
  90. /**
  91. * \brief whether the pipe is stopped
  92. *
  93. * \param pipe the pipe, must be valid
  94. * \return
  95. * - true if the pipe is stopped
  96. * - true if the pipe is running
  97. */
  98. bool osiPipeIsStopped(osiPipe_t *pipe);
  99. /**
  100. * \brief set eof for the pipe
  101. *
  102. * This should be called by writer, to indicate the end of pipe write.
  103. *
  104. * \param pipe the pipe, must be valid
  105. */
  106. void osiPipeSetEof(osiPipe_t *pipe);
  107. /**
  108. * \brief whether the pipe write is endded
  109. *
  110. * This should be called by reader.
  111. *
  112. * \param pipe the pipe, must be valid
  113. * \return
  114. * - true if eof flag is set by writer
  115. * - false if not
  116. */
  117. bool osiPipeIsEof(osiPipe_t *pipe);
  118. /**
  119. * \brief read data from pipe
  120. *
  121. * When data available in pipe is less than \p size, the return value will
  122. * be less than \p size.
  123. *
  124. * \param pipe the pipe, must be valid
  125. * \param buf buffer to store data
  126. * \param size buffer size
  127. * \return
  128. * - the number of bytes actually read from pipe
  129. * - -1 on invalid parameter
  130. */
  131. int osiPipeRead(osiPipe_t *pipe, void *buf, unsigned size);
  132. /**
  133. * \brief write data to pipe
  134. *
  135. * When available space in pipe is less than \p size, the return value will
  136. * be less than \p size.
  137. *
  138. * \param pipe the pipe, must be valid
  139. * \param buf buffer to be sent
  140. * \param size buffer size
  141. * \return
  142. * - the number of bytes actually written to pipe
  143. * - -1 on invalid parameter
  144. */
  145. int osiPipeWrite(osiPipe_t *pipe, const void *buf, unsigned size);
  146. /**
  147. * \brief read data from pipe, and wait with timeout
  148. *
  149. * When data available in pipe is less than \p size, it will be blocked to
  150. * wait data available.
  151. *
  152. * \param pipe the pipe, must be valid
  153. * \param buf buffer to store data
  154. * \param size buffer size
  155. * \param timeout wait timeout in milliseconds
  156. * \return
  157. * - the number of bytes actually read from pipe
  158. * - -1 on invalid parameter
  159. */
  160. int osiPipeReadAll(osiPipe_t *pipe, void *buf, unsigned size, unsigned timeout);
  161. /**
  162. * \brief write data to pipe, and wait with timeout
  163. *
  164. * When available space in pipe is less than \p size, it will be blocked to
  165. * wait available space.
  166. *
  167. * \param pipe the pipe, must be valid
  168. * \param buf buffer to be sent
  169. * \param size buffer size
  170. * \param timeout wait timeout in milliseconds
  171. * \return
  172. * - the number of bytes actually written to pipe
  173. * - -1 on invalid parameter
  174. */
  175. int osiPipeWriteAll(osiPipe_t *pipe, const void *buf, unsigned size, unsigned timeout);
  176. /**
  177. * \brief data available in pipe for read
  178. *
  179. * \param pipe the pipe, must be valid
  180. * \return
  181. * - the number of bytes available for read
  182. * - -1 on invalid parameter
  183. */
  184. int osiPipeReadAvail(osiPipe_t *pipe);
  185. /**
  186. * \brief space available in pipe for write
  187. *
  188. * \param pipe the pipe, must be valid
  189. * \return
  190. * - the number of bytes available for write
  191. * - -1 on invalid parameter
  192. */
  193. int osiPipeWriteAvail(osiPipe_t *pipe);
  194. /**
  195. * \brief wait available bytes for read
  196. *
  197. * \param pipe the pipe, must be valid
  198. * \param timeout wait timeout
  199. * \return
  200. * - true if there are available bytes for read
  201. * - false on timeout
  202. */
  203. bool osiPipeWaitReadAvail(osiPipe_t *pipe, unsigned timeout);
  204. /**
  205. * \brief wait available space for write
  206. *
  207. * \param pipe the pipe, must be valid
  208. * \param timeout wait timeout
  209. * \return
  210. * - true if there are available space for write
  211. * - false on timeout
  212. */
  213. bool osiPipeWaitWriteAvail(osiPipe_t *pipe, unsigned timeout);
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif