1 
2 #ifndef BES_KV_H
3 #define BES_KV_H
4 
5 
6 #if defined(__cplusplus)
7 extern "C" {
8 #endif
9 
10 #include "cmsis_os.h"
11 
12 #ifndef u8
13 #define u8  unsigned char
14 #endif
15 #ifndef u16
16 #define u16  unsigned short
17 #endif
18 #ifndef u32
19 #define u32  unsigned int
20 #endif
21 
22 #define  MAX_KEY_NUMBER  32
23 #define  MAX_NEME_LEN    32
24 #define  ALIAS_BASE      64
25 
26 /**
27  * Key-value item description
28  *
29  * key_name:    Name of this key, string type.
30  * real_value:  Real time value of this key, its value will be cleared after every time it is read.
31  * accu_value:  Accumulate value of this key.
32  * interval:    Interval time keys were dumped.
33  * alias:       Alise of the key_name, 0 for not used.
34  *
35  */
36 typedef struct _bes_kv_t
37 {
38     u8   key_name[MAX_NEME_LEN];
39     u32  real_value;
40     u32  accu_value;
41     u32  alias;
42 } bes_kv_t;
43 
44 /**
45  * struct bes_global_stat - Statistics of the golbal kv.
46  * @en: enable/disable the task which will output statistics results periodically
47  * @interval_sec: period time
48  * @thread_id: task's id.
49  * @bes_kv_t: struct _bes_kv_t.
50  *
51  */
52 typedef struct bes_global_stat {
53     u8     en;
54     u8     interval_sec;
55     osThreadId  thread_id;
56     bes_kv_t bes_key[MAX_KEY_NUMBER];
57 } bes_global_stat_t;
58 
59 
60 
61 /**
62  * @brief Initialize the kv module
63  *
64  * @retrun 0 on success, otherwise will be failed.
65  *
66  */
67 int bes_kv_init(void);
68 
69 /**
70  * Add the KV pair by its key.
71  *
72  * @param[in]  key     the the name of the KV pair.
73  *
74  * @return  0 on success, negative error on failure.
75  */
76 int bes_kv_add(char *key);
77 
78 /**
79  * Delete the KV pair by its key index.
80  *
81  * @param[in]  index  the index mapped to its key of the KV pair.
82  *
83  * @return  0 on success, negative error on failure.
84  */
85 int bes_kv_item_delete(int index);
86 
87 /**
88  * Add a new KV pair.
89  *
90  * @param[in]  index  the index mapped to its key of the KV pair.
91  * @param[in]  val    the value of the KV pair.
92  *
93  * @return  total lost data len.
94  */
95 int bes_kv_item_set(int index, u32 val);
96 
97 /**
98  * This function is used to enable/disable the statistics of the global kv pair.
99  *
100  * @param[in]	en Set to 1 to enable the statistics ,0 to disable it.
101  * @param[in]	interval_sec Time of the statistics in seconds.
102  * @return	0 ,OK; negative value,  ERROR
103  * @note	If enabled, the statistics information will be output throuth a default log uart.
104  */
105 int bes_kv_item_get(u8 en, u32 interval_sec);
106 
107 #if defined(__cplusplus)
108 }
109 #endif
110 
111 #endif /* BES_KV_H */
112