luat_fskv.c 7.2 KB

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