diff --git a/NetworkManagerGnomeEvents.cpp b/NetworkManagerGnomeEvents.cpp index 0e0321fd..56e94be4 100644 --- a/NetworkManagerGnomeEvents.cpp +++ b/NetworkManagerGnomeEvents.cpp @@ -72,10 +72,14 @@ namespace WPEFramework static void deviceStateChangeCb(NMDevice *device, GParamSpec *pspec, NMEvents *nmEvents) { + static bool isEthDisabled = false; + static bool isWlanDisabled = false; + if(device == nullptr) + return; NMDeviceState deviceState; deviceState = nm_device_get_state(device); std::string ifname = nm_device_get_iface(device); - if(ifname == nmEvents->ifnameWlan0) + if(ifname == nmUtils::wlanIface()) { if(!NM_IS_DEVICE_WIFI(device)) { NMLOG_FATAL("not a wifi device !"); @@ -118,19 +122,24 @@ namespace WPEFramework case NM_DEVICE_STATE_UNKNOWN: wifiState = "WIFI_STATE_UNINSTALLED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_UNINSTALLED); + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); break; case NM_DEVICE_STATE_UNMANAGED: wifiState = "WIFI_STATE_DISABLED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_DISABLED); + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); + isWlanDisabled = true; break; case NM_DEVICE_STATE_UNAVAILABLE: case NM_DEVICE_STATE_DISCONNECTED: wifiState = "WIFI_STATE_DISCONNECTED"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_DISCONNECTED); + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_LINK_DOWN, nmUtils::wlanIface()); break; case NM_DEVICE_STATE_PREPARE: wifiState = "WIFI_STATE_PAIRING"; GnomeNetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WIFI_STATE_PAIRING); + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_LINK_UP, nmUtils::wlanIface()); break; case NM_DEVICE_STATE_CONFIG: wifiState = "WIFI_STATE_CONNECTING"; @@ -159,18 +168,27 @@ namespace WPEFramework wifiState = "Un handiled: " ; wifiState += std::to_string(deviceState); } + + if(isWlanDisabled & deviceState > NM_DEVICE_STATE_UNMANAGED) + { + isWlanDisabled = false; + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_ADDED, nmUtils::wlanIface()); + } + + break; } } NMLOG_DEBUG("wifi state: %s", wifiState.c_str()); } - else if(ifname == nmEvents->ifnameEth0) + else if(ifname == nmUtils::ethIface()) { switch (deviceState) { case NM_DEVICE_STATE_UNKNOWN: case NM_DEVICE_STATE_UNMANAGED: - GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_DISABLED, nmUtils::ethIface()); - break; + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::ethIface()); + isEthDisabled = true; + break; case NM_DEVICE_STATE_UNAVAILABLE: case NM_DEVICE_STATE_DISCONNECTED: GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_LINK_DOWN, nmUtils::ethIface()); @@ -187,10 +205,15 @@ namespace WPEFramework default: NMLOG_WARNING("Unhandiled state change %d", deviceState); } + + if(isEthDisabled & deviceState > NM_DEVICE_STATE_UNMANAGED) + { + isEthDisabled = false; + GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_ADDED, nmUtils::ethIface()); + } } NMLOG_DEBUG("%s state: (%d)", ifname.c_str(), deviceState); - } static void ip4ChangedCb(NMIPConfig *ipConfig, GParamSpec *pspec, gpointer userData) @@ -282,15 +305,15 @@ namespace WPEFramework if( ((device != NULL) && NM_IS_DEVICE(device)) ) { std::string ifname = nm_device_get_iface(device); - if(ifname == nmEvents->ifnameWlan0) { + if(ifname == nmUtils::wlanIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_ADDED, nmUtils::wlanIface()); } - else if(ifname == nmEvents->ifnameEth0) { + else if(ifname == nmUtils::ethIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_ADDED, nmUtils::ethIface()); } /* ip events added only for eth0 and wlan0 */ - if((ifname == nmEvents->ifnameEth0) || (ifname == nmEvents->ifnameWlan0)) + if(ifname == nmUtils::ethIface() || ifname == nmUtils::wlanIface()) { g_signal_connect(device, "notify::" NM_DEVICE_STATE, G_CALLBACK(deviceStateChangeCb), nmEvents); // TODO call notify::" NM_DEVICE_ACTIVE_CONNECTION if needed @@ -314,11 +337,11 @@ namespace WPEFramework if( ((device != NULL) && NM_IS_DEVICE(device)) ) { std::string ifname = nm_device_get_iface(device); - if(ifname == nmEvents->ifnameWlan0) { + if(ifname == nmUtils::wlanIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::wlanIface()); g_signal_handlers_disconnect_by_func(device, (gpointer)deviceStateChangeCb, nmEvents); } - else if(ifname == nmEvents->ifnameEth0) { + else if(ifname == nmUtils::ethIface()) { GnomeNetworkManagerEvents::onInterfaceStateChangeCb(Exchange::INetworkManager::INTERFACE_REMOVED, nmUtils::ethIface()); g_signal_handlers_disconnect_by_func(device, (gpointer)deviceStateChangeCb, nmEvents); } @@ -383,11 +406,11 @@ namespace WPEFramework NMDevice *device = NM_DEVICE(g_ptr_array_index(devices, count)); if( ((device != NULL) && NM_IS_DEVICE(device)) ) { - g_signal_connect(device, "notify::" NM_DEVICE_STATE, G_CALLBACK(deviceStateChangeCb), nmEvents); - //g_signal_connect(device, "notify::" NM_DEVICE_ACTIVE_CONNECTION, G_CALLBACK(deviceActiveConnChangeCb), NULL); std::string ifname = nm_device_get_iface(device); - if((ifname == nmEvents->ifnameEth0) || (ifname == nmEvents->ifnameWlan0)) /* ip events added only for eth0 and wlan0 */ + if((ifname == nmUtils::ethIface()) || (ifname == nmUtils::wlanIface())) { + deviceStateChangeCb(device, nullptr, nullptr); //posting event if interface already connected + g_signal_connect(device, "notify::" NM_DEVICE_STATE, G_CALLBACK(deviceStateChangeCb), nmEvents); NMIPConfig *ipv4Config = nm_device_get_ip4_config(device); NMIPConfig *ipv6Config = nm_device_get_ip6_config(device); if (ipv4Config) { @@ -485,8 +508,6 @@ namespace WPEFramework return; } _nmEventInstance = this; - nmEvents.ifnameEth0 = nmUtils::ethIface(); - nmEvents.ifnameWlan0 = nmUtils::wlanIface(); } /* Gnome networkmanger new events */ @@ -506,7 +527,7 @@ namespace WPEFramework void GnomeNetworkManagerEvents::onInterfaceStateChangeCb(uint8_t newState, std::string iface) { - std::string state = ""; + std::string state {}; switch (newState) { case Exchange::INetworkManager::INTERFACE_ADDED: @@ -530,8 +551,9 @@ namespace WPEFramework default: state = "Unknown"; } + NMLOG_DEBUG("%s interface state changed - %s", iface.c_str(), state.c_str()); - if(_instance != nullptr && (iface == _nmEventInstance->nmEvents.ifnameWlan0 || iface == _nmEventInstance->nmEvents.ifnameEth0)) + if(_instance != nullptr && (iface == nmUtils::wlanIface() || iface == nmUtils::ethIface())) _instance->ReportInterfaceStateChange(static_cast(newState), iface); } @@ -607,11 +629,8 @@ namespace WPEFramework } ssidList.ToString(ssidListJson); - if(_nmEventInstance->debugLogs) { - _nmEventInstance->debugLogs = false; - NMLOG_INFO("Number of Access Points Available = %d", static_cast(accessPoints->len)); - NMLOG_DEBUG("Scanned APIs are = %s",ssidListJson.c_str()); - } + NMLOG_INFO("No of AP Available = %d", static_cast(accessPoints->len)); + //NMLOG_DEBUG("Scanned APIs are = %s",ssidListJson.c_str()); if(_nmEventInstance->doScanNotify) { _nmEventInstance->doScanNotify = false; @@ -619,14 +638,13 @@ namespace WPEFramework } } - void GnomeNetworkManagerEvents::setwifiScanOptions(bool doNotify, bool enableLogs) + void GnomeNetworkManagerEvents::setwifiScanOptions(bool doNotify) { doScanNotify.store(doNotify); if(!doScanNotify) { NMLOG_DEBUG("stop periodic wifi scan result notify"); } - debugLogs = enableLogs; } } // Plugin diff --git a/NetworkManagerGnomeEvents.h b/NetworkManagerGnomeEvents.h index d775aee9..69f08bdd 100644 --- a/NetworkManagerGnomeEvents.h +++ b/NetworkManagerGnomeEvents.h @@ -53,7 +53,7 @@ namespace WPEFramework static GnomeNetworkManagerEvents* getInstance(); bool startNetworkMangerEventMonitor(); void stopNetworkMangerEventMonitor(); - void setwifiScanOptions(bool doNotify, bool enableLogs = false); + void setwifiScanOptions(bool doNotify); private: static void* networkMangerEventMonitor(void *arg); @@ -61,7 +61,6 @@ namespace WPEFramework ~GnomeNetworkManagerEvents(); std::atomicisEventThrdActive = {false}; std::atomicdoScanNotify = {false}; - std::atomicdebugLogs = {false}; NMEvents nmEvents; GThread *eventThrdID; }; diff --git a/NetworkManagerGnomeProxy.cpp b/NetworkManagerGnomeProxy.cpp index b8780aaa..bf8b66bd 100644 --- a/NetworkManagerGnomeProxy.cpp +++ b/NetworkManagerGnomeProxy.cpp @@ -224,7 +224,6 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::SetInterfaceState(const string& interface/* @in */, const bool enabled /* @in */) { - std::string wifiname = nmUtils::wlanIface(), ethname = nmUtils::ethIface(); if(client == nullptr) { @@ -232,19 +231,18 @@ namespace WPEFramework return Core::ERROR_RPC_CALL_FAILED; } - if(interface.empty() || (wifiname != interface && ethname != interface)) + if(interface.empty() || (interface != nmUtils::wlanIface() && interface != nmUtils::ethIface())) { NMLOG_ERROR("interface: %s; not valied", interface.c_str()!=nullptr? interface.c_str():"empty"); return Core::ERROR_GENERAL; } - NMDevice *device = nm_client_get_device_by_iface(client, interface.c_str()); - if (device == NULL) { - NMLOG_FATAL("libnm doesn't have device corresponding to %s", interface.c_str()); + if(!wifi->setInterfaceState(interface, enabled)) + { + NMLOG_ERROR("interface state change failed"); return Core::ERROR_GENERAL; } - nm_device_set_managed(device, enabled); NMLOG_INFO("interface %s state: %s", interface.c_str(), enabled ? "enabled" : "disabled"); return Core::ERROR_NONE; } @@ -407,29 +405,40 @@ namespace WPEFramework } result.autoconfig = isAutoConnectEnabled(conn); - if(ipversion.empty() || ipversion == "null" || nmUtils::caseInsensitiveCompare(ipversion, "IPV4")) // default ipversion ipv4 + + if(ipversion.empty() || nmUtils::caseInsensitiveCompare(ipversion, "IPV4")) // default ipversion ipv4 { ip4_config = nm_active_connection_get_ip4_config(conn); NMIPAddress *ipAddr = NULL; std::string ipStr; - if (ip4_config != NULL) { - const GPtrArray *ipByte; - ipByte = nm_ip_config_get_addresses(ip4_config); - for (int i = 0; i < ipByte->len; i++) { - ipAddr = static_cast(ipByte->pdata[i]); - if(ipAddr) - ipStr = nm_ip_address_get_address(ipAddr); - if(!ipStr.empty()) - { - result.ipaddress = nm_ip_address_get_address(ipAddr); - result.prefix = nm_ip_address_get_prefix(ipAddr); - NMLOG_INFO("IPv4 addr: %s/%d", result.ipaddress.c_str(), result.prefix); - result.ipversion = "IPv4"; // if null add as default - } + if (ip4_config == nullptr) { + NMLOG_WARNING("no IPv4 configurtion on %s", interface.c_str()); + return Core::ERROR_GENERAL; + } + + const GPtrArray *ipByte = nullptr; + ipByte = nm_ip_config_get_addresses(ip4_config); + if (ipByte == nullptr) { + NMLOG_WARNING("No IPv4 data found on %s", interface.c_str()); + return Core::ERROR_GENERAL; + } + + for (int i = 0; i < ipByte->len; i++) + { + ipAddr = static_cast(ipByte->pdata[i]); + if(ipAddr) + ipStr = nm_ip_address_get_address(ipAddr); + if(!ipStr.empty()) + { + result.ipaddress = nm_ip_address_get_address(ipAddr); + result.prefix = nm_ip_address_get_prefix(ipAddr); + NMLOG_INFO("IPv4 addr: %s/%d", result.ipaddress.c_str(), result.prefix); + result.ipversion = "IPv4"; // if null add as default } - gateway = nm_ip_config_get_gateway(ip4_config); } + gateway = nm_ip_config_get_gateway(ip4_config); + dnsArr = (char **)nm_ip_config_get_nameservers(ip4_config); dhcp4_config = nm_active_connection_get_dhcp4_config(conn); if(dhcp4_config) @@ -453,54 +462,52 @@ namespace WPEFramework ip6_config = nm_active_connection_get_ip6_config(conn); if(ip6_config == nullptr) { - NMLOG_WARNING("no ipv6 config found"); - rc = Core::ERROR_GENERAL; + NMLOG_WARNING("no IPv6 configurtion on %s", interface.c_str()); + return Core::ERROR_GENERAL; } - else + + std::string ipStr; + const GPtrArray *ipArray = nullptr; + ipArray = nm_ip_config_get_addresses(ip6_config); + for (int i = 0; i < ipArray->len; i++) { - std::string ipStr; - const GPtrArray *ipArray = nullptr; - ipArray = nm_ip_config_get_addresses(ip6_config); - for (int i = 0; i < ipArray->len; i++) + ipAddr = static_cast(ipArray->pdata[i]); + if(ipAddr) + ipStr = nm_ip_address_get_address(ipAddr); + if(!ipStr.empty()) { - ipAddr = static_cast(ipArray->pdata[i]); - if(ipAddr) - ipStr = nm_ip_address_get_address(ipAddr); - if(!ipStr.empty()) - { - if (ipStr.compare(0, 5, "fe80:") == 0 || ipStr.compare(0, 6, "fe80::") == 0) { - result.ula = ipStr; - NMLOG_INFO("link-local ip: %s", result.ula.c_str()); - } - else { - result.prefix = nm_ip_address_get_prefix(ipAddr); - if(result.ipaddress.empty()) // SLAAC mutiple ip not added - result.ipaddress = ipStr; - NMLOG_INFO("global ip %s/%d", ipStr.c_str(), result.prefix); - } + if (ipStr.compare(0, 5, "fe80:") == 0 || ipStr.compare(0, 6, "fe80::") == 0) { + result.ula = ipStr; + NMLOG_INFO("link-local ip: %s", result.ula.c_str()); + } + else { + result.prefix = nm_ip_address_get_prefix(ipAddr); + if(result.ipaddress.empty()) // SLAAC mutiple ip not added + result.ipaddress = ipStr; + NMLOG_INFO("global ip %s/%d", ipStr.c_str(), result.prefix); } } + } - gateway = nm_ip_config_get_gateway(ip6_config); - if(gateway) - result.gateway= gateway; - dnsArr = (char **)nm_ip_config_get_nameservers(ip6_config); - if((*(&dnsArr[0]))!= NULL) - result.primarydns = *(&dnsArr[0]); - if((*(&dnsArr[1]))!=NULL ) - result.secondarydns = *(&dnsArr[1]); - - dhcp6_config = nm_active_connection_get_dhcp6_config(conn); - if(dhcp6_config) - { - dhcpserver = nm_dhcp_config_get_one_option (dhcp6_config, "dhcp_server_identifier"); - if(dhcpserver) { - result.dhcpserver = dhcpserver; - } + gateway = nm_ip_config_get_gateway(ip6_config); + if(gateway) + result.gateway= gateway; + dnsArr = (char **)nm_ip_config_get_nameservers(ip6_config); + if((*(&dnsArr[0]))!= NULL) + result.primarydns = *(&dnsArr[0]); + if((*(&dnsArr[1]))!=NULL ) + result.secondarydns = *(&dnsArr[1]); + + dhcp6_config = nm_active_connection_get_dhcp6_config(conn); + if(dhcp6_config) + { + dhcpserver = nm_dhcp_config_get_one_option (dhcp6_config, "dhcp_server_identifier"); + if(dhcpserver) { + result.dhcpserver = dhcpserver; } - result.ipversion = "IPv6"; - rc = Core::ERROR_NONE; } + result.ipversion = "IPv6"; + rc = Core::ERROR_NONE; } else NMLOG_WARNING("ipversion error IPv4/IPv6"); @@ -518,6 +525,7 @@ namespace WPEFramework } else { NMLOG_DEBUG("Deactivating connection successful"); } + g_main_loop_quit((GMainLoop*)user_data); } // Callback for nm_client_activate_connection_async @@ -644,7 +652,7 @@ namespace WPEFramework uint32_t rc = Core::ERROR_RPC_CALL_FAILED; (void) ssids; - nmEvent->setwifiScanOptions(true, true); + nmEvent->setwifiScanOptions(true); if(wifi->wifiScanRequest(frequency)) rc = Core::ERROR_NONE; return rc; @@ -710,7 +718,7 @@ namespace WPEFramework // Check the last scanning time and if it exceeds 5 sec do a rescanning if(!wifi->isWifiScannedRecently()) { - nmEvent->setwifiScanOptions(false, false); + nmEvent->setwifiScanOptions(false); if(!wifi->wifiScanRequest()) { NMLOG_WARNING("scanning failed but try to connect"); diff --git a/NetworkManagerGnomeUtils.cpp b/NetworkManagerGnomeUtils.cpp index d6099c6d..63a4a3b0 100644 --- a/NetworkManagerGnomeUtils.cpp +++ b/NetworkManagerGnomeUtils.cpp @@ -216,20 +216,20 @@ namespace WPEFramework ssidStr = nm_utils_ssid_to_utf8((const guint8*)g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid)); string ssidString(ssidStr); ssidObj["ssid"] = ssidString; + strength = nm_access_point_get_strength(ap); + apFreq = nm_access_point_get_frequency(ap); + flags = nm_access_point_get_flags(ap); + wpaFlags = nm_access_point_get_wpa_flags(ap); + rsnFlags = nm_access_point_get_rsn_flags(ap); + freq = nmUtils::wifiFrequencyFromAp(apFreq); + security = nmUtils::wifiSecurityModeFromAp(flags, wpaFlags, rsnFlags); + + ssidObj["security"] = security; + ssidObj["strength"] = nmUtils::convertPercentageToSignalStrengtStr(strength); + ssidObj["frequency"] = freq; } else - ssidObj["ssid"] = "---"; // hidden ssid TODO modify - strength = nm_access_point_get_strength(ap); - apFreq = nm_access_point_get_frequency(ap); - flags = nm_access_point_get_flags(ap); - wpaFlags = nm_access_point_get_wpa_flags(ap); - rsnFlags = nm_access_point_get_rsn_flags(ap); - freq = nmUtils::wifiFrequencyFromAp(apFreq); - security = nmUtils::wifiSecurityModeFromAp(flags, wpaFlags, rsnFlags); - - ssidObj["security"] = security; - ssidObj["signalStrength"] = strength; - ssidObj["frequency"] = freq; + NMLOG_DEBUG("hidden ssid found, bssid: %s", nm_access_point_get_bssid(ap)); return ssidObj; } diff --git a/NetworkManagerGnomeWIFI.cpp b/NetworkManagerGnomeWIFI.cpp index a16b4456..4d665b91 100644 --- a/NetworkManagerGnomeWIFI.cpp +++ b/NetworkManagerGnomeWIFI.cpp @@ -236,7 +236,7 @@ namespace WPEFramework NMLOG_DEBUG("bitrate : %s kbit/s", wifiInfo.rate.c_str()); //TODO signal strenght to dBm wifiInfo.strength = std::string(nmUtils::convertPercentageToSignalStrengtStr(strength)); - NMLOG_DEBUG("sterngth: %s %%", wifiInfo.strength.c_str()); + NMLOG_DEBUG("sterngth: %s dbm", wifiInfo.strength.c_str()); wifiInfo.security = static_cast(nmUtils::wifiSecurityModeFromAp(flags, wpaFlags, rsnFlags)); NMLOG_DEBUG("security %s", nmUtils::getSecurityModeString(flags, wpaFlags, rsnFlags).c_str()); NMLOG_DEBUG("Mode: %s", mode == NM_802_11_MODE_ADHOC ? "Ad-Hoc": mode == NM_802_11_MODE_INFRA ? "Infrastructure": "Unknown"); @@ -249,13 +249,13 @@ namespace WPEFramework NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); if(wifiDevice == NULL) { - NMLOG_DEBUG("NMDeviceWifi * NULL !"); + NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; } NMAccessPoint *activeAP = nm_device_wifi_get_active_access_point(wifiDevice); if(activeAP == NULL) { - NMLOG_DEBUG("No active access point found !"); + NMLOG_INFO("No active access point found !"); return false; } else @@ -270,7 +270,7 @@ namespace WPEFramework NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); if(wifiDevice == NULL) { - NMLOG_DEBUG("NMDeviceWifi * NULL !"); + NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; } @@ -286,7 +286,7 @@ namespace WPEFramework return true; } - static void wifiDisconnectCb(GObject *object, GAsyncResult *result, gpointer user_data) + static void disconnectCb(GObject *object, GAsyncResult *result, gpointer user_data) { NMDevice *device = NM_DEVICE(object); GError *error = NULL; @@ -316,11 +316,11 @@ namespace WPEFramework NMDevice *wifiNMDevice = getNmDevice(); if(wifiNMDevice == NULL) { - NMLOG_ERROR("NMDeviceWifi NULL !"); - return false; + NMLOG_WARNING("wifi state is unmanaged !"); + return true; } - nm_device_disconnect_async(wifiNMDevice, NULL, wifiDisconnectCb, this); + nm_device_disconnect_async(wifiNMDevice, NULL, disconnectCb, this); wait(loop); return isSuccess; } @@ -887,7 +887,7 @@ namespace WPEFramework return false; NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); if(wifiDevice == NULL) { - NMLOG_DEBUG("NMDeviceWifi * NULL !"); + NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; } isSuccess = false; @@ -993,5 +993,83 @@ namespace WPEFramework return true; } + static void deviceManagedCb(GObject *object, GAsyncResult *result, gpointer user_data) + { + wifiManager *_wifiManager = static_cast(user_data); + GError *error = nullptr; + + if (!nm_client_dbus_set_property_finish(NM_CLIENT(object), result, &error)) { + if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + g_error_free(error); + return; + } + + NMLOG_ERROR("Failed to set Managed property: %s", error->message); + g_error_free(error); + _wifiManager->isSuccess = false; + } else { + NMLOG_DEBUG("Successfully set Managed property."); + _wifiManager->isSuccess = true; + } + + _wifiManager->quit(nullptr); + } + + bool wifiManager::setInterfaceState(std::string interface, bool enabled) + { + isSuccess = false; + NMDevice *device = nullptr; + + if (!createClientNewConnection()) + return false; + + GPtrArray *devices = const_cast(nm_client_get_devices(client)); + if (devices == nullptr) { + NMLOG_ERROR("Failed to get device list."); + return isSuccess; + } + + for (guint j = 0; j < devices->len; j++) { + device = NM_DEVICE(devices->pdata[j]); + const char *ifaceStr = nm_device_get_iface(device); + if (ifaceStr == nullptr) + continue; + if (interface == ifaceStr) { + // NMLOG_DEBUG("Device found: %s", interface.c_str()); + break; + } else { + device = nullptr; + } + } + + if (device == nullptr) + return false; + + NMDeviceState deviceState = nm_device_get_state(device); + + if (enabled) { + NMLOG_DEBUG("Enabling interface..."); + if (deviceState >= NM_DEVICE_STATE_DISCONNECTED) // already enabled + return true; + } else { + NMLOG_DEBUG("Disabling interface..."); + if (deviceState <= NM_DEVICE_STATE_UNMANAGED) // already disabled + return true; + else if (deviceState > NM_DEVICE_STATE_DISCONNECTED) { + nm_device_disconnect_async(device, nullptr, disconnectCb, this); + wait(loop); + sleep(1); // to remove the connection + } + } + + const char *objectPath = nm_object_get_path(NM_OBJECT(device)); + GVariant *value = g_variant_new_boolean(enabled); + + nm_client_dbus_set_property( client, objectPath, NM_DBUS_INTERFACE_DEVICE,"Managed", + value, -1, nullptr, deviceManagedCb, this); + wait(loop); + return isSuccess; + } + } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManagerGnomeWIFI.h b/NetworkManagerGnomeWIFI.h index 4a646225..74704015 100644 --- a/NetworkManagerGnomeWIFI.h +++ b/NetworkManagerGnomeWIFI.h @@ -55,6 +55,7 @@ namespace WPEFramework bool wait(GMainLoop *loop, int timeOutMs = 10000); // default maximium set as 10 sec bool initiateWPS(); bool cancelWPS(); + bool setInterfaceState(std::string interface, bool enabled); private: NMDevice *getNmDevice();