From ee8d598e71f26afaf8b853ea0d64a76bd238b044 Mon Sep 17 00:00:00 2001 From: lovechinamo Date: Thu, 26 May 2022 10:11:00 +0800 Subject: [PATCH] Signed-off-by: lovechinamo Changes to be committed: --- .../time_manager/include/ntp_update_time.h | 1 + services/time_manager/src/ntp_update_time.cpp | 50 +++++++++++++++---- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/services/time_manager/include/ntp_update_time.h b/services/time_manager/include/ntp_update_time.h index 4b025fe..303e1c7 100644 --- a/services/time_manager/include/ntp_update_time.h +++ b/services/time_manager/include/ntp_update_time.h @@ -37,6 +37,7 @@ public: void SetSystemTime(); void Stop(); void Init(); + int32_t MonitorNetwork(); private: bool GetAutoTimeInfoFromFile(autoTimeInfo &info); bool SaveAutoTimeInfoToFile(autoTimeInfo &info); diff --git a/services/time_manager/src/ntp_update_time.cpp b/services/time_manager/src/ntp_update_time.cpp index 5202175..0a41ea5 100644 --- a/services/time_manager/src/ntp_update_time.cpp +++ b/services/time_manager/src/ntp_update_time.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "ntp_trusted_time.h" #include "time_common.h" @@ -66,15 +67,20 @@ void NtpUpdateTime::Init() return; } } - // observer net connection - NetSpecifier netSpecifier; - NetAllCapabilities netAllCapabilities; - netAllCapabilities.netCaps_.insert(NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET); - netSpecifier.ident_ = "wifi"; - netSpecifier.netCapabilities_ = netAllCapabilities; - sptr specifier = new NetSpecifier(netSpecifier); - sptr observer = new NetConnCallbackObserver; - DelayedSingleton::GetInstance()->RegisterNetConnCallback(specifier, observer, 0); + + std::thread th = std::thread([this]() { + constexpr int RETRY_MAX_TIMES = 100; + int retryCount = 0; + constexpr int RETRY_TIME_INTERVAL_MILLISECOND = 1 * 1000 * 1000; // retry after 2 second + do { + if (this->MonitorNetwork() == NET_CONN_SUCCESS) { + break; + } + retryCount++; + usleep(RETRY_TIME_INTERVAL_MILLISECOND); + } while (retryCount < RETRY_MAX_TIMES); + }); + th.detach(); int32_t timerType = ITimerManager::TimerType::ELAPSED_REALTIME; auto callback = [this](uint64_t id) { @@ -87,11 +93,35 @@ void NtpUpdateTime::Init() TimeService::GetInstance()->StartTimer(timerId_, nextTriggerTime_); } +int32_t NtpUpdateTime::MonitorNetwork() +{ + // observer net connection + TIME_HILOGD(TIME_MODULE_SERVICE, "NtpUpdateTime::MonitorNetwork"); + NetSpecifier netSpecifier; + NetAllCapabilities netAllCapabilities; + netAllCapabilities.netCaps_.insert(NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET); + netSpecifier.netCapabilities_ = netAllCapabilities; + sptr specifier = new(std::nothrow) NetSpecifier(netSpecifier); + if (specifier == nullptr) { + TIME_HILOGD(TIME_MODULE_SERVICE, "new operator error.specifier is nullptr"); + return NET_CONN_ERR_INPUT_NULL_PTR; + } + sptr observer = new(std::nothrow) NetConnCallbackObserver(); + if (observer == nullptr) { + TIME_HILOGD(TIME_MODULE_SERVICE, "new operator error.observer is nullptr"); + return NET_CONN_ERR_INPUT_NULL_PTR; + } + int nRet = DelayedSingleton::GetInstance()->RegisterNetConnCallback(specifier, observer, 0); + TIME_HILOGD(TIME_MODULE_SERVICE, "RegisterNetConnCallback retcode= %{public}d", nRet); + + return nRet; +} + void NtpUpdateTime::SubscriberNITZTimeChangeCommonEvent() { // Broadcast subscription MatchingSkills matchingSkills; - matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_NITZ_TIME_UPDATED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED); CommonEventSubscribeInfo subscriberInfo(matchingSkills); std::shared_ptr subscriberPtr = std::make_shared(subscriberInfo);