From 87dc3eae8eeb774aecf005465ddd9eecdd829b8a Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 28 Jul 2011 16:00:49 +0200 Subject: better version of the logwriter.c and started to devide gui and logic --- workspace/LogReceiver/LogReceiver | Bin 60452 -> 65044 bytes workspace/LogReceiver/LogReceiver.pro | 12 ++- workspace/LogReceiver/logreceiver.cpp | 184 +++++++++++++++++++-------------- workspace/LogReceiver/logreceiver.h | 32 ++---- workspace/LogReceiver/main.cpp | 2 +- workspace/LogReceiver/ndgui.cpp | 73 +++++++++++++ workspace/LogReceiver/ndgui.h | 38 +++++++ workspace/LogReceiver/ndgui.ui | 19 ++++ workspace/customdhcpcd/src/dhcpcd.c | 2 +- workspace/customdhcpcd/src/logwriter.c | 77 +++++++------- workspace/customdhcpcd/src/logwriter.h | 15 ++- 11 files changed, 304 insertions(+), 150 deletions(-) create mode 100644 workspace/LogReceiver/ndgui.cpp create mode 100644 workspace/LogReceiver/ndgui.h create mode 100644 workspace/LogReceiver/ndgui.ui diff --git a/workspace/LogReceiver/LogReceiver b/workspace/LogReceiver/LogReceiver index b324cb7..f08dc0a 100755 Binary files a/workspace/LogReceiver/LogReceiver and b/workspace/LogReceiver/LogReceiver differ diff --git a/workspace/LogReceiver/LogReceiver.pro b/workspace/LogReceiver/LogReceiver.pro index 299de02..ef51cfb 100644 --- a/workspace/LogReceiver/LogReceiver.pro +++ b/workspace/LogReceiver/LogReceiver.pro @@ -3,9 +3,13 @@ TARGET = LogReceiver QT += core \ gui \ network -HEADERS += status.h \ +LIBS += -lsysfs +HEADERS += ndgui.h \ + status.h \ logreceiver.h -SOURCES += main.cpp \ +SOURCES += ndgui.cpp \ + main.cpp \ logreceiver.cpp -FORMS += logreceiver.ui -RESOURCES += \ No newline at end of file +FORMS += ndgui.ui \ + logreceiver.ui +RESOURCES += diff --git a/workspace/LogReceiver/logreceiver.cpp b/workspace/LogReceiver/logreceiver.cpp index 4aeebfc..554b721 100644 --- a/workspace/LogReceiver/logreceiver.cpp +++ b/workspace/LogReceiver/logreceiver.cpp @@ -1,4 +1,3 @@ - #include #include #include #include @@ -8,55 +7,41 @@ #include #include #include + #include #include "logreceiver.h" #include #include #include "status.h" + #include "dhcp.h" -LogReceiver::LogReceiver(QWidget *parent) : - QDialog(parent) { - ui.setupUi(this); - - statusLabel = new QLabel; - quitButton = new QPushButton(tr("Quit")); - quitButton->setAutoDefault(false); +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; } - statusLabel->setText(tr("The server is running.\n" - "Run the C Client example now.")); - - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); - QHBoxLayout *buttonLayout = new QHBoxLayout; - buttonLayout->addStretch(1); - buttonLayout->addWidget(quitButton); - buttonLayout->addStretch(1); - - QVBoxLayout *mainLayout = new QVBoxLayout; - mainLayout->addWidget(statusLabel); - mainLayout->addLayout(buttonLayout); - //setLayout(mainLayout); - QList list = getListOfNetworkInterfaces(); - buildGui(); - addInterfacesToGroupBox(interfacesMap); + //qDebug() << list.size(); + //checkCarrierState(list); + //qDebug() << list.size(); + //checkCarrierState("eth1"); + //checkCarrierState("eth0"); pathToDhcpcdExe = "/home/niklas/fbgui/workspace/customdhcpcd/src/dhcpcd"; - //dhcpcdArguments.append("-d"); + dhcpcdArguments.append("-d"); QString ifName("eth1"); - runDHCPCD(list); - + runDHCPCD(ifName); - setWindowTitle(tr("NetD")); } LogReceiver::~LogReceiver() { @@ -166,6 +151,36 @@ QList LogReceiver::getListOfNetworkInterfaces() { { 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 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); @@ -199,6 +214,67 @@ void LogReceiver::runDHCPCD(QString interface) { 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) { @@ -224,59 +300,7 @@ void LogReceiver::handleProcessStarted() { qDebug() << "process started: "; } -void LogReceiver::buildGui() { - - ndStatusLabel = new QLabel(tr("test")); - ndStatusLabel->setSizePolicy(QSizePolicy::Expanding, - QSizePolicy::Expanding); - ndStatusLabel->setAlignment(Qt::AlignCenter); - ndStatusLabel->setMinimumSize(100, 20); - - // create interface group box - createInterfaceGroupBox(); - - mainLayout = new QVBoxLayout; - mainLayout->addWidget(ndStatusLabel); - mainLayout->addWidget(interfaceGroupBox); - - setLayout(mainLayout); -} - -void LogReceiver::createInterfaceGroupBox(){ - interfaceGroupBox = new QGroupBox(tr("Interfaces")); - - interfaceGroupBoxLayout = new QVBoxLayout; - /* add interfaces via addInterfacesToGroupBox()*/ - - interfaceGroupBox->setLayout(interfaceGroupBoxLayout); -} - -void LogReceiver::addInterfacesToGroupBox(QMap &interfaces) { - for(int i = 0; i < interfaces.size(); i++) { - QHBoxLayout *hBoxLayout = new QHBoxLayout; - QLabel *label = new QLabel(interfaces.value(i).humanReadableName()); - QProgressBar *pBar = new QProgressBar(this); - pBar->setRange(1,100); - pBar->setMaximumSize(200,20); - - progressBars.insert(i,pBar); - - hBoxLayout->addWidget(label, Qt::AlignLeft); - hBoxLayout->addWidget(pBar, Qt::AlignRight); - - interfaceGroupBoxLayout->addLayout(hBoxLayout,2); - } -} -void LogReceiver::handleProgress(int iFaceIndex, int newValue) { - QProgressBar * pBar = progressBars.value(iFaceIndex); - if(newValue >= pBar->value()) { - pBar->setValue(newValue); - } - else { - qDebug() << "Error: new value is smaller than the old value!"; - } -} bool LogReceiver::checkBlackList(QString i) { if (i.startsWith("v", Qt::CaseInsensitive)) { diff --git a/workspace/LogReceiver/logreceiver.h b/workspace/LogReceiver/logreceiver.h index 2dce00a..e4592c0 100644 --- a/workspace/LogReceiver/logreceiver.h +++ b/workspace/LogReceiver/logreceiver.h @@ -1,25 +1,19 @@ #ifndef LOGRECEIVER_H #define LOGRECEIVER_H -#include -#include #include #include -#include -#include -#include -#include "ui_logreceiver.h" class QLabel; class QPushButton; class QLocalServer; class QLocalSocket; -class LogReceiver: public QDialog { +class LogReceiver: public QObject { Q_OBJECT public: - LogReceiver(QWidget *parent = 0); + LogReceiver(); ~LogReceiver(); private slots: @@ -28,12 +22,9 @@ private slots: void handleNewInputLine(QString data); void handleProcessFinished(int exitCode, QProcess::ExitStatus exitStatus); void handleProcessStarted(); - void handleProgress(int iFaceIndex, int newValue); + private: - Ui::LogReceiverClass ui; - QLabel *statusLabel; - QPushButton *quitButton; QLocalServer *server; quint16 blockSize; QMap interfacesMap; @@ -43,24 +34,13 @@ private: QString pathToDhcpcdExe; QStringList dhcpcdArguments; - - /*gui elements*/ - QMap progressBars; - QLabel *ndStatusLabel; - QGroupBox *interfaceGroupBox; - QVBoxLayout *mainLayout; - QVBoxLayout *interfaceGroupBoxLayout; - /**/ - void runDHCPCD(QList &interfaces); void runDHCPCD(QString interface); + void checkCarrierState(QList &interfaces); + bool checkCarrierState(QString interface); QList getListOfNetworkInterfaces(); - - /*gui functions*/ - void buildGui(); - void createInterfaceGroupBox(); - void addInterfacesToGroupBox(QMap &interfaces); bool checkBlackList(QString i); + }; #endif // LOGRECEIVER_H diff --git a/workspace/LogReceiver/main.cpp b/workspace/LogReceiver/main.cpp index e545906..52d0ac9 100644 --- a/workspace/LogReceiver/main.cpp +++ b/workspace/LogReceiver/main.cpp @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - LogReceiver w; + ndgui w; w.show(); return a.exec(); } diff --git a/workspace/LogReceiver/ndgui.cpp b/workspace/LogReceiver/ndgui.cpp new file mode 100644 index 0000000..0d4c505 --- /dev/null +++ b/workspace/LogReceiver/ndgui.cpp @@ -0,0 +1,73 @@ +#include "ndgui.h" + +ndgui::ndgui(QWidget *parent) + : QWidget(parent) +{ + ui.setupUi(this); + + buildGui(); + addInterfacesToGroupBox(list); + + + setWindowTitle(tr("NetD")); +} + +ndgui::~ndgui() +{ + +} + +void ndgui::buildGui() { + + ndStatusLabel = new QLabel(tr("test")); + ndStatusLabel->setSizePolicy(QSizePolicy::Expanding, + QSizePolicy::Expanding); + ndStatusLabel->setAlignment(Qt::AlignCenter); + ndStatusLabel->setMinimumSize(100, 20); + + // create interface group box + createInterfaceGroupBox(); + + mainLayout = new QVBoxLayout; + mainLayout->addWidget(ndStatusLabel); + mainLayout->addWidget(interfaceGroupBox); + + setLayout(mainLayout); +} + +void ndgui::createInterfaceGroupBox(){ + interfaceGroupBox = new QGroupBox(tr("Interfaces")); + + interfaceGroupBoxLayout = new QVBoxLayout; + /* add interfaces via addInterfacesToGroupBox()*/ + + interfaceGroupBox->setLayout(interfaceGroupBoxLayout); +} + +void ndgui::addInterfacesToGroupBox(QList &interfaces) { + foreach(QNetworkInterface nI, interfaces){ + int index = indexToIfaceNameMap.value(nI.humanReadableName()); + QHBoxLayout *hBoxLayout = new QHBoxLayout; + QLabel *label = new QLabel(nI.humanReadableName()); + QProgressBar *pBar = new QProgressBar(this); + pBar->setRange(1,100); + pBar->setMaximumSize(200,20); + + progressBars.insert(index,pBar); + + hBoxLayout->addWidget(label, Qt::AlignLeft); + hBoxLayout->addWidget(pBar, Qt::AlignRight); + + interfaceGroupBoxLayout->addLayout(hBoxLayout,2); + } +} + +void ndgui::handleProgress(int iFaceIndex, int newValue) { + QProgressBar * pBar = progressBars.value(iFaceIndex); + if(newValue >= pBar->value()) { + pBar->setValue(newValue); + } + else { + qDebug() << "Error: new value is smaller than the old value!"; + } +} diff --git a/workspace/LogReceiver/ndgui.h b/workspace/LogReceiver/ndgui.h new file mode 100644 index 0000000..01b9d01 --- /dev/null +++ b/workspace/LogReceiver/ndgui.h @@ -0,0 +1,38 @@ +#ifndef NDGUI_H +#define NDGUI_H + +#include +#include "ui_ndgui.h" + +class ndgui: public QWidget { +Q_OBJECT + +public: + ndgui(QWidget *parent = 0); + ~ndgui(); + +public slots: + void handleProgress(int iFaceIndex, int newValue); + +private: + Ui::ndguiClass ui; + + /*gui elements*/ + QMap progressBars; + QLabel *ndStatusLabel; + QGroupBox *interfaceGroupBox; + QVBoxLayout *mainLayout; + QVBoxLayout *interfaceGroupBoxLayout; + /**/ + + /*gui functions*/ + void buildGui(); + void createInterfaceGroupBox(); + void addInterfacesToGroupBox(QList &interfaces); + + + + +}; + +#endif // NDGUI_H diff --git a/workspace/LogReceiver/ndgui.ui b/workspace/LogReceiver/ndgui.ui new file mode 100644 index 0000000..4a4c466 --- /dev/null +++ b/workspace/LogReceiver/ndgui.ui @@ -0,0 +1,19 @@ + + ndguiClass + + + + 0 + 0 + 400 + 300 + + + + ndgui + + + + + + diff --git a/workspace/customdhcpcd/src/dhcpcd.c b/workspace/customdhcpcd/src/dhcpcd.c index 550c25a..43ce3ed 100644 --- a/workspace/customdhcpcd/src/dhcpcd.c +++ b/workspace/customdhcpcd/src/dhcpcd.c @@ -646,7 +646,7 @@ int main (int argc, char **argv) /* Massage our filters per platform */ setup_packet_filters (); - /*dhcp_run : defined in client.c*/ + /* dhcp_run : defined in client.c */ if (dhcp_run (options, &pidfd) == 0) retval = EXIT_SUCCESS; diff --git a/workspace/customdhcpcd/src/logwriter.c b/workspace/customdhcpcd/src/logwriter.c index 8b6d6dc..be8ee26 100644 --- a/workspace/customdhcpcd/src/logwriter.c +++ b/workspace/customdhcpcd/src/logwriter.c @@ -11,23 +11,24 @@ #include "common.h" #include "dhcp.h" +#include "dhcpcd.h" #include "logger.h" #include "logwriter.h" #include "status.h" /*sockets for the logger and the qt-reader */ int sockfd, ns; -int retval; -char *socketName; -char *interfaceName; +int retval = -1; +char socketName[QTSOCKETADDRESSLENGTH]; +char interfaceName[IF_NAMESIZE]; char mesg[256] = {0}; -void setSocketName(char * sn) { - socketName = sn; +void setSocketName(const char * sn) { + snprintf(socketName, sizeof(socketName), "%s", sn); } void setInterfaceName(const char * in){ - interfaceName = in; + snprintf(interfaceName, sizeof(interfaceName), "%s", in); } int initQtLoggerSocket() { @@ -74,62 +75,66 @@ void logToQt(char * status, char * substatus, char * msg) { */ -void sendToQt(char *msg) { - int n = write(sockfd, msg, strlen(msg)); +void sendToQt(log_msg * msg) { + int n = -1; + const char *tpl = "%s;%d;%d;%s\n"; + char *outbuf; + size_t outbuf_size = + sizeof(char)*4 + // ";" *3 + newline + sizeof(int)*2 + // status, substatus + sizeof(msg->device) + // devicename + sizeof(msg->msg); // msg + outbuf = malloc(outbuf_size); + memset(outbuf,0,outbuf_size); + snprintf(outbuf, sizeof(char)*3 + sizeof(int)*2 + sizeof(msg->device) + sizeof(msg->msg), tpl, msg->device, msg->status, msg->substatus, msg->msg); + if (outbuf != NULL){ + n = write(sockfd, outbuf, outbuf_size); + } + free(outbuf); + syslog (LOG_INFO, "[fbgui] INFO writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); // fflush(sockfd); - if (n < 0) - syslog (LOG_ERR, sprintf("[fbgui] ERROR writing to socket: %s", msg)); + if (n < 0) { + syslog (LOG_ERR, "[fbgui] ERROR writing to socket: [%d:%d] %s (%s)", msg->status, msg->substatus, msg->msg, msg->device); // fprintf(stdout, "ERROR writing to socket: %s", msg); + } } void logToQt(int status, int substatus, const char * msg) { if (retval >= 0) { - char * st = (char *) malloc(sizeof(int) * 4 + 1); - char * sst = (char *) malloc(sizeof(int) * 4 + 1); - sprintf(st, "%d", status); - sprintf(sst, "%d", substatus); - char * m = (char *) malloc(strlen(interfaceName) + strlen(st) + strlen( - sst) + strlen(msg) + 4); - strcpy(m, interfaceName); - strcat(m, ";"); - strcat(m, st); - strcat(m, ";"); - strcat(m, sst); - strcat(m, ";"); - strcat(m, msg); - sendToQt(m); - - free(st); - free(sst); - free(m); + log_msg lm; + lm.status = status; + lm.substatus = substatus; + snprintf(lm.msg, sizeof(lm.msg), "%s", msg); + snprintf(lm.device, sizeof(lm.device), "%s", interfaceName); + sendToQt(&lm); } } void logSendToQt(int type) { switch(type) { case DHCP_DISCOVER: - logToQt(LOG_INFO, DHCP_DISCOVER, ""); + logToQt(LOG_INFO, DHCP_DISCOVER, "send discover"); break; case DHCP_OFFER: - logToQt(LOG_INFO, DHCP_OFFER, ""); + logToQt(LOG_INFO, DHCP_OFFER, "send offer"); break; case DHCP_REQUEST: - logToQt(LOG_INFO, DHCP_REQUEST, ""); + logToQt(LOG_INFO, DHCP_REQUEST, "send request"); break; case DHCP_DECLINE: - logToQt(LOG_INFO, DHCP_DECLINE, ""); + logToQt(LOG_INFO, DHCP_DECLINE, "send decline"); break; case DHCP_ACK: - logToQt(LOG_INFO, DHCP_ACK, ""); + logToQt(LOG_INFO, DHCP_ACK, "send ack"); break; case DHCP_NAK: - logToQt(LOG_INFO, DHCP_NAK, ""); + logToQt(LOG_INFO, DHCP_NAK, "send nak"); break; case DHCP_RELEASE: - logToQt(LOG_INFO, DHCP_RELEASE, ""); + logToQt(LOG_INFO, DHCP_RELEASE, "send release"); break; case DHCP_INFORM: - logToQt(LOG_INFO, DHCP_INFORM, ""); + logToQt(LOG_INFO, DHCP_INFORM, "send inform"); break; default : break; diff --git a/workspace/customdhcpcd/src/logwriter.h b/workspace/customdhcpcd/src/logwriter.h index 84d409d..1a755fc 100644 --- a/workspace/customdhcpcd/src/logwriter.h +++ b/workspace/customdhcpcd/src/logwriter.h @@ -8,10 +8,22 @@ #ifndef LOGWRITER_H_ #define LOGWRITER_H_ +#include "dhcpcd.h" + +#define LOG_MSG_SIZE 1024 + +typedef struct _log_msg log_msg; +struct _log_msg { + int status; + int substatus; + char device[IF_NAMESIZE]; + char msg[LOG_MSG_SIZE]; +}; + /** * new functions for communicating with Qt */ -void setSocketName(char * sn); +void setSocketName(const char * sn); void setInterfaceName(const char * in); int initQtLoggerSocket (); void closeQtLoggerSocket (); @@ -22,5 +34,4 @@ void logLoggerToQt(int level, const char *fmt, va_list args); //void logToQt(char * status, char * substatus, char * msg); - #endif /* LOGWRITER_H_ */ -- cgit v1.2.3-55-g7522