luat_fskv_pc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "luat_base.h"
  2. #include "luat_fskv.h"
  3. #include "luat_malloc.h"
  4. #define LUAT_LOG_TAG "fskv"
  5. #include "luat_log.h"
  6. #include "lfs.h"
  7. #define LFS_BLOCK_DEVICE_READ_SIZE (256)
  8. #define LFS_BLOCK_DEVICE_PROG_SIZE (256)
  9. #define LFS_BLOCK_DEVICE_CACHE_SIZE (256)
  10. #define LFS_BLOCK_DEVICE_ERASE_SIZE (4096) // one sector 4KB
  11. #define LFS_BLOCK_DEVICE_TOTOAL_SIZE (64 * 1024)
  12. #define LFS_BLOCK_DEVICE_LOOK_AHEAD (16)
  13. static lfs_t fskv_lfs;
  14. static struct lfs_config fskv_lfs_conf;
  15. static char fskv_buff[LFS_BLOCK_DEVICE_TOTOAL_SIZE];
  16. // Read a block
  17. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  18. lfs_off_t off, void *buffer, lfs_size_t size)
  19. {
  20. (void)cfg;
  21. memcpy(buffer, fskv_buff + (block * 4096 + off), size);
  22. return LFS_ERR_OK;
  23. }
  24. // Program a block
  25. //
  26. // The block must have previously been erased.
  27. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  28. lfs_off_t off, const void *buffer, lfs_size_t size)
  29. {
  30. (void)cfg;
  31. memcpy(fskv_buff + (block * 4096 + off), buffer, size);
  32. return LFS_ERR_OK;
  33. }
  34. // Erase a block
  35. //
  36. // A block must be erased before being programmed. The
  37. // state of an erased block is undefined.
  38. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block)
  39. {
  40. (void)cfg;
  41. memset(fskv_buff + (block * 4096), 0, 4096);
  42. return 0;
  43. }
  44. // Sync the block device
  45. static int block_device_sync(const struct lfs_config *cfg)
  46. {
  47. (void)cfg;
  48. return 0;
  49. }
  50. int luat_fskv_init(void)
  51. {
  52. int ret = 0;
  53. fskv_lfs_conf.read = block_device_read;
  54. fskv_lfs_conf.prog = block_device_prog;
  55. fskv_lfs_conf.erase = block_device_erase;
  56. fskv_lfs_conf.sync = block_device_sync;
  57. fskv_lfs_conf.attr_max = 0;
  58. fskv_lfs_conf.file_max = 4096;
  59. fskv_lfs_conf.block_count = (LFS_BLOCK_DEVICE_TOTOAL_SIZE) / LFS_BLOCK_DEVICE_ERASE_SIZE;
  60. fskv_lfs_conf.block_size = LFS_BLOCK_DEVICE_ERASE_SIZE;
  61. fskv_lfs_conf.block_cycles = 200;
  62. fskv_lfs_conf.name_max = 63;
  63. fskv_lfs_conf.read_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  64. fskv_lfs_conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  65. fskv_lfs_conf.prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  66. fskv_lfs_conf.lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  67. fskv_lfs_conf.lookahead_buffer = luat_heap_malloc(4096);
  68. fskv_lfs_conf.prog_buffer = luat_heap_malloc(4096);
  69. fskv_lfs_conf.read_buffer = luat_heap_malloc(4096);
  70. fskv_lfs_conf.context = NULL;
  71. ret = lfs_mount(&fskv_lfs, &fskv_lfs_conf);
  72. if (ret != LFS_ERR_OK)
  73. {
  74. LLOGI("fskv_lfs mount ret %d, exec auto-format", ret);
  75. ret = lfs_format(&fskv_lfs, &fskv_lfs_conf);
  76. if (ret != LFS_ERR_OK)
  77. {
  78. LLOGE("fskv_lfs auto-format ret %d", ret);
  79. return ret;
  80. }
  81. ret = lfs_mount(&fskv_lfs, &fskv_lfs_conf);
  82. if (ret != LFS_ERR_OK)
  83. {
  84. LLOGE("fskv_lfs remount ret %d", ret);
  85. return ret;
  86. }
  87. }
  88. LLOGD("init ok");
  89. return 0;
  90. }
  91. int luat_fskv_del(const char *key)
  92. {
  93. lfs_remove(&fskv_lfs, key);
  94. return 0;
  95. }
  96. int luat_fskv_set(const char *key, void *data, size_t len)
  97. {
  98. lfs_file_t fd = {0};
  99. int ret = 0;
  100. ret = lfs_file_open(&fskv_lfs, &fd, key, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC);
  101. if (ret != LFS_ERR_OK)
  102. {
  103. return -1;
  104. }
  105. ret = lfs_file_write(&fskv_lfs, &fd, data, len);
  106. ret |= lfs_file_close(&fskv_lfs, &fd);
  107. return ret;
  108. }
  109. int luat_fskv_get(const char *key, void *data, size_t len)
  110. {
  111. lfs_file_t fd = {0};
  112. int ret = 0;
  113. ret = lfs_file_open(&fskv_lfs, &fd, key, LFS_O_RDONLY);
  114. if (ret != LFS_ERR_OK)
  115. {
  116. return 0;
  117. }
  118. ret = lfs_file_read(&fskv_lfs, &fd, data, len);
  119. lfs_file_close(&fskv_lfs, &fd);
  120. return ret > 0 ? ret : 0;
  121. }
  122. int luat_fskv_clear(void)
  123. {
  124. int ret = 0;
  125. ret = lfs_format(&fskv_lfs, &fskv_lfs_conf);
  126. if (ret != LFS_ERR_OK)
  127. {
  128. LLOGE("fskv clear ret %d", ret);
  129. return ret;
  130. }
  131. ret = lfs_mount(&fskv_lfs, &fskv_lfs_conf);
  132. if (ret != LFS_ERR_OK)
  133. {
  134. LLOGE("fskv reinit ret %d", ret);
  135. return ret;
  136. }
  137. return 0;
  138. }
  139. int luat_fskv_stat(size_t *using_sz, size_t *total, size_t *kv_count)
  140. {
  141. *using_sz = lfs_fs_size(&fskv_lfs) * LFS_BLOCK_DEVICE_ERASE_SIZE;
  142. *total = LFS_BLOCK_DEVICE_TOTOAL_SIZE;
  143. lfs_dir_t dir = {0};
  144. int ret = lfs_dir_open(&fskv_lfs, &dir, "");
  145. if (ret != LFS_ERR_OK)
  146. {
  147. LLOGW("lfs_dir_open ret %d", ret);
  148. return -1;
  149. }
  150. size_t count = 0;
  151. struct lfs_info info = {0};
  152. while (1)
  153. {
  154. ret = lfs_dir_read(&fskv_lfs, &dir, &info);
  155. if (ret > 0)
  156. {
  157. if (info.type == LFS_TYPE_REG)
  158. count++;
  159. }
  160. else
  161. break;
  162. }
  163. lfs_dir_close(&fskv_lfs, &dir);
  164. *kv_count = count;
  165. return 0;
  166. }
  167. int luat_fskv_size(const char *key, char buff[4])
  168. {
  169. lfs_file_t fd = {0};
  170. int ret = 0;
  171. ret = lfs_file_open(&fskv_lfs, &fd, key, LFS_O_RDONLY);
  172. if (ret != LFS_ERR_OK)
  173. {
  174. return 0;
  175. }
  176. ret = lfs_file_size(&fskv_lfs, &fd);
  177. if (ret > 1 && ret < 256)
  178. {
  179. int ret2 = lfs_file_read(&fskv_lfs, &fd, buff, ret);
  180. if (ret2 != ret)
  181. {
  182. ret = -2; // 读取失败,肯定有问题
  183. }
  184. }
  185. lfs_file_close(&fskv_lfs, &fd);
  186. return ret;
  187. }
  188. int luat_fskv_next(char *buff, size_t offset)
  189. {
  190. lfs_dir_t dir = {0};
  191. struct lfs_info info = {0};
  192. // offset要+2, 因为前2个值是"."和".."两个dir
  193. offset += 2;
  194. int ret = lfs_dir_open(&fskv_lfs, &dir, "");
  195. if (ret < 0)
  196. {
  197. LLOGW("lfs_dir_open ret %d", ret);
  198. return -1;
  199. }
  200. ret = lfs_dir_seek(&fskv_lfs, &dir, offset);
  201. if (ret < 0)
  202. {
  203. lfs_dir_close(&fskv_lfs, &dir);
  204. return -2;
  205. }
  206. ret = lfs_dir_read(&fskv_lfs, &dir, &info);
  207. if (ret <= 0)
  208. {
  209. lfs_dir_close(&fskv_lfs, &dir);
  210. return -3;
  211. }
  212. memcpy(buff, info.name, strlen(info.name) + 1);
  213. lfs_dir_close(&fskv_lfs, &dir);
  214. return 0;
  215. }