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

RDKE-352 - Modified the WPS API #40

Merged
merged 26 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1c6b4f2
RDKE-352 - Modified the WPS API
Nov 21, 2024
e3070ae
RDKE-352 - Modified the WPS API
Nov 21, 2024
55e7b29
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
c49e328
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
97c1b99
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
b72c480
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
42bbdf5
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
f724d47
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
544d286
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
7648c38
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 22, 2024
3dbb26b
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 23, 2024
abd1e5b
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 23, 2024
a0923f1
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 24, 2024
03c7c95
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 24, 2024
643519e
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 24, 2024
0b8a6e7
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 24, 2024
f7880e6
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
63da6a1
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
71648ba
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
e18425e
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
eb4056b
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
6a4a09c
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
d93a99b
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
562b93b
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
a13ea56
Merge branch 'wps_chg' of github.com:gururaajar/networkmanager into w…
Nov 25, 2024
c520040
Merge branch 'develop' into wps_chg
karuna2git Nov 26, 2024
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
222 changes: 175 additions & 47 deletions NetworkManagerGnomeWIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,22 @@ namespace WPEFramework
return wifiDevice;
}

bool static getConnectedSSID(NMDeviceWifi *wifiDevice, std::string& ssidin)
bool static getConnectedSSID(NMDevice *device, std::string& ssidin)
{
GBytes *ssid;
NMAccessPoint *activeAP = nm_device_wifi_get_active_access_point(wifiDevice);
if(activeAP == NULL) {
return false;
NMDeviceState device_state = nm_device_get_state(device);
if (device_state == NM_DEVICE_STATE_ACTIVATED)
{
NMAccessPoint *activeAP = nm_device_wifi_get_active_access_point(NM_DEVICE_WIFI(device));
ssid = nm_access_point_get_ssid(activeAP);
gsize size;
const guint8 *ssidData = static_cast<const guint8 *>(g_bytes_get_data(ssid, &size));
std::string ssidTmp(reinterpret_cast<const char *>(ssidData), size);
ssidin = ssidTmp;
NMLOG_INFO("connected ssid: %s", ssidin.c_str());
return true;
}

ssid = nm_access_point_get_ssid(activeAP);
gsize size;
const guint8 *ssidData = static_cast<const guint8 *>(g_bytes_get_data(ssid, &size));
std::string ssidTmp(reinterpret_cast<const char *>(ssidData), size);
ssidin = ssidTmp;
NMLOG_INFO("connected ssid: %s", ssidin.c_str());
return true;
return false;
}

static void getApInfo(NMAccessPoint *AccessPoint, Exchange::INetworkManager::WiFiSSIDInfo &wifiInfo)
Expand Down Expand Up @@ -584,7 +585,7 @@ namespace WPEFramework
return false;

std::string activeSSID;
if(getConnectedSSID(NM_DEVICE_WIFI(device), activeSSID))
if(getConnectedSSID(device, activeSSID))
{
if(ssidInfo.ssid == activeSSID)
{
Expand Down Expand Up @@ -944,52 +945,179 @@ namespace WPEFramework
return false;
}

bool wifiManager::initiateWPS()
void wifiManager::wpsAction()
{
Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatch>(Core::ProxyType<Job>::Create([&]() {
const GPtrArray *aps;
int count = 1, wpsConnected = 0;
if(!createClientNewConnection())
FILE *fp = nullptr;
std::ifstream configFile(WPA_SUPPLICANT_CONF);
std::string line = "";
std::string securityPattern = "key_mgmt=";
std::string ssidPattern = "ssid=";
std::string passphrasePattern = "psk=";
std::string security = "", ssid = "", passphrase = "";
Exchange::INetworkManager::WiFiConnectTo wifiData = {};
std::array<char, 128> buffer = {};
std::string wpaCliResult = "";
gboolean wpsConnect = false;
struct timespec startTime = {}, endTime = {};
long timeDiff = 0;

if (!g_main_context_acquire(wpsContext))
{
NMLOG_ERROR("Failed to acquire wpsContext");
return;
}

g_main_context_push_thread_default(wpsContext);

std::string wpaCliCommand = "wpa_cli -i " + std::string(nmUtils::wlanIface()) + " wps_pbc";
fp = popen(wpaCliCommand.c_str(), "r");
if (fp == nullptr)
{
NMLOG_ERROR("WPS failed to connect with the SSID");
g_main_context_pop_thread_default(wpsContext);
g_main_context_release(wpsContext);
return ;
}
pclose(fp);
std::string wpaCliStatus = WPA_CLI_STATUS;
clock_gettime(CLOCK_MONOTONIC, &startTime);
while(!wpsStop.load())
{
fp = popen(wpaCliStatus.c_str(), "r");
if (fp == nullptr)
{
NMLOG_ERROR("WPS not able to fetch the connection status");
continue;
}
while (fgets(buffer.data(), buffer.size(), fp) != nullptr)
{
wpaCliResult += buffer.data();
}
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)
break;
pclose(fp);
sleep(5);
}

if(!wpsConnect)
{
g_main_context_pop_thread_default(wpsContext);
g_main_context_release(wpsContext);/* TODO: Need to disconnect the wpa_cli connection, as the libnm is not aware of the connection created by wpa_cli */
return;
}

{
if (!configFile.is_open())
{
NMLOG_ERROR("WPS connected with an SSID but not able to fetch IP address");
g_main_context_pop_thread_default(wpsContext);
g_main_context_release(wpsContext);
return;
}

sleep(10); /* As we will get the ap info with NM_802_11_AP_FLAGS_WPS_PBC set after pressing the PBC button.
So we are waiting for 10 seconds here*/
do{
if(wifiScanRequest(""))
while (std::getline(configFile, line))
{
aps = nm_device_wifi_get_access_points(NM_DEVICE_WIFI(getNmDevice()));
for (guint i = 0; i < aps->len; i++) {
NMAccessPoint *ap = static_cast<NMAccessPoint *>(g_ptr_array_index(aps, i));

guint32 flags = nm_access_point_get_flags(ap);

NMLOG_INFO("AP Flags: 0x%x\n", flags);

if (flags & NM_802_11_AP_FLAGS_WPS_PBC) {
Exchange::INetworkManager::WiFiConnectTo wifiData;
GBytes *ssid;
ssid = nm_access_point_get_ssid(ap);
gsize size;
const guint8 *ssidData = static_cast<const guint8 *>(g_bytes_get_data(ssid, &size));
std::string ssidTmp(reinterpret_cast<const char *>(ssidData), size);
wifiData.ssid = ssidTmp.c_str();
NMLOG_INFO("connected ssid: %s", ssidTmp.c_str());
if(wifiConnect(wifiData))
wpsConnected = 1;
break;
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)
{
pos += ssidPattern.length();
size_t end = line.find('"', pos + 1);
if (end == std::string::npos)
{
end = line.length();
}
ssid = line.substr(pos + 1, end - pos - 1);
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);
continue;
}
}
sleep(3); /* Waiting time between successive scan */
count++;
}while(count <= 3 && !wpsConnected);
NMLOG_INFO("Completed scanning and wpsconnect status = %d", wpsConnected);
})));

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) {
g_main_context_unref(wpsContext);
wpsContext = nullptr;
}
return;
}

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

if(!wpsContext){
if (wpsThread.joinable()) {
wpsThread.join();
}
wpsContext = g_main_context_new();
wpsThread = std::thread(&wifiManager::wpsAction, this);
}
else
NMLOG_INFO("Start WPS is already in progress");

return true;
}

bool wifiManager::cancelWPS()
{
NMLOG_INFO ("Stop WPS %s", __FUNCTION__);
wpsStop.store(true);
if (wpsThread.joinable()) {
wpsThread.join();
}
if (wpsContext) {
g_main_context_unref(wpsContext);
wpsContext = nullptr;
}
wpsStop.store(false);
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions NetworkManagerGnomeWIFI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <atomic>

#define WPA_SUPPLICANT_CONF "/opt/secure/wifi/wpa_supplicant.conf"
#define WPA_CLI_STATUS "wpa_cli status"

namespace WPEFramework
{
Expand Down Expand Up @@ -55,6 +59,7 @@ namespace WPEFramework
bool wait(GMainLoop *loop, int timeOutMs = 10000); // default maximium set as 10 sec
bool initiateWPS();
bool cancelWPS();
void wpsAction();
bool setInterfaceState(std::string interface, bool enabled);
private:
NMDevice *getNmDevice();
Expand All @@ -78,12 +83,15 @@ namespace WPEFramework
void operator=(wifiManager const&) = delete;

bool createClientNewConnection();
std::atomic<bool> wpsStop = {false};
std::thread wpsThread;

public:
NMClient *client;
GMainLoop *loop;
gboolean createNewConnection;
GMainContext *nmContext = nullptr;
GMainContext *wpsContext = nullptr;
const char* objectPath;
NMDevice *wifidevice;
GSource *source;
Expand Down