summaryrefslogtreecommitdiff
path: root/tests/manual/trk/runner.cpp
blob: e1280ffc769f5e3e210747b919ebe4f971dc638d (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**************************************************************************
**
** 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://qt.nokia.com/contact.
**
**************************************************************************/

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QHash>
#include <QtCore/QProcess>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
#include <QtCore/QTimer>

#include <QtGui/QApplication>
#include <QtGui/QTextEdit>

#include <unistd.h>

///////////////////////////////////////////////////////////////////////
//
// Runner
//
///////////////////////////////////////////////////////////////////////

class Runner : public QObject
{
    Q_OBJECT

public:
    Runner();
    void parseArguments(const QStringList &args);

signals:
    void output(const QString &senderName, const QString &data);

private slots:
    void handleProcError(QProcess::ProcessError error);
    void handleProcFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void handleProcReadyReadStandardError();
    void handleProcReadyReadStandardOutput();
    void handleProcStarted();
    void handleProcStateChanged(QProcess::ProcessState newState);
    void run();
    void runRfcomm();

private:
    friend class RunnerGui;
    void launchAdapter();
    void launchTrkServer();
    void writeGdbInit();
    void connectProcess(QProcess *proc);
    void sendOutput(QObject *sender, const QString &data);
    void sendOutput(const QString &data) { sendOutput(0, data); }

    QStringList m_adapterOptions;
    QStringList m_trkServerOptions;
    QString m_endianness;
    QString m_trkServerName;
    bool m_runTrkServer;
    bool m_isUnix;
    bool m_waitForAdapter;
    QString m_gdbServerIP;
    QString m_gdbServerPort;

    QProcess m_adapterProc;
    QProcess m_trkServerProc;
    QProcess m_debuggerProc;
};

Runner::Runner()
{
#ifdef Q_OS_UNIX
    m_isUnix = true;
#else
    m_isUnix = false;
#endif
    m_endianness = "little";
    m_runTrkServer = true;
    m_waitForAdapter = false;

    uid_t userId = getuid();
    m_gdbServerIP = "127.0.0.1";
    m_gdbServerPort = QString::number(2222 + userId);

    m_trkServerProc.setObjectName("TRKSERVER PROCESS");
    m_adapterProc.setObjectName("ADAPTER PROCESS");
    m_debuggerProc.setObjectName("GDB PROCESS");

    connectProcess(&m_trkServerProc);
    connectProcess(&m_adapterProc);
    connectProcess(&m_debuggerProc);
}

void Runner::connectProcess(QProcess *proc)
{
    connect(proc, SIGNAL(error(QProcess::ProcessError)),
        this, SLOT(handleProcError(QProcess::ProcessError)));
    connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)),
        this, SLOT(handleProcFinished(int, QProcess::ExitStatus)));
    connect(proc, SIGNAL(readyReadStandardError()),
        this, SLOT(handleProcReadyReadStandardError()));
    connect(proc, SIGNAL(readyReadStandardOutput()),
        this, SLOT(handleProcReadyReadStandardOutput()));
    connect(proc, SIGNAL(started()),
        this, SLOT(handleProcStarted()));
    connect(proc, SIGNAL(stateChanged(QProcess::ProcessState)),
        this, SLOT(handleProcStateChanged(QProcess::ProcessState)));
}


void Runner::sendOutput(QObject *sender, const QString &data)
{
    if (sender)
        emit output(sender->objectName(), data);
    else
        emit output(QString(), data);
}

void Runner::handleProcError(QProcess::ProcessError error)
{
    sendOutput(sender(), QString("Process Error %1").arg(error));
}

void Runner::handleProcFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    sendOutput(sender(),
        QString("ProcessFinished %1 %2").arg(exitCode).arg(exitStatus));
}

void Runner::handleProcReadyReadStandardError()
{
    QByteArray ba = qobject_cast<QProcess *>(sender())->readAllStandardError();
    sendOutput(sender(), QString("stderr: %1").arg(QString::fromLatin1(ba)));
}

void Runner::handleProcReadyReadStandardOutput()
{
    QByteArray ba = qobject_cast<QProcess *>(sender())->readAllStandardOutput();
    sendOutput(sender(), QString("stdout: %1").arg(QString::fromLatin1(ba)));
}

void Runner::handleProcStarted()
{
    sendOutput(sender(), QString("Process Started"));
}

void Runner::handleProcStateChanged(QProcess::ProcessState newState)
{
    sendOutput(sender(), QString("Process State %1").arg(newState));
}


static QString usage()
{
   return QString("Usage: %1 -w -av -aq -au -tv -tq -l [COM]\n\n"
    "Options:\n"
    "     -av     Adapter verbose\n"
    "     -aq     Adapter quiet\n"
    "     -au     Adapter turn off buffered memory read\n"
    "     -af     Adapter turn off serial frame\n"
    "     -w      Wait for termination of Adapter (Bluetooth)\n"
    "     -tv     TrkServer verbose\n"
    "     -tq     TrkServer quiet\n"
    "\n"
    "     trkserver simulator will be run unless COM is specified\n"
    "\n"
    "Bluetooth:\n"
    "     rfcomm listen /dev/rfcomm0 1 $PWD/run.pl -av -af -w {}\n")
        .arg(QCoreApplication::arguments().at(0));
}

void Runner::parseArguments(const QStringList &args)
{
    for (int i = 1; i < args.size(); ++i) {
        const QString arg = args.at(i);
        if (arg.startsWith('-')) {
            if (arg == "-av") {
                m_adapterOptions.append("-v");
            } else if (arg == "-aq") {
                m_adapterOptions.append("-q");
            } else if (arg == "-af") {
                m_adapterOptions.append("-f");
            } else if (arg == "-au") {
                m_adapterOptions.append("-u");
            } else if (arg == "-w") {
                m_waitForAdapter = true;
            } else if (arg == "-tv") {
                m_trkServerOptions.append("-v");
            } else if (arg == "-tq") {
                m_trkServerOptions.append("-q");
            } else if (arg == "-h") {
                qDebug() << usage();
                qApp->exit(0);
            } else {
                qDebug() << usage();
                qApp->exit(1);
            }
        } else {
            m_trkServerName = arg;
            m_runTrkServer = false;
        }
    }
}

void Runner::launchTrkServer()
{
    sendOutput("Launching Trk Server");
    const QString dumpPostfix = ".bin";

    QString trkServerName;
    if (m_isUnix)
        trkServerName = QDir::currentPath() + "/trkserver";
    else
	trkServerName = "cmd.exe";

    QStringList trkServerArgs;
    if (!m_isUnix) {
	trkServerArgs << "/c" << "start"
            << QDir::currentPath() + "/debug/trkserver.exe";
    }

    trkServerArgs << m_trkServerOptions;
    trkServerArgs << m_trkServerName;
    trkServerArgs << "TrkDump-78-6a-40-00" + dumpPostfix;
    trkServerArgs << "0x00402000" + dumpPostfix;
    trkServerArgs << "0x786a4000" + dumpPostfix;
    trkServerArgs << "0x00600000" + dumpPostfix;

    sendOutput("### Starting " + trkServerName + " " + trkServerArgs.join(" "));
    m_trkServerProc.start(trkServerName, trkServerArgs);
    m_trkServerProc.waitForStarted();
}

void Runner::launchAdapter()
{
    QString adapterName;
    if (m_isUnix)
        adapterName = QDir::currentPath() + "/adapter";
    else
	adapterName = "cmd.exe";

    QStringList adapterArgs;
    if (!m_isUnix) {
	adapterArgs << "/c" << "start"
            << QDir::currentPath() + "/debug/adapter.exe";
    }
    adapterArgs << m_adapterOptions;
    //adapterArgs << "-s";
    adapterArgs << m_trkServerName << m_gdbServerIP + ':' + m_gdbServerPort;

    sendOutput("### Starting " + adapterName + " " + adapterArgs.join(" "));

    m_adapterProc.start(adapterName, adapterArgs);
    m_adapterProc.waitForStarted();
    //die ('Unable to launch adapter') if $adapterpid == -1;

/*
    if (m_waitForAdapter) {
        print '### kill -USR1 ',$adapterpid,"\n";    
        waitpid($adapterpid, 0);
    }    
*/
}

void Runner::writeGdbInit()
{
    qDebug() << "WRITE GDBINIT";
    QString gdbInitFile = QDir::currentPath() + "/.gdbinit";
    QFile file(gdbInitFile);
    if (!file.open(QIODevice::ReadWrite)) {
        sendOutput(QString("Cannot open %1 for writing").arg(gdbInitFile));
        return;
    }
    QTextStream ts(&file);
    ts << "# This is generated. Changes will be lost.";
    ts << "#set remote noack-packet on";
    ts << "set confirm off";
    ts << "set endian " + m_endianness;
    ts << "#set debug remote 1";
    ts << "#target remote $m_gdbServerIP:$m_gdbServerPort";
    ts << "target extended-remote $m_gdbServerIP:$m_gdbServerPort";
    ts << "#file filebrowseapp.sym";
    ts << "add-symbol-file filebrowseapp.sym 0x786A4000";
    ts << "symbol-file filebrowseapp.sym";
    ts << "print E32Main";
    ts << "break E32Main";
    ts << "#continue";
    ts << "#info files";
    ts << "#file filebrowseapp.sym -readnow";
    ts << "#add-symbol-file filebrowseapp.sym 0x786A4000";
}

void Runner::run()
{
    if (m_isUnix) {
        QProcess::execute("killall -s USR adapter trkserver");
        QProcess::execute("killall adapter trkserver");
    }

    launchAdapter();

    uid_t userId = getuid();
    if (m_trkServerName.isEmpty())
        m_trkServerName = QString("TRKSERVER-%1").arg(userId);

    if (m_isUnix) {
        QProcess fuserProc;
        fuserProc.start("fuser -n tcp -k " + m_gdbServerPort);
        fuserProc.waitForStarted();
        fuserProc.closeWriteChannel();
        fuserProc.waitForFinished();
        qDebug() << "fuser: " << fuserProc.readAllStandardOutput();
        qDebug() << "fuser: " << fuserProc.readAllStandardError();
    }

    // Who writes that?
    //my $tempFile = File::Spec->catfile(File::Temp::tempdir(),
    //m_trkServerName);
    //unlink ($tempFile) if  -f $tempFile;

    if (m_runTrkServer)
        launchTrkServer();
    
    writeGdbInit();
}

void Runner::runRfcomm()
{
    qDebug() << "RUNNING RFCCOMM";
    QProcess *proc = new QProcess;
    connectProcess(proc);
    proc->start("rfcomm listen /dev/rfcomm0 1");
    proc->waitForStarted();
    qDebug() << "STARTED";
    proc->write("\x90\x01\x00\x05\x7e\x00\x00\xff\x7e");
    qDebug() << "WRITTEN";
    proc->waitForFinished();
}

///////////////////////////////////////////////////////////////////////
//
// RunnerGui
//
///////////////////////////////////////////////////////////////////////

class RunnerGui : public QTextEdit
{
    Q_OBJECT

public:
    RunnerGui(Runner *runner);

private slots:
    void handleOutput(const QString &senderName, const QString &data);

private:
    Runner *m_runner;
};

RunnerGui::RunnerGui(Runner *runner)
    : m_runner(runner) 
{
    resize(600, 800);
    connect(runner, SIGNAL(output(QString,QString)),
        this, SLOT(handleOutput(QString,QString)));
}

void RunnerGui::handleOutput(const QString &senderName, const QString &data)
{
    append(senderName + " : " + data);
}

///////////////////////////////////////////////////////////////////////
//
// main
//
///////////////////////////////////////////////////////////////////////

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
#if 1
    QStringList args = QCoreApplication::arguments();
    qDebug() << "RUNNER ARGS: " << args;
#else
    // Important options: -w wait for adapter, -af omit serial frame.
    QStringList args = QStringList() << "-w" << "-af" << "COM5";
#endif
    Runner runner;
    runner.parseArguments(args);
    RunnerGui gui(&runner);
    gui.show();
    QTimer::singleShot(0, &runner, SLOT(run()));
    //QTimer::singleShot(0, &runner, SLOT(runRfcomm()));
    return app.exec();
}

#include "runner.moc"