#include "WifiInterface.h" // Qt #include #include #include #include // Project #include "main.h" #include "Logger.h" #include "StorageGlobals.h" using namespace Storage; WifiInterface::WifiInterface(QObject *parent) : QObject(parent) { _processScan.setParent(this); _processDisconnectNetwork.setParent(this); _processReadIPSettings.setParent(this); _processReadGateway.setParent(this); _processReadDNS.setParent(this); _processGenerateWPASupplicant.setParent(this); _processStartWPASupplicant.setParent(this); _processRequestAutoAssignedIP.setParent(this); _processSetStaticIPAddress.setParent(this); _processSetStaticGateway.setParent(this); _processSetStaticSubnetMask.setParent(this); _processSetDNS.setParent(this); if (hasConnectedToWifi()) rejoinLastWifiNetwork(); } /*! * \brief WifiInterface::hasConnectedToWifi * Checks whether we have previously connected to a wifi network * \return (bool) true if we have, false otherwise */ bool WifiInterface::hasConnectedToWifi() { if (WifiSettings.value(WifiSettings_MacAddress).toString().isEmpty()) return false; if (!QFile(_wpaSupplicantConfPath).exists()) return false; return true; } /*! * \brief WifiInterface::getLastWifiNetworkConnectedTo * Gets the last network we connected to from settings * \return (Network) the network we last connected to */ Network WifiInterface::getLastWifiNetworkConnectedTo() { QString macAddress = WifiSettings.value(WifiSettings_MacAddress).toString(); QString ssid = WifiSettings.value(WifiSettings_SSID).toString(); QList securityTypesVar = WifiSettings.value(WifiSettings_Security_Types).toList(); Network::STATUS status = Network::STATUS::NOT_CONNECTED; Network::SIGNAL_LEVEL signalLevel = Network::SIGNAL_LEVEL::NO_SIGNAL; _useDHCP = WifiSettings.value(WifiSettings_UseDHCP).toBool(); return Network(macAddress, ssid, variantListToSecurityTypes(securityTypesVar), status, signalLevel); } /*! * \brief WifiInterface::rejoinLastWifiNetwork * Attemps to rejoin the network we last connected to */ void WifiInterface::rejoinLastWifiNetwork() { _network = getLastWifiNetworkConnectedTo(); if (_processStartWPASupplicant.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processStartWPASupplicant.objectName())); return; } _processStartWPASupplicant.start(Wifi_Start_WPA_Supplicant, QStringList() << _iface << _wpaSupplicantConfPath); } void WifiInterface::initConnections() { connect(this, SIGNAL(didStatusChanged(const QString)), this, SLOT(onStatusChanged(const QString))); connect(this, SIGNAL(didError(const QString)), this, SLOT(onStatusChanged(const QString))); connect(&_processScan, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedScan(int,QProcess::ExitStatus))); connect(&_processReadIPSettings, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedReadIP(int,QProcess::ExitStatus))); connect(&_processReadGateway, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedReadGateway(int,QProcess::ExitStatus))); connect(&_processReadDNS, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedReadDNS(int,QProcess::ExitStatus))); connect(&_processGenerateWPASupplicant, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedGenerateWPASupplicant(int,QProcess::ExitStatus))); connect(&_processStartWPASupplicant, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedStartWPASupplicant(int,QProcess::ExitStatus))); connect(&_processRequestAutoAssignedIP, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedRequestAutoAssignedIP(int,QProcess::ExitStatus))); connect(&_processDisconnectNetwork, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedDisconnectNetwork(int,QProcess::ExitStatus))); connect(this, SIGNAL(didRequestAutoAssignedIP()), this, SLOT(onRequestAutoAssignedIP())); // static ip address connections connect(&_processSetStaticIPAddress, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedSetIPAddress(int,QProcess::ExitStatus))); connect(&_processSetStaticGateway, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedSetGateway(int,QProcess::ExitStatus))); connect(&_processSetStaticSubnetMask, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedSetSubnetMask(int,QProcess::ExitStatus))); connect(&_processSetDNS, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinishedSetDNS(int,QProcess::ExitStatus))); } /*! * \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(); } // coco end /*! * \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()); } // 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; initConnections(); LOG_EVENT("UI," + tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief WifiInterface::quit * Called when the application is exiting. */ void WifiInterface::onQuit() { quitThread(); // verified } /*! * \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() { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); if (_scanRunning) { LOG_DEBUG("Wifi network scan is already running."); return; } LOG_DEBUG("Scanning for Wifi Access Points..."); if (_processScan.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processScan.objectName())); return; } _processScan.setWorkingDirectory(Wifi_Scripts_Dir); _scanRunning = true; emit didScanStatusChanged(_scanRunning); _processScan.start(Wifi_Scan_For_Networks, QStringList() << _iface); LOG_DEBUG(QString("WifiInterface::%1 Finished").arg(__FUNCTION__)); } /*! * \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::onProcessFinishedScan(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitCode) Q_UNUSED(vExitStatus) QString out = _processScan.readAllStandardOutput(); QString err = _processScan.readAllStandardError(); LOG_DEBUG(QString("stdout: %1").arg(out)); LOG_DEBUG(QString("stderr: %1").arg(err)); _scanRunning = false; emit didScanStatusChanged(_scanRunning); parseWifiScan(out); LOG_DEBUG(QString("WifiInterface: %1 Finished").arg(__FUNCTION__)); } /*! * \brief getTextBetweenDelimiters * Gets the text between two delimiters * \param vText - (QString) the text to search * \param vLeftDelim - (QString) the left delimiter * \param vRightDelim - (QString) the right delimiter * \return (QString) the text between the delimiters if found, "" otherwise */ QString WifiInterface::getTextBetweenDelimiters(const QString &vText, const QString &vLeftDelim, const QString &vRightDelim) { QString result = ""; QString rightHalf = ""; QStringList tokenList = vText.split(vLeftDelim); if (tokenList.length() > 1) rightHalf = tokenList[1]; tokenList = rightHalf.split(vRightDelim); if (tokenList.length() > 1) result = tokenList[0]; return result; } /*! * \brief Network::parseWifiScan * Extract desired information from the wifi scan output. Sorts by signal stength * * \param vOutput - (QString) output collected from QProcess execution */ QList WifiInterface::parseWifiScan(const QString &vOutput) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); QList networks; QString outputNoReturns = vOutput; outputNoReturns = outputNoReturns.replace("\r", ""); QStringList temp = outputNoReturns.split("Cell"); const QString searchTermSignalLevel = "Signal level="; const QString searchTermMacAddress = "Address:"; const QString searchTermSSID = "ESSID:"; for (const QString &cell : temp) { if (cell.contains(searchTermSSID) && cell.contains(searchTermMacAddress) && cell.contains(searchTermSignalLevel)) { QString ssid = getTextBetweenDelimiters(cell, searchTermSSID, "\n").replace("\"", "").trimmed(); if (ssid == "") continue; QString macAddress = getTextBetweenDelimiters(cell, searchTermMacAddress, "\n").trimmed(); int signalLevel = getTextBetweenDelimiters(cell, searchTermSignalLevel, "dBm").trimmed().toInt(); QList securityTypes; bool requiresKey = QRegularExpression("Encryption key:on").match(cell).hasMatch(); QStringList infoElement = cell.split("IE: "); if (infoElement.length() > 1) { QString element = infoElement[1]; bool isWPA2 = QRegularExpression(".*WPA2 ").match(element).hasMatch(); bool isWPA = QRegularExpression(".*WPA ").match(element).hasMatch(); bool supportsAES = QRegularExpression("Pairwise Ciphers.*CCMP").match(element).hasMatch(); bool requiresPSK = QRegularExpression("Authentication Suites.*PSK").match(element).hasMatch(); bool supportsTKIP = QRegularExpression("Pairwise Ciphers.*TKIP").match(element).hasMatch(); Network::SECURITY_TYPE securityType = Network::SECURITY_TYPE::UNSUPPORTED; if (isWPA2 && requiresPSK && supportsAES && !supportsTKIP) securityType = Network::SECURITY_TYPE::WPA2_AES; else if (isWPA && requiresPSK && supportsAES && supportsTKIP) securityType = Network::SECURITY_TYPE::WPA_TKIP_AES; else if (isWPA && requiresPSK && supportsTKIP) securityType = Network::SECURITY_TYPE::WPA_TKIP; else if (isWPA && requiresPSK && supportsAES) securityType = Network::SECURITY_TYPE::WPA_AES; else if (!isWPA && !isWPA2 && !requiresPSK && !supportsAES && !supportsTKIP && requiresKey) securityType = Network::SECURITY_TYPE::WEP; if (securityType != Network::SECURITY_TYPE::UNSUPPORTED) securityTypes.append(securityType); } Network network(macAddress, ssid, securityTypes, Network::STATUS::NOT_CONNECTED, signalLevel); networks.append(network); emit didAddNetwork(network); } } LOG_DEBUG(QString("WifiInterface::%1 Finished").arg(__FUNCTION__)); return networks; } /*! * \brief WifiInterface::onStatusChanged * Ensures any status changes are logged * \param vMessage (QString) the message detail */ void WifiInterface::onStatusChanged(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("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); _network = vNetwork; _useDHCP = true; WifiSettings.setValue(WifiSettings_UseDHCP, _useDHCP); LOG_DEBUG(QString("Joining Network %1").arg(vNetwork.ssid())); if (_processGenerateWPASupplicant.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processGenerateWPASupplicant.objectName())); return; } QString securityType; if (vNetwork.securityTypes().contains(Network::SECURITY_TYPE::WPA2_AES)) securityType = "wpa2"; else if (vNetwork.securityTypes().contains(Network::SECURITY_TYPE::WPA_TKIP_AES) || vNetwork.securityTypes().contains(Network::SECURITY_TYPE::WPA_AES) || vNetwork.securityTypes().contains(Network::SECURITY_TYPE::WPA_TKIP)) securityType = "wpa"; else if (vNetwork.securityTypes().contains(Network::SECURITY_TYPE::WEP)) securityType = "wep"; else { LOG_DEBUG(QString("Network %1 has an unsupported security type.").arg(vNetwork.ssid())); emit didError(QString("Network %1 has an unsupported security type.").arg(vNetwork.ssid())); } WifiSettings.setValue(WifiSettings_SSID, vNetwork.ssid()); WifiSettings.setValue(WifiSettings_MacAddress, vNetwork.macAddress()); WifiSettings.setValue(WifiSettings_ConfPath, _wpaSupplicantConfPath); WifiSettings.setValue(WifiSettings_Security_Types, securityTypesToStringList(vNetwork.securityTypes())); _processGenerateWPASupplicant.start(Wifi_Generate_WPA_Supplicant, QStringList() << vNetwork.ssid() << vPassword << _wpaSupplicantConfPath << securityType); } /*! * \brief WifiInterface::onProcessFinishedGenerateWPASupplicant * Called when the process that generates the WPA supplicant file has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedGenerateWPASupplicant(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError("Could not configure network."); return; } if (_processStartWPASupplicant.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processStartWPASupplicant.objectName())); return; } _processStartWPASupplicant.start(Wifi_Start_WPA_Supplicant, QStringList() << _iface << _wpaSupplicantConfPath); } /*! * \brief WifiInterface::onProcessFinishedStartWPASupplicant * Called when the process that starts the WPA supplicant has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedStartWPASupplicant(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError("Could not configure network."); return; } emit didRequestAutoAssignedIP(); } /*! * \brief WifiInterface::onRequestAutoAssignedIP * Handles the request for an auto assigned IP address */ void WifiInterface::onRequestAutoAssignedIP() { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); if (_processRequestAutoAssignedIP.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processRequestAutoAssignedIP.objectName())); return; } emit didStatusChanged(tr("Obtaining IP Address...")); _processRequestAutoAssignedIP.start(Wifi_Get_Auto_Assigned_IP, QStringList() << _iface); } /*! * \brief WifiInterface::onProcessFinishedRequestAutoAssignedIP * Called when the process that requests an auto-assigned IP has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedRequestAutoAssignedIP(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitStatus) if (vExitCode != 0) { if (_dhcpAttemptsLeft > 0) { _dhcpAttemptsLeft--; emit didRequestAutoAssignedIP(); } else { emit didError(QString("Could not connect to %1").arg(_network.ssid())); _dhcpAttemptsLeft = 5; } return; } _dhcpAttemptsLeft = 5; doRequestIPSettings(); } /*! * \brief WifiInterface::doRequestIPSettings * Handles a request to read the IP settings of the device. */ void WifiInterface::doRequestIPSettings() { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); if (_processReadIPSettings.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processReadIPSettings.objectName())); return; } _processReadIPSettings.start(Wifi_Read_IP_Settings, QStringList() << _iface); } /*! * \brief WifiInterface::onProcessFinishedReadIP * Parses the IP settings of the device when the process has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedReadIP(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitCode); Q_UNUSED(vExitStatus); QString output = _processReadIPSettings.readAllStandardOutput(); _network.mIPSettings.mIPAddress = parseIP(output); _network.mIPSettings.mBroadcast = parseBroadcast(output); _network.mIPSettings.mSubnetMask = parseSubnetMask(output); if (_processReadGateway.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processReadGateway.objectName())); return; } _processReadGateway.start(Wifi_Read_Gateway); } /*! * \brief WifiInterface::onProcessFinishedReadGateway * Parses the gateway of the device when the process has finished then * initiates reading the DNS * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedReadGateway(int vExitCode, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); Q_UNUSED(vExitCode); Q_UNUSED(vExitStatus); _network.mIPSettings.mGateway = parseGateway(_processReadGateway.readAllStandardOutput()); if (_processReadDNS.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processReadDNS.objectName())); return; } _processReadDNS.start(Wifi_Read_DNS); } /*! * \brief WifiInterface::onProcessFinishedReadDNS * Parses the DNS of the device when the process has finished then * emits the newtwork with all the ip address information filled * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedReadDNS(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitCode); Q_UNUSED(vExitStatus); _network.mIPSettings.mDNS = parseDNS(_processReadDNS.readAllStandardOutput()); if (!_useDHCP) { // Use current static IP settings QString ipAddress = WifiSettings.value(WifiSettings_IPAddress).toString().trimmed(); QString gateway = WifiSettings.value(WifiSettings_Gateway).toString().trimmed(); QString subnetMask = WifiSettings.value(WifiSettings_SubnetMask).toString().trimmed(); QString dns = WifiSettings.value(WifiSettings_DNS).toString().trimmed(); if (!ipAddress.isEmpty()) doRequestSetIPAddress(_network.mIPSettings.mIPAddress); if (!gateway.isEmpty()) doRequestSetGateway(_network.mIPSettings.mGateway); if (!subnetMask.isEmpty()) doRequestSetSubnetMask(_network.mIPSettings.mSubnetMask); if (!dns.isEmpty()) doRequestSetDNS(_network.mIPSettings.mDNS); } emit didConnectToNetwork(_network); } /*! * \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("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); LOG_DEBUG(QString("Disconnecting from Network %1").arg(vNetwork.ssid())); if (_processDisconnectNetwork.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processDisconnectNetwork.objectName())); return; } _processDisconnectNetwork.start(Wifi_Disconnect_Network, QStringList() << _iface); } /*! * \brief WifiInterface::onProcessFinishedDisconnectNetwork * Called when we disconnect from the wifi network * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedDisconnectNetwork(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError(tr("Failed to disconnect from %1").arg(_network.ssid())); return; } emit didDisconnectNetwork(_network); _network.clearSettings(); } /*! * \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) { const QString leftDelim = "inet addr:"; const QString rightDelim = "Bcast:"; return getTextBetweenDelimiters(vOutput, leftDelim, rightDelim); } /*! * \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) { const QString leftDelim = "Bcast:"; const QString rightDelim = "Mask:"; return getTextBetweenDelimiters(vOutput, leftDelim, rightDelim); } /*! * \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) { const QString leftDelim = "Mask:"; const QString rightDelim = "\n"; return getTextBetweenDelimiters(vOutput, leftDelim, rightDelim); } /*! * \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) { const QString leftDelim = "default via"; const QString rightDelim = "dev"; return getTextBetweenDelimiters(vOutput, leftDelim, rightDelim); } /*! * \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) { const QString leftDelim = "nameserver"; const QString rightDelim = "\n"; return getTextBetweenDelimiters(vOutput, leftDelim, rightDelim); } /*! * \brief WifiInterface::doRequestSetIPAddress * Issues command to set a static IP address * \param vIPAddress - (QString) the IP address */ void WifiInterface::doRequestSetIPAddress(const QString &vIPAddress) { Q_UNUSED(vIPAddress) LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); LOG_DEBUG(QString("Handling request to set static IP address: %1").arg(vIPAddress)); if (_processSetStaticIPAddress.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processSetStaticIPAddress.objectName())); return; } WifiSettings.setValue(WifiSettings_IPAddress, vIPAddress); _useDHCP = false; WifiSettings.setValue(WifiSettings_UseDHCP, _useDHCP); _processSetStaticIPAddress.start(Wifi_Set_Static_IP, QStringList() << _iface << vIPAddress); } /*! * \brief WifiInterface::onProcessFinishedSetIPAddress * Called when settings a static IP address finishes * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedSetIPAddress(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError(tr("Failed to set static IP Address")); return; } emit didSetStaticIPAddress(); } /*! * \brief WifiInterface::doRequestSetGateway * Issues command to set a static gateway * \param vGateway - (QString) the gateway */ void WifiInterface::doRequestSetGateway(const QString &vGateway) { Q_UNUSED(vGateway) LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); LOG_DEBUG(QString("Handling request to set static gateway: %1").arg(vGateway)); if (_processSetStaticGateway.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processSetStaticGateway.objectName())); return; } WifiSettings.setValue(WifiSettings_Gateway, vGateway); _useDHCP = false; WifiSettings.setValue(WifiSettings_UseDHCP, _useDHCP); _processSetStaticGateway.start(Wifi_Set_Gateway, QStringList() << vGateway); } /*! * \brief WifiInterface::onProcessFinishedSetGateway * Called when the the process to set the gateway has finished * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedSetGateway(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError(tr("Failed to set gateway.")); return; } emit didSetGateway(); } /*! * \brief WifiInterface::doRequestSetSubnetMask * Issues command to set the subnet mask * \param vSubnetMask - (QString) the subnet mask */ void WifiInterface::doRequestSetSubnetMask(const QString &vSubnetMask) { Q_UNUSED(vSubnetMask) LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); LOG_DEBUG(QString("Handling request to set static subnet mask: %1").arg(vSubnetMask)); if (_processSetStaticSubnetMask.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processSetStaticSubnetMask.objectName())); return; } WifiSettings.setValue(WifiSettings_SubnetMask, vSubnetMask); _useDHCP = false; WifiSettings.setValue(WifiSettings_UseDHCP, _useDHCP); _processSetStaticSubnetMask.start(Wifi_Set_SubnetMask, QStringList() << _iface << vSubnetMask); } /*! * \brief WifiInterface::onProcessFinishedSetSubnetMask * Called when the process to set the subnet mask finishes * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedSetSubnetMask(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError(tr("Failed to set subnet mask.")); return; } emit didSetSubnetMask(); } /*! * \brief WifiInterface::doRequestSetDNS * Issues a command to set the DNS * \param vDNS - (QString) the DNS */ void WifiInterface::doRequestSetDNS(const QString &vDNS) { LOG_DEBUG(QString("WifiInterface::%1 %2").arg(__FUNCTION__).arg(QThread::currentThread()->objectName())); LOG_DEBUG(QString("Handling request to set DNS: %1").arg(vDNS)); if (_processSetDNS.state() != QProcess::NotRunning) { LOG_DEBUG(QString("Rejecting request for %1. %2 is already running.") .arg(__FUNCTION__) .arg(_processSetDNS.objectName())); return; } WifiSettings.setValue(WifiSettings_DNS, vDNS); _processSetDNS.start(Wifi_Set_DNS, QStringList() << vDNS); } /*! * \brief WifiInterface::onProcessFinishedSetDNS * Called when the process that finishes setting the DNS completes * \param vExitCode - (int) the exit code * \param vExitStatus - (QProcess::ExitStatus) the exit status */ void WifiInterface::onProcessFinishedSetDNS(int vExitCode, QProcess::ExitStatus vExitStatus) { Q_UNUSED(vExitStatus) if (vExitCode != 0) { emit didError(tr("Failed to set DNS.")); return; } emit didSetDNS(); } /*! * \brief WifiInterface::securityTypesToStringList * Converts a QList of network security types to a QStringList * \param securityTypes (QList) the list of security type * \return (QStringList) the security types as a string list */ QStringList WifiInterface::securityTypesToStringList(const QList &securityTypes) { QStringList securityTypesStrList; for (const Network::SECURITY_TYPE &type : securityTypes) { securityTypesStrList.append(QString("%1").arg(type)); } return securityTypesStrList; } /*! * \brief variantListToSecurityTypes * Converts a QVariantList of security types to a QList * \param securityTypesVar (QVariantList) security types * \return (QList) the list of network security types */ QList WifiInterface::variantListToSecurityTypes(const QList &securityTypesVar) { QList securityTypes; for (const QVariant &type : securityTypesVar) { bool ok = false; Network::SECURITY_TYPE val = Network::SECURITY_TYPE(type.toInt(&ok)); if (ok) securityTypes.append(val); } return securityTypes; }