Skip to content

Commit 82c363a

Browse files
Tpimpfpoussin
authored andcommitted
Adding the ability to make asynchronous bulk transfers
Needs to better declare it is a bulk transfer.
1 parent 34cd739 commit 82c363a

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/usb/qusbendpoint.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,43 @@ int QUsbEndpointPrivate::writeUsb(const char *data, qint64 maxSize)
299299
return rc;
300300
}
301301

302+
int QUsbEndpointPrivate::writeUsbSynchronous(const char *data, qint64 maxSize)
303+
{
304+
Q_Q(QUsbEndpoint);
305+
DbgPrivPrintFuncName();
306+
int rc = -1;
307+
m_buf_mutex.lock();
308+
m_buf.resize(static_cast<int>(maxSize));
309+
memcpy(m_buf.data(), data, static_cast<ulong>(maxSize));
310+
int transferred = 0;
311+
auto buf = reinterpret_cast<uchar *>(m_buf.data());
312+
auto sizeInt = static_cast<int>(maxSize);
313+
auto handle = q->m_dev->d_func()->m_devHandle;
314+
auto timeout = q->m_dev->timeout();
315+
316+
// perform synchronous transfer
317+
rc = libusb_bulk_transfer(handle, q->m_ep, buf, sizeInt, &transferred, timeout);
318+
319+
if (rc == LIBUSB_ERROR_TIMEOUT)
320+
{
321+
qDebug() << "timeout (%d)\n" << transferred << " on bulk syncrhonous transfer";
322+
}
323+
else if (rc < 0)
324+
{
325+
qDebug() << "Error while waiting for char: %d\n" << rc;
326+
}
327+
if (rc != LIBUSB_SUCCESS) {
328+
setStatus(QUsbEndpoint::transferError);
329+
error(QUsbEndpoint::transferError);
330+
// TODO: Check if QUsbEndpoint::QUsbDevice must be const...
331+
QUsbDevice *dev = const_cast<QUsbDevice *>(q->m_dev);
332+
dev->handleUsbError(rc);
333+
}
334+
m_buf_mutex.unlock();
335+
return rc;
336+
337+
}
338+
302339
void QUsbEndpointPrivate::setPolling(bool enable)
303340
{
304341
Q_Q(QUsbEndpoint);
@@ -725,3 +762,21 @@ qint64 QUsbEndpoint::writeData(const char *data, qint64 maxSize)
725762

726763
return maxSize;
727764
}
765+
766+
qint64 QUsbEndpoint::writeDataSynchronous(const char *data, qint64 maxSize)
767+
{
768+
if (this->openMode() != WriteOnly)
769+
return -1;
770+
771+
Q_D(QUsbEndpoint);
772+
DbgPrintFuncName();
773+
Q_CHECK_PTR(data);
774+
775+
if (!d->isValid())
776+
return -1;
777+
778+
if (d->writeUsbSynchronous(data, maxSize) != 0)
779+
return -1;
780+
781+
return maxSize;
782+
}

src/usb/qusbendpoint.h

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Q_USB_EXPORT QUsbEndpoint : public QIODevice
9393
void setPolling(bool enable);
9494
bool polling();
9595
bool poll();
96+
qint64 writeDataSynchronous(const char *data, qint64 maxSize);
9697

9798
public Q_SLOTS:
9899
void cancelTransfer();

src/usb/qusbendpoint_p.h

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class QUsbEndpointPrivate : public QIODevicePrivate
4545

4646
int readUsb(qint64 maxSize);
4747
int writeUsb(const char *data, qint64 maxSize);
48+
int writeUsbSynchronous(const char *data, qint64 maxSize);
4849

4950
void setPolling(bool enable);
5051
bool polling() { return m_poll; }

0 commit comments

Comments
 (0)