kv.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #ifndef AOS_KV_H
  5. #define AOS_KV_H
  6. // #include <aos/kernel.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include <string.h>
  12. #define KV_PATH_DEFULT "/data/kv/kv"
  13. #ifndef CONFIG_NV_PATH
  14. #define NV_PATH_DEFUALT "/mnt/PARAM/nv"
  15. #else
  16. #define NV_PATH_DEFUALT CONFIG_NV_PATH
  17. #endif
  18. /**
  19. * This function will init kv system
  20. * @return 0 on success, negative error on failure.
  21. */
  22. int aos_kv_init(const char *pathname);
  23. /**
  24. * This function will reset all kv, restore factory settting
  25. * @return 0 on success, negative error on failure.
  26. */
  27. int aos_kv_reset(void);
  28. /**
  29. * Add a new KV pair.
  30. *
  31. * @param[in] key the key of the KV pair.
  32. * @param[in] value the value of the KV pair.
  33. * @param[in] len the length of the value.
  34. * @param[in] sync save the KV pair to flash right now (should always be 1).
  35. *
  36. * @return 0 on success, negative error on failure.
  37. */
  38. int aos_kv_set(const char *key, void *value, int len, int sync);
  39. int aos_kv_setfloat(const char *key, float v);
  40. int aos_kv_setint(const char *key, int v);
  41. /**
  42. * Get the KV pair's value stored in buffer by its key.
  43. *
  44. * @note: the buffer_len should be larger than the real length of the value,
  45. * otherwise buffer would be NULL.
  46. *
  47. * @param[in] key the key of the KV pair to get.
  48. * @param[out] buffer the memory to store the value.
  49. * @param[in-out] buffer_len in: the length of the input buffer.
  50. * out: the real length of the value.
  51. *
  52. * @return 0 on success, negative error on failure.
  53. */
  54. int aos_kv_get(const char *key, void *buffer, int *buffer_len);
  55. int aos_kv_getfloat(const char *key, float *value);
  56. int aos_kv_getint(const char *key, int *value);
  57. /**
  58. *
  59. * @param[in] key the key of the KV pair.
  60. * @param[in] value the value of the KV pair.
  61. * @param[in] len the length of the value.
  62. * @param[in] sync save the KV pair to flash right now (should always be 1).
  63. *
  64. * @setstring return 0 on success, negative error on failure.
  65. * @getstring return >=0 on success(read len), negative error on failure.
  66. */
  67. int aos_kv_setstring(const char *key, const char *v);
  68. int aos_kv_getstring(const char *key, char *value, int len);
  69. /**
  70. * Delete the KV pair by its key.
  71. *
  72. * @param[in] key the key of the KV pair to delete.
  73. *
  74. * @return 0 on success, negative error on failure.
  75. */
  76. int aos_kv_del(const char *key);
  77. void aos_kv_foreach(void (*func)(char *key, char *val, uint16_t val_size, void *arg), void *arg);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* AOS_KV_H */