From 714e5dadf2d6064aaddd7d4e0abd55af332b9448 Mon Sep 17 00:00:00 2001 From: Rami Potinkara Date: Mon, 25 May 2020 18:52:09 +0300 Subject: new menu item for usb ethernet protocol selection Task-number: QTBUG-83011 Change-Id: I8098ef26c9fa42cdf301145937eca011365d6f19 Pick-to: 5.15 Reviewed-by: Teemu Holappa --- .../connman/qnetworksettingsmanager_p.cpp | 86 ++++++++++++++++++++++ .../connman/qnetworksettingsmanager_p.h | 11 ++- 2 files changed, 96 insertions(+), 1 deletion(-) (limited to 'src/networksettings/connman') diff --git a/src/networksettings/connman/qnetworksettingsmanager_p.cpp b/src/networksettings/connman/qnetworksettingsmanager_p.cpp index 5bea9e2..a56edca 100644 --- a/src/networksettings/connman/qnetworksettingsmanager_p.cpp +++ b/src/networksettings/connman/qnetworksettingsmanager_p.cpp @@ -33,10 +33,14 @@ #include "qnetworksettingsinterface_p.h" #include "qnetworksettingsservicemodel.h" #include "qnetworksettingsuseragent.h" +#include +#include QT_BEGIN_NAMESPACE const QString ConnManServiceName(QStringLiteral("net.connman")); +const QString QdbdFileName(QStringLiteral("/etc/default/qdbd")); +const char* SearchKeyword("USB_ETHERNET_PROTOCOL="); QNetworkSettingsManagerPrivate::QNetworkSettingsManagerPrivate(QNetworkSettingsManager *parent) :QObject(parent) @@ -254,6 +258,7 @@ void QNetworkSettingsManagerPrivate::onServicesChanged(ConnmanMapStructList chan foreach (QString newService, newServices) { handleNewService(newService); } + } void QNetworkSettingsManagerPrivate::handleNewService(const QString &servicePath) @@ -316,7 +321,88 @@ void QNetworkSettingsManagerPrivate::serviceReady() technology->setState(technology->state()); } } + } +} + +QString QNetworkSettingsManagerPrivate::usbEthernetInternetProtocolAddress() +{ + QString usbEthernetIp = QLatin1String("Not connected"); + QNetworkInterface interface = QNetworkInterface::interfaceFromName(QLatin1String("usb0")); + if (interface.flags().testFlag(QNetworkInterface::IsUp)) { + for (QNetworkAddressEntry &entry:interface.addressEntries()) { + if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol){ + usbEthernetIp = entry.ip().toString(); + break; + } + } + } + return usbEthernetIp; +} + +QString QNetworkSettingsManagerPrivate::usbVirtualEthernetLinkProtocol() +{ + QByteArray line(QNetworkSettingsManagerPrivate::readUsbEthernetProtocolLine()); + QString protocol; + if (line.size()) { + int keywordStartIndex(line.indexOf("=")); + line = line.trimmed(); + protocol = QString::fromLatin1( line.mid(keywordStartIndex + 1, (line.length() - 1)).toUpper() ); + } + return protocol; +} +bool QNetworkSettingsManagerPrivate::hasUsbEthernetProtocolConfiguration() +{ + return !(QNetworkSettingsManagerPrivate::readUsbEthernetProtocolLine().isEmpty()); +} + +void QNetworkSettingsManagerPrivate::setUsbVirtualEthernetLinkProtocol(const QString &protocol) +{ + if (QLatin1String("RNDIS") == protocol || QLatin1String("CDCECM") == protocol) { + QByteArray fileContent(QNetworkSettingsManagerPrivate::readQdbdFileContent()); + writeUsbEthernetProtocolToFileContent(fileContent, protocol); + } else{ + qWarning("Unsupported USB Ethernet protocol"); + } +} + +QByteArray QNetworkSettingsManagerPrivate::readQdbdFileContent() +{ + QFile qdbdFile(QdbdFileName); + QByteArray fileContent; + if (qdbdFile.open(QIODevice::ReadOnly | QIODevice::Text)) + fileContent = qdbdFile.readAll(); + + return fileContent; +} + +QByteArray QNetworkSettingsManagerPrivate::readUsbEthernetProtocolLine() +{ + QByteArray fileContent(QNetworkSettingsManagerPrivate::readQdbdFileContent()); + int keywordStartIndex(fileContent.indexOf(SearchKeyword, 0)); + int keywordLineEndIndex(fileContent.indexOf("\n", keywordStartIndex)); + QByteArray keywordLine = fileContent.mid(keywordStartIndex, keywordLineEndIndex); + return keywordLine; +} + +void QNetworkSettingsManagerPrivate::writeUsbEthernetProtocolToFileContent(QByteArray &fileContent, const QString &protocol) +{ + int keywordStartIndex(fileContent.indexOf(SearchKeyword)); + QByteArray previousLines = fileContent.mid(0, keywordStartIndex); + int keywordLineEndIndex(fileContent.indexOf("\n", keywordStartIndex)); + QByteArray keywordLine = fileContent.mid(keywordStartIndex, keywordLineEndIndex); + QByteArray followingLines = fileContent.mid((keywordLineEndIndex), (fileContent.length() - 1)); + QByteArray updatedLines = previousLines.append(SearchKeyword); + updatedLines.append(protocol.toLatin1().toLower()); + updatedLines.append(followingLines); + QFile qdbdFile(QdbdFileName); + if (qdbdFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + int result = qdbdFile.write(updatedLines); + if (-1 == result) + qDebug("USB Ethernet protocol write to file failed"); + + } else { + qDebug("USB Ethernet protocol file open failed"); } } diff --git a/src/networksettings/connman/qnetworksettingsmanager_p.h b/src/networksettings/connman/qnetworksettingsmanager_p.h index 8b5d1a7..c6f31e6 100644 --- a/src/networksettings/connman/qnetworksettingsmanager_p.h +++ b/src/networksettings/connman/qnetworksettingsmanager_p.h @@ -74,6 +74,10 @@ public: QNetworkSettingsService* currentWifiConnection() const {return m_currentWifiConnection;} void setCurrentWiredConnection(QNetworkSettingsService *connection) {m_currentWiredConnection = connection;} QNetworkSettingsService* currentWiredConnection() const {return m_currentWiredConnection;} + QString usbEthernetInternetProtocolAddress(); + QString usbVirtualEthernetLinkProtocol(); + bool hasUsbEthernetProtocolConfiguration(); + void setUsbVirtualEthernetLinkProtocol(const QString &protocol); public slots: void getServicesFinished(QDBusPendingCallWatcher *watcher); @@ -84,9 +88,14 @@ public slots: void onConnmanServiceRegistered(const QString &serviceName); void onTechnologyAdded(const QDBusObjectPath &technology, const QVariantMap &properties); void onTechnologyRemoved(const QDBusObjectPath &technology); + private: bool initialize(); - void handleNewService(const QString& servicePath); + void handleNewService(const QString &servicePath); + void writeUsbEthernetProtocolToFileContent(QByteArray &fileContent, const QString &protocol); + static QByteArray readQdbdFileContent(); + static QByteArray readUsbEthernetProtocolLine(); + protected: QNetworkSettingsInterfaceModel m_interfaceModel; QNetworkSettingsServiceModel *m_serviceModel; -- cgit v1.2.3