diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e9a2b8..93ad2e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ add_definitions(-DPLUGIN_BUILD_REFERENCE=${PLUGIN_BUILD_REFERENCE}) if(ENABLE_GNOME_NETWORKMANAGER) pkg_check_modules(GLIB REQUIRED glib-2.0) +pkg_check_modules(GIO REQUIRED gio-2.0) pkg_check_modules(LIBNM REQUIRED libnm) else() find_package(IARMBus REQUIRED) @@ -102,12 +103,16 @@ set_target_properties(${MODULE_NAME} PROPERTIES if(ENABLE_GNOME_NETWORKMANAGER) - target_sources(${MODULE_NAME} PRIVATE NetworkManagerGnomeProxy.cpp - NetworkManagerGnomeWIFI.cpp - NetworkManagerGnomeEvents.cpp - NetworkManagerGnomeUtils.cpp ) - target_include_directories(${MODULE_NAME} PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBNM_INCLUDE_DIRS}) - target_link_libraries(${MODULE_NAME} PRIVATE ${LIBNM_LIBRARIES}) + target_sources(${MODULE_NAME} PRIVATE + gdbus/NetworkManagerGdbusProxy.cpp + gdbus/NetworkManagerGdbusClient.cpp + gdbus/NetworkManagerGdbusEvent.cpp + gdbus/NetworkManagerGdbusMgr.cpp + gdbus/NetworkManagerGdbusWPS.cpp + gdbus/NetworkManagerGdbusUtils.cpp ) + + target_include_directories(${MODULE_NAME} PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBNM_INCLUDE_DIRS} ${GIO_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS}) + target_link_libraries(${MODULE_NAME} PRIVATE ${GLIB_LIBRARIES} ${GIO_LIBRARIES} uuid) else() target_sources(${MODULE_NAME} PRIVATE NetworkManagerRDKProxy.cpp) target_include_directories(${MODULE_NAME} PRIVATE ${IARMBUS_INCLUDE_DIRS}) @@ -171,5 +176,5 @@ write_config(PLUGINS LegacyPlugin_WiFiManagerAPIs CLASSNAME WiFiManager LOCATOR if(ENABLE_UNIT_TESTING) include(Tests/unit_test/unit_tests.cmake) - include(Tests/raspberrypi/NetworkManagerGdbusTest.cmake) endif(ENABLE_UNIT_TESTING) +include(Tests/raspberrypi/NetworkManagerGdbusTest.cmake) diff --git a/NetworkManagerLogger.cpp b/NetworkManagerLogger.cpp index 313fdcb..4dc9d0d 100644 --- a/NetworkManagerLogger.cpp +++ b/NetworkManagerLogger.cpp @@ -34,7 +34,7 @@ #endif namespace NetworkManagerLogger { - static LogLevel gDefaultLogLevel = INFO_LEVEL; + static LogLevel gDefaultLogLevel = DEBUG_LEVEL; #ifdef USE_RDK_LOGGER diff --git a/Tests/raspberrypi/NetworkManagerGdbusTest.cmake b/Tests/raspberrypi/NetworkManagerGdbusTest.cmake index bceae28..9091f54 100644 --- a/Tests/raspberrypi/NetworkManagerGdbusTest.cmake +++ b/Tests/raspberrypi/NetworkManagerGdbusTest.cmake @@ -15,6 +15,7 @@ add_executable(${RPI_TEST} gdbus/NetworkManagerGdbusUtils.cpp gdbus/NetworkManagerGdbusMgr.cpp gdbus/NetworkManagerGdbusEvent.cpp + gdbus/NetworkManagerGdbusWPS.cpp Tests/raspberrypi/NetworkManagerGdbusTest.cpp ) @@ -37,3 +38,4 @@ target_include_directories(${RPI_TEST} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) # Install the executable to the appropriate location install(TARGETS ${RPI_TEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) +install(TARGETS ${RPI_TEST} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/${STORAGE_DIRECTORY}/plugins) diff --git a/Tests/raspberrypi/NetworkManagerGdbusTest.cpp b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp index fbb7d80..2c41021 100644 --- a/Tests/raspberrypi/NetworkManagerGdbusTest.cpp +++ b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp @@ -4,6 +4,8 @@ #include "NetworkManagerGdbusEvent.h" #include "INetworkManager.h" #include "NetworkManagerGdbusUtils.h" +#include "NetworkManagerImplementation.h" +#include "NetworkManagerGdbusWPS.h" #include #include #include @@ -15,7 +17,46 @@ using namespace WPEFramework::Plugin; using namespace WPEFramework::Exchange; using namespace std; -void displayMenu() { +namespace WPEFramework +{ + namespace Plugin + { + + void NetworkManagerImplementation::ReportInterfaceStateChange(const Exchange::INetworkManager::InterfaceState state, const string interface) + { + NMLOG_INFO("calling 'ReportInterfaceStateChange' cb"); + } + void NetworkManagerImplementation::ReportActiveInterfaceChange(const string prevActiveInterface, const string currentActiveinterface) + { + NMLOG_INFO("calling 'ReportActiveInterfaceChange' cb"); + } + void NetworkManagerImplementation::ReportIPAddressChange(const string interface, const string ipversion, const string ipaddress, const Exchange::INetworkManager::IPStatus status) + { + NMLOG_INFO("calling 'ReportIPAddressChange' cb"); + } + void NetworkManagerImplementation::ReportInternetStatusChange(const Exchange::INetworkManager::InternetStatus prevState, const Exchange::INetworkManager::InternetStatus currState) + { + NMLOG_INFO("calling 'ReportInternetStatusChange' cb"); + } + void NetworkManagerImplementation::ReportAvailableSSIDs(JsonArray &arrayofWiFiScanResults) + { + NMLOG_INFO("calling 'ReportAvailableSSIDs' cb"); + } + void NetworkManagerImplementation::ReportWiFiStateChange(const Exchange::INetworkManager::WiFiState state) + { + NMLOG_INFO("calling 'ReportWiFiStateChange' cb"); + } + void NetworkManagerImplementation::ReportWiFiSignalStrengthChange(const string ssid, const string strength, const Exchange::INetworkManager::WiFiSignalQuality quality) + { + NMLOG_INFO("calling 'ReportWiFiSignalStrengthChange' cb"); + } + + NetworkManagerImplementation* _instance = nullptr; + } +} + +void displayMenu() +{ std::cout << "\n--- Network Manager API Test Menu ---" << std::endl; std::cout << "1. Get Known SSIDs" << std::endl; std::cout << "2. Get Available SSIDs" << std::endl; @@ -27,11 +68,13 @@ void displayMenu() { std::cout << "8. WiFi Disconnect" << std::endl; std::cout << "9. Get WiFi State" << std::endl; std::cout << "10. Get WiFi Signal Strength" << std::endl; + std::cout << "11. start wps" << std::endl; std::cout << "0. Exit" << std::endl; std::cout << "-------------------------------------" << std::endl; } -WPEFramework::Exchange::INetworkManager::WIFISecurityMode getSecurityType() { +WPEFramework::Exchange::INetworkManager::WIFISecurityMode getSecurityType() +{ int securityChoice; std::cout << "Select Security Type:" << std::endl; std::cout << "1. WPA/WPA2 PSK AES" << std::endl; @@ -52,8 +95,8 @@ WPEFramework::Exchange::INetworkManager::WIFISecurityMode getSecurityType() { } } - -void printSSIDs(const std::list& ssids) { +void printSSIDs(const std::list& ssids) +{ if (ssids.empty()) { std::cout << "No SSIDs found" << std::endl; } else { @@ -64,13 +107,14 @@ void printSSIDs(const std::list& ssids) { } } -int main() { - - DbusMgr test; +int main() +{ + NetworkManagerClient* nmClient = NetworkManagerClient::getInstance(); NetworkManagerEvents* nmEvents = NetworkManagerEvents::getInstance(); + NetworkManagerWps* nmwps = NetworkManagerWps::getInstance(); int choice = -1; - + while (choice != 0) { displayMenu(); std::cout << "Enter your choice: \n"; @@ -216,6 +260,15 @@ int main() { break; } + case 11: { + if (nmwps->startWPS()) { + NMLOG_INFO("start wps success"); + } else { + NMLOG_ERROR("start wps failed"); + } + break; + } + case 0: std::cout << "Exiting program." << std::endl; break; @@ -229,4 +282,4 @@ int main() { NMLOG_INFO("Program completed successfully"); nmEvents->stopNetworkMangerEventMonitor(); return 0; -} +} \ No newline at end of file diff --git a/gdbus/NetworkManagerGdbusClient.cpp b/gdbus/NetworkManagerGdbusClient.cpp index 1c1aa73..3963518 100644 --- a/gdbus/NetworkManagerGdbusClient.cpp +++ b/gdbus/NetworkManagerGdbusClient.cpp @@ -52,13 +52,7 @@ namespace WPEFramework if(ConnProxy == NULL) return false; - settingsProxy = g_dbus_proxy_call_sync(ConnProxy, - "GetSettings", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + settingsProxy = g_dbus_proxy_call_sync(ConnProxy, "GetSettings", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!settingsProxy) { g_dbus_error_strip_remote_error(error); NMLOG_ERROR("Failed to get connection settings: %s", error->message); @@ -156,13 +150,7 @@ namespace WPEFramework if(ConnProxy == NULL) return false; - deleteVar = g_dbus_proxy_call_sync(ConnProxy, - "Delete", - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); + deleteVar = g_dbus_proxy_call_sync(ConnProxy, "Delete", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (!deleteVar) { g_dbus_error_strip_remote_error(error); NMLOG_ERROR("Failed to get connection settings: %s", error->message); @@ -886,8 +874,9 @@ namespace WPEFramework wProxy = m_dbus.getNetworkManagerDeviceProxy(devInfo.path.c_str()); if(wProxy == NULL) return false; - + else g_dbus_proxy_call_sync(wProxy, "Disconnect", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + return true; if (error) { NMLOG_ERROR("Error calling Disconnect method: %s", error->message); g_error_free(error); @@ -895,8 +884,9 @@ namespace WPEFramework return false; } else - NMLOG_INFO("wifi disconnected success"); + NMLOG_INFO("wifi disconnected success"); + g_object_unref(wProxy); return true; } @@ -911,4 +901,4 @@ namespace WPEFramework } } // WPEFramework -} // Plugin +} // Plugin \ No newline at end of file diff --git a/gdbus/NetworkManagerGdbusClient.h b/gdbus/NetworkManagerGdbusClient.h index 15b8eb1..a93f9e1 100644 --- a/gdbus/NetworkManagerGdbusClient.h +++ b/gdbus/NetworkManagerGdbusClient.h @@ -64,4 +64,4 @@ namespace WPEFramework DbusMgr m_dbus; }; } // Plugin -} // WPEFramework \ No newline at end of file +} // WPEFramework diff --git a/gdbus/NetworkManagerGdbusEvent.cpp b/gdbus/NetworkManagerGdbusEvent.cpp index f94c434..061494a 100644 --- a/gdbus/NetworkManagerGdbusEvent.cpp +++ b/gdbus/NetworkManagerGdbusEvent.cpp @@ -359,74 +359,76 @@ namespace WPEFramework } static void onConnectionSignalReceivedCB (GDBusProxy *proxy, - gchar *sender_name, - gchar *signal_name, + gchar *senderName, + gchar *signalName, GVariant *parameters, - gpointer user_data) { + gpointer userData) { - if (g_strcmp0(signal_name, "NewConnection") == 0) { - NMLOG_INFO("new Connection added success"); + if (g_strcmp0(senderName, "NewConnection") == 0) { + NMLOG_INFO("new connection added success"); NMLOG_DEBUG("Parameters: %s", g_variant_print(parameters, TRUE)); - } else if (g_strcmp0(signal_name, "ConnectionRemoved") == 0) { + } else if (g_strcmp0(signalName, "ConnectionRemoved") == 0) { NMLOG_INFO("connection remove success"); NMLOG_DEBUG("Parameters: %s", g_variant_print(parameters, TRUE)); } } - static void ipV4addressChangeCb(GDBusProxy *proxy, - GVariant *changed_properties, - GStrv invalidated_properties, - gpointer user_data) { - if (changed_properties == NULL || proxy == NULL) { - NMLOG_FATAL("cb doesn't have changed_properties "); + static void ipV4addressChangeCb(GDBusProxy *proxy, GVariant *changedProps, GStrv invalidProps, gpointer userData) + { + if (changedProps == NULL || proxy == NULL) { + NMLOG_FATAL("cb doesn't have changed properties "); return; } - GVariant *addressDataVariant = g_variant_lookup_value(changed_properties, "Addresses", NULL); - if (addressDataVariant != NULL) + GVariant *addressVariant = g_variant_lookup_value(changedProps, "Addresses", NULL); + if (addressVariant != NULL) { - const char* iface = static_cast(user_data); + const char* iface = static_cast(userData); if(iface != NULL ) { std::string ipAddr; uint32_t prifix; if(GnomeUtils::getIpv4AddrFromIP4ConfigProxy(proxy, ipAddr, prifix)) NetworkManagerEvents::onAddressChangeCb(std::string(iface), true, false, ipAddr); } + g_variant_unref(addressVariant); } } - static void ipV6addressChangeCb(GDBusProxy *proxy, - GVariant *changed_properties, - GStrv invalidated_properties, - gpointer user_data) { - if (changed_properties == NULL || proxy == NULL) { - NMLOG_FATAL("cb doesn't have changed_properties "); + static void ipV6addressChangeCb(GDBusProxy *proxy, GVariant *changedProps, GStrv invalidProps, gpointer userData) + { + if (changedProps == NULL || proxy == NULL) { + NMLOG_FATAL("cb doesn't have changed properties "); return; } - GVariant *addressDataVariant = g_variant_lookup_value(changed_properties, "Addresses", NULL); - if (addressDataVariant != NULL) + GVariant *addressVariant = g_variant_lookup_value(changedProps, "Addresses", NULL); + if (addressVariant != NULL) { - const char* iface = static_cast(user_data); - if(iface != NULL ) + const char* iface = static_cast(userData); + if(iface != NULL) { std::string ipAddr; uint32_t prifix; if(GnomeUtils::getIpv6AddrFromIP6ConfigProxy(proxy, ipAddr, prifix)) NetworkManagerEvents::onAddressChangeCb(std::string(iface), true, true, ipAddr); } + + g_variant_unref(addressVariant); } else { - GVariantIter *props_iter; - const gchar *property_name; - GVariant *property_value; + // TODO remove + GVariantIter *propsIter= NULL; + const gchar *propertyName= NULL; + GVariant *propertyValue= NULL; // Iterate over all changed properties - g_variant_get(changed_properties, "a{sv}", &props_iter); - while (g_variant_iter_loop(props_iter, "{&sv}", &property_name, &property_value)) { - NMLOG_DEBUG("Other Property: %s", property_name); + g_variant_get(changedProps, "a{sv}", &propsIter); + while (g_variant_iter_loop(propsIter, "{&sv}", &propertyName, &propertyValue)) { + NMLOG_DEBUG("Other Property: %s", propertyName); + // if(propertyValue) + // g_variant_unref(changedProps); } - g_variant_iter_free(props_iter); + g_variant_iter_free(propsIter); } } @@ -445,6 +447,8 @@ namespace WPEFramework NMLOG_DEBUG("interface is not eth0/wlan0: %s", ifname.c_str()); return; } + else + NMLOG_DEBUG("monitoring %s interface events ..", ifname.c_str()); GDBusProxy *deviceProxy =_NetworkManagerEvents->eventDbus.getNetworkManagerDeviceProxy(devicePath); if(deviceProxy == nullptr) @@ -452,14 +456,15 @@ namespace WPEFramework g_signal_connect(deviceProxy, "g-signal", G_CALLBACK(deviceStateChangedCB), NULL); NMLOG_DEBUG("Monitoring device: %s", devicePath); - if(ifname == _NetworkManagerEvents->wlanIfname) + if(ifname == GnomeUtils::getWifiIfname()) { _NetworkManagerEvents->nmEvents.wirelessDeviceProxy = deviceProxy; _NetworkManagerEvents->nmEvents.wifiDevicePath = devicePath; + userdata = _NetworkManagerEvents->wlanIfname; // TODO change to GnomeUtils::getWifiIfname() subscribeForlastScanPropertyEvent(devicePath); - userdata = _NetworkManagerEvents->wlanIfname; } - else{ + else if(ifname == GnomeUtils::getEthIfname()) + { _NetworkManagerEvents->nmEvents.wiredDeviceProxy = deviceProxy; _NetworkManagerEvents->nmEvents.ethDevicePath = devicePath; userdata = _NetworkManagerEvents->ethIfname; @@ -467,15 +472,17 @@ namespace WPEFramework const gchar *ipv4ConfigPath = NULL; const gchar *ipv6ConfigPath = NULL; - GVariant *ip4_config = g_dbus_proxy_get_cached_property(deviceProxy, "Ip4Config"); - if (ip4_config) { - ipv4ConfigPath = g_variant_get_string(ip4_config, NULL); + GVariant *ip4Config = g_dbus_proxy_get_cached_property(deviceProxy, "Ip4Config"); + if (ip4Config) + { + ipv4ConfigPath = g_variant_get_string(ip4Config, NULL); NMLOG_DEBUG("Monitoring ip4_config_path: %s", ipv4ConfigPath); } - GVariant *ip6_config = g_dbus_proxy_get_cached_property(deviceProxy, "Ip6Config"); - if (ip6_config) { - ipv6ConfigPath = g_variant_get_string(ip6_config, NULL); + GVariant *ip6Config = g_dbus_proxy_get_cached_property(deviceProxy, "Ip6Config"); + if(ip6Config) + { + ipv6ConfigPath = g_variant_get_string(ip6Config, NULL); NMLOG_DEBUG("Monitoring ip6_config_path: %s", ipv6ConfigPath); } @@ -483,13 +490,26 @@ namespace WPEFramework { GDBusProxy *ipV4Proxy = _NetworkManagerEvents->eventDbus.getNetworkManagerIpv4Proxy(ipv4ConfigPath); g_signal_connect(ipV4Proxy, "g-properties-changed", G_CALLBACK(ipV4addressChangeCb), userdata); + if(ifname == GnomeUtils::getEthIfname()) + _NetworkManagerEvents->nmEvents.ethIPv4Proxy = ipV4Proxy; + else if(ifname == GnomeUtils::getWifiIfname()) + _NetworkManagerEvents->nmEvents.wlanIPv4Proxy = ipV4Proxy; } if(ipv6ConfigPath) { GDBusProxy *ipV6Proxy = _NetworkManagerEvents->eventDbus.getNetworkManagerIpv6Proxy(ipv6ConfigPath); g_signal_connect(ipV6Proxy, "g-properties-changed", G_CALLBACK(ipV6addressChangeCb), userdata); + if(ifname == GnomeUtils::getEthIfname()) + _NetworkManagerEvents->nmEvents.ethIPv6Proxy = ipV6Proxy; + else if(ifname == GnomeUtils::getWifiIfname()) + _NetworkManagerEvents->nmEvents.wlanIPv6Proxy = ipV6Proxy; } + + if(ip4Config) + g_variant_unref(ip4Config); + if(ip6Config) + g_variant_unref(ip6Config); } void* NetworkManagerEvents::networkMangerEventMonitor(void *arg) @@ -512,12 +532,13 @@ namespace WPEFramework GVariant *devices = g_dbus_proxy_get_cached_property(nmEvents->networkManagerProxy, "Devices"); if (devices != NULL) { GVariantIter iter; - const gchar *devicePath; + gchar *devicePath = NULL; g_variant_iter_init(&iter, devices); while (g_variant_iter_loop(&iter, "&o", &devicePath)) { monitorDevice(devicePath); } + g_variant_unref(devices); } @@ -540,8 +561,16 @@ namespace WPEFramework g_object_unref(nmEvents->networkManagerProxy); if(nmEvents->settingsProxy) g_object_unref(nmEvents->settingsProxy); - - NMLOG_WARNING("stoping all event monitor"); + if(nmEvents->ethIPv4Proxy) + g_object_unref(nmEvents->ethIPv4Proxy); + if(nmEvents->ethIPv6Proxy) + g_object_unref(nmEvents->ethIPv6Proxy); + if(nmEvents->wlanIPv4Proxy) + g_object_unref(nmEvents->wlanIPv4Proxy); + if(nmEvents->wlanIPv6Proxy) + g_object_unref(nmEvents->wlanIPv6Proxy); + + NMLOG_WARNING("unregistered all event monitor"); return nullptr; } diff --git a/gdbus/NetworkManagerGdbusEvent.h b/gdbus/NetworkManagerGdbusEvent.h index aea8598..b89f8fb 100644 --- a/gdbus/NetworkManagerGdbusEvent.h +++ b/gdbus/NetworkManagerGdbusEvent.h @@ -39,6 +39,11 @@ namespace WPEFramework GDBusProxy *networkManagerProxy; // networkmanager main bus GDBusProxy *settingsProxy; // settings + GDBusProxy *ethIPv4Proxy; + GDBusProxy *ethIPv6Proxy; + GDBusProxy *wlanIPv4Proxy; + GDBusProxy *wlanIPv6Proxy; + std::string wifiDevicePath; std::string ethDevicePath; @@ -69,7 +74,7 @@ namespace WPEFramework std::atomicdoScanNotify = {true}; GThread *eventThrdID; public: - NMEvents nmEvents; + NMEvents nmEvents{}; DbusMgr eventDbus; char wlanIfname[16]; char ethIfname[16]; diff --git a/gdbus/NetworkManagerGdbusMgr.cpp b/gdbus/NetworkManagerGdbusMgr.cpp index 2d92834..5f4df1f 100644 --- a/gdbus/NetworkManagerGdbusMgr.cpp +++ b/gdbus/NetworkManagerGdbusMgr.cpp @@ -62,7 +62,8 @@ namespace WPEFramework return NULL; } - flags = static_cast(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START); + // flags = static_cast(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START); + flags = static_cast(G_DBUS_PROXY_FLAGS_NONE); return connection; } diff --git a/gdbus/NetworkManagerGdbusProxy.cpp b/gdbus/NetworkManagerGdbusProxy.cpp index 2067d4b..c01be30 100644 --- a/gdbus/NetworkManagerGdbusProxy.cpp +++ b/gdbus/NetworkManagerGdbusProxy.cpp @@ -20,6 +20,7 @@ #include "NetworkManagerImplementation.h" #include "NetworkManagerGdbusClient.h" #include "NetworkManagerGdbusEvent.h" +#include "NetworkManagerGdbusWPS.h" using namespace WPEFramework; using namespace WPEFramework::Plugin; @@ -32,6 +33,7 @@ namespace WPEFramework NetworkManagerImplementation* _instance = nullptr; NetworkManagerClient* _nmGdbusClient = nullptr; NetworkManagerEvents* _nmGdbusEvents = nullptr; + NetworkManagerWps* _nmWPS = nullptr; void NetworkManagerInternalEventHandler(const char *owner, int eventId, void *data, size_t len) { return; @@ -42,6 +44,7 @@ namespace WPEFramework ::_instance = this; _nmGdbusClient = NetworkManagerClient::getInstance(); _nmGdbusEvents = NetworkManagerEvents::getInstance(); + _nmWPS = NetworkManagerWps::getInstance(); } uint32_t NetworkManagerImplementation::GetAvailableInterfaces (Exchange::INetworkManager::IInterfaceDetailsIterator*& interfacesItr/* @out */) @@ -186,6 +189,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StartWPS(const WiFiWPS& method /* @in */, const string& wps_pin /* @in */) { + _nmWPS->startWPS(); return Core::ERROR_NONE; } diff --git a/gdbus/NetworkManagerGdbusUtils.cpp b/gdbus/NetworkManagerGdbusUtils.cpp index 9de2fe6..dfe7802 100644 --- a/gdbus/NetworkManagerGdbusUtils.cpp +++ b/gdbus/NetworkManagerGdbusUtils.cpp @@ -21,7 +21,7 @@ #include #include #include - +#include #include "NetworkManagerLogger.h" #include "NetworkManagerGdbusUtils.h" #include "NetworkManagerGdbusMgr.h" @@ -231,7 +231,6 @@ namespace WPEFramework if(iface != NULL) { properties.interface = iface; - //g_free(iface); } //NMLOG_DEBUG("Interface: %s", iface); g_variant_unref(devicesVar); @@ -272,10 +271,10 @@ namespace WPEFramework return false; } - GVariantIter* iter; + GVariantIter* iter = NULL; gchar* devicePath = NULL; g_variant_get(devicesVar, "(ao)", &iter); - while (g_variant_iter_loop(iter, "o", &devicePath)) + while (iter != NULL && g_variant_iter_loop(iter, "o", &devicePath)) { if(devicePath == NULL ) continue; @@ -344,6 +343,17 @@ namespace WPEFramework return ret; } + std::string GnomeUtils::generateUUID() + { + uuid_t uuid{}; + char buffer[37] = {0}; // 36 + NULL + uuid_generate_random(uuid); + uuid_unparse_lower(uuid, buffer); + if(buffer[0] != '\0') + return std::string(buffer); + return std::string(""); + } + bool GnomeUtils::getApDetails(DbusMgr& m_dbus, const char* apPath, Exchange::INetworkManager::WiFiSSIDInfo& wifiInfo) { guint32 flags= 0, wpaFlags= 0, rsnFlags= 0, freq= 0, bitrate= 0; diff --git a/gdbus/NetworkManagerGdbusUtils.h b/gdbus/NetworkManagerGdbusUtils.h index 222d686..5c8f82f 100644 --- a/gdbus/NetworkManagerGdbusUtils.h +++ b/gdbus/NetworkManagerGdbusUtils.h @@ -65,6 +65,7 @@ namespace WPEFramework { class GnomeUtils { public: + static std::string generateUUID(); static bool getDeviceByIpIface(DbusMgr& m_dbus, const gchar *iface_name, std::string& path); static bool getApDetails(DbusMgr& m_dbus, const char* apPath, Exchange::INetworkManager::WiFiSSIDInfo& wifiInfo); static bool getConnectionPaths(DbusMgr& m_dbus, std::list& pathsList); diff --git a/gdbus/NetworkManagerGdbusWPS.cpp b/gdbus/NetworkManagerGdbusWPS.cpp new file mode 100644 index 0000000..7c1984e --- /dev/null +++ b/gdbus/NetworkManagerGdbusWPS.cpp @@ -0,0 +1,398 @@ + +/** +* If not stated otherwise in this file or this component's LICENSE +* file the following copyright and licenses apply: +* +* Copyright 2022 RDK Management +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +**/ + +#include +#include +#include +#include +#include +#include +#include +#include "NetworkManagerLogger.h" +#include "NetworkManagerGdbusWPS.h" +#include "NetworkManagerGdbusUtils.h" + +namespace WPEFramework +{ + namespace Plugin + { + + NetworkManagerWps::NetworkManagerWps() { + NMLOG_INFO("NetworkManagerWps"); + } + + NetworkManagerWps::~NetworkManagerWps() { + NMLOG_INFO("~NetworkManagerWps"); + } + + static bool isWpsPBCWifiAP(DbusMgr& m_dbus, const char* apPath, std::string &ssid) + { + uint32_t flags= 0, wpaFlags= 0, rsnFlags= 0; + GVariant* ssidVariant = NULL; + bool ret = false; + + GDBusProxy* proxy = m_dbus.getNetworkManagerAccessPointProxy(apPath); + if (proxy == NULL) { + return false; + } + + GnomeUtils::getCachedPropertyU(proxy, "Flags", &flags); + GnomeUtils::getCachedPropertyU(proxy, "WpaFlags", &wpaFlags); + GnomeUtils::getCachedPropertyU(proxy, "RsnFlags", &rsnFlags); + + if(flags & static_cast(NM80211ApFlags::NM_802_11_AP_FLAGS_WPS_PBC)) + { + gsize ssid_length = 0; + ssidVariant = g_dbus_proxy_get_cached_property(proxy,"Ssid"); + if (!ssidVariant) { + NMLOG_ERROR("Failed to get AP properties."); + g_object_unref(proxy); + return false; + } + + const guchar *ssid_data = static_cast(g_variant_get_fixed_array(ssidVariant, &ssid_length, sizeof(guchar))); + if (ssid_data && ssid_length > 0 && ssid_length <= 32) + { + ssid.assign(reinterpret_cast(ssid_data), ssid_length); + NMLOG_INFO("WPS PBC SSID: %s", ssid.c_str()); + } + + if(ssidVariant) + g_variant_unref(ssidVariant); + ret = true; + } + + if(proxy) + g_object_unref(proxy); + + return ret; + } + + bool NetworkManagerWps::isWpsApAvailable() + { + GError* error = nullptr; + GDBusProxy* wProxy = nullptr; + deviceInfo devProperty{}; + bool ret = false; + + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), devProperty)) + return false; + + if(devProperty.path.empty() || devProperty.state < NM_DEVICE_STATE_DISCONNECTED) + { + NMLOG_ERROR("no wifi device found"); + return false; + } + + wProxy = m_dbus.getNetworkManagerWirelessProxy(devProperty.path.c_str()); + if (wProxy == NULL) + return false; + + GVariant* result = g_dbus_proxy_call_sync(wProxy, "GetAllAccessPoints", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error) { + NMLOG_ERROR("Error creating proxy: %s", error->message); + g_error_free(error); + g_object_unref(wProxy); + return false; + } + + GVariantIter* iter = NULL; + const gchar* apPath = NULL; + g_variant_get(result, "(ao)", &iter); + while (g_variant_iter_loop(iter, "o", &apPath)) + { + if(isWpsPBCWifiAP(m_dbus, apPath, wpsApSSID)) + { + wpsApPath = apPath; + NMLOG_DEBUG("Access Point Path: %s", apPath); + ret = true; + break; + } + } + + g_variant_iter_free(iter); + g_variant_unref(result); + g_object_unref(wProxy); + + return ret; + } + + static bool connectionBuilder(GVariantBuilder& connBuilder, std::string ssid, bool agentOwned = false) + { + GVariantBuilder settingsBuilder; + g_variant_builder_init (&connBuilder, G_VARIANT_TYPE ("a{sa{sv}}")); + if(ssid.empty() || ssid.length() > 32) + { + NMLOG_WARNING("ssid name is missing or invalied"); + return false; + } + + NMLOG_INFO("ssid %s, agentOwned %d ", ssid.c_str(), agentOwned); + + /* Adding 'connection' settings */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + std::string uuid = GnomeUtils::generateUUID(); + if(uuid.empty() || uuid.length() < 32) + { + NMLOG_ERROR("uuid generate failed"); + return false; + } + + g_variant_builder_add (&settingsBuilder, "{sv}", "uuid", g_variant_new_string (uuid.c_str())); + g_variant_builder_add (&settingsBuilder, "{sv}", "id", g_variant_new_string (ssid.c_str())); // connection id = ssid name + g_variant_builder_add (&settingsBuilder, "{sv}", "interface-name", g_variant_new_string (GnomeUtils::getWifiIfname())); + g_variant_builder_add (&settingsBuilder, "{sv}", "type", g_variant_new_string ("802-11-wireless")); + g_variant_builder_add (&connBuilder, "{sa{sv}}", "connection", &settingsBuilder); + /* Adding '802-11-wireless 'settings */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + GVariant *ssidArray = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, (const guint8 *)ssid.c_str(), ssid.length(), 1); + g_variant_builder_add (&settingsBuilder, "{sv}", "ssid", ssidArray); + g_variant_builder_add (&settingsBuilder, "{sv}", "mode", g_variant_new_string("infrastructure")); + g_variant_builder_add (&settingsBuilder, "{sv}", "hidden", g_variant_new_boolean(true)); // set hidden: yes + g_variant_builder_add (&connBuilder, "{sa{sv}}", "802-11-wireless", &settingsBuilder); + + /* Adding '802-11-wireless-security' settings */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + + NMLOG_DEBUG("802-11-wireless-security key-mgmt: 'wpa-psk' for wps"); + g_variant_builder_add (&settingsBuilder, "{sv}", "key-mgmt", g_variant_new_string("wpa-psk")); + if(agentOwned) + g_variant_builder_add (&settingsBuilder, "{sv}", "psk-flags", g_variant_new_uint32(NM_SETTING_SECRET_FLAG_AGENT_OWNED)); + g_variant_builder_add (&connBuilder, "{sa{sv}}", "802-11-wireless-security", &settingsBuilder); + + /* Adding the 'ipv4' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&settingsBuilder, "{sv}", "method", g_variant_new_string ("auto")); + g_variant_builder_add (&connBuilder, "{sa{sv}}", "ipv4", &settingsBuilder); + + /* Adding the 'ipv6' Setting */ + g_variant_builder_init (&settingsBuilder, G_VARIANT_TYPE ("a{sv}")); + g_variant_builder_add (&settingsBuilder, "{sv}", "method", g_variant_new_string ("auto")); + g_variant_builder_add (&connBuilder, "{sa{sv}}", "ipv6", &settingsBuilder); + NMLOG_DEBUG("wps connection builder success..."); + return true; + } + + bool NetworkManagerWps::addWpsConnection() + { + GVariantBuilder connBuilder; + bool ret = false; + GVariant *result = NULL; + GError* error = NULL; + const char *newConPath = NULL; + deviceInfo deviceProp; + + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), deviceProp)) + return false; + + if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) + { + NMLOG_WARNING("wlan0 interface not active"); + return false; + } + + if(!connectionBuilder(connBuilder, wpsApSSID)) + { + NMLOG_WARNING("connection builder failed"); + GVariant *variant = g_variant_builder_end(&connBuilder); + if(variant) + g_variant_unref(variant); + return false; + } + + GDBusProxy *proxy = NULL; + proxy = m_dbus.getNetworkManagerSettingsProxy(); + if (proxy != nullptr) + { + result = g_dbus_proxy_call_sync (proxy, "AddConnection", + g_variant_new ("(a{sa{sv}})", &connBuilder), G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + + if (error != nullptr) { + g_dbus_error_strip_remote_error (error); + NMLOG_ERROR ("Could not create NetworkManager/Settings proxy: %s", error->message); + g_error_free (error); + g_object_unref(proxy); + return false; + } + + if (result) { + g_variant_get (result, "(&o)", &newConPath); + NMLOG_INFO("Added a new connection: %s", newConPath); + wpsConnPath = newConPath; + ret = true; + g_variant_unref (result); + } else { + g_dbus_error_strip_remote_error (error); + NMLOG_ERROR("Error adding connection: %s", error->message); + g_clear_error (&error); + } + g_object_unref(proxy); + } + + return ret; + } + + + static bool updateConnctionAndactivate(DbusMgr& m_dbus, /*GVariantBuilder& connBuilder,*/ const char* devicePath, const char* connPath, const char* apPath) + { + GDBusProxy* proxy = nullptr; + GError* error = nullptr; + GVariant* result = nullptr; + + // proxy = m_dbus.getNetworkManagerSettingsConnectionProxy(connPath); + // if(proxy == NULL) + // return false; + + // result = g_dbus_proxy_call_sync (proxy, "Update", + // g_variant_new("(a{sa{sv}})", connBuilder), + // G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + // if (error) { + // NMLOG_ERROR("Failed to call Update : %s", error->message); + // g_clear_error(&error); + // g_object_unref(proxy); + // return false; + // } + + // if(result) + // g_variant_unref(result); + // g_object_unref(proxy); + + proxy = m_dbus.getNetworkManagerProxy(); + if(proxy == NULL) + return false; + + if (error) { + NMLOG_ERROR( "Failed to create proxy: %s", error->message); + g_clear_error(&error); + return false; + } + //const char* specificObject = "/"; + + result = g_dbus_proxy_call_sync (proxy, "ActivateConnection", + g_variant_new("(ooo)", connPath, devicePath, apPath), + G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error); + + if (error) { + NMLOG_ERROR("Failed to call ActivateConnection: %s", error->message); + g_clear_error(&error); + g_object_unref(proxy); + return false; + } + + if(result) { + GVariant* activeConnVariant; + g_variant_get(result, "(@o)", &activeConnVariant); + const char* activeConnPath = g_variant_get_string(activeConnVariant, nullptr); + NMLOG_DEBUG("connPath %s; activeconn %s", connPath, activeConnPath); + g_variant_unref(activeConnVariant); + } + + if(result) + g_variant_unref(result); + if(proxy) + g_object_unref(proxy); + + return true; + } + + bool NetworkManagerWps::startWPS() + { + GVariant *result = NULL; + GError* error = NULL; + std::string connPath{}; + + if(!isWpsApAvailable()) + { + NMLOG_INFO("no wps network found"); + return false; + } + + GDBusProxy* proxy = g_dbus_proxy_new_sync( + m_dbus.getConnection(), G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.NetworkManager", + "/org/freedesktop/NetworkManager/AgentManager", + "org.freedesktop.NetworkManager.AgentManager", + NULL, + &error + ); + + if (error != NULL) { + g_dbus_error_strip_remote_error(error); + NMLOG_FATAL("Error creating proxy: %s", error->message); + g_clear_error(&error); + return false; + } + + const char *identifier = "rdk-nm-agent"; + // Call the Register method + result = g_dbus_proxy_call_sync( + proxy, + "Register", + g_variant_new("(s)", identifier), + G_DBUS_CALL_FLAGS_NONE, + -1, // Timeout (default) + NULL, // GCancellable + &error + ); + + if (!result) { + NMLOG_FATAL("Failed to call Register method: %s", error->message); + g_error_free(error); + } else { + NMLOG_INFO("Agent successfully registered with identifier: %s", identifier); + g_variant_unref(result); + } + + addWpsConnection(); + + deviceInfo deviceProp; + if(!GnomeUtils::getDeviceInfoByIfname(m_dbus, GnomeUtils::getWifiIfname(), deviceProp)) + return false; + + if(deviceProp.path.empty() || deviceProp.state == NM_DEVICE_STATE_UNKNOWN) + { + NMLOG_WARNING("wlan0 interface not active"); + return false; + } + + updateConnctionAndactivate(m_dbus, deviceProp.path.c_str(), wpsConnPath.c_str(), wpsApPath.c_str()); +sleep(30); + result = g_dbus_proxy_call_sync( + proxy, + "Unregister", + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, // Timeout (default) + NULL, // GCancellable + &error + ); + + if (!result) { + NMLOG_FATAL("Failed to call unRegister method: %s", error->message); + g_error_free(error); + } else { + NMLOG_INFO("Agent successfully unregistered with identifier: %s", identifier); + g_variant_unref(result); + } + return true; + } + + } // WPEFramework +} // Plugin diff --git a/gdbus/NetworkManagerGdbusWPS.h b/gdbus/NetworkManagerGdbusWPS.h new file mode 100644 index 0000000..9cd6f9b --- /dev/null +++ b/gdbus/NetworkManagerGdbusWPS.h @@ -0,0 +1,61 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#include +#include +#include +#include + +#include "NetworkManagerLogger.h" +#include "NetworkManagerGdbusMgr.h" +#include "INetworkManager.h" + +namespace WPEFramework +{ + namespace Plugin + { + + class NetworkManagerWps + { + public: + static NetworkManagerWps* getInstance() + { + static NetworkManagerWps instance; + return &instance; + } + + NetworkManagerWps(const NetworkManagerWps&) = delete; + NetworkManagerWps& operator=(const NetworkManagerWps&) = delete; + + bool startWPS(); + bool stopWPS(); + std::string wpsApSSID{}; + std::string wpsApPath{}; + std::string wpsConnPath{}; + + private: + NetworkManagerWps(); + ~NetworkManagerWps(); + bool isWpsApAvailable(); + bool addWpsConnection(); + DbusMgr m_dbus; + }; + } // Plugin +} // WPEFramework \ No newline at end of file