Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_ost / qostdevice.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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     TInt& AoFlags() { return ((TInt*)&iStatus)[1]; }
61
62 private:
63     void RunL();
64     void DoCancel();
65
66 private:
67     RUsbOstComm ost;
68     TBuf8<4096> readBuf;
69     QByteArray dataBuf;
70     TBool inReadyRead;
71 };
72
73 QOstDevice::QOstDevice(QObject *parent) :
74     QIODevice(parent), d_ptr(new QOstDevicePrivate)
75 {
76     d_ptr->q_ptr = this;
77 }
78
79 QOstDevice::~QOstDevice()
80 {
81     close();
82     delete d_ptr;
83 }
84
85 bool QOstDevice::open(int ostProtocolId)
86 {
87     if (isOpen())
88         return false;
89
90     Q_D(QOstDevice);
91     TInt err = d->ost.Connect();
92     if (!err) err = d->ost.Open();
93     const TVersion KRequiredVersion(1,1,0);
94     TVersion version = d->ost.Version();
95     if (version.iMajor < KRequiredVersion.iMajor ||
96             (version.iMajor == KRequiredVersion.iMajor && version.iMinor < KRequiredVersion.iMinor)) {
97         setErrorString("CODA version too old. At least version 4.0.18 (without TRK) is required.");
98         return false;
99     }
100
101     if (!err) err = d->ost.RegisterProtocolID((TOstProtIds)ostProtocolId, EFalse);
102     if (!err) {
103         d->ost.ReadMessage(d->iStatus, d->readBuf);
104         d->SetActive();
105         return QIODevice::open(ReadWrite | Unbuffered);
106     }
107     return false;
108 }
109
110 void QOstDevicePrivate::RunL()
111 {
112     Q_Q(QOstDevice);
113     //qDebug("QOstDevice received %d bytes q=%x", readBuf.Size(), q);
114     if (iStatus == KErrNone) {
115         QByteArray data = QByteArray::fromRawData((const char*)readBuf.Ptr(), readBuf.Size());
116         dataBuf.append(data);
117
118         readBuf.Zero();
119         ost.ReadMessage(iStatus, readBuf);
120         SetActive();
121
122         if (!inReadyRead) {
123             inReadyRead = true;
124             emit q->readyRead();
125             inReadyRead = false;
126         }
127     } else {
128         q->setErrorString(QString("Error %1 from RUsbOstComm::ReadMessage()").arg(iStatus.Int()));
129     }
130     //qDebug("-QOstDevicePrivate RunL");
131 }
132
133 void QOstDevicePrivate::DoCancel()
134 {
135     ost.ReadCancel();
136 }
137
138 void QOstDevice::close()
139 {
140     Q_D(QOstDevice);
141     QIODevice::close();
142     d->Cancel();
143     // RDbgTrcComm::Close isn't safe to call when not open, sigh
144     if (d->ost.Handle()) {
145         d->ost.Close();
146     }
147 }
148
149 qint64 QOstDevice::readData(char *data, qint64 maxSize)
150 {
151     Q_D(QOstDevice);
152     if (d->dataBuf.length() == 0 && !d->IsActive())
153         return -1;
154     qint64 available = qMin(maxSize, (qint64)d->dataBuf.length());
155     memcpy(data, d->dataBuf.constData(), available);
156     d->dataBuf.remove(0, available);
157     return available;
158 }
159
160 static const TInt KMaxOstPacketLen = 4096;
161
162 qint64 QOstDevice::writeData(const char *data, qint64 maxSize)
163 {
164     Q_D(QOstDevice);
165     TPtrC8 ptr((const TUint8*)data, (TInt)maxSize);
166     while (ptr.Length()) {
167         TPtrC8 fragment = ptr.Left(qMin(ptr.Length(), KMaxOstPacketLen));
168         //qDebug("QOstDevice writing %d bytes", fragment.Length());
169         TRequestStatus stat;
170         d->ost.WriteMessage(stat, fragment);
171         User::WaitForRequest(stat);
172         if (stat.Int() != KErrNone) {
173             setErrorString(QString("Error %1 from RUsbOstComm::WriteMessage()").arg(stat.Int()));
174             return -1;
175         }
176         ptr.Set(ptr.Mid(fragment.Length()));
177     }
178     emit bytesWritten(maxSize); //TODO does it matter this is emitted synchronously?
179     //qDebug("QOstDevice wrote %d bytes", ptr.Size());
180     return maxSize;
181 }
182
183 qint64 QOstDevice::bytesAvailable() const
184 {
185     Q_D(const QOstDevice);
186     return d->dataBuf.length();
187 }
188
189 bool QOstDevice::waitForReadyRead(int msecs)
190 {
191     Q_D(QOstDevice);
192     if (msecs >= 0) {
193         RTimer timer;
194         TInt err = timer.CreateLocal();
195         if (err) return false;
196         TRequestStatus timeoutStat;
197         timer.After(timeoutStat, msecs*1000);
198         User::WaitForRequest(timeoutStat, d->iStatus);
199         if (timeoutStat != KRequestPending) {
200             // Timed out
201             timer.Close();
202             return false;
203         } else {
204             // We got data, so cancel timer
205             timer.Cancel();
206             User::WaitForRequest(timeoutStat);
207             timer.Close();
208             // And drop through
209         }
210     } else {
211         // Just wait forever for data
212         User::WaitForRequest(d->iStatus);
213     }
214
215     // If we get here we have data
216     TInt err = d->iStatus.Int();
217     d->AoFlags() &= ~3; // This is necessary to clean up the scheduler as you're not supposed to bypass it like this
218     TRAP_IGNORE(d->RunL());
219     return err == KErrNone;
220 }