]> pilppa.org Git - lib1wire.git/blobdiff - src/DeviceConfig.cc
cleaned up test_w1_datalog_write example applications parameter handling.
[lib1wire.git] / src / DeviceConfig.cc
index f94637cca7ab5b3e716a7d33eebfec2426cbaa0d..da3d85ffa93f744f16fa80e3a26d6a826eef8986 100644 (file)
@@ -22,13 +22,13 @@ using namespace w1;
 string DeviceConfig::store_base_dir    = DEFAULT_STORAGE_BASE_DIR;
 
 ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) {
-       ctx     = ctx_param;
-       pkg     = pkg_param;
+       _ctx    = ctx_param;
+       _pkg    = pkg_param;
 }
 
 ConfigHandle::~ConfigHandle() {
-       uci_unload(ctx, pkg);
-       uci_free_context(ctx);
+       uci_unload(_ctx, _pkg);
+       uci_free_context(_ctx);
 }
 
 DeviceConfig::DeviceConfig(string device_id_param) {
@@ -38,7 +38,7 @@ DeviceConfig::DeviceConfig(string device_id_param) {
                device_type     = get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
        }
        else {
-               log_error("Could not read device config\n");
+               log_error("Could not read device configuration.\n");
        }
 }
 
@@ -92,17 +92,22 @@ string DeviceConfig::get_cfg_value(string key) {
        string                  ret_val;
 
        if (uci_handle != NULL) {
-               section = uci_lookup_section(uci_handle->ctx, uci_handle->pkg, DEVICE_CONFIG__SECTION_NAME);
+               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());
-                       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;
+                       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());
                        }
                }
        }
@@ -110,8 +115,8 @@ string DeviceConfig::get_cfg_value(string key) {
 }
 
 void DeviceConfig::set_cfg_value(string key, string value) {
-       string cfg_dir;
-       string cfg_fl;
+       string  cfg_dir;
+       string  cfg_fl;
 
        cfg_dir = get_dir_name(device_id);
        cfg_fl  = DEVICE_CONFIG__FILE_NAME;
@@ -123,8 +128,8 @@ void DeviceConfig::set_cfg_value(string key, string value) {
                        value.c_str());
 }
 
-enum_summary_calculation DeviceConfig::get_summary_calculation_type() {
-       enum_summary_calculation        ret_val;
+EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
+       EnumSummaryCalculationType      ret_val;
 
        ret_val = MEAN;
        if (device_type.empty() == false) {
@@ -150,27 +155,29 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
                        W1Util::mkdirs(cfg_dir.c_str());
                }
                cfg_fl  = get_file_name(device_id_param);
-               ctx     = uci_alloc_context();
-               if (ctx != NULL) {
-                       log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
-                       uci_set_confdir(ctx, cfg_dir.c_str());
-                       if (access(cfg_fl.c_str(), R_OK) == 0) {
+               if (access(cfg_fl.c_str(), R_OK) == 0) {
+                       ctx     = uci_alloc_context();
+                       if (ctx != NULL) {
+                               //log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
+                               uci_set_confdir(ctx, cfg_dir.c_str());
                                err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
                                if (err_flg == UCI_OK) {
-                                       log_debug("Loaded device configuration file: %s\n.", cfg_fl.c_str());
+                                       //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
                                        ret_val = new ConfigHandle(ctx, pkg);
                                }
                                else {
-                                       log_debug("Failed to load file: %s, err code: %d\n.", cfg_fl.c_str(), UCI_OK);
+                                       log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
                                        set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
+                                       uci_free_context(ctx);
                                }
                        }
                        else {
-                               log_error("Failed to load device device configuration, file does not exit: %s.\n", cfg_fl.c_str());
+                               log_error("Failed to load device device configuration, memory allocation error.\n");
+                               set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
                        }
                }
                else {
-                       log_error("Failed to load device device configuration, invalid device id: %s.\n", cfg_dir.c_str());
+                       log_error("Failed to load device device configuration, file does not exist: %s.\n", cfg_fl.c_str());
                }
        }
        return ret_val;