From a91a5c6f36da6b7324d0a52ecf1ab3c7517c5375 Mon Sep 17 00:00:00 2001 From: Gatis Paeglis Date: Mon, 6 Jul 2015 15:14:27 +0200 Subject: Wifi: Remove android specific code paths eAndroid support has been deprecated. Change-Id: Id62ce140917dc1e45e94e800dc8cdc54736dfc36 Reviewed-by: aavit --- src/wifi/qwificontroller.cpp | 217 +++---------------------------------------- src/wifi/qwificontroller_p.h | 26 ------ src/wifi/qwifidevice.cpp | 44 +-------- src/wifi/qwifimanager.cpp | 20 +--- src/wifi/wifi.pro | 25 ++--- 5 files changed, 26 insertions(+), 306 deletions(-) diff --git a/src/wifi/qwificontroller.cpp b/src/wifi/qwificontroller.cpp index 7b8be80..779b2a5 100644 --- a/src/wifi/qwificontroller.cpp +++ b/src/wifi/qwificontroller.cpp @@ -25,106 +25,6 @@ #include #include -#ifdef Q_OS_ANDROID_NO_SDK -#include -#include -#include -#endif - -#ifdef Q_OS_ANDROID_NO_SDK -/* - * Workaround API differences between Android versions - */ -int q_wifi_start_supplicant() -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR < 1 - return wifi_start_supplicant(); -#else - return wifi_start_supplicant(0); -#endif -} - -int q_wifi_stop_supplicant() -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR < 1 - return wifi_stop_supplicant(); -#else - return wifi_stop_supplicant(0); -#endif -} - -int q_wifi_connect_to_supplicant(const char *ifname) -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && (Q_ANDROID_VERSION_MINOR < 4 && Q_ANDROID_VERSION_MINOR >= 1) - return wifi_connect_to_supplicant(ifname); -#else - Q_UNUSED(ifname); - return wifi_connect_to_supplicant(); -#endif -} - -void q_wifi_close_supplicant_connection(const char *ifname) -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && (Q_ANDROID_VERSION_MINOR < 4 && Q_ANDROID_VERSION_MINOR >= 1) - wifi_close_supplicant_connection(ifname); -#else - Q_UNUSED(ifname); - wifi_close_supplicant_connection(); -#endif -} - -int q_wifi_wait_for_event(const char *ifname, char *buf, size_t len) -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && (Q_ANDROID_VERSION_MINOR < 4 && Q_ANDROID_VERSION_MINOR >= 1) - return wifi_wait_for_event(ifname, buf, len); -#else - Q_UNUSED(ifname); - return wifi_wait_for_event(buf, len); -#endif -} - -int q_wifi_command(const char *ifname, const char *command, char *reply, size_t *reply_len) -{ -#if Q_ANDROID_VERSION_MAJOR == 4 && (Q_ANDROID_VERSION_MINOR < 4 && Q_ANDROID_VERSION_MINOR >= 1) - return wifi_command(ifname, command, reply, reply_len); -#else - Q_UNUSED(ifname); - return wifi_command(command, reply, reply_len); -#endif -} - -/* - * This function is borrowed from /system/core/libnetutils/dhcp_utils.c - * - * Wait for a system property to be assigned a specified value. - * If desired_value is NULL, then just wait for the property to - * be created with any value. maxwait is the maximum amount of - * time in seconds to wait before giving up. - */ -const int NAP_TIME = 200; // wait for 200ms at a time when polling for property values -int wait_for_property(const char *name, const char *desired_value, int maxwait) -{ - char value[PROPERTY_VALUE_MAX] = {'\0'}; - int maxnaps = (maxwait * 1000) / NAP_TIME; - - if (maxnaps < 1) { - maxnaps = 1; - } - - while (maxnaps-- > 0) { - usleep(NAP_TIME * 1000); - if (property_get(name, value, NULL)) { - if (desired_value == NULL || - strcmp(value, desired_value) == 0) { - return 0; - } - } - } - return -1; /* failure */ -} - -#endif // Q_OS_ANDROID_NO_SDK - QT_BEGIN_NAMESPACE Q_LOGGING_CATEGORY(B2QT_WIFI, "qt.b2qt.wifi") @@ -182,46 +82,23 @@ QWifiController::QWifiController(QWifiManager *manager, QWifiManagerPrivate *man m_manager(manager), m_managerPrivate(managerPrivate), m_exitEventThread(false), -#ifdef Q_OS_ANDROID_NO_SDK - m_qcSocket(0), -#endif m_eventThread(0) { m_interface = QWifiDevice::wifiInterfaceName(); m_eventThread = new QWifiEventThread(this); qRegisterMetaType("QWifiManager::BackendState"); -#ifdef Q_OS_ANDROID_NO_SDK - qRegisterMetaType("QAbstractSocket::SocketState"); -#endif } QWifiController::~QWifiController() { exitWifiEventThread(); delete m_eventThread; -#ifdef Q_OS_ANDROID_NO_SDK - delete m_qcSocket; -#endif -} - -void QWifiController::allocateOnThisThread() -{ -#ifdef Q_OS_ANDROID_NO_SDK - m_qcSocket = new QLocalSocket; - int qcFd = socket_local_client("qconnectivity", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); - if (qcFd != -1) { - m_qcSocket->setSocketDescriptor(qcFd); - } else { - qCWarning(B2QT_WIFI) << "failed to get file descriptor of a qconnectivity socket!"; - } -#endif } void QWifiController::run() { qCDebug(B2QT_WIFI) << "running wifi backend controller thread"; - allocateOnThisThread(); Method method; forever { m_methodsMutex.lock(); @@ -260,23 +137,15 @@ void QWifiController::initializeBackend() { qCDebug(B2QT_WIFI) << "initializing wifi backend"; emit backendStateChanged(QWifiManager::Initializing); - bool initFailed = false; -#ifdef Q_OS_ANDROID_NO_SDK - qCDebug(B2QT_WIFI) << "initialize driver"; - if (!(is_wifi_driver_loaded() || wifi_load_driver() == 0)) { - qCWarning(B2QT_WIFI) << "failed to load a driver"; - initFailed = true; - } -#else qCDebug(B2QT_WIFI) << "run ifconfig (up)"; QProcess ifconfig; ifconfig.start(QStringLiteral("ifconfig"), QStringList() << QLatin1String(m_interface) << QStringLiteral("up")); ifconfig.waitForFinished(); + bool initFailed = false; if (ifconfig.exitStatus() != QProcess::NormalExit && ifconfig.exitCode() != 0) { qCWarning(B2QT_WIFI) << "failed to bring up wifi interface!"; initFailed = true; } -#endif if (!initFailed && resetSupplicantSocket()) { qCDebug(B2QT_WIFI) << "wifi backend started successfully"; emit backendStateChanged(QWifiManager::Running); @@ -297,12 +166,6 @@ bool QWifiController::resetSupplicantSocket() qCWarning(B2QT_WIFI) << "failed to start a supplicant!"; return false; } -#ifdef Q_OS_ANDROID_NO_SDK - if (wait_for_property("init.svc.wpa_supplicant", "running", 5) < 0) { - qCWarning(B2QT_WIFI) << "timed out waiting for supplicant to start!"; - return false; - } -#endif qCDebug(B2QT_WIFI) << "connect to supplicant"; if (q_wifi_connect_to_supplicant(m_interface) != 0) { qCWarning(B2QT_WIFI) << "failed to connect to a supplicant!"; @@ -320,14 +183,14 @@ void QWifiController::terminateBackend() if (q_wifi_stop_supplicant() < 0) qCWarning(B2QT_WIFI) << "failed to stop supplicant!"; q_wifi_close_supplicant_connection(m_interface); -#ifndef Q_OS_ANDROID_NO_SDK + qCDebug(B2QT_WIFI) << "run ifconfig (down)"; QProcess ifconfig; ifconfig.start(QStringLiteral("ifconfig"), QStringList() << QLatin1String(m_interface) << QStringLiteral("down")); ifconfig.waitForFinished(); if (ifconfig.exitStatus() != QProcess::NormalExit && ifconfig.exitCode() != 0) qCWarning(B2QT_WIFI) << "failed to bring down wifi interface!"; -#endif + stopDhcp(); qCDebug(B2QT_WIFI) << "wifi backend stopped successfully"; emit backendStateChanged(QWifiManager::NotRunning); @@ -349,66 +212,25 @@ void QWifiController::exitWifiEventThread() } } -#ifdef Q_OS_ANDROID_NO_SDK -bool QWifiController::getQConnectivityReply() -{ - bool arrived = false; - if (m_qcSocket->canReadLine()) { - arrived = true; - QByteArray received = m_qcSocket->readLine(m_qcSocket->bytesAvailable()); - if (received != "success" && received != "failed") { - qCWarning(B2QT_WIFI) << "unknown message: " << received; - received = "failed"; - } - emit dhcpRequestFinished(QLatin1String(received)); - } - return arrived; -} -#else void QWifiController::killDhcpProcess(const QString &path) const { QFile pidFile(path); - if (pidFile.exists()) { - pidFile.open(QIODevice::ReadOnly); - QByteArray pid = pidFile.readAll(); - QProcess kill; - kill.start(QStringLiteral("kill"), QStringList() << QLatin1String(pid.trimmed())); - kill.waitForFinished(); - if (kill.exitStatus() != QProcess::NormalExit && kill.exitCode() != 0) - qCWarning(B2QT_WIFI) << "killing dhcp process failed!"; + if (!pidFile.exists() || !pidFile.open(QIODevice::ReadOnly)) { + qCWarning(B2QT_WIFI) << "could not read pid file: " << path; + return; } - pidFile.close(); + + QByteArray pid = pidFile.readAll(); + QProcess kill; + kill.start(QStringLiteral("kill"), QStringList() << QStringLiteral("-9") << QLatin1String(pid.trimmed())); + kill.waitForFinished(); + if (kill.exitStatus() != QProcess::NormalExit && kill.exitCode() != 0) + qCWarning(B2QT_WIFI) << "killing dhcp process failed!"; } -#endif void QWifiController::acquireIPAddress() { qCDebug(B2QT_WIFI, "acquireIPAddress"); -#ifdef Q_OS_ANDROID_NO_SDK - QByteArray request = m_interface + " connect\n"; - m_qcSocket->abort(); - m_qcSocket->connectToServer(QStringLiteral(ANDROID_SOCKET_DIR "/qconnectivity")); - bool timeout = false; - if (m_qcSocket->waitForConnected()) { - m_qcSocket->write(request, request.length()); - m_qcSocket->flush(); - do { - if (m_qcSocket->waitForReadyRead()) { - if (getQConnectivityReply()) - break; - } else { - timeout = true; - qCWarning(B2QT_WIFI) << "waiting a message from qconnectivity timed out!"; - break; - } - } while (true); - } else { - timeout = true; - qCWarning(B2QT_WIFI) << "connecting to qconnectivity server socket timed out!"; - } - if (timeout) - emit dhcpRequestFinished(QStringLiteral("failed")); -#else QString filePath = QLatin1String("/var/run/udhcpc." + m_interface + ".pid"); killDhcpProcess(filePath); QStringList args; @@ -426,26 +248,13 @@ void QWifiController::acquireIPAddress() status = QLatin1String("failed"); emit dhcpRequestFinished(status); -#endif } void QWifiController::stopDhcp() const { qCDebug(B2QT_WIFI, "stopDhcp"); -#ifdef Q_OS_ANDROID_NO_SDK - QByteArray request = m_interface + " disconnect\n"; - m_qcSocket->abort(); - m_qcSocket->connectToServer(QStringLiteral(ANDROID_SOCKET_DIR "/qconnectivity")); - if (m_qcSocket->waitForConnected()) { - m_qcSocket->write(request, request.length()); - m_qcSocket->flush(); - } else { - qCWarning(B2QT_WIFI) << "connecting to qconnectivity server socket timed out!"; - } -#else QString filePath = QLatin1String("/var/run/udhcpc." + m_interface + ".pid"); killDhcpProcess(filePath); -#endif } QT_END_NAMESPACE diff --git a/src/wifi/qwificontroller_p.h b/src/wifi/qwificontroller_p.h index 185cff9..952b8d4 100644 --- a/src/wifi/qwificontroller_p.h +++ b/src/wifi/qwificontroller_p.h @@ -28,27 +28,12 @@ #include #include -#ifdef Q_OS_ANDROID_NO_SDK -#include -#include -#else #include "qwifielinux_p.h" -#endif QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(B2QT_WIFI) -#ifdef Q_OS_ANDROID_NO_SDK -int q_wifi_start_supplicant(); -int q_wifi_stop_supplicant(); -int q_wifi_connect_to_supplicant(const char *ifname); -void q_wifi_close_supplicant_connection(const char *ifname); -int q_wifi_wait_for_event(const char *ifname, char *buf, size_t len); -int q_wifi_command(const char *ifname, const char *command, char *reply, size_t *reply_len); -int wait_for_property(const char *name, const char *desired_value, int maxwait); -#endif - const QEvent::Type WIFI_SCAN_RESULTS = (QEvent::Type) (QEvent::User + 2001); const QEvent::Type WIFI_CONNECTED = (QEvent::Type) (QEvent::User + 2002); const QEvent::Type WIFI_HANDSHAKE_FAILED = (QEvent::Type) (QEvent::User + 2003); @@ -58,9 +43,6 @@ const QEvent::Type WIFI_DISCONNECTED = (QEvent::Type) (QEvent::User + 2005); class QWifiManager; class QWifiManagerPrivate; class QWifiEventThread; -#ifdef Q_OS_ANDROID_NO_SDK -class QLocalSocket; -#endif class QWifiEvent : public QEvent { @@ -108,21 +90,13 @@ protected: void initializeBackend(); void terminateBackend(); void exitWifiEventThread(); - void allocateOnThisThread(); -#ifdef Q_OS_ANDROID_NO_SDK - bool getQConnectivityReply(); -#else void killDhcpProcess(const QString &path) const; -#endif private: QWifiManager *m_manager; QWifiManagerPrivate *const m_managerPrivate; bool m_exitEventThread; QByteArray m_interface; -#ifdef Q_OS_ANDROID_NO_SDK - QLocalSocket *m_qcSocket; -#endif QVector m_methods; QWifiEventThread *m_eventThread; QMutex m_methodsMutex; diff --git a/src/wifi/qwifidevice.cpp b/src/wifi/qwifidevice.cpp index 0b52780..5ddc02b 100644 --- a/src/wifi/qwifidevice.cpp +++ b/src/wifi/qwifidevice.cpp @@ -20,10 +20,6 @@ #include #include -#ifdef Q_OS_ANDROID_NO_SDK -#include -#include -#endif QT_BEGIN_NAMESPACE @@ -97,41 +93,18 @@ QWifiDevice::~QWifiDevice() */ bool QWifiDevice::wifiSupported() { -#ifdef Q_OS_ANDROID_NO_SDK - const char *fwpath = 0; - // reload wifi firmware - fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA); - if (!fwpath) { - qCWarning(B2QT_WIFI) << "failed to get firmware path"; - return false; - } - if (wifi_change_fw_path((const char *)fwpath)) { - qCWarning(B2QT_WIFI) << "failed to change firmware path"; - return false; - } -#endif QByteArray ifc = wifiInterfaceName(); bool hasInterface = QDir().exists(QString::fromLatin1("/sys/class/net/" + ifc)); if (!hasInterface) qCWarning(B2QT_WIFI) << "could not find wifi interface in \"/sys/class/net/\", " "looking for interface named: " << ifc; -#ifdef Q_OS_ANDROID_NO_SDK - if (hasInterface && wifi_load_driver() == 0) { - return true; - } else { - qCWarning(B2QT_WIFI) << "wifi driver is not available"; - return false; - } -#else return hasInterface; -#endif } /*! Returns Wifi interface name. - \note On Android, the Wifi interface name is read from "wifi.interface" - system property. On Linux, it is read from the \c B2QT_WIFI_INTERFACE + Interface name is obtained from the \c B2QT_WIFI_INTERFACE environment variable if it is set, otherwise, the default interface name ("\e{wlan0}") is used. @@ -139,16 +112,7 @@ bool QWifiDevice::wifiSupported() */ QByteArray QWifiDevice::wifiInterfaceName() { - QByteArray ifc; -#ifdef Q_OS_ANDROID_NO_SDK - char interface[PROPERTY_VALUE_MAX]; - property_get("wifi.interface", interface, NULL); - ifc = interface[0] == '\0' ? "wlan0" : interface; -#else - ifc = qEnvironmentVariableIsSet("B2QT_WIFI_INTERFACE") - ? qgetenv("B2QT_WIFI_INTERFACE") : "wlan0"; -#endif - return ifc; + return qEnvironmentVariableIsSet("B2QT_WIFI_INTERFACE") ? qgetenv("B2QT_WIFI_INTERFACE") : "wlan0"; } /*! @@ -158,11 +122,7 @@ QByteArray QWifiDevice::wifiInterfaceName() */ void QWifiDevice::setWifiInterfaceName(const QByteArray &name) { -#ifdef Q_OS_ANDROID_NO_SDK - property_set("wifi.interface", name); -#else qputenv("B2QT_WIFI_INTERFACE", name); -#endif } QT_END_NAMESPACE diff --git a/src/wifi/qwifimanager.cpp b/src/wifi/qwifimanager.cpp index 739b399..697af4c 100644 --- a/src/wifi/qwifimanager.cpp +++ b/src/wifi/qwifimanager.cpp @@ -111,20 +111,10 @@ void QWifiManagerPrivate::updateBackendState(QWifiManager::BackendState backendS void QWifiManagerPrivate::updateWifiState() { - bool supplicantRunning = false; -#ifdef Q_OS_ANDROID_NO_SDK - char supplicantState[PROPERTY_VALUE_MAX]; - if (property_get("init.svc.wpa_supplicant", supplicantState, 0)) { - if (strcmp(supplicantState, "running") == 0) - supplicantRunning = true; - } -#else QProcess ps; ps.start(QStringLiteral("ps")); ps.waitForFinished(); - if (ps.readAll().contains("wpa_supplicant")) - supplicantRunning = true; -#endif + bool supplicantRunning = ps.readAll().contains("wpa_supplicant"); if (supplicantRunning && m_wifiController->resetSupplicantSocket()) m_backendState = QWifiManager::Running; } @@ -137,12 +127,6 @@ QString QWifiManagerPrivate::call(const QString &command) char data[2048]; size_t len = sizeof(data) - 1; // -1: room to add a 0-terminator QString actualCommand = command; -#ifdef Q_OS_ANDROID_NO_SDK -#if !(Q_ANDROID_VERSION_MAJOR == 4 && Q_ANDROID_VERSION_MINOR < 4) - QString prefix = QLatin1String("IFNAME=" + m_interface + " "); - actualCommand.prepend(prefix); -#endif -#endif qCDebug(B2QT_WIFI) << "call command: " << actualCommand.toLocal8Bit(); if (q_wifi_command(m_interface, actualCommand.toLocal8Bit(), data, &len) < 0) { qCDebug(B2QT_WIFI) << "call to supplicant failed!"; @@ -371,7 +355,7 @@ void QWifiManager::setScanning(bool scanning) emit scanningChanged(d->m_scanning); if (d->m_scanning) { d->call(QStringLiteral("SCAN")); - // ### TODO android has property for this - wifi.supplicant_scan_interval + // ### TODO expose this with a property d->m_scanTimer = startTimer(5000); } else { killTimer(d->m_scanTimer); diff --git a/src/wifi/wifi.pro b/src/wifi/wifi.pro index f248774..e674c95 100644 --- a/src/wifi/wifi.pro +++ b/src/wifi/wifi.pro @@ -17,7 +17,8 @@ HEADERS += \ $$PWD/qwificontroller_p.h \ $$PWD/qwifidevice.h \ $$PWD/qwificonfiguration.h \ - $$PWD/qwifiutils_p.h + $$PWD/qwifiutils_p.h \ + $$PWD/qwifielinux_p.h SOURCES += \ $$PWD/qwifimanager.cpp \ @@ -26,20 +27,12 @@ SOURCES += \ $$PWD/qwificontroller.cpp \ $$PWD/qwifidevice.cpp \ $$PWD/qwificonfiguration.cpp \ - $$PWD/qwifiutils.cpp + $$PWD/qwifiutils.cpp \ + $$PWD/qwifielinux.cpp \ + $$[QT_SYSROOT]/usr/include/wpa-supplicant/wpa_ctrl.c \ + $$[QT_SYSROOT]/usr/include/wpa-supplicant/os_unix.c -android: { - LIBS += -lhardware_legacy -lcutils -} else { - DEFINES += \ - CONFIG_CTRL_IFACE \ - CONFIG_CTRL_IFACE_UNIX +DEFINES += \ + CONFIG_CTRL_IFACE \ + CONFIG_CTRL_IFACE_UNIX - HEADERS += \ - $$PWD/qwifielinux_p.h - - SOURCES += \ - $$PWD/qwifielinux.cpp \ - $$[QT_SYSROOT]/usr/include/wpa-supplicant/wpa_ctrl.c \ - $$[QT_SYSROOT]/usr/include/wpa-supplicant/os_unix.c -} -- cgit v1.2.3