diff --git a/LegacyPlugin_WiFiManagerAPIs.cpp b/LegacyPlugin_WiFiManagerAPIs.cpp index 9b7ec736..4a9e0a56 100644 --- a/LegacyPlugin_WiFiManagerAPIs.cpp +++ b/LegacyPlugin_WiFiManagerAPIs.cpp @@ -250,32 +250,14 @@ namespace WPEFramework { LOG_INPARAM(); uint32_t rc = Core::ERROR_GENERAL; + string ssid{}; + ssid = parameters["ssid"].String(); auto _nwmgr = m_service->QueryInterfaceByCallsign(NETWORK_MANAGER_CALLSIGN); - if(_nwmgr) + if (_nwmgr) { - std::string ssid = {}; - if (parameters.HasLabel("ssid")) - { - ssid = parameters["ssid"].String(); - rc = _nwmgr->RemoveKnownSSID(ssid); - } - else - { - ::WPEFramework::RPC::IIteratorType* _ssids{}; - rc = _nwmgr->GetKnownSSIDs(_ssids); - if (Core::ERROR_NONE == rc && _ssids != nullptr) - { - string _resultItem_{}; - while (_ssids->Next(_resultItem_) == true) { - /* remove all the ssid */ - rc = _nwmgr->RemoveKnownSSID(_resultItem_); - } - _ssids->Release(); - } - } - - _nwmgr->Release(); + rc = _nwmgr->RemoveKnownSSID(ssid); + _nwmgr->Release(); } else rc = Core::ERROR_UNAVAILABLE; diff --git a/NetworkManagerGnomeProxy.cpp b/NetworkManagerGnomeProxy.cpp index fa062ca1..a95ab1ac 100644 --- a/NetworkManagerGnomeProxy.cpp +++ b/NetworkManagerGnomeProxy.cpp @@ -518,16 +518,8 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::SetIPSettings(const string& interface /* @in */, const IPAddress& address /* @in */) { uint32_t rc = Core::ERROR_GENERAL; - if(nmUtils::caseInsensitiveCompare("IPv6", address.ipversion)) - { - NMLOG_WARNING("Setting IPv6 is not supported at this point in time"); - rc = Core::ERROR_NOT_SUPPORTED; - } - else - { - if(wifi->setIpSettings(interface, address)) - rc = Core::ERROR_NONE; - } + if(wifi->setIpSettings(interface, address)) + rc = Core::ERROR_NONE; return rc; } @@ -675,12 +667,6 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StartWPS(const WiFiWPS& method /* @in */, const string& wps_pin /* @in */) { uint32_t rc = Core::ERROR_NONE; - if(method != WIFI_WPS_PBC) - { - NMLOG_ERROR("wps method not support only support wps-pbc"); - return Core::ERROR_GENERAL; - } - if(wifi->initiateWPS()) NMLOG_INFO ("startWPS success"); else diff --git a/NetworkManagerGnomeUtils.cpp b/NetworkManagerGnomeUtils.cpp index 6b10ec2a..3d4b9aa3 100644 --- a/NetworkManagerGnomeUtils.cpp +++ b/NetworkManagerGnomeUtils.cpp @@ -268,6 +268,32 @@ namespace WPEFramework } } + NMDevice* nmUtils::getEthDevice(NMClient *client) + { + NMDevice *ethDevice = NULL; + GPtrArray *devices = const_cast(nm_client_get_devices(client)); + if (devices == NULL) { + NMLOG_ERROR("Failed to get device list."); + return ethDevice; + } + + for (guint j = 0; j < devices->len; j++) + { + NMDevice *device = NM_DEVICE(devices->pdata[j]); + const char* interface = nm_device_get_iface(device); + if(interface == nullptr) + continue; + std::string iface = interface; + if (iface == nmUtils::ethIface()) + { + ethDevice = device; + break; + } + } + + return ethDevice; + } + bool nmUtils::caseInsensitiveCompare(const std::string& str1, const std::string& str2) { std::string upperStr1 = str1; std::string upperStr2 = str2; diff --git a/NetworkManagerGnomeUtils.h b/NetworkManagerGnomeUtils.h index d305fc7f..27bb2336 100644 --- a/NetworkManagerGnomeUtils.h +++ b/NetworkManagerGnomeUtils.h @@ -36,6 +36,7 @@ namespace WPEFramework static bool getInterfacesName(); static const char* wlanIface(); static const char* ethIface(); + static NMDevice* getEthDevice(NMClient *client); static const char* convertPercentageToSignalStrengtStr(int percentage); static bool caseInsensitiveCompare(const std::string& str1, const std::string& str2); static uint8_t wifiSecurityModeFromAp(guint32 flags, guint32 wpaFlags, guint32 rsnFlags); diff --git a/NetworkManagerGnomeWIFI.cpp b/NetworkManagerGnomeWIFI.cpp index c494fc68..6f59f392 100644 --- a/NetworkManagerGnomeWIFI.cpp +++ b/NetworkManagerGnomeWIFI.cpp @@ -1175,8 +1175,7 @@ namespace WPEFramework NMDeviceState deviceState = nm_device_get_state(device); - if (enabled) - { + if (enabled) { NMLOG_DEBUG("Enabling interface..."); if (deviceState >= NM_DEVICE_STATE_DISCONNECTED) // already enabled { @@ -1188,8 +1187,7 @@ namespace WPEFramework return true; } } - else - { + else { NMLOG_DEBUG("Disabling interface..."); if (deviceState <= NM_DEVICE_STATE_UNMANAGED) // already disabled { @@ -1253,124 +1251,138 @@ namespace WPEFramework bool wifiManager::setIpSettings(const string interface, const Exchange::INetworkManager::IPAddress address) { isSuccess = false; - NMSettingIP4Config *s_ip4 = nullptr; - NMSettingIP6Config *s_ip6 = nullptr; - NMConnection *connection = nullptr; - NMSettingConnection *settings = nullptr; - NMRemoteConnection *remote_connection = nullptr; - NMSetting *setting = nullptr; - NMDevice *device = nullptr; - const char *spec_object = nullptr; - const char *uuid = nullptr; + NMConnection *connection = NULL; + NMRemoteConnection *remoteConn = NULL; + NMActiveConnection* activeConnection = NULL; + NMSetting *setting = NULL; + NMDevice *device = NULL; + const char *specObject = NULL; if (!createClientNewConnection()) return false; - const GPtrArray *connections = nm_client_get_connections(client); - if(connections == nullptr) - { - NMLOG_WARNING("no connections availble to edit "); + device = nm_client_get_device_by_iface(client, interface.c_str()); + if(device == NULL) return false; - } - for (guint i = 0; i < connections->len; i++) + if(interface == nmUtils::ethIface()) { - NMConnection *tmpConn = NM_CONNECTION(connections->pdata[i]); - if(tmpConn == nullptr) - continue; - settings = nm_connection_get_setting_connection(connection); + NMSettingConnection *settings = NULL; + if(device == NULL) + return false; - /* Check if the interface name matches */ - if (g_strcmp0(nm_setting_connection_get_interface_name(settings), interface.c_str()) == 0) { - // FIXME : multiple wifi connection available only first wifi connection will take - connection = tmpConn; - break; + const GPtrArray *connections = nm_device_get_available_connections(device); + if (connections == NULL || connections->len == 0) + { + NMLOG_WARNING("no connections availble to edit "); + return false; + } + + for (guint i = 0; i < connections->len; i++) + { + NMConnection *tmpConn = NM_CONNECTION(connections->pdata[i]); + if(tmpConn == nullptr) + continue; + settings = nm_connection_get_setting_connection(connection); + if (g_strcmp0(nm_setting_connection_get_interface_name(settings), interface.c_str()) == 0) { + connection = tmpConn; + + if (NM_IS_REMOTE_CONNECTION(connection)) { + remoteConn = NM_REMOTE_CONNECTION(connection); + } else { + NMLOG_ERROR("The connection is not a remote connection."); + return false; /* connection and remoteconnection should match */ + } + + NMLOG_DEBUG("ethernet connection found "); + break; + } + } + } + else if(interface == nmUtils::wlanIface()) + { + /* for wifi we need active connection to modify becuse of multiple wifi connection */ + activeConnection = nm_device_get_active_connection(device); + if(activeConnection == NULL) + { + NMLOG_ERROR("no active connection for wifi"); + return false; } + remoteConn = nm_active_connection_get_connection(activeConnection); + connection = NM_CONNECTION(remoteConn); } - // FIXME : multiple wifi connection case if(connection == nullptr) { - // no wifi/eth connection added do we need to return error? NMLOG_WARNING("not a single connection availble for %s", interface.c_str()); return false; } - if (!address.autoconfig) + if(address.autoconfig) { + NMSettingIP4Config *sIp4 = nullptr; + NMSettingIP6Config *sIp6 = nullptr; if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) { - NMSettingIPConfig *ip4_config = nm_connection_get_setting_ip4_config(connection); - if (ip4_config == NULL) + if(checkAutoConnectEnabledInIPv4Conn(connection)) // already auto connect true connection { - ip4_config = (NMSettingIPConfig *)nm_setting_ip4_config_new(); + NMLOG_INFO("Setting IPv4 auto connect enabled"); + return true; // no need to modify } - NMIPAddress *ipAddress; - setting = nm_connection_get_setting_by_name(connection, "ipv4"); - ipAddress = nm_ip_address_new(AF_INET, address.ipaddress.c_str(), address.prefix, NULL); - nm_setting_ip_config_clear_addresses(ip4_config); - nm_setting_ip_config_add_address(NM_SETTING_IP_CONFIG(setting), ipAddress); - nm_setting_ip_config_clear_dns(ip4_config); - nm_setting_ip_config_add_dns(ip4_config, address.primarydns.c_str()); - nm_setting_ip_config_add_dns(ip4_config, address.secondarydns.c_str()); - - g_object_set(G_OBJECT(ip4_config), - NM_SETTING_IP_CONFIG_GATEWAY, address.gateway.c_str(), - NM_SETTING_IP_CONFIG_NEVER_DEFAULT, - FALSE, - NULL); + sIp4 = (NMSettingIP4Config *)nm_setting_ip4_config_new(); + g_object_set(G_OBJECT(sIp4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); + nm_connection_add_setting(connection, NM_SETTING(sIp4)); } else { - //FIXME : Add IPv6 support here - NMLOG_WARNING("Setting IPv6 is not supported at this point in time. This is just a place holder"); + sIp6 = (NMSettingIP6Config *)nm_setting_ip6_config_new(); + g_object_set(G_OBJECT(sIp6), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + nm_connection_add_setting(connection, NM_SETTING(sIp6)); } } - else // autoconfig true + else { if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) { - if(checkAutoConnectEnabledInIPv4Conn(connection)) // already auto connect true connection + NMSettingIPConfig *ip4Config = nm_connection_get_setting_ip4_config(connection); + if (ip4Config == nullptr) { - NMLOG_INFO("Setting IPv4 auto connect enabled"); - return true; // no need to modify + ip4Config = (NMSettingIPConfig *)nm_setting_ip4_config_new(); } - s_ip4 = (NMSettingIP4Config *)nm_setting_ip4_config_new(); - g_object_set(G_OBJECT(s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - nm_connection_add_setting(connection, NM_SETTING(s_ip4)); + NMIPAddress *ipAddress; + setting = nm_connection_get_setting_by_name(connection, "ipv4"); + ipAddress = nm_ip_address_new(AF_INET, address.ipaddress.c_str(), address.prefix, NULL); + nm_setting_ip_config_clear_addresses(ip4Config); + nm_setting_ip_config_add_address(NM_SETTING_IP_CONFIG(setting), ipAddress); + nm_setting_ip_config_clear_dns(ip4Config); + nm_setting_ip_config_add_dns(ip4Config, address.primarydns.c_str()); + nm_setting_ip_config_add_dns(ip4Config, address.secondarydns.c_str()); + + g_object_set(G_OBJECT(ip4Config),NM_SETTING_IP_CONFIG_GATEWAY, address.gateway.c_str(), NULL); + g_object_set(G_OBJECT(ip4Config),NM_SETTING_IP_CONFIG_NEVER_DEFAULT, FALSE, NULL); + g_object_set(G_OBJECT(ip4Config), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL, NULL); } else { - s_ip6 = (NMSettingIP6Config *)nm_setting_ip6_config_new(); - g_object_set(G_OBJECT(s_ip6), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - nm_connection_add_setting(connection, NM_SETTING(s_ip6)); - } - } - device = nm_client_get_device_by_iface(client, interface.c_str()); - uuid = nm_connection_get_uuid(connection); - remote_connection = nm_client_get_connection_by_uuid(client, uuid); - // FIXME: if connection is not active it will fail - NMActiveConnection *active_connection = nullptr; - const GPtrArray *acv_connections = nm_client_get_active_connections(client); - for (guint i = 0; i < acv_connections->len; i++) { - NMActiveConnection *connection1 = NM_ACTIVE_CONNECTION(acv_connections->pdata[i]); - settings = nm_connection_get_setting_connection(NM_CONNECTION(nm_active_connection_get_connection(connection1))); - - /* Check if the interface name matches */ - if (g_strcmp0(nm_setting_connection_get_interface_name(settings), interface.c_str()) == 0) { - active_connection = connection1; - break; + //FIXME : Add IPv6 support here + NMLOG_WARNING("Setting IPv6 is not supported at this point in time. This is just a place holder"); } } // TODO fix depricated api - nm_remote_connection_commit_changes(remote_connection, false, NULL, NULL); - if(active_connection != nullptr) - { - spec_object = nm_object_get_path(NM_OBJECT(active_connection)); - nm_client_deactivate_connection(client, active_connection, NULL, NULL); + nm_remote_connection_commit_changes(remoteConn, false, NULL, NULL); + if (activeConnection != NULL) { + specObject = nm_object_get_path(NM_OBJECT(activeConnection)); + GError *deactivate_error = NULL; + // TODO fix depricated api + if (!nm_client_deactivate_connection(client, activeConnection, NULL, &deactivate_error)) { + NMLOG_ERROR("Failed to deactivate connection: %s", deactivate_error->message); + g_clear_error(&deactivate_error); + return false; + } } - nm_client_activate_connection_async(client, connection, device, spec_object, NULL, on_activate_complete, this); + + nm_client_activate_connection_async(client, connection, device, specObject, NULL, on_activate_complete, this); wait(loop); return isSuccess; } diff --git a/NetworkManagerJsonRpc.cpp b/NetworkManagerJsonRpc.cpp index 4af043b6..bb143f0d 100644 --- a/NetworkManagerJsonRpc.cpp +++ b/NetworkManagerJsonRpc.cpp @@ -330,49 +330,29 @@ namespace WPEFramework Exchange::INetworkManager::IPAddress address{}; string interface = {}; - string ipversion = {}; - if (parameters.HasLabel("interface")) - interface = parameters["interface"].String(); - else - { + if (!parameters.HasLabel("interface") || !parameters.HasLabel("ipversion") || !parameters.HasLabel("autoconfig")) rc = Core::ERROR_BAD_REQUEST; - return rc; - } - - if (parameters.HasLabel("ipversion")) - { - ipversion = parameters["ipversion"].String(); - address.ipversion = ipversion; - } - - if(ipversion.empty()) - { - rc = Core::ERROR_BAD_REQUEST; - return rc; - } - - if (parameters.HasLabel("autoconfig")) - { - address.autoconfig = parameters["autoconfig"].Boolean(); - } else - address.autoconfig = true; - - if (!address.autoconfig) { - address.ipaddress = parameters["ipaddress"].String(); - address.prefix = parameters["prefix"].Number(); - address.gateway = parameters["gateway"].String(); - address.primarydns = parameters["primarydns"].String(); - address.secondarydns = parameters["secondarydns"].String(); - } + interface = parameters["interface"].String(); + address.ipversion = parameters["ipversion"].String(); + address.autoconfig = parameters["autoconfig"].Boolean(); - if (_networkManager) - rc = _networkManager->SetIPSettings(interface, address); - else - rc = Core::ERROR_UNAVAILABLE; + if (!address.autoconfig) + { + address.ipaddress = parameters["ipaddress"].String(); + address.prefix = parameters["prefix"].Number(); + address.gateway = parameters["gateway"].String(); + address.primarydns = parameters["primarydns"].String(); + address.secondarydns = parameters["secondarydns"].String(); + } + if (_networkManager) + rc = _networkManager->SetIPSettings(interface, address); + else + rc = Core::ERROR_UNAVAILABLE; + } returnJson(rc); }