Skip to content

Commit 213cc80

Browse files
Adding KVStore api from mbedos: interface
1 parent f64f904 commit 213cc80

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

libraries/KVStore/src/KVStore.h

+229
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_KVSTORE_H
18+
#define MBED_KVSTORE_H
19+
20+
#include <stdint.h>
21+
#include <string.h>
22+
23+
#define KVSTORE_SUCCESS 0
24+
#define KVSTORE_ERROR_READ_FAILED 283
25+
#define KVSTORE_ERROR_WRITE_FAILED 284
26+
#define KVSTORE_ERROR_INVALID_DATA_DETECTED 258
27+
#define KVSTORE_ERROR_INVALID_SIZE 261
28+
#define KVSTORE_ERROR_INVALID_ARGUMENT 257
29+
#define KVSTORE_ERROR_ITEM_NOT_FOUND 263
30+
#define KVSTORE_ERROR_MEDIA_FULL 267
31+
#define KVSTORE_ERROR_WRITE_PROTECTED 274
32+
#define KVSTORE_ERROR_OUT_OF_RESOURCES 288
33+
#define KVSTORE_ERROR_NOT_READY 270
34+
#define KVSTORE_ERROR_FAILED_OPERATION 271
35+
36+
namespace mbed {
37+
38+
/** KVStore class
39+
*
40+
* Interface class for Key Value Storage
41+
*/
42+
class KVStore {
43+
public:
44+
enum create_flags {
45+
WRITE_ONCE_FLAG = (1 << 0),
46+
REQUIRE_CONFIDENTIALITY_FLAG = (1 << 1),
47+
RESERVED_FLAG = (1 << 2),
48+
REQUIRE_REPLAY_PROTECTION_FLAG = (1 << 3),
49+
};
50+
51+
static const uint32_t MAX_KEY_SIZE = 128;
52+
53+
typedef struct _opaque_set_handle *set_handle_t;
54+
55+
typedef struct _opaque_key_iterator *iterator_t;
56+
57+
/**
58+
* Holds key information
59+
*/
60+
typedef struct info {
61+
/**
62+
* The key size
63+
*/
64+
size_t size;
65+
/*
66+
* The Key flags, possible flags combination:
67+
* WRITE_ONCE_FLAG,
68+
* REQUIRE_CONFIDENTIALITY_FLAG,
69+
* REQUIRE_REPLAY_PROTECTION_FLAG
70+
*/
71+
uint32_t flags;
72+
} info_t;
73+
74+
virtual ~KVStore() {};
75+
76+
/**
77+
* @brief Initialize KVStore
78+
*
79+
* @returns KVSTORE_SUCCESS on success or an error code on failure
80+
*/
81+
virtual int init() = 0;
82+
83+
/**
84+
* @brief Deinitialize KVStore
85+
*
86+
* @returns KVSTORE_SUCCESS on success or an error code on failure
87+
*/
88+
virtual int deinit() = 0;
89+
90+
91+
/**
92+
* @brief Reset KVStore contents (clear all keys)
93+
*
94+
* @returns KVSTORE_SUCCESS on success or an error code on failure
95+
*/
96+
virtual int reset() = 0;
97+
98+
/**
99+
* @brief Set one KVStore item, given key and value.
100+
*
101+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
102+
* @param[in] buffer Value data buffer.
103+
* @param[in] size Value data size.
104+
* @param[in] create_flags Flag mask.
105+
*
106+
* @returns KVSTORE_SUCCESS on success or an error code on failure
107+
*/
108+
virtual int set(const char *key, const void *buffer, size_t size, uint32_t create_flags) = 0;
109+
110+
/**
111+
* @brief Get one KVStore item, given key.
112+
*
113+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
114+
* @param[in] buffer Value data buffer.
115+
* @param[in] buffer_size Value data buffer size.
116+
* @param[out] actual_size Actual read size (NULL to pass nothing).
117+
* @param[in] offset Offset to read from in data.
118+
*
119+
* @returns KVSTORE_SUCCESS on success or an error code on failure
120+
*/
121+
virtual int get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size = NULL, size_t offset = 0) = 0;
122+
123+
/**
124+
* @brief Get information of a given key.
125+
*
126+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
127+
* @param[out] info Returned information structure (NULL to pass nothing).
128+
*
129+
* @returns KVSTORE_SUCCESS on success or an error code on failure
130+
*/
131+
virtual int get_info(const char *key, info_t *info = NULL) = 0;
132+
133+
/**
134+
* @brief Remove a KVStore item, given key.
135+
*
136+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
137+
*
138+
* @returns KVSTORE_SUCCESS on success or an error code on failure
139+
*/
140+
virtual int remove(const char *key) = 0;
141+
142+
143+
/**
144+
* @brief Start an incremental KVStore set sequence.
145+
*
146+
* @param[out] handle Returned incremental set handle.
147+
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
148+
* @param[in] final_data_size Final value data size.
149+
* @param[in] create_flags Flag mask.
150+
*
151+
* @returns KVSTORE_SUCCESS on success or an error code on failure
152+
*/
153+
virtual int set_start(set_handle_t *handle, const char *key, size_t final_data_size, uint32_t create_flags) = 0;
154+
155+
/**
156+
* @brief Add data to incremental KVStore set sequence.
157+
*
158+
* @param[in] handle Incremental set handle.
159+
* @param[in] value_data Value data to add.
160+
* @param[in] data_size Value data size.
161+
*
162+
* @returns KVSTORE_SUCCESS on success or an error code on failure
163+
*/
164+
virtual int set_add_data(set_handle_t handle, const void *value_data, size_t data_size) = 0;
165+
166+
/**
167+
* @brief Finalize an incremental KVStore set sequence.
168+
*
169+
* @param[in] handle Incremental set handle.
170+
*
171+
* @returns KVSTORE_SUCCESS on success or an error code on failure
172+
*/
173+
virtual int set_finalize(set_handle_t handle) = 0;
174+
175+
/**
176+
* @brief Start an iteration over KVStore keys.
177+
*
178+
* @param[out] it Returned iterator handle.
179+
* @param[in] prefix Key prefix (null for all keys).
180+
*
181+
* @returns KVSTORE_SUCCESS on success or an error code on failure
182+
*/
183+
virtual int iterator_open(iterator_t *it, const char *prefix = NULL) = 0;
184+
185+
/**
186+
* @brief Get next key in iteration.
187+
*
188+
* @param[in] it Iterator handle.
189+
* @param[in] key Buffer for returned key.
190+
* @param[in] key_size Key buffer size.
191+
*
192+
* @returns KVSTORE_SUCCESS on success or an error code on failure
193+
*/
194+
virtual int iterator_next(iterator_t it, char *key, size_t key_size) = 0;
195+
196+
/**
197+
* @brief Close iteration.
198+
*
199+
* @param[in] it Iterator handle.
200+
*
201+
* @returns KVSTORE_SUCCESS on success or an error code on failure
202+
*/
203+
virtual int iterator_close(iterator_t it) = 0;
204+
205+
/** Convenience function for checking key validity.
206+
* Key must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
207+
*
208+
* @param[in] key Key buffer.
209+
*
210+
* @returns KVSTORE_SUCCESS on success or an error code on failure
211+
*/
212+
bool is_valid_key(const char *key) const
213+
{
214+
if (!key || !strlen(key) || (strlen(key) > MAX_KEY_SIZE)) {
215+
return false;
216+
}
217+
218+
if (strpbrk(key, " */?:;\"|<>\\")) {
219+
return false;
220+
}
221+
return true;
222+
}
223+
224+
};
225+
/** @}*/
226+
227+
} // namespace mbed
228+
229+
#endif

0 commit comments

Comments
 (0)