1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef KV_API_H
6 #define KV_API_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include <stdint.h>
13 
14 /* Key-value function return code description */
15 #define KV_OK                 0     /* Successed */
16 #define KV_LOOP_CONTINUE      10000 /* Loop Continue */
17 #define KV_ERR_NO_SPACE      -10001 /* The space is out of range */
18 #define KV_ERR_INVALID_PARAM -10002 /* The parameter is invalid */
19 #define KV_ERR_MALLOC_FAILED -10003 /* The os memory malloc error */
20 #define KV_ERR_NOT_FOUND     -10004 /* Could not found the item */
21 #define KV_ERR_FLASH_READ    -10005 /* The flash read operation error */
22 #define KV_ERR_FLASH_WRITE   -10006 /* The flash write operation error */
23 #define KV_ERR_FLASH_ERASE   -10007 /* The flash erase operation error */
24 #define KV_ERR_OS_LOCK       -10008 /* The error related to os lock */
25 #define KV_ERR_OS_SEM        -10009 /* The error related to os semaphose */
26 
27 #define KV_ERR_ENCRYPT       -10010 /* Data encryption error */
28 #define KV_ERR_DECRYPT       -10011 /* Data decryption error */
29 #define KV_ERR_NOT_SUPPORT   -10012 /* The function is not support yet */
30 
31 /**
32  * @brief Initialize the kv module
33  *
34  * @retrun 0 on success, otherwise will be failed.
35  *
36  */
37 int32_t kv_init(void);
38 
39 /**
40  * @brief De-initialize the kv module
41  *
42  * @retrun none
43  */
44 void kv_deinit(void);
45 
46 /**
47  * Add a new KV pair.
48  *
49  * @param[in]  key    the key of the KV pair.
50  * @param[in]  val    the value of the KV pair.
51  * @param[in]  len    the length of the value.
52  *
53  * @return  0 on success, negative error on failure.
54  */
55 int32_t kv_item_set(const char *key, const void *val, int32_t len);
56 
57 /**
58  * Get the KV pair's value stored in buffer by its key.
59  *
60  * @note: the buffer_len should be larger than the real length of the value,
61  *        otherwise buffer would be NULL.
62  *
63  * @param[in]      key         the key of the KV pair to get.
64  * @param[out]     buffer      the memory to store the value.
65  * @param[in-out]  buffer_len  in: the length of the input buffer.
66  *                             out: the real length of the value.
67  *
68  * @return  0 on success, negative error on failure.
69  */
70 int32_t kv_item_get(const char *key, void *buffer, int32_t *buffer_len);
71 
72 /**
73  * Delete the KV pair by its key.
74  *
75  * @param[in]  key  the key of the KV pair to delete.
76  *
77  * @return  0 on success, negative error on failure.
78  */
79 int32_t kv_item_delete(const char *key);
80 
81 /**
82  * Delete the KV pair by its prefix.
83  *
84  * @param[in]  prefix  the prefix of the kv pair which is need to delete.
85  *
86  * @return  0 on success, negative error on failure.
87  */
88 int32_t kv_item_delete_by_prefix(const char *prefix);
89 
90 /**
91  * Add a new KV pair (secure mode).
92  *
93  * @param[in]  key    the key of the KV pair.
94  * @param[in]  val    the value of the KV pair.
95  * @param[in]  len    the length of the value.
96  *
97  * @return  0 on success, negative error on failure.
98  */
99 int32_t kv_item_secure_set(const char *key, const void *val, int32_t len);
100 
101 /**
102  * Get the KV pair's value stored in buffer by its key. (secure mode)
103  *
104  * @note: the buffer_len should be larger than the real length of the value,
105  *        otherwise buffer would be NULL.
106  *
107  * @param[in]      key         the key of the KV pair to get.
108  * @param[out]     buffer      the memory to store the value.
109  * @param[in-out]  buffer_len  in: the length of the input buffer.
110  *                             out: the real length of the value.
111  *
112  * @return  0 on success, negative error on failure.
113  */
114 int32_t kv_item_secure_get(const char *key, void *buffer, int32_t *buffer_len);
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif  /* KV_API_H */
121 
122