commit efcb7024735a86dadce4cdd89f38a8d00c24a614 Author: Andrius Štikonas Date: Sun Jan 11 00:17:06 2015 +0000 Initial version. diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..25f7a03 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 2.8) +project(s20) + +# Find includes in corresponding build directories +set(CMAKE_INCLUDE_CURRENT_DIR ON) +# Instruct CMake to run moc automatically when needed. +set(CMAKE_AUTOMOC ON) + +# Find the QtWidgets library +find_package(Qt5Network) + +# Tell CMake to create the helloworld executable +add_executable(s20 s20.cpp) + +# Use the Widgets module from Qt 5. +target_link_libraries(s20 Qt5::Network) \ No newline at end of file diff --git a/s20.cpp b/s20.cpp new file mode 100644 index 0000000..8f46118 --- /dev/null +++ b/s20.cpp @@ -0,0 +1,97 @@ +/************************************************************************* + * Copyright (C) 2015 by Andrius Štikonas * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see .* + *************************************************************************/ + +#include +#include +#include +#include + +class Server : public QObject +{ +Q_OBJECT +public: + Server(); + + bool received; + +private slots: + void readPendingDatagrams(); + +private: + QByteArray* makeNewDatagram(); + + QUdpSocket *udpSocketSend; + QUdpSocket *udpSocketGet; +}; + + +Server::Server(){ + received = false; + udpSocketSend = new QUdpSocket(); + udpSocketGet = new QUdpSocket(); + QHostAddress *host = new QHostAddress("192.168.1.212"); + QHostAddress *bcast = new QHostAddress("192.168.1.255"); + + udpSocketSend->connectToHost(*bcast, 10000); + udpSocketGet->bind(*host, 10000); + connect(udpSocketGet, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); + + QByteArray *datagram = makeNewDatagram(); // data from external function + udpSocketSend->write(*datagram); + qWarning() << "sent"; +} + +QByteArray* Server::makeNewDatagram() +{ + QByteArray *datagram = new QByteArray; + *datagram = QByteArray::fromHex("68 64 00 06 71 61"); // global discovery +// *datagram = QByteArray::fromHex("68 64 00 1e 63 6c ac cf 23 35 f5 8c 20 20 20 20 20 20 8c f5 35 23 cf ac 20 20 20 20 20 20"); // subscribe +// *datagram = QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 00"); // power off +// *datagram = QByteArray::fromHex("68 64 00 17 64 63 ac cf 23 35 f5 8c 20 20 20 20 20 20 00 00 00 00 01"); // power on + return datagram; +} + +void Server::readPendingDatagrams() +{ + qWarning() << "read0"; + while (udpSocketGet->hasPendingDatagrams()) { + QByteArray datagram; + datagram.resize(udpSocketGet->pendingDatagramSize()); + QHostAddress sender; + quint16 senderPort; + + udpSocketGet->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); + + qWarning() << "read"; + qWarning() << QString(datagram); +// processTheDatagram(datagram); + received = true; + } +} + +int main(int argc, char *argv[]) +{ + QCoreApplication s20(argc, argv); + Server server; + while (!server.received) + { + true; + } + return 0; +} + +#include "s20.moc"