From e8bdf1fada370b2e4b1bac1b42d6660e99c2a7c2 Mon Sep 17 00:00:00 2001 From: rafi Date: Mon, 9 Dec 2024 21:56:17 +0530 Subject: [PATCH] event memory fix added --- NetworkManagerLogger.cpp | 2 +- Tests/raspberrypi/NetworkManagerGdbusTest.cpp | 4 +- gdbus/NetworkManagerGdbusClient.cpp | 22 +--- gdbus/NetworkManagerGdbusEvent.cpp | 115 +++++++++++------- gdbus/NetworkManagerGdbusEvent.h | 7 +- gdbus/NetworkManagerGdbusUtils.cpp | 5 +- 6 files changed, 88 insertions(+), 67 deletions(-) diff --git a/NetworkManagerLogger.cpp b/NetworkManagerLogger.cpp index 313fdcbc..4dc9d0d5 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.cpp b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp index fbb7d804..ae6dad6d 100644 --- a/Tests/raspberrypi/NetworkManagerGdbusTest.cpp +++ b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp @@ -146,7 +146,7 @@ int main() { std::cout << "Enter SSID to scan (leave blank for all): "; std::cin.ignore(); std::getline(std::cin, ssid); - nmEvents->setwifiScanOptions(true); + // nmEvents->setwifiScanOptions(true); if (nmClient->startWifiScan(ssid)) { NMLOG_INFO("WiFi scan started successfully"); } else { @@ -227,6 +227,6 @@ int main() { } NMLOG_INFO("Program completed successfully"); - nmEvents->stopNetworkMangerEventMonitor(); + // nmEvents->stopNetworkMangerEventMonitor(); return 0; } diff --git a/gdbus/NetworkManagerGdbusClient.cpp b/gdbus/NetworkManagerGdbusClient.cpp index 1c1aa73f..59e4f678 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; } diff --git a/gdbus/NetworkManagerGdbusEvent.cpp b/gdbus/NetworkManagerGdbusEvent.cpp index f94c434a..210a714b 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); } } @@ -452,14 +454,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 +470,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 +488,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 +530,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 +559,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 aea85984..b89f8fb4 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/NetworkManagerGdbusUtils.cpp b/gdbus/NetworkManagerGdbusUtils.cpp index 9de2fe65..1fa5922f 100644 --- a/gdbus/NetworkManagerGdbusUtils.cpp +++ b/gdbus/NetworkManagerGdbusUtils.cpp @@ -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;