diff --git a/NetworkManagerGnomeEvents.cpp b/NetworkManagerGnomeEvents.cpp index 56e94be4..c5c5c723 100644 --- a/NetworkManagerGnomeEvents.cpp +++ b/NetworkManagerGnomeEvents.cpp @@ -624,8 +624,8 @@ namespace WPEFramework { JsonObject ssidObj; ap = static_cast(accessPoints->pdata[i]); - ssidObj = nmUtils::apToJsonObject(ap); - ssidList.Add(ssidObj); + if(nmUtils::apToJsonObject(ap, ssidObj)) + ssidList.Add(ssidObj); } ssidList.ToString(ssidListJson); diff --git a/NetworkManagerGnomeProxy.cpp b/NetworkManagerGnomeProxy.cpp index bf8b66bd..b6145ee4 100644 --- a/NetworkManagerGnomeProxy.cpp +++ b/NetworkManagerGnomeProxy.cpp @@ -514,136 +514,12 @@ namespace WPEFramework return rc; } - // Callback for nm_client_deactivate_connection_async - static void on_deactivate_complete(GObject *source_object, GAsyncResult *res, gpointer user_data) { - GError *error = NULL; - - // Check if the operation was successful - if (!nm_client_deactivate_connection_finish(NM_CLIENT(source_object), res, &error)) { - NMLOG_DEBUG("Deactivating connection failed: %s", error->message); - g_error_free(error); - } else { - NMLOG_DEBUG("Deactivating connection successful"); - } - g_main_loop_quit((GMainLoop*)user_data); - } - - // Callback for nm_client_activate_connection_async - static void on_activate_complete(GObject *source_object, GAsyncResult *res, gpointer user_data) { - GError *error = NULL; - - // Check if the operation was successful - if (!nm_client_activate_connection_finish(NM_CLIENT(source_object), res, &error)) { - NMLOG_DEBUG("Activating connection failed: %s", error->message); - g_error_free(error); - } else { - NMLOG_DEBUG("Activating connection successful"); - } - - g_main_loop_quit((GMainLoop*)user_data); - } - - /* @brief Set IP Address Of the Interface */ uint32_t NetworkManagerImplementation::SetIPSettings(const string& interface /* @in */, const IPAddress& address /* @in */) { - GMainLoop *g_loop; - g_loop = g_main_loop_new(NULL, FALSE); - uint32_t rc = Core::ERROR_NONE; - if(client == nullptr) - { - NMLOG_WARNING("client connection null"); - return Core::ERROR_GENERAL; - } - const GPtrArray *connections = nm_client_get_connections(client); - NMSettingIP4Config *s_ip4; - NMSettingIP6Config *s_ip6; - NMConnection *conn = NULL; - NMSettingConnection *settings; - NMRemoteConnection *remote_connection; - NMSetting *setting; - const char *uuid; - NMDevice *device = NULL; - const char *spec_object; - - for (guint i = 0; i < connections->len; i++) { - NMConnection *connection = NM_CONNECTION(connections->pdata[i]); - settings = nm_connection_get_setting_connection(connection); - - /* Check if the interface name matches */ - if (g_strcmp0(nm_setting_connection_get_interface_name(settings), interface.c_str()) == 0) { - conn = connection; - break; - } - } - if (!address.autoconfig) - { - if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) - { - NMSettingIPConfig *ip4_config = nm_connection_get_setting_ip4_config(conn); - if (ip4_config == NULL) - { - ip4_config = (NMSettingIPConfig *)nm_setting_ip4_config_new(); - } - NMIPAddress *ipAddress; - setting = nm_connection_get_setting_by_name(conn, "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); - } - else - { - //FIXME : Add IPv6 support here - NMLOG_WARNING("Setting IPv6 is not supported at this point in time. This is just a place holder"); - rc = Core::ERROR_NOT_SUPPORTED; - } - } - else - { - if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) - { - 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(conn, NM_SETTING(s_ip4)); - } - 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(conn, NM_SETTING(s_ip6)); - } - } - device = nm_client_get_device_by_iface(client, interface.c_str()); - uuid = nm_connection_get_uuid(conn); - remote_connection = nm_client_get_connection_by_uuid(client, uuid); - NMActiveConnection *active_connection = NULL; - - 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; - } - } - - spec_object = nm_object_get_path(NM_OBJECT(active_connection)); - nm_remote_connection_commit_changes(remote_connection, false, NULL, NULL); - nm_client_deactivate_connection_async(client, active_connection, NULL, on_deactivate_complete, NULL); - nm_client_activate_connection_async(client, conn, device, spec_object, NULL, on_activate_complete, g_loop); - g_main_loop_run(g_loop); + uint32_t rc = Core::ERROR_GENERAL; + if(wifi->setIpSettings(interface, address)) + rc = Core::ERROR_NONE; return rc; } diff --git a/NetworkManagerGnomeUtils.cpp b/NetworkManagerGnomeUtils.cpp index 63a4a3b0..6b10ec2a 100644 --- a/NetworkManagerGnomeUtils.cpp +++ b/NetworkManagerGnomeUtils.cpp @@ -198,17 +198,15 @@ namespace WPEFramework return freq; } - JsonObject nmUtils::apToJsonObject(NMAccessPoint *ap) + bool nmUtils::apToJsonObject(NMAccessPoint *ap, JsonObject& ssidObj) { - GError *error = NULL; GBytes *ssid = NULL; int strength = 0; std::string freq; int security; guint32 flags, wpaFlags, rsnFlags, apFreq; - JsonObject ssidObj; if(ap == nullptr) - return ssidObj; + return false; ssid = nm_access_point_get_ssid(ap); if (ssid) { @@ -227,11 +225,11 @@ namespace WPEFramework ssidObj["security"] = security; ssidObj["strength"] = nmUtils::convertPercentageToSignalStrengtStr(strength); ssidObj["frequency"] = freq; + return true; } - else - NMLOG_DEBUG("hidden ssid found, bssid: %s", nm_access_point_get_bssid(ap)); - - return ssidObj; + // else + // NMLOG_DEBUG("hidden ssid found, bssid: %s", nm_access_point_get_bssid(ap)); + return false; } void nmUtils::printActiveSSIDsOnly(NMDeviceWifi *wifiDevice) diff --git a/NetworkManagerGnomeUtils.h b/NetworkManagerGnomeUtils.h index f55e808f..d305fc7f 100644 --- a/NetworkManagerGnomeUtils.h +++ b/NetworkManagerGnomeUtils.h @@ -41,7 +41,7 @@ namespace WPEFramework static uint8_t wifiSecurityModeFromAp(guint32 flags, guint32 wpaFlags, guint32 rsnFlags); static std::string wifiFrequencyFromAp(guint32 apFreq); static std::string getSecurityModeString(guint32 flags, guint32 wpaFlags, guint32 rsnFlags); - static JsonObject apToJsonObject(NMAccessPoint *ap); + static bool apToJsonObject(NMAccessPoint *ap, JsonObject& ssidObj); static void printActiveSSIDsOnly(NMDeviceWifi *wifiDevice); static NMDeviceState ifaceState(NMClient *client, const char* interface); }; diff --git a/NetworkManagerGnomeWIFI.cpp b/NetworkManagerGnomeWIFI.cpp index 8fb62a70..22d8a1fc 100644 --- a/NetworkManagerGnomeWIFI.cpp +++ b/NetworkManagerGnomeWIFI.cpp @@ -123,7 +123,7 @@ namespace WPEFramework return true; } - NMDevice* wifiManager::getNmDevice() + NMDevice* wifiManager::getWifiDevice() { NMDevice *wifiDevice = NULL; @@ -248,7 +248,7 @@ namespace WPEFramework if(!createClientNewConnection()) return false; - NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); + NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getWifiDevice()); if(wifiDevice == NULL) { NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; @@ -269,7 +269,7 @@ namespace WPEFramework if(!createClientNewConnection()) return false; - NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); + NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getWifiDevice()); if(wifiDevice == NULL) { NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; @@ -315,7 +315,7 @@ namespace WPEFramework if(!createClientNewConnection()) return false; - NMDevice *wifiNMDevice = getNmDevice(); + NMDevice *wifiNMDevice = getWifiDevice(); if(wifiNMDevice == NULL) { NMLOG_WARNING("wifi state is unmanaged !"); return true; @@ -580,7 +580,7 @@ namespace WPEFramework if(!createClientNewConnection()) return false; - NMDevice *device = getNmDevice(); + NMDevice *device = getWifiDevice(); if(device == NULL) return false; @@ -725,7 +725,7 @@ namespace WPEFramework if(!createClientNewConnection()) return false; - NMDevice *device = getNmDevice(); + NMDevice *device = getWifiDevice(); if(device == NULL) return false; @@ -886,7 +886,7 @@ namespace WPEFramework { if(!createClientNewConnection()) return false; - NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); + NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getWifiDevice()); if(wifiDevice == NULL) { NMLOG_FATAL("NMDeviceWifi * NULL !"); return false; @@ -919,7 +919,7 @@ namespace WPEFramework if (!createClientNewConnection()) return false; - NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getNmDevice()); + NMDeviceWifi *wifiDevice = NM_DEVICE_WIFI(getWifiDevice()); if (wifiDevice == NULL) { NMLOG_ERROR("Invalid Wi-Fi device."); return false; @@ -1183,6 +1183,7 @@ namespace WPEFramework 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); @@ -1199,5 +1200,192 @@ namespace WPEFramework return isSuccess; } + static void onActivateComplete(GObject *source_object, GAsyncResult *res, gpointer user_data) + { + GError *error = NULL; + wifiManager *_wifiManager = static_cast(user_data); + NMLOG_DEBUG("activate connection completeing..."); + // Check if the operation was successful + if (!nm_client_activate_connection_finish(NM_CLIENT(source_object), res, &error)) { + NMLOG_DEBUG("Activating connection failed: %s", error->message); + g_error_free(error); + _wifiManager->isSuccess = false; + } else { + NMLOG_DEBUG("Activating connection successful"); + _wifiManager->isSuccess = true; + } + _wifiManager->quit(nullptr); + } + + bool static checkAutoConnectEnabledInIPv4Conn(NMConnection *connection) + { + if(connection == NULL) + return false; + NMSettingIPConfig *ipConfig = nm_connection_get_setting_ip4_config(connection); + if(ipConfig) + { + const char* ipConfMethod = nm_setting_ip_config_get_method (ipConfig); + if(ipConfMethod != NULL && g_strcmp0(ipConfMethod, "auto") == 0) + return true; + else + NMLOG_WARNING("ip configuration: %s", ipConfMethod != NULL? ipConfMethod: "null"); + } + + return false; + } + + bool wifiManager::setIpSettings(const string interface, const Exchange::INetworkManager::IPAddress address) + { + isSuccess = false; + NMConnection *connection = NULL; + NMRemoteConnection *remoteConn = NULL; + NMActiveConnection* activeConnection = NULL; + NMSetting *setting = NULL; + NMDevice *device = NULL; + const char *specObject = NULL; + + if (!createClientNewConnection()) + return false; + + device = nm_client_get_device_by_iface(client, interface.c_str()); + if(device == NULL) + return false; + + if(interface == nmUtils::ethIface()) + { + NMSettingConnection *settings = NULL; + if(device == NULL) + return false; + + 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); + } + else + return false; // interface is not eth0 or wlan0 + + if(connection == nullptr) + { + NMLOG_WARNING("not a single connection availble for %s", interface.c_str()); + return false; + } + + if(address.autoconfig) + { + NMSettingIP4Config *sIp4 = nullptr; + NMSettingIP6Config *sIp6 = nullptr; + if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) + { + if(checkAutoConnectEnabledInIPv4Conn(connection)) // already auto connect true connection + { + NMLOG_INFO("Setting IPv4 auto connect enabled"); + return true; // no need to modify + } + 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 + { + 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 + { + if (nmUtils::caseInsensitiveCompare("IPv4", address.ipversion)) + { + NMSettingIPConfig *ip4Config = nm_connection_get_setting_ip4_config(connection); + if (ip4Config == nullptr) + { + ip4Config = (NMSettingIPConfig *)nm_setting_ip4_config_new(); + } + 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); + if(!address.primarydns.empty()) + nm_setting_ip_config_add_dns(ip4Config, address.primarydns.c_str()); + if(!address.secondarydns.empty()) + 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 + { + //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 + GError *error = NULL; + + if (!nm_remote_connection_commit_changes(remoteConn, FALSE, NULL, &error)) { + if (error) { + NMLOG_ERROR("Failed to commit changes to the remote connection: %s", error->message); + g_error_free(error); // Free the GError object after handling it + } else { + NMLOG_ERROR("Failed to commit changes to the remote connection (unknown error)."); + } + return false; + } + + 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, specObject, NULL, onActivateComplete, this); + wait(loop); + return isSuccess; + } } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManagerGnomeWIFI.h b/NetworkManagerGnomeWIFI.h index eada5a8a..dcce93ca 100644 --- a/NetworkManagerGnomeWIFI.h +++ b/NetworkManagerGnomeWIFI.h @@ -61,8 +61,9 @@ namespace WPEFramework bool cancelWPS(); void wpsAction(); bool setInterfaceState(std::string interface, bool enabled); + bool setIpSettings(const string interface, const Exchange::INetworkManager::IPAddress address); private: - NMDevice *getNmDevice(); + NMDevice *getWifiDevice(); private: wifiManager(); diff --git a/NetworkManagerJsonRpc.cpp b/NetworkManagerJsonRpc.cpp index 5557e498..bb143f0d 100644 --- a/NetworkManagerJsonRpc.cpp +++ b/NetworkManagerJsonRpc.cpp @@ -329,33 +329,30 @@ namespace WPEFramework uint32_t rc = Core::ERROR_GENERAL; Exchange::INetworkManager::IPAddress address{}; - string interface = ""; - string ipversion = ""; + string interface = {}; - 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; - } - - address.autoconfig = parameters["autoconfig"].Boolean(); - if (!address.autoconfig) + else { - address.ipaddress = parameters["ipaddress"].String(); - address.ipversion = parameters["ipversion"].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); }