summaryrefslogtreecommitdiffstats
path: root/qdbd/usb-gadget/usbgadgetreader.cpp
blob: dfb5600cd2c82d79525cf0e9dbe0716db71f7ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/******************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Debug Bridge.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
******************************************************************************/
#include "usbgadgetreader.h"

#include "libqdb/protocol/protocol.h"
#include "libqdb/protocol/qdbmessage.h"

#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
#include <QtCore/qtimer.h>

UsbGadgetReader::UsbGadgetReader(QFile *readEndpoint)
    : m_readEndpoint{readEndpoint}
{

}

void UsbGadgetReader::executeRead()
{
    if (!m_readEndpoint->isOpen()) {
        qWarning() << "tried to receive from host through closed endpoint";
        return;
    }

    QTimer::singleShot(0, this, &UsbGadgetReader::executeRead);

    QByteArray headerBuffer{qdbHeaderSize, '\0'};
    int count = m_readEndpoint->read(headerBuffer.data(), headerBuffer.size());
    if (count == -1) {
        qWarning() << "error in reading message header from endpoint";
        return;
    } else if (count < headerBuffer.size()) {
        qWarning() << "error: read" << count << "out of" << headerBuffer.size() << "byte header from endpoint";
        return;
    }

    int dataSize = QdbMessage::GetDataSize(headerBuffer);
    Q_ASSERT(dataSize >= 0);
    if (dataSize == 0) {
        emit newRead(headerBuffer);
        return;
    }

    QByteArray dataBuffer{dataSize, '\0'};
    count = m_readEndpoint->read(dataBuffer.data(), dataBuffer.size());
    if (count == -1) {
        qWarning() << "error in reading data from endpoint";
        return;
    } else if (count < dataBuffer.size()) {
        qWarning() << "error: read" << count << "out of" << headerBuffer.size() << "byte data from endpoint";
        return;
    }

    emit newRead(headerBuffer + dataBuffer);
}