X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src%2FDeviceConfig.cc;h=75af9f396579b0adad82b6ce9144120a6c88d473;hb=860d8c023b82eeca9833d4a3f534c6d11a9523f3;hp=904257ec7f229743831c97a4d1409062b7478039;hpb=a8f0802c22beeb4b9b3418c11069082e02fa30ce;p=libplp.git diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index 904257e..75af9f3 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -10,31 +10,46 @@ #include #include -#include "log.h" - #include "DataReader.hh" #include "FileUtil.hh" +#include "log.h" +#include "config.h" +#include "private/uci_config.h" + using namespace std; using namespace plp; -string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR; +string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR; +long DeviceConfig::read_interval_seconds = 600; -ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) { - _ctx = ctx_param; - _pkg = pkg_param; +ConfigHandle::ConfigHandle(uci_context *ctx_param, + uci_package *pkg_param, + const char *short_fname_param, + const char *full_fname_param) { + _ctx = ctx_param; + _pkg = pkg_param; + short_fname = strdup(short_fname_param); + full_fname = strdup(full_fname_param); } ConfigHandle::~ConfigHandle() { uci_unload(_ctx, _pkg); uci_free_context(_ctx); + free(short_fname); + free(full_fname); } DeviceConfig::DeviceConfig(string device_id_param) { + bool succ; + device_id = device_id_param; uci_handle = load_device_config(device_id_param); if (uci_handle != NULL) { - device_type = get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE); + succ = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, device_type); + if (succ == false) { + log_error("Could not read device type from the configuration file.\n"); + } } else { log_error("Could not read device configuration.\n"); @@ -42,7 +57,14 @@ DeviceConfig::DeviceConfig(string device_id_param) { } DeviceConfig::~DeviceConfig() { + bool suc; + if (uci_handle != NULL) { + suc = uci_save_config_values(uci_handle->_ctx, + uci_handle->_pkg); + if (suc == true) { + log_debug("saved the configuration file: %s\n", uci_handle->full_fname); + } delete(uci_handle); uci_handle = NULL; } @@ -62,129 +84,174 @@ void DeviceConfig::set_base_dir_name(string store_param) { } } +long DeviceConfig::get_read_interval_seconds() { + return read_interval_seconds; +} + +void DeviceConfig::set_read_interval_seconds(long seconds_param) { + read_interval_seconds = seconds_param; +} + string DeviceConfig::get_base_dir_name() { return store_base_dir; } -string DeviceConfig::get_dir_name(string device_id_param) { +string DeviceConfig::get_config_path_name() { string ret_val; - string d_name; + string path_name; - d_name = DeviceConfig::get_base_dir_name(); - ret_val = FileUtil::concat_paths(d_name, device_id_param); + path_name = DeviceConfig::get_base_dir_name(); + ret_val = FileUtil::concat_paths(path_name, device_id); return ret_val; } -string DeviceConfig::get_file_name(string device_id_param) { +string DeviceConfig::get_config_file_name() { string ret_val; - string fname; + string fname_base; - fname = DEVICE_CONFIG__FILE_NAME; - ret_val = get_dir_name(device_id_param); - ret_val = FileUtil::concat_paths(ret_val, fname); + fname_base = DEVICE_CONFIG__FILE_NAME; + ret_val = get_config_path_name(); + ret_val = FileUtil::concat_paths(ret_val, fname_base); return ret_val; } -string DeviceConfig::get_cfg_value(string key) { - struct uci_section *section; - struct uci_option *option; - string ret_val; +string DeviceConfig::get_pathless_config_file_name() { + return DEVICE_CONFIG__FILE_NAME; +} - if (uci_handle != NULL) { - section = uci_lookup_section(uci_handle->_ctx, uci_handle->_pkg, DEVICE_CONFIG__SECTION_NAME); - if (section != NULL) { - option = uci_lookup_option(uci_handle->_ctx, section, key.c_str()); - if (option != NULL) { - switch (option->type) { - case UCI_TYPE_STRING: - //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string); - ret_val = option->v.string; - break; - default: - log_error("key: %s Failed to read parameter value\n", key.c_str()); - break; - } - } - else { - log_error("key: %s Failed to read parameter value\n", key.c_str()); +bool DeviceConfig::get_config_value(string key, string& value) { + char *ret; + bool ret_val; + + ret_val = false; + value.clear(); + if ((uci_handle != NULL) && + (uci_handle->_ctx != NULL) && + (uci_handle->_pkg != NULL)) { + if (key.empty() == false) { + ret = uci_get_config_value(uci_handle->_ctx, + uci_handle->_pkg, + DEVICE_CONFIG__SECTION_NAME, + key.c_str()); + if (ret != NULL) { + ret_val = true; + value = ret; } } + else { + log_error("Failed to read configuration value, key was empty string.\n"); + } + } + else { + log_error("Failed to read configuration value for key: %s. Invalid handle to configuration file.\n", key.c_str()); } return ret_val; } -void DeviceConfig::set_cfg_value(string key, string value) { +bool DeviceConfig::set_config_value(string key, + string value, + bool save_immediately) { string cfg_dir; string cfg_fl; + bool ret_val; - cfg_dir = get_dir_name(device_id); + cfg_dir = get_config_path_name(); cfg_fl = DEVICE_CONFIG__FILE_NAME; - set_config_value(cfg_dir.c_str(), - cfg_fl.c_str(), - DEVICE_CONFIG__SECTION_TYPE, - DEVICE_CONFIG__SECTION_NAME, - key.c_str(), - value.c_str()); + if (uci_handle != NULL) { + if ((uci_handle->_ctx != NULL) && + (uci_handle->_pkg != NULL) && + (uci_handle->short_fname != NULL) && + (uci_handle->full_fname != NULL)) { + //log_debug("uci_handle != null, short_name: %s, full_name: %s\n", + // uci_handle->short_fname, uci_handle->full_fname); + //log_debug("key: %s, value: %s\n", key.c_str(), value.c_str()); + ret_val = uci_set_config_value(uci_handle->_ctx, + uci_handle->_pkg, + uci_handle->short_fname, + uci_handle->full_fname, + DEVICE_CONFIG__SECTION_TYPE, + DEVICE_CONFIG__SECTION_NAME, + key.c_str(), + value.c_str(), + save_immediately); + } + else { + log_error("Could not set config value for device %s: key: %s, value: %s. Invalid filename in handle.\n", + device_id.c_str(), key.c_str(), value.c_str()); + } + } + else { + log_error("Could not set config value for device %s: key: %s, value: %s. Invalid handle to config file\n", + device_id.c_str(), key.c_str(), value.c_str()); + } + return ret_val; } +// TODO: This function should be moved else where or it should be dynamic... EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() { - EnumSummaryCalculationType ret_val; - - ret_val = MEAN; if (device_type.empty() == false) { if (device_type.compare("Counter Device") == 0) { - ret_val = DELTA; + return DELTA; } } - return ret_val;; + return MEAN; } ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { int err_flg; struct uci_context *ctx; struct uci_package *pkg; - string cfg_fl; + string fname_base; + string fname_full; string cfg_dir; ConfigHandle *ret_val; + FILE *fp; ret_val = NULL; - cfg_dir = get_dir_name(device_id_param); + cfg_dir = get_config_path_name(); if (cfg_dir.empty() == false) { - if (access(cfg_dir.c_str(), W_OK) != 0) { - FileUtil::mkdirs(cfg_dir.c_str()); - } - cfg_fl = get_file_name(device_id_param); - if (access(cfg_fl.c_str(), R_OK) == 0) { + fname_full = get_config_file_name(); + if (FileUtil::file_exist(fname_full.c_str(), false) == false) + FileUtil::mkfile(fname_full.c_str(), false); + if (access(fname_full.c_str(), R_OK) == 0) { ctx = uci_alloc_context(); if (ctx != NULL) { - //log_debug("uci_set_confdir: %s\n", cfg_dir.c_str()); + log_debug("configuration file: %s\n", fname_full.c_str()); uci_set_confdir(ctx, cfg_dir.c_str()); - err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg); + err_flg = uci_load(ctx, fname_full.c_str(), &pkg); if (err_flg == UCI_OK) { //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str()); - ret_val = new ConfigHandle(ctx, pkg); + fname_base = get_pathless_config_file_name(); + ret_val = new ConfigHandle(ctx, + pkg, + fname_base.c_str(), + fname_full.c_str()); } else { - log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK); - set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, ""); - uci_free_context(ctx); + log_debug("Failed to load device configuration: %s, err code: %d.\n", fname_full.c_str(), UCI_OK); + fp = fopen(fname_full.c_str(), "w+"); + if (fp != NULL) + fclose(fp); + //uci_free_context(ctx); } } else { - log_error("Failed to load device device configuration, memory allocation error.\n"); - set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, ""); + log_error("Failed to load device configuration, memory allocation error.\n"); + fp = fopen(fname_full.c_str(), "w+"); + if (fp != NULL) + fclose(fp); } } else { - log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str()); + log_error("Failed to load device configuration, file does not exist: %s.\n", fname_full.c_str()); } } return ret_val; } -DeviceConfig *DeviceConfig::get_device_config(string device_id) { +DeviceConfig *DeviceConfig::get_device_config(string device_id_param) { DeviceConfig *ret_val; - ret_val = new DeviceConfig(device_id); + ret_val = new DeviceConfig(device_id_param); return ret_val; }