summaryrefslogblamecommitdiffstats
path: root/workspace/LogReceiver/logreceiver.cpp
blob: 554b7210b9edea07ad3c725b73d1adb4527370e7 (plain) (tree)
1
2
3
4
5
6
7
8
9
                
                     

                    
 

                    
                    
                    
                            



                          
                    
                  

 
                            

                                        
                                                              
                  


                                                                                                

                                                                       


                       

                                                                                    
                                                                     




                                    
                                                                                 
                                     
                               
                          
 






                             

                                             
 












                                                                             












                                                    
                             



                                                       
 

                                                        
                             
 
                          
 

                                     

                     


                                                

                                   
                                                

                                
                                                

                                  
                                                




                                  
                                                












                                  


                                
                      
                     


                                                  
                                   
         
 
                                       

 


                                                                             
                  


                                                                              

                                                                                  


                                 





























                                                                                                                          

                                                   
                                            
                                                                      
                    





                                                                   
                                                               





                                                                                              
                                             


         










                                                                                              




























































                                                                                                                    




                                                                   
                                                                   

                                                                  

                                                                                    



                                          





                                                                       
 



                                        
 
 










                                                         
 #include <QMap>
 #include <QtNetwork>
 #include <QProcess>


 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <syslog.h>
 #include <sysfs/libsysfs.h>

 #include "logreceiver.h"
 #include <qlocalserver.h>
 #include <qlocalsocket.h>
 #include "status.h"
 #include "dhcp.h"


LogReceiver::LogReceiver() {

	server = new QLocalServer(this);
	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<QNetworkInterface> 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);

}

LogReceiver::~LogReceiver() {

}


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*> (QObject::sender());
	QLocalSocket* socket = static_cast<QLocalSocket*> (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);

	//qDebug() << logMsg;

	//qDebug() << msg;

	int st = s_state.toInt();
	int sst = s_subState.toInt();

	switch (st) {
	case LOG_INFO:
		qDebug() << "received LOG_INFO";
		qDebug() << sst;
		switch (sst) {
		case DHCP_DISCOVER:
			handleProgress(pBar,10);
			break;
		case DHCP_OFFER:
			handleProgress(pBar,20);
			break;
		case DHCP_REQUEST:
			handleProgress(pBar,30);
			break;
		case DHCP_DECLINE:

			break;
		case DHCP_ACK:
			handleProgress(pBar,40);
			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<QNetworkInterface> LogReceiver::getListOfNetworkInterfaces() {
	QList<QNetworkInterface> nIList = QNetworkInterface::allInterfaces();
	QList<QNetworkInterface> 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(!(nI.flags() & QNetworkInterface::IsUp)) {
			qDebug() << nI.humanReadableName() + " is DOWN ";

			QProcess * p = new QProcess(this);
			QStringList args;
			args.append(nI.humanReadableName());
			args.append("up");
			p->start("ifconfig",args);
			connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted()));
			p->waitForFinished();
			qDebug() << nI.humanReadableName() + " is UP ";
/*
			QNetworkConfigurationManager manager;
			QList<QNetworkConfiguration> confList = manager.allConfigurations();
			foreach(QNetworkConfiguration nC, confList){
				if( nC.name() == "Auto eth1") {
					QNetworkConfiguration conf = manager.configurationFromIdentifier(nC.identifier());
					if(conf.isValid()) {
						QNetworkSession *session = new QNetworkSession(conf);
						session->open();
					}
					qDebug() << "conf is not valid";
				}
				qDebug() << nC.name();
				qDebug() << nC.identifier();
			}
*/

		//}
	   	qDebug() << nI.humanReadableName();
	   	result.append(nI);
	   	interfacesMap.insert(i, nI);
	   	indexToIfaceNameMap.insert(nI.humanReadableName(), i);
	   	i++;
	}
	return result;
}

void LogReceiver::runDHCPCD(QList<QNetworkInterface> &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<QNetworkInterface> &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*> (QObject::sender());
	QProcess* process = static_cast<QProcess*> (sender);
	QProcess* p = qobject_cast<QProcess * >(QObject::sender());

	QProcess * client = clientProcesses.value(process->pid());

	qDebug() << "process finished: " << client->pid() << exitCode << exitStatus;
}

void LogReceiver::handleProcessStarted() {

/*
	//QObject* sender = const_cast<QObject*> (QObject::sender());
	QProcess* process = static_cast<QProcess*> (QObject::sender());
*/
	//QProcess* p = qobject_cast<QProcess * >(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;
	}
}