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;
99 if ((cfg_handle != NULL) &&
100 (cfg_handle->_ctx != NULL) &&
101 (cfg_handle->_pkg != NULL) &&
102 (section_type != NULL) &&
103 (section_name != NULL) &&
107 uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
108 tmp_sct = uci_to_section(elem);
109 if (strcmp(tmp_sct->type, section_type) == 0) {
115 log_debug("Creating new section %s to configuration file: %s\n", section_name, cfg_handle->full_fname);
116 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
117 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
118 err_flg = uci_create_named_section(cfg_handle,
122 if (err_flg == UCI_OK) {
123 uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
124 tmp_sct = uci_to_section(elem);
125 if (strcmp(tmp_sct->type, section_type) == 0) {
132 if (err_flg == UCI_OK) {
133 memset(&ptr, 0, sizeof(ptr));
134 ptr.package = cfg_handle->_pkg->e.name;
135 ptr.section = sct->e.name;
137 err_flg = uci_lookup_ptr(cfg_handle->_ctx, &ptr, NULL, false);
138 if (err_flg == UCI_OK) {
140 err_flg = uci_set(cfg_handle->_ctx, &ptr);
141 if (err_flg == UCI_OK) {
142 err_flg = uci_save(cfg_handle->_ctx, cfg_handle->_pkg);
143 if (err_flg == UCI_OK) {
145 log_debug("Set value to section %s in configuration file: %s\n", section_name, cfg_handle->full_fname);
148 log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", cfg_handle->full_fname);
152 log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", cfg_handle->full_fname);
156 log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n",
157 cfg_handle->full_fname,
158 cfg_handle->_pkg->e.name,
161 uci_free_context(cfg_handle->_ctx);
164 log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", cfg_handle->full_fname, section_name);
168 log_error("Failed to set value to configuration file, invalid parameters\n");
173 DeviceConfig::DeviceConfig(string device_id_param) {
174 device_id = device_id_param;
175 uci_handle = load_device_config(device_id_param);
176 if (uci_handle != NULL) {
177 device_type = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
180 log_error("Could not read device configuration.\n");
184 DeviceConfig::~DeviceConfig() {
185 if (uci_handle != NULL) {
191 void DeviceConfig::set_base_dir_name(string store_param) {
195 pos = store_param.find_last_of("/");
196 b_count = store_param.length();
197 if (pos == (b_count - 1)) {
198 store_base_dir = store_param;
201 store_base_dir = store_param + "/";
205 string DeviceConfig::get_base_dir_name() {
206 return store_base_dir;
209 string DeviceConfig::get_dir_name(string device_id_param) {
213 d_name = DeviceConfig::get_base_dir_name();
214 ret_val = FileUtil::concat_paths(d_name, device_id_param);
218 string DeviceConfig::get_file_name(string device_id_param) {
222 fname = DEVICE_CONFIG__FILE_NAME;
223 ret_val = get_dir_name(device_id_param);
224 ret_val = FileUtil::concat_paths(ret_val, fname);
228 string DeviceConfig::get_config_value(string key) {
229 struct uci_section *section;
230 struct uci_option *option;
233 if (uci_handle != NULL) {
234 section = uci_lookup_section(uci_handle->_ctx,
236 DEVICE_CONFIG__SECTION_NAME);
237 if (section != NULL) {
238 option = uci_lookup_option(uci_handle->_ctx, section, key.c_str());
239 if (option != NULL) {
240 switch (option->type) {
241 case UCI_TYPE_STRING:
242 //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
243 ret_val = option->v.string;
246 log_error("key: %s Failed to read parameter value\n", key.c_str());
251 log_error("key: %s Failed to read parameter value\n", key.c_str());
258 void DeviceConfig::set_config_value(string key,
263 cfg_dir = get_dir_name(device_id);
264 cfg_fl = DEVICE_CONFIG__FILE_NAME;
265 set_config_value_to_section(uci_handle,
266 DEVICE_CONFIG__SECTION_TYPE,
267 DEVICE_CONFIG__SECTION_NAME,
272 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
273 EnumSummaryCalculationType ret_val;
276 if (device_type.empty() == false) {
277 if (device_type.compare("Counter Device") == 0) {
284 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
286 struct uci_context *ctx;
287 struct uci_package *pkg;
292 ConfigHandle *ret_val;
295 cfg_dir = get_dir_name(device_id_param);
296 if (cfg_dir.empty() == false) {
297 if (access(cfg_dir.c_str(), W_OK) != 0) {
298 FileUtil::mkdirs(cfg_dir.c_str());
300 cfg_fl = get_file_name(device_id_param);
301 if (access(cfg_fl.c_str(), R_OK) == 0) {
302 ctx = uci_alloc_context();
304 log_debug("confdir: %s, file: %s\n", cfg_dir.c_str(), cfg_fl.c_str());
305 uci_set_confdir(ctx, cfg_dir.c_str());
306 err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
307 if (err_flg == UCI_OK) {
308 //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_param) {
339 DeviceConfig *ret_val;
341 ret_val = new DeviceConfig(device_id_param);