Browse Source

Try to connect to WiWo-S20 wifi.

master
Andrius Štikonas 5 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 )

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);
}
}

+ 1
- 0
consolereader.h View File

@@ -29,6 +29,7 @@ public:
ConsoleReader ( std::vector<Socket*> *sockets_vector );
void run();
void listSockets();
void connectSignals();

private:
std::vector<Socket*> *sockets;

+ 2
- 1
main.cpp View File

@@ -25,8 +25,9 @@ int main(int argc, char *argv[])
QCoreApplication app(argc, argv);

std::vector<Socket*> *sockets = new std::vector<Socket*>;
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();
}

+ 50
- 6
server.cpp View File

@@ -15,25 +15,65 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
*************************************************************************/

#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>

#include <QTimer>
#include <QUdpSocket>
#include <iostream>

#include "consolereader.h"
#include "server.h"

Server::Server ( std::vector<Socket*> *sockets_vector, uint16_t port )
Server::Server ( std::vector<Socket*> *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();

+ 8
- 3
server.h View File

@@ -26,20 +26,25 @@ class QUdpSocket;

class Server : public QThread
{
Q_OBJECT

public:
Server ( std::vector<Socket*> *sockets_vector, uint16_t port );
Server ( uint16_t port, QByteArray ssid, QByteArray password );
Server ( std::vector<Socket*> *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<Socket*> *sockets;

void discoverSockets(uint16_t port);
QByteArray listen( QByteArray message = 0 );
};


+ 0
- 3
socket.cpp View File

@@ -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:
{

Loading…
Cancel
Save