]> pilppa.org Git - libplp.git/blobdiff - src/DeviceConfig.cc
api fixes and cleanups
[libplp.git] / src / DeviceConfig.cc
index 66fe863be28c17766269f3203c88807d46ade757..75af9f396579b0adad82b6ce9144120a6c88d473 100644 (file)
 #include <string.h>
 #include <malloc.h>
 
-#include "log.h"
-
 #include "DataReader.hh"
 #include "FileUtil.hh"
 
+#include "log.h"
+#include "config.h"
+#include "private/uci_config.h"
+
 using namespace std;
 using namespace plp;
 
@@ -38,144 +40,16 @@ ConfigHandle::~ConfigHandle() {
        free(full_fname);
 }
 
-static int uci_create_named_section(ConfigHandle *cfg_handle,
-                               const char *section_type,
-                               const char *section_name,
-                               bool save_immediately)
-{
-       struct uci_ptr  ptr;
-       int             ret_val;
-       char            *cmd_dta;
-       int             len;
-
-       ret_val = -1;
-       if ((cfg_handle != NULL) &&
-           (cfg_handle->_ctx != NULL) &&
-           (cfg_handle->short_fname != NULL) &&
-           (cfg_handle->full_fname != NULL) &&
-           (section_type != NULL) &&
-           (section_name != NULL)) {
-               len     = strlen(cfg_handle->short_fname);
-               len     = len + 1;
-               len     = len + strlen(section_type);
-               len     = len + 1;
-               len     = len + strlen(section_name);
-               len     = len + 1;
-               cmd_dta = (char *)malloc(len);
-               if (cmd_dta != NULL) {
-                       snprintf(cmd_dta,
-                               len,
-                               "%s.%s=%s",
-                               cfg_handle->short_fname,
-                               section_name,
-                               section_type);
-                       if (uci_lookup_ptr(cfg_handle->_ctx, &ptr, cmd_dta, true) == UCI_OK) {
-                               ret_val = uci_set(cfg_handle->_ctx, &ptr);
-                               if (ret_val == UCI_OK) {
-                                       if (save_immediately) {
-                                               ret_val = uci_save(cfg_handle->_ctx, ptr.p);
-                                       }
-                               }
-                       }
-                       free(cmd_dta);
-               }
-       }
-       return ret_val;
-}
-
-static bool set_config_value_to_section(ConfigHandle *cfg_handle,
-                       const char *section_type,
-                       const char *section_name,
-                       const char *key,
-                       const char *value) {
-       struct uci_section      *sct;
-       struct uci_section      *tmp_sct;
-       int                     err_flg;
-       struct uci_element      *elem;
-       struct uci_ptr          ptr;
-       bool                    ret_val;
-
-       ret_val = false;
-       err_flg = UCI_OK;
-       if ((cfg_handle != NULL) &&
-           (cfg_handle->_ctx != NULL) &&
-           (cfg_handle->_pkg != NULL) &&
-           (section_type != NULL) &&
-           (section_name != NULL) &&
-           (key != NULL) &&
-           (value != NULL)) {
-               sct     = NULL;
-               uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
-                       tmp_sct = uci_to_section(elem);
-                       if (strcmp(tmp_sct->type, section_type) == 0) {
-                               sct     = tmp_sct;
-                               break;
-                       }
-               }
-               if (sct == NULL) {
-                       log_debug("Creating new section %s to configuration file: %s\n", section_name, cfg_handle->full_fname);
-                       //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
-                       //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
-                       err_flg = uci_create_named_section(cfg_handle,
-                                               section_type,
-                                               section_name,
-                                               false);
-                       if (err_flg == UCI_OK) {
-                               uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
-                                       tmp_sct = uci_to_section(elem);
-                                       if (strcmp(tmp_sct->type, section_type) == 0) {
-                                               sct     = tmp_sct;
-                                               break;
-                                       }
-                               }
-                       }
-               }
-               if (err_flg == UCI_OK) {
-                       memset(&ptr, 0, sizeof(ptr));
-                       ptr.package     = cfg_handle->_pkg->e.name;
-                       ptr.section     = sct->e.name;
-                       ptr.option      = key;
-                       err_flg         = uci_lookup_ptr(cfg_handle->_ctx, &ptr, NULL, false);
-                       if (err_flg == UCI_OK) {
-                               ptr.value       = value;
-                               err_flg         = uci_set(cfg_handle->_ctx, &ptr);
-                               if (err_flg == UCI_OK) {
-                                       err_flg = uci_save(cfg_handle->_ctx, cfg_handle->_pkg);
-                                       if (err_flg == UCI_OK) {
-                                               ret_val = true;
-                                               log_debug("Set value to section %s in configuration file: %s\n", section_name, cfg_handle->full_fname);
-                                       }
-                                       else {
-                                               log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", cfg_handle->full_fname);
-                                       }
-                               }
-                               else {
-                                       log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", cfg_handle->full_fname);
-                               }
-                       }
-                       else {
-                               log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n",
-                                       cfg_handle->full_fname,
-                                       cfg_handle->_pkg->e.name,
-                                       sct->e.name);
-                       }
-                       uci_free_context(cfg_handle->_ctx);
-               }
-               else {
-                       log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", cfg_handle->full_fname, section_name);
-               }
-       }
-       else {
-               log_error("Failed to set value to configuration file, invalid parameters\n");
-       }
-       return ret_val;
-}
-
 DeviceConfig::DeviceConfig(string device_id_param) {
+       bool    succ;
+
        device_id       = device_id_param;
        uci_handle      = load_device_config(device_id_param);
        if (uci_handle != NULL) {
-               device_type     = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
+               succ    = get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, device_type);
+               if (succ == false) {
+                       log_error("Could not read device type from the configuration file.\n");
+               }
        }
        else {
                log_error("Could not read device configuration.\n");
@@ -183,7 +57,14 @@ DeviceConfig::DeviceConfig(string device_id_param) {
 }
 
 DeviceConfig::~DeviceConfig() {
+       bool    suc;
+
        if (uci_handle != NULL) {
+               suc     = uci_save_config_values(uci_handle->_ctx,
+                                       uci_handle->_pkg);
+               if (suc == true) {
+                       log_debug("saved the configuration file: %s\n", uci_handle->full_fname);
+               }
                delete(uci_handle);
                uci_handle      = NULL;
        }
@@ -215,130 +96,154 @@ string DeviceConfig::get_base_dir_name() {
        return store_base_dir;
 }
 
-string DeviceConfig::get_dir_name(string device_id_param) {
+string DeviceConfig::get_config_path_name() {
        string  ret_val;
-       string  d_name;
+       string  path_name;
 
-       d_name  = DeviceConfig::get_base_dir_name();
-       ret_val = FileUtil::concat_paths(d_name, device_id_param);
+       path_name       = DeviceConfig::get_base_dir_name();
+       ret_val         = FileUtil::concat_paths(path_name, device_id);
        return ret_val;
 }
 
-string DeviceConfig::get_file_name(string device_id_param) {
+string DeviceConfig::get_config_file_name() {
        string  ret_val;
-       string  fname;
+       string  fname_base;
 
-       fname   = DEVICE_CONFIG__FILE_NAME;
-       ret_val = get_dir_name(device_id_param);
-       ret_val = FileUtil::concat_paths(ret_val, fname);
+       fname_base      = DEVICE_CONFIG__FILE_NAME;
+       ret_val         = get_config_path_name();
+       ret_val         = FileUtil::concat_paths(ret_val, fname_base);
        return ret_val;
 }
 
-string DeviceConfig::get_config_value(string key) {
-       struct uci_section      *section;
-       struct uci_option       *option;
-       string                  ret_val;
+string DeviceConfig::get_pathless_config_file_name() {
+       return DEVICE_CONFIG__FILE_NAME;
+}
 
-       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());
-                       if (option != NULL) {
-                               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;
-                               }
-                       }
-                       else {
-                               log_error("key: %s Failed to read parameter value\n", key.c_str());
+bool DeviceConfig::get_config_value(string key, string& value) {
+       char    *ret;
+       bool    ret_val;
+
+       ret_val = false;
+       value.clear();
+       if ((uci_handle != NULL) &&
+           (uci_handle->_ctx != NULL) &&
+           (uci_handle->_pkg != NULL)) {
+               if (key.empty() == false) {
+                       ret     = uci_get_config_value(uci_handle->_ctx,
+                                               uci_handle->_pkg,
+                                               DEVICE_CONFIG__SECTION_NAME,
+                                               key.c_str());
+                       if (ret != NULL) {
+                               ret_val = true;
+                               value   = ret;
                        }
                }
+               else {
+                       log_error("Failed to read configuration value, key was empty string.\n");
+               }
+       }
+       else {
+               log_error("Failed to read configuration value for key: %s. Invalid handle to configuration file.\n", key.c_str());
        }
        return ret_val;
 }
 
-void DeviceConfig::set_config_value(string key,
-                               string value) {
+bool DeviceConfig::set_config_value(string key,
+                               string value,
+                               bool save_immediately) {
        string  cfg_dir;
        string  cfg_fl;
+       bool    ret_val;
 
-       cfg_dir = get_dir_name(device_id);
+       cfg_dir = get_config_path_name();
        cfg_fl  = DEVICE_CONFIG__FILE_NAME;
-       set_config_value_to_section(uci_handle,
-                       DEVICE_CONFIG__SECTION_TYPE,
-                       DEVICE_CONFIG__SECTION_NAME,
-                       key.c_str(),
-                       value.c_str());
+       if (uci_handle != NULL) {
+               if ((uci_handle->_ctx != NULL) &&
+                   (uci_handle->_pkg != NULL) &&
+                   (uci_handle->short_fname != NULL) &&
+                   (uci_handle->full_fname != NULL)) {
+                       //log_debug("uci_handle != null, short_name: %s, full_name: %s\n",
+                       //      uci_handle->short_fname, uci_handle->full_fname);
+                       //log_debug("key: %s, value: %s\n", key.c_str(), value.c_str());
+                       ret_val = uci_set_config_value(uci_handle->_ctx,
+                                               uci_handle->_pkg,
+                                               uci_handle->short_fname,
+                                               uci_handle->full_fname,
+                                               DEVICE_CONFIG__SECTION_TYPE,
+                                               DEVICE_CONFIG__SECTION_NAME,
+                                               key.c_str(),
+                                               value.c_str(),
+                                               save_immediately);
+               }
+               else {
+                       log_error("Could not set config value for device %s: key: %s, value: %s. Invalid filename in handle.\n",
+                               device_id.c_str(), key.c_str(), value.c_str());
+               }
+       }
+       else {
+               log_error("Could not set config value for device %s: key: %s, value: %s. Invalid handle to config file\n",
+                       device_id.c_str(), key.c_str(), value.c_str());
+       }
+       return ret_val;
 }
 
+// TODO: This function should be moved else where or it should be dynamic...
 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 DELTA;
                }
        }
-       return ret_val;;
+       return MEAN;
 }
 
 ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
        int                     err_flg;
        struct uci_context      *ctx;
        struct uci_package      *pkg;
-       string                  cfg_fl;
+       string                  fname_base;
+       string                  fname_full;
        string                  cfg_dir;
-       int                     b_count;
-       char                    *fname;
        ConfigHandle            *ret_val;
+       FILE                    *fp;
 
        ret_val = NULL;
-       cfg_dir = get_dir_name(device_id_param);
+       cfg_dir = get_config_path_name();
        if (cfg_dir.empty() == false) {
-               if (access(cfg_dir.c_str(), W_OK) != 0) {
-                       FileUtil::mkdirs(cfg_dir.c_str());
-               }
-               cfg_fl  = get_file_name(device_id_param);
-               if (access(cfg_fl.c_str(), R_OK) == 0) {
+               fname_full      = get_config_file_name();
+               if (FileUtil::file_exist(fname_full.c_str(), false) == false)
+                       FileUtil::mkfile(fname_full.c_str(), false);
+               if (access(fname_full.c_str(), R_OK) == 0) {
                        ctx     = uci_alloc_context();
                        if (ctx != NULL) {
-                               log_debug("confdir: %s, file: %s\n", cfg_dir.c_str(), cfg_fl.c_str());
+                               log_debug("configuration file: %s\n", fname_full.c_str());
                                uci_set_confdir(ctx, cfg_dir.c_str());
-                               err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
+                               err_flg = uci_load(ctx, fname_full.c_str(), &pkg);
                                if (err_flg == UCI_OK) {
                                        //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
-                                       b_count = strlen(cfg_dir.c_str()) + strlen(cfg_fl.c_str()) + 10;
-                                       fname   = (char *)calloc(1, b_count);
-                                       if (fname != NULL) {
-                                               strncpy(fname, cfg_dir.c_str(), b_count);
-                                               strncat(fname, "/", 1);
-                                               strncat(fname, cfg_fl.c_str(), strlen(cfg_fl.c_str()) + 1);
-
-                                               ret_val = new ConfigHandle(ctx, pkg, cfg_fl.c_str(), fname);
-                                               free(fname);
-                                       }
+                                       fname_base      = get_pathless_config_file_name();
+                                       ret_val = new ConfigHandle(ctx,
+                                                               pkg,
+                                                               fname_base.c_str(),
+                                                               fname_full.c_str());
                                }
                                else {
-                                       log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
-                                       set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
-                                       uci_free_context(ctx);
+                                       log_debug("Failed to load device configuration: %s, err code: %d.\n", fname_full.c_str(), UCI_OK);
+                                       fp      = fopen(fname_full.c_str(), "w+");
+                                       if (fp != NULL)
+                                               fclose(fp);
+                                       //uci_free_context(ctx);
                                }
                        }
                        else {
                                log_error("Failed to load device configuration, memory allocation error.\n");
-                               set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
+                               fp      = fopen(fname_full.c_str(), "w+");
+                               if (fp != NULL)
+                                       fclose(fp);
                        }
                }
                else {
-                       log_error("Failed to load device configuration, file does not exist: %s.\n", cfg_fl.c_str());
+                       log_error("Failed to load device configuration, file does not exist: %s.\n", fname_full.c_str());
                }
        }
        return ret_val;