1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4 #include <stdint.h>
5
6 #include "kv.h"
7 #include "aos/errno.h"
8
_kv_to_aos_res(int res)9 static int _kv_to_aos_res(int res)
10 {
11 switch (res) {
12 case KV_OK:
13 return 0;
14 case KV_ERR_NO_SPACE:
15 return -ENOSPC;
16 case KV_ERR_INVALID_PARAM:
17 return -EINVAL;
18 case KV_ERR_MALLOC_FAILED:
19 return -ENOMEM;
20 case KV_ERR_NOT_FOUND:
21 return -ENOENT;
22 case KV_ERR_FLASH_READ:
23 case KV_ERR_FLASH_WRITE:
24 case KV_ERR_FLASH_ERASE:
25 return -EIO;
26 case KV_ERR_NOT_SUPPORT:
27 return -ENOSYS;
28 default:
29 return -EBUSY;
30 }
31 }
32
aos_kv_init(void)33 int aos_kv_init(void)
34 {
35 return _kv_to_aos_res(kv_init());
36 }
37
aos_kv_deinit(void)38 void aos_kv_deinit(void)
39 {
40 kv_deinit();
41 }
42
aos_kv_set(const char * key,const void * value,int len,int sync)43 int aos_kv_set(const char *key, const void *value, int len, int sync)
44 {
45 return _kv_to_aos_res(kv_item_set(key, value, len));
46 }
47
aos_kv_get(const char * key,void * buffer,int * buffer_len)48 int aos_kv_get(const char *key, void *buffer, int *buffer_len)
49 {
50 return _kv_to_aos_res(kv_item_get(key, buffer, (int32_t *)buffer_len));
51 }
52
aos_kv_del(const char * key)53 int aos_kv_del(const char *key)
54 {
55 return _kv_to_aos_res(kv_item_delete(key));
56 }
57
aos_kv_del_by_prefix(const char * prefix)58 int aos_kv_del_by_prefix(const char *prefix)
59 {
60 return _kv_to_aos_res(kv_item_delete_by_prefix(prefix));
61 }
62
aos_kv_secure_set(const char * key,const void * value,int len,int sync)63 int aos_kv_secure_set(const char *key, const void *value, int len, int sync)
64 {
65 return _kv_to_aos_res(kv_item_secure_set(key, value, len));
66 }
67
aos_kv_secure_get(const char * key,void * buffer,int * buffer_len)68 int aos_kv_secure_get(const char *key, void *buffer, int *buffer_len)
69 {
70 return _kv_to_aos_res(kv_item_secure_get(key, buffer, (int32_t *)buffer_len));
71 }
72
73