fdb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Initialize interface.
  9. *
  10. * Some initialize interface for this library.
  11. */
  12. #include "flashdb.h"
  13. #include "fdb_low_lvl.h"
  14. #include <string.h>
  15. #include <inttypes.h>
  16. #define FDB_LOG_TAG ""
  17. #if !defined(FDB_USING_FAL_MODE) && !defined(FDB_USING_FILE_MODE)
  18. #error "Please defined the FDB_USING_FAL_MODE or FDB_USING_FILE_MODE macro"
  19. #endif
  20. fdb_err_t _fdb_init_ex(fdb_db_t db,
  21. const char* name,
  22. const char* path,
  23. fdb_db_type type,
  24. void* user_data) {
  25. FDB_ASSERT(db);
  26. FDB_ASSERT(name);
  27. FDB_ASSERT(path);
  28. if (db->init_ok) {
  29. return FDB_NO_ERR;
  30. }
  31. db->name = name;
  32. db->type = type;
  33. db->user_data = user_data;
  34. if (db->file_mode) {
  35. #ifdef FDB_USING_FILE_MODE
  36. /* must set when using file mode */
  37. FDB_ASSERT(db->sec_size != 0);
  38. FDB_ASSERT(db->max_size != 0);
  39. #ifdef FDB_USING_FILE_POSIX_MODE
  40. db->cur_file = -1;
  41. #else
  42. db->cur_file = 0;
  43. #endif
  44. db->storage.dir = path;
  45. FDB_ASSERT(strlen(path) != 0)
  46. #endif
  47. } else {
  48. #ifdef FDB_USING_FAL_MODE
  49. size_t block_size;
  50. /* FAL (Flash Abstraction Layer) initialization */
  51. fal_init();
  52. /* check the flash partition */
  53. if ((db->storage.part = fal_partition_find(path)) == NULL) {
  54. FDB_INFO("Error: Partition (%s) not found.\n", path);
  55. return FDB_PART_NOT_FOUND;
  56. }
  57. block_size =
  58. fal_flash_device_find(db->storage.part->flash_name)->blk_size;
  59. if (db->sec_size == 0) {
  60. db->sec_size = block_size;
  61. } else {
  62. // rbg/kcf
  63. /* must be aligned with block size */
  64. if (db->sec_size % block_size != 0) {
  65. // FDB_INFO("Error: db sector size (%" PRIu32 ") MUST align
  66. // with block size (%" PRIu32 ").\n", db->sec_size,
  67. // block_size);
  68. FDB_INFO(
  69. "Error: db sector size (%lu) MUST align with block size "
  70. "(%u).\n",
  71. db->sec_size, block_size);
  72. return FDB_INIT_FAILED;
  73. }
  74. }
  75. db->max_size = db->storage.part->len;
  76. #endif /* FDB_USING_FAL_MODE */
  77. }
  78. /* the block size MUST to be the Nth power of 2 */
  79. FDB_ASSERT((db->sec_size & (db->sec_size - 1)) == 0);
  80. /* must align with sector size */
  81. if (db->max_size % db->sec_size != 0) {
  82. FDB_INFO("Error: db total size (%" PRIu32
  83. ") MUST align with sector size (%" PRIu32 ").\n",
  84. db->max_size, db->sec_size);
  85. return FDB_INIT_FAILED;
  86. }
  87. /* must has more than or equal 2 sectors */
  88. if (db->max_size / db->sec_size < 2) {
  89. FDB_INFO(
  90. "Error: db MUST has more than or equal 2 sectors, current has "
  91. "%" PRIu32 " sector(s)\n",
  92. db->max_size / db->sec_size);
  93. return FDB_INIT_FAILED;
  94. }
  95. return FDB_NO_ERR;
  96. }
  97. void _fdb_init_finish(fdb_db_t db, fdb_err_t result) {
  98. static pika_bool log_is_show = pika_false;
  99. if (result == FDB_NO_ERR) {
  100. db->init_ok = pika_true;
  101. if (!log_is_show) {
  102. FDB_INFO("FlashDB V%s is initialize success.\n", FDB_SW_VERSION);
  103. FDB_INFO(
  104. "You can get the latest version on "
  105. "https://github.com/armink/FlashDB .\n");
  106. log_is_show = pika_true;
  107. }
  108. } else if (!db->not_formatable) {
  109. FDB_INFO("Error: %s (%s@%s) is initialize fail (%d).\n",
  110. db->type == FDB_DB_TYPE_KV ? "KVDB" : "TSDB", db->name,
  111. _fdb_db_path(db), (int)result);
  112. }
  113. }
  114. void _fdb_deinit(fdb_db_t db) {
  115. FDB_ASSERT(db);
  116. if (db->init_ok) {
  117. #ifdef FDB_USING_FILE_MODE
  118. #ifdef FDB_USING_FILE_POSIX_MODE
  119. if (db->cur_file > 0) {
  120. #if !defined(_MSC_VER)
  121. #include <unistd.h>
  122. #endif
  123. close(db->cur_file);
  124. }
  125. #else
  126. if (db->cur_file != 0) {
  127. pika_platform_fclose(db->cur_file);
  128. db->cur_file = 0;
  129. }
  130. #endif /* FDB_USING_FILE_POSIX_MODE */
  131. #endif /* FDB_USING_FILE_MODE */
  132. }
  133. db->init_ok = pika_false;
  134. }
  135. const char* _fdb_db_path(fdb_db_t db) {
  136. if (db->file_mode) {
  137. #ifdef FDB_USING_FILE_MODE
  138. return db->storage.dir;
  139. #else
  140. return NULL;
  141. #endif
  142. } else {
  143. #ifdef FDB_USING_FAL_MODE
  144. return db->storage.part->name;
  145. #else
  146. return NULL;
  147. #endif
  148. }
  149. }