Reformat with astyle.

This commit is contained in:
Andrius Štikonas 2015-08-18 15:29:09 +01:00
parent 4497d1e1c1
commit 482a2a0e5b
8 changed files with 168 additions and 215 deletions

View File

@ -36,13 +36,10 @@ void ConsoleReader::run()
unsigned int number = 0; unsigned int number = 0;
bool cont = true; bool cont = true;
while ( cont ) while (cont) {
{
std::cin >> command; std::cin >> command;
switch ( command[0] ) switch (command[0]) {
{ case 'a': {
case 'a':
{
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::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, password; std::string ssid, password;
std::cout << "Wireless name (\"c\" for current wifi): "; std::cout << "Wireless name (\"c\" for current wifi): ";
@ -52,8 +49,7 @@ void ConsoleReader::run()
Server *server = new Server(48899, QByteArray::fromStdString(ssid), QByteArray::fromStdString(password)); // HF-LPB100 chip can be controlled over port 48899 Server *server = new Server(48899, QByteArray::fromStdString(ssid), QByteArray::fromStdString(password)); // HF-LPB100 chip can be controlled over port 48899
break; break;
} }
case 'A': case 'A': {
{
std::cout << "Please set your Orvibo socket to factory reset mode (rapidly blinking red light)" << std::endl; std::cout << "Please set your Orvibo socket to factory reset mode (rapidly blinking red light)" << std::endl;
std::string password; std::string password;
std::cout << "Password: "; std::cout << "Password: ";
@ -76,8 +72,7 @@ void ConsoleReader::run()
case 'D': case 'D':
(*sockets) [number]->discover(); (*sockets) [number]->discover();
break; break;
case 'n': case 'n': {
{
std::string name; std::string name;
std::cout << "Please enter a new name: "; std::cout << "Please enter a new name: ";
std::cin >> name; std::cin >> name;
@ -93,8 +88,7 @@ void ConsoleReader::run()
else if (command == "on") else if (command == "on")
(*sockets) [number]->powerOn(); (*sockets) [number]->powerOn();
break; break;
case 'P': case 'P': {
{
std::string password; std::string password;
std::cout << "Please enter a new password: "; std::cout << "Please enter a new password: ";
std::cin >> password; std::cin >> password;
@ -110,8 +104,7 @@ void ConsoleReader::run()
--number; // count from 0 --number; // count from 0
listSockets(); listSockets();
break; break;
case 't': case 't': {
{
int timezone; int timezone;
std::cout << "Please enter a new timezone (integer from -11 to 12): "; std::cout << "Please enter a new timezone (integer from -11 to 12): ";
std::cin >> timezone; std::cin >> timezone;
@ -126,8 +119,7 @@ void ConsoleReader::run()
void ConsoleReader::listSockets() void ConsoleReader::listSockets()
{ {
for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) for (std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) {
{
std::cout << "_____________________________________________________________________________\n" << std::endl; std::cout << "_____________________________________________________________________________\n" << std::endl;
std::cout << "IP Address: " << (*i)->ip.toString().toStdString() << "\t MAC Address: " << (*i)->mac.toHex().toStdString() << "\t Power: " << ((*i)->powered ? "On" : "Off") << std::endl; std::cout << "IP Address: " << (*i)->ip.toString().toStdString() << "\t MAC Address: " << (*i)->mac.toHex().toStdString() << "\t Power: " << ((*i)->powered ? "On" : "Off") << std::endl;
std::cout << "Socket Name: " << (*i)->socketName.toStdString() << "\t Remote Password: " << (*i)->remotePassword.toStdString() << "\t Timezone: " << (*i)->timeZone.toHex().toStdString() << std::endl; std::cout << "Socket Name: " << (*i)->socketName.toStdString() << "\t Remote Password: " << (*i)->remotePassword.toStdString() << "\t Timezone: " << (*i)->timeZone.toHex().toStdString() << std::endl;
@ -148,8 +140,7 @@ void ConsoleReader::listSockets()
void ConsoleReader::connectSignals() void ConsoleReader::connectSignals()
{ {
for ( unsigned i = 0; i < sockets->size(); ++i ) for (unsigned i = 0; i < sockets->size(); ++i) {
{
connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets); connect((*sockets)[i], &Socket::stateChanged, this, &ConsoleReader::listSockets);
} }
} }

View File

@ -36,8 +36,7 @@ Dialog::~Dialog()
void Dialog::updateUi() void Dialog::updateUi()
{ {
for ( unsigned int i = 0; i < (*sockets).size(); ++i) for (unsigned int i = 0; i < (*sockets).size(); ++i) {
{
ui->toggleButton->setText((*sockets)[i]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on")); ui->toggleButton->setText((*sockets)[i]->powered ? QStringLiteral("Turn off") : QStringLiteral("Turn on"));
ui->comboBox->setItemText(i, (*sockets)[i]->socketName); ui->comboBox->setItemText(i, (*sockets)[i]->socketName);
} }
@ -46,8 +45,7 @@ void Dialog::updateUi()
void Dialog::discovered() void Dialog::discovered()
{ {
ui->comboBox->clear(); ui->comboBox->clear();
for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) for (std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) {
{
connect(*i, &Socket::stateChanged, this, &Dialog::updateUi); connect(*i, &Socket::stateChanged, this, &Dialog::updateUi);
connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower); connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower);
ui->comboBox->addItem("Socket"); ui->comboBox->addItem("Socket");

View File

@ -22,7 +22,8 @@
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui
{
class Dialog; class Dialog;
} }

View File

@ -48,25 +48,20 @@ Server::Server(uint16_t port, QByteArray ssid, QByteArray password)
ncm->updateConfigurations(); ncm->updateConfigurations();
*cfgInitial = ncm->defaultConfiguration(); *cfgInitial = ncm->defaultConfiguration();
if (ssid == "c") if (ssid == "c") {
{
ssid = cfgInitial->name().toLocal8Bit(); ssid = cfgInitial->name().toLocal8Bit();
qDebug() << "SSID unspecified, using current network: " << ssid; qDebug() << "SSID unspecified, using current network: " << ssid;
} }
bool stop = false; bool stop = false;
while ( !stop ) while (!stop) {
{
QThread::sleep(1); QThread::sleep(1);
auto nc = ncm->allConfigurations(); auto nc = ncm->allConfigurations();
for (auto &x : nc) for (auto & x : nc) {
{ if (x.bearerType() == QNetworkConfiguration::BearerWLAN) {
if (x.bearerType() == QNetworkConfiguration::BearerWLAN) if (x.name() == "WiWo-S20") {
{
if (x.name() == "WiWo-S20")
{
qWarning() << "Connecting to WiWo-S20 wireless"; qWarning() << "Connecting to WiWo-S20 wireless";
cfg = &x; cfg = &x;
stop = true; stop = true;
@ -122,15 +117,12 @@ QByteArray Server::listen(QByteArray message)
QHostAddress sender; QHostAddress sender;
quint16 senderPort; quint16 senderPort;
bool stop = false; bool stop = false;
while ( !stop ) while (!stop) {
{
QThread::msleep(50); QThread::msleep(50);
while ( udpSocketGet->hasPendingDatagrams() ) while (udpSocketGet->hasPendingDatagrams()) {
{
reply.resize(udpSocketGet->pendingDatagramSize()); reply.resize(udpSocketGet->pendingDatagramSize());
udpSocketGet->readDatagram(reply.data(), reply.size(), &sender, &senderPort); udpSocketGet->readDatagram(reply.data(), reply.size(), &sender, &senderPort);
if (reply != message) if (reply != message) {
{
stop = true; stop = true;
} }
} }
@ -145,8 +137,7 @@ void Server::run()
void Server::readPendingDatagrams() void Server::readPendingDatagrams()
{ {
while ( udpSocketGet->hasPendingDatagrams() ) while (udpSocketGet->hasPendingDatagrams()) {
{
QByteArray reply, mac; QByteArray reply, mac;
reply.resize(udpSocketGet->pendingDatagramSize()); reply.resize(udpSocketGet->pendingDatagramSize());
QHostAddress sender; QHostAddress sender;
@ -154,35 +145,26 @@ void Server::readPendingDatagrams()
udpSocketGet->readDatagram(reply.data(), reply.size(), &sender, &senderPort); udpSocketGet->readDatagram(reply.data(), reply.size(), &sender, &senderPort);
if ( reply != discover && reply.left ( 2 ) == magicKey ) // check for Magic Key if (reply != discover && reply.left(2) == magicKey) { // check for Magic Key
{ if (reply.mid(4, 2) == QStringLiteral("qa").toLatin1() || reply.mid(4, 2) == QStringLiteral("qg").toLatin1()) { // Reply to discover packet
if ( reply.mid ( 4, 2 ) == QStringLiteral("qa").toLatin1() || reply.mid ( 4, 2 ) == QStringLiteral("qg").toLatin1()) // Reply to discover packet
{
bool duplicate = false; bool duplicate = false;
for ( std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) for (std::vector<Socket*>::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) {
{ if ((*i)->ip == sender) {
if ( (*i)->ip == sender )
{
duplicate = true; duplicate = true;
break; break;
} }
} }
if ( !duplicate ) if (!duplicate) {
{
Socket *socket = new Socket(sender, reply); Socket *socket = new Socket(sender, reply);
sockets->push_back(socket); sockets->push_back(socket);
Q_EMIT discovered(); Q_EMIT discovered();
} }
mac = reply.mid(7, 6); mac = reply.mid(7, 6);
} } else {
else
{
mac = reply.mid(6, 6); mac = reply.mid(6, 6);
} }
for ( std::vector<Socket*>::iterator i = sockets->begin() ; i != sockets->end(); ++i ) for (std::vector<Socket*>::iterator i = sockets->begin() ; i != sockets->end(); ++i) {
{ if ((*i)->mac == mac) {
if ( (*i)->mac == mac )
{
(*i)->parseReply(reply); (*i)->parseReply(reply);
break; break;
} }
@ -207,34 +189,28 @@ void broadcastPassword(QString password)
QUdpSocket *udpSocket = new QUdpSocket(); QUdpSocket *udpSocket = new QUdpSocket();
udpSocket->connectToHost(QHostAddress::Broadcast, 49999); udpSocket->connectToHost(QHostAddress::Broadcast, 49999);
uint sleep = 15; uint sleep = 15;
for (uint j=0; j < 4; ++j) // FIXME: stopping loop on discovery for (uint j = 0; j < 4; ++j) { // FIXME: stopping loop on discovery
{
qWarning() << j; qWarning() << j;
for (unsigned short int i = 0; i < 200; ++i) for (unsigned short int i = 0; i < 200; ++i) {
{
udpSocket->write(fives(76)); udpSocket->write(fives(76));
QThread::msleep(sleep); QThread::msleep(sleep);
} }
for (unsigned short int i = 0; i < 3; ++i) for (unsigned short int i = 0; i < 3; ++i) {
{
udpSocket->write(fives(89)); udpSocket->write(fives(89));
QThread::msleep(sleep); QThread::msleep(sleep);
} }
QChar *data = password.data(); QChar *data = password.data();
while (!data->isNull()) while (!data->isNull()) {
{
udpSocket->write(fives(data->unicode() + 76)); udpSocket->write(fives(data->unicode() + 76));
QThread::msleep(sleep); QThread::msleep(sleep);
++data; ++data;
} }
for (unsigned short int i = 0; i < 3; ++i) for (unsigned short int i = 0; i < 3; ++i) {
{
udpSocket->write(fives(86)); udpSocket->write(fives(86));
QThread::msleep(sleep); QThread::msleep(sleep);
} }
for (unsigned short int i = 0; i < 3; ++i) for (unsigned short int i = 0; i < 3; ++i) {
{
udpSocket->write(fives(332 + password.length())); udpSocket->write(fives(332 + password.length()));
QThread::msleep(sleep); QThread::msleep(sleep);
} }

View File

@ -78,22 +78,18 @@ void Socket::sendDatagram ( Datagram d )
void Socket::run() void Socket::run()
{ {
QMutex mutex; QMutex mutex;
if ( !mutex.tryLock() ) if (!mutex.tryLock()) {
{
return; return;
} }
unsigned short retryCount = 0; unsigned short retryCount = 0;
QByteArray currentDatagram, previousDatagram = 0, recordLength; QByteArray currentDatagram, previousDatagram = 0, recordLength;
while ( commands.size() > 0 ) while (commands.size() > 0) {
{
currentDatagram = datagram[commands.head()]; currentDatagram = datagram[commands.head()];
if ( previousDatagram == currentDatagram ) if (previousDatagram == currentDatagram) {
{
++retryCount; ++retryCount;
} }
if ( retryCount == 5 ) if (retryCount == 5) {
{
std::cout << "Stop retrying: " << currentDatagram.toHex().toStdString() << std::endl; std::cout << "Stop retrying: " << currentDatagram.toHex().toStdString() << std::endl;
commands.dequeue(); commands.dequeue();
retryCount = 0; retryCount = 0;
@ -196,18 +192,15 @@ void Socket::tableData()
bool Socket::parseReply(QByteArray reply) bool Socket::parseReply(QByteArray reply)
{ {
if ( reply.left(2) != magicKey ) if (reply.left(2) != magicKey) {
{
return false; return false;
} }
QByteArray id = reply.mid(4, 2); QByteArray id = reply.mid(4, 2);
unsigned int datagram = std::distance(commandID, std::find(commandID, commandID + MaxCommands, id)); // match commandID with enum unsigned int datagram = std::distance(commandID, std::find(commandID, commandID + MaxCommands, id)); // match commandID with enum
if ( datagram == ReadTable ) // determine the table number if (datagram == ReadTable) { // determine the table number
{
unsigned int table = reply[reply.indexOf(twenties) + 11]; unsigned int table = reply[reply.indexOf(twenties) + 11];
switch ( table ) switch (table) {
{
case 1: case 1:
break; break;
case 3: case 3:
@ -224,11 +217,9 @@ bool Socket::parseReply ( QByteArray reply )
return false; return false;
} }
} }
switch ( datagram ) switch (datagram) {
{
case QueryAll: case QueryAll:
case Discover: case Discover: {
{
QByteArray timeArray = reply.right(5).left(4); QByteArray timeArray = reply.right(5).left(4);
QDataStream stream(&timeArray, QIODevice::ReadOnly); QDataStream stream(&timeArray, QIODevice::ReadOnly);
stream.setByteOrder(QDataStream::LittleEndian); stream.setByteOrder(QDataStream::LittleEndian);
@ -240,16 +231,13 @@ bool Socket::parseReply ( QByteArray reply )
} }
case Subscribe: case Subscribe:
case PowerOff: case PowerOff:
case PowerOn: case PowerOn: {
{
bool poweredOld = powered; bool poweredOld = powered;
powered = reply.right(1) == one; powered = reply.right(1) == one;
if ( powered != poweredOld ) if (powered != poweredOld) {
{
Q_EMIT stateChanged(); Q_EMIT stateChanged();
} }
if ( datagram == PowerOff && powered == true ) // Required to deque if (datagram == PowerOff && powered == true) { // Required to deque
{
datagram = PowerOn; datagram = PowerOn;
} }
break; break;
@ -259,8 +247,7 @@ bool Socket::parseReply ( QByteArray reply )
socketTableVersion = reply.mid(reply.indexOf(QByteArray::fromHex("000100000600")) + 6, 2); socketTableVersion = reply.mid(reply.indexOf(QByteArray::fromHex("000100000600")) + 6, 2);
// 000100000600 // 000100000600
break; break;
case SocketData: case SocketData: {
{
// std::cout << reply.toHex().toStdString() << " " << datagram << std::endl; // for debugging purposes only // std::cout << reply.toHex().toStdString() << " " << datagram << std::endl; // for debugging purposes only
unsigned short int index = reply.indexOf(rmac + twenties); unsigned short int index = reply.indexOf(rmac + twenties);
versionID = reply.mid(index - 14, 2); versionID = reply.mid(index - 14, 2);
@ -314,10 +301,8 @@ bool Socket::parseReply ( QByteArray reply )
return false; return false;
} }
if (commands.size() > 0) if (commands.size() > 0) {
{ if (datagram == commands.head()) {
if ( datagram == commands.head() )
{
commands.dequeue(); commands.dequeue();
} }
} }

View File

@ -76,7 +76,9 @@ private:
void sendDatagram(Datagram); void sendDatagram(Datagram);
void subscribe(); void subscribe();
void processQueue() { start(); } void processQueue() {
start();
}
void run(); void run();
void writeSocketData(QByteArray socketName, QByteArray remotePassword, QByteArray timeZone, uint16_t countdown); void writeSocketData(QByteArray socketName, QByteArray remotePassword, QByteArray timeZone, uint16_t countdown);