#include #include #include #include #include #include #include #include #include "logreceiver.h" #include #include #include "status.h" #include "dhcp.h" LogReceiver::LogReceiver() { server = new QLocalServer(this); } LogReceiver::~LogReceiver() { } void LogReceiver::initAndRun() { if (!server->listen("/var/tmp/qt_c_socket_default")) { /* QMessageBox::critical(this, tr("LogReceiver"), tr( "Unable to start the server: %1.") .arg(server->errorString())); close(); */ // emit signal to the gui that a critial error occoured return; } connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); QList list = getListOfNetworkInterfaces(); //qDebug() << list.size(); //checkCarrierState(list); //qDebug() << list.size(); //checkCarrierState("eth1"); //checkCarrierState("eth0"); pathToDhcpcdExe = "/home/niklas/fbgui/workspace/customdhcpcd/src/dhcpcd"; dhcpcdArguments.append("-d"); QString ifName("eth1"); runDHCPCD(ifName); } void LogReceiver::handleNewConnection() { qDebug() << "New Connection arrived"; QLocalSocket * client = server ->nextPendingConnection(); clients.insert(client, client); connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater())); connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput())); } void LogReceiver::handleNewInput() { QObject* sender = const_cast (QObject::sender()); QLocalSocket* socket = static_cast (sender); QLocalSocket * client = clients.value(socket); QString data(client->readAll()); data = data.trimmed(); QStringList lines = data.split("\n"); for (int i=0; i < lines.length(); i++) { handleNewInputLine(lines.at(i)); } } void LogReceiver::handleNewInputLine(QString data) { QString logMsg(data); QString interface = logMsg.section(";",0,0); QString s_state = logMsg.section(";", 1, 1); QString s_subState = logMsg.section(";", 2, 2); QString msg = logMsg.section(";", 3, 3); int pBar = indexToIfaceNameMap.value(interface.trimmed()); //qDebug() << logMsg; //qDebug() << msg; int st = s_state.trimmed().toInt(); int sst = s_subState.trimmed().toInt(); switch (st) { case LOG_INFO: qDebug() << "received LOG_INFO"; qDebug() << sst; switch (sst) { case DHCP_DISCOVER: emit changeProgressBarValue(pBar,10); break; case DHCP_OFFER: emit changeProgressBarValue(pBar,20); break; case DHCP_REQUEST: emit changeProgressBarValue(pBar,30); break; case DHCP_DECLINE: break; case DHCP_ACK: emit changeProgressBarValue(pBar,100); break; case DHCP_NAK: break; case DHCP_RELEASE: break; case DHCP_INFORM: break; default: break; } qDebug() << msg; break; case LOG_ERR: qDebug() << "received stat_error"; break; default: qDebug() << logMsg; } //statusLabel->setText(logMsg); } QList LogReceiver::getListOfNetworkInterfaces() { QList nIList = QNetworkInterface::allInterfaces(); QList result; int i = 0; foreach(QNetworkInterface nI, nIList) { if (((!(nI.flags() & QNetworkInterface::CanBroadcast)|| nI.flags() & QNetworkInterface::IsLoopBack) || nI.flags() & QNetworkInterface::IsPointToPoint) || checkBlackList(nI.humanReadableName())) { continue; } if (!checkCarrierState(nI.humanReadableName())) { continue; } // qDebug() << nI.humanReadableName(); result.append(nI); interfacesMap.insert(i, nI); indexToIfaceNameMap.insert(nI.humanReadableName(), i); emit addNewInterface(nI.humanReadableName(), i); i++; } return result; } void LogReceiver::runDHCPCD(QList &interfaces) { foreach(QNetworkInterface ni, interfaces) { dhcpcdArguments.append(ni.humanReadableName()); QProcess * p = new QProcess(this); clientProcesses.insert(p->pid(),p); p->start(pathToDhcpcdExe,dhcpcdArguments); connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinished(int, QProcess::ExitStatus))); dhcpcdArguments.removeLast(); } } void LogReceiver::runDHCPCD(QString interface) { dhcpcdArguments.append(interface); QProcess * p = new QProcess(this); clientProcesses.insert(p->pid(),p); p->start(pathToDhcpcdExe,dhcpcdArguments); connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted())); connect(p, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleProcessFinished(int, QProcess::ExitStatus))); dhcpcdArguments.removeLast(); } void LogReceiver::checkCarrierState(QList &interfaces) { foreach(QNetworkInterface nI, interfaces) { if(checkCarrierState(nI.humanReadableName())) { // everything is fine, cable is plugged, // go on with the next interface continue; } else { // cable is unplugged, // remove interface out of the list, // remove interface out ot the index maps int i = indexToIfaceNameMap.value(nI.humanReadableName()); indexToIfaceNameMap.remove(nI.humanReadableName()); interfacesMap.remove(i); interfaces.removeAt(i); } } } bool LogReceiver::checkCarrierState(QString interface) { qDebug() << "check carrier state for interface " << interface; QByteArray ba = interface.toAscii(); const char * iface = ba.data(); struct sysfs_class_device *class_device = sysfs_open_class_device("net", iface); struct dlist *attrlist = sysfs_get_classdev_attributes(class_device); if (attrlist != NULL) { struct sysfs_attribute *attr = NULL; dlist_for_each_data(attrlist, attr, struct sysfs_attribute) { if (strcmp("carrier", attr->name) == 0) { QString value(attr->value); bool ok = false; bool * pok = &ok; int v = value.toInt(pok); if (*pok) { if (v == 1) { qDebug() << "carrier is 1. Cable is plugged. return true"; return true; } else { qDebug() << "carrier is 0. Cable is unplugged. return false"; return false; } } else { qDebug() << "conversion error"; } } } } else { qDebug() << "attrlist is Null"; } sysfs_close_class_device(class_device); return true; } void LogReceiver::handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { //QObject* sender = const_cast (QObject::sender()); //QProcess* process = static_cast (sender); QProcess* p = qobject_cast(QObject::sender()); QProcess * client = clientProcesses.value(p->pid()); qDebug() << "process finished: " << client->pid() << exitCode << exitStatus; } void LogReceiver::handleProcessStarted() { /* //QObject* sender = const_cast (QObject::sender()); QProcess* process = static_cast (QObject::sender()); */ //QProcess* p = qobject_cast(QObject::sender()); //QProcess * client = clientProcesses.value(process->pid()); qDebug() << "process started: "; } bool LogReceiver::checkBlackList(QString i) { if (i.startsWith("v", Qt::CaseInsensitive)) { return true; } else if(i.startsWith("d", Qt::CaseInsensitive)) { return true; } else { return false; } }