Back to index

lshw  02.16
heuristics.cc
Go to the documentation of this file.
00001 /*
00002  * heuristics.cc
00003  *
00004  *
00005  */
00006 
00007 #include "version.h"
00008 #include "sysfs.h"
00009 #include "jedec.h"
00010 #include "osutils.h"
00011 
00012 #include <stdlib.h>
00013 #include <regex.h>
00014 
00015 __ID("@(#) $Id: heuristics.cc 2433 2012-01-10 22:01:30Z lyonel $");
00016 
00017 string guessBusInfo(const string & info)
00018 {
00019                                                   // 2.6-style PCI
00020   if(matches(info,"^[[:xdigit:]][[:xdigit:]][[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]\\.[[:xdigit:]]$"))
00021   {
00022     return "pci@" + info;
00023   }
00024                                                   // 2.4-style PCI
00025   if(matches(info,"^[[:xdigit:]][[:xdigit:]]:[[:xdigit:]][[:xdigit:]]\\.[[:xdigit:]]$"))
00026   {
00027     return "pci@0000:" + info;
00028   }
00029 
00030                                                   // USB: host-port[.port]:config.interface
00031   if(matches(info, "^[0-9]+-[0-9]+(\\.[0-9]+)*:[0-9]+\\.[0-9]+$"))
00032   {
00033     size_t colon = info.rfind(":");
00034     size_t dash = info.find("-");
00035 
00036     return "usb@" + info.substr(0, dash) + ":" + info.substr(dash+1, colon-dash-1);
00037   }
00038 
00039   if(matches(info, "^[[:xdigit:]]+-[0-9]+$"))     // Firewire: guid-function
00040   {
00041     size_t dash = info.find("-");
00042 
00043     return "firewire@" + info.substr(0, dash);
00044   }
00045 
00046 #if 1
00047 //#ifdef __hppa__
00048   if(matches(info, "^[0-9]+(:[0-9]+)*$"))         // PA-RISC: x:y:z:t corresponds to /x/y/z/t
00049   {
00050     string result = "parisc@";
00051 
00052     for(unsigned i=0; i<info.length(); i++)
00053       if(info[i] == ':')
00054         result += '/';
00055     else
00056       result += info[i];
00057     return result;
00058   }
00059 #endif
00060   return "";
00061 }
00062 
00063 
00064 static string guessParentBusInfo(const hwNode & child)
00065 {
00066   string sysfs_path = sysfs_finddevice(child.getLogicalName());
00067   vector < string > path;
00068   string result = "";
00069 
00070   if(sysfs_path == "") return "";
00071 
00072   splitlines(sysfs_path, path, '/');
00073 
00074   if(path.size()>1)
00075     path.pop_back();
00076   else
00077     return "";
00078 
00079   while((result=="") && (path.size()>1))
00080   {
00081     result = guessBusInfo(path[path.size()-1]);
00082     path.pop_back();
00083   }
00084   return result;
00085 }
00086 
00087 
00088 hwNode * guessParent(const hwNode & child, hwNode & base)
00089 {
00090   return base.findChildByBusInfo(guessParentBusInfo(child));
00091 }
00092 
00093 static const char *disk_manufacturers[] =
00094 {
00095   "^ST.+", "Seagate",
00096   "^D...-.+", "IBM",
00097   "^IBM.+", "IBM",
00098   "^HITACHI.+", "Hitachi",
00099   "^IC.+", "Hitachi",
00100   "^HTS.+", "Hitachi",
00101   "^FUJITSU.+", "Fujitsu",
00102   "^MP.+", "Fujitsu",
00103   "^TOSHIBA.+", "Toshiba",
00104   "^MK.+", "Toshiba",
00105   "^MAXTOR.+", "Maxtor",
00106   "^Pioneer.+", "Pioneer",
00107   "^PHILIPS.+", "Philips",
00108   "^QUANTUM.+", "Quantum",
00109   "FIREBALL.+", "Quantum",
00110   "^WDC.+", "Western Digital",
00111   "WD.+", "Western Digital",
00112   NULL, NULL
00113 };
00114 
00115 bool guessVendor(hwNode & device)
00116 {
00117   int i = 0;
00118   bool result = false;
00119 
00120 
00121   device.setVendor(jedec_resolve(device.getVendor()));
00122 
00123   if(device.getVendor() != "")
00124     return false;
00125 
00126  
00127   if(device.getClass() == hw::disk)
00128     while (disk_manufacturers[i])
00129     {
00130       if (matches(device.getProduct().c_str(), disk_manufacturers[i], REG_ICASE))
00131       {
00132         device.setVendor(disk_manufacturers[i + 1]);
00133         result = true;
00134       }
00135       i += 2;
00136     }
00137 
00138   return result;
00139 }
00140 
00141 
00142 static string parsehex(const string & s)
00143 {
00144   int i = 0;
00145   string result = "";
00146 
00147   if(matches(s,"^0x[[:xdigit:]][[:xdigit:]]+$"))
00148   {
00149     for(i=2; i<s.length(); i+=2)
00150     {
00151       string c = s.substr(i,2);
00152       static char code[2];
00153 
00154       code[0] = strtol(c.c_str(), NULL, 16);
00155       code[1] = '\0';
00156       
00157       if(code[0] < ' ') return s;
00158       result += string(code);
00159     }
00160    }
00161    else
00162      return s;
00163 
00164   return result;
00165 }
00166 
00167 bool guessProduct(hwNode & device)
00168 {
00169   string product = device.getProduct();
00170 
00171   if(product == "")
00172     return false;
00173 
00174   device.setProduct(parsehex(product));
00175 
00176   return true;
00177 }
00178