kvdb_basic_sample.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. {
  18. struct fdb_blob blob;
  19. int boot_count = 0;
  20. FDB_INFO("==================== kvdb_basic_sample ====================\n");
  21. { /* GET the KV value */
  22. /* get the "boot_count" KV value */
  23. fdb_kv_get_blob(kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  24. /* the blob.saved.len is more than 0 when get the value successful */
  25. if (blob.saved.len > 0) {
  26. FDB_INFO("get the 'boot_count' value is %d\n", boot_count);
  27. } else {
  28. FDB_INFO("get the 'boot_count' failed\n");
  29. }
  30. }
  31. { /* CHANGE the KV value */
  32. /* increase the boot count */
  33. boot_count ++;
  34. /* change the "boot_count" KV's value */
  35. fdb_kv_set_blob(kvdb, "boot_count", fdb_blob_make(&blob, &boot_count, sizeof(boot_count)));
  36. FDB_INFO("set the 'boot_count' value to %d\n", boot_count);
  37. }
  38. FDB_INFO("===========================================================\n");
  39. }
  40. #endif /* FDB_USING_KVDB */