4 * Created on: Oct 20, 2010
16 #include "W1Scanner.hh"
17 #include "W1TemperatureSensor.hh"
22 W1Scanner::W1Scanner() {
26 W1Scanner::~W1Scanner() {
27 log_debug("destroyed");
30 bool W1Scanner::is_subdirectory(dirent *direntry) {
32 if (strcmp(direntry->d_name, ".") == 0 ||
33 (strcmp(direntry->d_name, "..") == 0)) {
40 bool string_to_number(T& result,
41 const std::string& string_param,
42 std::ios_base& (*format)(std::ios_base&))
44 std::istringstream iss(string_param);
45 return !(iss >> format >> result).fail();
48 W1Device *W1Scanner::create_device(dirent *direntry) {
58 folder_name = direntry->d_name;
59 pos = folder_name.find("-");
61 temp_str = folder_name.substr(0, pos);
62 // number in string is in hex format, convert to int
63 sucFlg = string_to_number<int>(family_code, temp_str, hex);
65 cout << "family_code: " << family_code << endl;
66 device_name = folder_name.substr(pos + 1, folder_name.length() - pos);
70 cout << "temperature sensor: " << family_code << endl;
71 ret_val = new W1TemperatureSensor(direntry, family_code, device_name);
80 list<W1Device *> W1Scanner::get_device_list() {
83 struct dirent *direntry;
85 list<W1Device *> ret_val;
87 dir = opendir(W1_SCAN_ROOTDIR);
89 direntry = readdir(dir);
90 while(direntry != NULL) {
91 if (is_subdirectory(direntry) == true) {
92 log_debug("dir_name: %s\n", direntry->d_name);
93 device = create_device(direntry);
95 ret_val.push_back(device);
98 direntry = readdir(dir);
100 err_flg = closedir(dir);
102 log_error("failed to close 1-wire device directory scanned: %s\n", W1_SCAN_ROOTDIR);