/* * DeviceConfig.cc * * Created on: Dec 9, 2010 * Author: lamikr */ #include "DeviceConfig.hh" #include #include #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; long DeviceConfig::read_interval_seconds = 600; 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) { 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"); } } 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; } } void DeviceConfig::set_base_dir_name(string store_param) { int pos; int b_count; pos = store_param.find_last_of("/"); b_count = store_param.length(); if (pos == (b_count - 1)) { store_base_dir = store_param; } else { store_base_dir = 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_config_path_name() { string ret_val; string path_name; path_name = DeviceConfig::get_base_dir_name(); ret_val = FileUtil::concat_paths(path_name, device_id); return ret_val; } string DeviceConfig::get_config_file_name() { string ret_val; string fname_base; 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_pathless_config_file_name() { return DEVICE_CONFIG__FILE_NAME; } 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; } bool DeviceConfig::set_config_value(string key, string value, bool save_immediately) { string cfg_dir; string cfg_fl; bool ret_val; cfg_dir = get_config_path_name(); cfg_fl = DEVICE_CONFIG__FILE_NAME; 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() { if (device_type.empty() == false) { if (device_type.compare("Counter Device") == 0) { return DELTA; } } return MEAN; } ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { int err_flg; struct uci_context *ctx; struct uci_package *pkg; string fname_base; string fname_full; string cfg_dir; ConfigHandle *ret_val; FILE *fp; ret_val = NULL; cfg_dir = get_config_path_name(); if (cfg_dir.empty() == false) { 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("configuration file: %s\n", fname_full.c_str()); uci_set_confdir(ctx, cfg_dir.c_str()); 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()); 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", 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 configuration, memory allocation error.\n"); fp = fopen(fname_full.c_str(), "w+"); if (fp != NULL) fclose(fp); } } else { 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_param) { DeviceConfig *ret_val; ret_val = new DeviceConfig(device_id_param); return ret_val; }