kvdb_basic_sample.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief basic KV samples.
  9. *
  10. * basic Key-Value Database KV feature samples
  11. * get and show currnet boot counts
  12. */
  13. #include "flashdb.h"
  14. #ifdef FDB_USING_KVDB
  15. #define FDB_LOG_TAG "[sample][kvdb][basic]"
  16. void kvdb_basic_sample(fdb_kvdb_t kvdb) {
  17. struct fdb_blob blob;
  18. int boot_count = 0;
  19. FDB_INFO("==================== kvdb_basic_sample ====================\n");
  20. { /* GET the KV value */
  21. /* get the "boot_count" KV value */
  22. // fdb_kv_get_blob(kvdb, "boot_count", fdb_blob_make(&blob, &boot_count,
  23. // sizeof(boot_count)));
  24. fdb_blob_make(&blob, &boot_count, sizeof(boot_count));
  25. fdb_kv_get_blob(
  26. kvdb, "boot_count",
  27. &blob); // fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  28. /* the blob.saved.len is more than 0 when get the value successful */
  29. if (blob.saved.len > 0) {
  30. FDB_INFO("get the 'boot_count' value is %d\n", boot_count);
  31. } else {
  32. FDB_INFO("get the 'boot_count' failed\n");
  33. }
  34. }
  35. { /* CHANGE the KV value */
  36. /* increase the boot count */
  37. boot_count++;
  38. /* change the "boot_count" KV's value */
  39. fdb_kv_set_blob(kvdb, "boot_count",
  40. fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  41. FDB_INFO("set the 'boot_count' value to %d\n", boot_count);
  42. }
  43. FDB_INFO("===========================================================\n");
  44. }
  45. #endif /* FDB_USING_KVDB */