Index: sources/wifi/WifiInterface.cpp =================================================================== diff -u -r5600d2133dd0ea6dc1f733aa78bd26e2a4892a38 -r2085962f7bd0a2239ee5c857928a11d5e38fe0a2 --- sources/wifi/WifiInterface.cpp (.../WifiInterface.cpp) (revision 5600d2133dd0ea6dc1f733aa78bd26e2a4892a38) +++ sources/wifi/WifiInterface.cpp (.../WifiInterface.cpp) (revision 2085962f7bd0a2239ee5c857928a11d5e38fe0a2) @@ -17,8 +17,11 @@ void WifiInterface::onInitConnections() { - connect(&_scanProcess, SIGNAL(finished(int, QProcess::ExitStatus)), + connect(&_processScan, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onScanFinished(int, QProcess::ExitStatus))); + + connect(this, SIGNAL(didFailToConnect(const QString)), + this, SLOT(onLogFailure(const QString))); } /*! @@ -88,10 +91,10 @@ } LOG_DEBUG("Scanning for Wifi Access Points..."); - _scanProcess.setWorkingDirectory(Wifi_Scripts_Dir); + _processScan.setWorkingDirectory(Wifi_Scripts_Dir); _scanRunning = true; emit didScanStatusChanged(_scanRunning); - _scanProcess.start(Wifi_Scan_For_Networks); + _processScan.start(Wifi_Scan_For_Networks); } @@ -104,8 +107,8 @@ void WifiInterface::onScanFinished(int vPid, QProcess::ExitStatus vExitStatus) { LOG_DEBUG(QString("%1: %2,%3").arg(__FUNCTION__).arg(vPid).arg(vExitStatus)); - QString out = _scanProcess.readAllStandardOutput(); - QString err = _scanProcess.readAllStandardError(); + QString out = _processScan.readAllStandardOutput(); + QString err = _processScan.readAllStandardError(); LOG_DEBUG(out); LOG_DEBUG(err); _scanRunning = false; @@ -117,12 +120,12 @@ * \brief Network::onParseWifiScan * Extract desired information from the wifi scan output. Sorts by signal stength * - * \param output - (QString) output collected from QProcess execution + * \param vOutput - (QString) output collected from QProcess execution */ -void WifiInterface::onParseWifiScan(const QString &output) +void WifiInterface::onParseWifiScan(const QString &vOutput) { QList networks; - QStringList temp = output.split("Cell"); + QStringList temp = vOutput.split("Cell"); const QString signalLevelSearchTerm = "Signal level="; const QString macAddressSearchTerm = "Address:"; const QString ssidSearchTerm = "ESSID:"; @@ -158,21 +161,202 @@ } /*! + * \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())); + _network = vNetwork; + if (!generateWPASupplicant(_network, vPassword)) + { + emit didFailToConnect("Could not configure network."); + return; + } - _wifiInterfaceProcess.start(Wifi_Generate_WPA_Supplicant, + if (!startWPASupplicant()) + { + emit didFailToConnect("Could not configure network."); + return; + } + + // TODO: Add option to setup with static IP settings instead + if (!requestAutoAssignedIP()) + { + emit didFailToConnect("Could not obtain IP Address."); + return; + } + + Network::IPSettings ipSettings; + QString output = readIPSettings(); + ipSettings.mIPAddress = parseIP(output); + ipSettings.mBroadcast = parseBroadcast(output); + ipSettings.mSubnetMask = parseSubnetMask(output); + ipSettings.mGateway = readGateway(); + ipSettings.mDns = readDNS(); + _network.ipSettings(ipSettings); + emit didConnectToNetwork(_network); +} + +/*! + * \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..."); + _processJoinNetwork.start(Wifi_Generate_WPA_Supplicant, QStringList() << vNetwork.ssid() << vPassword << _wpaSupplicantConfPath); + return _processJoinNetwork.waitForFinished(_defaultTimeout); +} - _wifiInterfaceProcess.start(Wifi_Start_WPA_Supplicant, +/*! + * \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..."); + _processJoinNetwork.start(Wifi_Start_WPA_Supplicant, QStringList() << _iface << _wpaSupplicantConfPath); + return _processJoinNetwork.waitForFinished(_defaultTimeout); +} +/*! + * \brief WifiInterface::requestAutoAssignedIP + * Requests an auto-assigned IP addressed + * \return true if successful, false on timeout + */ +bool WifiInterface::requestAutoAssignedIP() +{ + LOG_DEBUG("Requesting auto-assigned IP address..."); + _processJoinNetwork.start(Wifi_Get_Auto_Assigned_IP, + QStringList() << _iface); + return _processJoinNetwork.waitForFinished(_dhcpTimeout); } + +/*! + * \brief WifiInterface::readIPSettings + * Reads the IP settings of the device + * \return (QString) the unparsed standard output + */ +QString WifiInterface::readIPSettings() +{ + QString result = ""; + _processGetIPAddress.start(Wifi_Read_IP_Settings, + QStringList() << _iface); + if (_processGetIPAddress.waitForFinished(_defaultTimeout)) + result = _processGetIPAddress.readAllStandardOutput(); + 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() +{ + _processGetIPAddress.start(Wifi_Read_Gateway); + if (_processGetIPAddress.waitForFinished(_defaultTimeout)) + return parseGateway(_processGetIPAddress.readAllStandardOutput()); + return ""; +} + +/*! + * \brief WifiInterface::readDNS + * Reads the DNS setting + * \return (QString) the first dns found, "" otherwise + */ +QString WifiInterface::readDNS() +{ + _processGetIPAddress.start(Wifi_Read_DNS); + if (_processGetIPAddress.waitForFinished(_defaultTimeout)) + return parseDNS(_processGetIPAddress.readAllStandardOutput()); + return ""; +}