diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5828d86..b4d4c65 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,17 +1,6 @@
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 OFF)
-SET(CMAKE_CXX_FLAGS "-std=gnu++11")
-
-# Find the QtWidgets library
find_package(Qt5 REQUIRED Core Network)
-
-# Tell CMake to create the helloworld executable
-add_executable(s20 s20.cpp)
-
-# Use the Widgets module from Qt 5.
+add_executable(s20 main.cpp socket.cpp)
target_link_libraries(s20 Qt5::Network)
diff --git a/s20.cpp b/main.cpp
similarity index 57%
rename from s20.cpp
rename to main.cpp
index 05fe2cf..9def8b0 100644
--- a/s20.cpp
+++ b/main.cpp
@@ -15,50 +15,31 @@
* along with this program. If not, see .*
*************************************************************************/
-#include
+#include
+#include
+
#include
-#include
+#include
#include
-class Socket : public QObject
+#include "socket.h"
+
+void listSockets(std::vector const &sockets);
+
+QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61");
+
+int main(int argc, char *argv[])
{
-public:
- Socket();
+ QCoreApplication s20(argc, argv);
-private:
- void readPendingDatagrams();
- QByteArray* makeNewDatagram();
+ QUdpSocket *udpSocketSend = new QUdpSocket();
+ QUdpSocket *udpSocketGet = new QUdpSocket();
- QUdpSocket *udpSocketSend;
- QUdpSocket *udpSocketGet;
- QHostAddress socketIPAddress;
-
- enum {Discover, Subscribe, PowerOff, PowerOn};
- QByteArray datagram[4] = {
- QByteArray::fromHex("68 64 00 06 71 61"), // global discovery
- 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
- };
-};
-
-
-Socket::Socket() {
- 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);
+ udpSocketSend->connectToHost(QHostAddress::Broadcast, 10000);
udpSocketGet->bind(QHostAddress::Any, 10000);
-// QObject::connect(udpSocketGet, &QIODevice::readyRead, this, &Socket::readPendingDatagrams);
- udpSocketSend->write(datagram[Discover]);
- readPendingDatagrams();
-}
-
-void Socket::readPendingDatagrams()
-{
+ udpSocketSend->write(discover);
+ std::vector sockets;
while (udpSocketGet->waitForReadyRead(1000)) // 1s
{
while (udpSocketGet->hasPendingDatagrams())
@@ -70,18 +51,31 @@ void Socket::readPendingDatagrams()
udpSocketGet->readDatagram(datagramGet.data(), datagramGet.size(), &sender, &senderPort);
- if (datagramGet != datagram[Discover])
+ if (datagramGet != discover)
{
- socketIPAddress = sender;
+ bool duplicate = false;
+ for(std::vector::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
+ {
+ if (i->ip == sender)
+ duplicate = true;
+ }
+ if(!duplicate)
+ {
+ Socket socket(sender, datagramGet);
+ sockets.push_back(socket);
+ }
}
}
}
-}
-
-int main(int argc, char *argv[])
-{
- QCoreApplication s20(argc, argv);
- Socket socket;
+ listSockets(sockets);
return 0;
}
+
+void listSockets(std::vector const &sockets)
+{
+ for (std::vector::const_iterator i = sockets.begin() ; i != sockets.end(); ++i)
+ {
+ std::cout << "IP Address: " << i->ip.toString().toStdString() << "\t MAC Address: " << i->mac.toHex().toStdString() << "\t Powered: " << i->powered << std::endl;
+ }
+}
diff --git a/socket.cpp b/socket.cpp
new file mode 100644
index 0000000..980d6b0
--- /dev/null
+++ b/socket.cpp
@@ -0,0 +1,25 @@
+/*************************************************************************
+ * 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 "socket.h"
+
+Socket::Socket(QHostAddress IPaddress, QByteArray reply)
+{
+ ip = IPaddress;
+ mac = reply.mid(7, 6);
+ powered = reply.right(1) == QByteArray::fromHex("01");
+}
\ No newline at end of file
diff --git a/socket.h b/socket.h
new file mode 100644
index 0000000..c069d56
--- /dev/null
+++ b/socket.h
@@ -0,0 +1,36 @@
+/*************************************************************************
+ * 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
+
+class Socket
+{
+public:
+ Socket(QHostAddress, QByteArray);
+
+ QHostAddress ip;
+ QByteArray mac;
+ bool powered;
+ enum {Subscribe, PowerOff, PowerOn};
+
+private:
+ 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
+};