stream.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2018 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file stream interface
  8. */
  9. #ifndef __IOSTREAM_H__
  10. #define __IOSTREAM_H__
  11. #include <os_common_api.h>
  12. #include <sys/util.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. struct __stream;
  17. typedef struct __stream *io_stream_t;
  18. /** stream notify type */
  19. typedef enum {
  20. /** stream notify read */
  21. STREAM_NOTIFY_READ = BIT(0),
  22. /** stream notify write */
  23. STREAM_NOTIFY_WRITE = BIT(1),
  24. /** stream notify seek */
  25. STREAM_NOTIFY_SEEK = BIT(2),
  26. /** stream notify flush */
  27. STREAM_NOTIFY_FLUSH = BIT(3),
  28. /** stream notify pre write */
  29. STREAM_NOTIFY_PRE_WRITE = BIT(4),
  30. } stream_notify_type;
  31. typedef void (*stream_observer_notify)(void *observer, int readoff, int writeoff, int total_size,
  32. unsigned char *buf, int num, stream_notify_type type);
  33. /**
  34. * @defgroup stream_apis Stream APIs
  35. * @ingroup system_apis
  36. * @{
  37. */
  38. /** seek mode */
  39. typedef enum {
  40. /** seek from file begin*/
  41. SEEK_DIR_BEG,
  42. /** seek from cur position*/
  43. SEEK_DIR_CUR,
  44. /** seek from file end*/
  45. SEEK_DIR_END,
  46. } seek_dir;
  47. /** tell mode */
  48. typedef enum {
  49. /** tell current off*/
  50. TELL_CUR,
  51. /** tell end of*/
  52. TELL_END,
  53. } tell_mode;
  54. /** stream read or write mode */
  55. typedef enum {
  56. /** read only stream */
  57. MODE_IN = 0x01,
  58. /** write only stream */
  59. MODE_OUT = 0x02,
  60. /** read & write stream */
  61. MODE_IN_OUT = 0x03,
  62. /** read stream block*/
  63. MODE_READ_BLOCK = 0x04,
  64. /** write stream block*/
  65. MODE_WRITE_BLOCK = 0x08,
  66. /**
  67. * read/write stream block timeout , 1s timeout,
  68. * if not set this bit ,timeout for forever
  69. */
  70. MODE_BLOCK_TIMEOUT = 0x10,
  71. } stream_mode;
  72. /** stream state */
  73. typedef enum {
  74. /** stream state init*/
  75. STATE_INIT = 1,
  76. /** stream state open */
  77. STATE_OPEN ,
  78. /** stream state close */
  79. STATE_CLOSE ,
  80. } stream_state;
  81. /** structure of stream*/
  82. typedef struct {
  83. /** stream open operation Function pointer*/
  84. int (*init)(io_stream_t handle,void *param);
  85. /** stream open operation Function pointer*/
  86. int (*open)(io_stream_t handle, stream_mode mode);
  87. /** stream read operation Function pointer*/
  88. int (*read)(io_stream_t handle, unsigned char *buf, int num);
  89. /** stream seek operation Function pointer*/
  90. int (*seek)(io_stream_t handle, int offset,seek_dir origin);
  91. /** stream tell operation Function pointer*/
  92. int (*tell)(io_stream_t handle);
  93. /** stream get_length operation Function pointer*/
  94. int (*get_length)(io_stream_t handle);
  95. /** stream get space operation Function pointer*/
  96. int (*get_space)(io_stream_t handle);
  97. /** stream write operation Function pointer*/
  98. int (*write)(io_stream_t handle, unsigned char *buf, int num);
  99. /** stream flush operation Function pointer*/
  100. int (*flush)(io_stream_t handle);
  101. /** stream close operation Function pointer*/
  102. int (*close)(io_stream_t handle);
  103. /** stream destroy operation Function pointer*/
  104. int (*destroy)(io_stream_t handle);
  105. void *(*get_ringbuffer)(io_stream_t handle);
  106. } stream_ops_t;
  107. /**
  108. * @brief create stream , return stream handle
  109. *
  110. * This routine provides create stream ,and return stream handle.
  111. * and stream state is STATE_INIT
  112. *
  113. * @param ops sub stream operations
  114. * @param init_param create stream init param
  115. *
  116. * @return stream handle if create stream success
  117. * @return NULL if create stream failed
  118. */
  119. io_stream_t stream_create(const stream_ops_t *ops, void *init_param);
  120. /**
  121. * @brief open stream
  122. *
  123. * This routine provides open stream, stream state must be STATE_INIT
  124. * and after this routine stream state change to STATE_OPEN
  125. * stream read and write operation can be dong after stream open
  126. *
  127. * @param handle handle of stream
  128. * @param mode mode of stream
  129. *
  130. * @return 0 stream open success
  131. * @return != 0 stream open failed
  132. */
  133. int stream_open(io_stream_t handle, stream_mode mode);
  134. /**
  135. * @brief read from stream
  136. *
  137. * This routine provides read num of bytes from stream, if stream states is
  138. * not STATE_OPEN , return err , else return the realy read data len to user
  139. *
  140. * @param handle handle of stream
  141. * @param buf out put data buffer pointer
  142. * @param num bytes user want to read
  143. *
  144. * @return >=0 the realy read data length ,if return < num, means read finished.
  145. * @return <0 stream read failed
  146. */
  147. int stream_read(io_stream_t handle, void *buf, int num);
  148. /**
  149. * @brief write to stream
  150. *
  151. * This routine provides write num of bytes to stream, if stream states is
  152. * not STATE_OPEN , return err , else return the realy write data len to user
  153. *
  154. *
  155. * @param handle handle of stream
  156. * @param buf data poninter of write
  157. * @param num bytes user want to write
  158. *
  159. * @return >=0 the realy write data length ,if return < num, means write finished.
  160. * @return <0 stream write failed
  161. */
  162. int stream_write(io_stream_t handle, const void *buf, int num);
  163. /**
  164. * @brief seek stream
  165. *
  166. * This routine provides seek stream .
  167. *
  168. *
  169. * @param handle handle of stream
  170. * @param offset offset want to seek
  171. * @param origin seek mode , net stream only support SEEK_DIR_CUR
  172. *
  173. * @return 0 seek success
  174. * @return !=0 seek failed
  175. */
  176. int stream_seek(io_stream_t handle, int offset,seek_dir origin);
  177. /**
  178. * @brief tell stream
  179. *
  180. * This routine provides return stream len , this interface only support
  181. * for file stream ,return the file len.
  182. *
  183. * @param handle handle of stream
  184. *
  185. * @return len return the stream len
  186. */
  187. int stream_tell(io_stream_t handle);
  188. /**
  189. * @brief check stream finished
  190. *
  191. * This routine provides check the stream finined
  192. *
  193. * @param handle handle of stream
  194. *
  195. * @return true finished else no finished
  196. */
  197. bool stream_check_finished(io_stream_t handle);
  198. /**
  199. * @brief flush stream
  200. *
  201. * This routine provides flush stream , change state to STATE_CLOSE
  202. * read and write seek tell operation not allowed after this routine
  203. * execute.
  204. *
  205. * @param handle handle of stream
  206. *
  207. * @return 0 close success
  208. * @return !=0 close failed
  209. */
  210. int stream_flush(io_stream_t handle);
  211. /**
  212. * @brief close stream
  213. *
  214. * This routine provides close stream , change state to STATE_CLOSE
  215. * read and write seek tell operation not allowed after this routine
  216. * execute.
  217. *
  218. * @param handle handle of stream
  219. *
  220. * @return 0 close success
  221. * @return !=0 close failed
  222. */
  223. int stream_close(io_stream_t handle);
  224. /**
  225. * @brief destroy stream
  226. *
  227. * This routine provides destroy stream , free all stream resurce
  228. * not allowed access stream handle after this routine execute.
  229. *
  230. * @param handle handle of stream
  231. *
  232. * @return 0 destroy success
  233. * @return !=0 destroy failed
  234. */
  235. int stream_destroy(io_stream_t handle);
  236. /**
  237. * @brief get stream data length
  238. *
  239. * This routine provides get stream length.
  240. *
  241. * @param handle handle of stream
  242. *
  243. * @return -1 get failed
  244. * @return size of cache data length
  245. */
  246. int stream_get_length(io_stream_t handle);
  247. /**
  248. * @brief get stream base ringbuffer
  249. *
  250. * This routine provides get stream ringbuffer datastructer
  251. *
  252. * @param handle handle of stream
  253. *
  254. * @return ringbuffer base address
  255. */
  256. void *stream_get_ringbuffer(io_stream_t handle);
  257. /**
  258. * @brief get stream free space
  259. *
  260. * This routine provides get stream free space. only support by in_out_mode
  261. * stream
  262. *
  263. * @param handle handle of stream
  264. *
  265. * @return -1 get failed
  266. * @return size of free space in stream
  267. */
  268. int stream_get_space(io_stream_t handle);
  269. /**
  270. * @brief stream attach
  271. *
  272. * This routine provides attach dest stream to origin stream, data will be read/write to dest stream
  273. * when data write to origin stream,
  274. *
  275. * @param origin handle of origin stream
  276. * @param attach_stream handle of dest attach stream
  277. * @param attach_type MODE_IN, MODE_OUT
  278. *
  279. * @return 0 attach success
  280. * @return !=0 attach failed
  281. */
  282. int stream_attach(io_stream_t origin, io_stream_t attach_stream, int attach_type);
  283. /**
  284. * @brief stream detach
  285. *
  286. * This routine provides detach dest stream from origin stream
  287. *
  288. * @param origin handle of origin stream
  289. * @param attach_stream handle of dest attach stream
  290. *
  291. * @return 0 detach success
  292. * @return !=0 detach failed
  293. */
  294. int stream_detach(io_stream_t origin, io_stream_t attach_stream);
  295. /**
  296. * @brief stream dump information
  297. *
  298. * This routine provides dumping stream information
  299. *
  300. * @param stream handle of stream
  301. * @param name dump name of stream
  302. * @param line_prefix Prefix of each line.
  303. *
  304. * @return N/A
  305. */
  306. void stream_dump(io_stream_t stream, const char *name, const char *line_prefix);
  307. /**
  308. * @cond INTERNAL_HIDDEN
  309. */
  310. /** structure of stream*/
  311. struct __stream {
  312. /** stream type */
  313. uint8_t type;
  314. /** stream mode */
  315. uint8_t mode;
  316. /** stream state */
  317. uint8_t state;
  318. /** stream format for play*/
  319. uint8_t format;
  320. /** read off set of stream*/
  321. uint32_t rofs;
  322. /** write off set of stream*/
  323. uint32_t wofs;
  324. /** state of stream is wroking*/
  325. uint32_t isworking:1;
  326. /** flag of stream write finished*/
  327. uint32_t write_finished:1;
  328. /** flag of stream write must wait for free space*/
  329. uint32_t write_pending:1;
  330. /** flag of stream write must wait for free space*/
  331. uint32_t cache_size_changed:1;
  332. /** file cache size*/
  333. uint32_t cache_size;
  334. /** total size*/
  335. uint32_t total_size;
  336. os_sem *sync_sem;
  337. void *observer[2];
  338. uint8_t observer_type[2];
  339. stream_observer_notify observer_notify[2];
  340. /* attach direction (MODE_IN, MODE_OUT) : origin stream data read or write */
  341. uint8_t attach_mode[1];
  342. /* stream array that will be attach to */
  343. io_stream_t attach_stream[1];
  344. /* stream is attache */
  345. io_stream_t attached_stream;
  346. /* attach lock */
  347. os_mutex attach_lock;
  348. const stream_ops_t *ops;
  349. void *data;
  350. };
  351. /**
  352. * @brief set stream cache size
  353. *
  354. * This routine provides set stream cache size. only support by in_out_mode
  355. * stream
  356. *
  357. * @param handle handle of stream
  358. * @param size cache size of stream
  359. *
  360. * @return 0 set success
  361. * @return !=0 set failed
  362. */
  363. int stream_set_cache_size(io_stream_t handle, int size);
  364. /**
  365. * @brief add stream observer
  366. *
  367. * This routine provides add stream observer. if stream data changed , stream observer must be
  368. * notified by stream
  369. *
  370. * @param handle handle of stream
  371. * @param observer handle of observer
  372. * @param notify notify function
  373. * @param type notify type
  374. *
  375. * @return 0 set success
  376. * @return !=0 set failed
  377. */
  378. int stream_set_observer(io_stream_t handle, void *observer, stream_observer_notify notify, uint8_t type);
  379. /**
  380. * INTERNAL_HIDDEN @endcond
  381. */
  382. /**
  383. * @} end defgroup stream_apis
  384. */
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif /* __IOSTREAM_H__ */