luat_fskv.c 7.5 KB

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