diff --git a/services/time_manager/include/ntp_update_time.h b/services/time_manager/include/ntp_update_time.h index 4b025fee2ae3c0b53c01dd3419bd8b041d9f0694..303e1c7c7b0f452b3d268396102869b22ecc8058 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 6febfd2b63cf52d68854c60f3d8731f5730820b7..837924d9c7d1f17313896ef31a4506e263ca3b61 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,6 +93,23 @@ 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 NetSpecifier(netSpecifier); + sptr observer = new NetConnCallbackObserver; + 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