]> pilppa.org Git - lib1wire.git/blobdiff - src/Factory.cc
cleanups for log messages.
[lib1wire.git] / src / Factory.cc
index e9637a4bd7101487cf4f2cf83d27dd4e04ddd415..b10c3eaa46342ec9e5d3def1b9fe2fbd16f9a866 100644 (file)
@@ -5,14 +5,30 @@
  *      Author: lamikr
  */
 
+#include <string>
+#include <sstream>
+#include <iostream>
+
+#include <string.h>
+
 #include <plp/log.h>
 #include "Factory.hh"
 #include "W1TemperatureSensor.hh"
 #include "W1CounterDevice.hh"
+#include "W1Util.hh"
 
 using namespace w1;
 using namespace std;
 
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
+                 const std::string& string_param,
+                 std::ios_base& (*format)(std::ios_base&))
+{
+       std::istringstream iss(string_param);
+       return !(iss >> format >> result).fail();
+}
+
 Factory::Factory() {
        // TODO Auto-generated constructor stub
 }
@@ -26,34 +42,138 @@ W1Device *Factory::get_device(int family_code,
                        dirent *direntry_param) {
        W1Device        *ret_val;
        DeviceConfig    *config;
+       string          type;
 
        ret_val = NULL;
-       log_debug("family_code: %d\n", family_code);
        switch(family_code) {
                case    0x10:
                case    0x28:
                        ret_val = new W1TemperatureSensor(family_code, device_id, direntry_param);
-                       log_debug("temperature sensor: %d\n", ret_val->get_family_code());
                        break;
                case    0x1d:
                        ret_val = new W1CounterDevice(family_code, device_id, direntry_param);
-                       log_debug("counter device: %d\n", family_code);
+                       break;
+               case    0x81:
+                       // 0x81 is the 1-wire USB dongle... No need to create device for it.
                        break;
                default:
-                       log_debug("device not created, unsupported device type: %d\n", family_code);
+                       log_debug("Unsupported 1-wire-family code: %#x, device not created: %s\n", family_code, device_id.c_str());
                        break;
        }
        if (ret_val != NULL) {
+               log_debug("%s: %#x\n", ret_val->get_device_type().c_str(), ret_val->get_w1_family_code());
                // check that device config exist
-               // if not, create default...
                config  = get_device_config(device_id);
                if (config != NULL) {
-                       string  type;
+                       // if not, create default device config
                        type    = config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
                        if (type.empty() == true) {
                                type    = ret_val->get_device_type();
                                config->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type);
+                               config->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__ID, ret_val->get_id());
+                       }
+                       delete(config);
+               }
+       }
+       return ret_val;
+}
+
+W1Device *Factory::create_device(dirent *direntry_param, int *err_code_param) {
+       string          folder_name;
+       string          tmp_str;
+       string          device_name;
+       int             pos;
+       int             family_code;
+       bool            suc_flg;
+       W1Device        *ret_val;
+
+       ret_val         = NULL;
+       *err_code_param = 0;
+       folder_name     = direntry_param->d_name;
+       pos             = folder_name.find("-");
+       if (pos > 0) {
+               tmp_str = folder_name.substr(0, pos);
+               // number in string is in hex format, convert to int
+               suc_flg = string_to_number<int>(family_code, tmp_str, hex);
+               // if family code = 0x81 (1-wire usb dongle), do not try to create the device
+               if (family_code != 0x81) {
+                       if (suc_flg == true) {
+                               log_debug("1-wire device family code: %#x\n", family_code);
+                               device_name     = folder_name.substr(pos + 1, folder_name.length() - pos);
+                               ret_val         = Factory::get_device(family_code,
+                                                               device_name,
+                                                               direntry_param);
+                               if ((ret_val == NULL) &&
+                                   (family_code != 0x81)) {
+                                       *err_code_param = 1;
+                               }
+                       }
+               }
+       }
+       return ret_val;
+}
+
+list<W1Device *> Factory::get_device_list() {
+       DIR                     *dir;
+       int                     err_flg;
+       struct dirent           *direntry;
+       W1Device                *device;
+       bool                    is_subdir;
+       list<W1Device *>        ret_val;
+
+       dir     = opendir(W1_SCAN_ROOTDIR);
+       if (dir != NULL) {
+               direntry        = readdir(dir);
+               while(direntry != NULL) {
+                       is_subdir       = W1Util::is_subdirectory(W1_SCAN_ROOTDIR, direntry);
+                       if (is_subdir == true) {
+                               err_flg = 0;
+                               device  = create_device(direntry, &err_flg);
+                               if (device != NULL) {
+                                       ret_val.push_back(device);
+                               }
+                               else {
+                                       if (err_flg != 0) {
+                                               log_info("Unsupported 1-wire device detected: %s\n", direntry->d_name);
+                                       }
+                               }
+                       }
+                       direntry        = readdir(dir);
+               }
+               err_flg = closedir(dir);
+               if (err_flg < 0) {
+                       log_error("Failed to close 1-wire device directory: %s\n", W1_SCAN_ROOTDIR);
+               }
+       }
+       return ret_val;
+}
+
+list<DeviceData *> Factory::get_device_data_list() {
+       DIR                     *dir;
+       string                  dr_name;
+       int                     err_flg;
+       struct dirent           *direntry;
+       DeviceData              *dev_dta;
+       bool                    is_subdir;
+       list<DeviceData *>      ret_val;
+
+       dr_name = DeviceConfig::get_base_dir_name();
+       dir     = opendir(dr_name.c_str());
+       if (dir != NULL) {
+               direntry        = readdir(dir);
+               while(direntry != NULL) {
+                       if (strcmp(direntry->d_name, "cache") != 0) {
+                               is_subdir       = W1Util::is_subdirectory(dr_name.c_str(), direntry);
+                               if (is_subdir == true) {
+                                       dev_dta = new DeviceData(direntry->d_name);
+                                       ret_val.push_back(dev_dta);
+                               }
                        }
+                       direntry        = readdir(dir);
+               }
+               err_flg = closedir(dir);
+               if (err_flg < 0) {
+                       log_error("Failed to close 1-wire device data directory: %s\n", dr_name.c_str());
                }
        }
        return ret_val;