diff --git a/consolereader.cpp b/consolereader.cpp index a2bca23..a8cec61 100644 --- a/consolereader.cpp +++ b/consolereader.cpp @@ -28,11 +28,7 @@ ConsoleReader::ConsoleReader ( std::vector *sockets_vector ) void ConsoleReader::run() { - QThread::sleep(1); // wait until sockets are discovered - for ( unsigned i = 0; i < sockets->size(); ++i ) - { - connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets); - } + connectSignals(); listSockets(); std::string command; @@ -46,14 +42,11 @@ void ConsoleReader::run() { case 'a': { - std::cout << "Please set your Orvibo socket to pair mode (rapidly blinking blue light) and connect computer to socket's WiFi network (WiWo-S20)" << std::endl; - std::string ssid; - std::cout << "SSID of your Wifi: "; - std::cin >> ssid; + std::cout << "Please set your Orvibo socket to pair mode (rapidly blinking blue light) and wait until new wifi network (WiWo-S20) appears" << std::endl; std::string password; std::cout << "Password: "; std::cin >> password; - Server *server = new Server(48899, QByteArray::fromStdString(ssid), QByteArray::fromStdString(password)); // HF-A11 chip can be controlled over port 48899 + Server *server = new Server(48899, QByteArray::fromStdString(password)); // HF-A11 chip can be controlled over port 48899 break; } case 'd': @@ -115,3 +108,11 @@ void ConsoleReader::listSockets() std::cout << "P - change remote password (max 12 characters)\nq - quit\ns - pick another socket (default is 1)\nt - change timezone" << std::endl; std::cout << "Enter command: " << std::endl; } + +void ConsoleReader::connectSignals() +{ + for ( unsigned i = 0; i < sockets->size(); ++i ) + { + connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets); + } +} diff --git a/consolereader.h b/consolereader.h index 4b21578..0059740 100644 --- a/consolereader.h +++ b/consolereader.h @@ -29,6 +29,7 @@ public: ConsoleReader ( std::vector *sockets_vector ); void run(); void listSockets(); + void connectSignals(); private: std::vector *sockets; diff --git a/main.cpp b/main.cpp index 308355b..d2225ad 100644 --- a/main.cpp +++ b/main.cpp @@ -25,8 +25,9 @@ int main(int argc, char *argv[]) QCoreApplication app(argc, argv); std::vector *sockets = new std::vector; - Server server(sockets, 10000); + Server *server = new Server(sockets); ConsoleReader *reader = new ConsoleReader(sockets); + QObject::connect(server, &Server::discovered, reader, &ConsoleReader::connectSignals); return app.exec(); } diff --git a/server.cpp b/server.cpp index 220a232..73df59a 100644 --- a/server.cpp +++ b/server.cpp @@ -15,25 +15,65 @@ * along with this program. If not, see .* *************************************************************************/ +#include +#include +#include + +#include #include #include #include "consolereader.h" #include "server.h" -Server::Server ( std::vector *sockets_vector, uint16_t port ) +Server::Server ( std::vector *sockets_vector ) { sockets = sockets_vector; udpSocketGet = new QUdpSocket(); - udpSocketGet->bind ( QHostAddress::Any, port ); + udpSocketGet->bind ( QHostAddress::Any, 10000 ); connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams); - discoverSockets(port); + discoverSockets(); + QTimer *discoverTimer = new QTimer(this); + discoverTimer->setInterval(1*60*1000); // 1 min + discoverTimer->setSingleShot(false); + connect(discoverTimer, &QTimer::timeout, this, &Server::discoverSockets); + discoverTimer->start(); start(); } -Server::Server(uint16_t port, QByteArray ssid, QByteArray password) +Server::Server(uint16_t port, QByteArray password) { + QNetworkConfiguration *cfg = new QNetworkConfiguration; + QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager; + QEventLoop *loop = new QEventLoop; + loop->connect(ncm, &QNetworkConfigurationManager::updateCompleted, loop, &QEventLoop::quit); + ncm->updateConfigurations(); + loop->exec(); + delete loop; + *cfg = ncm->defaultConfiguration(); + QByteArray ssid = cfg->name().toLocal8Bit(); + + auto nc = ncm->allConfigurations(); + + for (auto &x : nc) + { + if (x.bearerType() == QNetworkConfiguration::BearerWLAN) + { + if (x.name() == "WiWo-S20") + { + std::cout << "Connecting to WiWo-S20 wireless" << std::endl; + cfg = &x; + } + } + } + + auto session = new QNetworkSession(*cfg, this); + session->open(); + std::cout << "Wait for connected!" << std::endl; + if (session->waitForOpened()) + std::cout << "Connected!" << std::endl; + QUdpSocket *udpSocketSend = new QUdpSocket(); udpSocketGet = new QUdpSocket(); udpSocketGet->bind ( QHostAddress::Any, port); @@ -49,11 +89,14 @@ Server::Server(uint16_t port, QByteArray ssid, QByteArray password) udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSSSID=") + ssid + QByteArray::fromStdString("\r"), ip, port ); listen(); udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSKEY=WPA2PSK,AES,") + password + QByteArray::fromStdString("\r"), ip, port ); // FIXME: support different security settings + // OPEN, SHARED, WPAPSK......NONE, WEP, TKIP, AES listen(); udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WMODE=STA\r"), ip, port ); listen(); udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+Z\r"), ip, port ); // reboot + session->close(); // FIXME: discover the new socket + std::cout << "Finished" << std::endl; } Server::~Server() @@ -117,6 +160,7 @@ void Server::readPendingDatagrams() { Socket *socket = new Socket ( sender, reply ); sockets->push_back ( socket ); + Q_EMIT discovered(); } } else @@ -136,10 +180,10 @@ void Server::readPendingDatagrams() } } -void Server::discoverSockets(uint16_t port) +void Server::discoverSockets() { QUdpSocket *udpSocketSend = new QUdpSocket(); - udpSocketSend->connectToHost ( QHostAddress::Broadcast, port ); + udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 ); udpSocketSend->write ( discover ); udpSocketSend->write ( discover ); udpSocketSend->disconnectFromHost(); diff --git a/server.h b/server.h index d299a91..f2754ad 100644 --- a/server.h +++ b/server.h @@ -26,20 +26,25 @@ class QUdpSocket; class Server : public QThread { +Q_OBJECT + public: - Server ( std::vector *sockets_vector, uint16_t port ); - Server ( uint16_t port, QByteArray ssid, QByteArray password ); + Server ( std::vector *sockets_vector ); + Server ( uint16_t port, QByteArray password ); ~Server(); + void discoverSockets(); void readPendingDatagrams(); void run(); +Q_SIGNALS: + void discovered(); + private: QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" ); QUdpSocket *udpSocketGet; std::vector *sockets; - void discoverSockets(uint16_t port); QByteArray listen( QByteArray message = 0 ); }; diff --git a/socket.cpp b/socket.cpp index 80ab9cd..605c7c4 100644 --- a/socket.cpp +++ b/socket.cpp @@ -51,7 +51,6 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply ) udpSocket->connectToHost ( ip, 10000 ); connect (this, &Socket::datagramQueued, this, &Socket::listen); - subscribed = false; subscribeTimer = new QTimer(this); subscribeTimer->setInterval(2*60*1000); // 2 min subscribeTimer->setSingleShot(false); @@ -174,8 +173,6 @@ bool Socket::parseReply ( QByteArray reply ) switch ( datagram ) { case Subscribe: - subscribed = true; - subscribeTimer->setInterval(2*60*1000); // 2min case PowerOff: case PowerOn: {