4 * Created on: Dec 9, 2010
8 #include "DeviceConfig.hh"
15 #include "W1Configure.hh"
22 string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
24 ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) {
29 ConfigHandle::~ConfigHandle() {
30 uci_unload(_ctx, _pkg);
31 uci_free_context(_ctx);
34 DeviceConfig::DeviceConfig(string device_id_param) {
35 device_id = device_id_param;
36 uci_handle = load_device_config(device_id_param);
37 if (uci_handle != NULL) {
38 device_type = get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
41 log_error("Could not read device configuration.\n");
45 DeviceConfig::~DeviceConfig() {
46 if (uci_handle != NULL) {
52 void DeviceConfig::set_base_dir_name(string store_param) {
56 pos = store_param.find_last_of("/");
57 b_count = store_param.length();
58 if (pos == (b_count - 1)) {
59 store_base_dir = store_param;
62 store_base_dir = store_param + "/";
66 string DeviceConfig::get_base_dir_name() {
67 return store_base_dir;
70 string DeviceConfig::get_dir_name(string device_id_param) {
74 d_name = DeviceConfig::get_base_dir_name();
75 ret_val = W1Util::concat_paths(d_name, device_id_param);
79 string DeviceConfig::get_file_name(string device_id_param) {
83 fname = DEVICE_CONFIG__FILE_NAME;
84 ret_val = get_dir_name(device_id);
85 ret_val = W1Util::concat_paths(ret_val, fname);
89 string DeviceConfig::get_cfg_value(string key) {
90 struct uci_section *section;
91 struct uci_option *option;
94 if (uci_handle != NULL) {
95 section = uci_lookup_section(uci_handle->_ctx, uci_handle->_pkg, DEVICE_CONFIG__SECTION_NAME);
96 if (section != NULL) {
97 option = uci_lookup_option(uci_handle->_ctx, section, key.c_str());
99 switch (option->type) {
100 case UCI_TYPE_STRING:
101 //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
102 ret_val = option->v.string;
105 log_error("key: %s Failed to read parameter value\n", key.c_str());
110 log_error("key: %s Failed to read parameter value\n", key.c_str());
117 void DeviceConfig::set_cfg_value(string key, string value) {
121 cfg_dir = get_dir_name(device_id);
122 cfg_fl = DEVICE_CONFIG__FILE_NAME;
123 set_config_value(cfg_dir.c_str(),
125 DEVICE_CONFIG__SECTION_TYPE,
126 DEVICE_CONFIG__SECTION_NAME,
131 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
132 EnumSummaryCalculationType ret_val;
135 if (device_type.empty() == false) {
136 if (device_type.compare("Counter Device") == 0) {
143 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
145 struct uci_context *ctx;
146 struct uci_package *pkg;
149 ConfigHandle *ret_val;
152 cfg_dir = get_dir_name(device_id_param);
153 if (cfg_dir.empty() == false) {
154 if (access(cfg_dir.c_str(), W_OK) != 0) {
155 W1Util::mkdirs(cfg_dir.c_str());
157 cfg_fl = get_file_name(device_id_param);
158 if (access(cfg_fl.c_str(), R_OK) == 0) {
159 ctx = uci_alloc_context();
161 //log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
162 uci_set_confdir(ctx, cfg_dir.c_str());
163 err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
164 if (err_flg == UCI_OK) {
165 //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
166 ret_val = new ConfigHandle(ctx, pkg);
169 log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
170 set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
171 uci_free_context(ctx);
175 log_error("Failed to load device device configuration, memory allocation error.\n");
176 set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
180 log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str());