diff --git a/INetworkManager.h b/INetworkManager.h index 254eb06..c1920bb 100644 --- a/INetworkManager.h +++ b/INetworkManager.h @@ -243,7 +243,7 @@ namespace WPEFramework virtual uint32_t StopConnectivityMonitoring(void) const = 0; /* @brief Get the Public IP used for external world communication */ - virtual uint32_t GetPublicIP (const string &ipversion /* @in */, string& ipaddress /* @out */) = 0; + virtual uint32_t GetPublicIP (string &ipversion /* @inout */, string& ipaddress /* @out */) = 0; /* @brief Request for ping and get the response in as event. The GUID used in the request will be returned in the event. */ virtual uint32_t Ping (const string ipversion /* @in */, const string endpoint /* @in */, const uint32_t count /* @in */, const uint16_t timeout /* @in */, const string guid /* @in */, string& response /* @out */) = 0; diff --git a/LegacyPlugin_WiFiManagerAPIs.cpp b/LegacyPlugin_WiFiManagerAPIs.cpp index 8e67072..3369132 100644 --- a/LegacyPlugin_WiFiManagerAPIs.cpp +++ b/LegacyPlugin_WiFiManagerAPIs.cpp @@ -159,7 +159,7 @@ namespace WPEFramework Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T("127.0.0.1:9998"))); m_networkmanager = make_shared >(_T(NETWORK_MANAGER_CALLSIGN), _T("org.rdk.Wifi"), query); - //subscribeToEvents(); + subscribeToEvents(); return string(); } diff --git a/NetworkManager.h b/NetworkManager.h index a2543be..353c34f 100644 --- a/NetworkManager.h +++ b/NetworkManager.h @@ -132,6 +132,14 @@ namespace WPEFramework void Deinitialize(PluginHost::IShell *service) override; string Information() const override; + //override Subscribe from IDispatcher + uint32_t Subscribe(const uint32_t channel, const string& event, const string& designator) override + { + NMLOG_DEBUG("Subscription received for %s event from channelID (%u) with designator as %s", event.c_str(), channel, designator.c_str()); + JSONRPC::Subscribe(channel, event, designator); + return Core::ERROR_NONE; + } + // Do not allow copy/move constructors NetworkManager(const NetworkManager &) = delete; NetworkManager &operator=(const NetworkManager &) = delete; diff --git a/NetworkManagerImplementation.cpp b/NetworkManagerImplementation.cpp index 997eaf2..311967f 100644 --- a/NetworkManagerImplementation.cpp +++ b/NetworkManagerImplementation.cpp @@ -280,7 +280,7 @@ namespace WPEFramework } /* @brief Get the Public IP used for external world communication */ - uint32_t NetworkManagerImplementation::GetPublicIP (const string &ipversion /* @in */, string& ipaddress /* @out */) + uint32_t NetworkManagerImplementation::GetPublicIP (string &ipversion /* @inout */, string& ipaddress /* @out */) { LOG_ENTRY_FUNCTION(); stun::bind_result result; @@ -289,6 +289,11 @@ namespace WPEFramework stun::protocol proto (isIPv6 ? stun::protocol::af_inet6 : stun::protocol::af_inet); if(stunClient.bind(m_stunEndPoint, m_stunPort, m_defaultInterface, proto, m_stunBindTimeout, m_stunCacheTimeout, result)) { + if (isIPv6) + ipversion = "IPv6"; + else + ipversion = "IPv4"; + ipaddress = result.public_ip; return Core::ERROR_NONE; } diff --git a/NetworkManagerImplementation.h b/NetworkManagerImplementation.h index 64767f9..8897b7d 100644 --- a/NetworkManagerImplementation.h +++ b/NetworkManagerImplementation.h @@ -197,7 +197,7 @@ namespace WPEFramework uint32_t StopConnectivityMonitoring(void) const override; /* @brief Get the Public IP used for external world communication */ - uint32_t GetPublicIP (const string &ipversion /* @in */, string& ipaddress /* @out */) override; + uint32_t GetPublicIP (string &ipversion /* @inout */, string& ipaddress /* @out */) override; /* @brief Request for ping and get the response in as event. The GUID used in the request will be returned in the event. */ uint32_t Ping (const string ipversion /* @in */, const string endpoint /* @in */, const uint32_t noOfRequest /* @in */, const uint16_t timeOutInSeconds /* @in */, const string guid /* @in */, string& response /* @out */) override; diff --git a/NetworkManagerJsonRpc.cpp b/NetworkManagerJsonRpc.cpp index 1079443..6628628 100644 --- a/NetworkManagerJsonRpc.cpp +++ b/NetworkManagerJsonRpc.cpp @@ -567,7 +567,6 @@ namespace WPEFramework void NetworkManager::PublishToThunderAboutInternet() { - NMLOG_DEBUG("No public IP persisted yet; Update the data"); if (m_publicIPAddress.empty()) { JsonObject input, output; @@ -576,6 +575,7 @@ namespace WPEFramework if (!m_publicIPAddress.empty()) { + NMLOG_DEBUG("No public IP persisted yet; Update the data"); PluginHost::ISubSystem* subSystem = _service->SubSystems(); if (subSystem != nullptr) diff --git a/NetworkManagerStunClient.cpp b/NetworkManagerStunClient.cpp index 999f14e..1960f0f 100644 --- a/NetworkManagerStunClient.cpp +++ b/NetworkManagerStunClient.cpp @@ -51,7 +51,7 @@ namespace details { int m_fd; }; - #ifdef _STUN_DEBUG +#ifdef _STUN_DEBUG void dump_buffer(char const * prefix, buffer const & buff) { if (prefix) @@ -61,14 +61,7 @@ namespace details { printf("\n"); return; } - #endif - - #ifdef _STUN_DEBUG - #define STUN_TRACE(format, ...) printf("STUN:" format __VA_OPT__(,) __VA_ARGS__) - #else - #define STUN_TRACE(format, ...) - #endif - +#endif void throw_error(char const * format, ...) { @@ -126,7 +119,7 @@ namespace details { details::throw_error("failed to find ip for interface:%s", iface.c_str()); return iface_info; } - STUN_TRACE("local_addr:%s\n", sockaddr_to_string(iface_info).c_str()); + NMLOG_DEBUG("local_addr:%s", sockaddr_to_string(iface_info).c_str()); return iface_info; } @@ -290,7 +283,6 @@ client::client() , m_cache_timeout(30) , m_last_cache_time() , m_last_result() - , m_verbose(true) , m_fd(-1) { } @@ -350,7 +342,7 @@ bool client::bind( dirty = true; } - verbose("client::bind enter: server=%s port=%u iface=%s ipv6=%u timeout=%u cache_timeout=%u dirty=%u\n", + NMLOG_DEBUG("client::bind enter: server=%s port=%u iface=%s ipv6=%u timeout=%u cache_timeout=%u dirty=%u", hostname.c_str(), port, interface.c_str(), proto == stun::protocol::af_inet6, bind_timeout, cache_timeout, dirty); if(m_cache_timeout > 0 /*asking if caching is enabled*/ @@ -360,18 +352,18 @@ bool client::bind( auto time_in_cache = std::chrono::duration_cast( std::chrono::steady_clock::now() - m_last_cache_time); - verbose("client::bind cache time=%lld\n", time_in_cache.count()); + NMLOG_DEBUG("client::bind cache time=%ld", time_in_cache.count()); if(time_in_cache.count() < m_cache_timeout) { result = m_last_result; - verbose("client::bind returning cached result: %s\n", result.public_ip.c_str()); + NMLOG_DEBUG("client::bind returning cached result: %s", result.public_ip.c_str()); return true; } else { - verbose("client::client::bind cached result expired\n"); + NMLOG_DEBUG("client::client::bind cached result expired"); } } @@ -387,7 +379,7 @@ bool client::bind( std::chrono::milliseconds wait_time(interval_wait_time); for (int i = 0; i < num_attempts && total_time < m_bind_timeout; ++i) { - verbose("client::bind sending bind request\n"); + NMLOG_DEBUG("client::bind sending bind request"); std::unique_ptr binding_response = send_binding_request(wait_time); @@ -415,25 +407,25 @@ bool client::bind( m_last_cache_time = std::chrono::steady_clock::now(); - verbose("client::bind success: public_ip=%s\n", result.public_ip.c_str()); + NMLOG_DEBUG("client::bind success: public_ip=%s", result.public_ip.c_str()); ret_ok = true; } else { - verbose("client::bind failed: ip missing from binding response\n"); + NMLOG_DEBUG("client::bind failed: ip missing from binding response"); } } else { - verbose("client::bind failed: no response received from server\n"); + NMLOG_INFO("client::bind failed: no response received from server"); } } } #ifdef __cpp_exceptions catch (std::exception const & err) { - verbose("client::bind failed: %s\n", err.what()); + NMLOG_WARNING("client::bind failed: %s", err.what()); } #endif @@ -454,13 +446,13 @@ void client::create_udp_socket(int inet_family) if (inet_family != AF_INET && inet_family != AF_INET6) details::throw_error("invalid inet family:%d", inet_family); - verbose("creating udp/%s socket\n", details::family_to_string(inet_family)); + NMLOG_DEBUG("creating udp/%s socket", details::family_to_string(inet_family)); int soc = socket(inet_family, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (soc < 0) details::throw_error("error creating socket. %s", strerror(errno)); - #ifdef _SUN_USE_MSGHDR + #ifdef _STUN_USE_MSGHDR int optval = 1; setsockopt(soc, IPPROTO_IP, IP_PKTINFO, &optval, sizeof(int)); #endif @@ -468,7 +460,7 @@ void client::create_udp_socket(int inet_family) if (!m_interface.empty()) { sockaddr_storage local_addr = details::get_interface_address(m_interface, inet_family); - verbose("binding to local interface %s/%s\n", m_interface.c_str(), + NMLOG_DEBUG("binding to local interface %s/%s", m_interface.c_str(), sockaddr_to_string(local_addr).c_str()); int ret = ::bind(soc, reinterpret_cast(&local_addr), details::socket_length(local_addr)); @@ -479,18 +471,16 @@ void client::create_udp_socket(int inet_family) sockaddr_to_string(local_addr).c_str(), strerror(err)); } else { - if (m_verbose) { sockaddr_storage local_endpoint; socklen_t socklen = sizeof(sockaddr_storage); int ret = getsockname(soc, reinterpret_cast(&local_endpoint), &socklen); if (ret == 0) - verbose("local endpoint %s/%d\n", sockaddr_to_string(local_endpoint).c_str(), + NMLOG_DEBUG("local endpoint %s/%d", sockaddr_to_string(local_endpoint).c_str(), details::sockaddr_get_port(local_endpoint)); - } } } else - verbose("no local interface supplied to bind to\n"); + NMLOG_DEBUG("no local interface supplied to bind to"); if (m_fd != -1) close(m_fd); @@ -506,13 +496,13 @@ message * client::send_message(sockaddr_storage const & remote_addr, message con buffer bytes = req.encode(); - STUN_TRACE("remote_addr:%s\n", sockaddr_to_string(remote_addr).c_str()); + NMLOG_DEBUG("remote_addr:%s", sockaddr_to_string(remote_addr).c_str()); #ifdef _STUN_DEBUG details::dump_buffer("STUN >>> ", bytes); #endif - verbose("sending messsage\n"); + NMLOG_DEBUG("sending messsage"); ssize_t n = sendto(m_fd, &bytes[0], bytes.size(), 0, (sockaddr *) &remote_addr, details::socket_length(remote_addr)); if (n < 0) @@ -537,10 +527,10 @@ message * client::send_message(sockaddr_storage const & remote_addr, message con timeout.tv_sec = (timeout.tv_usec / kMicrosecondsPerSecond); timeout.tv_usec -= (timeout.tv_sec * kMicrosecondsPerSecond); } - verbose("waiting for response, timeout set to %lus - %luus\n", timeout.tv_sec, timeout.tv_usec); + NMLOG_DEBUG("waiting for response, timeout set to %lus - %luus", timeout.tv_sec, timeout.tv_usec); int ret = select(m_fd + 1, &rfds, nullptr, nullptr, &timeout); if (ret == 0) { - STUN_TRACE("select timeout out\n"); + NMLOG_DEBUG("select timeout out"); return nullptr; } @@ -608,18 +598,6 @@ message * client::send_message(sockaddr_storage const & remote_addr, message con return decoder::decode_message(bytes, nullptr); } -void client::verbose(char const * format, ...) -{ - if (!m_verbose) - return; - va_list ap; - va_start(ap, format); - printf("STUN:"); - vprintf(format, ap); - va_end(ap); - return; -} - network_access_type client::discover_network_access_type(server const & srv) { std::chrono::milliseconds wait_time(250); @@ -672,7 +650,7 @@ std::unique_ptr client::send_binding_request(std::chrono::milliseconds std::unique_ptr client::send_binding_request(sockaddr_storage const & addr, std::chrono::milliseconds wait_time) { - this->verbose("sending binding request with wait time:%lld ms\n", wait_time.count()); + NMLOG_DEBUG("sending binding request with wait time:%ld ms", wait_time.count()); this->create_udp_socket(addr.ss_family); std::unique_ptr binding_request(message_factory::create_binding_request()); std::unique_ptr binding_response(this->send_message(addr, *binding_request, wait_time)); diff --git a/NetworkManagerStunClient.h b/NetworkManagerStunClient.h index c2f6929..09f093a 100644 --- a/NetworkManagerStunClient.h +++ b/NetworkManagerStunClient.h @@ -8,6 +8,7 @@ #include #include #include +#include "NetworkManagerLogger.h" namespace stun { @@ -157,11 +158,7 @@ class client { network_access_type discover_network_access_type(server const & srv); - inline void set_verbose(bool b) { - m_verbose = b; - } private: - void verbose(char const * format, ...) __attribute__((format(printf, 2, 3))); void create_udp_socket(int inet_family); std::unique_ptr send_binding_request(std::chrono::milliseconds wait_time); @@ -180,7 +177,6 @@ class client { uint16_t m_cache_timeout; std::chrono::time_point m_last_cache_time; bind_result m_last_result; - bool m_verbose; int m_fd; }; diff --git a/Tests/unit_test/test_LegacyPlugin_NetworkAPIs.cpp b/Tests/unit_test/test_LegacyPlugin_NetworkAPIs.cpp index ff8c803..e452a11 100644 --- a/Tests/unit_test/test_LegacyPlugin_NetworkAPIs.cpp +++ b/Tests/unit_test/test_LegacyPlugin_NetworkAPIs.cpp @@ -10,8 +10,8 @@ using namespace std; using namespace WPEFramework; using namespace WPEFramework::Plugin; +using ::testing::NiceMock; - class NetworkTest : public ::testing::Test { protected: Core::ProxyType plugin; @@ -33,6 +33,47 @@ class NetworkTest : public ::testing::Test { } }; +class NetworkInitializedTest : public NetworkTest { +protected: + NiceMock service; + NetworkInitializedTest() + { + EXPECT_EQ(string(""), plugin->Initialize(&service)); + + EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_)) + .Times(1) + .WillOnce(::testing::Invoke( + [&](const uint32_t, const string& name) -> void* { + EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1"))); + return nullptr; + })); + } + + virtual ~NetworkInitializedTest() override + { + plugin->Deinitialize(&service); + } + +}; + +TEST_F(NetworkTest, RegisteredMethods) +{ + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getStbIp"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getInterfaces"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isInterfaceEnabled"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setInterfaceEnabled"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getDefaultInterface"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getIPSettings"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("isConnectedToInternet"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getPublicIP"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getSTBIPFamily"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setConnectivityTestEndpoints"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getCaptivePortalURI"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("startConnectivityMonitoring"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("stopConnectivityMonitoring"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setDefaultInterface"))); + EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("setIPSettings"))); +} diff --git a/Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp b/Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp index a7cbd7d..6050b4f 100644 --- a/Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp +++ b/Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp @@ -11,10 +11,17 @@ using namespace std; using namespace WPEFramework; using namespace WPEFramework::Plugin; using ::testing::NiceMock; +using ::testing::Return; + +class MockWiFiManager : public WiFiManager { +public: + MOCK_METHOD(subscribeToEvents, void()); +}; class WiFiManagerTest : public ::testing::Test { protected: - Core::ProxyType plugin; + // Core::ProxyType plugin; + Core::ProxyType plugin; Core::JSONRPC::Handler& handler; DECL_CORE_JSONRPC_CONX connection; Core::JSONRPC::Message message; @@ -22,7 +29,7 @@ class WiFiManagerTest : public ::testing::Test { ServiceMock services; WiFiManagerTest() - : plugin(Core::ProxyType::Create()) + : plugin(Core::ProxyType::Create()) //plugin(Core::ProxyType::Create()) , handler(*(plugin)) , INIT_CONX(1, 0) { @@ -48,6 +55,10 @@ class WiFiManagerInitializedTest : public WiFiManagerTest { EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1"))); return nullptr; })); + + EXPECT_CALL(*plugin, subscribeToEvents()) + .Times(1) + .WillOnce(Return()); } virtual ~WiFiManagerInitializedTest() override diff --git a/Tests/unit_test/unit_tests.cmake b/Tests/unit_test/unit_tests.cmake index b324092..febad7e 100644 --- a/Tests/unit_test/unit_tests.cmake +++ b/Tests/unit_test/unit_tests.cmake @@ -14,7 +14,7 @@ add_executable(${UNIT_TEST} Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp Tests/unit_test/test_NetworkManagerConnectivity.cpp Tests/unit_test/test_NetworkManagerStunClient.cpp - Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp + Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp Tests/mocks/thunder/Module.cpp WiFiSignalStrengthMonitor.cpp NetworkManagerLogger.cpp