2009-07-15 14:21:26 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://www.qtsoftware.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
#include "trkutils.h"
|
|
|
|
|
|
2009-07-15 14:21:26 +02:00
|
|
|
#include <QtCore/QCoreApplication>
|
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
|
|
|
|
|
|
#include <QtNetwork/QLocalServer>
|
|
|
|
|
#include <QtNetwork/QLocalSocket>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
|
void signalHandler(int)
|
|
|
|
|
{
|
|
|
|
|
qApp->exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
using namespace trk;
|
|
|
|
|
|
|
|
|
|
// Format of the replay source is something like
|
|
|
|
|
// ---IDE------------------------------------------------------
|
|
|
|
|
// Command: 0x05 Support Mask
|
|
|
|
|
// [05 02]
|
|
|
|
|
//---TRK------------------------------------------------------
|
|
|
|
|
// Command: 0x80 Acknowledge
|
|
|
|
|
// Error: 0x00
|
|
|
|
|
// [80 02 00 7E 00 4F 5F 01 00 00 00 0F 1F 00 00 00
|
|
|
|
|
// 00 00 00 01 00 11 00 03 00 00 00 00 00 03 00 00...]
|
2009-07-15 14:21:26 +02:00
|
|
|
|
|
|
|
|
class TrkServer : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2009-07-16 10:13:04 +02:00
|
|
|
TrkServer();
|
2009-07-15 14:21:26 +02:00
|
|
|
~TrkServer();
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void setServerName(const QString &name) { m_serverName = name; }
|
|
|
|
|
void setReplaySource(const QString &source) { m_replaySource = source; }
|
|
|
|
|
void startServer();
|
|
|
|
|
|
2009-07-15 14:21:26 +02:00
|
|
|
private slots:
|
|
|
|
|
void handleConnection();
|
2009-07-16 10:13:04 +02:00
|
|
|
void readFromAdapter();
|
|
|
|
|
void writeToAdapter(byte command, byte token, const QByteArray &data);
|
|
|
|
|
|
|
|
|
|
void handleAdapterMessage(const TrkResult &result);
|
2009-07-15 14:21:26 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void logMessage(const QString &msg);
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
QString m_serverName;
|
|
|
|
|
QString m_replaySource;
|
|
|
|
|
|
|
|
|
|
QByteArray m_adapterReadBuffer;
|
|
|
|
|
|
|
|
|
|
QList<QByteArray> m_replayData;
|
|
|
|
|
QLocalServer m_server;
|
2009-07-15 14:21:26 +02:00
|
|
|
int m_lastSent;
|
2009-07-16 10:13:04 +02:00
|
|
|
QLocalSocket *m_adapterConnection;
|
2009-07-15 14:21:26 +02:00
|
|
|
};
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
TrkServer::TrkServer()
|
2009-07-15 14:21:26 +02:00
|
|
|
{
|
2009-07-16 10:13:04 +02:00
|
|
|
m_adapterConnection = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TrkServer::~TrkServer()
|
|
|
|
|
{
|
|
|
|
|
logMessage("Shutting down.\n");
|
|
|
|
|
m_server.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TrkServer::startServer()
|
|
|
|
|
{
|
|
|
|
|
QFile file(m_replaySource);
|
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
m_replayData = file.readAll().split('\n');
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
logMessage(QString("Read %1 lines of data from %2")
|
|
|
|
|
.arg(m_replayData.size()).arg(m_replaySource));
|
|
|
|
|
|
2009-07-15 14:21:26 +02:00
|
|
|
m_lastSent = 0;
|
2009-07-16 10:13:04 +02:00
|
|
|
if (!m_server.listen(m_serverName)) {
|
2009-07-15 14:21:26 +02:00
|
|
|
logMessage(QString("Error: Unable to start the TRK server %1: %2.")
|
2009-07-16 10:13:04 +02:00
|
|
|
.arg(m_serverName).arg(m_server.errorString()));
|
2009-07-15 14:21:26 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logMessage("The TRK server is running. Run the adapter now.");
|
2009-07-16 10:13:04 +02:00
|
|
|
connect(&m_server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
|
2009-07-15 14:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void TrkServer::logMessage(const QString &msg)
|
2009-07-15 14:21:26 +02:00
|
|
|
{
|
2009-07-16 10:13:04 +02:00
|
|
|
qDebug() << "TRKSERVER: " << qPrintable(msg);
|
2009-07-15 14:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void TrkServer::handleConnection()
|
2009-07-15 14:21:26 +02:00
|
|
|
{
|
2009-07-16 10:13:04 +02:00
|
|
|
//QByteArray block;
|
|
|
|
|
|
|
|
|
|
//QByteArray msg = m_replayData[m_lastSent ++];
|
|
|
|
|
|
|
|
|
|
//QDataStream out(&block, QIODevice::WriteOnly);
|
|
|
|
|
//out.setVersion(QDataStream::Qt_4_0);
|
|
|
|
|
//out << (quint16)0;
|
|
|
|
|
//out << m_replayData;
|
|
|
|
|
//out.device()->seek(0);
|
|
|
|
|
//out << (quint16)(block.size() - sizeof(quint16));
|
|
|
|
|
|
|
|
|
|
m_adapterConnection = m_server.nextPendingConnection();
|
|
|
|
|
connect(m_adapterConnection, SIGNAL(disconnected()),
|
|
|
|
|
m_adapterConnection, SLOT(deleteLater()));
|
|
|
|
|
connect(m_adapterConnection, SIGNAL(readyRead()),
|
|
|
|
|
this, SLOT(readFromAdapter()));
|
|
|
|
|
|
|
|
|
|
//m_adapterConnection->write(block);
|
|
|
|
|
//m_adapterConnection->flush();
|
|
|
|
|
//m_adapterConnection->disconnectFromHost();
|
2009-07-15 14:21:26 +02:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void TrkServer::readFromAdapter()
|
2009-07-15 14:21:26 +02:00
|
|
|
{
|
2009-07-16 10:13:04 +02:00
|
|
|
QByteArray packet = m_adapterConnection->readAll();
|
|
|
|
|
m_adapterReadBuffer.append(packet);
|
|
|
|
|
|
|
|
|
|
logMessage("trk: -> " + stringFromArray(packet));
|
|
|
|
|
|
|
|
|
|
if (packet != m_adapterReadBuffer)
|
|
|
|
|
logMessage("buffer: " + stringFromArray(m_adapterReadBuffer));
|
2009-07-15 14:21:26 +02:00
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
while (!m_adapterReadBuffer.isEmpty())
|
|
|
|
|
handleAdapterMessage(extractResult(&m_adapterReadBuffer));
|
|
|
|
|
}
|
2009-07-15 14:21:26 +02:00
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void TrkServer::writeToAdapter(byte command, byte token, const QByteArray &data)
|
|
|
|
|
{
|
|
|
|
|
QByteArray msg = frameMessage(command, token, data);
|
|
|
|
|
logMessage("trk: <- " + stringFromArray(msg));
|
|
|
|
|
m_adapterConnection->write(msg);
|
|
|
|
|
}
|
2009-07-15 14:21:26 +02:00
|
|
|
|
2009-07-16 10:13:04 +02:00
|
|
|
void TrkServer::handleAdapterMessage(const TrkResult &result)
|
|
|
|
|
{
|
|
|
|
|
QByteArray data;
|
2009-07-16 16:05:43 +02:00
|
|
|
data.append(char(0x00)); // No error
|
2009-07-16 10:13:04 +02:00
|
|
|
switch (result.code) {
|
|
|
|
|
case 0x00: { // Ping
|
|
|
|
|
writeToAdapter(0x80, 0x00, data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-07-16 16:05:43 +02:00
|
|
|
case 0x01: { // Connect
|
|
|
|
|
writeToAdapter(0x80, result.token, data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-07-16 10:13:04 +02:00
|
|
|
default:
|
2009-07-16 16:05:43 +02:00
|
|
|
data[0] = 0x10; // Command not supported
|
2009-07-16 10:13:04 +02:00
|
|
|
writeToAdapter(0xff, result.token, data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-07-15 14:21:26 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
2009-07-16 10:13:04 +02:00
|
|
|
if (argc < 3) {
|
|
|
|
|
qDebug() << "Usage: " << argv[0] << " <trkservername>"
|
|
|
|
|
<< " <replaysource>";
|
2009-07-15 14:21:26 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
|
signal(SIGUSR1, signalHandler);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
QCoreApplication app(argc, argv);
|
2009-07-16 10:13:04 +02:00
|
|
|
|
|
|
|
|
TrkServer server;
|
|
|
|
|
server.setServerName(argv[1]);
|
|
|
|
|
server.setReplaySource(argv[2]);
|
|
|
|
|
server.startServer();
|
|
|
|
|
|
2009-07-15 14:21:26 +02:00
|
|
|
return app.exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include "trkserver.moc"
|