From 32cf746aceef2198a4e9269c3087afa48c8d7e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Wed, 27 May 2015 00:03:33 +0100 Subject: [PATCH] Rename some commandID enums to be closer their ASCII value GlobalDiscover -> QueryAll TableData -> ReadTable WriteTableData -> TableModify --- server.cpp | 2 +- socket.cpp | 30 +++++++++++++++--------------- socket.h | 13 ++++++++++++- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/server.cpp b/server.cpp index bd477c5..aff8e0a 100644 --- a/server.cpp +++ b/server.cpp @@ -156,7 +156,7 @@ void Server::readPendingDatagrams() if ( reply != discover && reply.left ( 2 ) == magicKey ) // check for Magic Key { - if ( reply.mid ( 4, 2 ) == QByteArray::fromHex ( "71 61" ) || reply.mid ( 4, 2 ) == QByteArray::fromHex ( "71 67" )) // 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; for ( std::vector::const_iterator i = sockets->begin() ; i != sockets->end(); ++i ) diff --git a/socket.cpp b/socket.cpp index e644e90..90d1994 100644 --- a/socket.cpp +++ b/socket.cpp @@ -32,15 +32,15 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply ) std::reverse ( rmac.begin(), rmac.end() ); powered = reply.right ( 1 ) == one; - commandID[GlobalDiscover] = QStringLiteral( "qa" ).toLatin1(); // Query All + commandID[QueryAll] = QStringLiteral( "qa" ).toLatin1(); commandID[Discover] = QStringLiteral( "qg" ).toLatin1(); // qg commandID[Subscribe] = QStringLiteral( "cl" ).toLatin1(); // Login Command - commandID[PowerOn] = QStringLiteral( "sf" ).toLatin1(); // sf (change of power state) + commandID[PowerOn] = QStringLiteral( "sf" ).toLatin1(); // State Flip (change of power state) commandID[PowerOff] = commandID[PowerOn]; - commandID[TableData] = QStringLiteral( "rt" ).toLatin1(); // Read Table - commandID[SocketData] = commandID[TableData]; - commandID[TimingData] = commandID[TableData]; - commandID[WriteSocketData] = QStringLiteral( "tm" ).toLatin1(); // Table Modify + 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 // 2 hex bytes are the total length of the message @@ -48,7 +48,7 @@ Socket::Socket ( QHostAddress IPaddress, QByteArray reply ) datagram[Subscribe] = commandID[Subscribe] + mac + twenties + rmac + twenties; datagram[PowerOn] = commandIDPower + mac + twenties + zeros + one; datagram[PowerOff] = commandIDPower + mac + twenties + zeros + zero; - datagram[TableData] = commandID[TableData] + 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 ); @@ -170,13 +170,13 @@ void Socket::writeSocketData(QByteArray name, QByteArray password, QByteArray ti uint16_t length = record.length(); stream << length; - datagram[WriteSocketData] = commandID[WriteSocketData] + mac + twenties + zeros + QByteArray::fromHex ( "04:00:01" ) /*table number and unknown*/ + recordLength + record; - sendDatagram ( WriteSocketData ); + datagram[TableModify] = commandID[TableModify] + mac + twenties + zeros + QByteArray::fromHex ( "04:00:01" ) /*table number and unknown*/ + recordLength + record; + sendDatagram ( TableModify ); } void Socket::tableData() { - sendDatagram ( 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; // table number + 00 + version number @@ -193,7 +193,7 @@ bool Socket::parseReply ( QByteArray reply ) QByteArray id = reply.mid ( 4, 2 ); unsigned int datagram = std::distance ( commandID, std::find ( commandID, commandID + MaxCommands, id ) ); // match commandID with enum - if ( datagram == TableData ) // determine the table number + if ( datagram == ReadTable ) // determine the table number { unsigned int table = reply[reply.indexOf ( twenties ) + 11]; switch ( table ) @@ -210,13 +210,13 @@ bool Socket::parseReply ( QByteArray reply ) qWarning() << "No table"; // FIXME: initial data query default: qWarning() << "Failed to identify data table."; - datagram = TableData; + datagram = ReadTable; return false; } } switch ( datagram ) { - case GlobalDiscover: + case QueryAll: case Discover: { QByteArray timeArray = reply.right(5).left(4); @@ -244,7 +244,7 @@ bool Socket::parseReply ( QByteArray reply ) } break; } - case TableData: + case ReadTable: // FIXME: order might be swapped; socketTableVersion = reply.mid ( reply.indexOf ( QByteArray::fromHex ( "000100000600" ) ) + 6, 2 ); // 000100000600 @@ -292,7 +292,7 @@ bool Socket::parseReply ( QByteArray reply ) case TimingData: // std::cout << reply.toHex().toStdString() << " " << datagram << std::endl; // for debugging purposes only break; - case WriteSocketData: + case TableModify: sendDatagram ( SocketData ); break; default: diff --git a/socket.h b/socket.h index 6aff2a1..3360252 100644 --- a/socket.h +++ b/socket.h @@ -59,7 +59,18 @@ public: QDateTime socketDateTime; private: - enum Datagram {GlobalDiscover, Discover, Subscribe, PowerOff, PowerOn, TableData, SocketData, TimingData, WriteSocketData, MaxCommands}; + enum Datagram { + QueryAll, + Discover, + Subscribe, + PowerOff, + PowerOn, + ReadTable, + SocketData, + TimingData, + TableModify, + MaxCommands +}; void sendDatagram ( Datagram ); void subscribe();