Signed-off-by: lovechinamo <wangdongqi2@huawei.com>

Changes to be committed:
This commit is contained in:
lovechinamo
2022-05-24 17:22:02 +08:00
parent a33402b071
commit 920b992336
2 changed files with 34 additions and 9 deletions
@@ -37,6 +37,7 @@ public:
void SetSystemTime();
void Stop();
void Init();
int32_t MonitorNetwork();
private:
bool GetAutoTimeInfoFromFile(autoTimeInfo &info);
bool SaveAutoTimeInfoToFile(autoTimeInfo &info);
+33 -9
View File
@@ -19,6 +19,7 @@
#include <string>
#include <fstream>
#include <mutex>
#include <unistd.h>
#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<NetSpecifier> specifier = new NetSpecifier(netSpecifier);
sptr<NetConnCallbackObserver> observer = new NetConnCallbackObserver;
DelayedSingleton<NetConnClient>::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) {
@@ -85,8 +91,26 @@ void NtpUpdateTime::Init()
RefreshNextTriggerTime();
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp update triggertime: %{public}" PRId64 "", nextTriggerTime_);
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<NetSpecifier> specifier = new NetSpecifier(netSpecifier);
sptr<NetConnCallbackObserver> observer = new NetConnCallbackObserver;
int nRet = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetConnCallback(specifier, observer, 0);
TIME_HILOGD(TIME_MODULE_SERVICE,"RegisterNetConnCallback retcode= %{public}d", nRet);
return nRet;
}
void NtpUpdateTime::SubscriberNITZTimeChangeCommonEvent()
{
// Broadcast subscription