Skip to content

Commit

Permalink
multi get supported (with creating multiple request)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarsnik committed Jan 14, 2019
1 parent 6f3bc6d commit f238833
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 23 deletions.
22 changes: 21 additions & 1 deletion QUsb2snes.qbs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,27 @@ Project {
name : "Qt";
submodules : ["gui", "core", "widgets", "network", "serialport", "websockets"]
}
}/*
}

QtApplication {
name : "TestQUsb2Snes"
cpp.cxxLanguageVersion: "c++11"
files: [
"testmain.cpp",
"client/usb2snes.h",
"client/usb2snes.cpp",
]

Group { // Properties for the produced executable
fileTagsFilter: "application"
qbs.install: true
}
Depends {
name : "Qt";
submodules : ["core", "network", "websockets"]
}
}
/*
Product {
name : "deploy"
Depends {
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int main(int ac, char *ag[])
if (mlog.open(QIODevice::WriteOnly | QIODevice::Text))
qInstallMessageHandler(myMessageOutput);
QApplication::setApplicationName("QUsb2Snes");
QApplication::setApplicationVersion("0.4");
QApplication::setApplicationVersion("0.5");
if (app.arguments().size() == 2 && app.arguments().at(1) == "-nogui")
{
QTimer::singleShot(100, &startServer);
Expand Down
33 changes: 33 additions & 0 deletions testmain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <QDebug>

#include "client/usb2snes.h"


USB2snes* usb;


void runThing()
{
qDebug() << "Runnning thing";
usb->getAddress(0xF50500, 2000);

qApp->exit(0);
}

void stateChanged()
{
qDebug() << "State Changed";
if (usb->state() == USB2snes::Ready)
runThing();
}

int main(int ac, char* ag[])
{
QCoreApplication app(ac, ag);

usb = new USB2snes(true);
usb->connect();
QObject::connect(usb, &USB2snes::stateChanged, stateChanged);
//usb->getAddress()
app.exec();
}
34 changes: 27 additions & 7 deletions usbconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ USBConnection::USBConnection(QString portName)
m_getSize = 0;
fileGetCmd = false;
bytesReceived = 0;
blockSize = 512;
}

bool USBConnection::open()
Expand Down Expand Up @@ -86,7 +87,8 @@ void USBConnection::spReadyRead()
}

dataReceived.append(dataRead);
if ((m_currentCommand == SD2Snes::opcode::GET || m_currentCommand == SD2Snes::opcode::VGET)
// FIXME maybe for 64B mode get?
if ((m_currentCommand == SD2Snes::opcode::GET)
&& m_getSize <= 0)
{
m_getSize = 0;
Expand All @@ -108,18 +110,25 @@ void USBConnection::spReadyRead()
} else {
sDebug() << "Unsized command" << m_currentCommand;
if ((this->*checkCommandEnd)()) {
if ((m_currentCommand == SD2Snes::opcode::GET || m_currentCommand == SD2Snes::opcode::VGET))
if (m_currentCommand == SD2Snes::opcode::GET)
{
if (dataRead.size() == dataReceived.size())
emit getDataReceived(dataRead.mid(512, m_getSize));
else
emit getDataReceived(dataRead.left(bytesReceived - m_getSize));
}
if (m_currentCommand == SD2Snes::opcode::VGET)
{
if (dataRead.size() == dataReceived.size())
emit getDataReceived(dataRead);
else
emit getDataReceived(dataRead.left(m_getSize % 64));
}
goto LcmdFinished;
} else {
if ((m_currentCommand == SD2Snes::opcode::GET || m_currentCommand == SD2Snes::opcode::VGET))
{
if (firstBlock)
if (firstBlock && (m_commandFlags & SD2Snes::server_flags::NORESP) == 0)
emit getDataReceived(dataRead.mid(512));
else
emit getDataReceived(dataRead);
Expand Down Expand Up @@ -191,15 +200,19 @@ bool USBConnection::checkEndForLs()
bool USBConnection::checkEndForGet()
{
quint64 cmp_size = m_getSize;
if (m_getSize % 512 != 0)
cmp_size = (m_getSize / 512) * 512 + 512;
if (m_getSize % blockSize != 0)
cmp_size = (m_getSize / blockSize) * blockSize + blockSize;
sDebug() << cmp_size;
return bytesReceived == cmp_size + 512;
if (m_commandFlags & SD2Snes::server_flags::NORESP)
return bytesReceived == cmp_size;
else
return bytesReceived == cmp_size + blockSize;
}

void USBConnection::sendCommand(SD2Snes::opcode opcode, SD2Snes::space space, unsigned char flags, const QByteArray& arg, const QByteArray arg2 = QByteArray())
{
unsigned int filer_size = 512 - 7;
blockSize = 512;
m_commandFlags = flags;
sDebug() << "CMD : " << opcode << space << flags << arg;
QByteArray data("USBA");
Expand All @@ -221,7 +234,10 @@ void USBConnection::sendCommand(SD2Snes::opcode opcode, SD2Snes::space space,
void USBConnection::sendVCommand(SD2Snes::opcode opcode, SD2Snes::space space, unsigned char flags,
const QList<QPair<unsigned int, quint8> >& args)
{
unsigned int filer_size = 512 - 7;
unsigned int filer_size = 64 - 7;
// SD2Snes expect this flags for vget and vput
flags |= SD2Snes::server_flags::DATA64B | SD2Snes::server_flags::NORESP;
blockSize = 64;
m_commandFlags = flags;
sDebug() << "CMD : " << opcode << space << flags << args;
QByteArray data("USBA");
Expand All @@ -230,14 +246,18 @@ void USBConnection::sendVCommand(SD2Snes::opcode opcode, SD2Snes::space space
data.append((char) flags);
data.append(QByteArray().fill(0, filer_size));
unsigned int i = 0;
unsigned int tsize = 0;
foreach (auto infos, args) {
data[32 + i * 4] = infos.second;
data[33 + i * 4] = (infos.first >> 16) & 0xFF;
data[34 + i * 4] = (infos.first >> 8) & 0xFF;
data[35 + i * 4] = infos.first & 0xFF;
i++;
tsize += infos.second;
}
sDebug() << "VCMD Sending : " << data;
if (opcode == SD2Snes::opcode::VGET)
m_getSize = tsize;
m_state = BUSY;
m_currentCommand = opcode;
writeData(data);
Expand Down
14 changes: 8 additions & 6 deletions usbconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ private slots:
QSerialPort m_port;

QByteArray dataReceived;
int responseSizeExpected;
int bytesReceived;
bool fileGetCmd;
bool isGetCmd;
int responseSizeExpected;
int bytesReceived;
bool fileGetCmd;
bool isGetCmd;
quint16 blockSize;

SD2Snes::opcode m_currentCommand;
unsigned char m_commandFlags;
quint64 m_getSize;
unsigned char m_commandFlags;
qint64 m_getSize;


bool (USBConnection::*checkCommandEnd)();
Expand Down
2 changes: 1 addition & 1 deletion wsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void WSServer::onDeviceGetDataReceived(QByteArray data)
devicesInfos[device].currentWS->sendBinaryMessage(data);
}

// FIXME Why this exist?
// Used for Get File
void WSServer::onDeviceSizeGet(unsigned int size)
{
ADevice* device = qobject_cast<ADevice*>(sender());
Expand Down
30 changes: 23 additions & 7 deletions wsservercommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ void WSServer::executeRequest(MRequest *req)
}
case USB2SnesWS::GetFile : {
CMD_TAKE_ONE_ARG("GetFile")
connect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)));
connect(device, SIGNAL(sizeGet(uint)), this, SLOT(onDeviceSizeGet(uint)));
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;
Expand Down Expand Up @@ -147,19 +147,35 @@ void WSServer::executeRequest(MRequest *req)
clientError(ws);
return ;
}
connect(device, SIGNAL(getDataReceived(QByteArray)), this, SLOT(onDeviceGetDataReceived(QByteArray)));
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, quint8> > pairs;

QList<QPair<unsigned int, unsigned int> > 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)));
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);
}
device->getAddrCommand(req->space, pairs);
}
req->state = RequestState::WAITINGREPLY;
break;
Expand Down Expand Up @@ -390,7 +406,7 @@ void WSServer::processIpsData(QWebSocket* ws)
newReq->state = RequestState::NEW;
newReq->space = SD2Snes::space::SNES;
newReq->timeCreated = QTime::currentTime();
newReq->wasPending = false;
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")
Expand Down

0 comments on commit f238833

Please sign in to comment.