1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef BES_KV_H 16 #define BES_KV_H 17 18 19 #if defined(__cplusplus) 20 extern "C" { 21 #endif 22 23 #include "cmsis_os.h" 24 25 #ifndef u8 26 #define u8 unsigned char 27 #endif 28 #ifndef u16 29 #define u16 unsigned short 30 #endif 31 //#ifndef u32 32 //#define u32 unsigned int 33 //#endif 34 35 #define MAX_KEY_NUMBER 32 36 #define MAX_NEME_LEN 32 37 #define ALIAS_BASE 64 38 39 /** 40 * Key-value item description 41 * 42 * key_name: Name of this key, string type. 43 * real_value: Real time value of this key, its value will be cleared after every time it is read. 44 * accu_value: Accumulate value of this key. 45 * interval: Interval time keys were dumped. 46 * alias: Alise of the key_name, 0 for not used. 47 * 48 */ 49 typedef struct _bes_kv_t 50 { 51 u8 key_name[MAX_NEME_LEN]; 52 u32 real_value; 53 u32 accu_value; 54 u32 alias; 55 } bes_kv_t; 56 57 /** 58 * struct bes_global_stat - Statistics of the golbal kv. 59 * @en: enable/disable the task which will output statistics results periodically 60 * @interval_sec: period time 61 * @thread_id: task's id. 62 * @bes_kv_t: struct _bes_kv_t. 63 * 64 */ 65 typedef struct bes_global_stat { 66 u8 en; 67 u8 interval_sec; 68 osThreadId thread_id; 69 bes_kv_t bes_key[MAX_KEY_NUMBER]; 70 } bes_global_stat_t; 71 72 73 74 /** 75 * @brief Initialize the kv module 76 * 77 * @retrun 0 on success, otherwise will be failed. 78 * 79 */ 80 int bes_kv_init(void); 81 82 /** 83 * Add the KV pair by its key. 84 * 85 * @param[in] key the the name of the KV pair. 86 * 87 * @return 0 on success, negative error on failure. 88 */ 89 int bes_kv_add(char *key); 90 91 /** 92 * Delete the KV pair by its key index. 93 * 94 * @param[in] index the index mapped to its key of the KV pair. 95 * 96 * @return 0 on success, negative error on failure. 97 */ 98 int bes_kv_item_delete(int index); 99 100 /** 101 * Add a new KV pair. 102 * 103 * @param[in] index the index mapped to its key of the KV pair. 104 * @param[in] val the value of the KV pair. 105 * 106 * @return total lost data len. 107 */ 108 int bes_kv_item_set(int index, u32 val); 109 110 /** 111 * This function is used to enable/disable the statistics of the global kv pair. 112 * 113 * @param[in] en Set to 1 to enable the statistics ,0 to disable it. 114 * @param[in] interval_sec Time of the statistics in seconds. 115 * @return 0 ,OK; negative value, ERROR 116 * @note If enabled, the statistics information will be output throuth a default log uart. 117 */ 118 int bes_kv_item_get(u8 en, u32 interval_sec); 119 120 #if defined(__cplusplus) 121 } 122 #endif 123 124 #endif /* BES_KV_H */ 125