From 482a2a0e5b3d67209020bea7144c36a9198b9a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Tue, 18 Aug 2015 15:29:09 +0100 Subject: [PATCH] Reformat with astyle. --- consolereader.cpp | 35 ++++------ consolereader.h | 2 +- dialog.cpp | 8 +-- dialog.h | 3 +- server.cpp | 126 +++++++++++++++-------------------- server.h | 10 +-- socket.cpp | 163 +++++++++++++++++++++------------------------- socket.h | 36 +++++----- 8 files changed, 168 insertions(+), 215 deletions(-) diff --git a/consolereader.cpp b/consolereader.cpp index 3504e69..7182a2d 100644 --- a/consolereader.cpp +++ b/consolereader.cpp @@ -20,7 +20,7 @@ #include "consolereader.h" #include "server.h" -ConsoleReader::ConsoleReader ( std::vector *sockets_vector ) +ConsoleReader::ConsoleReader(std::vector *sockets_vector) { sockets = sockets_vector; start(); @@ -36,13 +36,10 @@ void ConsoleReader::run() unsigned int number = 0; bool cont = true; - while ( cont ) - { + while (cont) { std::cin >> command; - switch ( command[0] ) - { - case 'a': - { + switch (command[0]) { + 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::string ssid, password; 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 break; } - case 'A': - { + 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: "; @@ -76,8 +72,7 @@ void ConsoleReader::run() case 'D': (*sockets) [number]->discover(); break; - case 'n': - { + case 'n': { std::string name; std::cout << "Please enter a new name: "; std::cin >> name; @@ -93,8 +88,7 @@ void ConsoleReader::run() else if (command == "on") (*sockets) [number]->powerOn(); break; - case 'P': - { + case 'P': { std::string password; std::cout << "Please enter a new password: "; std::cin >> password; @@ -103,15 +97,14 @@ void ConsoleReader::run() } case 'q': cont = false; - emit ( QCoreApplication::quit() ); + emit(QCoreApplication::quit()); break; case 's': std::cin >> number; --number; // count from 0 listSockets(); break; - case 't': - { + case 't': { int timezone; std::cout << "Please enter a new timezone (integer from -11 to 12): "; std::cin >> timezone; @@ -126,12 +119,11 @@ void ConsoleReader::run() void ConsoleReader::listSockets() { - for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) - { + for (std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) { 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 << "Countdown: " << (*i)->countdown << " " << ( (*i)->countdownEnabled ? "(enabled)" : "(disabled)" ) << "\t\t Time: " << (*i)->socketDateTime.toString().toStdString() << std::endl; + std::cout << "Countdown: " << (*i)->countdown << " " << ((*i)->countdownEnabled ? "(enabled)" : "(disabled)") << "\t\t Time: " << (*i)->socketDateTime.toString().toStdString() << std::endl; } std::cout << "_____________________________________________________________________________\n" << std::endl; std::cout << "a - add unpaired socket (WiFi needed)\n"; @@ -148,8 +140,7 @@ void ConsoleReader::listSockets() 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); } } diff --git a/consolereader.h b/consolereader.h index 0059740..45c4616 100644 --- a/consolereader.h +++ b/consolereader.h @@ -26,7 +26,7 @@ class ConsoleReader : public QThread { public: - ConsoleReader ( std::vector *sockets_vector ); + ConsoleReader(std::vector *sockets_vector); void run(); void listSockets(); void connectSignals(); diff --git a/dialog.cpp b/dialog.cpp index 9a7d009..19b7d4b 100644 --- a/dialog.cpp +++ b/dialog.cpp @@ -36,8 +36,7 @@ Dialog::~Dialog() 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->comboBox->setItemText(i, (*sockets)[i]->socketName); } @@ -46,13 +45,12 @@ void Dialog::updateUi() void Dialog::discovered() { ui->comboBox->clear(); - for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) - { + for (std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) { connect(*i, &Socket::stateChanged, this, &Dialog::updateUi); connect(ui->toggleButton, &QPushButton::clicked, this, &Dialog::togglePower); ui->comboBox->addItem("Socket"); } - + updateUi(); } diff --git a/dialog.h b/dialog.h index 4668167..1b10fc0 100644 --- a/dialog.h +++ b/dialog.h @@ -22,7 +22,8 @@ #include -namespace Ui { +namespace Ui +{ class Dialog; } diff --git a/server.cpp b/server.cpp index aff8e0a..51081c7 100644 --- a/server.cpp +++ b/server.cpp @@ -24,15 +24,15 @@ #include "consolereader.h" #include "server.h" -Server::Server ( std::vector *sockets_vector ) +Server::Server(std::vector *sockets_vector) { sockets = sockets_vector; udpSocketGet = new QUdpSocket(); - udpSocketGet->bind ( QHostAddress::Any, 10000 ); - connect ( udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams); + udpSocketGet->bind(QHostAddress::Any, 10000); + connect(udpSocketGet, &QUdpSocket::readyRead, this, &Server::readPendingDatagrams); discoverSockets(); QTimer *discoverTimer = new QTimer(this); - discoverTimer->setInterval(1*60*1000); // 1 min + discoverTimer->setInterval(1 * 60 * 1000); // 1 min discoverTimer->setSingleShot(false); connect(discoverTimer, &QTimer::timeout, this, &Server::discoverSockets); discoverTimer->start(); @@ -48,25 +48,20 @@ Server::Server(uint16_t port, QByteArray ssid, QByteArray password) ncm->updateConfigurations(); *cfgInitial = ncm->defaultConfiguration(); - if (ssid == "c") - { + if (ssid == "c") { ssid = cfgInitial->name().toLocal8Bit(); qDebug() << "SSID unspecified, using current network: " << ssid; } bool stop = false; - while ( !stop ) - { + while (!stop) { QThread::sleep(1); auto nc = ncm->allConfigurations(); - for (auto &x : nc) - { - if (x.bearerType() == QNetworkConfiguration::BearerWLAN) - { - if (x.name() == "WiWo-S20") - { + for (auto & x : nc) { + if (x.bearerType() == QNetworkConfiguration::BearerWLAN) { + if (x.name() == "WiWo-S20") { qWarning() << "Connecting to WiWo-S20 wireless"; cfg = &x; stop = true; @@ -83,24 +78,24 @@ Server::Server(uint16_t port, QByteArray ssid, QByteArray password) QUdpSocket *udpSocketSend = new QUdpSocket(); udpSocketGet = new QUdpSocket(); - udpSocketGet->bind ( QHostAddress::Any, port); + udpSocketGet->bind(QHostAddress::Any, port); QByteArray reply; - udpSocketGet->writeDatagram ( QByteArray::fromStdString("HF-A11ASSISTHREAD"), QHostAddress::Broadcast, port ); + udpSocketGet->writeDatagram(QByteArray::fromStdString("HF-A11ASSISTHREAD"), QHostAddress::Broadcast, port); reply = listen(QByteArray::fromStdString("HF-A11ASSISTHREAD")); QList list = reply.split(','); QHostAddress ip(QString::fromLatin1(list[0])); qWarning() << "IP: " << ip.toString(); - udpSocketGet->writeDatagram ( QByteArray::fromStdString("+ok"), ip, port ); - udpSocketGet->writeDatagram ( QByteArray::fromStdString("AT+WSSSID=") + ssid + QByteArray::fromStdString("\r"), ip, port ); + udpSocketGet->writeDatagram(QByteArray::fromStdString("+ok"), ip, port); + 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 + 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 ); + udpSocketGet->writeDatagram(QByteArray::fromStdString("AT+WMODE=STA\r"), ip, port); 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 qWarning() << "Finished"; @@ -122,15 +117,12 @@ QByteArray Server::listen(QByteArray message) QHostAddress sender; quint16 senderPort; bool stop = false; - while ( !stop ) - { + while (!stop) { QThread::msleep(50); - while ( udpSocketGet->hasPendingDatagrams() ) - { - reply.resize ( udpSocketGet->pendingDatagramSize() ); - udpSocketGet->readDatagram ( reply.data(), reply.size(), &sender, &senderPort ); - if (reply != message) - { + while (udpSocketGet->hasPendingDatagrams()) { + reply.resize(udpSocketGet->pendingDatagramSize()); + udpSocketGet->readDatagram(reply.data(), reply.size(), &sender, &senderPort); + if (reply != message) { stop = true; } } @@ -145,44 +137,34 @@ void Server::run() void Server::readPendingDatagrams() { - while ( udpSocketGet->hasPendingDatagrams() ) - { + while (udpSocketGet->hasPendingDatagrams()) { QByteArray reply, mac; - reply.resize ( udpSocketGet->pendingDatagramSize() ); + reply.resize(udpSocketGet->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; - 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.mid ( 4, 2 ) == QStringLiteral("qa").toLatin1() || reply.mid ( 4, 2 ) == QStringLiteral("qg").toLatin1()) // Reply to discover packet - { + 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 bool duplicate = false; - for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) - { - if ( (*i)->ip == sender ) - { + for (std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i) { + if ((*i)->ip == sender) { duplicate = true; break; } } - if ( !duplicate ) - { - Socket *socket = new Socket ( sender, reply ); - sockets->push_back ( socket ); + if (!duplicate) { + Socket *socket = new Socket(sender, reply); + sockets->push_back(socket); Q_EMIT discovered(); } - mac = reply.mid(7,6); + mac = reply.mid(7, 6); + } else { + mac = reply.mid(6, 6); } - else - { - mac = reply.mid(6,6); - } - for ( std::vector::iterator i = sockets->begin() ; i != sockets->end(); ++i ) - { - if ( (*i)->mac == mac ) - { + for (std::vector::iterator i = sockets->begin() ; i != sockets->end(); ++i) { + if ((*i)->mac == mac) { (*i)->parseReply(reply); break; } @@ -195,9 +177,9 @@ void Server::readPendingDatagrams() void Server::discoverSockets() { QUdpSocket *udpSocketSend = new QUdpSocket(); - udpSocketSend->connectToHost ( QHostAddress::Broadcast, 10000 ); - udpSocketSend->write ( discover ); - udpSocketSend->write ( discover ); + udpSocketSend->connectToHost(QHostAddress::Broadcast, 10000); + udpSocketSend->write(discover); + udpSocketSend->write(discover); udpSocketSend->disconnectFromHost(); delete udpSocketSend; } @@ -205,37 +187,31 @@ void Server::discoverSockets() void broadcastPassword(QString password) { QUdpSocket *udpSocket = new QUdpSocket(); - udpSocket->connectToHost ( QHostAddress::Broadcast, 49999 ); + udpSocket->connectToHost(QHostAddress::Broadcast, 49999); 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; - for (unsigned short int i = 0; i < 200; ++i) - { - udpSocket->write ( fives(76) ); + 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) ); + 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) ); + 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) ); + 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()) ); + for (unsigned short int i = 0; i < 3; ++i) { + udpSocket->write(fives(332 + password.length())); QThread::msleep(sleep); } } diff --git a/server.h b/server.h index 87a53df..ee448c5 100644 --- a/server.h +++ b/server.h @@ -26,11 +26,11 @@ class QUdpSocket; class Server : public QThread { -Q_OBJECT + Q_OBJECT public: - Server ( std::vector *sockets_vector ); - Server ( uint16_t port, QByteArray ssid, QByteArray password ); + Server(std::vector *sockets_vector); + Server(uint16_t port, QByteArray ssid, QByteArray password); ~Server(); void discoverSockets(); @@ -41,11 +41,11 @@ Q_SIGNALS: void discovered(); private: - QByteArray discover = QByteArray::fromHex ( "68 64 00 06 71 61" ); + QByteArray discover = QByteArray::fromHex("68 64 00 06 71 61"); QUdpSocket *udpSocketGet; std::vector *sockets; - QByteArray listen( QByteArray message = 0 ); + QByteArray listen(QByteArray message = 0); }; void broadcastPassword(QString password); diff --git a/socket.cpp b/socket.cpp index 8accf50..fe1c584 100644 --- a/socket.cpp +++ b/socket.cpp @@ -24,38 +24,38 @@ #include #include -Socket::Socket ( QHostAddress IPaddress, QByteArray reply ) +Socket::Socket(QHostAddress IPaddress, QByteArray reply) { ip = IPaddress; - mac = reply.mid ( 7, 6 ); + mac = reply.mid(7, 6); rmac = mac; - std::reverse ( rmac.begin(), rmac.end() ); - powered = reply.right ( 1 ) == one; + std::reverse(rmac.begin(), rmac.end()); + powered = reply.right(1) == one; - commandID[QueryAll] = QStringLiteral( "qa" ).toLatin1(); - commandID[Discover] = QStringLiteral( "qg" ).toLatin1(); // qg - commandID[Subscribe] = QStringLiteral( "cl" ).toLatin1(); // Login Command - commandID[PowerOn] = QStringLiteral( "sf" ).toLatin1(); // State Flip (change of power state) + commandID[QueryAll] = QStringLiteral("qa").toLatin1(); + commandID[Discover] = QStringLiteral("qg").toLatin1(); // qg + commandID[Subscribe] = QStringLiteral("cl").toLatin1(); // Login Command + commandID[PowerOn] = QStringLiteral("sf").toLatin1(); // State Flip (change of power state) commandID[PowerOff] = commandID[PowerOn]; - commandID[ReadTable] = QStringLiteral( "rt" ).toLatin1(); + commandID[ReadTable] = QStringLiteral("rt").toLatin1(); commandID[SocketData] = commandID[ReadTable]; commandID[TimingData] = commandID[ReadTable]; - commandID[TableModify] = QStringLiteral( "tm" ).toLatin1(); - QByteArray commandIDPower = QStringLiteral( "dc" ).toLatin1(); // Socket change responce + commandID[TableModify] = QStringLiteral("tm").toLatin1(); + QByteArray commandIDPower = QStringLiteral("dc").toLatin1(); // Socket change responce // 2 hex bytes are the total length of the message datagram[Discover] = commandID[Discover] + mac + twenties; datagram[Subscribe] = commandID[Subscribe] + mac + twenties + rmac + twenties; datagram[PowerOn] = commandIDPower + mac + twenties + zeros + one; datagram[PowerOff] = commandIDPower + mac + twenties + zeros + zero; - datagram[ReadTable] = commandID[ReadTable] + mac + twenties + /*zeros*/QByteArray::fromHex ( "72 00 00 00" ) + QByteArray::fromHex ( "01 00 00" ) + zeros; + datagram[ReadTable] = commandID[ReadTable] + mac + twenties + /*zeros*/QByteArray::fromHex("72 00 00 00") + QByteArray::fromHex("01 00 00") + zeros; udpSocket = new QUdpSocket(); - udpSocket->connectToHost ( ip, 10000 ); + udpSocket->connectToHost(ip, 10000); - connect (this, &Socket::datagramQueued, this, &Socket::processQueue); + connect(this, &Socket::datagramQueued, this, &Socket::processQueue); subscribeTimer = new QTimer(this); - subscribeTimer->setInterval(2*60*1000); // 2 min + subscribeTimer->setInterval(2 * 60 * 1000); // 2 min subscribeTimer->setSingleShot(false); connect(subscribeTimer, &QTimer::timeout, this, &Socket::subscribe); subscribeTimer->start(); @@ -69,7 +69,7 @@ Socket::~Socket() delete udpSocket; } -void Socket::sendDatagram ( Datagram d ) +void Socket::sendDatagram(Datagram d) { commands.enqueue(d); Q_EMIT datagramQueued(); @@ -78,22 +78,18 @@ void Socket::sendDatagram ( Datagram d ) void Socket::run() { QMutex mutex; - if ( !mutex.tryLock() ) - { + if (!mutex.tryLock()) { return; } unsigned short retryCount = 0; QByteArray currentDatagram, previousDatagram = 0, recordLength; - while ( commands.size() > 0 ) - { + while (commands.size() > 0) { currentDatagram = datagram[commands.head()]; - if ( previousDatagram == currentDatagram ) - { + if (previousDatagram == currentDatagram) { ++retryCount; } - if ( retryCount == 5 ) - { + if (retryCount == 5) { std::cout << "Stop retrying: " << currentDatagram.toHex().toStdString() << std::endl; commands.dequeue(); retryCount = 0; @@ -103,7 +99,7 @@ void Socket::run() uint16_t length = currentDatagram.length() + 4; // +4 for magicKey and total message length stream << length; - udpSocket->write ( magicKey + recordLength + currentDatagram ); + udpSocket->write(magicKey + recordLength + currentDatagram); previousDatagram = currentDatagram; QThread::msleep(100); } @@ -112,46 +108,46 @@ void Socket::run() void Socket::subscribe() { - sendDatagram ( Subscribe ); + sendDatagram(Subscribe); } void Socket::discover() { - sendDatagram ( Discover ); + sendDatagram(Discover); } void Socket::toggle() { - sendDatagram ( powered ? PowerOff : PowerOn ); + sendDatagram(powered ? PowerOff : PowerOn); } void Socket::powerOff() { if (powered) - sendDatagram ( PowerOff ); + sendDatagram(PowerOff); Q_EMIT stateChanged(); } void Socket::powerOn() { if (!powered) - sendDatagram ( PowerOn ); + sendDatagram(PowerOn); Q_EMIT stateChanged(); } -void Socket::changeSocketName ( QString newName ) +void Socket::changeSocketName(QString newName) { QByteArray name = newName.toLatin1().leftJustified(16, ' ', true); writeSocketData(name, remotePassword, timeZone, countdown); } -void Socket::changeSocketPassword ( QString newPassword ) +void Socket::changeSocketPassword(QString newPassword) { QByteArray password = newPassword.toLatin1().leftJustified(12, ' ', true); writeSocketData(socketName, password, timeZone, countdown); } -void Socket::changeTimezone ( int8_t newTimezone ) +void Socket::changeTimezone(int8_t newTimezone) { QByteArray timezone; QDataStream stream(&timezone, QIODevice::WriteOnly); @@ -160,7 +156,7 @@ void Socket::changeTimezone ( int8_t newTimezone ) writeSocketData(socketName, remotePassword, timezone, countdown); } -void Socket::setCountDown ( uint16_t countdown ) +void Socket::setCountDown(uint16_t countdown) { writeSocketData(socketName, remotePassword, timeZone, countdown); } @@ -172,7 +168,7 @@ void Socket::writeSocketData(QByteArray socketName, QByteArray remotePassword, Q stream1.setByteOrder(QDataStream::LittleEndian); stream1 << countdown; - QByteArray record = QByteArray::fromHex ( "01:00" ) /* record number = 1*/ + versionID + mac + twenties + rmac + twenties + remotePassword + socketName + icon + hardwareVersion + firmwareVersion + wifiFirmwareVersion + port + staticServerIP + port + domainServerName + localIP + localGatewayIP + localNetMask + dhcpNode + discoverable + timeZoneSet + timeZone + ( countdownEnabled ? QByteArray::fromHex ( "01:00" ) : QByteArray::fromHex ( "00:ff" ) ) + countDown + zeros + zeros + zeros + zeros + QStringLiteral("000000000000000000000000000000").toLocal8Bit(); + QByteArray record = QByteArray::fromHex("01:00") /* record number = 1*/ + versionID + mac + twenties + rmac + twenties + remotePassword + socketName + icon + hardwareVersion + firmwareVersion + wifiFirmwareVersion + port + staticServerIP + port + domainServerName + localIP + localGatewayIP + localNetMask + dhcpNode + discoverable + timeZoneSet + timeZone + (countdownEnabled ? QByteArray::fromHex("01:00") : QByteArray::fromHex("00:ff")) + countDown + zeros + zeros + zeros + zeros + QStringLiteral("000000000000000000000000000000").toLocal8Bit(); QByteArray recordLength; QDataStream stream(&recordLength, QIODevice::WriteOnly); @@ -180,34 +176,31 @@ void Socket::writeSocketData(QByteArray socketName, QByteArray remotePassword, Q uint16_t length = record.length(); stream << length; - datagram[TableModify] = commandID[TableModify] + mac + twenties + zeros + QByteArray::fromHex ( "04:00:01" ) /*table number and unknown*/ + recordLength + record; - sendDatagram ( TableModify ); + datagram[TableModify] = commandID[TableModify] + mac + twenties + zeros + QByteArray::fromHex("04:00:01") /*table number and unknown*/ + recordLength + record; + sendDatagram(TableModify); } void Socket::tableData() { - sendDatagram ( ReadTable ); - datagram[SocketData] = commandID[SocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04 00 00" ) + zeros; - datagram[TimingData] = commandID[TimingData] + mac + twenties + zeros + QByteArray::fromHex ( "03 00 00" ) + zeros; + sendDatagram(ReadTable); + datagram[SocketData] = commandID[SocketData] + mac + twenties + zeros + QByteArray::fromHex("04 00 00") + zeros; + datagram[TimingData] = commandID[TimingData] + mac + twenties + zeros + QByteArray::fromHex("03 00 00") + zeros; // table number + 00 + version number - sendDatagram ( SocketData ); - sendDatagram ( TimingData ); + sendDatagram(SocketData); + sendDatagram(TimingData); } -bool Socket::parseReply ( QByteArray reply ) +bool Socket::parseReply(QByteArray reply) { - if ( reply.left(2) != magicKey ) - { + if (reply.left(2) != magicKey) { return false; } - QByteArray id = reply.mid ( 4, 2 ); - unsigned int datagram = std::distance ( commandID, std::find ( commandID, commandID + MaxCommands, id ) ); // match commandID with enum - if ( datagram == ReadTable ) // determine the table number - { - unsigned int table = reply[reply.indexOf ( twenties ) + 11]; - switch ( table ) - { + QByteArray id = reply.mid(4, 2); + unsigned int datagram = std::distance(commandID, std::find(commandID, commandID + MaxCommands, id)); // match commandID with enum + if (datagram == ReadTable) { // determine the table number + unsigned int table = reply[reply.indexOf(twenties) + 11]; + switch (table) { case 1: break; case 3: @@ -224,11 +217,9 @@ bool Socket::parseReply ( QByteArray reply ) return false; } } - switch ( datagram ) - { + switch (datagram) { case QueryAll: - case Discover: - { + case Discover: { QByteArray timeArray = reply.right(5).left(4); QDataStream stream(&timeArray, QIODevice::ReadOnly); stream.setByteOrder(QDataStream::LittleEndian); @@ -240,64 +231,60 @@ bool Socket::parseReply ( QByteArray reply ) } case Subscribe: case PowerOff: - case PowerOn: - { + case PowerOn: { bool poweredOld = powered; powered = reply.right(1) == one; - if ( powered != poweredOld ) - { + if (powered != poweredOld) { Q_EMIT stateChanged(); } - if ( datagram == PowerOff && powered == true ) // Required to deque - { + if (datagram == PowerOff && powered == true) { // Required to deque datagram = PowerOn; } break; } case ReadTable: // FIXME: order might be swapped; - socketTableVersion = reply.mid ( reply.indexOf ( QByteArray::fromHex ( "000100000600" ) ) + 6, 2 ); + socketTableVersion = reply.mid(reply.indexOf(QByteArray::fromHex("000100000600")) + 6, 2); // 000100000600 break; - case SocketData: - { + case SocketData: { // std::cout << reply.toHex().toStdString() << " " << datagram << std::endl; // for debugging purposes only - unsigned short int index = reply.indexOf ( rmac + twenties ); - versionID = reply.mid ( index - 14, 2 ); + unsigned short int index = reply.indexOf(rmac + twenties); + versionID = reply.mid(index - 14, 2); index += 12; // length of rmac + padding - remotePassword = reply.mid ( index, 12 ); // max 12 symbols + remotePassword = reply.mid(index, 12); // max 12 symbols index += 12; - socketName = reply.mid ( index, 16 ); // max 16 symbols + socketName = reply.mid(index, 16); // max 16 symbols index += 16; - icon = reply.mid ( index, 2 ); + icon = reply.mid(index, 2); index += 2; - hardwareVersion = reply.mid ( index, 4 ); + hardwareVersion = reply.mid(index, 4); index += 4; - firmwareVersion = reply.mid ( index, 4 ); + firmwareVersion = reply.mid(index, 4); index += 4; - wifiFirmwareVersion = reply.mid ( index, 4 ); + wifiFirmwareVersion = reply.mid(index, 4); index += 6; - staticServerIP = reply.mid ( index, 4 ); // 42.121.111.208 is used + staticServerIP = reply.mid(index, 4); // 42.121.111.208 is used index += 6; - domainServerName = reply.mid ( index, 40); + domainServerName = reply.mid(index, 40); index += 40; - localIP = reply.mid ( index, 4 ); + localIP = reply.mid(index, 4); index += 4; - localGatewayIP = reply.mid ( index, 4 ); + localGatewayIP = reply.mid(index, 4); index += 4; - localNetMask = reply.mid ( index, 4 ); + localNetMask = reply.mid(index, 4); index += 4; - dhcpNode = reply.mid ( index, 1 ); + dhcpNode = reply.mid(index, 1); ++index; - discoverable = reply.mid ( index, 1 ); + discoverable = reply.mid(index, 1); ++index; - timeZoneSet = reply.mid ( index, 1 ); + timeZoneSet = reply.mid(index, 1); ++index; - timeZone = reply.mid ( index, 1 ); + timeZone = reply.mid(index, 1); ++index; - countdownEnabled = reply.mid ( index, 2 ) == QByteArray::fromHex ( "01:00" ); + countdownEnabled = reply.mid(index, 2) == QByteArray::fromHex("01:00"); index += 2; - QByteArray countDown = reply.mid ( index, 2 ); + QByteArray countDown = reply.mid(index, 2); QDataStream stream(&countDown, QIODevice::ReadOnly); stream.setByteOrder(QDataStream::LittleEndian); stream >> countdown; @@ -308,16 +295,14 @@ bool Socket::parseReply ( QByteArray reply ) // std::cout << reply.toHex().toStdString() << " " << datagram << std::endl; // for debugging purposes only break; case TableModify: - sendDatagram ( SocketData ); + sendDatagram(SocketData); break; default: return false; } - if (commands.size() > 0) - { - if ( datagram == commands.head() ) - { + if (commands.size() > 0) { + if (datagram == commands.head()) { commands.dequeue(); } } diff --git a/socket.h b/socket.h index 2e41330..89428b9 100644 --- a/socket.h +++ b/socket.h @@ -27,29 +27,29 @@ class QUdpSocket; -const QByteArray magicKey = QByteArray::fromHex ( "68 64" ); // recognize datagrams from the socket +const QByteArray magicKey = QByteArray::fromHex("68 64"); // recognize datagrams from the socket class Socket : public QThread { -Q_OBJECT + Q_OBJECT Q_SIGNALS: void stateChanged(); void datagramQueued(); public: - Socket ( QHostAddress, QByteArray ); // from discovery packet + Socket(QHostAddress, QByteArray); // from discovery packet ~Socket(); void toggle(); void powerOff(); void powerOn(); void discover(); void tableData(); - void changeSocketName ( QString newName ); - void changeSocketPassword ( QString newPassword ); - void changeTimezone ( int8_t newTimezone ); - void setCountDown ( uint16_t countdown ); - bool parseReply ( QByteArray ); + void changeSocketName(QString newName); + void changeSocketPassword(QString newPassword); + void changeTimezone(int8_t newTimezone); + void setCountDown(uint16_t countdown); + bool parseReply(QByteArray); QHostAddress ip; QByteArray mac; @@ -72,13 +72,15 @@ private: TimingData, TableModify, MaxCommands -}; + }; - void sendDatagram ( Datagram ); + void sendDatagram(Datagram); void subscribe(); - void processQueue() { start(); } + void processQueue() { + start(); + } void run(); - void writeSocketData (QByteArray socketName, QByteArray remotePassword, QByteArray timeZone, uint16_t countdown); + void writeSocketData(QByteArray socketName, QByteArray remotePassword, QByteArray timeZone, uint16_t countdown); QByteArray commandID[MaxCommands]; QByteArray datagram[MaxCommands]; @@ -89,7 +91,7 @@ private: QByteArray firmwareVersion; QByteArray wifiFirmwareVersion; // CC3000 firmware version QByteArray staticServerIP; - QByteArray port = QByteArray::fromHex ( "10:27" ); // 10000 in hex (little endian) + QByteArray port = QByteArray::fromHex("10:27"); // 10000 in hex (little endian) QByteArray domainServerName; QByteArray localIP; // same as QHostAddress ip but in QByteArray, maybe fix later... QByteArray localGatewayIP; @@ -99,10 +101,10 @@ private: QByteArray timeZoneSet; QByteArray socketTableNumber, socketTableVersion, timingTableNumber, timingTableVersion; // FIXME: not used yet - const QByteArray twenties = QByteArray::fromHex ( "20 20 20 20 20 20" ); // mac address padding, 6 spaces - const QByteArray zeros = QByteArray::fromHex ( "00 00 00 00" ); - const QByteArray zero = QByteArray::fromHex ( "00" ); - const QByteArray one = QByteArray::fromHex ( "01" ); + const QByteArray twenties = QByteArray::fromHex("20 20 20 20 20 20"); // mac address padding, 6 spaces + const QByteArray zeros = QByteArray::fromHex("00 00 00 00"); + const QByteArray zero = QByteArray::fromHex("00"); + const QByteArray one = QByteArray::fromHex("01"); QUdpSocket *udpSocket; QTimer *subscribeTimer;