Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_ost / qostdevice.cpp
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 QtDeclarative module 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 "qostdevice.h"
43 #include <e32base.h>
44
45 #include "usbostcomm.h"
46
47 class QOstDevicePrivate : public CActive
48 {
49     QOstDevice* q_ptr;
50     Q_DECLARE_PUBLIC(QOstDevice)
51
52 public:
53     QOstDevicePrivate() : CActive(CActive::EPriorityStandard) {
54         CActiveScheduler::Add(this);
55     }
56     ~QOstDevicePrivate() {
57         Cancel();
58     }
59
60 private:
61     void RunL();
62     void DoCancel();
63
64 private:
65     RUsbOstComm ost;
66     TBuf8<4096> readBuf;
67     QByteArray dataBuf;
68 };
69
70 QOstDevice::QOstDevice(QObject *parent) :
71     QIODevice(parent), d_ptr(new QOstDevicePrivate)
72 {
73     d_ptr->q_ptr = this;
74 }
75
76 QOstDevice::~QOstDevice()
77 {
78     close();
79     delete d_ptr;
80 }
81
82 bool QOstDevice::open(int ostProtocolId)
83 {
84     if (isOpen())
85         return false;
86
87     Q_D(QOstDevice);
88     TInt err = d->ost.Connect();
89     if (!err) err = d->ost.Open();
90     const TVersion KRequiredVersion(1,1,0);
91     TVersion version = d->ost.Version();
92     if (version.iMajor < KRequiredVersion.iMajor ||
93             (version.iMajor == KRequiredVersion.iMajor && version.iMinor < KRequiredVersion.iMinor)) {
94         setErrorString("CODA version too old. At least version 4.0.18 (without TRK) is required.");
95         return false;
96     }
97
98     if (!err) err = d->ost.RegisterProtocolID((TOstProtIds)ostProtocolId, EFalse);
99     if (!err) {
100         d->ost.ReadMessage(d->iStatus, d->readBuf);
101         d->SetActive();
102         return QIODevice::open(ReadWrite | Unbuffered);
103     }
104     return false;
105 }
106
107 void QOstDevicePrivate::RunL()
108 {
109     Q_Q(QOstDevice);
110     //qDebug("QOstDevice received %d bytes q=%x", readBuf.Size(), q);
111     if (iStatus == KErrNone) {
112         QByteArray data = QByteArray::fromRawData((const char*)readBuf.Ptr(), readBuf.Size());
113         dataBuf.append(data);
114
115         readBuf.Zero();
116         ost.ReadMessage(iStatus, readBuf);
117         SetActive();
118
119         emit q->readyRead();
120     } else {
121         q->setErrorString(QString("Error %1 from RUsbOstComm::ReadMessage()").arg(iStatus.Int()));
122     }
123     //qDebug("-QOstDevicePrivate RunL");
124 }
125
126 void QOstDevicePrivate::DoCancel()
127 {
128     ost.ReadCancel();
129 }
130
131 void QOstDevice::close()
132 {
133     Q_D(QOstDevice);
134     QIODevice::close();
135     d->Cancel();
136     // RDbgTrcComm::Close isn't safe to call when not open, sigh
137     if (d->ost.Handle()) {
138         d->ost.Close();
139     }
140 }
141
142 qint64 QOstDevice::readData(char *data, qint64 maxSize)
143 {
144     Q_D(QOstDevice);
145     if (d->dataBuf.length() == 0 && !d->IsActive())
146         return -1;
147     qint64 available = qMin(maxSize, (qint64)d->dataBuf.length());
148     memcpy(data, d->dataBuf.constData(), available);
149     d->dataBuf.remove(0, available);
150     return available;
151 }
152
153 static const TInt KMaxOstPacketLen = 4096;
154
155 qint64 QOstDevice::writeData(const char *data, qint64 maxSize)
156 {
157     Q_D(QOstDevice);
158     TPtrC8 ptr((const TUint8*)data, (TInt)maxSize);
159     while (ptr.Length()) {
160         TPtrC8 fragment = ptr.Left(qMin(ptr.Length(), KMaxOstPacketLen));
161         //qDebug("QOstDevice writing %d bytes", fragment.Length());
162         TRequestStatus stat;
163         d->ost.WriteMessage(stat, fragment);
164         User::WaitForRequest(stat);
165         if (stat.Int() != KErrNone) {
166             setErrorString(QString("Error %1 from RUsbOstComm::WriteMessage()").arg(stat.Int()));
167             return -1;
168         }
169         ptr.Set(ptr.Mid(fragment.Length()));
170     }
171     emit bytesWritten(maxSize); //TODO does it matter this is emitted synchronously?
172     //qDebug("QOstDevice wrote %d bytes", ptr.Size());
173     return maxSize;
174 }
175
176 qint64 QOstDevice::bytesAvailable() const
177 {
178     Q_D(const QOstDevice);
179     return d->dataBuf.length();
180 }