forked from Skarsnik/QUsb2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsservercommands.cpp
410 lines (386 loc) · 14 KB
/
wsservercommands.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "ipsparse.h"
#include "wsserver.h"
#include <QLoggingCategory>
#include <QSerialPortInfo>
bool WSServer::isFileCommand(USB2SnesWS::opcode opcode)
{
return (opcode == USB2SnesWS::GetFile || opcode == USB2SnesWS::PutFile || opcode == USB2SnesWS::List ||
opcode == USB2SnesWS::Rename || opcode == USB2SnesWS::MakeDir || opcode == USB2SnesWS::Rename ||
opcode == USB2SnesWS::Remove);
}
bool WSServer::isControlCommand(USB2SnesWS::opcode opcode)
{
return (opcode == USB2SnesWS::Boot || opcode == USB2SnesWS::Reset || opcode == USB2SnesWS::Menu);
}
#define sDebug() qCDebug(log_wsserver)
#define CMD_TAKE_ONE_ARG(cmd) if (req->arguments.size() != 1) \
{ \
setError(ErrorType::CommandError, QString("%1 command take one argument").arg(cmd)); \
clientError(ws); \
return ; \
}
void WSServer::executeRequest(MRequest *req)
{
ADevice* device = nullptr;
QWebSocket* ws = req->owner;
sDebug() << "Executing request : " << *req << "for" << wsInfos.value(ws).name;
if (wsInfos.value(ws).attached)
device = wsInfos.value(ws).attachedTo;
switch (req->opcode)
{
case USB2SnesWS::DeviceList : {
QStringList l = getDevicesList();
sendReply(ws, l);
break;
}
case USB2SnesWS::Attach : {
CMD_TAKE_ONE_ARG("Attach")
cmdAttach(req);
break;
}
case USB2SnesWS::AppVersion : {
sendReply(ws, "7.42.0");
break;
}
case USB2SnesWS::Name : {
CMD_TAKE_ONE_ARG("Name")
wsInfos[ws].name = req->arguments.at(0);
break;
}
case USB2SnesWS::Info : {
device->infoCommand();
req->state = RequestState::WAITINGREPLY;
break;
}
/*
* Control commands
*/
case USB2SnesWS::Reset :
{
device->controlCommand(SD2Snes::opcode::RESET);
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::Menu :
{
device->controlCommand(SD2Snes::opcode::MENU_RESET);
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::Boot :
{
CMD_TAKE_ONE_ARG("Boot")
device->controlCommand(SD2Snes::opcode::BOOT, req->arguments.at(0).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
/*
* File commands
*/
case USB2SnesWS::List : {
CMD_TAKE_ONE_ARG("List")
device->fileCommand(SD2Snes::opcode::LS, req->arguments.at(0).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::GetFile : {
CMD_TAKE_ONE_ARG("GetFile")
connect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)), Qt::UniqueConnection);
connect(device, SIGNAL(sizeGet(uint)), this, SLOT(onDeviceSizeGet(uint)), Qt::UniqueConnection);
device->fileCommand(SD2Snes::opcode::GET, req->arguments.at(0).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::PutFile : {
if (req->arguments.size() != 2)
{
setError(ErrorType::CommandError, "PutFile command take 2 arguments (file1, SizeInHex)");
clientError(ws);
return ;
}
bool ok;
device->putFile(req->arguments.at(0).toLatin1(), req->arguments.at(1).toInt(&ok, 16));
req->state = RequestState::WAITINGREPLY;
wsInfos[ws].commandState = ClientCommandState::WAITINGBDATAREPLY;
break;
}
case USB2SnesWS::Rename : {
if (req->arguments.size() != 2)
{
setError(ErrorType::CommandError, "Rename command take 2 arguments (file1, file2)");
clientError(ws);
return ;
}
device->fileCommand(SD2Snes::opcode::MV, QVector<QByteArray>() << req->arguments.at(0).toLatin1()
<< req->arguments.at(1).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::Remove : {
CMD_TAKE_ONE_ARG("Remove")
device->fileCommand(SD2Snes::opcode::RM, req->arguments.at(0).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::MakeDir : {
CMD_TAKE_ONE_ARG("MakeDir")
device->fileCommand(SD2Snes::opcode::MKDIR, req->arguments.at(0).toLatin1());
req->state = RequestState::WAITINGREPLY;
break;
}
/*
* Address command
*/
case USB2SnesWS::GetAddress : {
if (req->arguments.size() < 2)
{
setError(ErrorType::CommandError, "GetAddress commands take at least 2 arguments (AddressInHex, SizeInHex)");
clientError(ws);
return ;
}
connect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)), Qt::UniqueConnection);
//connect(device, SIGNAL(sizeGet(uint)), this, SLOT(onDeviceSizeGet(uint)));
bool ok;
if (req->arguments.size() == 2)
{
device->getAddrCommand(req->space, req->arguments.at(0).toInt(&ok, 16), req->arguments.at(1).toInt(&ok, 16));
} else {
QList<QPair<unsigned int, unsigned int> > pairs;
for (unsigned int i = 0; i < req->arguments.size(); i += 2)
{
pairs.append(QPair<unsigned int, unsigned int>(req->arguments.at(i).toUInt(&ok, 16), req->arguments.at(i + 1).toUInt(&ok, 16)));
}
//device->getAddrCommand(req->space, pairs);*/
// FIXME this is a meh workaround, but it works I guess?
device->getAddrCommand(req->space, req->arguments.at(0).toInt(&ok, 16), req->arguments.at(1).toInt(&ok, 16));
for (unsigned int i = 1; i < pairs.size(); i++)
{
MRequest* newReq = new MRequest();
newReq->owner = ws;
newReq->state = RequestState::NEW;
newReq->space = SD2Snes::space::SNES;
newReq->timeCreated = QTime::currentTime();
newReq->wasPending = false;
newReq->opcode = USB2SnesWS::GetAddress;
newReq->arguments.append(QString::number(pairs.at(i).first, 16));
newReq->arguments.append(QString::number(pairs.at(i).second, 16));
pendingRequests[device].append(newReq);
}
}
req->state = RequestState::WAITINGREPLY;
break;
}
case USB2SnesWS::PutAddress : {
if (req->arguments.size() < 2)
{
setError(ErrorType::CommandError, "PutAddress command take at least 2 arguments (AddressInHex, SizeInHex)");
clientError(ws);
return ;
}
bool ok;
// Basic usage of PutAddress
// FIXME add flags in VPUT
if (req->arguments.size() == 2)
{
if (req->flags.isEmpty())
device->putAddrCommand(req->space, req->arguments.at(0).toInt(&ok, 16), req->arguments.at(1).toInt(&ok, 16));
else {
unsigned char flags = 0;
foreach(QString flag, req->flags)
{
flags |= (SD2Snes::server_flags) flagsMetaEnum.keyToValue(qPrintable(flag));
}
device->putAddrCommand(req->space, flags, req->arguments.at(0).toInt(&ok, 16), req->arguments.at(1).toInt(&ok, 16));
}
if (req->wasPending && !wsInfos[ws].pendingPutDatas.isEmpty())
{
device->writeData(wsInfos[ws].pendingPutDatas.takeFirst());
}
} else {
QList<QPair<unsigned int, quint8> > pairs;
for (unsigned int i = 0; i < req->arguments.size(); i += 2)
{
pairs.append(QPair<unsigned int, quint8>(req->arguments.at(i).toUInt(&ok, 16), req->arguments.at(i + 1).toUShort(&ok, 16)));
}
device->putAddrCommand(req->space, pairs);
}
req->state = RequestState::WAITINGREPLY;
wsInfos[ws].commandState = ClientCommandState::WAITINGBDATAREPLY;
break;
}
/*
* PutIPS
*/
case USB2SnesWS::PutIPS : {
if (req->arguments.size() < 2)
{
setError(ErrorType::CommandError, "PutIPS command take at least 2 arguments (Name, SizeInHex)");
clientError(ws);
return ;
}
bool ok;
req->state = RequestState::WAITINGREPLY;
wsInfos[ws].commandState = ClientCommandState::WAITINGBDATAREPLY;
wsInfos[ws].ipsSize = req->arguments.at(1).toUInt(&ok, 16);
break;
}
default:
{
setError(ErrorType::ProtocolError, "Invalid command or non implemented");
clientError(ws);
}
}
sDebug() << "Request executed";
}
#undef CMD_TAKE_ONE_ARG
void WSServer::processDeviceCommandFinished(ADevice* device)
{
DeviceInfos& info = devicesInfos[device];
sDebug() << "Processing command finished" << info.currentCommand;
switch (info.currentCommand) {
case USB2SnesWS::Info :
{
USB2SnesInfo ifo = device->parseInfo(device->dataRead);
sendReply(info.currentWS, QStringList() << ifo.version << "foo" << ifo.romPlaying << ifo.flags);
break;
}
// FILE Command
case USB2SnesWS::List :
{
QList<ADevice::FileInfos> lfi = device->parseLSCommand(device->dataRead);
QStringList rep;
foreach(ADevice::FileInfos fi, lfi)
{
rep << QString::number((quint32) fi.type);
rep << fi.name;
}
sendReply(info.currentWS, rep);
break;
}
case USB2SnesWS::GetFile :
{
disconnect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)));
disconnect(device, SIGNAL(sizeGet(uint)), this, SLOT(onDeviceSizeGet(uint)));
break;
}
case USB2SnesWS::Rename :
case USB2SnesWS::Remove :
case USB2SnesWS::MakeDir :
case USB2SnesWS::PutFile :
case USB2SnesWS::PutAddress :
case USB2SnesWS::Menu :
case USB2SnesWS::Reset :
case USB2SnesWS::Boot :
{
break;
}
case USB2SnesWS::GetAddress :
{
disconnect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)));
//disconnect(device, SIGNAL(sizeGet(uint)), this, SLOT(onDeviceSizeGet(uint)));
break;
}
default:
{
sDebug() << "Error, command not found";
return;
}
}
currentRequests[device]->state = RequestState::DONE;
sDebug() << "Request " << *(currentRequests.value(device)) << "processed in " << currentRequests.value(device)->timeCreated.msecsTo(QTime::currentTime()) << " ms";
delete currentRequests[device];
currentRequests[device] = nullptr;
}
/*
* Attach stuff
*/
QStringList WSServer::getDevicesList()
{
QStringList toret;
sDebug() << "Device List";
foreach (DeviceFactory* devFact, deviceFactories)
{
toret.append(devFact->listDevices());
}
return toret;
}
void WSServer::cmdAttach(MRequest *req)
{
QString deviceToAttach = req->arguments.at(0);
wsInfos[req->owner].pendingAttach = true;
ADevice* devGet = nullptr;
foreach (DeviceFactory* devFact, deviceFactories)
{
sDebug() << devFact->name();
devGet = devFact->attach(deviceToAttach);
if (devGet != nullptr)
{
mapDevFact[devGet] = devFact;
break;
}
if (!devFact->attachError().isEmpty())
{
setError(ErrorType::CommandError, "Attach Error with " + deviceToAttach + " - " + devFact->attachError());
clientError(req->owner);
return ;
}
}
if (devGet != nullptr)
{
sDebug() << "Found device" << devGet->name() << "from" << mapDevFact[devGet]->name() << "State : " << devGet->state();
sDebug() << "Attaching " << wsInfos.value(req->owner).name << " to " << deviceToAttach;
if (devGet->state() == ADevice::State::CLOSED)
{
sDebug() << "Trying to open device";
if (!devGet->open())
{
setError(ErrorType::CommandError, "Attach: Can't open the device on " + deviceToAttach);
clientError(req->owner);
return;
}
}
if (!devices.contains(devGet))
addDevice(devGet);
wsInfos[req->owner].attached = true;
wsInfos[req->owner].attachedTo = devGet;
wsInfos[req->owner].pendingAttach = false;
processCommandQueue(devGet);
return ;
} else {
setError(ErrorType::CommandError, "Trying to Attach to an unknow device");
clientError(req->owner);
return ;
}
}
void WSServer::processIpsData(QWebSocket* ws)
{
sDebug() << "processing IPS data";
WSInfos& infos = wsInfos[ws];
MRequest* ipsReq = currentRequests[infos.attachedTo];
QList<IPSReccord> ipsReccords = parseIPSData(infos.ipsData);
// Creating new PutAddress requests
unsigned int cpt = 0;
sDebug() << "Creating " << ipsReccords.size() << "PutRequests";
foreach(IPSReccord ipsr, ipsReccords)
{
MRequest* newReq = new MRequest();
newReq->owner = ws;
newReq->state = RequestState::NEW;
newReq->space = SD2Snes::space::SNES;
newReq->timeCreated = QTime::currentTime();
newReq->wasPending = false;
newReq->opcode = USB2SnesWS::PutAddress;
// Special stuff for patch that install stuff in the NMI of sd2snes
if (cpt == 0 && ipsReq->arguments.at(0) == "hook")
newReq->flags << "CLRX";
if (cpt == (ipsReccords.size() - 1) && ipsReq->arguments.at(0) == "hook")
newReq->flags << "SETX";
newReq->arguments << QString::number(ipsr.offset, 16) << QString::number(ipsr.size, 16);
pendingRequests[infos.attachedTo].append(newReq);
infos.pendingPutDatas.append(ipsr.data);
//sDebug() << "IPS:" << QString::number(ipsr.offset, 16) << ipsr.data.toHex();
cpt++;
}
infos.ipsData.clear();
processCommandQueue(infos.attachedTo);
}