From: Mika Laitio Date: Thu, 16 Jun 2011 18:58:21 +0000 (+0300) Subject: better error handling in case where the device specific config file is missing X-Git-Url: http://pilppa.org/gitweb/?p=lib1wire.git;a=commitdiff_plain;h=5b8fec4ada47c630e4484d21408fb354d0fb7d93 better error handling in case where the device specific config file is missing Signed-off-by: Mika Laitio --- diff --git a/src/Factory.cc b/src/Factory.cc index 152d740..6145a46 100644 --- a/src/Factory.cc +++ b/src/Factory.cc @@ -41,9 +41,15 @@ Factory::~Factory() { // TODO Auto-generated destructor stub } +/** + * Returns family code for the device. + * + * @return w1 family code for the device type. If family code is not found, -1 is returned. + */ int Factory::get_family_code_by_device_type(string device_type_param) { - int ret_val = -1; + int ret_val; + ret_val = -1; if (device_type_param.compare(DEVICE_TYPE_TEMPERATURESENSOR) == 0) { ret_val = 0x10; } @@ -76,38 +82,56 @@ Device *Factory::create_w1_device(int family_code_param, string device_id_param, dirent *direntry_param) { Device *ret_val; - DeviceConfig *config; string type; ret_val = NULL; type = get_device_type_by_family_code(family_code_param); - switch(family_code_param) { - case 0x10: - case 0x28: - ret_val = new W1TemperatureSensor(device_id_param, type, direntry_param); - break; - case 0x1d: - ret_val = new W1CounterDevice(device_id_param, type, direntry_param); - break; - case 0x81: - // 0x81 is the 1-wire USB dongle... No need to create device for it. - break; - default: - log_debug("Unsupported 1-wire-family code: %#x, device not created: %s\n", family_code_param, device_id_param.c_str()); - break; + if (type.empty() == false) { + ret_val = create_w1_device(family_code_param, + type, + device_id_param, + direntry_param); } - if (ret_val != NULL) { - // check that device config exist - config = DeviceConfig::get_device_config(device_id_param); - if (config != NULL) { - // if not, create default device config - type = config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE); - if (type.empty() == true) { - type = ret_val->get_type(); - config->set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type); - config->set_config_value(DEVICE_CONFIG_VALUE_KEY__ID, ret_val->get_id()); + return ret_val; +} + +Device *Factory::create_w1_device(int family_code_param, + string device_id_param, + string type_param, + dirent *direntry_param) { + Device *ret_val; + DeviceConfig *config; + + ret_val = NULL; + if (type_param.empty() == false) { + switch(family_code_param) { + case 0x10: + case 0x28: + ret_val = new W1TemperatureSensor(device_id_param, type_param, direntry_param); + break; + case 0x1d: + ret_val = new W1CounterDevice(device_id_param, type_param, direntry_param); + break; + case 0x81: + // 0x81 is the 1-wire USB dongle... No need to create device for it. + break; + default: + log_debug("Unsupported 1-wire-family code: %#x, device not created: %s\n", family_code_param, device_id_param.c_str()); + break; + } + if (ret_val != NULL) { + // check that device config exist + config = DeviceConfig::get_device_config(device_id_param); + if (config != NULL) { + // if not, create default device config + type_param = config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE); + if (type_param.empty() == true) { + type_param = ret_val->get_type(); + config->set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type_param); + config->set_config_value(DEVICE_CONFIG_VALUE_KEY__ID, ret_val->get_id()); + } + delete(config); } - delete(config); } } return ret_val; @@ -118,12 +142,18 @@ Device *Factory::create_w1_device(string device_type_param, int family_code; Device *ret_val; - family_code = get_family_code_by_device_type(device_type_param); - ret_val = create_w1_device(family_code, device_id_param, NULL); + ret_val = NULL; + if (device_type_param.empty() == false) { + family_code = get_family_code_by_device_type(device_type_param); + if (family_code != -1) { + ret_val = create_w1_device(family_code, device_type_param, device_id_param, NULL); + } + } return ret_val; } -Device *Factory::create_w1_device(dirent *direntry_param, int *err_code_param) { +Device *Factory::create_w1_device(dirent *direntry_param, + int *err_code_param) { string folder_name; string tmp_str; string device_name; @@ -217,8 +247,12 @@ list Factory::get_device_list() { if (found == false) { // reader device is not in the list of active devices. create and add it to list as in-active one... type = reader->get_device_type(); - device = create_w1_device(type, id1); - ret_val.push_back(device); + if (type.empty() == false) { + device = create_w1_device(type, id1); + if (device != NULL) { + ret_val.push_back(device); + } + } } } while(rdr_list.empty() == false) { diff --git a/src/Factory.hh b/src/Factory.hh index ce29f23..c4adccb 100644 --- a/src/Factory.hh +++ b/src/Factory.hh @@ -29,6 +29,7 @@ namespace w1 { static std::list get_device_list(); private: //int parse_family_code(std::string folder_name); + static plp::Device *create_w1_device(int family_code_param, string device_id_param, string type_param, dirent *direntry_param); static plp::Device *create_w1_device(int family_code, std::string device_id, dirent *direntry_param); static plp::Device *create_w1_device(std::string device_type_param, std::string device_id_param); static plp::Device *create_w1_device(dirent *direntry_param, int *err_code_param);