* Copyright (c) 2022 Huawei Technologies Co.,Ltd.
*
* CBB is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*
* cm_config.h
*
*
* IDENTIFICATION
* src/cm_utils/cm_config.h
*
* -------------------------------------------------------------------------
*/
#ifndef __CM_CONFIG_H__
#define __CM_CONFIG_H__
#include "cm_defs.h"
#include "cm_text.h"
#include "cm_file.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CM_CONFIG_HASH_BUCKETS 512
#define CM_CONFIG_ALIAS_HASH_BUCKETS 30
typedef status_t (*config_verify_t)(void *lex, void *def);
typedef status_t (*config_notify_t)(void *se, void *item, char *value);
typedef enum en_config_effect {
EFFECT_IMMEDIATELY = 0,
EFFECT_RECONNECT = 1,
EFFECT_REBOOT = 2
} config_effect_t;
typedef enum en_config_scale {
CFG_DB = 0,
CFG_INS = 1,
CFG_SE = 2,
} config_scale_t;
typedef enum en_config_flag {
FLAG_NONE = 0,
FLAG_ZFILE = 1,
FLAG_INFILE = 2,
} config_flag_t;
typedef enum en_config_attr {
ATTR_NONE = 0x0,
ATTR_READONLY = 0x1,
ATTR_HIDDEN = 0x2,
} config_attr_t;
typedef struct st_config_item {
char *name;
bool32 is_default;
uint32 attr;
char *default_value;
char *value;
char *runtime_value;
char *description;
char *range;
char *datatype;
char *comment;
uint32 id;
config_effect_t effect;
config_scale_t scale;
config_verify_t verify;
config_notify_t notify;
config_notify_t notify_pfile;
char *alias;
struct st_config_item *next;
struct st_config_item *hash_next;
struct st_config_item *hash_next2;
struct st_config_item *next_file;
config_flag_t flag;
char *pfile_value;
bool32 is_diff;
bool32 hit_alias;
} config_item_t;
typedef struct st_config {
int32 file;
char file_name[CM_FILE_NAME_BUFFER_SIZE];
char file_buf[CM_MAX_CONFIG_FILE_SIZE];
char *value_buf;
uint32 value_offset;
uint32 value_buf_size;
uint32 text_size;
uint32 item_count;
config_item_t *items;
config_item_t *first_item;
config_item_t *last_item;
config_item_t *name_map[CM_CONFIG_HASH_BUCKETS];
config_item_t *alias_map[CM_CONFIG_ALIAS_HASH_BUCKETS];
config_item_t *first_file;
config_item_t *last_file;
bool32 ignore;
} config_t;
typedef struct st_config_stream {
config_t *config;
uint32 offset;
} config_stream_t;
typedef enum en_config_scope {
CONFIG_SCOPE_MEMORY = 1,
CONFIG_SCOPE_DISK = 2,
CONFIG_SCOPE_BOTH = 3,
} config_scope_t;
void cm_init_config(config_item_t *items, uint32 item_count, config_t *config);
status_t cm_load_config(
config_item_t *items, uint32 item_count, const char *file_name, config_t *config, bool32 set_alias);
char *cm_get_config_value(const config_t *config, const char *name);
status_t cm_read_config(const char *file_name, config_t *config);
void cm_free_config_buf(config_t *config);
config_item_t *cm_get_config_item(const config_t *config, text_t *name, bool32 set_alias);
status_t cm_set_config_item(
config_t *config, text_t *name, text_t *value, text_t *comment, bool32 is_infile, bool32 set_alias);
status_t cm_save_config(config_t *config);
status_t cm_alter_config(config_t *config, const char *name, const char *value, config_scope_t scope, bool32 force);
#ifdef __cplusplus
}
#endif
#endif