forked from Bntdumas/SamsungIPRemote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvNetworkControl.cpp
183 lines (155 loc) · 5.67 KB
/
tvNetworkControl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "tvNetworkControl.h"
#include <QTcpSocket>
#include <QStringList>
#include <QtEndian>
tvNetworkControl::tvNetworkControl(QObject *parent) :
QObject(parent),
m_socket(0),
m_connected(false)
{
}
void tvNetworkControl::connectToTV(const QString & ip)
{
if (m_socket) {
delete m_socket;
}
m_socket = new QTcpSocket(this);
m_socket->connectToHost(ip, 55000);
connect(m_socket, SIGNAL(connected()), this, SLOT(sltConnected()));
connect(m_socket, SIGNAL(disconnected()), this, SLOT(sltDisconnected()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(sltError(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(readyRead()), this, SLOT(sltReadyRead()));
}
void tvNetworkControl::sltConnected()
{
emit logText(tr("<b>Connected!</b><br>"));
m_connected = true;
}
void tvNetworkControl::sltReadyRead()
{
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0)
return;
QByteArray recievedData = socket->readAll().toHex();
emit logText(tr("============ <b>Recieved Datagram</b> =============<br>%1").arg(byteArrayToHexaString(recievedData, false)));
}
void tvNetworkControl::sltDisconnected()
{
emit logText(tr("<b>Disconnected!</b><br>"));
m_socket->abort();
m_connected = false;
}
void tvNetworkControl::sltError(QAbstractSocket::SocketError error)
{
Q_UNUSED(error);
emit logText(tr("<b>Network error: %1</b><br>").arg(m_socket->errorString()));
}
void tvNetworkControl::sendIDPacket(const QString &remoteIP, const QString &ID, const QString &name)
{
const char packetStart[] = {
0x00, 0x13, 0x00, 0x69, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x2e, 0x69, 0x61, 0x70, 0x70, 0x2e, 0x73,
0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67
};
const QByteArray remoteIPEncoded = encodeString(remoteIP);
const QByteArray remoteIDEncoded = encodeString(ID);
const QByteArray remoteNameEncoded = encodeString(name);
const char payLoadStart[] = {
0x64, 0x00
};
QByteArray payLoad;
payLoad.append(QByteArray::fromRawData(payLoadStart, sizeof(payLoadStart)));
payLoad.append(QByteArray::fromRawData(remoteIPEncoded.data(), remoteIPEncoded.size()));
payLoad.append(QByteArray::fromRawData(remoteIDEncoded.data(), remoteIDEncoded.size()));
payLoad.append(QByteArray::fromRawData(remoteNameEncoded.data(), remoteNameEncoded.size()));
const qint16 payLoadSize = payLoad.size();
QByteArray packet;
QDataStream stream(&packet, QIODevice::WriteOnly);
stream.writeRawData(packetStart, sizeof(packetStart));
stream.setByteOrder(QDataStream::LittleEndian);
stream << payLoadSize;
stream.setByteOrder(QDataStream::BigEndian);
stream.writeRawData(payLoad, payLoadSize);
if (m_socket) {
emit logText(tr("=========== <b>Send ID Datagram</b> ===========<br>%1").arg(byteArrayToHexaString(packet)));
sendRaw(packet);
}
}
void tvNetworkControl::sendKey(const QString &key)
{
#ifdef QT5
QByteArray keyByteArray(key.toLatin1());
#else
QByteArray keyByteArray(key.toAscii());
#endif
keyByteArray = keyByteArray.toBase64();
const char rawKeyPacketStart[] = {
0x00, 0x13, 0x00, 0x69, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x2e, 0x69, 0x61, 0x70, 0x70, 0x2e, 0x73,
0x61, 0x6d, 0x73, 0x75, 0x6e, 0x67
};
const char payloadStart[] = {0x00, 0x00, 0x00};
const qint16 keySize = keyByteArray.size();
const qint16 payloadSize = keyByteArray.size() + 5; // 5 is 3 0x00 bytes + 2 bytes for the key code size
QByteArray keyPacket;
QDataStream stream(&keyPacket, QIODevice::WriteOnly);
stream.writeRawData(rawKeyPacketStart, sizeof(rawKeyPacketStart));
stream.setByteOrder(QDataStream::LittleEndian);
stream << payloadSize;
stream.setByteOrder(QDataStream::BigEndian);
stream.writeRawData(payloadStart, sizeof(payloadStart));
stream.setByteOrder(QDataStream::LittleEndian);
stream << keySize;
stream.setByteOrder(QDataStream::BigEndian);
stream.writeRawData(keyByteArray, keyByteArray.size());
if (m_socket) {
emit logText(tr("============ <b>Send Datagram</b> =============<br>%1").arg(byteArrayToHexaString(keyPacket)));
sendRaw(keyPacket);
}
}
void tvNetworkControl::sendRaw(const QByteArray &data)
{
if (m_connected) {
m_socket->write(data, data.size());
}
}
QString tvNetworkControl::byteArrayToHexaString(const QByteArray &array, bool needHexConversion) const
{
QString hex;
if (needHexConversion) {
hex = QString(array.toHex());
} else {
hex = QString(array);
}
int byteCount = 0;
for (int i = 0; i < hex.size() + 1; i++) { //add a space to make groups of bytes
if ((i+1) % 3) {
hex.insert(i++, ' ');
byteCount++;
}
}
// big hack to break lines, ugly but it works
QStringList byteList = hex.split(' ');
for (double i = 1; i < (byteCount / 16.0) + 1; i++) {
byteList.insert(i * 17, "<br>");
}
// make it nicer, still super hacky but it does the trick
hex = byteList.join(" ");
hex.replace(QString("<br> "), QString("<br>"));
return hex;
}
QByteArray tvNetworkControl::encodeString(const QString &str)
{
#ifdef QT5
const QByteArray strBase64 = QByteArray(str.toLatin1()).toBase64();
#else
const QByteArray strBase64 = QByteArray(str.toAscii()).toBase64();
#endif
const qint16 strSize = strBase64.size();
QByteArray encodedStr;
QDataStream stream(&encodedStr, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << strSize;
stream.setByteOrder(QDataStream::BigEndian);
stream.writeRawData(strBase64, strSize);
return encodedStr;
}