From 17a2b1325bf2316363e09c1e080cfe6d992f13c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 14 Jan 2015 13:55:28 +0000 Subject: [PATCH] Implement basic toggling support. --- CMakeLists.txt | 2 +- main.cpp | 36 ++++++++++++++++++++++++------------ socket.cpp | 27 ++++++++++++++++++++++++++- socket.h | 11 +++++++---- 4 files changed, 58 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4d4c65..a41e0e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(s20) find_package(Qt5 REQUIRED Core Network) add_executable(s20 main.cpp socket.cpp) -target_link_libraries(s20 Qt5::Network) +target_link_libraries(s20 Qt5::Core Qt5::Network) diff --git a/main.cpp b/main.cpp index 9def8b0..b106ffa 100644 --- a/main.cpp +++ b/main.cpp @@ -16,22 +16,17 @@ *************************************************************************/ #include -#include - -#include -#include -#include +#include #include "socket.h" +void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector &sockets); void listSockets(std::vector const &sockets); QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61"); int main(int argc, char *argv[]) { - QCoreApplication s20(argc, argv); - QUdpSocket *udpSocketSend = new QUdpSocket(); QUdpSocket *udpSocketGet = new QUdpSocket(); @@ -39,7 +34,27 @@ int main(int argc, char *argv[]) udpSocketGet->bind(QHostAddress::Any, 10000); udpSocketSend->write(discover); + udpSocketSend->disconnectFromHost(); + delete udpSocketSend; std::vector sockets; + + readDiscoverDatagrams(udpSocketGet, sockets); + listSockets(sockets); + + char command; + std::cin >> command; + switch(command) + { + case 'q': + break; + case 't': + sockets.begin()->toggle(); + } + return 0; +} + +void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector &sockets) +{ while (udpSocketGet->waitForReadyRead(1000)) // 1s { while (udpSocketGet->hasPendingDatagrams()) @@ -51,7 +66,7 @@ int main(int argc, char *argv[]) udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort); - if (datagramGet != discover) + if (datagramGet != discover && datagramGet.left(2) == QByteArray::fromHex("68 64")) { bool duplicate = false; for(std::vector::const_iterator i = sockets.begin() ; i != sockets.end(); ++i) @@ -61,15 +76,12 @@ int main(int argc, char *argv[]) } if(!duplicate) { - Socket socket(sender, datagramGet); + const Socket socket(sender, datagramGet); sockets.push_back(socket); } } } } - - listSockets(sockets); - return 0; } void listSockets(std::vector const &sockets) diff --git a/socket.cpp b/socket.cpp index 980d6b0..ff7c154 100644 --- a/socket.cpp +++ b/socket.cpp @@ -17,9 +17,34 @@ #include "socket.h" +#include + Socket::Socket(QHostAddress IPaddress, QByteArray reply) { ip = IPaddress; mac = reply.mid(7, 6); + rmac = mac; + std::reverse(rmac.begin(), rmac.end()); powered = reply.right(1) == QByteArray::fromHex("01"); -} \ No newline at end of file + QByteArray twenties = QByteArray::fromHex("20 20 20 20 20 20"); + QByteArray zeros = QByteArray::fromHex("00 00 00 00"); + + datagram[Subscribe] = QByteArray::fromHex("68 64 00 1e 63 6c") + mac + twenties + rmac + twenties; + datagram[PowerOn] = QByteArray::fromHex("68 64 00 17 64 63") + mac + twenties + zeros + QByteArray::fromHex("01"); + datagram[PowerOff] = QByteArray::fromHex("68 64 00 17 64 63") + mac + twenties + zeros + QByteArray::fromHex("00"); +} + +bool Socket::toggle() +{ + sendDatagram(datagram[Subscribe]); // TODO: process replies + sendDatagram(datagram[powered ? PowerOff : PowerOn]); +} + +void Socket::sendDatagram(QByteArray datagram) +{ + udpSocketSend = new QUdpSocket(); + udpSocketSend->connectToHost(ip, 10000); + udpSocketSend->write(datagram); + udpSocketSend->disconnectFromHost(); + delete udpSocketSend; +} diff --git a/socket.h b/socket.h index c069d56..30a414b 100644 --- a/socket.h +++ b/socket.h @@ -17,20 +17,23 @@ #include #include +#include class Socket { public: Socket(QHostAddress, QByteArray); + bool toggle(); QHostAddress ip; QByteArray mac; bool powered; enum {Subscribe, PowerOff, PowerOn}; - + private: + void sendDatagram(QByteArray datagram); + QByteArray datagram[3]; -// 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 -// 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 -// 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 + QByteArray rmac; // Reveresed mac + QUdpSocket *udpSocketSend; };