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

Changes to be committed:
This commit is contained in:
lovechinamo
2022-05-26 10:11:00 +08:00
parent bd2f812d42
commit ee8d598e71
2 changed files with 41 additions and 10 deletions
@@ -37,6 +37,7 @@ public:
void SetSystemTime();
void Stop();
void Init();
int32_t MonitorNetwork();
private:
bool GetAutoTimeInfoFromFile(autoTimeInfo &info);
bool SaveAutoTimeInfoToFile(autoTimeInfo &info);
+40 -10
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) {
@@ -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<NetSpecifier> 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<NetConnCallbackObserver> 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<NetConnClient>::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<NITZSubscriber> subscriberPtr =
std::make_shared<NITZSubscriber>(subscriberInfo);