diff options
author | Kari Oikarinen <kari.oikarinen@qt.io> | 2017-03-21 13:56:36 +0200 |
---|---|---|
committer | Kari Oikarinen <kari.oikarinen@qt.io> | 2017-05-02 13:30:59 +0000 |
commit | 1893ea57fb31d51c64c20c3376659f9d6a2ea4cf (patch) | |
tree | 60b662e530c939ab4cfcf3b470b8e86917b0aafd /qdb | |
parent | 842e0c9a9e95c7e8b0b02791f620fd859477d2e1 (diff) |
Reuse network configurations set on device if possible
Don't allow reconfiguring the network on the device without a reset in
between. It wouldn't lead to a working configuration, since the host has
already gotten an IP from the existing configuration at this point.
But if the already configured network happens to be free, use it. This
allows devices to work keeping the same address even if host qdb is
restarted.
This doesn't guard against the possibility that the existing
configuration conflicts with a network already in place. But telling
apart that situation would be too difficult: How to tell whether the
network is the device or something else? In that case the user just
has to replug the device. Then it will pick an unused subnet.
Task-number: QTBUG-59451
Change-Id: I10c948713736dd79442265ac6a590b8a7cf8345a
Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
Diffstat (limited to 'qdb')
-rw-r--r-- | qdb/server/devicemanager.cpp | 10 | ||||
-rw-r--r-- | qdb/server/networkconfigurationservice.cpp | 22 | ||||
-rw-r--r-- | qdb/server/networkconfigurationservice.h | 4 | ||||
-rw-r--r-- | qdb/server/networkconfigurator.cpp | 40 | ||||
-rw-r--r-- | qdb/server/networkconfigurator.h | 4 |
5 files changed, 71 insertions, 9 deletions
diff --git a/qdb/server/devicemanager.cpp b/qdb/server/devicemanager.cpp index 56ab7df..2c3b9f6 100644 --- a/qdb/server/devicemanager.cpp +++ b/qdb/server/devicemanager.cpp @@ -51,9 +51,13 @@ void DeviceManager::start() void DeviceManager::handleDeviceConfigured(UsbDevice device, bool success) { - qCDebug(devicesC) << "Configured device" << device.serial - << (success ? "successfully" : "unsuccessfully"); - fetchDeviceInformation(device); + if (success) { + qCDebug(devicesC) << "Configured device" << device.serial << "successfully"; + fetchDeviceInformation(device); + } else { + qCWarning(devicesC) << "Failed to configure device" << device.serial; + // Discard the device + } } void DeviceManager::handleDeviceInformation(UsbDevice device, DeviceInformationFetcher::Info info) diff --git a/qdb/server/networkconfigurationservice.cpp b/qdb/server/networkconfigurationservice.cpp index a052156..dbb9e56 100644 --- a/qdb/server/networkconfigurationservice.cpp +++ b/qdb/server/networkconfigurationservice.cpp @@ -67,8 +67,26 @@ void NetworkConfigurationService::receive(StreamPacket packet) uint32_t value; packet >> value; + const auto result = static_cast<ConfigurationResult>(value); + if (result != ConfigurationResult::Success + && result != ConfigurationResult::Failure + && result != ConfigurationResult::AlreadySet) { + qCCritical(configurationC) << "Unknown network configuration result" << value + << "received from device"; + failedResponse(); + } + + if (result == ConfigurationResult::AlreadySet) { + QString subnet; + packet >> subnet; + + m_responded = true; + emit alreadySetResponse(subnet); + return; + } + m_responded = true; - emit response(value == 1); + emit response(result); } void NetworkConfigurationService::onStreamClosed() @@ -85,7 +103,7 @@ void NetworkConfigurationService::handleDisconnected() void NetworkConfigurationService::failedResponse() { if (!m_responded) { - emit response(false); + emit response(ConfigurationResult::Failure); m_responded = true; } } diff --git a/qdb/server/networkconfigurationservice.h b/qdb/server/networkconfigurationservice.h index acdb513..7354853 100644 --- a/qdb/server/networkconfigurationservice.h +++ b/qdb/server/networkconfigurationservice.h @@ -21,6 +21,7 @@ #ifndef NETWORKCONFIGURATIONSERVICE_H #define NETWORKCONFIGURATIONSERVICE_H +#include "libqdb/networkconfigurationcommon.h" #include "service.h" class Connection; @@ -36,7 +37,8 @@ public: void configure(QString subnet); signals: - void response(bool success); + void response(ConfigurationResult result); + void alreadySetResponse(QString subnet); public slots: void receive(StreamPacket packet) override; diff --git a/qdb/server/networkconfigurator.cpp b/qdb/server/networkconfigurator.cpp index cbcac35..896dfe7 100644 --- a/qdb/server/networkconfigurator.cpp +++ b/qdb/server/networkconfigurator.cpp @@ -66,6 +66,8 @@ void NetworkConfigurator::configure() service, &QObject::deleteLater); connect(service, &NetworkConfigurationService::response, this, &NetworkConfigurator::handleResponse); + connect(service, &NetworkConfigurationService::alreadySetResponse, + this, &NetworkConfigurator::handleAlreadySetResponse); connect(service, &Service::initialized, [=]() { service->configure(subnetString); }); @@ -73,7 +75,41 @@ void NetworkConfigurator::configure() service->initialize(); } -void NetworkConfigurator::handleResponse(bool success) +void NetworkConfigurator::handleAlreadySetResponse(QString subnet) { - emit configured(m_device, success); + const QStringList parts = subnet.split(QLatin1Char{'/'}); + if (parts.size() != 2) { + qCCritical(configuratorC) << "Invalid already set subnet from device" << m_device.serial + << ":" << subnet; + emit configured(m_device, false); + return; + } + + const QHostAddress address{parts[0]}; + const int prefixLength = parts[1].toInt(); + if (address.isNull() || prefixLength < 1 || prefixLength > 32) { + qCCritical(configuratorC) << "Invalid already set subnet from device" << m_device.serial + << ":" << subnet; + emit configured(m_device, false); + return; + } + + Subnet subnetStruct{address, prefixLength}; + SubnetReservation reservation = SubnetPool::instance()->reserve(subnetStruct); + if (!reservation) { + qCWarning(configuratorC) << "Could not reserve already set subnet" << subnet + << "for device" << m_device.serial; + emit configured(m_device, false); + return; + } + + qCDebug(configuratorC) << "Reused already set configuration" << subnet << "for device" + << m_device.serial; + m_device.reservation = reservation; + emit configured(m_device, true); +} + +void NetworkConfigurator::handleResponse(ConfigurationResult result) +{ + emit configured(m_device, result == ConfigurationResult::Success); } diff --git a/qdb/server/networkconfigurator.h b/qdb/server/networkconfigurator.h index 01eaf74..4b840ac 100644 --- a/qdb/server/networkconfigurator.h +++ b/qdb/server/networkconfigurator.h @@ -21,6 +21,7 @@ #ifndef NETWORKCONFIGURATOR_H #define NETWORKCONFIGURATOR_H +#include "libqdb/networkconfigurationcommon.h" #include "usb-host/usbdevice.h" class Connection; class ConnectionPool; @@ -42,7 +43,8 @@ signals: void configured(UsbDevice device, bool success); private slots: - void handleResponse(bool success); + void handleAlreadySetResponse(QString subnet); + void handleResponse(ConfigurationResult result); private: std::shared_ptr<Connection> m_connection; |