From: Mika Laitio Date: Wed, 15 Aug 2012 09:27:06 +0000 (+0300) Subject: cleanups for devices with no data X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=0fa52fa51ad87bfb7871956f2cca83434f3bff7e;p=libplp.git cleanups for devices with no data Signed-off-by: Mika Laitio --- diff --git a/src/Device.cc b/src/Device.cc index 714e830..d09d53b 100644 --- a/src/Device.cc +++ b/src/Device.cc @@ -16,6 +16,7 @@ Device::Device(string id_param, id = id_param; type = type_param; lifecycle_status = LIFECYCLE_STATUS__UNAVAILABLE; + _cfg = NULL; } Device::Device(std::string id_param, @@ -26,10 +27,11 @@ Device::Device(std::string id_param, type = type_param; name = name_param; lifecycle_status = status_param; + _cfg = NULL; } //Device::~Device() { -// log_debug("Device: constructor\n"); +// delete(_cfg); //} string Device::get_id() { diff --git a/src/Device.hh b/src/Device.hh index daef2f8..d73395a 100644 --- a/src/Device.hh +++ b/src/Device.hh @@ -9,6 +9,7 @@ #define DEVICEINFO_HH_ #include +#include "DeviceConfig.hh" using namespace std; @@ -18,8 +19,11 @@ namespace plp { class Device { public: Device(string id_param, string type_param); - Device(string id_param, string type_param, string name_param, EnumDeviceLifeCycleStatus status_param); - virtual ~Device() { } + Device(string id_param, + string type_param, + string name_param, + EnumDeviceLifeCycleStatus status_param); + virtual ~Device() { delete(_cfg); } std::string get_id(); std::string get_name(); std::string get_type(); @@ -30,6 +34,7 @@ namespace plp { std::string id; std::string name; std::string type; + plp::DeviceConfig *_cfg; plp::EnumDeviceLifeCycleStatus lifecycle_status; }; } diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index 75af9f3..a257275 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -16,6 +16,7 @@ #include "log.h" #include "config.h" #include "private/uci_config.h" +#include "retval.h" using namespace std; using namespace plp; @@ -122,6 +123,7 @@ string DeviceConfig::get_pathless_config_file_name() { bool DeviceConfig::get_config_value(string key, string& value) { char *ret; bool ret_val; + int err_flg; ret_val = false; value.clear(); @@ -132,8 +134,10 @@ bool DeviceConfig::get_config_value(string key, string& value) { ret = uci_get_config_value(uci_handle->_ctx, uci_handle->_pkg, DEVICE_CONFIG__SECTION_NAME, - key.c_str()); - if (ret != NULL) { + key.c_str(), + &err_flg); + if ((err_flg == PLP_OK) && + (ret != NULL)) { ret_val = true; value = ret; } @@ -216,7 +220,7 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { if (access(fname_full.c_str(), R_OK) == 0) { ctx = uci_alloc_context(); if (ctx != NULL) { - log_debug("configuration file: %s\n", fname_full.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, fname_full.c_str(), &pkg); if (err_flg == UCI_OK) { diff --git a/src/DeviceData.cc b/src/DeviceData.cc index 9deed93..fa57a52 100644 --- a/src/DeviceData.cc +++ b/src/DeviceData.cc @@ -20,12 +20,16 @@ DeviceData::DeviceData(string id_param, string type_param, string name_param, EnumDeviceLifeCycleStatus status_param, - Data *latest_data) : Device(id_param, type_param, name_param, status_param) { - _latest_data = latest_data; + Data *data_param) : Device(id_param, type_param, name_param, status_param) { + //if (latest_data != NULL) + // _latest_data = latest_data->clone(); + _latest_data = data_param; } DeviceData::~DeviceData() { - delete(_latest_data); + if (_latest_data != NULL) + delete(_latest_data); + _latest_data = NULL; } void DeviceData::printout() { @@ -37,9 +41,10 @@ void DeviceData::printout() { log_debug("\tname: %s\n", get_name().c_str()); log_debug("\ttype: %s\n", get_type().c_str()); log_debug("\tlifecycle state: %d\n", get_lifecycle_state()); - if (_latest_data != NULL) + if (_latest_data != NULL) { log_debug("data not null\n"); + _latest_data->printout(); + } else log_debug("data null\n"); - _latest_data->printout(); } diff --git a/src/DeviceData.hh b/src/DeviceData.hh index 8e9335e..fdc3c79 100644 --- a/src/DeviceData.hh +++ b/src/DeviceData.hh @@ -14,7 +14,8 @@ namespace plp { class DeviceData : public Device { public: - DeviceData(std::string id_param, std::string type_param); + DeviceData(std::string id_param, + std::string type_param); DeviceData(std::string id_param, std::string type_param, std::string name_param, diff --git a/src/config.c b/src/config.c index 9c479a0..2c746f2 100644 --- a/src/config.c +++ b/src/config.c @@ -125,7 +125,8 @@ char* get_config_value_and_close(const char *conf_dir_name, ret_val = uci_get_config_value(ctx, pkg, section_name, - key); + key, + &err_flg); // need to duplicate response val, as uci_free_context() would free the value otherwise if (ret_val != NULL) ret_val = strdup(ret_val); diff --git a/src/private/uci_config.c b/src/private/uci_config.c index 756f2b7..765a1be 100644 --- a/src/private/uci_config.c +++ b/src/private/uci_config.c @@ -7,6 +7,7 @@ #include "uci_config.h" #include "../log.h" +#include "../retval.h" int uci_create_named_section(struct uci_context *ctx, const char *conf_fname_base, @@ -161,12 +162,14 @@ bool uci_set_config_value(struct uci_context *ctx, char *uci_get_config_value(struct uci_context *ctx, struct uci_package *pkg, const char *section_name, - const char *key_name) { + const char *key_name, + int *err_flg) { struct uci_section *section; struct uci_option *option; char *ret_val; ret_val = NULL; + *err_flg = PLP_OK; if ((ctx != NULL) && (pkg != NULL)) { section = uci_lookup_section(ctx, @@ -184,15 +187,18 @@ char *uci_get_config_value(struct uci_context *ctx, break; default: log_error("Failed to read configuration value for key: %s\n", key_name); + *err_flg = PLP_ERR_IO; break; } } else { - log_error("Failed to find configuration key: %s\n", key_name); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + //log_error("Failed to find configuration key: %s\n", key_name); } } else { - log_error("Failed to find configuration section name: %s\n", section_name); + *err_flg = PLP_ERR_DATA_NOT_FOUND; + //log_error("Failed to find configuration section name: %s\n", section_name); } } return ret_val; diff --git a/src/private/uci_config.h b/src/private/uci_config.h index c06e725..c3bb231 100644 --- a/src/private/uci_config.h +++ b/src/private/uci_config.h @@ -43,7 +43,8 @@ extern "C" char *uci_get_config_value(struct uci_context *ctx, struct uci_package *pkg, const char *section_name, - const char *key_name); + const char *key_name, + int *err_flg); #ifdef __cplusplus extern "C"