tools/runonphone/main.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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 "symbianutils/trkutils.h"
       
    48 #include "symbianutils/trkdevice.h"
       
    49 #include "symbianutils/launcher.h"
       
    50 
       
    51 #include "trksignalhandler.h"
       
    52 #include "serenum.h"
       
    53 
       
    54 void printUsage(QTextStream& outstream)
       
    55 {
       
    56     outstream << "runtest [options] <program> [program arguments]" << endl
       
    57             << "-s, --sis <file>                     specify sis file to install" << endl
       
    58             << "-p, --portname <COMx>                specify COM port to use by device name" << endl
       
    59             << "-f, --portfriendlyname <substring>   specify COM port to use by friendly name" << endl
       
    60             << "-t, --timeout <milliseconds>         terminate test if timeout occurs" << endl
       
    61             << "-v, --verbose                        show debugging output" << endl
       
    62             << "-q, --quiet                          hide progress messages" << endl
       
    63             << endl
       
    64             << "USB COM ports can usually be autodetected" << endl;
       
    65 }
       
    66 
       
    67 int main(int argc, char *argv[])
       
    68 {
       
    69     QCoreApplication a(argc, argv);
       
    70 
       
    71     QString serialPortName;
       
    72     QString serialPortFriendlyName;
       
    73     QString sisFile;
       
    74     QString exeFile;
       
    75     QStringList cmdLine;
       
    76     QStringList args = QCoreApplication::arguments();
       
    77     QTextStream outstream(stdout);
       
    78     QTextStream errstream(stderr);
       
    79     int loglevel=1;
       
    80     int timeout=0;
       
    81     for (int i=1;i<args.size();i++) {
       
    82         QString arg = args.at(i);
       
    83         if (arg.startsWith("-")) {
       
    84             if (args.size() < i+2) {
       
    85                 errstream << "Command line missing argument parameters" << endl;
       
    86                 return 1;
       
    87             }
       
    88             QString param = args.at(i+1);
       
    89             if(arg.compare("--portname", Qt::CaseSensitive) == 0
       
    90                || arg.compare("-p", Qt::CaseSensitive) == 0) {
       
    91                 serialPortName = param;
       
    92                 i++;
       
    93             }
       
    94             else if(arg.compare("--portfriendlyname", Qt::CaseSensitive) == 0
       
    95                     || arg.compare("-f", Qt::CaseSensitive) == 0) {
       
    96                 serialPortFriendlyName = param;
       
    97                 i++;
       
    98             }
       
    99             else if(arg.compare("--sis", Qt::CaseSensitive) == 0
       
   100                     || arg.compare("-s", Qt::CaseSensitive) == 0) {
       
   101                 sisFile = param;
       
   102                 i++;
       
   103             }
       
   104             else if(arg.compare("--timeout", Qt::CaseSensitive) == 0
       
   105                     || arg.compare("-t", Qt::CaseSensitive) == 0) {
       
   106                 bool ok;
       
   107                 timeout = param.toInt(&ok);
       
   108                 if (!ok) {
       
   109                     errstream << "Timeout must be specified in milliseconds" << endl;
       
   110                     return 1;
       
   111                 }
       
   112                 i++;
       
   113             }
       
   114             else if(arg.compare("--verbose", Qt::CaseSensitive) == 0
       
   115                     || arg.compare("-v", Qt::CaseSensitive) == 0)
       
   116                 loglevel=2;
       
   117             else if(arg.compare("--quiet", Qt::CaseSensitive) == 0
       
   118                     || arg.compare("-q", Qt::CaseSensitive) == 0)
       
   119                 loglevel=0;
       
   120             else
       
   121                 errstream << "unknown command line option " << arg << endl;
       
   122         } else {
       
   123             exeFile = arg;
       
   124             i++;
       
   125             for(;i<args.size();i++) {
       
   126                 cmdLine.append(args.at(i));
       
   127             }
       
   128         }
       
   129     }
       
   130 
       
   131     if(exeFile.isEmpty()) {
       
   132         printUsage(outstream);
       
   133         return 1;
       
   134     }
       
   135 
       
   136     if (serialPortName.isEmpty()) {
       
   137         if (loglevel > 0)
       
   138             outstream << "Detecting serial ports" << endl;
       
   139         QList <SerialPortId> ports = enumerateSerialPorts();
       
   140         foreach(SerialPortId id, ports) {
       
   141             if (loglevel > 0)
       
   142                 outstream << "Port Name: " << id.portName << ", "
       
   143                      << "Friendly Name:" << id.friendlyName << endl;
       
   144             if (serialPortName.isEmpty()) {
       
   145                 if (!id.friendlyName.isEmpty()
       
   146                         && serialPortFriendlyName.isEmpty()
       
   147                         && (id.friendlyName.contains("symbian", Qt::CaseInsensitive)
       
   148                             || id.friendlyName.contains("s60", Qt::CaseInsensitive)
       
   149                             || id.friendlyName.contains("nokia", Qt::CaseInsensitive)))
       
   150                         serialPortName = id.portName;
       
   151                 else if (!id.friendlyName.isEmpty()
       
   152                         && !serialPortFriendlyName.isEmpty()
       
   153                         && id.friendlyName.contains(serialPortFriendlyName))
       
   154                     serialPortName = id.portName;
       
   155             }
       
   156         }
       
   157         if (serialPortName.isEmpty()) {
       
   158             errstream << "No phone found, ensure USB cable is connected or specify manually with -p" << endl;
       
   159             return 1;
       
   160         }
       
   161     }
       
   162 
       
   163     QScopedPointer<trk::Launcher> launcher;
       
   164 
       
   165     if (sisFile.isEmpty()) {
       
   166         launcher.reset(new trk::Launcher(trk::Launcher::ActionCopyRun));
       
   167         launcher->setCopyFileName(exeFile, QString("c:\\sys\\bin\\") + exeFile);
       
   168         errstream << "System TRK required to copy EXE, use --sis if using Application TRK" << endl;
       
   169     } else {
       
   170         launcher.reset(new trk::Launcher(trk::Launcher::ActionCopyInstallRun));
       
   171         launcher->addStartupActions(trk::Launcher::ActionInstall);
       
   172         launcher->setCopyFileName(sisFile, "c:\\data\\testtemp.sis");
       
   173         launcher->setInstallFileName("c:\\data\\testtemp.sis");
       
   174     }
       
   175     if (loglevel > 0)
       
   176         outstream << "Connecting to target via " << serialPortName << endl;
       
   177     launcher->setTrkServerName(serialPortName);
       
   178 
       
   179     launcher->setFileName(QString("c:\\sys\\bin\\") + exeFile);
       
   180     launcher->setCommandLineArgs(cmdLine);
       
   181 
       
   182     if (loglevel > 1)
       
   183         launcher->setVerbose(1);
       
   184 
       
   185     TrkSignalHandler handler;
       
   186     handler.setLogLevel(loglevel);
       
   187 
       
   188     QObject::connect(launcher.data(), SIGNAL(copyingStarted()), &handler, SLOT(copyingStarted()));
       
   189     QObject::connect(launcher.data(), SIGNAL(canNotConnect(const QString &)), &handler, SLOT(canNotConnect(const QString &)));
       
   190     QObject::connect(launcher.data(), SIGNAL(canNotCreateFile(const QString &, const QString &)), &handler, SLOT(canNotCreateFile(const QString &, const QString &)));
       
   191     QObject::connect(launcher.data(), SIGNAL(canNotWriteFile(const QString &, const QString &)), &handler, SLOT(canNotWriteFile(const QString &, const QString &)));
       
   192     QObject::connect(launcher.data(), SIGNAL(canNotCloseFile(const QString &, const QString &)), &handler, SLOT(canNotCloseFile(const QString &, const QString &)));
       
   193     QObject::connect(launcher.data(), SIGNAL(installingStarted()), &handler, SLOT(installingStarted()));
       
   194     QObject::connect(launcher.data(), SIGNAL(canNotInstall(const QString &, const QString &)), &handler, SLOT(canNotInstall(const QString &, const QString &)));
       
   195     QObject::connect(launcher.data(), SIGNAL(installingFinished()), &handler, SLOT(installingFinished()));
       
   196     QObject::connect(launcher.data(), SIGNAL(startingApplication()), &handler, SLOT(startingApplication()));
       
   197     QObject::connect(launcher.data(), SIGNAL(applicationRunning(uint)), &handler, SLOT(applicationRunning(uint)));
       
   198     QObject::connect(launcher.data(), SIGNAL(canNotRun(const QString &)), &handler, SLOT(canNotRun(const QString &)));
       
   199     QObject::connect(launcher.data(), SIGNAL(applicationOutputReceived(const QString &)), &handler, SLOT(applicationOutputReceived(const QString &)));
       
   200     QObject::connect(launcher.data(), SIGNAL(copyProgress(int)), &handler, SLOT(copyProgress(int)));
       
   201     QObject::connect(launcher.data(), SIGNAL(stateChanged(int)), &handler, SLOT(stateChanged(int)));
       
   202     QObject::connect(launcher.data(), SIGNAL(processStopped(uint,uint,uint,QString)), &handler, SLOT(stopped(uint,uint,uint,QString)));
       
   203     QObject::connect(&handler, SIGNAL(resume(uint,uint)), launcher.data(), SLOT(resumeProcess(uint,uint)));
       
   204     QObject::connect(&handler, SIGNAL(terminate()), launcher.data(), SLOT(terminate()));
       
   205     QObject::connect(launcher.data(), SIGNAL(finished()), &handler, SLOT(finished()));
       
   206 
       
   207     QTimer timer;
       
   208     timer.setSingleShot(true);
       
   209     QObject::connect(&timer, SIGNAL(timeout()), &handler, SLOT(timeout()));
       
   210     if (timeout > 0) {
       
   211         timer.start(timeout);
       
   212     }
       
   213 
       
   214     QString errorMessage;
       
   215     if (!launcher->startServer(&errorMessage)) {
       
   216         errstream << errorMessage << endl;
       
   217         return 1;
       
   218     }
       
   219 
       
   220     return a.exec();
       
   221 }
       
   222