-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqinvsimulator.cpp
More file actions
187 lines (126 loc) · 3.89 KB
/
Copy pathqinvsimulator.cpp
File metadata and controls
187 lines (126 loc) · 3.89 KB
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
184
185
186
187
#include <QTcpServer>
#include <QTcpSocket>
#include <QSignalMapper>
#include <QPair>
#include <QByteArray>
#include <QHash>
#include <QQueue>
#include <QStringList>
#include <QDebug>
#include "qinvsimulator.hpp"
QInvSimulator::QInvSimulator(QObject *parent) : QObject(parent)
{
id = 0;
sock = new QTcpServer(this);
querymapper = new QSignalMapper(this);
removemapper = new QSignalMapper(this);
sock->listen(QHostAddress(QHostAddress::LocalHost), 12345);
connect(sock, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
connect(querymapper, SIGNAL(mapped(int)), this, SLOT(sendResponse(int)));
connect(removemapper, SIGNAL(mapped(int)), this, SLOT(removeSocket(int)));
}
QInvSimulator::~QInvSimulator()
{
}
void QInvSimulator::handleNewConnection()
{
id++;
QTcpSocket* conn = sock->nextPendingConnection();
querymapper->setMapping(conn, id);
connect(conn, SIGNAL(readyRead()), querymapper, SLOT(map()));
connect(conn, SIGNAL(disconnected()), removemapper, SLOT(map()));
queue.insert(id, conn);
qDebug() << "New connection" << id;
}
void QInvSimulator::removeSocket(int id)
{
QTcpSocket* sck = queue[id];
queue.remove(id);
delete sck;
qDebug() << "Delete connection" << id;
}
void QInvSimulator::sendResponse(int id)
{
QTcpSocket* sck = queue[id];
QByteArray raw = sck->readAll();
QString query(raw);
qDebug() << "[" << id << "] RECV " << query;
QString resp = responsePrepare(query);
qDebug() << "[" << id << "] SEND " << resp;
QByteArray response = resp.toAscii();
sck->write(response);
}
QString QInvSimulator::responsePrepare(QString query)
{
qDebug() << "responsePrepare";
QStringList frag = query.split("|");
QStringList flags = frag[0].mid(1).split(";");
QString check = frag[2].mid(0, 4);
QString tocheck = frag[0].mid(1) + "|" + frag[1] + "|";
qDebug() << tocheck << checksum(tocheck) << check;
if(checksum(tocheck) != check)
return nullptr;
qDebug() << " Checkusm OK";
if(query.length() != flags[2].toInt(nullptr, 16))
return nullptr;
qDebug() << " Lenght OK";
QQueue<QString> resvalues;
foreach(QString elem, frag[1].mid(4).split(";")) {
int value = 0;
if(elem == "UDC")
value = randInt(2700, 4200);
if(elem == "IDC")
value = randInt(0, 1000);
if(elem == "UL1")
value = randInt(2200, 2450);
if(elem == "IL1")
value = randInt(0, 1600);
if(elem == "PAC")
value = randInt(0, 5500);
if(elem == "PRL")
value = randInt(1, 30);
if(elem == "TKK")
value = randInt(10, 60);
if(elem == "TNF")
value = randInt(4990, 5010);
if(elem == "KDY")
value = randInt(40, 150);
if(elem == "KLD")
value = randInt(40, 150);
QString item = elem + "=" + QString::number(value, 16).toUpper();
resvalues.enqueue(item);
}
QString allvalues;
while(!resvalues.isEmpty())
allvalues += resvalues.dequeue() + ";";
allvalues = allvalues.left(allvalues.length() - 1) ;
QString len;
len.sprintf("%02X", 13 + allvalues.length() + 6);
QString response;
response += flags[1] + ";";
response += flags[0] + ";";
response += len;
response += "|64:";
response += allvalues;
response += "|";
response = "{" + response + checksum(response) + "}";
return response;
}
QString QInvSimulator::checksum(QString input)
{
if(input == nullptr)
return nullptr;
if(input.length() == 0)
return "";
int sum = 0;
foreach(QChar c, input) {
sum += c.toAscii();
sum %= 0xffff;
}
QString ret;
return ret.sprintf("%04X", sum);
}
int QInvSimulator::randInt(int low, int high)
{
return qrand() % ((high + 1) - low) + low;
}