#include <string.h>
#include <plp/log.h>
+#include <plp/DeviceConfig.hh>
+#include <plp/DeviceTypes.hh>
+#include <plp/FileUtil.hh>
+
#include "Factory.hh"
#include "W1TemperatureSensor.hh"
#include "W1CounterDevice.hh"
-#include "W1Util.hh"
using namespace w1;
+using namespace plp;
using namespace std;
template <class NumberDataType>
// TODO Auto-generated destructor stub
}
-W1Device *Factory::get_device(int family_code,
- string device_id,
+int Factory::get_family_code_by_device_type(string device_type_param) {
+ int ret_val = -1;
+
+ if (device_type_param.compare(DEVICE_TYPE_TEMPERATURESENSOR) == 0) {
+ ret_val = 0x10;
+ }
+ else if (device_type_param.compare(DEVICE_TYPE_COUNTER_DEVICE) == 0) {
+ ret_val = 0x1d;
+ }
+ return ret_val;
+}
+
+string Factory::get_device_type_by_family_code(int family_code_param) {
+ string ret_val;
+
+ switch(family_code_param) {
+ case 0x10:
+ case 0x28:
+ ret_val = DEVICE_TYPE_TEMPERATURESENSOR;
+ break;
+ case 0x1d:
+ ret_val = DEVICE_TYPE_COUNTER_DEVICE;
+ break;
+ default:
+ ret_val = DEVICE_TYPE_UNKNOWN;
+ log_error("Unknown w1 device type: %d\n", family_code_param);
+ break;
+ }
+ return ret_val;
+}
+
+Device *Factory::create_w1_device(int family_code_param,
+ string device_id_param,
dirent *direntry_param) {
- W1Device *ret_val;
+ Device *ret_val;
DeviceConfig *config;
string type;
ret_val = NULL;
- switch(family_code) {
+ type = get_device_type_by_family_code(family_code_param);
+ switch(family_code_param) {
case 0x10:
case 0x28:
- ret_val = new W1TemperatureSensor(family_code, device_id, direntry_param);
+ ret_val = new W1TemperatureSensor(device_id_param, type, direntry_param);
break;
case 0x1d:
- ret_val = new W1CounterDevice(family_code, device_id, direntry_param);
+ 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, device_id.c_str());
+ 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) {
- log_debug("%s: %#x\n", ret_val->get_device_type().c_str(), ret_val->get_w1_family_code());
// check that device config exist
- config = get_device_config(device_id);
+ config = DeviceConfig::get_device_config(device_id_param);
if (config != NULL) {
// 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();
+ type = ret_val->get_type();
config->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type);
config->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__ID, ret_val->get_id());
}
return ret_val;
}
-W1Device *Factory::create_device(dirent *direntry_param) {
- string folder_name;
- string tmp_str;
- string device_name;
- int pos;
- int family_code;
- bool suc_flg;
- W1Device *ret_val;
+Device *Factory::create_w1_device(string device_type_param,
+ string device_id_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);
+ return ret_val;
+}
+
+Device *Factory::create_w1_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;
+ Device *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 (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 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::create_w1_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;
+list<Device *> Factory::get_device_list() {
+ DIR *dir;
+ int err_flg;
+ struct dirent *direntry;
+ Device *device;
+ bool is_subdir;
+ list<DataReader *> rdr_list;
+ list<Device *> ret_val;
+ list<Device *>::iterator dev_iter;
+ list<DataReader *>::iterator rdr_iter;
+ DataReader *reader;
+ string id1;
+ string id2;
+ string type;
+ string dev_type;
+ bool found;
+ // scan through the list of devices detected from the 1-wire network
dir = opendir(W1_SCAN_ROOTDIR);
if (dir != NULL) {
direntry = readdir(dir);
while(direntry != NULL) {
- is_subdir = W1Util::is_subdirectory(W1_SCAN_ROOTDIR, direntry);
+ is_subdir = FileUtil::is_subdirectory(W1_SCAN_ROOTDIR, direntry);
if (is_subdir == true) {
- device = create_device(direntry);
+ err_flg = 0;
+ device = create_w1_device(direntry, &err_flg);
if (device != NULL) {
ret_val.push_back(device);
}
else {
- log_info("Unsupported 1-wire device detected: %s\n", direntry->d_name);
+ if (err_flg != 0) {
+ log_info("Unsupported 1-wire device detected: %s\n", direntry->d_name);
+ }
}
}
direntry = readdir(dir);
log_error("Failed to close 1-wire device directory: %s\n", W1_SCAN_ROOTDIR);
}
}
+ // scan through the list of devices which have saved data
+ rdr_list = get_data_reader_list();
+ for (rdr_iter = rdr_list.begin(); rdr_iter != rdr_list.end(); rdr_iter++) {
+ reader = (DataReader *)*rdr_iter;
+ id1 = reader->get_device_id();
+ found = false;
+ for (dev_iter = ret_val.begin(); dev_iter != ret_val.end(); dev_iter++) {
+ device = (Device *)*dev_iter;
+ id2 = device->get_id();
+ if (id1.compare(id2) == 0) {
+ found = true;
+ break;
+ }
+ }
+ 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);
+ }
+ }
return ret_val;
}
-list<DeviceData *> Factory::get_device_data_list() {
+list<DataReader *> Factory::get_data_reader_list() {
DIR *dir;
string dr_name;
int err_flg;
struct dirent *direntry;
- DeviceData *dev_dta;
+ DataReader *reader;
bool is_subdir;
- list<DeviceData *> ret_val;
+ list<DataReader *> ret_val;
dr_name = DeviceConfig::get_base_dir_name();
dir = opendir(dr_name.c_str());
direntry = readdir(dir);
while(direntry != NULL) {
if (strcmp(direntry->d_name, "cache") != 0) {
- is_subdir = W1Util::is_subdirectory(dr_name.c_str(), direntry);
+ is_subdir = FileUtil::is_subdirectory(dr_name.c_str(), direntry);
if (is_subdir == true) {
- dev_dta = new DeviceData(direntry->d_name);
- ret_val.push_back(dev_dta);
+ reader = new DataReader(direntry->d_name);
+ ret_val.push_back(reader);
}
}
direntry = readdir(dir);
}
return ret_val;
}
-
-DeviceConfig *Factory::get_device_config(string device_id) {
- DeviceConfig *ret_val;
-
- ret_val = new DeviceConfig(device_id);
- return ret_val;
-}