summaryrefslogblamecommitdiffstats
path: root/src/fbgui/sysinfo.cpp
blob: 1af6f76c9990b6dc2fd771a585c6add35c495425 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                    








                                                       
                                                                                                   


                       

                    
                                                                                                   


                      

                     
                                                                                                   








                                                                                  








                                                                         
                                                         
                                                     
                                       


                                                   

                                       
 
                     
                                                
                       
 









                                                                                          


                                                                                                  














                                                                           
                                             
                    

                                                                           

                                                                         
 







                                                                   
                                                          

                 
                                                                                                 

                                          
 
                                                                                                   




















                                                                          
                                                              
 
                               

                     
 





                                                   
                                                                   


                                                                      
 
                 
                                                                             
    
                                                                                                  


                                                       
 

                                   








                                                                            


                                                        
                                                                          

                        
 
                                              
                                                                                    




                                                
                                                                                      

                        

                                                                         
                   
                                                                     

                        
                                                  
                                                                   

                                                                            
                   
                                                                


                                                              

                                                                         
                   
                                                                


                                                                  
 
                    





                                                                 



                                                                             
 
 
     
                                                
                                                         
               
     
 
#include "sysinfo.h"


#include <log4cxx/logger.h>
#include "qlog4cxx.h"

using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr siLogger(Logger::getLogger("fbgui.sysinfo"));


// ------------------------------------------------------------------------------------------------
/**
 * A empty constructor.
 */
SysInfo::SysInfo() {
}
// ------------------------------------------------------------------------------------------------
/**
 * A empty destructor.
 */
SysInfo::~SysInfo() {
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns system informations.
 *
 * This method returns system informations according to the parameter.
 * This method can be called from the JavascriptInterface class with the
 * method JavascriptInterface::getSysInfo(const QString& info).
 *
 * @param infoName
 *   Is of type QString. Defines which method will be called. Possible values are:
 *     - mbserial
 *     - usb
 *
 * @return QString
 *   the output of the called method or "info_error" if an error occurred
 *   (e. g. invalid parameter).
 *
 * @see JavascriptInterface::getSysInfo(const QString& info)
 */
const QString SysInfo::getInfo(const QString& infoName) {
   LOG4CXX_DEBUG(siLogger, "requested " << infoName);
   if (infoName == QString("mbserial"))
      return getMainboardSerial();
   else if (infoName == QString("usb"))
      return getUsbVendorIdProductIdSerialNumber();
   else if (infoName == QString("mac"))
      return getMACAddress();

   /* unknown code */
   LOG4CXX_DEBUG(siLogger, "unknown requested");
   return "info_error";
}
const QString SysInfo::getMACAddress() {
   // Returns MAC address of eth0 for now
   QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
   if (!qni.isValid()) {
      //qxtLog->debug() << "[sysinfo] MAC Address: No interface matching \"eth0\" found.";
      return "no_eth0";
   }
   //eth0_index = qni.index();
   return qni.hardwareAddress();
}
// -----------------------------------------------------------------------------------------------
//                         Mainboard / USB Infos using libsysfs
// -----------------------------------------------------------------------------------------------
/**
 * This method returns the Mainboard Serial Number.
 *
 * This method returns the Mainboard Serial Number. The mainboard serial
 * number is used as part of the data to compute the hardwarehash of the
 * client machine. To call this method use the
 * SysInfo::getInfo(const QString& infoName) method with
 * the parameter "mbserial"
 *
 * @return QString
 *   the mainboard serial or "mainboard_serial_error" if an error occurred.
 *
 * @see fbgui::generatePOSTData()
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getMainboardSerial() {
   QString out = "";
   struct sysfs_class_device *class_device = sysfs_open_class_device("dmi",
         "id");
   struct dlist *attrlist = sysfs_get_classdev_attributes(class_device);
   struct sysfs_device *device = sysfs_get_classdev_device(class_device);

   if (attrlist != NULL) {
      struct sysfs_attribute *attr = NULL;
      dlist_for_each_data(attrlist, attr, struct sysfs_attribute) {
         QVariantMap a;
         if(QString(attr->name) == QString("board_serial")) {
            out = QString(attr->value);
         }
      }
      LOG4CXX_DEBUG(siLogger, "Mainboard Serial: " + out);
      return out;
   }
   LOG4CXX_DEBUG(siLogger, "Mainboard Serial: attrlist is null! return: mainboard_serial_error");
   sysfs_close_class_device(class_device);
   return "mainboard_serial_error";
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns inforamtions about connected usb devices.
 *
 * This method returns the inforamtions about connected usb devices
 * as a json formated string.
 * Those informations are:
 *   - the vendor
 *   - the vendorID
 *   - the product
 *   - the productID
 *   - the manufacturer
 *   - the serial number
 *   To call this method use the SysInfo::getInfo(const QString& infoName)
 *   method with the parameter "usb"
 *
 * @return QString
 *   all above described informations as a json formated string or "error"
 *   if an error occurred.
 *
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getUsbVendorIdProductIdSerialNumber() {

   QString tag = "Usb Serial:";
   QString out = "";
   QVariantList list;

   libusb_device **devs;
   libusb_context *ctx = NULL; //a libusb session
   ssize_t cnt; //holding number of devices in list
   int r = 1;
   r = libusb_init(&ctx);
   if (r < 0) {
      LOG4CXX_DEBUG(siLogger, tag + "failed to initialise libusb");
      return "error";
   }
   cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

   if (cnt < 0) {
      LOG4CXX_DEBUG(siLogger, tag + "Get Device Error"); //there was an error
   }
   LOG4CXX_DEBUG(siLogger, tag << cnt << " Devices in list."); //print total number of usb devices
   ssize_t i; //for iterating through the list#
   for (i = 0; i < cnt; i++) {
      //printdev(devs[i]); //print specs of this device

      QVariantMap infos;
      libusb_device *dev = devs[i];

      libusb_config_descriptor *config;
      int cr = 1;
      cr = libusb_get_config_descriptor(dev, 1, &config);
      if (cr < 0) {
         LOG4CXX_DEBUG(siLogger, "Couldn't open config desc, code: " << cr);
      }
      //LOG4CXX_DEBUG(siLogger, "bmAttribute: " << config->bmAttributes);

      libusb_device_descriptor desc;
      int re = libusb_get_device_descriptor(dev, &desc);
      if (re < 0) {
         LOG4CXX_DEBUG(siLogger, tag + "failed to get device descriptor");
         return "error";
      }

      infos.insert("vendorId", desc.idVendor);
      //LOG4CXX_DEBUG(siLogger, "idVendor: " << infos.value("vendorId").toString());
      infos.insert("productId", desc.idProduct);
      unsigned char string[256];
      libusb_device_handle *handle;
      re = libusb_open(dev, &handle);
      if (re != 0) {
         LOG4CXX_DEBUG(siLogger, tag + "failed to get handler / fail to open device");
         return "error";
      }
      re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
            string, sizeof(string));
      if (re < 0) {
         LOG4CXX_DEBUG(siLogger, tag + "failed to get SerialNumber");
         return "error";
      }
      //LOG4CXX_DEBUG(siLogger, "SN: " << string);
      infos.insert("serialnumber", QString((const char *) string));
      re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string,
            sizeof(string));
      if (re < 0) {
         LOG4CXX_DEBUG(siLogger, tag + "failed to get Product");
         return "error";
      }
      infos.insert("product", QString((const char *) string));
      re = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
            string, sizeof(string));
      if (re < 0) {
         LOG4CXX_DEBUG(siLogger, tag + "failed to get Product");
         return "error";
      }
      infos.insert("manuacturer", QString((const char *) string));

      list << infos;

      /*
      for (int i = 0; i < list.size(); ++i) {
         LOG4CXX_DEBUG(siLogger, "list: " << list[i].toString());
      }*/

      libusb_close(handle);
   }
   libusb_free_device_list(devs, 1); //free the list, unref the devices in it
   libusb_exit(ctx); //close the session


   /*
   QByteArray json = serializer.serialize(list);
   LOG4CXX_DEBUG(siLogger, tag + "json object: " + json);
   return json;
   */
}