]> pilppa.org Git - lib1wire.git/blobdiff - src/Factory.cc
better error handling in case where the device specific config file is missing
[lib1wire.git] / src / Factory.cc
index 152d7400302a42ad5c610cfb2553269705493e19..6145a469065afe0a7dc37b03dbebd94d0a17ceb6 100644 (file)
@@ -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<Device *> 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) {