4 * Created on: Dec 9, 2010
8 #include "DeviceConfig.hh"
15 #include "DataReader.hh"
16 #include "FileUtil.hh"
21 string DeviceConfig::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
23 ConfigHandle::ConfigHandle(uci_context *ctx_param,
24 uci_package *pkg_param,
25 const char *short_fname_param,
26 const char *full_fname_param) {
29 short_fname = strdup(short_fname_param);
30 full_fname = strdup(full_fname_param);
33 ConfigHandle::~ConfigHandle() {
34 uci_unload(_ctx, _pkg);
35 uci_free_context(_ctx);
40 static int uci_create_named_section(ConfigHandle *cfg_handle,
41 const char *section_type,
42 const char *section_name,
43 bool save_immediately)
51 if ((cfg_handle != NULL) &&
52 (cfg_handle->_ctx != NULL) &&
53 (cfg_handle->short_fname != NULL) &&
54 (cfg_handle->full_fname != NULL) &&
55 (section_type != NULL) &&
56 (section_name != NULL)) {
57 len = strlen(cfg_handle->short_fname);
59 len = len + strlen(section_type);
61 len = len + strlen(section_name);
63 cmd_dta = (char *)malloc(len);
64 if (cmd_dta != NULL) {
68 cfg_handle->short_fname,
71 if (uci_lookup_ptr(cfg_handle->_ctx, &ptr, cmd_dta, true) == UCI_OK) {
72 ret_val = uci_set(cfg_handle->_ctx, &ptr);
73 if (ret_val == UCI_OK) {
74 if (save_immediately) {
75 ret_val = uci_save(cfg_handle->_ctx, ptr.p);
85 static bool set_config_value_to_section(ConfigHandle *cfg_handle,
86 const char *section_type,
87 const char *section_name,
90 struct uci_section *sct;
91 struct uci_section *tmp_sct;
93 struct uci_element *elem;
98 if ((cfg_handle != NULL) &&
99 (cfg_handle->_ctx != NULL) &&
100 (cfg_handle->_pkg != NULL) &&
101 (section_type != NULL) &&
102 (section_name != NULL) &&
106 uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
107 tmp_sct = uci_to_section(elem);
108 if (strcmp(tmp_sct->type, section_type) == 0) {
114 log_debug("Creating new section %s to configuration file: %s\n", section_name, cfg_handle->full_fname);
115 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
116 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
117 err_flg = uci_create_named_section(cfg_handle,
121 if (err_flg == UCI_OK) {
122 uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
123 tmp_sct = uci_to_section(elem);
124 if (strcmp(tmp_sct->type, section_type) == 0) {
131 if (err_flg == UCI_OK) {
132 memset(&ptr, 0, sizeof(ptr));
133 ptr.package = cfg_handle->_pkg->e.name;
134 ptr.section = sct->e.name;
136 err_flg = uci_lookup_ptr(cfg_handle->_ctx, &ptr, NULL, false);
137 if (err_flg == UCI_OK) {
139 err_flg = uci_set(cfg_handle->_ctx, &ptr);
140 if (err_flg == UCI_OK) {
141 err_flg = uci_save(cfg_handle->_ctx, cfg_handle->_pkg);
142 if (err_flg == UCI_OK) {
144 log_debug("Set value to section %s in configuration file: %s\n", section_name, cfg_handle->full_fname);
147 log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", cfg_handle->full_fname);
151 log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", cfg_handle->full_fname);
155 log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n",
156 cfg_handle->full_fname,
157 cfg_handle->_pkg->e.name,
160 uci_free_context(cfg_handle->_ctx);
163 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", cfg_handle->full_fname, section_name);
167 log_error("Failed to set value to configuration file, invalid parameters\n");
172 DeviceConfig::DeviceConfig(string device_id_param) {
173 device_id = device_id_param;
174 uci_handle = load_device_config(device_id_param);
175 if (uci_handle != NULL) {
176 device_type = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
179 log_error("Could not read device configuration.\n");
183 DeviceConfig::~DeviceConfig() {
184 if (uci_handle != NULL) {
190 void DeviceConfig::set_base_dir_name(string store_param) {
194 pos = store_param.find_last_of("/");
195 b_count = store_param.length();
196 if (pos == (b_count - 1)) {
197 store_base_dir = store_param;
200 store_base_dir = store_param + "/";
204 string DeviceConfig::get_base_dir_name() {
205 return store_base_dir;
208 string DeviceConfig::get_dir_name(string device_id_param) {
212 d_name = DeviceConfig::get_base_dir_name();
213 ret_val = FileUtil::concat_paths(d_name, device_id_param);
217 string DeviceConfig::get_file_name(string device_id_param) {
221 fname = DEVICE_CONFIG__FILE_NAME;
222 ret_val = get_dir_name(device_id_param);
223 ret_val = FileUtil::concat_paths(ret_val, fname);
227 string DeviceConfig::get_config_value(string key) {
228 struct uci_section *section;
229 struct uci_option *option;
232 if (uci_handle != NULL) {
233 section = uci_lookup_section(uci_handle->_ctx,
235 DEVICE_CONFIG__SECTION_NAME);
236 if (section != NULL) {
237 option = uci_lookup_option(uci_handle->_ctx, section, key.c_str());
238 if (option != NULL) {
239 switch (option->type) {
240 case UCI_TYPE_STRING:
241 //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
242 ret_val = option->v.string;
245 log_error("key: %s Failed to read parameter value\n", key.c_str());
250 log_error("key: %s Failed to read parameter value\n", key.c_str());
257 void DeviceConfig::set_config_value(string key,
262 cfg_dir = get_dir_name(device_id);
263 cfg_fl = DEVICE_CONFIG__FILE_NAME;
264 set_config_value_to_section(uci_handle,
265 DEVICE_CONFIG__SECTION_TYPE,
266 DEVICE_CONFIG__SECTION_NAME,
271 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
272 EnumSummaryCalculationType ret_val;
275 if (device_type.empty() == false) {
276 if (device_type.compare("Counter Device") == 0) {
283 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
285 struct uci_context *ctx;
286 struct uci_package *pkg;
289 ConfigHandle *ret_val;
292 cfg_dir = get_dir_name(device_id_param);
293 if (cfg_dir.empty() == false) {
294 if (access(cfg_dir.c_str(), W_OK) != 0) {
295 FileUtil::mkdirs(cfg_dir.c_str());
297 cfg_fl = get_file_name(device_id_param);
298 if (access(cfg_fl.c_str(), R_OK) == 0) {
299 ctx = uci_alloc_context();
301 //log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
302 uci_set_confdir(ctx, cfg_dir.c_str());
303 err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
304 if (err_flg == UCI_OK) {
305 //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
309 b_count = strlen(cfg_dir.c_str()) + strlen(cfg_fl.c_str()) + 10;
310 fname = (char *)calloc(1, b_count);
312 strncpy(fname, cfg_dir.c_str(), b_count);
313 strncat(fname, "/", 1);
314 strncat(fname, cfg_fl.c_str(), strlen(cfg_fl.c_str()) + 1);
316 ret_val = new ConfigHandle(ctx, pkg, cfg_fl.c_str(), fname);
321 log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
322 set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
323 uci_free_context(ctx);
327 log_error("Failed to load device device configuration, memory allocation error.\n");
328 set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
332 log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str());
338 DeviceConfig *DeviceConfig::get_device_config(string device_id) {
339 DeviceConfig *ret_val;
341 ret_val = new DeviceConfig(device_id);