ql_fs_demo.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "ql_api_osi.h"
  9. #include "ql_log.h"
  10. #include "ql_fs_demo.h"
  11. #include "ql_fs.h"
  12. #include "ql_api_dev.h"
  13. #define QL_FS_DEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
  14. #define QL_FS_DEMO_LOG(msg, ...) QL_LOG(QL_FS_DEMO_LOG_LEVEL, "ql_fs_demo", msg, ##__VA_ARGS__)
  15. ql_task_t ql_fs_task = NULL;
  16. #define QL_FS_COPY_FILE_BUFF_SIZE (10240)
  17. #define QL_FS_COPY_FILE_FEED_DOG_SIZE (1024*110) /* 写110K数据喂一次看门狗,避免写大文件看门狗复位 */
  18. int ql_fs_copy_file(const char * sour_file_name, const char * dest_file_name)
  19. {
  20. int sour_fd = 0, dest_fd = 0;
  21. int64 err = QL_FILE_OK;
  22. void * data_buff = NULL;
  23. sour_fd = ql_fopen(sour_file_name, "r");
  24. if(sour_fd < 0)
  25. {
  26. QL_FS_DEMO_LOG("open sour file failed");
  27. err = sour_fd;
  28. goto exit;
  29. }
  30. dest_fd = ql_fopen(dest_file_name, "w+");
  31. if(dest_fd < 0)
  32. {
  33. QL_FS_DEMO_LOG("open dest file failed");
  34. err = dest_fd;
  35. goto exit;
  36. }
  37. data_buff = malloc(QL_FS_COPY_FILE_BUFF_SIZE);
  38. while(TRUE)
  39. {
  40. err = ql_fread(data_buff, QL_FS_COPY_FILE_BUFF_SIZE, 1, sour_fd);
  41. if(err > 0)
  42. {
  43. static int i = 1;
  44. err = ql_fwrite(data_buff, err, 1, dest_fd);
  45. if(err < 0)
  46. {
  47. QL_FS_DEMO_LOG("write file failed");
  48. goto exit;
  49. }
  50. if(i * QL_FS_COPY_FILE_BUFF_SIZE >= QL_FS_COPY_FILE_FEED_DOG_SIZE)
  51. {
  52. ql_dev_feed_wdt();
  53. i = 0;
  54. }
  55. i++;
  56. }
  57. else if(err == 0)
  58. {
  59. goto exit;
  60. }
  61. else if(err < 0)
  62. {
  63. if(QL_FILE_READ_ZERO == err)
  64. {
  65. err = QL_FILE_OK;
  66. }
  67. else
  68. {
  69. QL_FS_DEMO_LOG("read file failed");
  70. }
  71. goto exit;
  72. }
  73. }
  74. exit:
  75. if(sour_fd > 0)
  76. {
  77. ql_fclose(sour_fd);
  78. }
  79. if(dest_fd > 0)
  80. {
  81. ql_fclose(dest_fd);
  82. }
  83. if(NULL != data_buff)
  84. {
  85. free(data_buff);
  86. }
  87. return err;
  88. }
  89. void ql_fs_callback(void *ctx)
  90. {
  91. int fd = 0;
  92. int err = QL_FILE_OK;
  93. char buffer[100] = {0};
  94. char *str = TEST_STR;
  95. ql_rtos_task_sleep_s(15);
  96. err = ql_mkdir(DIR_PATH, 0);
  97. if(err < 0)
  98. {
  99. if(err == QL_DIR_DIR_ALREADY_EXIST)
  100. {
  101. QL_FS_DEMO_LOG("exist");
  102. }
  103. else
  104. {
  105. QL_FS_DEMO_LOG("failed");
  106. goto exit;
  107. }
  108. }
  109. /* open encrypted file to write*/
  110. fd = ql_fopen(FILE_PATH, "ew");
  111. if(fd < 0)
  112. {
  113. QL_FS_DEMO_LOG("failed");
  114. err = fd;
  115. goto exit;
  116. }
  117. /* write encrypted file */
  118. err = ql_fwrite(str, strlen(str) + 1, 1, fd); //strlen not include '\0'
  119. if(err < 0)
  120. {
  121. QL_FS_DEMO_LOG("failed");
  122. ql_fclose(fd);
  123. goto exit;
  124. }
  125. ql_fclose(fd);
  126. /* open encrypted file to read*/
  127. fd = ql_fopen(FILE_PATH, "er");
  128. if(fd < 0)
  129. {
  130. QL_FS_DEMO_LOG("failed");
  131. ql_fclose(fd);
  132. goto exit;
  133. }
  134. /* read encrypted file */
  135. memset(buffer, 0, 100);
  136. err = ql_fread(buffer, ql_fsize(fd), 1, fd);
  137. if(err < 0)
  138. {
  139. QL_FS_DEMO_LOG("failed");
  140. ql_fclose(fd);
  141. goto exit;
  142. }
  143. QL_FS_DEMO_LOG("encrypted:%s", buffer);
  144. ql_fclose(fd);
  145. /* write common file */
  146. fd = ql_fopen(FILE_PATH, "wb+");
  147. if(fd < 0)
  148. {
  149. QL_FS_DEMO_LOG("failed");
  150. err = fd;
  151. goto exit;
  152. }
  153. err = ql_fwrite(str, strlen(str) + 1, 1, fd); //strlen not include '\0'
  154. if(err < 0)
  155. {
  156. QL_FS_DEMO_LOG("failed");
  157. ql_fclose(fd);
  158. goto exit;
  159. }
  160. /* read common file */
  161. err = ql_frewind(fd);
  162. if(err < 0)
  163. {
  164. QL_FS_DEMO_LOG("failed");
  165. ql_fclose(fd);
  166. goto exit;
  167. }
  168. memset(buffer, 0, 100);
  169. err = ql_fread(buffer, ql_fsize(fd), 1, fd);
  170. if(err < 0)
  171. {
  172. QL_FS_DEMO_LOG("failed");
  173. ql_fclose(fd);
  174. goto exit;
  175. }
  176. QL_FS_DEMO_LOG("common:%s", buffer);
  177. ql_fclose(fd);
  178. err = ql_nvm_fwrite(TEST_NVM, str, strlen(str) + 1, 1); //nvm 配置文件不需要输入路径
  179. if(err < 0)
  180. {
  181. QL_FS_DEMO_LOG("failed");
  182. goto exit;
  183. }
  184. memset(buffer, 0, 100);
  185. err = ql_nvm_fread(TEST_NVM, buffer, strlen(str) + 1, 1);
  186. if(err < 0)
  187. {
  188. QL_FS_DEMO_LOG("failed");
  189. goto exit;
  190. }
  191. QL_FS_DEMO_LOG("nvm:%s", buffer);
  192. err = ql_cust_nvm_fwrite(str, strlen(str) + 1, 1); //客户nvm 配置文件不需要输入文件名,使用空间不大于1KB
  193. if(err < 0)
  194. {
  195. QL_FS_DEMO_LOG("failed");
  196. goto exit;
  197. }
  198. memset(buffer, 0, 100);
  199. err = ql_cust_nvm_fread(buffer, strlen(str) + 1, 1);
  200. if(err < 0)
  201. {
  202. QL_FS_DEMO_LOG("failed");
  203. goto exit;
  204. }
  205. QL_FS_DEMO_LOG("cust nvm:%s", buffer);
  206. memset(buffer, 0, 100);
  207. err = ql_fac_cust_nvm_fread(0, buffer, 100, 1);
  208. if(err < 0)
  209. {
  210. QL_FS_DEMO_LOG("failed");
  211. goto exit;
  212. }
  213. QL_FS_DEMO_LOG("fac cust nvm:%s", buffer);
  214. err = ql_fs_copy_file(FILE_PATH, DEST_FILE_PATH);
  215. if(err < 0)
  216. {
  217. QL_FS_DEMO_LOG("failed");
  218. goto exit;
  219. }
  220. fd = ql_fopen(DEST_FILE_PATH, "r");
  221. if(fd < 0)
  222. {
  223. QL_FS_DEMO_LOG("failed");
  224. err = fd;
  225. goto exit;
  226. }
  227. memset(buffer, 0, 100);
  228. err = ql_fread(buffer, ql_fsize(fd), 1, fd);
  229. if(err < 0)
  230. {
  231. QL_FS_DEMO_LOG("failed");
  232. ql_fclose(fd);
  233. goto exit;
  234. }
  235. QL_FS_DEMO_LOG("copyed:%s", buffer);
  236. ql_fclose(fd);
  237. ql_rmdir_ex(DIR_PATH); //删除DIR_PATH目录和目录里面的文件
  238. exit:
  239. if(err < 0)
  240. {
  241. QL_FS_DEMO_LOG("errcode is %x", err);
  242. }
  243. QL_FS_DEMO_LOG("exit file demo");
  244. ql_rtos_task_delete(NULL);
  245. }
  246. int ql_fs_demo_init(void)
  247. {
  248. QlOSStatus err = QL_OSI_SUCCESS;
  249. err = ql_rtos_task_create(&ql_fs_task, QL_FS_TASK_STACK_SIZE, QL_FS_TASK_PRIO, "ql_fs_task", ql_fs_callback, NULL, QL_FS_TASK_EVENT_CNT);
  250. if(err != QL_OSI_SUCCESS)
  251. {
  252. QL_FS_DEMO_LOG("file_task created failed");
  253. }
  254. return err;
  255. }