luat_fskv.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_READ_SIZE;
  79. fskv_lfs->conf.prog_size = LFS_BLOCK_DEVICE_PROG_SIZE;
  80. fskv_lfs->conf.cache_size = LFS_BLOCK_DEVICE_CACHE_SIZE;
  81. fskv_lfs->conf.lookahead_size = LFS_BLOCK_DEVICE_LOOK_AHEAD;
  82. fskv_lfs->conf.lookahead_buffer = fskv_lfs->lookahead_buffer;
  83. fskv_lfs->conf.prog_buffer = fskv_lfs->prog_buffer;
  84. fskv_lfs->conf.read_buffer = fskv_lfs->read_buffer;
  85. ret = lfs_mount(&fskv_lfs->lfs, &fskv_lfs->conf);
  86. if (ret != LFS_ERR_OK) {
  87. ret = lfs_format(&fskv_lfs->lfs, &fskv_lfs->conf);
  88. if (ret != LFS_ERR_OK) {
  89. free(fskv_lfs);
  90. fskv_lfs = NULL;
  91. LLOGE("fskv_lfs auto-format ret %d", ret);
  92. return ret;
  93. }
  94. ret = lfs_mount(&fskv_lfs->lfs, &fskv_lfs->conf);
  95. if (ret != LFS_ERR_OK) {
  96. free(fskv_lfs);
  97. fskv_lfs = NULL;
  98. LLOGE("fskv_lfs remount ret %d", ret);
  99. return ret;
  100. }
  101. }
  102. LLOGE("init ok");
  103. }
  104. return 0;
  105. }
  106. int luat_fskv_del(const char* key) {
  107. lfs_remove(&fskv_lfs->lfs, key);
  108. return 0;
  109. }
  110. int luat_fskv_set(const char* key, void* data, size_t len) {
  111. lfs_file_t fd = {0};
  112. int ret = 0;
  113. ret = lfs_file_open(&fskv_lfs->lfs, &fd, key, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC);
  114. if (ret != LFS_ERR_OK) {
  115. return -1;
  116. }
  117. ret = lfs_file_write(&fskv_lfs->lfs, &fd, data, len);
  118. ret |= lfs_file_close(&fskv_lfs->lfs, &fd);
  119. return ret;
  120. }
  121. int luat_fskv_get(const char* key, void* data, size_t len) {
  122. lfs_file_t fd = {0};
  123. int ret = 0;
  124. ret = lfs_file_open(&fskv_lfs->lfs, &fd, key, LFS_O_RDONLY);
  125. if (ret != LFS_ERR_OK) {
  126. return 0;
  127. }
  128. ret = lfs_file_read(&fskv_lfs->lfs, &fd, data, len);
  129. lfs_file_close(&fskv_lfs->lfs, &fd);
  130. return ret > 0 ? ret : 0;
  131. }
  132. int luat_fskv_clear(void) {
  133. int ret = 0;
  134. ret = lfs_format(&fskv_lfs->lfs, &fskv_lfs->conf);
  135. if (ret != LFS_ERR_OK) {
  136. LLOGE("fskv clear ret %d", ret);
  137. return ret;
  138. }
  139. ret = lfs_mount(&fskv_lfs->lfs, &fskv_lfs->conf);
  140. if (ret != LFS_ERR_OK) {
  141. LLOGE("fskv reinit ret %d", ret);
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. int luat_fskv_stat(size_t *using_sz, size_t *total, size_t *kv_count) {
  147. *using_sz = lfs_fs_size(&fskv_lfs->lfs) * LFS_BLOCK_DEVICE_ERASE_SIZE;
  148. *total = fskv_lfs->total_len;
  149. lfs_dir_t dir = {0};
  150. int ret = lfs_dir_open(&fskv_lfs->lfs, &dir, "");
  151. if (ret != LFS_ERR_OK) {
  152. LLOGE("lfs_dir_open ret %d", ret);
  153. return -1;
  154. }
  155. size_t count = 0;
  156. struct lfs_info info = {0};
  157. while (1) {
  158. ret = lfs_dir_read(&fskv_lfs->lfs, &dir, &info);
  159. if (ret > 0) {
  160. if (info.type == LFS_TYPE_REG)
  161. count ++;
  162. }
  163. else
  164. break;
  165. }
  166. lfs_dir_close(&fskv_lfs->lfs, &dir);
  167. *kv_count = count;
  168. return 0;
  169. }
  170. int luat_fskv_next(char* buff, size_t offset) {
  171. lfs_dir_t dir = {0};
  172. struct lfs_info info = {0};
  173. // offset要+2, 因为前2个值是"."和".."两个dir
  174. offset += 2;
  175. int ret = lfs_dir_open(&fskv_lfs->lfs, &dir, "");
  176. if (ret < 0) {
  177. LLOGE("lfs_dir_open ret %d", ret);
  178. return -1;
  179. }
  180. ret = lfs_dir_seek(&fskv_lfs->lfs, &dir, offset);
  181. if (ret < 0) {
  182. lfs_dir_close(&fskv_lfs->lfs, &dir);
  183. return -2;
  184. }
  185. ret = lfs_dir_read(&fskv_lfs->lfs, &dir, &info);
  186. if (ret <= 0) {
  187. lfs_dir_close(&fskv_lfs->lfs, &dir);
  188. return -3;
  189. }
  190. memcpy(buff, info.name, strlen(info.name) + 1);
  191. lfs_dir_close(&fskv_lfs->lfs, &dir);
  192. return 0;
  193. }
  194. int luat_fskv_init(void)
  195. {
  196. if (fskv_lfs == NULL) {
  197. if (fskv_lfs == NULL) {
  198. size_t len = 0;
  199. size_t start_address = luat_flash_get_fskv_addr(&len);
  200. if (start_address == 0) {
  201. LLOGE("fskv init failed");
  202. return -1;
  203. }
  204. LLOGE("fskv addr and len 0x%08X 0x%08X", start_address, len);
  205. luat_fskv_lfs_init(start_address, len);
  206. }
  207. if (fskv_lfs == NULL) {
  208. LLOGE("fskv init failed");
  209. return -1;
  210. }
  211. }
  212. return 0;
  213. }
  214. #endif