luat_fskv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "luat_base.h"
  2. #include "luat_fskv.h"
  3. #include "luat_malloc.h"
  4. #include "luat_msgbus.h"
  5. #include "luat_sfd.h"
  6. #define LUAT_LOG_TAG "fskv"
  7. #include "luat_log.h"
  8. #include "lfs.h"
  9. luat_fskv_t* fskv;
  10. static sfd_onchip_t onchip;
  11. int sfd_onchip_init (void* userdata);
  12. int sfd_onchip_status (void* userdata);
  13. int sfd_onchip_read (void* userdata, char* buff, size_t offset, size_t len);
  14. int sfd_onchip_write (void* userdata, const char* buff, size_t offset, size_t len);
  15. int sfd_onchip_erase (void* userdata, size_t offset, size_t len);
  16. int sfd_onchip_ioctl (void* userdata, size_t cmd, void* buff);
  17. // Read a block
  18. static int block_device_read(const struct lfs_config *cfg, lfs_block_t block,
  19. lfs_off_t off, void *buffer, lfs_size_t size) {
  20. int ret = sfd_onchip_read(&onchip, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  21. // LLOGD("sfd read %d %d %d %d", block, off, size, ret);
  22. if (ret >= 0) {
  23. // LLOGD("block_device_read return LFS_ERR_OK");
  24. return LFS_ERR_OK;
  25. }
  26. // LLOGD("block_device_read return LFS_ERR_IO");
  27. return LFS_ERR_IO;
  28. }
  29. // Program a block
  30. //
  31. // The block must have previously been erased.
  32. static int block_device_prog(const struct lfs_config *cfg, lfs_block_t block,
  33. lfs_off_t off, const void *buffer, lfs_size_t size) {
  34. int ret = sfd_onchip_write(&onchip, buffer, block * LFS_BLOCK_DEVICE_ERASE_SIZE + off, size);
  35. // LLOGD("sfd write %d %d %d %d", block, off, size, ret);
  36. if (ret >= 0) {
  37. // LLOGD("block_device_prog return LFS_ERR_OK");
  38. return LFS_ERR_OK;
  39. }
  40. // LLOGD("block_device_prog return LFS_ERR_IO");
  41. return LFS_ERR_IO;
  42. }
  43. // Erase a block
  44. //
  45. // A block must be erased before being programmed. The
  46. // state of an erased block is undefined.
  47. static int block_device_erase(const struct lfs_config *cfg, lfs_block_t block) {
  48. int ret = sfd_onchip_erase(&onchip, block * LFS_BLOCK_DEVICE_ERASE_SIZE, LFS_BLOCK_DEVICE_ERASE_SIZE);
  49. // LLOGD("sfd erase %d %d", block, ret);
  50. (void)ret;
  51. return 0;
  52. }
  53. // Sync the block device
  54. static int block_device_sync(const struct lfs_config *cfg) {
  55. return 0;
  56. }
  57. int luat_fskv_init(void) {
  58. int ret = 0;
  59. if (fskv == NULL) {
  60. fskv = luat_heap_malloc(sizeof(luat_fskv_t));
  61. if (fskv == NULL) {
  62. LLOGE("out of memory when malloc fskv");
  63. return -1;
  64. }
  65. memset(fskv, 0, sizeof(luat_fskv_t));
  66. fskv->conf.read = block_device_read;
  67. fskv->conf.prog = block_device_prog;
  68. fskv->conf.erase = block_device_erase;
  69. fskv->conf.sync = block_device_sync;
  70. fskv->conf.attr_max = 0;
  71. fskv->conf.file_max = 4096;
  72. fskv->conf.block_count = (LFS_BLOCK_DEVICE_TOTOAL_SIZE) / LFS_BLOCK_DEVICE_ERASE_SIZE;
  73. fskv->conf.block_size = LFS_BLOCK_DEVICE_ERASE_SIZE;
  74. fskv->conf.block_cycles = 200;
  75. fskv->conf.name_max = 63;
  76. fskv->conf.read_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  77. fskv->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  78. fskv->conf.prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  79. fskv->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  80. fskv->conf.lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  81. fskv->conf.lookahead_buffer = fskv->lookahead_buffer;
  82. fskv->conf.prog_buffer = fskv->prog_buffer;
  83. fskv->conf.read_buffer = fskv->read_buffer;
  84. // LLOGD("fskv->conf.block_count %d", fskv->conf.block_count);
  85. // LLOGD("fskv->conf.block_size %d", fskv->conf.block_size);
  86. // LLOGD("fskv->conf.read_size %d", fskv->conf.read_size);
  87. // LLOGD("fskv->conf.prog_size %d", fskv->conf.prog_size);
  88. // LLOGD("fskv->conf.cache_size %d", fskv->conf.cache_size);
  89. onchip.block_count = fskv->conf.block_count;
  90. onchip.block_size = LFS_BLOCK_DEVICE_ERASE_SIZE;
  91. memcpy(onchip.name, "fskv", 5);
  92. sfd_onchip_init(&onchip);
  93. // block_device_read(NULL, 0, 0, fskv->prog_buffer, 256);
  94. // LLOGD("Flash starts %02X %02X %02X %02X", fskv->prog_buffer[0], fskv->prog_buffer[1],
  95. // fskv->prog_buffer[2], fskv->prog_buffer[3]);
  96. // if (fskv->prog_buffer[0] == 0x00 && fskv->prog_buffer[1] == 0x00 &&
  97. // fskv->prog_buffer[2] == 245 && fskv->prog_buffer[3] == 256) {
  98. // }
  99. // sfd_onchip_erase(NULL, 0, LFS_BLOCK_DEVICE_TOTOAL_SIZE);
  100. ret = lfs_mount(&fskv->lfs, &fskv->conf);
  101. if (ret != LFS_ERR_OK) {
  102. LLOGI("fskv mount ret %d, exe auto-format", ret);
  103. ret = lfs_format(&fskv->lfs, &fskv->conf);
  104. if (ret != LFS_ERR_OK) {
  105. luat_heap_free(fskv);
  106. fskv = NULL;
  107. LLOGE("fskv auto-format ret %d", ret);
  108. return ret;
  109. }
  110. ret = lfs_mount(&fskv->lfs, &fskv->conf);
  111. if (ret != LFS_ERR_OK) {
  112. luat_heap_free(fskv);
  113. fskv = NULL;
  114. LLOGE("fskv remount ret %d", ret);
  115. return ret;
  116. }
  117. }
  118. LLOGD("init ok");
  119. }
  120. return 0;
  121. }
  122. int luat_fskv_del(const char* key) {
  123. lfs_remove(&fskv->lfs, key);
  124. return 0;
  125. }
  126. int luat_fskv_set(const char* key, void* data, size_t len) {
  127. lfs_file_t fd = {0};
  128. int ret = 0;
  129. ret = lfs_file_open(&fskv->lfs, &fd, key, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC);
  130. if (ret != LFS_ERR_OK) {
  131. return -1;
  132. }
  133. ret = lfs_file_write(&fskv->lfs, &fd, data, len);
  134. lfs_file_close(&fskv->lfs, &fd);
  135. return ret;
  136. }
  137. int luat_fskv_get(const char* key, void* data, size_t len) {
  138. lfs_file_t fd = {0};
  139. int ret = 0;
  140. ret = lfs_file_open(&fskv->lfs, &fd, key, LFS_O_RDONLY);
  141. if (ret != LFS_ERR_OK) {
  142. return 0;
  143. }
  144. ret = lfs_file_read(&fskv->lfs, &fd, data, len);
  145. lfs_file_close(&fskv->lfs, &fd);
  146. return ret > 0 ? ret : 0;
  147. }
  148. int luat_fskv_clear(void) {
  149. int ret = 0;
  150. ret = lfs_format(&fskv->lfs, &fskv->conf);
  151. if (ret != LFS_ERR_OK) {
  152. luat_heap_free(fskv);
  153. LLOGE("fskv clear ret %d", ret);
  154. return ret;
  155. }
  156. ret = lfs_mount(&fskv->lfs, &fskv->conf);
  157. if (ret != LFS_ERR_OK) {
  158. luat_heap_free(fskv);
  159. LLOGE("fskv reinit ret %d", ret);
  160. return ret;
  161. }
  162. return 0;
  163. }
  164. int luat_fskv_stat(size_t *using_sz, size_t *total, size_t *kv_count) {
  165. *using_sz = lfs_fs_size(&fskv->lfs) * LFS_BLOCK_DEVICE_ERASE_SIZE;
  166. *total = LFS_BLOCK_DEVICE_TOTOAL_SIZE;
  167. lfs_dir_t dir = {0};
  168. int ret = lfs_dir_open(&fskv->lfs, &dir, "");
  169. if (ret != LFS_ERR_OK) {
  170. LLOGW("lfs_dir_open ret %d", ret);
  171. return -1;
  172. }
  173. size_t count = 0;
  174. struct lfs_info info = {0};
  175. while (1) {
  176. ret = lfs_dir_read(&fskv->lfs, &dir, &info);
  177. if (ret > 0) {
  178. if (info.type == LFS_TYPE_REG)
  179. count ++;
  180. }
  181. else
  182. break;
  183. }
  184. lfs_dir_close(&fskv->lfs, &dir);
  185. *kv_count = count;
  186. return 0;
  187. }