From 8fbc0a431cc25292978323edc2f53bfa9f2c3c96 Mon Sep 17 00:00:00 2001 From: Niklas Date: Mon, 10 Oct 2011 17:07:14 +0200 Subject: corrected some errors --- src/sysinfo.cpp | 167 +++++--------------------------------------------------- 1 file changed, 15 insertions(+), 152 deletions(-) (limited to 'src/sysinfo.cpp') diff --git a/src/sysinfo.cpp b/src/sysinfo.cpp index 4797a86..7d6ac92 100644 --- a/src/sysinfo.cpp +++ b/src/sysinfo.cpp @@ -22,8 +22,6 @@ SysInfo::~SysInfo() { * * @param infoName * Is of type QString. Defines which method will be called. Possible values are: - * - mac - * - ip * - mbserial * - usb * @@ -35,121 +33,17 @@ SysInfo::~SysInfo() { */ const QString SysInfo::getInfo(const QString& infoName) { qxtLog->debug() << "[sysinfo] requested " << infoName; - if (infoName == QString("mac")) - return getMACAddress(); - else if (infoName == QString("ip")) - return getIPAddress(); - else if (infoName == QString("all")) - return getAllInfos(); - else if (infoName == QString("mbserial")) + if (infoName == QString("mbserial")) return getMainboardSerial(); else if (infoName == QString("usb")) return getUsbVendorIdProductIdSerialNumber(); - else if (infoName == QString("json")) - return getNames(); /* unknown code */ qxtLog->debug() << "[sysinfo] unknown requested"; return "info_error"; } -// ------------------------------------------------------------------------------------------------ -/** - * This method returns the clients MAC-Address. - * - * This method returns the clients MAC-Address of the "eth0" interface. - * The MAC-Address 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 "mac" - * - * @return QString - * the MAC-Address or "no_eth0" if an error occurred. - * - * @see fbgui::generatePOSTData() - * @see SysInfo::getInfo(const QString& infoName) - */ -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(); -} -// ------------------------------------------------------------------------------------------------ -/** - * This method returns the clients IP-Address. - * - * This method returns the clients IP-Address of the "eth0" interface. - * To call this method use the - * SysInfo::getInfo(const QString& infoName) method with - * the parameter "ip" - * - * @return QString - * the IP-Address or "ip_error" if an error occurred. - * - * @see SysInfo::getInfo(const QString& infoName) - */ -const QString SysInfo::getIPAddress() { - // Again for eth0 only at the moment. - // TODO: this doesn't quite work yet... - QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0")); - QList addrlist = qni.allAddresses(); - // This is does not return the right IP atm... - foreach(QHostAddress addr, addrlist) - { - if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr != QHostAddress::LocalHost) { - return addr.toString(); - } - } - // still here? - qxtLog->debug() << "[sysinfo] IP Address: ip_error"; - return "ip_error"; -} -// ------------------------------------------------------------------------------------------------ -/** - * just a test method for json. - */ -const QByteArray SysInfo::getNames() { - - QVariantMap foo; - foo.insert("name", "foo"); - foo.insert("type", 123); - - QVariantMap fooo; - fooo.insert("name", "boo"); - fooo.insert("type", 321); - - QVariantList jsonV; - jsonV << foo << fooo; - - QJson::Serializer serializer; - QByteArray json = serializer.serialize(jsonV); - - qxtLog->debug() << json; - return json; - -} -// ------------------------------------------------------------------------------------------------ -/** - * just a test method for json. - */ -QString SysInfo::getAllInfos() { - QVariantMap infos; - infos.insert("mac", getMACAddress()); - infos.insert("ip", getIPAddress()); - infos.insert("whoami", getScriptOutput("whoami")); - //infos.insert("pwd", getScriptOutput("pwd")); - - //QJson::Serializer serializer; - QByteArray json = serializer.serialize(infos); - - qxtLog->debug() << json; - return json; -} -// ------------------------------------------------------------------------------------------------ - +// ----------------------------------------------------------------------------------------------- +// Mainboard / USB Infos using libsysfs +// ----------------------------------------------------------------------------------------------- /** * This method returns the Mainboard Serial Number. * @@ -167,7 +61,8 @@ QString SysInfo::getAllInfos() { */ const QString SysInfo::getMainboardSerial() { QString out = ""; - struct sysfs_class_device *class_device = sysfs_open_class_device("dmi", "id"); + 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); @@ -210,6 +105,7 @@ const QString SysInfo::getMainboardSerial() { * @see SysInfo::getInfo(const QString& infoName) */ const QString SysInfo::getUsbVendorIdProductIdSerialNumber() { + QString tag = "[sysinfo] Usb Serial:"; QString out = ""; QVariantList list; @@ -248,19 +144,22 @@ const QString SysInfo::getUsbVendorIdProductIdSerialNumber() { qxtLog->debug() << tag + "failed to get handler / fail to open device"; return "error"; } - re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string)); + re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, + string, sizeof(string)); if (re < 0) { qxtLog->debug() << tag + "failed to get SerialNumber"; return "error"; } infos.insert("serialnumber", QString((const char *) string)); - re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string)); + re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, + sizeof(string)); if (re < 0) { qxtLog->debug() << 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)); + re = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, + string, sizeof(string)); if (re < 0) { qxtLog->debug() << tag + "failed to get Product"; return "error"; @@ -273,45 +172,9 @@ const QString SysInfo::getUsbVendorIdProductIdSerialNumber() { 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); qxtLog->debug() << tag + "json object: " + json; return json; + */ } - -// ------------------------------------------------------------------------------------------------ -/** - * This method returns the output of the provided script. - * - * This method returns the output of the provided script. - * Script could be any command. - * This method is not used so far. - * - * @param cmd - * Is of type QString. The command which will be executed - * - * @return QString - * output of the script. - */ -QString SysInfo::getScriptOutput(QString cmd) { - QProcess *process = new QProcess(); - qxtLog->debug() << "[sysinfo] Script Output: try to open: " << cmd; - process->start(cmd, QIODevice::ReadOnly); - - if (!process->waitForStarted()) - qxtLog->debug() << "[sysinfo] Script Output: process couldn't get opened"; - - QString output; - process->waitForFinished(); - - QTextStream *txt_stream = new QTextStream(process); - - while (!txt_stream->atEnd()) { - qxtLog->debug() << "[sysinfo] Script Output: read output: "; - QString tmp_str = txt_stream->readLine(); - output += tmp_str; - qxtLog->debug() << "[sysinfo] Script Output: " << tmp_str; - } - qxtLog->debug() << "[sysinfo] Script Output: process finished: "; - return output; -} - -- cgit v1.2.3-55-g7522