diff --git a/NetworkManager/NetworkManagerConnectivity.cpp b/NetworkManager/NetworkManagerConnectivity.cpp index e7dc7a608d..ad2599f623 100644 --- a/NetworkManager/NetworkManagerConnectivity.cpp +++ b/NetworkManager/NetworkManagerConnectivity.cpp @@ -1,26 +1,52 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include -#include "Module.h" -#include "NetworkManagerConnectivity.h" +#include +#include +#include +#include +#include + #include "NetworkManagerImplementation.h" +#include "NetworkManagerConnectivity.h" +#include "NetworkManagerLogger.h" -namespace WPEFramework { - namespace Plugin { +namespace WPEFramework +{ + namespace Plugin + { extern NetworkManagerImplementation* _instance; + static const char* getInternetStateString(nsm_internetState state) + { + switch(state) + { + case NO_INTERNET: return "NO_INTERNET"; + case LIMITED_INTERNET: return "LIMITED_INTERNET"; + case CAPTIVE_PORTAL: return "CAPTIVE_PORTAL"; + case FULLY_CONNECTED: return "FULLY_CONNECTED"; + default: return "UNKNOWN"; + } + } + bool EndpointCache::isEndpointCashFileExist() { std::ifstream fileStream(CachefilePath); @@ -37,7 +63,6 @@ namespace WPEFramework { outputFile << str << '\n'; } outputFile.close(); - NMLOG_INFO("Connectivity endpoints successfully written to a file"); } else { @@ -65,110 +90,19 @@ namespace WPEFramework { return readStrings; } - bool ConnectivityMonitor::isConnectivityMonitorEndpointSet() - { - const std::lock_guard lock(endpointMutex); - if(monitorEndpoints.size() > 0) - { - return true; - } - else if(endpointCache.isEndpointCashFileExist()) - { - monitorEndpoints = endpointCache.readEnpointsFromFile(); - std::string endpoints_str; - for (const auto& endpoint : monitorEndpoints) - endpoints_str.append(endpoint).append(" "); - NMLOG_INFO("updated endpoints count %d and endpoints:- %s", static_cast(monitorEndpoints.size()), endpoints_str.c_str()); - if(monitorEndpoints.size() > 0) - return true; - } - return false; - } - - bool ConnectivityMonitor::isConnectedToInternet(nsm_ipversion ipversion) - { - if (nsm_internetState::FULLY_CONNECTED == getInternetConnectionState(ipversion)) - { - NMLOG_INFO("isConnectedToInternet = true"); - return true; - } - - NMLOG_WARNING("isConnectedToInternet = false"); - return false; - } - - nsm_internetState ConnectivityMonitor::getInternetConnectionState(nsm_ipversion ipversion) - { - - nsm_internetState internetState = nsm_internetState::UNKNOWN; - - // If monitor connectivity is running take the cashe value - if ( isMonitorThreadRunning() && (ipversion == NSM_IPRESOLVE_WHATEVER) && (g_internetState != nsm_internetState::UNKNOWN) ) - { - internetState = g_internetState; - } - else - { - if(isConnectivityMonitorEndpointSet()) - internetState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, ipversion, true); - else - internetState = testConnectivity(getConnectivityDefaultEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, ipversion, true); - } - - return internetState; - } - - std::string ConnectivityMonitor::getCaptivePortalURI() + TestConnectivity::TestConnectivity(const std::vector& endpoints, long timeout_ms, bool headReq, nsm_ipversion ipversion) { - if (g_internetState == nsm_internetState::CAPTIVE_PORTAL) - { - return getCaptivePortal(); - } - else - { - std::string captivePortal; - if(getInternetConnectionState(NSM_IPRESOLVE_WHATEVER) == nsm_internetState::CAPTIVE_PORTAL) - { - return getCaptivePortal(); - } - else - NMLOG_WARNING("No captive portal found !"); - } - return std::string(""); - } - - void ConnectivityMonitor::setConnectivityMonitorEndpoints(const std::vector &endpoints) - { - const std::lock_guard lock(endpointMutex); - monitorEndpoints.clear(); - for (auto endpoint : endpoints) { - if(!endpoint.empty() && endpoint.size() > 3) - monitorEndpoints.push_back(endpoint.c_str()); - else - NMLOG_INFO("endpoint not vallied = %s", endpoint.c_str()); + internetSate = UNKNOWN; + if(endpoints.size() < 1) { + NMLOG_ERROR("Endpoints size error ! curl check not possible"); + return; } - // write the endpoints to a file - endpointCache.writeEnpointsToFile(monitorEndpoints); - - std::string endpoints_str; - for (const auto& endpoint : monitorEndpoints) - endpoints_str.append(endpoint).append(" "); - NMLOG_INFO("Connectivity monitor endpoints -: %d :- %s", static_cast(monitorEndpoints.size()), endpoints_str.c_str()); - } - - std::vector ConnectivityMonitor::getConnectivityMonitorEndpoints() - { - const std::lock_guard lock(endpointMutex); - std::vector endpoints; - for (auto endpoint : monitorEndpoints) { - endpoints.push_back(endpoint); - } - return endpoints; + internetSate = checkCurlResponse(endpoints, timeout_ms, headReq, ipversion); } static bool curlVerboseEnabled() { - std::ifstream fileStream("/tmp/network.plugin.debug"); + std::ifstream fileStream("/tmp/nm.plugin.debug"); return fileStream.is_open(); } @@ -180,18 +114,14 @@ namespace WPEFramework { } static size_t writeFunction(void* ptr, size_t size, size_t nmemb, std::string* data) { #ifdef DBG_CURL_GET_RESPONSE - NMLOG_TRACE("%s",(char*)ptr); + LOG_DBG("%s",(char*)ptr); #endif return size * nmemb; } - nsm_internetState Connectivity::testConnectivity(const std::vector& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly) + nsm_internetState TestConnectivity::checkCurlResponse(const std::vector& endpoints, long timeout_ms, bool headReq, nsm_ipversion ipversion) { long deadline = current_time() + timeout_ms, time_now = 0, time_earlier = 0; - if(endpoints.size() < 1) { - NMLOG_ERROR("endpoints size error ! "); - return NO_INTERNET; - } CURLM *curl_multi_handle = curl_multi_init(); if (!curl_multi_handle) @@ -219,15 +149,23 @@ namespace WPEFramework { /* set our custom set of headers */ curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPHEADER, chunk); curl_easy_setopt(curl_easy_handle, CURLOPT_USERAGENT, "RDKCaptiveCheck/1.0"); - /* set CURLOPT_HTTPGET option insted of CURLOPT_CONNECT_ONLY bcause we need to get the captiveportal URI not just connection only */ - if(connectOnly) - curl_easy_setopt(curl_easy_handle, CURLOPT_CONNECT_ONLY, 1L); - else - curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPGET, 1L); /* HTTPGET request added insted of HTTPHEAD request fix for DELIA-61526 */ + curl_easy_setopt(curl_easy_handle, CURLOPT_CONNECT_ONLY, 1L); + if(!headReq) { + // NMLOG_TRACE("curlopt get request"); + /* HTTPGET request added insted of HTTPHEAD request fix for DELIA-61526 */ + curl_easy_setopt(curl_easy_handle, CURLOPT_HTTPGET, 1L); + } curl_easy_setopt(curl_easy_handle, CURLOPT_WRITEFUNCTION, writeFunction); curl_easy_setopt(curl_easy_handle, CURLOPT_TIMEOUT_MS, deadline - current_time()); if ((ipversion == CURL_IPRESOLVE_V4) || (ipversion == CURL_IPRESOLVE_V6)) + { + NMLOG_INFO("curlopt ipversion = %s reqtyp = %s", ipversion == CURL_IPRESOLVE_V4?"ipv4 only":"ipv6 only", headReq? "HEAD":"GET"); curl_easy_setopt(curl_easy_handle, CURLOPT_IPRESOLVE, ipversion); + } + else + { + NMLOG_INFO("curlopt ipversion = whatever reqtyp = %s", headReq? "HEAD":"GET"); + } if(curlVerboseEnabled()) curl_easy_setopt(curl_easy_handle, CURLOPT_VERBOSE, 1L); if (CURLM_OK != (mc = curl_multi_add_handle(curl_multi_handle, curl_easy_handle))) @@ -258,21 +196,19 @@ namespace WPEFramework { continue; if (CURLE_OK == msg->data.result) { curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &endpoint); - if(connectOnly) - response_code = HttpStatus_204_No_Content; - else if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) { - if(curlVerboseEnabled()) - NMLOG_INFO("endpoint = <%s> response code <%d>",endpoint, static_cast(response_code)); + if (curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response_code) == CURLE_OK) + { + //NMLOG_TRACE("endpoint = <%s> http response code <%d>", endpoint, static_cast(response_code)); if (HttpStatus_302_Found == response_code) { if ( (curl_easy_getinfo(msg->easy_handle, CURLINFO_REDIRECT_URL, &url) == CURLE_OK) && url != nullptr) { - NMLOG_WARNING("captive portal found !!!"); - setCaptivePortal(url); + NMLOG_TRACE("captive portal found !!!"); + captivePortalURI = url; } } } } else - NMLOG_TRACE("endpoint = <%s> curl error = %d (%s)", endpoint, msg->data.result, curl_easy_strerror(msg->data.result)); + NMLOG_ERROR("endpoint = <%s> curl error = %d (%s)", endpoint, msg->data.result, curl_easy_strerror(msg->data.result)); http_responses.push_back(response_code); } time_earlier = time_now; @@ -282,7 +218,7 @@ namespace WPEFramework { #if LIBCURL_VERSION_NUM < 0x074200 if (CURLM_OK != (mc = curl_multi_wait(curl_multi_handle, NULL, 0, deadline - time_now, &numfds))) { - NMLOG_ERROR("curl_multi_wait returned %d (%s)", mc, curl_multi_strerror(mc)); + LOGERR("curl_multi_wait returned %d (%s)", mc, curl_multi_strerror(mc)); break; } if (numfds == 0) @@ -303,14 +239,14 @@ namespace WPEFramework { } if(curlVerboseEnabled()) { - NMLOG_INFO("endpoints count = %d response count %d, handles = %d, deadline = %ld, time_now = %ld, time_earlier = %ld", + NMLOG_TRACE("endpoints count = %d response count %d, handles = %d, deadline = %ld, time_now = %ld, time_earlier = %ld", static_cast(endpoints.size()), static_cast(http_responses.size()), handles, deadline, time_now, time_earlier); } for (const auto& curl_easy_handle : curl_easy_handles) { curl_easy_getinfo(curl_easy_handle, CURLINFO_PRIVATE, &endpoint); - //NMLOG_TRACE("endpoint = <%s> terminating attempt", endpoint); + //LOG_DBG("endpoint = <%s> terminating attempt", endpoint); curl_multi_remove_handle(curl_multi_handle, curl_easy_handle); curl_easy_cleanup(curl_easy_handle); } @@ -321,16 +257,16 @@ namespace WPEFramework { } /* - * verifying Most occurred response code is 50 % or more - * Example 1 : - * if we have 5 endpoints so response also 5 ( 204 302 204 204 200 ) . Here count is 204 :- 3, 302 :- 1, 200 :- 1 - * Return Internet State: FULLY_CONNECTED - 60 % - * Example 2 : - * if we have 4 endpoints so response also 4 ( 204 204 200 200 ) . Here count is 204 :- 2, 200 :- 2 - * Return Internet State: FULLY_CONNECTED - 50 % - */ - - nsm_internetState Connectivity::checkInternetStateFromResponseCode(const std::vector& responses) + * verifying Most occurred response code is 50 % or more + * Example 1 : + * if we have 5 endpoints so response also 5 ( 204 302 204 204 200 ) . Here count is 204 :- 3, 302 :- 1, 200 :- 1 + * Return Internet State: FULLY_CONNECTED - 60 % + * Example 2 : + * if we have 4 endpoints so response also 4 ( 204 204 200 200 ) . Here count is 204 :- 2, 200 :- 2 + * Return Internet State: FULLY_CONNECTED - 50 % + */ + + nsm_internetState TestConnectivity::checkInternetStateFromResponseCode(const std::vector& responses) { nsm_internetState InternetConnectionState = NO_INTERNET; nsm_connectivity_httpcode http_response_code = HttpStatus_response_error; @@ -365,220 +301,424 @@ namespace WPEFramework { case HttpStatus_511_Authentication_Required: case HttpStatus_302_Found: InternetConnectionState = CAPTIVE_PORTAL; - NMLOG_INFO("Internet State: CAPTIVE_PORTAL -%.1f%%", (percentage*100)); + NMLOG_INFO("Internet State: CAPTIVE_PORTAL - %.1f%%", (percentage*100)); break; default: InternetConnectionState = NO_INTERNET; if(http_response_code == -1) - NMLOG_ERROR("Internet State: NO_INTERNET curl error"); + NMLOG_ERROR("Internet State: NO_INTERNET (curl error)"); else - NMLOG_INFO("Internet State: NO_INTERNET Received http response code: <%d> %.1f%%", static_cast(http_response_code), percentage * 100); + NMLOG_WARNING("Internet State: NO_INTERNET (http code: %d - %.1f%%)", static_cast(http_response_code), percentage * 100); break; } } return InternetConnectionState; } - bool ConnectivityMonitor::doContinuousConnectivityMonitoring(int timeoutInSeconds) + ConnectivityMonitor::ConnectivityMonitor() { - if(!isConnectivityMonitorEndpointSet()) + if(endpointCache.isEndpointCashFileExist()) { - NMLOG_INFO("Connectivity monitor endpoints are not set !"); - return false; + std::vector cachedEndPnt = endpointCache.readEnpointsFromFile(); + setConnectivityMonitorEndpoints(cachedEndPnt); + NMLOG_WARNING("cached connectivity endpoints loaded .."); } + else + connectivityMonitorEndpt.push_back("http://clients3.google.com/generate_204"); + + doContinuousMonitor = false; + doConnectivityMonitor = false; + gInternetState = nsm_internetState::UNKNOWN; + gIpv4InternetState = nsm_internetState::UNKNOWN; + gIpv6InternetState = nsm_internetState::UNKNOWN; + ginterfaceStatus = false; + } - timeout.store(timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec); + ConnectivityMonitor::~ConnectivityMonitor() + { + NMLOG_WARNING("~ConnectivityMonitor"); + doContinuousMonitor = false; + doConnectivityMonitor = false; + cvConnectivityMonitor.notify_one(); + cvContinuousMonitor.notify_one(); + if (continuousMonitorThrd.joinable()) + continuousMonitorThrd.join(); + if (connectivityMonitorThrd.joinable()) + connectivityMonitorThrd.join(); + } - if (isMonitorThreadRunning() && stopFlag == false) - { - isContinuesMonitoringNeeded = true; - resetTimeout = true; - cv_.notify_all(); + std::vector ConnectivityMonitor::getConnectivityMonitorEndpoints() + { + const std::lock_guard lock(endpointMutex); + std::vector endpoints; + for (auto endpoint : connectivityMonitorEndpt) { + endpoints.push_back(endpoint); } - else - { - if (thread_.joinable()) - { - NMLOG_WARNING("Connectivity Monitor joinable Thread is active"); - stopFlag = true; - cv_.notify_all(); - thread_.join(); - } + return endpoints; + } - isContinuesMonitoringNeeded = true; - stopFlag = false; - thread_ = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this); - NMLOG_INFO("Connectivity monitor started with %d", timeout.load()); + void ConnectivityMonitor::setConnectivityMonitorEndpoints(const std::vector &endpoints) + { + const std::lock_guard lock(endpointMutex); + connectivityMonitorEndpt.clear(); + for (auto endpoint : endpoints) { + if(!endpoint.empty() && endpoint.size() > 3) + connectivityMonitorEndpt.push_back(endpoint.c_str()); + else + NMLOG_ERROR("endpoint not vallied = %s", endpoint.c_str()); } - return true; + // write the endpoints to a file + endpointCache.writeEnpointsToFile(connectivityMonitorEndpt); + + std::string endpointsStr; + for (const auto& endpoint : connectivityMonitorEndpt) + endpointsStr.append(endpoint).append(" "); + NMLOG_INFO("Connectivity monitor endpoints -: %d :- %s", static_cast(connectivityMonitorEndpt.size()), endpointsStr.c_str()); } - bool ConnectivityMonitor::doInitialConnectivityMonitoring(int timeoutInSeconds) + bool ConnectivityMonitor::isConnectedToInternet(nsm_ipversion ipversion) { - if(!isConnectivityMonitorEndpointSet()) + if (nsm_internetState::FULLY_CONNECTED == getInternetState(ipversion)) { - NMLOG_INFO("Connectivity monitor endpoints are not set !"); - return false; + NMLOG_INFO("isConnectedToInternet %s = true", (ipversion == nsm_ipversion::NSM_IPRESOLVE_WHATEVER)?"":(ipversion == nsm_ipversion::NSM_IPRESOLVE_V4? "IPv4":"IPv6")); + return true; } + NMLOG_WARNING("isConnectedToInternet %s = false",(ipversion == nsm_ipversion::NSM_IPRESOLVE_WHATEVER)?"":(ipversion == nsm_ipversion::NSM_IPRESOLVE_V4? "IPv4":"IPv6") ); + return false; + } - if (isMonitorThreadRunning() && stopFlag == false) - { - NMLOG_INFO("Connectivity Monitor Thread is active so notify"); - g_internetState = nsm_internetState::UNKNOWN; - cv_.notify_all(); + nsm_internetState ConnectivityMonitor::getInternetState(nsm_ipversion ipversion) + { + nsm_internetState internetState = nsm_internetState::UNKNOWN; + // If monitor connectivity is running take the cache value + + if ( doContinuousMonitor && nsm_ipversion::NSM_IPRESOLVE_V4 == ipversion + && gIpv4InternetState != nsm_internetState::UNKNOWN ) { + NMLOG_WARNING("Reading Ipv4 internet state cached value %s", getInternetStateString(gIpv4InternetState)); + internetState = gIpv4InternetState; + } + else if ( doContinuousMonitor && nsm_ipversion::NSM_IPRESOLVE_V6 == ipversion + && gIpv6InternetState != nsm_internetState::UNKNOWN ) { + NMLOG_WARNING("Reading Ipv6 internet state cached value %s", getInternetStateString(gIpv6InternetState)); + internetState = gIpv6InternetState; } else { - if (thread_.joinable()) - { - NMLOG_WARNING("Connectivity Monitor joinable Thread is active"); - stopFlag = true; - cv_.notify_all(); - thread_.join(); - } - - stopFlag = false; - timeout.store(timeoutInSeconds >= MONITOR_TIMEOUT_INTERVAL_MIN ? timeoutInSeconds:defaultTimeoutInSec); - thread_ = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this); - NMLOG_INFO("Initial Connectivity Monitoring started with %d", timeout.load()); + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, NMCONNECTIVITY_CURL_GET_REQUEST, ipversion); + internetState = testInternet.getInternetState(); } - - return true; + return internetState; } - bool ConnectivityMonitor::isMonitorThreadRunning() + std::string ConnectivityMonitor::getCaptivePortalURI() { - return thread_.joinable(); + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, NMCONNECTIVITY_CURL_GET_REQUEST, NSM_IPRESOLVE_WHATEVER); + if(nsm_internetState::CAPTIVE_PORTAL == testInternet.getInternetState()) + { + NMLOG_WARNING("captive portal URI = %s", testInternet.getCaptivePortal().c_str()); + return testInternet.getCaptivePortal(); + } + NMLOG_WARNING("No captive portal found !"); + return std::string(""); } - bool ConnectivityMonitor::stopInitialConnectivityMonitoring() + bool ConnectivityMonitor::startContinuousConnectivityMonitor(int timeoutInSeconds) { - if(isContinuesMonitoringNeeded) + continuousMonitorTimeout.store(timeoutInSeconds >= NMCONNECTIVITY_MONITOR_MIN_INTERVAL ? timeoutInSeconds : NMCONNECTIVITY_MONITOR_DEFAULT_INTERVAL); + if (doContinuousMonitor) { - NMLOG_WARNING("Continuous Connectivity Monitor is running"); + if(doConnectivityMonitor) + { + NMLOG_INFO("continuous monitor new timeout updated %d Sec", continuousMonitorTimeout.load()); + } + else + { + NMLOG_INFO("continuous monitor restarted with %d Sec", continuousMonitorTimeout.load()); + cvContinuousMonitor.notify_one(); + } return true; } - stopFlag = true; - cv_.notify_all(); + if (continuousMonitorThrd.joinable()) { + NMLOG_WARNING("continuous monitor joinable thread running"); + doContinuousMonitor = false; + continuousMonitorThrd.join(); + } - if (thread_.joinable()) - thread_.join(); + doContinuousMonitor = true; + continuousMonitorThrd = std::thread(&ConnectivityMonitor::continuousMonitorFunction, this); + NMLOG_INFO("continuous connectivity monitor started with %d Sec", continuousMonitorTimeout.load()); + if(!continuousMonitorThrd.joinable()) { + NMLOG_ERROR("continuous connectivity monitor start Failed"); + return false; + } + return true; + } - NMLOG_INFO("Initial Connectivity Monitor stopped"); + bool ConnectivityMonitor::stopContinuousConnectivityMonitor() + { + doContinuousMonitor = false; + cvContinuousMonitor.notify_one(); + if (continuousMonitorThrd.joinable()) + continuousMonitorThrd.join(); + NMLOG_INFO("continuous connectivity monitor stopped"); return true; } - bool ConnectivityMonitor::stopContinuousConnectivityMonitoring() + /* + * + * call startConnectivityMonitor function + * --> when IP address accuired + * --> when etherenet/wifi disconnected + */ + bool ConnectivityMonitor::startConnectivityMonitor(bool interfaceStatus) { - stopFlag = true; - cv_.notify_all(); + ginterfaceStatus = interfaceStatus; /* this will give interface status connected/ disconnected */ + gInternetState = UNKNOWN; + if (doConnectivityMonitor) + { + cvConnectivityMonitor.notify_one(); + NMLOG_TRACE("trigger connectivity monitor thread"); + return true; + } + + if (connectivityMonitorThrd.joinable()) { // cleanup of previous thread + doConnectivityMonitor = false; + connectivityMonitorThrd.join(); + } - if (thread_.joinable()) - thread_.join(); + doConnectivityMonitor = true; + connectivityMonitorThrd = std::thread(&ConnectivityMonitor::connectivityMonitorFunction, this); + if(!connectivityMonitorThrd.joinable()) { + NMLOG_ERROR("connectivity monitor start failed"); + return false; + } - isContinuesMonitoringNeeded = false; - NMLOG_INFO("Continuous Connectivity monitor stopped"); + NMLOG_ERROR("connectivity monitor started %d", NMCONNECTIVITY_MONITOR_MIN_INTERVAL); return true; } - void ConnectivityMonitor::signalConnectivityMonitor() + void ConnectivityMonitor::notifyInternetStatusChangedEvent(nsm_internetState newInternetState) { - if (isMonitorThreadRunning()) + if(_instance != nullptr) { - /* Reset the global value to UNKNOWN state so the cache is reset */ - g_internetState = nsm_internetState::UNKNOWN; - cv_.notify_all(); + Exchange::INetworkManager::InternetStatus oldState = static_cast(gInternetState.load()); + Exchange::INetworkManager::InternetStatus newState = static_cast(newInternetState); + _instance->ReportInternetStatusChangedEvent(oldState , newState); + gInternetState = newInternetState; } + else + NMLOG_WARNING("NetworkManagerImplementation Instance NULL notifyInternetStatusChange failed."); } - void ConnectivityMonitor::connectivityMonitorFunction() + void ConnectivityMonitor::continuousMonitorFunction() { - nsm_internetState InternetConnectionState = nsm_internetState::UNKNOWN; - int notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT; - int tempTimeout = defaultTimeoutInSec; + int TempInterval = continuousMonitorTimeout.load(); + std::mutex connMutex; + nsm_ipversion ipResolveTyp = NSM_IPRESOLVE_WHATEVER; + int notifyPreRetry = 1; + nsm_internetState currentInternetState = nsm_internetState::UNKNOWN; + do { - if(g_internetState.load() == nsm_internetState::FULLY_CONNECTED) - /*if previous check was fully connected then do connect only curl check*/ - InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, true); - else - /*curl get request*/ - InternetConnectionState = testConnectivity(getConnectivityMonitorEndpoints(), TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS, NSM_IPRESOLVE_WHATEVER, false); + if(doConnectivityMonitor) + { + NMLOG_TRACE("connectivity monitor running so skiping ccm check"); + gIpv4InternetState = UNKNOWN; + gIpv6InternetState = UNKNOWN; + std::unique_lock lock(connMutex); + cvContinuousMonitor.wait_for(lock, std::chrono::seconds(continuousMonitorTimeout.load())); + ipResolveTyp = NSM_IPRESOLVE_WHATEVER; /* some interface change happense*/ + continue; + } + else if (ipResolveTyp == NSM_IPRESOLVE_WHATEVER) + { + nsm_internetState ipV4InternetState = UNKNOWN; + nsm_internetState ipV6InternetState = UNKNOWN; + auto curlCheckThrdIpv4 = [&]() { + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, + NMCONNECTIVITY_CURL_GET_REQUEST, NSM_IPRESOLVE_V4); + ipV4InternetState = testInternet.getInternetState(); + }; + + auto curlCheckThrdIpv6 = [&]() { + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, + NMCONNECTIVITY_CURL_GET_REQUEST, NSM_IPRESOLVE_V6); + ipV6InternetState = testInternet.getInternetState(); + }; + std::thread ipv4thread(curlCheckThrdIpv4); + std::thread ipv6thread(curlCheckThrdIpv6); + // Wait for both threads to finish + ipv4thread.join(); + ipv6thread.join(); + if(ipV4InternetState == FULLY_CONNECTED) { + ipResolveTyp = NSM_IPRESOLVE_V4; + currentInternetState = ipV4InternetState; + NMLOG_INFO("connectivity monitor default ip resolve IPV4"); + } + else if(ipV6InternetState == FULLY_CONNECTED) { + ipResolveTyp = NSM_IPRESOLVE_V6; + currentInternetState = ipV6InternetState; + NMLOG_INFO("connectivity monitor default ip resolve IPV6"); + } + else /* not changing ip resolve type */ + currentInternetState = ipV4InternetState; + } + else /* IPV4 or IPV6 based on default values */ + { + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, + NMCONNECTIVITY_CURL_HEAD_REQUEST, ipResolveTyp); + currentInternetState = testInternet.getInternetState(); + if(ipResolveTyp == NSM_IPRESOLVE_V4) + gIpv4InternetState = currentInternetState; + else if(ipResolveTyp == NSM_IPRESOLVE_V6) + gIpv6InternetState = currentInternetState; + } - if(g_internetState.load() != InternetConnectionState) + if (currentInternetState == NO_INTERNET) { - NMLOG_TRACE("notification count %d ", notifyWaitCount); - if(InternetConnectionState == nsm_internetState::NO_INTERNET && isContinuesMonitoringNeeded && notifyWaitCount > 0) + if(gInternetState == FULLY_CONNECTED && notifyPreRetry < NMCONNECTIVITY_NO_INTERNET_RETRY_COUNT) { - /* Decrease the notification count to create a delay in posting the 'no internet' state. */ - notifyWaitCount--; - /* change the timeout value to 5 sec so that next check will happens with in 5 sec */ - tempTimeout = 5; - NMLOG_INFO("notification count change to %d timeout %d ", notifyWaitCount, tempTimeout); + /* it will prevent posting notification */ + currentInternetState = gInternetState; + TempInterval = 5; + NMLOG_INFO("No internet retrying connection check %d ...", notifyPreRetry); + notifyPreRetry++; + /* no internet state retry do it in ipv4 and ipv6 sepratly in two thread */ + ipResolveTyp = NSM_IPRESOLVE_WHATEVER; } else { - g_internetState.store(InternetConnectionState); - if(_instance != nullptr) - { - Exchange::INetworkManager::InternetStatus oldState = static_cast(g_internetState.load()); - Exchange::INetworkManager::InternetStatus newstate = static_cast(InternetConnectionState); - _instance->ReportInternetStatusChangedEvent(oldState , newstate); - NMLOG_TRACE("ReportInternetStatusChangedEvent called"); - } - else - NMLOG_WARNING("ReportInternetStatusChangedEvent callback not set"); - notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT; - /* change the timeout value to actual requested value */ - tempTimeout = timeout.load(); - NMLOG_TRACE("notification count change to default %d ...", notifyWaitCount); + notifyPreRetry = 1; + TempInterval = continuousMonitorTimeout.load(); } } - else - { - notifyWaitCount = DEFAULT_MONITOR_RETRY_COUNT; - tempTimeout = timeout.load(); + else { + notifyPreRetry = 1; + TempInterval = continuousMonitorTimeout.load(); } - if(!isContinuesMonitoringNeeded && (g_internetState.load() == FULLY_CONNECTED)) + if(gInternetState != currentInternetState) { - stopFlag = true; - NMLOG_INFO("Initial Connectivity Monitoring done Exiting ... FULLY_CONNECTED"); - break; + NMLOG_INFO("Internet state changed to %s", getInternetStateString(currentInternetState)); + /* Notify Internet state change */ + notifyInternetStatusChangedEvent(currentInternetState); } - if(stopFlag) + //NMLOG_INFO("icm %d, ccm %d", doConnectivityMonitor.load(), doContinuousMonitor.load()); + if(!doContinuousMonitor) break; - // wait for next timout or conditon signal - std::unique_lock lock(mutex_); - if (cv_.wait_for(lock, std::chrono::seconds(tempTimeout)) != std::cv_status::timeout) + + /* wait for next interval */ + std::unique_lock lock(connMutex); + if (cvContinuousMonitor.wait_for(lock, std::chrono::seconds(TempInterval)) != std::cv_status::timeout) + NMLOG_INFO("continous connectivity monitor recieved signal. skping %d sec interval", TempInterval); + + } while(doContinuousMonitor); + + gIpv4InternetState = UNKNOWN; + gIpv6InternetState = UNKNOWN; + NMLOG_TRACE("continous connectivity monitor exit"); + } + + void ConnectivityMonitor::connectivityMonitorFunction() + { + int TempInterval = NMCONNECTIVITY_MONITOR_MIN_INTERVAL; + std::mutex connMutex; + bool notifyNow = true; + int notifyPreRetry = 1; + nsm_internetState currentInternetState = nsm_internetState::UNKNOWN; + nsm_internetState tempInternetState = nsm_internetState::UNKNOWN; + + do + { + TestConnectivity testInternet(getConnectivityMonitorEndpoints(), NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS, + NMCONNECTIVITY_CURL_GET_REQUEST, NSM_IPRESOLVE_WHATEVER); + currentInternetState = testInternet.getInternetState(); + if(currentInternetState == CAPTIVE_PORTAL) + { + /* set to every 30 sec interval */ + TempInterval = NMCONNECTIVITY_CONN_MONITOR_RETRY_INTERVAL; + } + else if(currentInternetState == LIMITED_INTERNET) + { + TempInterval = NMCONNECTIVITY_CONN_MONITOR_RETRY_INTERVAL; + } + else // fullyconnect / noInternet { - if(!stopFlag) + if(notifyPreRetry <= NMCONNECTIVITY_CONN_MONITOR_RETRY_COUNT - 1) { - /* - * We don't need to notify immediately when restarting the thread. - * Immediate notification should occur only when any connection change happens. - */ - if(resetTimeout) + if(tempInternetState != currentInternetState ) // check for continous same state { - NMLOG_INFO("Connectivity monitor Restarted with %d", timeout.load()); - tempTimeout = timeout.load(); - resetTimeout = false; + tempInternetState = currentInternetState; + notifyPreRetry = 1; + NMLOG_INFO("Connectivity check retrying %d ...", notifyPreRetry); } else { - notifyWaitCount = -1; - NMLOG_INFO("Connectivity monitor received trigger"); + notifyPreRetry++; + NMLOG_INFO("Connectivity check retrying %d ...", notifyPreRetry); } + + if(gInternetState != UNKNOWN) + currentInternetState = gInternetState; + TempInterval = 5; + } + else if(tempInternetState != currentInternetState) // last state have change + { + tempInternetState = currentInternetState; + notifyPreRetry = 1; + TempInterval = 5; + NMLOG_INFO("Connectivity check retrying %d ...", notifyPreRetry); } + else + { + if(currentInternetState == FULLY_CONNECTED) + { + doConnectivityMonitor = false; // self exit + notifyNow = true; // post current state when retry complete + } + else if(ginterfaceStatus) // interface is active and still no internet, continue check every 30 sec + { + TempInterval = NMCONNECTIVITY_CONN_MONITOR_RETRY_INTERVAL; + /* notify if retry completed and state stil no internet state */ + if(notifyPreRetry == NMCONNECTIVITY_CONN_MONITOR_RETRY_COUNT) + { + notifyPreRetry++; + notifyNow = true; + } + } + else // no interface connected + { + doConnectivityMonitor = false; + notifyNow = true; + } + } + } + + if(gInternetState != currentInternetState || notifyNow) + { + notifyNow = false; + NMLOG_INFO("notify internet state %s", getInternetStateString(currentInternetState)); + notifyInternetStatusChangedEvent(currentInternetState); } - } while (!stopFlag); + if(!doConnectivityMonitor) + break; + /* wait for next interval */ + std::unique_lock lock(connMutex); + if (cvConnectivityMonitor.wait_for(lock, std::chrono::seconds(TempInterval)) != std::cv_status::timeout) { + NMLOG_INFO("connectivity monitor recieved signal. skping %d sec interval", TempInterval); + notifyPreRetry = 1; + notifyNow = true; // new signal came should notify in next check + } + + } while(doConnectivityMonitor); - g_internetState = nsm_internetState::UNKNOWN; - NMLOG_WARNING("Connectivity monitor exiting"); + if(!doContinuousMonitor) + gInternetState = nsm_internetState::UNKNOWN; // no continous monitor running reset to unknow + NMLOG_TRACE("initial connectivity monitor exit"); } } // namespace Plugin diff --git a/NetworkManager/NetworkManagerConnectivity.h b/NetworkManager/NetworkManagerConnectivity.h index e99c0586d5..9259aa8a06 100644 --- a/NetworkManager/NetworkManagerConnectivity.h +++ b/NetworkManager/NetworkManagerConnectivity.h @@ -1,33 +1,52 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2020 RDK Management + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #pragma once -#include +#include #include #include +#include +#include #include #include #include #include -#include "Module.h" -#include "NetworkManagerLogger.h" - -#define CAPTIVEPORTAL_MAX_LEN 512 -#define DEFAULT_MONITOR_TIMEOUT 60 // in seconds -#define MONITOR_TIMEOUT_INTERVAL_MIN 5 // in min -#define TEST_CONNECTIVITY_DEFAULT_TIMEOUT_MS 5000 // in ms -#define DEFAULT_MONITOR_RETRY_COUNT 2 // 1 failed + 2 new trys - -enum nsm_ipversion { +#include +#include +#include +#include +#include + +enum nsm_ipversion +{ NSM_IPRESOLVE_WHATEVER = 0, /* default, resolves addresses to all IP*/ NSM_IPRESOLVE_V4 = 1, /* resolve to IPv4 addresses */ NSM_IPRESOLVE_V6 = 2 /* resolve to IPv6 addresses */ }; enum nsm_internetState { - UNKNOWN = -1, NO_INTERNET, LIMITED_INTERNET, CAPTIVE_PORTAL, - FULLY_CONNECTED + FULLY_CONNECTED, + UNKNOWN, }; enum nsm_connectivity_httpcode { @@ -43,104 +62,89 @@ enum nsm_connectivity_httpcode { HttpStatus_511_Authentication_Required = 511 // captive portal RFC 6585 }; -namespace WPEFramework { - namespace Plugin { - +#define NMCONNECTIVITY_CURL_HEAD_REQUEST true +#define NMCONNECTIVITY_CURL_GET_REQUEST false +#define NMCONNECTIVITY_DNS_RESOLVE_FILE "/etc/resolv.dnsmasq" + +#define NMCONNECTIVITY_MONITOR_DEFAULT_INTERVAL 60 // sec +#define NMCONNECTIVITY_MONITOR_MIN_INTERVAL 5 // sec +#define NMCONNECTIVITY_CURL_REQUEST_TIMEOUT_MS 5000 // ms +#define NMCONNECTIVITY_NO_INTERNET_RETRY_COUNT 4 // 4 retry +#define NMCONNECTIVITY_CONN_MONITOR_RETRY_COUNT 4 // 4 retry +#define NMCONNECTIVITY_CAPTIVE_MONITOR_INTERVAL 30 // sec +#define NMCONNECTIVITY_CONN_MONITOR_RETRY_INTERVAL 30 // sec + +namespace WPEFramework +{ + namespace Plugin + { /* save user specific endponint in to a chache file and load form the file if monitorEndpoints are empty case wpeframework restared */ class EndpointCache { public: - static EndpointCache& getInstance() { - static EndpointCache instance; - return instance; - } - bool isEndpointCashFileExist(); void writeEnpointsToFile(const std::vector& endpoints); std::vector readEnpointsFromFile(); - private: - EndpointCache() : CachefilePath("/tmp/network.plugin.endpoints") {} + EndpointCache() : CachefilePath("/tmp/nm.plugin.endpoints") {} ~EndpointCache(){} - + private: std::string CachefilePath; }; - class Connectivity { - - Connectivity(const Connectivity&) = delete; - const Connectivity& operator=(const Connectivity&) = delete; + class TestConnectivity + { + TestConnectivity(const TestConnectivity&) = delete; + const TestConnectivity& operator=(const TestConnectivity&) = delete; public: - Connectivity(const std::string& configFilePath = "/etc/netsrvmgr.conf") - { - if(m_defaultEndpoints.empty()) - { - NMLOG_ERROR("NETSRVMGR CONFIGURATION ERROR: CONNECTIVITY ENDPOINT EMPTY"); - m_defaultEndpoints.clear(); - m_defaultEndpoints.push_back("http://clients3.google.com/generate_204"); - } - } - ~Connectivity(){} - - nsm_internetState testConnectivity(const std::vector& endpoints, long timeout_ms, nsm_ipversion ipversion, bool connectOnly); - std::vector getConnectivityDefaultEndpoints() { return m_defaultEndpoints; }; - std::string getCaptivePortal() { const std::lock_guard lock(capitiveMutex); return g_captivePortal; } - void setCaptivePortal(const char* captivePortal) {const std::lock_guard lock(capitiveMutex); g_captivePortal = captivePortal; } - + TestConnectivity(const std::vector& endpoints, long timeout_ms = 2000, bool = true, nsm_ipversion ipversion = NSM_IPRESOLVE_WHATEVER); + ~TestConnectivity(){} + std::string getCaptivePortal() {return captivePortalURI;} + nsm_internetState getInternetState(){return internetSate;} private: + nsm_internetState checkCurlResponse(const std::vector& endpoints, long timeout_ms, bool headReq, nsm_ipversion ipversion); nsm_internetState checkInternetStateFromResponseCode(const std::vector& responses); - - std::vector m_defaultEndpoints; - std::map configMap; - std::mutex capitiveMutex; - std::string g_captivePortal; - bool configMonitorConnectivityEnabled = false; - int configMonitorInterval = DEFAULT_MONITOR_TIMEOUT; + std::string captivePortalURI; + nsm_internetState internetSate; }; - class ConnectivityMonitor : public Connectivity { - public: - bool isConnectedToInternet(nsm_ipversion ipversion); - nsm_internetState getInternetConnectionState(nsm_ipversion ipversion); - std::string getCaptivePortalURI(); - void setConnectivityMonitorEndpoints(const std::vector &endpoints); - std::vector getConnectivityMonitorEndpoints(); - bool doContinuousConnectivityMonitoring(int timeoutInSeconds); - bool doInitialConnectivityMonitoring(int timeoutInSeconds); - bool stopContinuousConnectivityMonitoring(); - bool stopInitialConnectivityMonitoring(); - bool isConnectivityMonitorEndpointSet(); - bool isMonitorThreadRunning(); - void signalConnectivityMonitor(); - - ConnectivityMonitor() : stopFlag(false), resetTimeout(false), isContinuesMonitoringNeeded(false) - { - setConnectivityMonitorEndpoints(getConnectivityDefaultEndpoints()); - } - - ~ConnectivityMonitor() { - NMLOG_INFO("~ConnectivityMonitor"); - stopContinuousConnectivityMonitoring(); - } - - private: - ConnectivityMonitor(const ConnectivityMonitor&) = delete; - ConnectivityMonitor& operator=(const ConnectivityMonitor&) = delete; - void connectivityMonitorFunction(); - - EndpointCache& endpointCache = EndpointCache::getInstance(); + class ConnectivityMonitor + { + public: + ConnectivityMonitor(); + ~ConnectivityMonitor(); + bool startContinuousConnectivityMonitor(int timeoutInSeconds); + bool stopContinuousConnectivityMonitor(); + bool startConnectivityMonitor(bool interfaceStatus); + void setConnectivityMonitorEndpoints(const std::vector &endpoints); + std::vector getConnectivityMonitorEndpoints(); + bool isConnectedToInternet(nsm_ipversion ipversion); + nsm_internetState getInternetState(nsm_ipversion ipversion); + std::string getCaptivePortalURI(); - std::thread thread_; - std::atomic stopFlag; - std::atomic resetTimeout; - std::atomic isContinuesMonitoringNeeded; - std::condition_variable cv_; - std::atomic timeout; - std::vector monitorEndpoints; - const int defaultTimeoutInSec = DEFAULT_MONITOR_TIMEOUT; - std::mutex mutex_; - std::mutex endpointMutex; - std::atomic g_internetState = {nsm_internetState::UNKNOWN}; + private: + ConnectivityMonitor(const ConnectivityMonitor&) = delete; + ConnectivityMonitor& operator=(const ConnectivityMonitor&) = delete; + void connectivityMonitorFunction(); + void notifyInternetStatusChangedEvent(nsm_internetState newState); + /* connectivity monitor */ + std::thread connectivityMonitorThrd; + std::condition_variable cvConnectivityMonitor; + std::atomic continuousMonitorTimeout; + std::atomic doConnectivityMonitor; + std::vector connectivityMonitorEndpt; + /*continuous connectivity monitor */ + std::atomic doContinuousMonitor; + std::thread continuousMonitorThrd; + std::condition_variable cvContinuousMonitor; + void continuousMonitorFunction(); + + EndpointCache endpointCache; + std::mutex endpointMutex; + std::atomic ginterfaceStatus; + std::atomic gInternetState; + std::atomic gIpv4InternetState; + std::atomic gIpv6InternetState; }; } // namespace Plugin } // namespace WPEFramework diff --git a/NetworkManager/NetworkManagerImplementation.cpp b/NetworkManager/NetworkManagerImplementation.cpp index e11cde15ac..ea64d92c4b 100644 --- a/NetworkManager/NetworkManagerImplementation.cpp +++ b/NetworkManager/NetworkManagerImplementation.cpp @@ -54,7 +54,6 @@ namespace WPEFramework NetworkManagerImplementation::~NetworkManagerImplementation() { - connectivityMonitor.stopContinuousConnectivityMonitoring(); LOG_ENTRY_FUNCTION(); } @@ -119,27 +118,35 @@ namespace WPEFramework NMLOG_TRACE("config : stun port %d", m_stunPort); NMLOG_TRACE("config : stun interval %d", m_stunBindTimeout); - NMLOG_TRACE("config : endpoint 1 %s", config.connectivity.endpoint_1.Value().c_str()); - NMLOG_TRACE("config : endpoint 2 %s", config.connectivity.endpoint_2.Value().c_str()); - NMLOG_TRACE("config : endpoint 3 %s", config.connectivity.endpoint_3.Value().c_str()); - NMLOG_TRACE("config : endpoint 4 %s", config.connectivity.endpoint_4.Value().c_str()); - NMLOG_TRACE("config : endpoint 5 %s", config.connectivity.endpoint_5.Value().c_str()); - NMLOG_TRACE("config : interval %d", config.connectivity.ConnectivityCheckInterval.Value()); - NMLOG_TRACE("config : loglevel %d", config.loglevel.Value()); logLevel = static_cast (config.loglevel.Value()); // configure loglevel in libWPEFrameworkNetworkManagerImplementation.so NetworkManagerLogger::SetLevel(static_cast (logLevel)); - std::vector endpoints; - endpoints.push_back(config.connectivity.endpoint_1.Value().c_str()); - endpoints.push_back(config.connectivity.endpoint_2.Value().c_str()); - endpoints.push_back(config.connectivity.endpoint_3.Value().c_str()); - endpoints.push_back(config.connectivity.endpoint_4.Value().c_str()); - endpoints.push_back(config.connectivity.endpoint_5.Value().c_str()); + /* load connectivity monitor endpoints */ + std::vector connectEndpts; + if(!config.connectivityConf.endpoint_1.Value().empty()) { + NMLOG_TRACE("config : connectivity enpt 1 %s", config.connectivityConf.endpoint_1.Value().c_str()); + connectEndpts.push_back(config.connectivityConf.endpoint_1.Value().c_str()); + } + if(!config.connectivityConf.endpoint_2.Value().empty()) { + NMLOG_TRACE("config : connectivity enpt 2 %s", config.connectivityConf.endpoint_2.Value().c_str()); + connectEndpts.push_back(config.connectivityConf.endpoint_2.Value().c_str()); + } + if(!config.connectivityConf.endpoint_3.Value().empty()) { + NMLOG_TRACE("config : connectivity enpt 3 %s", config.connectivityConf.endpoint_3.Value().c_str()); + connectEndpts.push_back(config.connectivityConf.endpoint_3.Value().c_str()); + } + if(!config.connectivityConf.endpoint_4.Value().empty()) { + NMLOG_TRACE("config : connectivity enpt 4 %s", config.connectivityConf.endpoint_4.Value().c_str()); + connectEndpts.push_back(config.connectivityConf.endpoint_4.Value().c_str()); + } + if(!config.connectivityConf.endpoint_5.Value().empty()) { + NMLOG_TRACE("config : connectivity enpt 5 %s", config.connectivityConf.endpoint_5.Value().c_str()); + connectEndpts.push_back(config.connectivityConf.endpoint_5.Value().c_str()); + } - //set connectivity endpoint - connectivityMonitor.setConnectivityMonitorEndpoints(endpoints); + connectivityMonitor.setConnectivityMonitorEndpoints(connectEndpts); } else NMLOG_ERROR("Plugin configuration read error !"); @@ -210,7 +217,7 @@ namespace WPEFramework else if(0 == strcasecmp("IPv6", ipversion.c_str())) tmpVersion = NSM_IPRESOLVE_V6; - isconnected = connectivityMonitor.getInternetConnectionState(tmpVersion); + isconnected = connectivityMonitor.getInternetState(tmpVersion); if (FULLY_CONNECTED == isconnected) result = INTERNET_FULLY_CONNECTED; else if (CAPTIVE_PORTAL == isconnected) @@ -235,7 +242,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StartConnectivityMonitoring(const uint32_t interval/* @in */) { LOG_ENTRY_FUNCTION(); - if (connectivityMonitor.doContinuousConnectivityMonitoring(interval)) + if (connectivityMonitor.startContinuousConnectivityMonitor(interval)) return Core::ERROR_NONE; else return Core::ERROR_GENERAL; @@ -245,7 +252,7 @@ namespace WPEFramework uint32_t NetworkManagerImplementation::StopConnectivityMonitoring(void) const { LOG_ENTRY_FUNCTION(); - if (connectivityMonitor.stopContinuousConnectivityMonitoring()) + if (connectivityMonitor.stopContinuousConnectivityMonitor()) return Core::ERROR_NONE; else return Core::ERROR_GENERAL; diff --git a/NetworkManager/NetworkManagerImplementation.h b/NetworkManager/NetworkManagerImplementation.h index 76cbd00b5a..a18eafe69c 100644 --- a/NetworkManager/NetworkManagerImplementation.h +++ b/NetworkManager/NetworkManagerImplementation.h @@ -54,11 +54,11 @@ namespace WPEFramework Config& operator=(const Config&); public: - class Connectivity : public Core::JSON::Container { + class ConnectivityConf : public Core::JSON::Container { public: - Connectivity& operator=(const Connectivity&) = delete; + ConnectivityConf& operator=(const ConnectivityConf&) = delete; - Connectivity() + ConnectivityConf() : Core::JSON::Container() , endpoint_1(_T("http://clients3.google.com/generate_204")) , endpoint_2(_T("")) @@ -74,7 +74,7 @@ namespace WPEFramework Add(_T("endpoint_5"), &endpoint_5); Add(_T("interval"), &ConnectivityCheckInterval); } - ~Connectivity() override = default; + ~ConnectivityConf() override = default; public: /* connectivity configuration */ @@ -113,14 +113,14 @@ namespace WPEFramework Config() : Core::JSON::Container() { - Add(_T("connectivity"), &connectivity); + Add(_T("connectivity"), &connectivityConf); Add(_T("stun"), &stun); Add(_T("loglevel"), &loglevel); } ~Config() override = default; public: - Connectivity connectivity; + ConnectivityConf connectivityConf; Stun stun; Core::JSON::DecUInt32 loglevel; }; diff --git a/NetworkManager/NetworkManagerRDKProxy.cpp b/NetworkManager/NetworkManagerRDKProxy.cpp index 861afcb4ec..c00f8a47ab 100644 --- a/NetworkManager/NetworkManagerRDKProxy.cpp +++ b/NetworkManager/NetworkManagerRDKProxy.cpp @@ -1,4 +1,5 @@ #include "NetworkManagerImplementation.h" +#include "NetworkManagerConnectivity.h" #include "WiFiSignalStrengthMonitor.h" #include "libIBus.h" @@ -428,8 +429,7 @@ namespace WPEFramework else { ::_instance->ReportInterfaceStateChangedEvent(Exchange::INetworkManager::INTERFACE_LINK_DOWN, interface); /* when ever interface down we start connectivity monitor to post noInternet event */ - ::_instance->connectivityMonitor.doInitialConnectivityMonitoring(5); - ::_instance->connectivityMonitor.stopInitialConnectivityMonitoring(); + ::_instance->connectivityMonitor.startConnectivityMonitor(false); // false = interface is down, auto exit after retry } } break; @@ -445,7 +445,7 @@ namespace WPEFramework if(e->acquired) { /* if ip address acquired we start connectivity monitor */ - ::_instance->connectivityMonitor.doInitialConnectivityMonitoring(5); + ::_instance->connectivityMonitor.startConnectivityMonitor(true); // true = interface is up } } break;