4 * Created on: Dec 9, 2010
8 #include "DeviceConfig.hh"
13 #include "DataReader.hh"
14 #include "FileUtil.hh"
18 #include "private/uci_config.h"
24 string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
25 long DeviceConfig::read_interval_seconds = 600;
27 ConfigHandle::ConfigHandle(uci_context *ctx_param,
28 uci_package *pkg_param,
29 const char *short_fname_param,
30 const char *full_fname_param) {
33 short_fname = strdup(short_fname_param);
34 full_fname = strdup(full_fname_param);
37 ConfigHandle::~ConfigHandle() {
38 uci_unload(_ctx, _pkg);
39 uci_free_context(_ctx);
44 DeviceConfig::DeviceConfig(string device_id_param) {
47 device_id = device_id_param;
48 uci_handle = load_device_config(device_id_param);
49 if (uci_handle != NULL) {
50 succ = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, device_type);
52 log_error("Could not read device type from the configuration file.\n");
56 log_error("Could not read device configuration.\n");
60 DeviceConfig::~DeviceConfig() {
63 if (uci_handle != NULL) {
64 suc = uci_save_config_values(uci_handle->_ctx,
67 log_debug("saved the configuration file: %s\n", uci_handle->full_fname);
74 void DeviceConfig::set_base_dir_name(string store_param) {
78 pos = store_param.find_last_of("/");
79 b_count = store_param.length();
80 if (pos == (b_count - 1)) {
81 store_base_dir = store_param;
84 store_base_dir = store_param + "/";
88 long DeviceConfig::get_read_interval_seconds() {
89 return read_interval_seconds;
92 void DeviceConfig::set_read_interval_seconds(long seconds_param) {
93 read_interval_seconds = seconds_param;
96 string DeviceConfig::get_base_dir_name() {
97 return store_base_dir;
100 string DeviceConfig::get_config_path_name() {
104 path_name = DeviceConfig::get_base_dir_name();
105 ret_val = FileUtil::concat_paths(path_name, device_id);
109 string DeviceConfig::get_config_file_name() {
113 fname_base = DEVICE_CONFIG__FILE_NAME;
114 ret_val = get_config_path_name();
115 ret_val = FileUtil::concat_paths(ret_val, fname_base);
119 string DeviceConfig::get_pathless_config_file_name() {
120 return DEVICE_CONFIG__FILE_NAME;
123 bool DeviceConfig::get_config_value(string key, string& value) {
130 if ((uci_handle != NULL) &&
131 (uci_handle->_ctx != NULL) &&
132 (uci_handle->_pkg != NULL)) {
133 if (key.empty() == false) {
134 ret = uci_get_config_value(uci_handle->_ctx,
136 DEVICE_CONFIG__SECTION_NAME,
139 if ((err_flg == PLP_OK) &&
146 log_error("Failed to read configuration value, key was empty string.\n");
150 log_error("Failed to read configuration value for key: %s. Invalid handle to configuration file.\n", key.c_str());
155 bool DeviceConfig::set_config_value(string key,
157 bool save_immediately) {
163 cfg_dir = get_config_path_name();
164 cfg_fl = DEVICE_CONFIG__FILE_NAME;
165 if (uci_handle != NULL) {
166 if ((uci_handle->_ctx != NULL) &&
167 (uci_handle->_pkg != NULL) &&
168 (uci_handle->short_fname != NULL) &&
169 (uci_handle->full_fname != NULL)) {
170 //log_debug("uci_handle != null, short_name: %s, full_name: %s\n",
171 // uci_handle->short_fname, uci_handle->full_fname);
172 //log_debug("key: %s, value: %s\n", key.c_str(), value.c_str());
173 ret_val = uci_set_config_value(uci_handle->_ctx,
175 uci_handle->short_fname,
176 uci_handle->full_fname,
177 DEVICE_CONFIG__SECTION_TYPE,
178 DEVICE_CONFIG__SECTION_NAME,
184 log_error("Could not set config value for device %s: key: %s, value: %s. Invalid filename in handle.\n",
185 device_id.c_str(), key.c_str(), value.c_str());
189 log_error("Could not set config value for device %s: key: %s, value: %s. Invalid handle to config file\n",
190 device_id.c_str(), key.c_str(), value.c_str());
195 // TODO: This function should be moved else where or it should be dynamic...
196 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
197 if (device_type.empty() == false) {
198 if (device_type.compare("Counter Device") == 0) {
205 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
207 struct uci_context *ctx;
208 struct uci_package *pkg;
212 ConfigHandle *ret_val;
216 cfg_dir = get_config_path_name();
217 if (cfg_dir.empty() == false) {
218 fname_full = get_config_file_name();
219 if (FileUtil::file_exist(fname_full.c_str(), false) == false)
220 FileUtil::mkfile(fname_full.c_str(), false);
221 if (access(fname_full.c_str(), R_OK) == 0) {
222 ctx = uci_alloc_context();
224 //log_debug("configuration file: %s\n", fname_full.c_str());
225 uci_set_confdir(ctx, cfg_dir.c_str());
226 err_flg = uci_load(ctx, fname_full.c_str(), &pkg);
227 if (err_flg == UCI_OK) {
228 //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
229 fname_base = get_pathless_config_file_name();
230 ret_val = new ConfigHandle(ctx,
236 log_debug("Failed to load device configuration: %s, err code: %d.\n", fname_full.c_str(), UCI_OK);
237 fp = fopen(fname_full.c_str(), "w+");
240 //uci_free_context(ctx);
244 log_error("Failed to load device configuration, memory allocation error.\n");
245 fp = fopen(fname_full.c_str(), "w+");
251 log_error("Failed to load device configuration, file does not exist: %s.\n", fname_full.c_str());
257 DeviceConfig *DeviceConfig::get_device_config(string device_id_param) {
258 DeviceConfig *ret_val;
260 ret_val = new DeviceConfig(device_id_param);