summaryrefslogtreecommitdiffstats
path: root/qdbd
diff options
context:
space:
mode:
authorKari Oikarinen <kari.oikarinen@qt.io>2016-09-19 11:21:26 +0300
committerKari Oikarinen <kari.oikarinen@qt.io>2016-10-18 12:36:55 +0000
commitbf3d39023b4cbeb20095ff2c84127ce824c2979e (patch)
tree6a83d377f090c1be7fe451bb9603bfcec4bdc3ee /qdbd
parent1a8ff526eb750c6629c03c694c07be8d0bebffe2 (diff)
Fetch device IP address on the USB NIC
Device IP address is needed by the host for connecting the device with SSH. The interface name for the USB gadget network is fetched from configfs using a hard-coded path that the current development script uses. The IPv4 address on that interface is then determined. When there is no IPv4 address (yet) on the interface, an empty string is returned. Task-number: QTBUG-55435 Change-Id: Ia16daa89d1b28a7f13ec2d8e8fe2131ff5a48c2b Reviewed-by: Samuli Piippo <samuli.piippo@qt.io>
Diffstat (limited to 'qdbd')
-rw-r--r--qdbd/handshakeexecutor.cpp28
-rw-r--r--qdbd/qdbd.pro2
2 files changed, 27 insertions, 3 deletions
diff --git a/qdbd/handshakeexecutor.cpp b/qdbd/handshakeexecutor.cpp
index 0ca9ec8..b5dc9cb 100644
--- a/qdbd/handshakeexecutor.cpp
+++ b/qdbd/handshakeexecutor.cpp
@@ -24,10 +24,33 @@
#include <QtCore/qdebug.h>
#include <QtCore/qfile.h>
+#include <QtNetwork/qnetworkinterface.h>
+
+const QString gadgetConfigFsPath = "/sys/kernel/config/usb_gadget/g1/";
+
+QString deviceIpAddress()
+{
+ QFile file{gadgetConfigFsPath + "functions/rndis.usb0/ifname"};
+ if (!file.open(QIODevice::ReadOnly))
+ return "";
+
+ const auto interfaceName = QString{file.readAll()}.trimmed();
+ const auto interface = QNetworkInterface::interfaceFromName(interfaceName);
+ const auto addressEntries = interface.addressEntries();
+
+ for (const auto &entry : addressEntries) {
+ const auto ip = entry.ip();
+ if (ip.protocol() == QAbstractSocket::IPv4Protocol) {
+ qDebug() << "Device IP address:" << ip.toString();
+ return ip.toString();
+ }
+ }
+ return "";
+}
QString deviceSerial()
{
- QFile file{"/sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber"};
+ QFile file{gadgetConfigFsPath + "strings/0x409/serialnumber"};
if (!file.open(QIODevice::ReadOnly))
return "";
return QString{file.readAll()}.trimmed();
@@ -35,7 +58,7 @@ QString deviceSerial()
QString hostSideMac()
{
- QFile file{"/sys/kernel/config/usb_gadget/g1/functions/rndis.usb0/host_addr"};
+ QFile file{gadgetConfigFsPath + "functions/rndis.usb0/host_addr"};
if (!file.open(QIODevice::ReadOnly))
return "";
return QString{file.readAll()}.trimmed();
@@ -56,5 +79,6 @@ void HandshakeExecutor::receive(StreamPacket packet)
StreamPacket response;
response << deviceSerial();
response << hostSideMac();
+ response << deviceIpAddress();
m_stream->write(response);
}
diff --git a/qdbd/qdbd.pro b/qdbd/qdbd.pro
index d92e05e..76ec5df 100644
--- a/qdbd/qdbd.pro
+++ b/qdbd/qdbd.pro
@@ -1,4 +1,4 @@
-QT += core
+QT += core network
QT -= gui
CONFIG += c++11