summaryrefslogtreecommitdiffstats
path: root/tools/runonphone/codasignalhandler.cpp
blob: 1df7834c1f581d8bfcd022662d2b2e0fb1029442 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QObject>
#include <QTimer>
#include "codasignalhandler.h"
#include "texttracehandler.h"

static const quint64 DEFAULT_CHUNK_SIZE = 40000;

class CodaSignalHandlerPrivate
{
    friend class CodaSignalHandler;
public:
    CodaSignalHandlerPrivate();
    ~CodaSignalHandlerPrivate();
private:
    SymbianUtils::CodaDevicePtr codaDevice;
    QEventLoop *eventLoop;
    QTextStream out;
    QTextStream err;
    int loglevel;
    int timeout;
    int result;
    CodaAction action;
    QString copySrcFileName;
    QString copyDstFileName;
    QString dlSrcFileName;
    QString dlDstFileName;
    QString appFileName;
    QString commandLineArgs;
    QString serialPortName;
    QString appID;
    QByteArray remoteFileHandle;
    QScopedPointer<QFile> localFile;
    QScopedPointer<QFile> remoteFile;
    quint64 putChunkSize;
    quint64 putLastChunkSize;
    quint64 remoteBytesLeft;
    quint64 remoteFileSize;
    bool putWriteOk;
    bool connected;
    bool debugSessionControl;
};

CodaSignalHandlerPrivate::CodaSignalHandlerPrivate()
    : eventLoop(0),
      out(stdout),
      err(stderr),
      loglevel(0),
      timeout(0),
      result(0),
      action(ActionPingOnly),
      putChunkSize(DEFAULT_CHUNK_SIZE),
      putLastChunkSize(0),
      remoteBytesLeft(0),
      remoteFileSize(0),
      putWriteOk(false),
      connected(false),
      debugSessionControl(false)
{
}

CodaSignalHandlerPrivate::~CodaSignalHandlerPrivate()
{
    delete eventLoop;
    out.flush();
    err.flush();
}

void CodaSignalHandler::error(const QString &errorMessage)
{
    reportError(tr("CODA error: %1").arg(errorMessage));
}

void CodaSignalHandler::logMessage(const QString &logMessage)
{
    reportMessage(tr("CODA log: %1").arg(logMessage));
}

void CodaSignalHandler::serialPong(const QString &codaVersion)
{
    reportMessage(tr("CODA version: %1").arg(codaVersion));
}

void CodaSignalHandler::tcfEvent(const Coda::CodaEvent &event)
{
    reportMessage(tr("CODA event: Type: %1 Message: %2").arg(event.type()).arg(event.toString()));

    switch (event.type()) {
    case Coda::CodaEvent::LocatorHello:
        handleConnected(event);
        break;
    case Coda::CodaEvent::ProcessExitedEvent:
        handleAppExited(event);
        break;
    default:
        reportMessage(tr("CODA unhandled event: Type: %1 Message: %2").arg(event.type()).arg(event.toString()));
        break;
    }
}

void CodaSignalHandler::terminate()
{
    if (d->codaDevice) {
        disconnect(d->codaDevice.data(), 0, this, 0);
        SymbianUtils::SymbianDeviceManager::instance()->releaseCodaDevice(d->codaDevice);
    }
}

void CodaSignalHandler::finished()
{
    if (d->eventLoop)
        d->eventLoop->exit();
}

void CodaSignalHandler::timeout()
{
    reportError(tr("Unable to connect to CODA device. Operation timed out."));
}

int CodaSignalHandler::run()
{
    d->codaDevice = SymbianUtils::SymbianDeviceManager::instance()->getCodaDevice(d->serialPortName);
    bool ok = d->codaDevice && d->codaDevice->device()->isOpen();
    if (!ok) {
        QString deviceError = "No such port";
        if (d->codaDevice)
            deviceError = d->codaDevice->device()->errorString();
        reportError(tr("Could not open serial device: %1").arg(deviceError));
        SymbianUtils::SymbianDeviceManager::instance()->releaseCodaDevice(d->codaDevice);
        return 1;
    }

    TextTraceHandler *traceHandler = new TextTraceHandler(
                SymbianUtils::SymbianDeviceManager::instance()->getOstChannel(d->serialPortName, 2), this);

    if (d->loglevel > 1) {
        d->codaDevice->setVerbose(1);
    }

    connect(d->codaDevice.data(), SIGNAL(error(const QString &)), this, SLOT(error(const QString &)));
    connect(d->codaDevice.data(), SIGNAL(logMessage(const QString &)), this, SLOT(logMessage(const QString &)));
    connect(d->codaDevice.data(), SIGNAL(serialPong(const QString &)), this, SLOT(serialPong(const QString &)));
    connect(d->codaDevice.data(), SIGNAL(tcfEvent(const Coda::CodaEvent &)), this, SLOT(tcfEvent(const Coda::CodaEvent &)));
    connect(this, SIGNAL(done()), this, SLOT(finished()));

    d->codaDevice->sendSerialPing(false);
    QTimer timer;
    if (d->timeout > 0) {
        connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
        timer.setSingleShot(true);
        timer.start(d->timeout);
    }
    d->eventLoop = new QEventLoop();
    d->eventLoop->exec();
    timer.stop();
    int result = d->result;
    reportMessage(tr("Done."));

    delete traceHandler;
    disconnect(d->codaDevice.data(), 0, this, 0);
    SymbianUtils::SymbianDeviceManager::instance()->releaseCodaDevice(d->codaDevice);

    return result;
}

void CodaSignalHandler::setActionType(CodaAction action)
{
    d->action = CodaAction(d->action | action);
}

void CodaSignalHandler::setAppFileName(const QString &fileName)
{
    d->appFileName = fileName;
}

void CodaSignalHandler::setCodaDevice(SymbianUtils::CodaDevicePtr &codaDevice)
{
    d->codaDevice = codaDevice;
}

void CodaSignalHandler::setCommandLineArgs(const QString &args)
{
    d->commandLineArgs = args;
}

void CodaSignalHandler::setCopyFileName(const QString &srcName, const QString &dstName)
{
    d->copySrcFileName = srcName;
    d->copyDstFileName = dstName;
}

void CodaSignalHandler::setDownloadFileName(const QString &srcName, const QString &dstName)
{
    d->dlSrcFileName = srcName;
    d->dlDstFileName = dstName;
}

void CodaSignalHandler::setLogLevel(int level)
{
    d->loglevel = level;
}

void CodaSignalHandler::setSerialPortName(const QString &serialPortName)
{
    d->serialPortName = serialPortName;
}

void CodaSignalHandler::setTimeout(const int msec)
{
    d->timeout = msec;
}

void CodaSignalHandler::closeFile()
{
    d->remoteFile.reset();
    if (!d->codaDevice) {
        emit done();
        return;
    }

    d->codaDevice->sendFileSystemCloseCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemClose),
                                              d->remoteFileHandle);
}

void CodaSignalHandler::handleConnected(const Coda::CodaEvent &event)
{
    if (d->connected)
        return;

    const Coda::CodaLocatorHelloEvent &hEvent = static_cast<const Coda::CodaLocatorHelloEvent &>(event);
    QStringList services = hEvent.services();
    if (services.contains("DebugSessionControl")) {
        d->debugSessionControl = true;
    }
    d->connected = true;
    handleActions();
}

void CodaSignalHandler::handleActions()
{
    if (d->action & ActionCopy) {
        initFileSending();
    } else if (d->action & ActionInstall) {
        initFileInstallation();
    } else if (d->action & ActionRun) {
        initAppRunning();
    } else if (d->action & ActionDownload) {
        initFileDownloading();
    } else {
        emit done();
    }
}

void CodaSignalHandler::handleAppExited(const Coda::CodaEvent &event)
{
    const Coda::CodaProcessExitedEvent &pEvent = static_cast<const Coda::CodaProcessExitedEvent &>(event);
    QString id = pEvent.idString();
    if (!id.compare(d->appID, Qt::CaseInsensitive)) {
        d->codaDevice->sendDebugSessionControlSessionEndCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleDebugSessionControlEnd));
        d->action = static_cast<CodaAction>(d->action & ~ActionRun);
        handleActions();
    }
}

void CodaSignalHandler::handleAppRunning(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        reportMessage(tr("Running..."));

        Coda::JsonValue value = result.values.at(0);
        readAppId(value);
    } else {
        reportError(tr("Launch failed: %1").arg(result.toString()));
    }
}

void CodaSignalHandler::handleDebugSessionControlEnd(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        // nothing to do
    }
}

void CodaSignalHandler::handleDebugSessionControlStart(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        d->codaDevice->sendRunProcessCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleAppRunning),
                                             d->appFileName.toAscii(),
                                             d->commandLineArgs.split(' '));
        reportMessage(tr("Launching %1...").arg(QFileInfo(d->appFileName).fileName()));
    } else {
        reportError(tr("Failed to start CODA debug session control."));
    }
}

void CodaSignalHandler::handleFileSystemClose(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        reportMessage(tr("File closed."));
        if (d->action & ActionCopy) {
            d->action = static_cast<CodaAction>(d->action & ~ActionCopy);
        } else if (d->action & ActionDownload) {
            d->action = static_cast<CodaAction>(d->action & ~ActionDownload);
        }
        handleActions();
    } else {
        reportError(tr("Failed to close the remote file: %1").arg(result.toString()));
    }
}

void CodaSignalHandler::handleFileSystemOpen(const Coda::CodaCommandResult &result)
{
    if (result.type != Coda::CodaCommandResult::SuccessReply) {
        reportError(tr("Could not open remote file: %1").arg(result.errorString()));
        return;
    }

    if (result.values.size() < 1 || result.values.at(0).data().isEmpty()) {
        reportError(tr("Internal error: No filehandle obtained"));
        return;
    }

    if (d->action & ActionCopy) {
        d->remoteFileHandle = result.values.at(0).data();
        d->remoteFile.reset(new QFile(d->copySrcFileName));
        if (!d->remoteFile->open(QIODevice::ReadOnly)) { // Should not fail, was checked before
            reportError(tr("Could not open local file %1").arg(d->copySrcFileName));
            return;
        }
        putSendNextChunk();
    } else if (d->action & ActionDownload) {
        d->remoteFileHandle = result.values.at(0).data();
        d->localFile.reset(new QFile(d->dlDstFileName));
        // remove any existing file with the same name
        if (d->localFile->exists() && !d->localFile->remove()) {
            reportError(tr("Could not create host file: %1 due to error: %2").arg(d->localFile->fileName(), d->localFile->errorString()));
            return;
        }
        // open local file in write-only mode
        if (!d->localFile->open(QFile::WriteOnly)) {
            reportError(tr("Could not open host file for writing: %1 due to error: %2").arg(d->localFile->fileName(), d->localFile->errorString()));
            return;
        }
        d->codaDevice->sendFileSystemFstatCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemStart),
                                                  d->remoteFileHandle);
    }
}

void CodaSignalHandler::handleFileSystemRead(const Coda::CodaCommandResult &result)
{
    if (result.type != Coda::CodaCommandResult::SuccessReply || result.values.size() != 2) {
        reportError(tr("Could not read remote file: %1").arg(result.errorString()));
        return;
    }

    QByteArray data = QByteArray::fromBase64(result.values.at(0).data());
    bool eof = result.values.at(1).toVariant().toBool();
    qint64 written = d->localFile->write(data);
    if (written < 0) {
        reportError(tr("Could not write data to host file: %1 due to error: %2").arg(d->localFile->fileName(), d->localFile->errorString()));
        return;
    }

    d->remoteBytesLeft -= written;
    if (!eof && d->remoteBytesLeft > 0) {
        readNextChunk();
    }
    else {
        d->localFile->flush();
        d->localFile->close();
        closeFile();
    }
}

void CodaSignalHandler::handleFileSystemStart(const Coda::CodaCommandResult &result)
{
    if (result.type != Coda::CodaCommandResult::SuccessReply) {
        reportError(tr("Could not open remote file: %1").arg(result.errorString()));
        return;
    }

    if (result.values.size() < 1 || result.values.at(0).children().isEmpty()) {
        reportError(tr("Could not get file attributes"));
        return;
    }

    Coda::JsonValue val = result.values.at(0).findChild("Size");
    d->remoteFileSize = val.isValid() ? val.data().toLong() : -1L;
    if (d->remoteFileSize < 0) {
        reportError(tr("Could not get file size"));
        return;
    }

    d->remoteBytesLeft = d->remoteFileSize;
    readNextChunk();
}

void CodaSignalHandler::handleFileSystemWrite(const Coda::CodaCommandResult &result)
{
    // Close remote file even if copy fails
    d->putWriteOk = result;
    if (!d->putWriteOk) {
        QString fileName = QFileInfo(d->copyDstFileName).fileName();
        reportError(tr("Could not write to file %1 on device: %2").arg(fileName).arg(result.errorString()));
    }

    if (!d->putWriteOk || d->putLastChunkSize < d->putChunkSize) {
        closeFile();
    } else {
        putSendNextChunk();
    }
}

void CodaSignalHandler::handleSymbianInstall(const Coda::CodaCommandResult &result)
{
    if (result.type == Coda::CodaCommandResult::SuccessReply) {
        reportMessage(tr("Installation has finished."));
        d->action = static_cast<CodaAction>(d->action & ~ActionInstall);
        handleActions();
    } else {
        reportError(tr("Installation failed: %1").arg(result.errorString()));
    }
}

void CodaSignalHandler::initAppRunning()
{
    if (!d->codaDevice || d->appFileName.isEmpty()) {
        emit done();
        return;
    }

    if (!d->debugSessionControl) {
        reportError(tr("CODA DebugSessionControl service not found, please update to CODA v4.0.23 or later."));
    }

    d->codaDevice->sendDebugSessionControlSessionStartCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleDebugSessionControlStart));
}

void CodaSignalHandler::initFileDownloading()
{
    if (!d->codaDevice || d->dlSrcFileName.isEmpty()) {
        emit done();
        return;
    }

    d->codaDevice->sendFileSystemOpenCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemOpen),
                                             d->dlSrcFileName.toAscii(), Coda::CodaDevice::FileSystem_TCF_O_READ);
    reportMessage(tr("Downloading %1...").arg(QFileInfo(d->dlSrcFileName).fileName()));
}

void CodaSignalHandler::initFileInstallation()
{
    if (!d->codaDevice || d->copyDstFileName.isEmpty()) {
        emit done();
        return;
    }

    QString installationDrive = "C";
    d->codaDevice->sendSymbianInstallSilentInstallCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleSymbianInstall),
                                                          d->copyDstFileName.toAscii(),
                                                          installationDrive.toAscii());
    reportMessage(tr("Installing package \"%1\" on drive %2...").arg(QFileInfo(d->copyDstFileName).fileName(), installationDrive));
}

void CodaSignalHandler::initFileSending()
{
    if (!d->codaDevice || d->copySrcFileName.isEmpty()) {
        emit done();
        return;
    }

    const unsigned flags =
            Coda::CodaDevice::FileSystem_TCF_O_WRITE
            |Coda::CodaDevice::FileSystem_TCF_O_CREAT
            |Coda::CodaDevice::FileSystem_TCF_O_TRUNC;
    d->putWriteOk = false;
    d->codaDevice->sendFileSystemOpenCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemOpen),
                                             d->copyDstFileName.toAscii(), flags);
    reportMessage(tr("Copying %1...").arg(QFileInfo(d->copyDstFileName).fileName()));
}

void CodaSignalHandler::putSendNextChunk()
{
    if (!d->codaDevice || !d->remoteFile) {
        emit done();
        return;
    }

    // Read and send off next chunk
    const quint64 pos = d->remoteFile->pos();
    const QByteArray data = d->remoteFile->read(d->putChunkSize);
    if (data.isEmpty()) {
        d->putWriteOk = true;
        closeFile();
    } else {
        d->putLastChunkSize = data.size();
        d->codaDevice->sendFileSystemWriteCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemWrite),
                                                  d->remoteFileHandle, data, unsigned(pos));
    }
}

void CodaSignalHandler::readNextChunk()
{
    const quint64 pos = d->remoteFileSize - d->remoteBytesLeft;
    const quint64 size = qMin(d->remoteBytesLeft, DEFAULT_CHUNK_SIZE);
    d->codaDevice->sendFileSystemReadCommand(Coda::CodaCallback(this, &CodaSignalHandler::handleFileSystemRead),
                                             d->remoteFileHandle, pos, size);
}

void CodaSignalHandler::readAppId(Coda::JsonValue value)
{
    if (value.isObject()) {
        Coda::JsonValue idValue = value.findChild("ID");
        if (idValue.isString()) {
            d->appID = idValue.data();
            return;
        }
    }

    reportError(tr("Could not get process ID of %1.").arg(QFileInfo(d->appFileName).fileName()));
}

void CodaSignalHandler::reportError(const QString &message)
{
    d->err << message << endl;
    d->result = 1;
    emit done();
}

void CodaSignalHandler::reportMessage(const QString &message)
{
    if (d->loglevel > 0)
        d->out << message << endl;
}

CodaSignalHandler::CodaSignalHandler()
    : d(new CodaSignalHandlerPrivate())
{
}

CodaSignalHandler::~CodaSignalHandler()
{
    delete d;
}