1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 #if AOS_COMP_CLI
5 #include <stdint.h>
6 #include "kv.h"
7 #include "kv_adapt.h"
8 #include "kv_types.h"
9 #include "aos/cli.h"
10 
11 extern kv_item_t *kv_item_traverse(item_func func, uint8_t blk_idx, const char *key);
12 extern int32_t cli_printf(const char *fmt, ...);
13 
__item_print_cb(kv_item_t * item,const char * key)14 static int32_t __item_print_cb(kv_item_t *item, const char *key)
15 {
16     kv_size_t off;
17 
18     char *p_key = NULL;
19     char *p_val = NULL;
20 
21     p_key = (char *)kv_malloc(item->hdr.key_len + 1);
22     if (!p_key) {
23         return KV_ERR_MALLOC_FAILED;
24     }
25 
26     memset(p_key, 0, item->hdr.key_len + 1);
27     off = item->pos + KV_ITEM_HDR_SIZE;
28     kv_flash_read(off, p_key, item->hdr.key_len);
29 
30     p_val = (char *)kv_malloc(item->hdr.val_len + 1);
31     if (!p_val) {
32         kv_free(p_key);
33         return KV_ERR_MALLOC_FAILED;
34     }
35 
36     memset(p_val, 0, item->hdr.val_len + 1);
37     off = item->pos + KV_ITEM_HDR_SIZE + item->hdr.key_len;
38     kv_flash_read(off, p_val, item->hdr.val_len);
39 
40     cli_printf("%s = %s\r\n", p_key, p_val);
41     kv_free(p_key);
42     kv_free(p_val);
43 
44     return KV_LOOP_CONTINUE;
45 }
46 
handle_kv_cmd(char * pwbuf,int blen,int argc,char ** argv)47 static void handle_kv_cmd(char *pwbuf, int blen, int argc, char **argv)
48 {
49     int i   = 0;
50     int num = 0;
51     int res = KV_OK;
52     int len = KV_MAX_VAL_LEN;
53 
54     char *buffer = NULL;
55 
56     const char *rtype = argc > 1 ? argv[1] : "";
57 
58     if (strcmp(rtype, "set") == KV_OK) {
59         if (argc != 4) {
60             return;
61         }
62 
63         res = kv_item_set(argv[2], argv[3], strlen(argv[3]));
64         if (res != KV_OK) {
65             cli_printf("cli set kv failed %d.\r\n", res);
66         }
67     } else if (strcmp(rtype, "get") == KV_OK) {
68         if (argc != 3) {
69             return;
70         }
71 
72         buffer = (char *)kv_malloc(KV_MAX_VAL_LEN);
73         if (!buffer) {
74             cli_printf("there is no space\r\n");
75             return;
76         }
77 
78         memset(buffer, 0, KV_MAX_VAL_LEN);
79         res = kv_item_get(argv[2], buffer, (int32_t *)&len);
80         if (res != 0) {
81             cli_printf("cli: no paired kv\r\n");
82         } else {
83             cli_printf("value is %s\r\n", buffer);
84         }
85 
86         if (buffer) {
87             kv_free(buffer);
88         }
89     } else if (strcmp(rtype, "del") == KV_OK) {
90         if (argc != 3) {
91             return;
92         }
93 
94         res = kv_item_delete(argv[2]);
95         if (res != KV_OK) {
96             cli_printf("cli kv del failed %d\r\n", res);
97         }
98     } else if (strcmp(rtype, "list") == KV_OK) {
99         for (i = 0; i < KV_BLOCK_NUMS; i++) {
100             kv_item_traverse(__item_print_cb, i, NULL);
101         }
102     } else if (strcmp(rtype, "seti") == KV_OK) {
103         if (argc != 4) {
104             return;
105         }
106 
107         num = atoi(argv[3]);
108         res = kv_item_set(argv[2], (void *)(&num), sizeof(int));
109         if (res != KV_OK) {
110             cli_printf("cli set integer kv failed %d.\r\n", res);
111         }
112     } else if (strcmp(rtype, "geti") == KV_OK) {
113         num = 0;
114         len = sizeof(int);
115 
116         if (argc != 3) {
117             return;
118         }
119 
120         res = kv_item_get(argv[2], &num, (int32_t *)&len);
121         if (res != 0) {
122             cli_printf("cli: no paired kv\r\n");
123         } else {
124             cli_printf("value is %d\r\n", num);
125         }
126     } else if (strcmp(rtype, "clr") == KV_OK) {
127         cli_printf("kv list before clear:\r\n", res);
128         for (i = 0; i < KV_BLOCK_NUMS; i++) {
129             kv_item_traverse(__item_print_cb, i, NULL);
130         }
131         kv_item_delete_by_prefix(NULL);
132         cli_printf("kv clear success!\r\n", res);
133     }
134 
135     return;
136 }
137 
138 static struct cli_command kv_cmd = {
139     "kv", "kv [set key value | get key | del key | seti key int_val | geti key | list | clr]", handle_kv_cmd
140 };
141 
kv_register_cli_command(void)142 void kv_register_cli_command(void)
143 {
144     aos_cli_register_command(&kv_cmd);
145 }
146 
147 #endif /* AOS_COMP_CLI */
148 
149