From 40847fd153dad8509c45670139e916d81c2a0850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 7 Jul 2015 17:35:27 +0200 Subject: [PATCH] added guhd.conf add settings to tcpserver --- data/config/guhd.conf | 8 ++ debian/guhd.install | 1 + libguh/guhsettings.cpp | 2 +- libguh/hardware/radio433/radio433.cpp | 12 ++- libguh/libguh.pro | 4 +- server/main.cpp | 1 + server/tcpserver.cpp | 145 +++++++++++++++++++------- server/tcpserver.h | 13 ++- 8 files changed, 143 insertions(+), 43 deletions(-) create mode 100644 data/config/guhd.conf diff --git a/data/config/guhd.conf b/data/config/guhd.conf new file mode 100644 index 00000000..8aa548e0 --- /dev/null +++ b/data/config/guhd.conf @@ -0,0 +1,8 @@ +[JSONRPC] +port=12345 +interfaces="lo","all" +ip="IPv4", "IPv6" + +[GPIO] +rf433rx=27 +rf433tx=22 diff --git a/debian/guhd.install b/debian/guhd.install index 759a0b0a..7bc31ca6 100644 --- a/debian/guhd.install +++ b/debian/guhd.install @@ -1,2 +1,3 @@ usr/bin/guhd data/init/* /etc/init.d +data/config/guhd.conf /etc/guh/guhd.conf diff --git a/libguh/guhsettings.cpp b/libguh/guhsettings.cpp index bff94cbc..ba2446d7 100644 --- a/libguh/guhsettings.cpp +++ b/libguh/guhsettings.cpp @@ -188,7 +188,7 @@ void GuhSettings::remove(const QString &key) void GuhSettings::setValue(const QString &key, const QVariant &value) { - Q_ASSERT_X(m_role != GuhSettings::SettingsRoleGlobal, "GuhSettings", "Bad settings implementation. The global settings file is read only."); + Q_ASSERT_X(m_role != GuhSettings::SettingsRoleGlobal, "GuhSettings", "Bad settings usage. The global settings file should be read only."); m_settings->setValue(key, value); } diff --git a/libguh/hardware/radio433/radio433.cpp b/libguh/hardware/radio433/radio433.cpp index da134cc3..3b587a4a 100644 --- a/libguh/hardware/radio433/radio433.cpp +++ b/libguh/hardware/radio433/radio433.cpp @@ -51,6 +51,7 @@ #include "radio433.h" #include "loggingcategories.h" +#include "guhsettings.h" #include @@ -58,11 +59,16 @@ Radio433::Radio433(QObject *parent) : QObject(parent) { - #ifdef GPIO433 - m_receiver = new Radio433Receiver(this,27); - m_transmitter = new Radio433Trasmitter(this,22); + GuhSettings settings(GuhSettings::SettingsRoleGlobal); + qCDebug(dcHardware) << "Loading GPIO settings from:" << settings.fileName(); + settings.beginGroup("GPIO"); + int receiverGpioNumber = settings.value("rf433rx",27).toInt(); + int transmitterGpioNumber = settings.value("rf433tx",22).toInt(); + settings.endGroup(); + m_receiver = new Radio433Receiver(this, receiverGpioNumber); + m_transmitter = new Radio433Trasmitter(this, transmitterGpioNumber); connect(m_receiver, &Radio433Receiver::readingChanged, this, &Radio433::readingChanged); connect(m_receiver, &Radio433Receiver::dataReceived, this, &Radio433::dataReceived); #endif diff --git a/libguh/libguh.pro b/libguh/libguh.pro index bfa9ad1e..e2a51bb4 100644 --- a/libguh/libguh.pro +++ b/libguh/libguh.pro @@ -38,7 +38,7 @@ SOURCES += plugin/device.cpp \ types/ruleactionparam.cpp \ types/statedescriptor.cpp \ loggingcategories.cpp \ - guhsettings.cpp + guhsettings.cpp \ HEADERS += plugin/device.h \ plugin/deviceclass.h \ @@ -72,5 +72,5 @@ HEADERS += plugin/device.h \ types/statedescriptor.h \ typeutils.h \ loggingcategories.h \ - guhsettings.h + guhsettings.h \ diff --git a/server/main.cpp b/server/main.cpp index f2a56815..f15d705a 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -86,6 +86,7 @@ int main(int argc, char *argv[]) parser.addOption(foregroundOption); QString debugDescription = QString("Debug categories to enable. Prefix with \"No\" to disable. Warnings from all categories will be printed unless explicitly muted with \"NoWarnings\". \n\nCategories are:"); + // create sorted loggingFiler list QStringList sortedFilterList = QStringList(s_loggingFilters.keys()); sortedFilterList.sort(); diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 06e98699..879477ad 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -31,32 +31,35 @@ namespace guhserver { TcpServer::TcpServer(QObject *parent) : QObject(parent) { - qCDebug(dcConnection) << "----------------------------"; - qCDebug(dcConnection) << "network interfaces:"; - foreach(const QNetworkInterface &interface, QNetworkInterface::allInterfaces()){ - qCDebug(dcConnection) << " -------------------------"; - qCDebug(dcConnection) << " name :" << interface.name(); - if(!interface.addressEntries().isEmpty()){ - qCDebug(dcConnection) << " ip :" << interface.addressEntries().first().ip().toString(); - } - qCDebug(dcConnection) << " mac : " << interface.hardwareAddress(); - } - qCDebug(dcConnection) << "----------------------------"; + // Timer for scanning network interfaces ever 5 seconds + // Note: QNetworkConfigurationManager does not work on embedded platforms + m_timer = new QTimer(this); + m_timer->setInterval(5000); + connect (m_timer, &QTimer::timeout, this, &TcpServer::onTimeout); - // load settings - bool ok; + // load JSON-RPC server settings GuhSettings settings(GuhSettings::SettingsRoleGlobal); + qCDebug(dcConnection) << "Loading TCP server settings from:" << settings.fileName(); settings.beginGroup("JSONRPC"); - // TODO: handle interfaces in settings (enable just localhost ecc...) + // load port + m_port = settings.value("port", 1234).toUInt(); - uint port = settings.value("port", 1234).toUInt(&ok); - settings.endGroup(); - if(ok){ - m_port = port; + // load interfaces + QStringList interfaceList = settings.value("interfaces", QStringList("all")).toStringList(); + if (interfaceList.contains("all")) { + m_networkInterfaces = QNetworkInterface::allInterfaces(); } else { - m_port = 1234; + foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) { + if (interfaceList.contains(interface.name())) { + m_networkInterfaces.append(interface); + } + } } + + // load IP versions (IPv4, IPv6 or any) + m_ipVersions = settings.value("ip", QStringList("any")).toStringList(); + settings.endGroup(); } void TcpServer::sendData(const QList &clients, const QByteArray &data) @@ -66,6 +69,7 @@ void TcpServer::sendData(const QList &clients, const QByteArray &data) } } + void TcpServer::sendData(const QUuid &clientId, const QByteArray &data) { QTcpSocket *client = m_clientList.value(clientId); @@ -98,11 +102,11 @@ void TcpServer::readPackage() QTcpSocket *client = qobject_cast(sender()); qCDebug(dcConnection) << "data comming from" << client->peerAddress().toString(); QByteArray message; - while(client->canReadLine()){ + while (client->canReadLine()) { QByteArray dataLine = client->readLine(); qCDebug(dcConnection) << "line in:" << dataLine; message.append(dataLine); - if(dataLine.endsWith('\n')){ + if (dataLine.endsWith('\n')) { emit dataAvailable(m_clientList.key(client), message); message.clear(); } @@ -117,37 +121,106 @@ void TcpServer::slotClientDisconnected() m_clientList.take(clientId)->deleteLater(); } +void TcpServer::onTimeout() +{ + // // check all networkinterfaces + // bool ipV4 = m_ipVersions.contains("IPv4"); + // bool ipV6 = m_ipVersions.contains("IPv6"); + // if (m_ipVersions.contains("any")) { + // ipV4 = ipV6 = true; + // } + + // QList m_serversToCreate; + // QList m_serversToDelete; + + // // check all available interfaces + // foreach (const QNetworkInterface &interface, m_networkInterfaces) { + // QList addresseEntries = interface.addressEntries(); + // QList addresses; + + // foreach (QNetworkAddressEntry entry, addresseEntries) { + // if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { + // addresses.append(entry.ip()); + // } + // if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) { + // addresses.append(entry.ip()); + // } + // } + + // // check each host address of this interface + + // foreach (QTcpServer *s, m_serverList) { + // if (!addresses.contains(s->serverAddress())) + // return; + + + + + // } + // } +} + bool TcpServer::startServer() { - // Listen on all Networkinterfaces - foreach(const QHostAddress &address, QNetworkInterface::allAddresses()){ - QTcpServer *server = new QTcpServer(this); - if(server->listen(address, m_port)) { - qCDebug(dcConnection) << "JSON-RPC server listening on" << address.toString() << ":" << m_port; - connect(server, SIGNAL(newConnection()), SLOT(newClientConnected())); - m_serverList.insert(QUuid::createUuid(), server); - } else { - qCWarning(dcConnection) << "can not listening to" << address.toString() << ":" << m_port; - delete server; + qCDebug(dcConnection) << "----------------------------"; + qCDebug(dcConnection) << "JSON-RPC server listening on:"; + qCDebug(dcConnection) << "----------------------------"; + + bool ipV4 = m_ipVersions.contains("IPv4"); + bool ipV6 = m_ipVersions.contains("IPv6"); + if (m_ipVersions.contains("any")) { + ipV4 = ipV6 = true; + } + + foreach (const QNetworkInterface &interface, m_networkInterfaces) { + QList addresseEntries = interface.addressEntries(); + QList addresses; + + foreach (QNetworkAddressEntry entry, addresseEntries) { + if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { + addresses.append(entry.ip()); + } + if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) { + addresses.append(entry.ip()); + } + } + + foreach(const QHostAddress &address, addresses){ + QTcpServer *server = new QTcpServer(this); + if(server->listen(address, m_port)) { + qCDebug(dcConnection) << "\tname |" << interface.name(); + qCDebug(dcConnection) << "\tip |" << address.toString(); + qCDebug(dcConnection) << "\tport |" << m_port; + qCDebug(dcConnection) << "\tmac |" << interface.hardwareAddress(); + qCDebug(dcConnection) << "\t-----+-------------------"; + + connect(server, SIGNAL(newConnection()), SLOT(newClientConnected())); + m_serverList.insert(QUuid::createUuid(), server); + } else { + qCWarning(dcConnection) << "ERROR: can not listen to" << interface.name() << address.toString() << m_port; + delete server; + } } } - if(m_serverList.empty()){ + + if (m_serverList.empty()) return false; - } + return true; } bool TcpServer::stopServer() { // Listen on all Networkinterfaces - foreach(QTcpServer *server, m_serverList){ + foreach (QTcpServer *server, m_serverList) { qCDebug(dcConnection) << "close server " << server->serverAddress().toString(); server->close(); delete server; } - if(!m_serverList.empty()){ + + if (!m_serverList.empty()) return false; - } + return true; } diff --git a/server/tcpserver.h b/server/tcpserver.h index c3b204b9..0ecdccf3 100644 --- a/server/tcpserver.h +++ b/server/tcpserver.h @@ -23,10 +23,11 @@ #define TCPSERVER_H #include -#include #include #include +#include #include +#include namespace guhserver { @@ -40,9 +41,18 @@ public: void sendData(const QList &clients, const QByteArray &data); private: + QTimer *m_timer; + QHash m_serverList; QHash m_clientList; + uint m_port; + QList m_networkInterfaces; + QStringList m_ipVersions; + +// QList serversToCreate(); +// QList serversToRemove(); + signals: void clientConnected(const QUuid &clientId); @@ -53,6 +63,7 @@ private slots: void newClientConnected(); void readPackage(); void slotClientDisconnected(); + void onTimeout(); public slots: bool startServer();