Initial version.

This commit is contained in:
Andrius Štikonas 2015-01-11 00:17:06 +00:00
commit efcb702473
2 changed files with 113 additions and 0 deletions

16
CMakeLists.txt Normal file
View File

@ -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)

97
s20.cpp Normal file
View File

@ -0,0 +1,97 @@
/*************************************************************************
* Copyright (C) 2015 by Andrius Štikonas <andrius@stikonas.eu> *
* *
* 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 <http://www.gnu.org/licenses/>.*
*************************************************************************/
#include <QCoreApplication>
#include <QByteArray>
#include <QHostAddress>
#include <QUdpSocket>
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"