1
/****************************************************************************
2
**
3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the tools applications of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include <QCoreApplication>
43
#include <QTextStream>
44
#include <QStringList>
45
#include <QScopedPointer>
46
#include <QTimer>
47
#include <QFileInfo>
48
#include "symbianutils/trkutils.h"
49
#include "symbianutils/trkdevice.h"
50
#include "symbianutils/launcher.h"
51
52
#include "trksignalhandler.h"
53
#include "serenum.h"
54
#include "ossignalconverter.h"
55
56
void printUsage(QTextStream& outstream, QString exeName)
57
{
58
    outstream << exeName << " [options] [program] [program arguments]" << endl
59
            << "-s, --sis <file>                         specify sis file to install" << endl
60
            << "-p, --portname <COMx>                    specify COM port to use by device name" << endl
61
            << "-f, --portfriendlyname <substring>       specify COM port to use by friendly name" << endl
62
            << "-t, --timeout <milliseconds>             terminate test if timeout occurs" << endl
63
            << "-v, --verbose                            show debugging output" << endl
64
            << "-q, --quiet                              hide progress messages" << endl
65
            << "-d, --download <remote file> <local file> copy file from phone to PC after running test" << endl
66
            << "--nocrashlog                             Don't capture call stack if test crashes" << endl
67
            << "--crashlogpath <dir>                     Path to save crash logs (default=working dir)" << endl
68
            << endl
69
            << "USB COM ports can usually be autodetected, use -p or -f to force a specific port." << endl
70
            << "If using System TRK, it is possible to copy the program directly to sys/bin on the phone." << endl
71
            << "-s can be used with both System and Application TRK to install the program" << endl;
72
}
73
74
#define CHECK_PARAMETER_EXISTS if(!it.hasNext()) { printUsage(outstream, args[0]); return 1; }
75
int main(int argc, char *argv[])
76
{
77
    QCoreApplication a(argc, argv);
78
79
    QString serialPortName;
80
    QString serialPortFriendlyName;
81
    QString sisFile;
82
    QString exeFile;
83
    QStringList cmdLine;
84
    QStringList args = QCoreApplication::arguments();
85
    QTextStream outstream(stdout);
86
    QTextStream errstream(stderr);
87
    QString downloadRemoteFile;
88
    QString downloadLocalFile;
89
    int loglevel=1;
90
    int timeout=0;
91
    bool crashlog = true;
92
    QString crashlogpath;
93
    QListIterator<QString> it(args);
94
    it.next(); //skip name of program
95
    while (it.hasNext()) {
96
        QString arg = it.next();
97
98
        if (arg.startsWith("-")) {
99
            if (arg == "--portname" || arg == "-p") {
100
                CHECK_PARAMETER_EXISTS
101
                serialPortName = it.next();
102
            }
103
            else if (arg == "--portfriendlyname" || arg == "-f") {
104
                CHECK_PARAMETER_EXISTS
105
                serialPortFriendlyName = it.next();
106
            }
107
            else if (arg == "--sis" || arg == "-s") {
108
                CHECK_PARAMETER_EXISTS
109
                sisFile = it.next();
110
                if (!QFileInfo(sisFile).exists()) {
111
                    errstream << "Sis file (" << sisFile << ") doesn't exist" << endl;
112
                    return 1;
113
                }
114
            }
115
            else if (arg == "--download" || arg == "-d") {
116
                CHECK_PARAMETER_EXISTS
117
                downloadRemoteFile = it.next();
118
                CHECK_PARAMETER_EXISTS
119
                downloadLocalFile = it.next();
120
            }
121
            else if (arg == "--timeout" || arg == "-t") {
122
                CHECK_PARAMETER_EXISTS
123
                bool ok;
124
                timeout = it.next().toInt(&ok);
125
                if (!ok) {
126
                    errstream << "Timeout must be specified in milliseconds" << endl;
127
                    return 1;
128
                }
129
            }
130
            else if (arg == "--verbose" || arg == "-v")
131
                loglevel=2;
132
            else if (arg == "--quiet" || arg == "-q")
133
                loglevel=0;
134
            else if (arg == "--nocrashlog")
135
                crashlog = false;
136
            else if (arg == "--crashlogpath") {
137
                CHECK_PARAMETER_EXISTS
138
                crashlogpath = it.next();
139
            }
140
            else
141
                errstream << "unknown command line option " << arg << endl;
142
        } else {
143
            exeFile = arg;
144
            while(it.hasNext()) {
145
                cmdLine.append(it.next());
146
            }
147
        }
148
    }
149
150
    if (exeFile.isEmpty() && sisFile.isEmpty() && 
151
        (downloadLocalFile.isEmpty() || downloadRemoteFile.isEmpty())) {
152
        printUsage(outstream, args[0]);
153
        return 1;
154
    }
155
156
    if (serialPortName.isEmpty()) {
157
        if (loglevel > 0)
158
            outstream << "Detecting serial ports" << endl;
159
        foreach (const SerialPortId &id, enumerateSerialPorts(loglevel)) {
160
            if (loglevel > 0)
161
                outstream << "Port Name: " << id.portName << ", "
162
                     << "Friendly Name:" << id.friendlyName << endl;
163
            if (!id.friendlyName.isEmpty()
164
                    && serialPortFriendlyName.isEmpty()
165
                    && (id.friendlyName.contains("symbian", Qt::CaseInsensitive)
166
                        || id.friendlyName.contains("s60", Qt::CaseInsensitive)
167
                        || id.friendlyName.contains("nokia", Qt::CaseInsensitive))) {
168
                serialPortName = id.portName;
169
                break;
170
            } else if (!id.friendlyName.isEmpty()
171
                    && !serialPortFriendlyName.isEmpty()
172
                    && id.friendlyName.contains(serialPortFriendlyName)) {
173
                serialPortName = id.portName;
174
                break;
175
            }
176
        }
177
        if (serialPortName.isEmpty()) {
178
            errstream << "No phone found, ensure USB cable is connected or specify manually with -p" << endl;
179
            return 1;
180
        }
181
    }
182
183
    QScopedPointer<trk::Launcher> launcher;
184
    launcher.reset(new trk::Launcher(trk::Launcher::ActionPingOnly));
185
    QFileInfo info(exeFile);
186
    if (!sisFile.isEmpty()) {
187
        launcher->addStartupActions(trk::Launcher::ActionCopyInstall);
188
        launcher->setCopyFileName(sisFile, "c:\\data\\testtemp.sis");
189
        launcher->setInstallFileName("c:\\data\\testtemp.sis");
190
    }
191
    else if (info.exists()) {
192
        launcher->addStartupActions(trk::Launcher::ActionCopy);
193
        launcher->setCopyFileName(exeFile, QString("c:\\sys\\bin\\") + info.fileName());
194
    }
195
    if (!exeFile.isEmpty()) {
196
        launcher->addStartupActions(trk::Launcher::ActionRun);
197
        launcher->setFileName(QString("c:\\sys\\bin\\") + info.fileName());
198
        launcher->setCommandLineArgs(cmdLine);
199
    }
200
    if (!downloadRemoteFile.isEmpty() && !downloadLocalFile.isEmpty()) {
201
        launcher->addStartupActions(trk::Launcher::ActionDownload);
202
        launcher->setDownloadFileName(downloadRemoteFile, downloadLocalFile);
203
    }
204
    if (loglevel > 0)
205
        outstream << "Connecting to target via " << serialPortName << endl;
206
    launcher->setTrkServerName(serialPortName);
207
208
    if (loglevel > 1)
209
        launcher->setVerbose(1);
210
211
    TrkSignalHandler handler;
212
    handler.setLogLevel(loglevel);
213
    handler.setCrashLogging(crashlog);
214
    handler.setCrashLogPath(crashlogpath);
215
216
    QObject::connect(launcher.data(), SIGNAL(copyingStarted()), &handler, SLOT(copyingStarted()));
217
    QObject::connect(launcher.data(), SIGNAL(canNotConnect(const QString &)), &handler, SLOT(canNotConnect(const QString &)));
218
    QObject::connect(launcher.data(), SIGNAL(canNotCreateFile(const QString &, const QString &)), &handler, SLOT(canNotCreateFile(const QString &, const QString &)));
219
    QObject::connect(launcher.data(), SIGNAL(canNotWriteFile(const QString &, const QString &)), &handler, SLOT(canNotWriteFile(const QString &, const QString &)));
220
    QObject::connect(launcher.data(), SIGNAL(canNotCloseFile(const QString &, const QString &)), &handler, SLOT(canNotCloseFile(const QString &, const QString &)));
221
    QObject::connect(launcher.data(), SIGNAL(installingStarted()), &handler, SLOT(installingStarted()));
222
    QObject::connect(launcher.data(), SIGNAL(canNotInstall(const QString &, const QString &)), &handler, SLOT(canNotInstall(const QString &, const QString &)));
223
    QObject::connect(launcher.data(), SIGNAL(installingFinished()), &handler, SLOT(installingFinished()));
224
    QObject::connect(launcher.data(), SIGNAL(startingApplication()), &handler, SLOT(startingApplication()));
225
    QObject::connect(launcher.data(), SIGNAL(applicationRunning(uint)), &handler, SLOT(applicationRunning(uint)));
226
    QObject::connect(launcher.data(), SIGNAL(canNotRun(const QString &)), &handler, SLOT(canNotRun(const QString &)));
227
    QObject::connect(launcher.data(), SIGNAL(applicationOutputReceived(const QString &)), &handler, SLOT(applicationOutputReceived(const QString &)));
228
    QObject::connect(launcher.data(), SIGNAL(copyProgress(int)), &handler, SLOT(copyProgress(int)));
229
    QObject::connect(launcher.data(), SIGNAL(stateChanged(int)), &handler, SLOT(stateChanged(int)));
230
    QObject::connect(launcher.data(), SIGNAL(processStopped(uint,uint,uint,QString)), &handler, SLOT(stopped(uint,uint,uint,QString)));
231
    QObject::connect(launcher.data(), SIGNAL(libraryLoaded(trk::Library)), &handler, SLOT(libraryLoaded(trk::Library)));
232
    QObject::connect(launcher.data(), SIGNAL(libraryUnloaded(trk::Library)), &handler, SLOT(libraryUnloaded(trk::Library)));
233
    QObject::connect(launcher.data(), SIGNAL(registersAndCallStackReadComplete(const QList<uint> &,const QByteArray &)), &handler, SLOT(registersAndCallStackReadComplete(const QList<uint> &,const QByteArray &)));
234
    QObject::connect(&handler, SIGNAL(resume(uint,uint)), launcher.data(), SLOT(resumeProcess(uint,uint)));
235
    QObject::connect(&handler, SIGNAL(terminate()), launcher.data(), SLOT(terminate()));
236
    QObject::connect(&handler, SIGNAL(getRegistersAndCallStack(uint,uint)), launcher.data(), SLOT(getRegistersAndCallStack(uint,uint)));
237
    QObject::connect(launcher.data(), SIGNAL(finished()), &handler, SLOT(finished()));
238
239
    QObject::connect(OsSignalConverter::instance(), SIGNAL(terminate()), launcher.data(), SLOT(terminate()), Qt::QueuedConnection);
240
241
    QTimer timer;
242
    timer.setSingleShot(true);
243
    QObject::connect(&timer, SIGNAL(timeout()), &handler, SLOT(timeout()));
244
    if (timeout > 0) {
245
        timer.start(timeout);
246
    }
247
248
    QString errorMessage;
249
    if (!launcher->startServer(&errorMessage)) {
250
        errstream << errorMessage << endl;
251
        return 1;
252
    }
253
254
    return a.exec();
255
}