#include "DeviceConfig.hh"
+#include <string.h>
+#include <malloc.h>
+
+#include <plp/log.h>
+#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_config_value(string key) {
- return NULL;
+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;
}
-enum_summary_calculation DeviceConfig::get_summary_calculation_type() {
- return MEAN;
+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("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;
+ }
+ }
+ }
+ return ret_val;
+}
+
+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());
+}
+
+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 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) {
+ if (access(cfg_dir.c_str(), W_OK) != 0) {
+ W1Util::mkdirs(cfg_dir.c_str());
+ }
+ cfg_fl = get_file_name(device_id_param);
+ if (access(cfg_fl.c_str(), R_OK) == 0) {
+ 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());
+ err_flg = uci_load(ctx, cfg_fl.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);
+ }
+ 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);
+ }
+ }
+ else {
+ log_error("Failed to load device device configuration, memory allocation error.\n");
+ set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
+ }
+ }
+ else {
+ log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str());
+ }
+ }
+ return ret_val;
}