1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qostdevice.h"
45 #include "usbostcomm.h"
47 class QOstDevicePrivate : public CActive
50 Q_DECLARE_PUBLIC(QOstDevice)
53 QOstDevicePrivate() : CActive(CActive::EPriorityStandard) {
54 CActiveScheduler::Add(this);
56 ~QOstDevicePrivate() {
60 TInt& AoFlags() { return ((TInt*)&iStatus)[1]; }
73 QOstDevice::QOstDevice(QObject *parent) :
74 QIODevice(parent), d_ptr(new QOstDevicePrivate)
79 QOstDevice::~QOstDevice()
85 bool QOstDevice::open(int ostProtocolId)
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.");
101 if (!err) err = d->ost.RegisterProtocolID((TOstProtIds)ostProtocolId, EFalse);
103 d->ost.ReadMessage(d->iStatus, d->readBuf);
105 return QIODevice::open(ReadWrite | Unbuffered);
110 void QOstDevicePrivate::RunL()
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);
119 ost.ReadMessage(iStatus, readBuf);
128 q->setErrorString(QString("Error %1 from RUsbOstComm::ReadMessage()").arg(iStatus.Int()));
130 //qDebug("-QOstDevicePrivate RunL");
133 void QOstDevicePrivate::DoCancel()
138 void QOstDevice::close()
143 // RDbgTrcComm::Close isn't safe to call when not open, sigh
144 if (d->ost.Handle()) {
149 qint64 QOstDevice::readData(char *data, qint64 maxSize)
152 if (d->dataBuf.length() == 0 && !d->IsActive())
154 qint64 available = qMin(maxSize, (qint64)d->dataBuf.length());
155 memcpy(data, d->dataBuf.constData(), available);
156 d->dataBuf.remove(0, available);
160 static const TInt KMaxOstPacketLen = 4096;
162 qint64 QOstDevice::writeData(const char *data, qint64 maxSize)
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());
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()));
176 ptr.Set(ptr.Mid(fragment.Length()));
178 emit bytesWritten(maxSize); //TODO does it matter this is emitted synchronously?
179 //qDebug("QOstDevice wrote %d bytes", ptr.Size());
183 qint64 QOstDevice::bytesAvailable() const
185 Q_D(const QOstDevice);
186 return d->dataBuf.length();
189 bool QOstDevice::waitForReadyRead(int msecs)
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) {
204 // We got data, so cancel timer
206 User::WaitForRequest(timeoutStat);
211 // Just wait forever for data
212 User::WaitForRequest(d->iStatus);
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;