diff --git a/Tests/raspberrypi/NetworkManagerGdbusTest.cpp b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp index ae6dad6..01d9a60 100644 --- a/Tests/raspberrypi/NetworkManagerGdbusTest.cpp +++ b/Tests/raspberrypi/NetworkManagerGdbusTest.cpp @@ -4,6 +4,7 @@ #include "NetworkManagerGdbusEvent.h" #include "INetworkManager.h" #include "NetworkManagerGdbusUtils.h" +#include "NetworkManagerImplementation.h" #include #include #include @@ -15,7 +16,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; @@ -31,7 +71,8 @@ void displayMenu() { 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 +93,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 +105,14 @@ void printSSIDs(const std::list& ssids) { } } -int main() { - - DbusMgr test; + +int main() +{ + NetworkManagerClient* nmClient = NetworkManagerClient::getInstance(); NetworkManagerEvents* nmEvents = NetworkManagerEvents::getInstance(); int choice = -1; - + while (choice != 0) { displayMenu(); std::cout << "Enter your choice: \n"; @@ -146,7 +188,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 +269,6 @@ int main() { } NMLOG_INFO("Program completed successfully"); - // nmEvents->stopNetworkMangerEventMonitor(); + nmEvents->stopNetworkMangerEventMonitor(); return 0; } diff --git a/gdbus/NetworkManagerGdbusEvent.cpp b/gdbus/NetworkManagerGdbusEvent.cpp index 210a714..9475a63 100644 --- a/gdbus/NetworkManagerGdbusEvent.cpp +++ b/gdbus/NetworkManagerGdbusEvent.cpp @@ -17,18 +17,17 @@ * limitations under the License. */ -#include -#include -#include -#include + #include +#include #include #include #include #include "NetworkManagerGdbusEvent.h" -#include "NetworkManagerLogger.h" #include "NetworkManagerGdbusUtils.h" +#include "NetworkManagerImplementation.h" +#include "NetworkManagerLogger.h" #include "INetworkManager.h" namespace WPEFramework @@ -37,29 +36,7 @@ namespace WPEFramework { static NetworkManagerEvents *_NetworkManagerEvents = nullptr; - - const char * nm_state_to_string(NMState state) - { - switch (state) { - case NM_STATE_ASLEEP: - return "asleep"; - case NM_STATE_CONNECTING: - return "connecting"; - case NM_STATE_CONNECTED_LOCAL: - return "connected (local only)"; - case NM_STATE_CONNECTED_SITE: - return "connected (site only)"; - case NM_STATE_CONNECTED_GLOBAL: - return "connected"; - case NM_STATE_DISCONNECTING: - return "disconnecting"; - case NM_STATE_DISCONNECTED: - return "disconnected"; - case NM_STATE_UNKNOWN: - default: - return "unknown"; - } - } + extern NetworkManagerImplementation* _instance; static std::string getInterafaceNameFormDevicePath(const char* DevicePath) { @@ -76,24 +53,22 @@ namespace WPEFramework const gchar *ifaceName = g_variant_get_string(ifaceVariant, NULL); if(ifaceName != nullptr) - ifname.assign(ifaceName, 0, 16); + ifname = ifaceName; g_variant_unref(ifaceVariant); g_object_unref(deviceProxy); return ifname; } - static void lastScanPropertiesChangedCB(GDBusProxy *proxy, - GVariant *changed_properties, - const gchar *invalidated_properties[], - gpointer user_data) { + static void lastScanPropertiesChangedCB(GDBusProxy *proxy, GVariant *changedProps, + const gchar *invalidProps[], gpointer userData) { if (proxy == NULL) { NMLOG_FATAL("cb doesn't have proxy "); return; } - GVariant *lastScanVariant = g_variant_lookup_value(changed_properties, "LastScan", NULL); + GVariant *lastScanVariant = g_variant_lookup_value(changedProps, "LastScan", NULL); if (lastScanVariant == NULL) return; @@ -101,13 +76,13 @@ namespace WPEFramework NMLOG_DEBUG("LastScan value changed: %" G_GINT64_FORMAT, lastScanTime); g_variant_unref(lastScanVariant); - const gchar *object_path = g_dbus_proxy_get_object_path(proxy); - if (object_path == NULL) { + const gchar *objectPath = g_dbus_proxy_get_object_path(proxy); + if (objectPath == NULL) { NMLOG_WARNING("Failed to get the proxy object path"); return; } - NetworkManagerEvents::onAvailableSSIDsCb(object_path); + NetworkManagerEvents::onAvailableSSIDsCb(objectPath); } static bool subscribeForlastScanPropertyEvent(const gchar *wirelessPath) @@ -122,19 +97,18 @@ namespace WPEFramework return true; } - static void primaryConnectionChangedCB(GDBusProxy *proxy, - GVariant *changed_properties, + static void primaryConnectionChangedCB(GDBusProxy *proxy, GVariant *changedPropes, GStrv invalidated_properties, gpointer user_data) { - if (changed_properties == NULL) { + if (changedPropes == NULL) { NMLOG_FATAL("cb doesn't have changed_properties "); return; } - GVariant *primaryConnPathVariant = g_variant_lookup_value(changed_properties, "PrimaryConnection", NULL); + GVariant *primaryConnPathVariant = g_variant_lookup_value(changedPropes, "PrimaryConnection", NULL); if (primaryConnPathVariant != NULL) { - const gchar *primaryConnPath; + const gchar *primaryConnPath = NULL; std::string newIface = ""; primaryConnPath = g_variant_get_string(primaryConnPathVariant, NULL); if(primaryConnPath && g_strcmp0(primaryConnPath, "/") == 0) @@ -142,6 +116,7 @@ namespace WPEFramework NMLOG_DEBUG("no active primary connection"); // sending empty string if no active interface NetworkManagerEvents::onActiveInterfaceChangeCb(std::string("Unknown")); + g_variant_unref(primaryConnPathVariant); return; } else if(primaryConnPath) @@ -165,6 +140,7 @@ namespace WPEFramework } else NMLOG_WARNING("connection type NULL"); + g_variant_unref(typeVariant); } else NMLOG_ERROR("PrimaryConnection Type property missing"); @@ -173,6 +149,7 @@ namespace WPEFramework } else NMLOG_WARNING("PrimaryConnection object missing"); + g_variant_unref(primaryConnPathVariant); } } @@ -358,12 +335,8 @@ namespace WPEFramework } - static void onConnectionSignalReceivedCB (GDBusProxy *proxy, - gchar *senderName, - gchar *signalName, - GVariant *parameters, - gpointer userData) { - + static void onConnectionSignalReceivedCB (GDBusProxy *proxy, gchar *senderName, gchar *signalName, + GVariant *parameters, gpointer userData) { if (g_strcmp0(senderName, "NewConnection") == 0) { NMLOG_INFO("new connection added success"); NMLOG_DEBUG("Parameters: %s", g_variant_print(parameters, TRUE)); @@ -387,7 +360,7 @@ namespace WPEFramework if(iface != NULL ) { std::string ipAddr; uint32_t prifix; - if(GnomeUtils::getIpv4AddrFromIP4ConfigProxy(proxy, ipAddr, prifix)) + if(GnomeUtils::getIPv4AddrFromIPv4ConfigProxy(proxy, ipAddr, prifix)) NetworkManagerEvents::onAddressChangeCb(std::string(iface), true, false, ipAddr); } g_variant_unref(addressVariant); @@ -408,7 +381,7 @@ namespace WPEFramework if(iface != NULL) { std::string ipAddr; uint32_t prifix; - if(GnomeUtils::getIpv6AddrFromIP6ConfigProxy(proxy, ipAddr, prifix)) + if(GnomeUtils::getIPv6AddrFromIPv6ConfigProxy(proxy, ipAddr, prifix)) NetworkManagerEvents::onAddressChangeCb(std::string(iface), true, true, ipAddr); } @@ -416,7 +389,6 @@ namespace WPEFramework } else { - // TODO remove GVariantIter *propsIter= NULL; const gchar *propertyName= NULL; GVariant *propertyValue= NULL; @@ -631,6 +603,8 @@ namespace WPEFramework { NMLOG_INFO("old interface - %s new interface - %s", oldIface.c_str(), newIface.c_str()); oldIface = newIface; + if(_instance != nullptr) + _instance->ReportActiveInterfaceChange(oldIface, newIface); } } @@ -660,22 +634,27 @@ namespace WPEFramework default: state = "Unknown"; } + NMLOG_DEBUG("%s interface state changed - %s", iface.c_str(), state.c_str()); + if(_instance != nullptr && (iface == GnomeUtils::getWifiIfname() || iface == GnomeUtils::getEthIfname())) + _instance->ReportInterfaceStateChange(static_cast(newState), iface); } void NetworkManagerEvents::onWIFIStateChanged(Exchange::INetworkManager::WiFiState state, std::string& wifiStateStr) { NMLOG_DEBUG("wifi state changed: %d ; NM wifi: %s", state, wifiStateStr.c_str()); + if(_instance != nullptr) + _instance->ReportWiFiStateChange(static_cast(state)); } - void NetworkManagerEvents::onAddressChangeCb(std::string iface, bool acqired, bool isIPv6, std::string ipAddress) + void NetworkManagerEvents::onAddressChangeCb(std::string iface, bool acquired, bool isIPv6, std::string ipAddress) { static std::map ipv6Map; static std::map ipv4Map; if (isIPv6) { - if (acqired == false) { // ip lost event + if (acquired == false) { // ip lost event if(ipv6Map.size() < 1) { return; // no ipv6 added yet no event posting @@ -683,15 +662,15 @@ namespace WPEFramework ipAddress = ipv6Map[iface]; ipv6Map[iface].clear(); } - else // acqired + else // acquired { if (ipAddress.compare(0, 5, "fe80:") == 0 || ipAddress.compare(0, 6, "fe80::") == 0) { NMLOG_DEBUG("It's link-local ip"); return; // It's link-local } - - if (ipv6Map[iface].find(ipAddress) == std::string::npos && !ipAddress.empty()) { // same ip comes multiple time so avoding that + /* same ip comes multiple time so avoding that */ + if (ipv6Map[iface].find(ipAddress) == std::string::npos && !ipAddress.empty()) { ipv6Map[iface] = ipAddress; // SLAAC protocol may include multip ipv6 address last one will save } else @@ -700,7 +679,7 @@ namespace WPEFramework } else { - if (acqired == false) { // ip lost event + if (acquired == false) { // ip lost event if(ipv4Map.size() < 1) { return; // no ipv4 added yet no event posting @@ -717,25 +696,31 @@ namespace WPEFramework } } - // if(_instance != nullptr) - //instance->ReportIPAddressChangedEvent(iface, acqired, true, ipAddress); - NMLOG_INFO("iface:%s - ipaddress:%s - %s - %s", iface.c_str(), ipAddress.c_str(), acqired?"acquired":"lost", isIPv6?"isIPv6":"isIPv4"); + Exchange::INetworkManager::IPStatus ipStatus{}; + if(acquired) + ipStatus = Exchange::INetworkManager::IP_ACQUIRED; + + if(_instance != nullptr) + _instance->ReportIPAddressChange(iface, isIPv6?"IPv6":"IPv4", ipAddress, ipStatus); + + NMLOG_INFO("iface:%s - ipaddress:%s - %s - %s", iface.c_str(), ipAddress.c_str(), acquired?"acquired":"lost", isIPv6?"isIPv6":"isIPv4"); } void NetworkManagerEvents::onAvailableSSIDsCb(const char* wifiDevicePath) { + GDBusProxy* wProxy = nullptr; + GError* error = nullptr; + NMLOG_DEBUG("wifi scanning completed ..."); if(_NetworkManagerEvents->doScanNotify == false) { return; } _NetworkManagerEvents->doScanNotify = false; - GDBusProxy* wProxy = nullptr; - GError* error = nullptr; wProxy = _NetworkManagerEvents->eventDbus.getNetworkManagerWirelessProxy(wifiDevicePath); - if(wProxy == NULL) return; + 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); @@ -744,16 +729,18 @@ namespace WPEFramework return; } - GVariantIter* iter; - const gchar* apPath; + GVariantIter* iter = NULL; + const gchar* apPath = NULL; JsonArray ssidList = JsonArray(); bool oneSSIDFound = false; g_variant_get(result, "(ao)", &iter); + if(iter == NULL) + return; while (g_variant_iter_loop(iter, "o", &apPath)) { Exchange::INetworkManager::WiFiSSIDInfo wifiInfo; // NMLOG_DEBUG("Access Point Path: %s", apPath); - if(GnomeUtils::getApDetails(_NetworkManagerEvents->eventDbus, apPath, wifiInfo)) + if(apPath != NULL && GnomeUtils::getApDetails(_NetworkManagerEvents->eventDbus, apPath, wifiInfo)) { JsonObject ssidObj; if(GnomeUtils::convertSsidInfoToJsonObject(wifiInfo, ssidObj)) @@ -762,12 +749,13 @@ namespace WPEFramework oneSSIDFound = true; } } - } if(oneSSIDFound) { - std::string ssids; + std::string ssids{}; ssidList.ToString(ssids); + if(_instance != nullptr) + _instance->ReportAvailableSSIDs(ssidList); NMLOG_DEBUG("scanned ssids: %s", ssids.c_str()); } diff --git a/gdbus/NetworkManagerGdbusEvent.h b/gdbus/NetworkManagerGdbusEvent.h index b89f8fb..69b1f0f 100644 --- a/gdbus/NetworkManagerGdbusEvent.h +++ b/gdbus/NetworkManagerGdbusEvent.h @@ -55,7 +55,7 @@ namespace WPEFramework public: static void onInterfaceStateChangeCb(Exchange::INetworkManager::InterfaceState newState, std::string iface); // ReportInterfaceStateChangedEvent - static void onAddressChangeCb(std::string iface, bool acqired, bool isIPv6, std::string ipAddress = ""); // ReportIPAddressChangedEvent + static void onAddressChangeCb(std::string iface, bool acquired, bool isIPv6, std::string ipAddress = ""); // ReportIPAddressChangedEvent static void onActiveInterfaceChangeCb(std::string newInterface); // ReportActiveInterfaceChangedEvent static void onAvailableSSIDsCb(const char* wifiDevicePath); // ReportAvailableSSIDsEvent static void onWIFIStateChanged(Exchange::INetworkManager::WiFiState state, std::string& wifiStateStr); // ReportWiFiStateChangedEvent diff --git a/gdbus/NetworkManagerGdbusUtils.cpp b/gdbus/NetworkManagerGdbusUtils.cpp index 1fa5922..260d59d 100644 --- a/gdbus/NetworkManagerGdbusUtils.cpp +++ b/gdbus/NetworkManagerGdbusUtils.cpp @@ -34,7 +34,7 @@ namespace WPEFramework static const char* ifnameEth = "eth0"; static const char* ifnameWlan = "wlan0"; - bool GnomeUtils::getIpv4AddrFromIP4ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prefix) + bool GnomeUtils::getIPv4AddrFromIPv4ConfigProxy(GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prefix) { GVariantIter *iter; GVariantIter *dictIter; @@ -58,7 +58,7 @@ namespace WPEFramework address = g_variant_dup_string(value, NULL); if(address!= NULL) { - ipAddr.assign(address, 16); // strlen of "255.255.255.255" + ipAddr = address; // strlen of "255.255.255.255" NMLOG_DEBUG("Address: %s", ipAddr.c_str()); ret = true; } @@ -77,7 +77,7 @@ namespace WPEFramework return ret; } - bool GnomeUtils::getIpv6AddrFromIP6ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prefix) + bool GnomeUtils::getIPv6AddrFromIPv6ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prefix) { GVariantIter *iter = NULL; GVariantIter *dictIter = NULL; @@ -95,12 +95,14 @@ namespace WPEFramework if (addressDataVariant != NULL) { g_variant_get(addressDataVariant, "aa{sv}", &iter); while (g_variant_iter_loop(iter, "a{sv}", &dictIter)) { - while (g_variant_iter_loop(dictIter, "{&sv}", &key, &value)) { - if (g_strcmp0(key, "address") == 0) { + while (g_variant_iter_loop(dictIter, "{&sv}", &key, &value)) + { + if (g_strcmp0(key, "address") == 0) + { address = g_variant_dup_string(value, NULL); if(address!= NULL) { - ipAddress.assign(address, 40); // strlen of "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF" + ipAddress = address; // strlen of "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF" if (ipAddress.compare(0, 5, "fe80:") == 0 || ipAddress.compare(0, 6, "fe80::") == 0) { // It's link-local starts with fe80 NMLOG_DEBUG("link-local ip: %s", address); @@ -113,7 +115,9 @@ namespace WPEFramework } } g_free(address); - } else if (g_strcmp0(key, "prefix") == 0) { + } + else if (g_strcmp0(key, "prefix") == 0) + { prefix = g_variant_get_uint32(value); NMLOG_DEBUG("Prefix: %u", prefix); } @@ -378,7 +382,7 @@ namespace WPEFramework } g_variant_get(result, "s", &_bssid); if(_bssid != NULL) { - wifiInfo.bssid.assign(_bssid); + wifiInfo.bssid = _bssid; g_free(_bssid); } g_variant_unref(result); diff --git a/gdbus/NetworkManagerGdbusUtils.h b/gdbus/NetworkManagerGdbusUtils.h index 222d686..4f9b8ce 100644 --- a/gdbus/NetworkManagerGdbusUtils.h +++ b/gdbus/NetworkManagerGdbusUtils.h @@ -72,8 +72,8 @@ namespace WPEFramework static bool getDevicePropertiesByPath(DbusMgr& m_dbus, const char* devPath, deviceInfo& properties); static bool getDeviceInfoByIfname(DbusMgr& m_dbus, const char* ifname, deviceInfo& properties); static bool getCachedPropertyU(GDBusProxy* proxy, const char* propertiy, uint32_t *value); - static bool getIpv4AddrFromIP4ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix); - static bool getIpv6AddrFromIP6ConfigProxy( GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix); + static bool getIPv4AddrFromIPv4ConfigProxy(GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix); + static bool getIPv6AddrFromIPv6ConfigProxy(GDBusProxy *ipProxy, std::string& ipAddr, uint32_t& prifix); static bool convertSsidInfoToJsonObject(Exchange::INetworkManager::WiFiSSIDInfo& wifiInfo, JsonObject& ssidObj); static const char* convertPercentageToSignalStrengtStr(int percentage);