summaryrefslogtreecommitdiffstats
path: root/qdb/server/devicemanager.cpp
blob: 3314546f9db066582b2f477f521f705ccb9b2a55 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/******************************************************************************
**
** 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 "devicemanager.h"

#include "networkmanagercontrol.h"

#include <QtCore/qdebug.h>
#include <QtCore/qloggingcategory.h>
#include <QtDBus/QDBusObjectPath>

Q_LOGGING_CATEGORY(devicesC, "qdb.devices");

DeviceManager::DeviceManager(QObject *parent)
    : QObject{parent},
      m_deviceEnumerator{},
      m_newDevices{},
      m_incompleteDevices{},
      m_fetchingDevice{},
      m_fetching{false},
      m_deviceInfos{}
{

}

std::vector<DeviceInformation> DeviceManager::listDevices()
{
    return m_deviceInfos;
}

void DeviceManager::start()
{
    connect(&m_deviceEnumerator, &UsbDeviceEnumerator::devicePluggedIn, this, &DeviceManager::handlePluggedInDevice);
    connect(&m_deviceEnumerator, &UsbDeviceEnumerator::deviceUnplugged, this, &DeviceManager::handleUnpluggedDevice);
    m_deviceEnumerator.startMonitoring();
}

void DeviceManager::handleDeviceInformation(DeviceInformation deviceInfo)
{
    m_fetching = false;
    if (deviceInfo.hostMac.isEmpty()) {
        qCWarning(devicesC) << "Could not fetch device information from" << m_fetchingDevice.serial;
        return; // Discard the device
    }

    qCDebug(devicesC) << "Configuring network for" << deviceInfo.serial;
    configureUsbNetwork(deviceInfo.serial, deviceInfo.hostMac);

    if (deviceInfo.ipAddress.isEmpty()) {
        qCDebug(devicesC) << "Incomplete information received for" << deviceInfo.serial;
        m_incompleteDevices.enqueue(m_fetchingDevice);
        QTimer::singleShot(1000, this, &DeviceManager::fetchIncomplete);
    } else {
        qCDebug(devicesC) << "Complete info received for" << deviceInfo.serial;
    }

    auto iter = std::find_if(m_deviceInfos.begin(), m_deviceInfos.end(),
                             [&](const DeviceInformation &oldInfo) {
                                 return deviceInfo.serial == oldInfo.serial;
                             });
    if (iter == m_deviceInfos.end()) {
        qCDebug(devicesC) << "Added new info for" << deviceInfo.serial;
        m_deviceInfos.push_back(deviceInfo);
        emit newDeviceInfo(deviceInfo);
    } else if (*iter != deviceInfo) {
        qCDebug(devicesC) << "Replaced old info for" << deviceInfo.serial;
        *iter = deviceInfo;
        emit newDeviceInfo(deviceInfo);
    }

    if (!m_newDevices.isEmpty())
        fetchDeviceInformation(m_newDevices.dequeue());
}

void DeviceManager::handlePluggedInDevice(UsbDevice device)
{
    qCDebug(devicesC) << "Device" << device.serial << "plugged in at" << device.address.busNumber << ":" << device.address.deviceAddress;
    if (m_fetching)
        m_newDevices.enqueue(device);
    else
        fetchDeviceInformation(device);
}

void DeviceManager::handleUnpluggedDevice(UsbAddress address)
{
    qCDebug(devicesC) << "Device unplugged from" << address.busNumber << ":" << address.deviceAddress;

    auto deviceAddressMatches = [&](const UsbDevice &device) {
        return device.address == address;
    };

    const auto newIter = std::find_if(m_newDevices.begin(), m_newDevices.end(), deviceAddressMatches);
    if (newIter != m_newDevices.end()) {
        m_newDevices.erase(newIter);
        return; // New devices can't yet be in the other lists.
    }

    const auto incompleteIter = std::find_if(m_incompleteDevices.begin(), m_incompleteDevices.end(),
                                             deviceAddressMatches);
    if (incompleteIter != m_incompleteDevices.end()) {
        m_incompleteDevices.erase(incompleteIter);
        // Incomplete devices already have information stored about them, so continue.
    }

    const auto infoIter = std::find_if(m_deviceInfos.begin(), m_deviceInfos.end(),
                                       [&](const DeviceInformation &info) {
                                           return info.usbAddress == address;
                                       });
    if (infoIter != m_deviceInfos.end()) {
        emit disconnectedDevice(infoIter->serial);
        m_deviceInfos.erase(infoIter);
    }
}

void DeviceManager::fetchDeviceInformation(UsbDevice device)
{
    qCDebug(devicesC) << "Fetching device information for" << device.serial;
    Q_ASSERT(!m_fetching);
    m_fetching = true;
    m_fetchingDevice = device;
    auto *fetcher = new DeviceInformationFetcher{device};
    connect(fetcher, &DeviceInformationFetcher::fetched, fetcher, &QObject::deleteLater);
    connect(fetcher, &DeviceInformationFetcher::fetched, this, &DeviceManager::handleDeviceInformation);

    fetcher->fetch();
}

void DeviceManager::fetchIncomplete()
{
    if (m_incompleteDevices.empty())
        return;

    if (m_fetching) {
        qCDebug(devicesC) << "Delaying fetching incomplete information due to fetch in progress";
        QTimer::singleShot(500, this, &DeviceManager::fetchIncomplete);
        return;
    }

    fetchDeviceInformation(m_incompleteDevices.dequeue());
}