Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement done on WPS #50

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 60 additions & 43 deletions NetworkManagerGnomeWIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,6 @@ namespace WPEFramework
void wifiManager::wpsAction()
{
FILE *fp = nullptr;
std::ifstream configFile(WPA_SUPPLICANT_CONF);
std::string line = "";
std::string securityPattern = "key_mgmt=";
std::string ssidPattern = "ssid=";
Expand All @@ -960,8 +959,10 @@ namespace WPEFramework
gboolean wpsConnect = false;
struct timespec startTime = {}, endTime = {};
long timeDiff = 0;
int count = 0;
bool ssidFound = false;

if (!g_main_context_acquire(wpsContext))
if (!wpsContext || !g_main_context_acquire(wpsContext))
{
NMLOG_ERROR("Failed to acquire wpsContext");
return;
Expand Down Expand Up @@ -993,13 +994,16 @@ namespace WPEFramework
{
wpaCliResult += buffer.data();
}
pclose(fp);
wpsConnect = (wpaCliResult.find("wpa_state=COMPLETED") != std::string::npos);
clock_gettime(CLOCK_MONOTONIC, &endTime);
timeDiff = (endTime.tv_sec - startTime.tv_sec);
NMLOG_DEBUG("Time elapsed before getting wifi connected = %ld", timeDiff);
if(wpsConnect || timeDiff > 120)
{
NMLOG_INFO("wpsConnect is %d", wpsConnect);
break;
pclose(fp);
}
sleep(5);
}

Expand All @@ -1010,7 +1014,9 @@ namespace WPEFramework
return;
}

while(count < 2 && !ssidFound)
{
std::ifstream configFile(WPA_SUPPLICANT_CONF);
if (!configFile.is_open())
{
NMLOG_ERROR("WPS connected with an SSID but not able to fetch IP address");
Expand All @@ -1023,20 +1029,6 @@ namespace WPEFramework
{
size_t pos;

// Fetch security value
pos = line.find(securityPattern);
if (pos != std::string::npos)
{
pos += securityPattern.length();
size_t end = line.find(' ', pos);
if (end == std::string::npos)
{
end = line.length();
}
security = line.substr(pos, end - pos);
continue;
}

// Fetch ssid value
pos = line.find(ssidPattern);
if (pos != std::string::npos)
Expand All @@ -1048,37 +1040,61 @@ namespace WPEFramework
end = line.length();
}
ssid = line.substr(pos + 1, end - pos - 1);
ssidFound = true;
NMLOG_INFO("SSID found");
continue;
}

// Fetch passphare value
pos = line.find(passphrasePattern);
if (pos != std::string::npos)
{
pos += passphrasePattern.length();
size_t end = line.find('"', pos + 1);
if (end == std::string::npos)
if (ssidFound) {
// Fetch security value
pos = line.find(securityPattern);
if (pos != std::string::npos)
{
end = line.length();
pos += securityPattern.length();
size_t end = line.find(' ', pos);
if (end == std::string::npos)
{
end = line.length();
}
security = line.substr(pos, end - pos);
continue;
}

// Fetch passphare value
pos = line.find(passphrasePattern);
if (pos != std::string::npos)
{
pos += passphrasePattern.length();
size_t end = line.find('"', pos + 1);
if (end == std::string::npos)
{
end = line.length();
}
passphrase = line.substr(pos + 1, end - pos - 1);
}
passphrase = line.substr(pos + 1, end - pos - 1);
continue;
}
}

if(ssid.empty())
{
count++;
NMLOG_INFO("SSID not found retrying with 5 sec delay");
sleep(5);
}
else
{
wifiData.ssid = ssid;
wifiData.passphrase = passphrase;
if(security == "WPA-PSK")
wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES;
else if(security == "WPA2-PSK")
wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES;
if(this->wifiConnect(wifiData))
NMLOG_INFO("WPS connected successfully");
else
NMLOG_ERROR("WPS connect failed");/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */
}
configFile.close();
wifiData.ssid = ssid;
wifiData.passphrase = passphrase;
if(security == "WPA-PSK")
wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES;
else if(security == "WPA2-PSK")
wifiData.security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES;
}
if(this->wifiConnect(wifiData))
NMLOG_INFO("WPS connected successfully");
else
NMLOG_ERROR("WPS connect failed");/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */

}
g_main_context_pop_thread_default(wpsContext);
g_main_context_release(wpsContext);
if (wpsContext) {
Expand All @@ -1090,14 +1106,15 @@ namespace WPEFramework

bool wifiManager::initiateWPS()
{
if (!createClientNewConnection())
return false;

if(!wpsContext){
if (wpsThread.joinable()) {
wpsThread.join();
}
wpsContext = g_main_context_new();
if (!wpsContext) {
NMLOG_ERROR("Failed to create new main context for WPS");
return false;
}
wpsThread = std::thread(&wifiManager::wpsAction, this);
}
else
Expand Down
Loading