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 ** 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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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() {
70 QOstDevice::QOstDevice(QObject *parent) :
71 QIODevice(parent), d_ptr(new QOstDevicePrivate)
76 QOstDevice::~QOstDevice()
82 bool QOstDevice::open(int ostProtocolId)
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.");
98 if (!err) err = d->ost.RegisterProtocolID((TOstProtIds)ostProtocolId, EFalse);
100 d->ost.ReadMessage(d->iStatus, d->readBuf);
102 return QIODevice::open(ReadWrite | Unbuffered);
107 void QOstDevicePrivate::RunL()
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);
116 ost.ReadMessage(iStatus, readBuf);
121 q->setErrorString(QString("Error %1 from RUsbOstComm::ReadMessage()").arg(iStatus.Int()));
123 //qDebug("-QOstDevicePrivate RunL");
126 void QOstDevicePrivate::DoCancel()
131 void QOstDevice::close()
136 // RDbgTrcComm::Close isn't safe to call when not open, sigh
137 if (d->ost.Handle()) {
142 qint64 QOstDevice::readData(char *data, qint64 maxSize)
145 if (d->dataBuf.length() == 0 && !d->IsActive())
147 qint64 available = qMin(maxSize, (qint64)d->dataBuf.length());
148 memcpy(data, d->dataBuf.constData(), available);
149 d->dataBuf.remove(0, available);
153 static const TInt KMaxOstPacketLen = 4096;
155 qint64 QOstDevice::writeData(const char *data, qint64 maxSize)
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());
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()));
169 ptr.Set(ptr.Mid(fragment.Length()));
171 emit bytesWritten(maxSize); //TODO does it matter this is emitted synchronously?
172 //qDebug("QOstDevice wrote %d bytes", ptr.Size());
176 qint64 QOstDevice::bytesAvailable() const
178 Q_D(const QOstDevice);
179 return d->dataBuf.length();