Add another method of pairing.

This commit is contained in:
Andrius Štikonas 2015-03-11 12:34:56 +00:00
parent 788cb86b50
commit 8999a55c30
5 changed files with 79 additions and 7 deletions

View File

@ -46,7 +46,16 @@ void ConsoleReader::run()
std::string password;
std::cout << "Password: ";
std::cin >> password;
Server *server = new Server(48899, QByteArray::fromStdString(password)); // HF-A11 chip can be controlled over port 48899
Server *server = new Server(48899, QByteArray::fromStdString(password)); // HF-LPB100 chip can be controlled over port 48899
break;
}
case 'A':
{
std::cout << "Please set your Orvibo socket to factory reset mode (rapidly blinking red light)" << std::endl;
std::string password;
std::cout << "Password: ";
std::cin >> password;
broadcastPassword(QString::fromStdString(password)); // HF-LPB100 chip can be controlled over port 49999
break;
}
case 'd':
@ -104,7 +113,7 @@ void ConsoleReader::listSockets()
std::cout << "Countdown: " << (*i)->countdown.toHex().toStdString() << std::endl;
}
std::cout << "_____________________________________________________________________________\n" << std::endl;
std::cout << "a - add unpaired socket (WiFi needed)\nd - update table data\nn - change socket name (max 16 characters)\np - toggle power state\n";
std::cout << "a - add unpaired socket (WiFi needed)\nA - add unpaired socket (no WiFi needed)\nd - update table data\nn - change socket name (max 16 characters)\np - toggle power state\n";
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;
}

View File

@ -18,9 +18,9 @@
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#include <QTimer>
#include <QUdpSocket>
#include <iostream>
#include "consolereader.h"
@ -169,7 +169,7 @@ void Server::readPendingDatagrams()
{
Socket *socket = new Socket ( sender, reply );
sockets->push_back ( socket );
Q_EMIT discovered();
Q_EMIT discovered();
}
}
else
@ -198,3 +198,53 @@ void Server::discoverSockets()
udpSocketSend->disconnectFromHost();
delete udpSocketSend;
}
void broadcastPassword(QString password)
{
QUdpSocket *udpSocket = new QUdpSocket();
udpSocket->connectToHost ( QHostAddress::Broadcast, 49999 );
uint sleep = 15;
for (uint j=0; j < 4; ++j) // FIXME: stopping loop on discovery
{
std::cout << j << std::endl;
for (unsigned short int i = 0; i < 200; ++i)
{
udpSocket->write ( fives(76) );
QThread::msleep(sleep);
}
for (unsigned short int i = 0; i < 3; ++i)
{
udpSocket->write ( fives(89) );
QThread::msleep(sleep);
}
QChar *data = password.data();
while (!data->isNull())
{
udpSocket->write( fives(data->unicode() + 76) );
QThread::msleep(sleep);
++data;
}
for (unsigned short int i = 0; i < 3; ++i)
{
udpSocket->write ( fives(86) );
QThread::msleep(sleep);
}
for (unsigned short int i = 0; i < 3; ++i)
{
udpSocket->write ( fives(332 + password.length()) );
QThread::msleep(sleep);
}
}
udpSocket->disconnectFromHost();
delete udpSocket;
// FIXME: special slightly modified SocketData packet might is needed here
}
QByteArray fives(unsigned short int length)
{
QByteArray packet;
packet.fill(0x05, length);
return packet;
}

View File

@ -48,4 +48,7 @@ private:
QByteArray listen( QByteArray message = 0 );
};
void broadcastPassword(QString password);
QByteArray fives(unsigned short int length);
#endif /* SERVER_H */

View File

@ -52,7 +52,7 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply )
udpSocket = new QUdpSocket();
udpSocket->connectToHost ( ip, 10000 );
connect (this, &Socket::datagramQueued, this, &Socket::listen);
connect (this, &Socket::datagramQueued, this, &Socket::processQueue);
subscribeTimer = new QTimer(this);
subscribeTimer->setInterval(2*60*1000); // 2 min
subscribeTimer->setSingleShot(false);
@ -78,7 +78,9 @@ void Socket::run()
{
QMutex mutex;
if ( !mutex.tryLock() )
{
return;
}
unsigned short retryCount = 0;
QByteArray currentDatagram, previousDatagram = 0, recordLength;
@ -86,7 +88,9 @@ void Socket::run()
{
currentDatagram = datagram[commands.head()];
if ( previousDatagram == currentDatagram )
{
++retryCount;
}
if ( retryCount == 5 )
{
std::cout << "Stop retrying: " << currentDatagram.toHex().toStdString() << std::endl;
@ -162,7 +166,7 @@ void Socket::tableData()
bool Socket::parseReply ( QByteArray reply )
{
if ( reply.left ( 2 ) != magicKey )
if ( reply.left(2) != magicKey )
{
return false;
}
@ -182,7 +186,11 @@ bool Socket::parseReply ( QByteArray reply )
case 4:
datagram = SocketData;
break;
case 0:
qWarning() << "No table"; // FIXME: initial data query
default:
qWarning() << "Failed to identify data table.";
datagram = TableData;
return false;
}
}
@ -195,7 +203,9 @@ bool Socket::parseReply ( QByteArray reply )
bool poweredOld = powered;
powered = reply.right(1) == one;
if ( powered != poweredOld )
{
Q_EMIT stateChanged();
}
if ( datagram == PowerOff && powered == true ) // Required to deque
{
datagram = PowerOn;

View File

@ -58,7 +58,7 @@ private:
void sendDatagram ( Datagram );
void subscribe();
void listen() { start(); }
void processQueue() { start(); }
void run();
void writeSocketData (QByteArray name, QByteArray password, QByteArray timezone);