Try to connect to WiWo-S20 wifi.
This commit is contained in:
parent
c7c93181bf
commit
6319fa237b
|
@ -28,11 +28,7 @@ ConsoleReader::ConsoleReader ( std::vector<Socket*> *sockets_vector )
|
||||||
|
|
||||||
void ConsoleReader::run()
|
void ConsoleReader::run()
|
||||||
{
|
{
|
||||||
QThread::sleep(1); // wait until sockets are discovered
|
connectSignals();
|
||||||
for ( unsigned i = 0; i < sockets->size(); ++i )
|
|
||||||
{
|
|
||||||
connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets);
|
|
||||||
}
|
|
||||||
listSockets();
|
listSockets();
|
||||||
|
|
||||||
std::string command;
|
std::string command;
|
||||||
|
@ -46,14 +42,11 @@ void ConsoleReader::run()
|
||||||
{
|
{
|
||||||
case 'a':
|
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::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 ssid;
|
|
||||||
std::cout << "SSID of your Wifi: ";
|
|
||||||
std::cin >> ssid;
|
|
||||||
std::string password;
|
std::string password;
|
||||||
std::cout << "Password: ";
|
std::cout << "Password: ";
|
||||||
std::cin >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'd':
|
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 << "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;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ public:
|
||||||
ConsoleReader ( std::vector<Socket*> *sockets_vector );
|
ConsoleReader ( std::vector<Socket*> *sockets_vector );
|
||||||
void run();
|
void run();
|
||||||
void listSockets();
|
void listSockets();
|
||||||
|
void connectSignals();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Socket*> *sockets;
|
std::vector<Socket*> *sockets;
|
||||||
|
|
3
main.cpp
3
main.cpp
|
@ -25,8 +25,9 @@ int main(int argc, char *argv[])
|
||||||
QCoreApplication app(argc, argv);
|
QCoreApplication app(argc, argv);
|
||||||
|
|
||||||
std::vector<Socket*> *sockets = new std::vector<Socket*>;
|
std::vector<Socket*> *sockets = new std::vector<Socket*>;
|
||||||
Server server(sockets, 10000);
|
Server *server = new Server(sockets);
|
||||||
ConsoleReader *reader = new ConsoleReader(sockets);
|
ConsoleReader *reader = new ConsoleReader(sockets);
|
||||||
|
QObject::connect(server, &Server::discovered, reader, &ConsoleReader::connectSignals);
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
56
server.cpp
56
server.cpp
|
@ -15,25 +15,65 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
|
#include <QNetworkConfiguration>
|
||||||
|
#include <QNetworkConfigurationManager>
|
||||||
|
#include <QNetworkSession>
|
||||||
|
|
||||||
|
#include <QTimer>
|
||||||
#include <QUdpSocket>
|
#include <QUdpSocket>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "consolereader.h"
|
#include "consolereader.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
Server::Server ( std::vector<Socket*> *sockets_vector, uint16_t port )
|
Server::Server ( std::vector<Socket*> *sockets_vector )
|
||||||
{
|
{
|
||||||
sockets = sockets_vector;
|
sockets = sockets_vector;
|
||||||
udpSocketGet = new QUdpSocket();
|
udpSocketGet = new QUdpSocket();
|
||||||
udpSocketGet->bind ( QHostAddress::Any, port );
|
udpSocketGet->bind ( QHostAddress::Any, 10000 );
|
||||||
connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams);
|
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();
|
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();
|
QUdpSocket *udpSocketSend = new QUdpSocket();
|
||||||
udpSocketGet = new QUdpSocket();
|
udpSocketGet = new QUdpSocket();
|
||||||
udpSocketGet->bind ( QHostAddress::Any, port);
|
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 );
|
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSSSID=") + ssid + QByteArray::fromStdString("\r"), ip, port );
|
||||||
listen();
|
listen();
|
||||||
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSKEY=WPA2PSK,AES,") + password + QByteArray::fromStdString("\r"), ip, port ); // FIXME: support different security settings
|
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();
|
listen();
|
||||||
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WMODE=STA\r"), ip, port );
|
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WMODE=STA\r"), ip, port );
|
||||||
listen();
|
listen();
|
||||||
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+Z\r"), ip, port ); // reboot
|
udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+Z\r"), ip, port ); // reboot
|
||||||
|
session->close();
|
||||||
// FIXME: discover the new socket
|
// FIXME: discover the new socket
|
||||||
|
std::cout << "Finished" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
Server::~Server()
|
Server::~Server()
|
||||||
|
@ -117,6 +160,7 @@ void Server::readPendingDatagrams()
|
||||||
{
|
{
|
||||||
Socket *socket = new Socket ( sender, reply );
|
Socket *socket = new Socket ( sender, reply );
|
||||||
sockets->push_back ( socket );
|
sockets->push_back ( socket );
|
||||||
|
Q_EMIT discovered();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -136,10 +180,10 @@ void Server::readPendingDatagrams()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::discoverSockets(uint16_t port)
|
void Server::discoverSockets()
|
||||||
{
|
{
|
||||||
QUdpSocket *udpSocketSend = new QUdpSocket();
|
QUdpSocket *udpSocketSend = new QUdpSocket();
|
||||||
udpSocketSend->connectToHost ( QHostAddress::Broadcast, port );
|
udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 );
|
||||||
udpSocketSend->write ( discover );
|
udpSocketSend->write ( discover );
|
||||||
udpSocketSend->write ( discover );
|
udpSocketSend->write ( discover );
|
||||||
udpSocketSend->disconnectFromHost();
|
udpSocketSend->disconnectFromHost();
|
||||||
|
|
11
server.h
11
server.h
|
@ -26,20 +26,25 @@ class QUdpSocket;
|
||||||
|
|
||||||
class Server : public QThread
|
class Server : public QThread
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server ( std::vector<Socket*> *sockets_vector, uint16_t port );
|
Server ( std::vector<Socket*> *sockets_vector );
|
||||||
Server ( uint16_t port, QByteArray ssid, QByteArray password );
|
Server ( uint16_t port, QByteArray password );
|
||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
|
void discoverSockets();
|
||||||
void readPendingDatagrams();
|
void readPendingDatagrams();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
Q_SIGNALS:
|
||||||
|
void discovered();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" );
|
QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" );
|
||||||
QUdpSocket *udpSocketGet;
|
QUdpSocket *udpSocketGet;
|
||||||
std::vector<Socket*> *sockets;
|
std::vector<Socket*> *sockets;
|
||||||
|
|
||||||
void discoverSockets(uint16_t port);
|
|
||||||
QByteArray listen( QByteArray message = 0 );
|
QByteArray listen( QByteArray message = 0 );
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,6 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply )
|
||||||
udpSocket->connectToHost ( ip, 10000 );
|
udpSocket->connectToHost ( ip, 10000 );
|
||||||
|
|
||||||
connect (this, &Socket::datagramQueued, this, &Socket::listen);
|
connect (this, &Socket::datagramQueued, this, &Socket::listen);
|
||||||
subscribed = false;
|
|
||||||
subscribeTimer = new QTimer(this);
|
subscribeTimer = new QTimer(this);
|
||||||
subscribeTimer->setInterval(2*60*1000); // 2 min
|
subscribeTimer->setInterval(2*60*1000); // 2 min
|
||||||
subscribeTimer->setSingleShot(false);
|
subscribeTimer->setSingleShot(false);
|
||||||
|
@ -174,8 +173,6 @@ bool Socket::parseReply ( QByteArray reply )
|
||||||
switch ( datagram )
|
switch ( datagram )
|
||||||
{
|
{
|
||||||
case Subscribe:
|
case Subscribe:
|
||||||
subscribed = true;
|
|
||||||
subscribeTimer->setInterval(2*60*1000); // 2min
|
|
||||||
case PowerOff:
|
case PowerOff:
|
||||||
case PowerOn:
|
case PowerOn:
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue