X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src%2FDeviceConfig.cc;h=ee06135a4270cc4594c0a2d6770a5ae18775c87b;hb=83dba70b46600014932da34e8744ba70584077aa;hp=aa191cc009cebd7f1b89d43a86d35349e3485955;hpb=9c47855d3ddc3d09b9733c43d420a7d5b739bd37;p=lib1wire.git diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index aa191cc..ee06135 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -7,20 +7,167 @@ #include "DeviceConfig.hh" +#include +#include + +#include +#include "W1Util.hh" +#include "W1Configure.hh" + +#include "Factory.hh" + using namespace std; using namespace w1; +string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR; + +ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) { + ctx = ctx_param; + pkg = pkg_param; +} + +ConfigHandle::~ConfigHandle() { + uci_unload(ctx, pkg); + uci_free_context(ctx); +} + DeviceConfig::DeviceConfig(string device_id_param) { 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); + } + else { + log_error("Could not read device config\n"); + } } DeviceConfig::~DeviceConfig() { + if (uci_handle != NULL) { + 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 + "/"; + } +} + +string DeviceConfig::get_base_dir_name() { + return store_base_dir; +} + +string DeviceConfig::get_dir_name(string device_id_param) { + string ret_val; + string d_name; + + d_name = DeviceConfig::get_base_dir_name(); + ret_val = W1Util::concat_paths(d_name, device_id_param); + return ret_val; +} + +string DeviceConfig::get_file_name(string device_id_param) { + string ret_val; + string fname; + + fname = DEVICE_CONFIG__FILE_NAME; + ret_val = get_dir_name(device_id); + ret_val = W1Util::concat_paths(ret_val, fname); + return ret_val; +} + +string DeviceConfig::get_cfg_value(string key) { + struct uci_section *section; + struct uci_option *option; + string ret_val; + + 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()); + switch (option->type) { + case UCI_TYPE_STRING: + log_info("config file: 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("config file: key: %s can not parse parameter value\n", key.c_str()); + break; + } + } + } + return ret_val; } -string DeviceConfig::get_config_value(string key) { - return NULL; +void DeviceConfig::set_cfg_value(string key, string value) { + string cfg_dir; + string cfg_fl; + + cfg_dir = get_dir_name(device_id); + 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()); } enum_summary_calculation DeviceConfig::get_summary_calculation_type() { - return MEAN; + enum_summary_calculation ret_val; + + ret_val = MEAN; + if (device_type.empty() == false) { + if (device_type.compare("counter") == 0) { + ret_val = DELTA; + } + } + return ret_val;; +} + +ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { + int err_flg; + struct uci_context *ctx; + struct uci_package *pkg; + string cfg_fl; + string cfg_dir; + ConfigHandle *ret_val; + + ret_val = NULL; + cfg_dir = get_dir_name(device_id_param); + if (cfg_dir.empty() == false) { + cfg_fl = get_file_name(device_id_param); + ctx = uci_alloc_context(); + if (ctx != NULL) { + log_debug("uci_set_confdir: %s\n", cfg_dir.c_str()); + uci_set_confdir(ctx, cfg_dir.c_str()); + if (access(cfg_fl.c_str(), R_OK) != 0) { + log_debug("loading file: %s\n", cfg_fl.c_str()); + err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg); + if (err_flg == UCI_OK) { + log_debug("Loaded device configuration: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg); + ret_val = new ConfigHandle(ctx, pkg); + } + else { + log_debug("Failed to load file: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg); + set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "unknowntype"); + } + } + } + else { + log_error("Failed to load device device configurations, invalid device id: %s.\n", cfg_dir.c_str()); + } + } + return ret_val; }