loop_fstream.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file file stream interface
  8. */
  9. #define SYS_LOG_DOMAIN "loopstream"
  10. #include <mem_manager.h>
  11. #include <fs_manager.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <assert.h>
  16. #include "loop_fstream.h"
  17. #include "stream_internal.h"
  18. #define ENERGY_FILTER_TIME (500) //uint ms
  19. typedef struct {
  20. /** hanlde of file fp*/
  21. fs_file_t fp;
  22. /** mutex used for sync*/
  23. os_mutex lock;
  24. } loop_fstream_info_t;
  25. /* loop file info structure */
  26. struct loop_file_info {
  27. uint32_t flen; //actual file length
  28. uint32_t flen_head; //length of file head(tag id3v2)
  29. uint32_t cur_rofs; //a virtual value to store current offset
  30. uint32_t flen_energy_clac; //the data that need calc energy
  31. uint32_t flen_notail; //file length without file tail(tag id3v1 and apev2)
  32. uint32_t flen_data; //file length without file head and tail
  33. uint32_t flen_remain; //the rest music data for next decode(a fake value)
  34. loopfs_energy_cb energy_filt_enable; //callback func to enable energy filter
  35. };
  36. static struct loop_file_info lp;
  37. /*eg. file_name: bycluster:SD:LOOP/cluster:2/320/12992420*/
  38. static bool get_cluster_by_name(char *file_name, char **dir, uint32_t *clust, uint32_t *blk_ofs)
  39. {
  40. char *str = NULL;
  41. char *cluster = NULL;
  42. char *blk = NULL;
  43. bool res = false;
  44. char *temp_name = mem_malloc(strlen(file_name) + 1);
  45. if (!temp_name)
  46. goto exit;
  47. strcpy(temp_name, file_name);
  48. str = strstr(temp_name, "bycluster:");
  49. if (!str)
  50. goto exit;
  51. str += strlen("bycluster:");
  52. *dir = (void *)str;
  53. /*for dir is /SD:*/
  54. if (*str == '/')
  55. str += 1;
  56. str = strchr(str, '/');
  57. if (!str)
  58. goto exit;
  59. str[0] = 0;
  60. str++;
  61. str = strstr(str, "cluster:");
  62. if (!str)
  63. goto exit;
  64. str += strlen("cluster:");
  65. cluster = str;
  66. str = strchr(str, '/');
  67. if (!str)
  68. goto exit;
  69. str[0] = 0;
  70. str++;
  71. blk = str;
  72. str = strchr(str, '/');
  73. if (!str)
  74. goto exit;
  75. str[0] = 0;
  76. *clust = atoi(cluster);
  77. *blk_ofs = atoi(blk);
  78. res = true;
  79. exit:
  80. if (temp_name)
  81. mem_free(temp_name);
  82. return res;
  83. }
  84. static void loop_fstream_clac_info(uint32_t head, uint32_t tail)
  85. {
  86. /* flen and flen_remain have be initialized */
  87. lp.flen_head = head;
  88. lp.flen_notail = lp.flen - tail;
  89. lp.flen_data = lp.flen_notail - lp.flen_head;
  90. lp.flen_remain -= lp.flen_head + tail;
  91. }
  92. int loop_fstream_init(io_stream_t handle, void *param)
  93. {
  94. int res = 0;
  95. loop_fstream_info_t *info = NULL;
  96. char *file_name = (char *)param;
  97. char *dir = NULL;
  98. uint32_t cluster = 0, blk_ofs = 0;
  99. info = mem_malloc(sizeof(loop_fstream_info_t));
  100. if (!info) {
  101. SYS_LOG_ERR("no memory\n");
  102. return -ENOMEM;
  103. }
  104. if (get_cluster_by_name(file_name, &dir, &cluster, &blk_ofs)) {
  105. res = fs_open_cluster(&info->fp, dir, cluster, blk_ofs);
  106. if (res) {
  107. SYS_LOG_ERR("open Failed %d\n", res);
  108. goto failed;
  109. }
  110. } else {
  111. res = fs_open(&info->fp, file_name, FA_READ | FA_WRITE | FA_OPEN_ALWAYS);
  112. if (res) {
  113. SYS_LOG_ERR("open Failed %d\n", res);
  114. goto failed;
  115. }
  116. }
  117. os_mutex_init(&info->lock);
  118. handle->data = info;
  119. return res;
  120. failed:
  121. mem_free(info);
  122. return res;
  123. }
  124. int loop_fstream_open(io_stream_t handle, stream_mode mode)
  125. {
  126. loop_fstream_info_t *info = (loop_fstream_info_t *)handle->data;
  127. assert(info);
  128. /* loop fstream is read only */
  129. if (mode != MODE_IN)
  130. return -EINVAL;
  131. handle->mode = mode;
  132. handle->write_finished = 0;
  133. handle->cache_size = 0;
  134. /* asume it a fake file length */
  135. handle->total_size = 0x7FFFFFFF;
  136. memset(&lp, 0, sizeof(struct loop_file_info));
  137. /* get actual file length and then seek back to file begin */
  138. if (!fs_seek(&info->fp, 0, FS_SEEK_END)) {
  139. lp.flen = fs_tell(&info->fp);
  140. fs_seek(&info->fp, 0, FS_SEEK_SET);
  141. }
  142. lp.flen_remain = handle->total_size;
  143. loop_fstream_clac_info(0, 0);
  144. handle->wofs = handle->total_size;
  145. SYS_LOG_INF("handle %p length %d mode %x \n", handle, lp.flen, mode);
  146. return 0;
  147. }
  148. int loop_fstream_read(io_stream_t handle, unsigned char *buf, int num)
  149. {
  150. int brw, cur_offset, read_len = num;
  151. unsigned char *ptr = buf;
  152. bool read_head_flag = false;
  153. loop_fstream_info_t *info = (loop_fstream_info_t *)handle->data;
  154. assert(info);
  155. brw = os_mutex_lock(&info->lock, OS_FOREVER);
  156. if (brw < 0){
  157. SYS_LOG_ERR("lock failed %d \n", brw);
  158. return brw;
  159. }
  160. cur_offset = fs_tell(&info->fp);
  161. /* enable energy limiter when file head or file tail */
  162. if (lp.energy_filt_enable) {
  163. if (cur_offset + lp.flen_energy_clac >= lp.flen
  164. || cur_offset - lp.flen_head <= lp.flen_energy_clac) {
  165. lp.energy_filt_enable(1);
  166. } else {
  167. lp.energy_filt_enable(0);
  168. }
  169. }
  170. /* reach file tail: read the rest data and ready for 2nd read */
  171. if (cur_offset + read_len > lp.flen_notail) {
  172. read_len = lp.flen_notail - cur_offset;
  173. /* make sure that it finally ends at the actual end of file */
  174. if (lp.flen_remain > lp.flen_data * 2) {
  175. lp.flen_remain -= lp.flen_data;
  176. read_head_flag = true;
  177. }
  178. }
  179. read_len = fs_read(&info->fp, ptr, read_len);
  180. if (read_len < 0) {
  181. SYS_LOG_ERR(" failed %d\n", read_len);
  182. goto err_out;
  183. }
  184. /* 2nd read data from acutal file head to get enough quantity data of "count" */
  185. ptr += read_len;
  186. if (read_head_flag) {
  187. brw = fs_seek(&info->fp, lp.flen_head, FS_SEEK_SET);
  188. if (brw < 0) {
  189. SYS_LOG_ERR(" seek head failed %d\n", brw);
  190. goto err_out;
  191. }
  192. brw = fs_read(&info->fp, ptr, num - read_len);
  193. if (brw < 0) {
  194. SYS_LOG_ERR(" failed %d\n", brw);
  195. goto err_out;
  196. }
  197. read_len += brw;
  198. }
  199. handle->rofs = fs_tell(&info->fp);
  200. lp.cur_rofs += read_len;
  201. if (lp.cur_rofs > handle->total_size)
  202. lp.cur_rofs -= handle->total_size;
  203. err_out:
  204. os_mutex_unlock(&info->lock);
  205. return read_len;
  206. }
  207. int loop_fstream_tell(io_stream_t handle)
  208. {
  209. /* return the read offset of virtual file */
  210. return lp.cur_rofs;
  211. }
  212. int loop_fstream_seek(io_stream_t handle, int offset, seek_dir origin)
  213. {
  214. /* must update lp.cur_rof, to indicate the virtual rofs */
  215. int whence = FS_SEEK_SET;
  216. int brw = 0;
  217. loop_fstream_info_t *info = (loop_fstream_info_t *)handle->data;
  218. assert(info);
  219. switch (origin) {
  220. case SEEK_DIR_CUR:
  221. if (lp.cur_rofs + offset > handle->total_size)
  222. return -1;//over length
  223. lp.cur_rofs += offset;
  224. /* convert to true file range */
  225. offset = lp.cur_rofs % lp.flen;
  226. break;
  227. case SEEK_DIR_END:
  228. if (offset > 0)
  229. return -1;//over length
  230. lp.cur_rofs = handle->total_size + offset;
  231. whence = FS_SEEK_END;
  232. break;
  233. case SEEK_DIR_BEG:
  234. if (offset > handle->total_size)
  235. return -1;//over length
  236. lp.cur_rofs = offset;
  237. /*do this because stream_seek has change SEEK_DIR_END to SEEK_DIR_BEG */
  238. if (handle->total_size - offset < lp.flen) {
  239. offset = lp.flen - (handle->total_size - offset);
  240. } else {
  241. /* calc the true offset because most cases "offset" is NOT in file */
  242. offset %= lp.flen;
  243. }
  244. default:
  245. break;
  246. }
  247. brw = fs_seek(&info->fp, offset, whence);
  248. if (brw) {
  249. SYS_LOG_ERR("seek failed %d,origin %d\n", brw, origin);
  250. return -1;
  251. }
  252. /* update the true rof */
  253. offset = fs_tell(&info->fp);
  254. handle->rofs = offset;
  255. return 0;
  256. }
  257. int loop_fstream_close(io_stream_t handle)
  258. {
  259. int res;
  260. loop_fstream_info_t *info = (loop_fstream_info_t *)handle->data;
  261. assert(info);
  262. res = os_mutex_lock(&info->lock, OS_FOREVER);
  263. if (res < 0) {
  264. SYS_LOG_ERR("lock failed %d \n",res);
  265. return res;
  266. }
  267. handle->wofs = 0;
  268. handle->rofs = 0;
  269. handle->state = STATE_CLOSE;
  270. os_mutex_unlock(&info->lock);
  271. return res;
  272. }
  273. int loop_fstream_destroy(io_stream_t handle)
  274. {
  275. int res;
  276. loop_fstream_info_t *info = (loop_fstream_info_t *)handle->data;
  277. assert(info);
  278. res = os_mutex_lock(&info->lock, OS_FOREVER);
  279. if (res < 0) {
  280. SYS_LOG_ERR("lock failed %d \n",res);
  281. return res;
  282. }
  283. res = fs_close(&info->fp);
  284. if (res) {
  285. SYS_LOG_ERR("close failed %d\n", res);
  286. }
  287. os_mutex_unlock(&info->lock);
  288. mem_free(info);
  289. return res;
  290. }
  291. /* get info from app who uses this stream */
  292. void loop_fstream_get_info(void *info, uint8_t info_id)
  293. {
  294. switch (info_id) {
  295. case LOOPFS_FILE_INFO:
  296. {
  297. struct loopfs_file_info *lps = (struct loopfs_file_info *)(void *)info;
  298. lp.flen_energy_clac = lps->bitrate / 8 * ENERGY_FILTER_TIME;
  299. loop_fstream_clac_info(lps->head, lps->tail);
  300. break;
  301. }
  302. case LOOPFS_CALLBACK:
  303. {
  304. lp.energy_filt_enable = (loopfs_energy_cb)info;
  305. break;
  306. }
  307. }
  308. }
  309. const stream_ops_t loop_fstream_ops = {
  310. .init = loop_fstream_init,
  311. .open = loop_fstream_open,
  312. .read = loop_fstream_read,
  313. .seek = loop_fstream_seek,
  314. .tell = loop_fstream_tell,
  315. .close = loop_fstream_close,
  316. .destroy = loop_fstream_destroy,
  317. };
  318. io_stream_t loop_fstream_create(const char *param)
  319. {
  320. return stream_create(&loop_fstream_ops, (void *)param);
  321. }