Browse Source

Try to connect to WiWo-S20 wifi.

master
Andrius Štikonas 4 years ago
parent
commit
6319fa237b
6 changed files with 72 additions and 23 deletions
  1. 11
    10
      consolereader.cpp
  2. 1
    0
      consolereader.h
  3. 2
    1
      main.cpp
  4. 50
    6
      server.cpp
  5. 8
    3
      server.h
  6. 0
    3
      socket.cpp

+ 11
- 10
consolereader.cpp View File

@@ -28,11 +28,7 @@ ConsoleReader::ConsoleReader ( std::vector<Socket*> *sockets_vector )
28 28
 
29 29
 void ConsoleReader::run()
30 30
 {
31
-    QThread::sleep(1); // wait until sockets are discovered
32
-    for ( unsigned i = 0; i < sockets->size(); ++i )
33
-    {
34
-        connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets);
35
-    }
31
+    connectSignals();
36 32
     listSockets();
37 33
 
38 34
     std::string command;
@@ -46,14 +42,11 @@ void ConsoleReader::run()
46 42
         {
47 43
         case 'a':
48 44
         {
49
-            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;
50
-            std::string ssid;
51
-            std::cout << "SSID of your Wifi: ";
52
-            std::cin >> ssid;
45
+            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;
53 46
             std::string password;
54 47
             std::cout << "Password: ";
55 48
             std::cin >> password;
56
-            Server *server = new Server(48899, QByteArray::fromStdString(ssid), QByteArray::fromStdString(password)); // HF-A11 chip can be controlled over port 48899
49
+            Server *server = new Server(48899, QByteArray::fromStdString(password)); // HF-A11 chip can be controlled over port 48899
57 50
             break;
58 51
         }
59 52
         case 'd':
@@ -115,3 +108,11 @@ void ConsoleReader::listSockets()
115 108
     std::cout << "P - change remote password (max 12 characters)\nq - quit\ns - pick another socket (default is 1)\nt - change timezone" << std::endl;
116 109
     std::cout << "Enter command: " << std::endl;
117 110
 }
111
+
112
+void ConsoleReader::connectSignals()
113
+{
114
+    for ( unsigned i = 0; i < sockets->size(); ++i )
115
+    {
116
+        connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets);
117
+    }
118
+}

+ 1
- 0
consolereader.h View File

@@ -29,6 +29,7 @@ public:
29 29
     ConsoleReader ( std::vector<Socket*> *sockets_vector );
30 30
     void run();
31 31
     void listSockets();
32
+    void connectSignals();
32 33
 
33 34
 private:
34 35
     std::vector<Socket*> *sockets;

+ 2
- 1
main.cpp View File

@@ -25,8 +25,9 @@ int main(int argc, char *argv[])
25 25
     QCoreApplication app(argc, argv);
26 26
 
27 27
     std::vector<Socket*> *sockets = new std::vector<Socket*>;
28
-    Server server(sockets, 10000);
28
+    Server *server = new Server(sockets);
29 29
     ConsoleReader *reader = new ConsoleReader(sockets);
30
+    QObject::connect(server, &Server::discovered, reader, &ConsoleReader::connectSignals);
30 31
 
31 32
     return app.exec();
32 33
 }

+ 50
- 6
server.cpp View File

@@ -15,25 +15,65 @@
15 15
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.*
16 16
  *************************************************************************/
17 17
 
18
+#include <QNetworkConfiguration>
19
+#include <QNetworkConfigurationManager>
20
+#include <QNetworkSession>
21
+
22
+#include <QTimer>
18 23
 #include <QUdpSocket>
19 24
 #include <iostream>
20 25
 
21 26
 #include "consolereader.h"
22 27
 #include "server.h"
23 28
 
24
-Server::Server ( std::vector<Socket*> *sockets_vector, uint16_t port )
29
+Server::Server ( std::vector<Socket*> *sockets_vector )
25 30
 {
26 31
     sockets = sockets_vector;
27 32
     udpSocketGet = new QUdpSocket();
28
-    udpSocketGet->bind ( QHostAddress::Any, port );
33
+    udpSocketGet->bind ( QHostAddress::Any, 10000 );
29 34
     connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams);
30
-    discoverSockets(port);
35
+    discoverSockets();
36
+    QTimer *discoverTimer = new QTimer(this);
37
+    discoverTimer->setInterval(1*60*1000); // 1 min
38
+    discoverTimer->setSingleShot(false);
39
+    connect(discoverTimer, &QTimer::timeout, this, &Server::discoverSockets);
40
+    discoverTimer->start();
31 41
 
32 42
     start();
33 43
 }
34 44
 
35
-Server::Server(uint16_t port, QByteArray ssid, QByteArray password)
45
+Server::Server(uint16_t port, QByteArray password)
36 46
 {
47
+    QNetworkConfiguration *cfg = new QNetworkConfiguration;
48
+    QNetworkConfigurationManager *ncm = new QNetworkConfigurationManager;
49
+    QEventLoop *loop = new QEventLoop;
50
+    loop->connect(ncm, &QNetworkConfigurationManager::updateCompleted, loop, &QEventLoop::quit);
51
+    ncm->updateConfigurations();
52
+    loop->exec();
53
+    delete loop;
54
+    *cfg = ncm->defaultConfiguration();
55
+    QByteArray ssid = cfg->name().toLocal8Bit();
56
+
57
+    auto nc = ncm->allConfigurations();
58
+
59
+    for (auto &x : nc)
60
+    {
61
+        if (x.bearerType() == QNetworkConfiguration::BearerWLAN)
62
+        {
63
+            if (x.name() == "WiWo-S20")
64
+            {
65
+                std::cout << "Connecting to WiWo-S20 wireless" << std::endl;
66
+                cfg = &x;
67
+            }
68
+        }
69
+    }
70
+
71
+    auto session = new QNetworkSession(*cfg, this);
72
+    session->open();
73
+    std::cout << "Wait for connected!" << std::endl;
74
+    if (session->waitForOpened())
75
+        std::cout << "Connected!" << std::endl;
76
+
37 77
     QUdpSocket *udpSocketSend = new QUdpSocket();
38 78
     udpSocketGet = new QUdpSocket();
39 79
     udpSocketGet->bind ( QHostAddress::Any, port);
@@ -49,11 +89,14 @@ Server::Server(uint16_t port, QByteArray ssid, QByteArray password)
49 89
     udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSSSID=") + ssid + QByteArray::fromStdString("\r"), ip, port );
50 90
     listen();
51 91
     udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSKEY=WPA2PSK,AES,") + password + QByteArray::fromStdString("\r"), ip, port ); // FIXME: support different security settings
92
+    // OPEN, SHARED, WPAPSK......NONE, WEP, TKIP, AES
52 93
     listen();
53 94
     udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WMODE=STA\r"), ip, port );
54 95
     listen();
55 96
     udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+Z\r"), ip, port ); // reboot
97
+    session->close();
56 98
     // FIXME: discover the new socket
99
+    std::cout << "Finished" << std::endl;
57 100
 }
58 101
 
59 102
 Server::~Server()
@@ -117,6 +160,7 @@ void Server::readPendingDatagrams()
117 160
                 {
118 161
                     Socket *socket = new Socket ( sender, reply );
119 162
                     sockets->push_back ( socket );
163
+		    Q_EMIT discovered();
120 164
                 }
121 165
             }
122 166
             else
@@ -136,10 +180,10 @@ void Server::readPendingDatagrams()
136 180
     }
137 181
 }
138 182
 
139
-void Server::discoverSockets(uint16_t port)
183
+void Server::discoverSockets()
140 184
 {
141 185
     QUdpSocket *udpSocketSend = new QUdpSocket();
142
-    udpSocketSend->connectToHost ( QHostAddress::Broadcast, port );
186
+    udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 );
143 187
     udpSocketSend->write ( discover );
144 188
     udpSocketSend->write ( discover );
145 189
     udpSocketSend->disconnectFromHost();

+ 8
- 3
server.h View File

@@ -26,20 +26,25 @@ class QUdpSocket;
26 26
 
27 27
 class Server : public QThread
28 28
 {
29
+Q_OBJECT
30
+
29 31
 public:
30
-    Server ( std::vector<Socket*> *sockets_vector, uint16_t port );
31
-    Server ( uint16_t port, QByteArray ssid, QByteArray password );
32
+    Server ( std::vector<Socket*> *sockets_vector );
33
+    Server ( uint16_t port, QByteArray password );
32 34
     ~Server();
33 35
 
36
+    void discoverSockets();
34 37
     void readPendingDatagrams();
35 38
     void run();
36 39
 
40
+Q_SIGNALS:
41
+    void discovered();
42
+
37 43
 private:
38 44
     QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" );
39 45
     QUdpSocket *udpSocketGet;
40 46
     std::vector<Socket*> *sockets;
41 47
 
42
-    void discoverSockets(uint16_t port);
43 48
     QByteArray listen( QByteArray message = 0 );
44 49
 };
45 50
 

+ 0
- 3
socket.cpp View File

@@ -51,7 +51,6 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply )
51 51
     udpSocket->connectToHost ( ip, 10000 );
52 52
 
53 53
     connect (this, &Socket::datagramQueued, this, &Socket::listen);
54
-    subscribed = false;
55 54
     subscribeTimer = new QTimer(this);
56 55
     subscribeTimer->setInterval(2*60*1000); // 2 min
57 56
     subscribeTimer->setSingleShot(false);
@@ -174,8 +173,6 @@ bool Socket::parseReply ( QByteArray reply )
174 173
     switch ( datagram )
175 174
     {
176 175
     case Subscribe:
177
-        subscribed = true;
178
-        subscribeTimer->setInterval(2*60*1000); // 2min
179 176
     case PowerOff:
180 177
     case PowerOn:
181 178
     {

Loading…
Cancel
Save