#include "WifiInterface.h" // Qt #include #include #include // Project #include "main.h" #include "Logger.h" #include "StorageGlobals.h" using namespace Storage; WifiInterface::WifiInterface(QObject *parent) : QObject(parent) {} void WifiInterface::onInitConnections() { connect(this, SIGNAL(didError(const QString)), this, SLOT(onLogFailure(const QString))); connect(&_processScan, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onScanFinished(int,QProcess::ExitStatus))); // connect(&_scanWatcher, SIGNAL(finished()), // this , SLOT(onFinishedScan())); connect(this, SIGNAL(didScan()), this, SLOT(onScan())); } /*! * \brief WifiInterface::init * \details Initialized the Class by calling the init() method first * And initializes the thread vThread by calling initThread * on success init(). * \param vThread - the thread * \return returns the return value of the init() method */ bool WifiInterface::init(QThread &vThread) { if (!init()) return false; initThread(vThread); return true; } /*! * \brief ApplicationController::initThread * \details Moves this object into the thread vThread. * And checks that this method is called from main thread. * Also connects quitThread to application aboutToQuit. * \param vThread - the thread */ void WifiInterface::initThread(QThread &vThread) { // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); _thread = &vThread; _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); _thread->start(); moveToThread(_thread); } /*! * \brief MessageAcknowModel::quit * \details quits the class * Calls quitThread */ void WifiInterface::quit() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. quitThread(); // validated } /*! * \brief MessageAcknowModel::quitThread * \details Moves this object to main thread to be handled by QApplicaiton * And to be destroyed there. */ void WifiInterface::quitThread() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. if (!_thread) return; // runs in thread moveToThread(qApp->thread()); // validated } // coco end /*! * \brief WifiInterface::init * \details Initializes the class by setting the connections * \return true on first initialization, false if it has already been initialized */ bool WifiInterface::init() { if (_init) return false; _init = true; onInitConnections(); LOG_EVENT("UI," + tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief WifiInterface::quit * Called when the application is exiting. */ void WifiInterface::onQuit() { onQuitThread(); // verified } /*! * \brief WifiInterface::quitThread * \details Moves this object to main thread to be handled by QApplicaiton * It will also be destroyed there. */ void WifiInterface::onQuitThread() { if (!_thread) return; moveToThread(qApp->thread()); } /*! * \brief WifiInterface::timerEvent * Built in QObject timer * \param event (QTimerEvent*) - the event timer */ void WifiInterface::timerEvent(QTimerEvent *event) { Q_UNUSED(event); } /*! * \brief WifiInterface::doScan * Scans for Wifi Access Points */ void WifiInterface::doScan() { qDebug() << __FUNCTION__ << QThread::currentThread()->objectName(); emit didScan(); } void WifiInterface::onScan() { qDebug() << __FUNCTION__ << QThread::currentThread()->objectName(); if (_scanRunning) { LOG_DEBUG("Wifi network scan is already running."); return; } LOG_DEBUG("Scanning for Wifi Access Points..."); _processScan.setWorkingDirectory(Wifi_Scripts_Dir); _scanRunning = true; emit didScanStatusChanged(_scanRunning); _processScan.start(Wifi_Scan_For_Networks); } /*! * \brief WifiInterface::onScanFinished * Called when the scan process has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onScanFinished(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitCode) Q_UNUSED(vExitStatus) // QString out = _processScan.readAllStandardOutput(); // QString err = _processScan.readAllStandardError(); // LOG_DEBUG(out); // LOG_DEBUG(err); // _scanRunning = false; // emit didScanStatusChanged(_scanRunning); // onParseWifiScan(out); } /*! * \brief Network::onParseWifiScan * Extract desired information from the wifi scan output. Sorts by signal stength * * \param vOutput - (QString) output collected from QProcess execution */ void WifiInterface::onParseWifiScan(const QString &vOutput) { QList networks; QStringList temp = vOutput.split("Cell"); const QString signalLevelSearchTerm = "Signal level="; const QString macAddressSearchTerm = "Address:"; const QString ssidSearchTerm = "ESSID:"; const QString groupCipherSearchTerm = "Group Cipher"; const QString authSuitesSearchTerm = "Authentication Suites"; for (const QString &line : temp) { if (line.contains(ssidSearchTerm) && line.contains(macAddressSearchTerm) && line.contains(signalLevelSearchTerm)) { QString ssid = line.split(ssidSearchTerm)[1].split("\n")[0].replace("\"", "").trimmed(); if (ssid == "") continue; QString macAddress = line.split(macAddressSearchTerm)[1].split("\n")[0].trimmed(); int signalLevel = line.split(signalLevelSearchTerm)[1].split("dBm")[0].trimmed().toInt(); bool isCCMP = line.split(groupCipherSearchTerm)[1].split("\n")[0].contains("CCMP"); bool isEnterprise = line.split(authSuitesSearchTerm)[1].split("\n")[0].contains("802.1x"); bool isPersonal = line.split(authSuitesSearchTerm)[1].split("\n")[0].contains("PSK"); Network::SECURITY_LEVEL securityLevel = Network::SECURITY_LEVEL::UNSUPPORTED; if (isPersonal && isCCMP) securityLevel = Network::SECURITY_LEVEL::WPA2_AES_PERSONAL; else if (isEnterprise && isCCMP) securityLevel = Network::SECURITY_LEVEL::WPA2_AES_ENTERPRISE; // TODO: Add support for the other security levels Network network(macAddress,ssid, securityLevel, Network::STATUS::NOT_CONNECTED, signalLevel); emit didAddNetwork(network); } } } /*! * \brief WifiInterface::onLogFailure * Ensures any failures reported are logged * \param vMessage (QString) the message detail of the failure */ void WifiInterface::onLogFailure(const QString &vMessage) { LOG_DEBUG(vMessage); } /*! * \brief WifiInterface::doJoinNetwork * Handles request to join a network * \param vMacAddress - (QString) the mac address of the network to join * \param vPassword - (QString) the password for the network provided by the user */ void WifiInterface::doJoinNetwork(const Network &vNetwork, const QString &vPassword) { LOG_DEBUG(QString("Joining Network %1").arg(vNetwork.ssid())); if (!generateWPASupplicant(vNetwork, vPassword)) { emit didError("Could not configure network."); return; } if (!startWPASupplicant()) { emit didError("Could not configure network."); return; } // TODO: Add option to setup with static IP settings instead if (!requestAutoAssignedIP()) { emit didError(QString("Could not connect to %1").arg(vNetwork.ssid())); return; } Network network = vNetwork; network.ipSettings(getIPSettings()); emit didConnectToNetwork(network); } /*! * \brief WifiInterface::getIPSettings * Gets the IP settings from the device. If an IP address * is not found, all other IP settings are left blank as well. */ Network::IPSettings WifiInterface::getIPSettings() { Network::IPSettings ipSettings; QString output = readIPSettings(); ipSettings.mIPAddress = parseIP(output); if (ipSettings.mIPAddress == "") { return ipSettings; } ipSettings.mBroadcast = parseBroadcast(output); ipSettings.mSubnetMask = parseSubnetMask(output); ipSettings.mGateway = readGateway(); ipSettings.mDns = readDNS(); return ipSettings; } /*! * \brief WifiInterface::doRequestIPSettings * Handles a request to read the IP settings of the device. */ void WifiInterface::doRequestIPSettings() { emit didGetIPSettings(getIPSettings()); } /*! * \brief WifiInterface::doDisconnectNetwork * Disconnects from the specified network * \param vNetwork - (Network) the network to disconnect from */ void WifiInterface::doDisconnectNetwork(const Network &vNetwork) { LOG_DEBUG(QString("Disconnecting from Network %1").arg(vNetwork.ssid())); QProcess process; process.start(Wifi_Disconnect_Network, QStringList() << _iface); if (process.waitForFinished(_defaultTimeout)) emit didDisconnectNetwork(vNetwork); process.kill(); emit didError(tr("Failed to disconnect from %1").arg(vNetwork.ssid())); } /*! * \brief WifiInterface::generateWPASupplicant * Generates the WPA Supplicant configuration file * \param vNetwork (Network) the network w/ ssid we want to connect to * \param vPassword (QString) the password the user entered for this network * \return (bool) true if writing the file completed, false otherwise */ bool WifiInterface::generateWPASupplicant(const Network &vNetwork, const QString &vPassword) { LOG_DEBUG("Generating WPA Supplicant..."); QProcess process; process.start(Wifi_Generate_WPA_Supplicant, QStringList() << vNetwork.ssid() << vPassword << _wpaSupplicantConfPath); if (!process.waitForFinished(_defaultTimeout)) { process.kill(); return false; } return true; } /*! * \brief WifiInterface::startWPASupplicant * Starts WPA supplicant in the background. Assumes the conf file has already been written * \return true if successful, false on timeout */ bool WifiInterface::startWPASupplicant() { LOG_DEBUG("Starting wpa supplicant..."); QProcess process; process.start(Wifi_Start_WPA_Supplicant, QStringList() << _iface << _wpaSupplicantConfPath); if (!process.waitForFinished(_defaultTimeout)) { process.kill(); return false; } return true; } /*! * \brief WifiInterface::requestAutoAssignedIP * Requests an auto-assigned IP addressed * \return true if successful, false on timeout */ bool WifiInterface::requestAutoAssignedIP(int vTries) { LOG_DEBUG(QString("Requesting auto-assigned IP address...")); QProcess process; process.start(Wifi_Get_Auto_Assigned_IP, QStringList() << _iface); if (!process.waitForFinished(_dhcpTimeout)) { if (vTries > 0) return requestAutoAssignedIP(--vTries); return false; } return true; } /*! * \brief WifiInterface::readIPSettings * Reads the IP settings of the device * \return (QString) the unparsed standard output */ QString WifiInterface::readIPSettings() { QString result = ""; QProcess process; process.start(Wifi_Read_IP_Settings, QStringList() << _iface); if (process.waitForFinished(_defaultTimeout)) result = process.readAllStandardOutput(); else process.kill(); LOG_DEBUG(QString("Output with IP address information: %1").arg(result)); return result; } /*! * \brief WifiInterface::parseIP * Parses the IP from the IP address output * \param vOutput - (QString) the console output * \return (QString) the IP Address only */ QString WifiInterface::parseIP(const QString &vOutput) { if (vOutput.contains("inet addr:") && vOutput.contains("Bcast:")) return vOutput.split("inet addr:")[1].split("Bcast:")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseBroadcast * Parses the Broadcast from the IP address output * \param vOutput - (QString) the console output * \return (QString) the Broadcast IP if found, "" otherwise */ QString WifiInterface::parseBroadcast(const QString &vOutput) { if (vOutput.contains("Bcast:") && vOutput.contains("Mask:")) return vOutput.split("Bcast:")[1].split("Mask:")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseSubnetMask * Parses the subnet mask from the provided output * \param vOutput - (QString) the console output * \return (QString) the subnet mask if found, "" otherwise */ QString WifiInterface::parseSubnetMask(const QString &vOutput) { if (vOutput.contains("Mask:")) return vOutput.split("Mask:")[1].trimmed(); return ""; } /*! * \brief WifiInterface::parseGateway * Parses the gateway from the provided output * \param vOutput - (QString) the console output * \return (QString) the gateway if found, "" otherwise */ QString WifiInterface::parseGateway(const QString &vOutput) { if (vOutput.contains("default via")) return vOutput.split("default via")[1].split("dev")[0].trimmed(); return ""; } /*! * \brief WifiInterface::parseDNS * Parses the DNS from the provided output * \param vOutput - (QString) the console output * \return (QString) the first DNS found, "" otherwise */ QString WifiInterface::parseDNS(const QString &vOutput) { if (vOutput.contains("nameserver")) return vOutput.split("nameserver")[1].split("\n")[0].trimmed(); return ""; } /*! * \brief WifiInterface::readGateway * Reads the current gateway * \return (QString) the gateway if found, "" otherwise */ QString WifiInterface::readGateway() { QProcess process; process.start(Wifi_Read_Gateway); if (process.waitForFinished(_defaultTimeout)) return parseGateway(process.readAllStandardOutput()); process.kill(); return ""; } /*! * \brief WifiInterface::readDNS * Reads the DNS setting * \return (QString) the first dns found, "" otherwise */ QString WifiInterface::readDNS() { QProcess process; process.start(Wifi_Read_DNS); if (process.waitForFinished(_defaultTimeout)) return parseDNS(process.readAllStandardOutput()); process.kill(); return ""; }