Implement basic toggling support.

This commit is contained in:
Andrius Štikonas 2015-01-14 13:55:28 +00:00
parent dc98486dc7
commit 17a2b1325b
4 changed files with 58 additions and 18 deletions

View File

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

View File

@ -16,22 +16,17 @@
*************************************************************************/
#include <iostream>
#include <set>
#include <QByteArray>
#include <QCoreApplication>
#include <QUdpSocket>
#include <vector>
#include "socket.h"
void readDiscoverDatagrams(QUdpSocket *udpSocketGet, std::vector<Socket> &sockets);
void listSockets(std::vector<Socket> 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<Socket> 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<Socket> &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<Socket>::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<Socket> const &sockets)

View File

@ -17,9 +17,34 @@
#include "socket.h"
#include <algorithm>
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");
}
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;
}

View File

@ -17,20 +17,23 @@
#include <QByteArray>
#include <QHostAddress>
#include <QUdpSocket>
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;
};