diff options
author | Kari Oikarinen <kari.oikarinen@qt.io> | 2016-08-26 10:06:23 +0300 |
---|---|---|
committer | Kari Oikarinen <kari.oikarinen@qt.io> | 2016-08-29 09:07:36 +0000 |
commit | 19e169c102c6f084bc13c738322f1e3e4f3219c3 (patch) | |
tree | 79672ff0edf3f4b4dfee5d44c66d40e0075b349c | |
parent | 0dd19a27b4cc398c9e4128223b393333ed99de12 (diff) |
Add HandshakeService for fetching device information
Device serial number and the host-side MAC address the USB NIC presents
are fetched from /sys. The paths are hardcoded to the ones that the
current script uses.
qdb subcommand network now fetches the device information and configures
the USB NIC. The serial number is used to name the connection. The
previous functionality is still available by giving a MAC address to the
network subcommand.
Task-number: QTBUG-55433
Change-Id: I812273fd378f093febd917b56bfaa850a3f6f621
Reviewed-by: Kimmo Ollila <kimmo.ollila@theqtcompany.com>
-rw-r--r-- | client/client.pro | 2 | ||||
-rw-r--r-- | client/handshakeservice.cpp | 76 | ||||
-rw-r--r-- | client/handshakeservice.h | 52 | ||||
-rw-r--r-- | client/main.cpp | 61 | ||||
-rw-r--r-- | client/networkmanagercontrol.cpp | 11 | ||||
-rw-r--r-- | client/networkmanagercontrol.h | 4 | ||||
-rw-r--r-- | libqdb/protocol/services.h | 1 | ||||
-rw-r--r-- | qdbd/createexecutor.cpp | 3 | ||||
-rw-r--r-- | qdbd/handshakeexecutor.cpp | 60 | ||||
-rw-r--r-- | qdbd/handshakeexecutor.h | 41 | ||||
-rw-r--r-- | qdbd/qdbd.pro | 2 |
11 files changed, 299 insertions, 14 deletions
diff --git a/client/client.pro b/client/client.pro index 9e127cd..3272d24 100644 --- a/client/client.pro +++ b/client/client.pro @@ -14,6 +14,7 @@ HEADERS += \ echoservice.h \ filepullservice.h \ filepushservice.h \ + handshakeservice.h \ networkmanagercontrol.h \ processservice.h \ service.h @@ -23,6 +24,7 @@ SOURCES += \ echoservice.cpp \ filepullservice.cpp \ filepushservice.cpp \ + handshakeservice.cpp \ main.cpp \ networkmanagercontrol.cpp \ processservice.cpp \ diff --git a/client/handshakeservice.cpp b/client/handshakeservice.cpp new file mode 100644 index 0000000..a592a75 --- /dev/null +++ b/client/handshakeservice.cpp @@ -0,0 +1,76 @@ +/****************************************************************************** +** +** 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 "handshakeservice.h" + +#include "connection.h" +#include "protocol/services.h" +#include "stream.h" + +#include <QtCore/qdebug.h> + +HandshakeService::HandshakeService(Connection *connection) + : m_connection{connection} +{ + +} + +HandshakeService::~HandshakeService() +{ + if (m_stream) + m_stream->requestClose(); +} + +void HandshakeService::initialize() +{ + m_connection->createStream(tagBuffer(HandshakeTag), [=](Stream *stream) { + this->streamCreated(stream); + }); +} + +bool HandshakeService::hasStream() const +{ + return m_stream != nullptr; +} + +void HandshakeService::ask() +{ + if (!m_stream) { + qCritical() << "No valid stream in HandshakeService when trying to send"; + return; + } + StreamPacket packet{}; + packet << 0; + m_stream->write(packet); +} + +void HandshakeService::close() +{ + m_stream->requestClose(); +} + +void HandshakeService::receive(StreamPacket packet) +{ + QString serial; + QString macAddress; + packet >> serial >> macAddress; + + emit response(serial, macAddress); +} diff --git a/client/handshakeservice.h b/client/handshakeservice.h new file mode 100644 index 0000000..2800b74 --- /dev/null +++ b/client/handshakeservice.h @@ -0,0 +1,52 @@ +/****************************************************************************** +** +** 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$ +** +******************************************************************************/ +#ifndef HANDSHAKESERVICE_H +#define HANDSHAKESERVICE_H + +#include "service.h" + +class Connection; +class Stream; +class StreamPacket; + +class HandshakeService : public Service +{ + Q_OBJECT +public: + explicit HandshakeService(Connection *connection); + ~HandshakeService(); + + void initialize() override; + + bool hasStream() const; + void ask(); + void close(); +signals: + void response(QString serial, QString macAddress); + +public slots: + void receive(StreamPacket packet) override; + +private: + Connection *m_connection; +}; + +#endif // HANDSHAKESERVICE_H diff --git a/client/main.cpp b/client/main.cpp index 3d555ec..cbef27c 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -23,6 +23,7 @@ #include "connection.h" #include "filepullservice.h" #include "filepushservice.h" +#include "handshakeservice.h" #include "interruptsignalhandler.h" #include "networkmanagercontrol.h" #include "processservice.h" @@ -106,9 +107,9 @@ void setupProcessService(Connection *connection, const QString &processName, con service->initialize(); } -void configureUsbNetwork(const QString &macAddress) +void configureUsbNetwork(const QString &serial, const QString &macAddress) { - qDebug() << "Configuring network for" << macAddress; + qDebug() << "Configuring network for" << serial << "at" << macAddress; NetworkManagerControl networkManager; auto deviceResult = networkManager.findNetworkDeviceByMac(macAddress); if (!deviceResult.isValid()) { @@ -123,11 +124,50 @@ void configureUsbNetwork(const QString &macAddress) return; } } - if (!networkManager.activateOrCreateConnection(QDBusObjectPath{networkCard}, macAddress)) + if (!networkManager.activateOrCreateConnection(QDBusObjectPath{networkCard}, serial, macAddress)) qWarning() << "Could not setup network settings for the USB Ethernet interface"; } } +void setupNetworkConfiguration(Connection *connection) +{ + auto *service = new HandshakeService{connection}; + + QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, + service, &QObject::deleteLater); + QObject::connect(service, &HandshakeService::response, + [](QString serial, QString mac) { + qDebug() << "Device serial:" << serial; + qDebug() << "Host-side MAC address:" << mac; + configureUsbNetwork(serial, mac); + QCoreApplication::quit(); + }); + QObject::connect(service, &Service::initialized, [=]() { + service->ask(); + }); + + service->initialize(); +} + +void setupHandshakeService(Connection *connection) +{ + auto *service = new HandshakeService{connection}; + + QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, + service, &QObject::deleteLater); + QObject::connect(service, &HandshakeService::response, + [](QString serial, QString mac) { + qDebug() << "Device serial:" << serial; + qDebug() << "Host-side MAC address:" << mac; + QCoreApplication::quit(); + }); + QObject::connect(service, &Service::initialized, [=]() { + service->ask(); + }); + + service->initialize(); +} + int main(int argc, char *argv[]) { QCoreApplication app{argc, argv}; @@ -165,7 +205,7 @@ int main(int argc, char *argv[]) qDebug() << "initialized connection"; QStringList arguments = parser.positionalArguments(); - if (arguments.size() < 2) + if (arguments.size() < 1) return 0; QString command = arguments[0]; @@ -179,9 +219,16 @@ int main(int argc, char *argv[]) Q_ASSERT(arguments.size() == 3); setupFilePullService(&connection, arguments[1], arguments[2]); } else if (command == "network") { - Q_ASSERT(arguments.size() == 2); - configureUsbNetwork(arguments[1]); - QTimer::singleShot(1, []() { QCoreApplication::quit(); }); + if (arguments.size() == 1) { + setupNetworkConfiguration(&connection); + } else { + Q_ASSERT(arguments.size() == 2); + const auto macAddress = arguments[1]; + configureUsbNetwork(QString{"B2Qt device at %1"}.arg(macAddress), macAddress); + QTimer::singleShot(1, []() { QCoreApplication::quit(); }); + } + } else if (command == "handshake") { + setupHandshakeService(&connection); } else { qDebug() << "Unrecognized command:" << command; return 1; diff --git a/client/networkmanagercontrol.cpp b/client/networkmanagercontrol.cpp index ed32e0c..ed0fff0 100644 --- a/client/networkmanagercontrol.cpp +++ b/client/networkmanagercontrol.cpp @@ -128,7 +128,7 @@ NetworkManagerControl::NetworkManagerControl() qDBusRegisterMetaType<SettingsMap>(); } -bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &devicePath, const QString &macAddress) +bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &devicePath, const QString &serial, const QString &macAddress) { qDebug() << "Activating or creating a connection"; const auto connectionsResult = findConnectionsByMac(macAddress); @@ -148,7 +148,7 @@ bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &de connectionPath = *it; } else { qDebug() << "Creating new connection"; - const auto result = createConnection(macAddress); + const auto result = createConnection(serial, macAddress); if (!result.isValid()) { qWarning() << "Could not create a NetworkManager connection for" << macAddress; return false; @@ -179,7 +179,7 @@ bool NetworkManagerControl::activateOrCreateConnection(const QDBusObjectPath &de return true; } -QVariant NetworkManagerControl::createConnection(const QString &macAddress) +QVariant NetworkManagerControl::createConnection(const QString &serial, const QString &macAddress) { /* * Connection settings have the D-Bus signature a{sa{sv}}. @@ -199,7 +199,7 @@ QVariant NetworkManagerControl::createConnection(const QString &macAddress) */ QMap<QString, QDBusVariant> connectionMap; - connectionMap["id"] = QDBusVariant{QString{"B2Qt connection to %1"}.arg(macAddress)}; + connectionMap["id"] = QDBusVariant{QString{"%1 via USB"}.arg(serial)}; connectionMap["type"] = QDBusVariant{QStringLiteral("802-3-ethernet")}; QMap<QString, QDBusVariant> ethernetMap; @@ -283,6 +283,7 @@ QVariant NetworkManagerControl::findNetworkDeviceByMac(const QString &macAddress QVariant result = listNetworkDevices(); const auto devices = result.value<QList<QDBusObjectPath>>(); + const auto normalizedMac = macAddress.toUpper(); for (const auto &device : devices) { QDBusInterface wiredDeviceInterface{networkManagerServiceName, device.path(), @@ -301,7 +302,7 @@ QVariant NetworkManagerControl::findNetworkDeviceByMac(const QString &macAddress continue; } - if (macResult.toString() == macAddress) { + if (macResult.toString().toUpper() == normalizedMac) { qDebug() << macAddress << "is" << device.path(); return device.path(); } diff --git a/client/networkmanagercontrol.h b/client/networkmanagercontrol.h index 291d88c..de0ca39 100644 --- a/client/networkmanagercontrol.h +++ b/client/networkmanagercontrol.h @@ -30,8 +30,8 @@ class NetworkManagerControl public: NetworkManagerControl(); - bool activateOrCreateConnection(const QDBusObjectPath &devicePath, const QString &macAddress); - QVariant createConnection(const QString &macAddress); + bool activateOrCreateConnection(const QDBusObjectPath &devicePath, const QString &serial, const QString &macAddress); + QVariant createConnection(const QString &serial, const QString &macAddress); QVariant findConnectionsByMac(const QString &macAddress); QVariant findNetworkDeviceByMac(const QString &macAddress); bool isActivated(const QString &devicePath); diff --git a/libqdb/protocol/services.h b/libqdb/protocol/services.h index 9b07d62..0c02004 100644 --- a/libqdb/protocol/services.h +++ b/libqdb/protocol/services.h @@ -32,6 +32,7 @@ enum ServiceTag : uint32_t ProcessTag, FilePushTag, FilePullTag, + HandshakeTag, }; const int fileTransferBlockSize = 4096; // in bytes diff --git a/qdbd/createexecutor.cpp b/qdbd/createexecutor.cpp index 1cc1dbf..06dc0c3 100644 --- a/qdbd/createexecutor.cpp +++ b/qdbd/createexecutor.cpp @@ -24,6 +24,7 @@ #include "echoexecutor.h" #include "filepullexecutor.h" #include "filepushexecutor.h" +#include "handshakeexecutor.h" #include "processexecutor.h" #include "protocol/services.h" @@ -45,6 +46,8 @@ std::unique_ptr<Executor> createExecutor(Stream *stream, const QByteArray &tagBu return make_unique<FilePushExecutor>(stream); case FilePullTag: return make_unique<FilePullExecutor>(stream); + case HandshakeTag: + return make_unique<HandshakeExecutor>(stream); default: qCritical("Unknown ServiceTag %d in createExecutor", tag); return std::unique_ptr<Executor>{}; diff --git a/qdbd/handshakeexecutor.cpp b/qdbd/handshakeexecutor.cpp new file mode 100644 index 0000000..0ca9ec8 --- /dev/null +++ b/qdbd/handshakeexecutor.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** +** +** 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 "handshakeexecutor.h" + +#include "stream.h" + +#include <QtCore/qdebug.h> +#include <QtCore/qfile.h> + +QString deviceSerial() +{ + QFile file{"/sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber"}; + if (!file.open(QIODevice::ReadOnly)) + return ""; + return QString{file.readAll()}.trimmed(); +} + +QString hostSideMac() +{ + QFile file{"/sys/kernel/config/usb_gadget/g1/functions/rndis.usb0/host_addr"}; + if (!file.open(QIODevice::ReadOnly)) + return ""; + return QString{file.readAll()}.trimmed(); +} + +HandshakeExecutor::HandshakeExecutor(Stream *stream) + : m_stream{stream} +{ + if (m_stream) + connect(m_stream, &Stream::packetAvailable, this, &Executor::receive); +} + +void HandshakeExecutor::receive(StreamPacket packet) +{ + Q_UNUSED(packet); + + qDebug() << "Responding to handshake with device information."; + StreamPacket response; + response << deviceSerial(); + response << hostSideMac(); + m_stream->write(response); +} diff --git a/qdbd/handshakeexecutor.h b/qdbd/handshakeexecutor.h new file mode 100644 index 0000000..ed736d8 --- /dev/null +++ b/qdbd/handshakeexecutor.h @@ -0,0 +1,41 @@ +/****************************************************************************** +** +** 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$ +** +******************************************************************************/ +#ifndef HANDSHAKEEXECUTOR_H +#define HANDSHAKEEXECUTOR_H + +#include "executor.h" + +class Stream; + +class HandshakeExecutor : public Executor +{ + Q_OBJECT +public: + HandshakeExecutor(Stream *stream); + +public slots: + void receive(StreamPacket packet) override; + +private: + Stream *m_stream; +}; + +#endif // HANDSHAKEEXECUTOR_H diff --git a/qdbd/qdbd.pro b/qdbd/qdbd.pro index 73d57c1..39137a8 100644 --- a/qdbd/qdbd.pro +++ b/qdbd/qdbd.pro @@ -15,6 +15,7 @@ SOURCES += \ executor.cpp \ filepullexecutor.cpp \ filepushexecutor.cpp \ + handshakeexecutor.cpp \ main.cpp \ processexecutor.cpp \ server.cpp \ @@ -29,6 +30,7 @@ HEADERS += \ executor.h \ filepullexecutor.h \ filepushexecutor.h \ + handshakeexecutor.h \ processexecutor.h \ server.h \ usb-gadget/usbgadget.h \ |