fdb.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #define FDB_LOG_TAG ""
  15. fdb_err_t _fdb_init_ex(fdb_db_t db, const char *name, const char *part_name, fdb_db_type type, void *user_data)
  16. {
  17. size_t block_size;
  18. FDB_ASSERT(db);
  19. FDB_ASSERT(name);
  20. FDB_ASSERT(part_name);
  21. if (db->init_ok) {
  22. return FDB_NO_ERR;
  23. }
  24. db->name = name;
  25. db->type = type;
  26. db->user_data = user_data;
  27. /* FAL (Flash Abstraction Layer) initialization */
  28. fal_init();
  29. /* check the flash partition */
  30. if ((db->part = fal_partition_find(part_name)) == NULL) {
  31. FDB_INFO("Error: Partition (%s) not found.\n", part_name);
  32. return FDB_PART_NOT_FOUND;
  33. }
  34. block_size = fal_flash_device_find(db->part->flash_name)->blk_size;
  35. if (db->sec_size == 0) {
  36. db->sec_size = block_size;
  37. } else {
  38. /* must be aligned with block size */
  39. FDB_ASSERT(db->sec_size % block_size == 0);
  40. }
  41. return FDB_NO_ERR;
  42. }
  43. void _fdb_init_finish(fdb_db_t db, fdb_err_t result)
  44. {
  45. static bool log_is_show = false;
  46. if (result == FDB_NO_ERR) {
  47. db->init_ok = true;
  48. if (!log_is_show) {
  49. FDB_INFO("FlashDB V%s is initialize success.\n", FDB_SW_VERSION);
  50. // FDB_INFO("You can get the latest version on https://github.com/armink/FlashDB .\n");
  51. log_is_show = true;
  52. }
  53. } else {
  54. FDB_INFO("Error: %s (%s) is initialize fail (%d).\n", db->type == FDB_DB_TYPE_KV ? "KVDB" : "TSDB",
  55. db->name, (int)result);
  56. }
  57. }