mirror of
https://github.com/openharmony/miscservices_time.git
synced 2026-07-19 20:03:38 -04:00
@@ -45,9 +45,13 @@ napi_value TimeNapiGetNull(napi_env env)
|
||||
return result;
|
||||
}
|
||||
|
||||
void TimeSetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result)
|
||||
void TimeSetPromise(const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result)
|
||||
{
|
||||
napi_resolve_deferred(env, deferred, result);
|
||||
if (errorCode == NO_ERROR) {
|
||||
NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, deferred, result));
|
||||
return;
|
||||
}
|
||||
NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, deferred, result));
|
||||
}
|
||||
|
||||
void TimeSetCallback(const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result)
|
||||
@@ -69,7 +73,7 @@ void TimeReturnCallbackPromise(const napi_env &env, const TimeCallbackPromiseInf
|
||||
if (info.isCallback) {
|
||||
TimeSetCallback(env, info.callback, info.errorCode, result);
|
||||
} else {
|
||||
TimeSetPromise(env, info.deferred, result);
|
||||
TimeSetPromise(env, info.deferred, info.errorCode, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +85,7 @@ napi_value TimeJSParaError(const napi_env &env, const napi_ref &callback)
|
||||
napi_value promise = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_create_promise(env, &deferred, &promise);
|
||||
TimeSetPromise(env, deferred, TimeNapiGetNull(env));
|
||||
TimeSetPromise(env, deferred, ERROR, TimeNapiGetNull(env));
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,9 +112,13 @@ napi_value GetCallbackErrorValue(napi_env env, int errCode)
|
||||
return result;
|
||||
}
|
||||
|
||||
void SetPromise(const napi_env &env, const napi_deferred &deferred, const napi_value &result)
|
||||
void SetPromise(const napi_env &env, const napi_deferred &deferred, const int &errorCode, const napi_value &result)
|
||||
{
|
||||
napi_resolve_deferred(env, deferred, result);
|
||||
if (errorCode == NO_ERROR) {
|
||||
NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, deferred, result));
|
||||
return;
|
||||
}
|
||||
NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, deferred, result));
|
||||
}
|
||||
|
||||
void SetCallback(const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result)
|
||||
@@ -139,7 +143,7 @@ napi_value JSParaError(const napi_env &env, const napi_ref &callback)
|
||||
napi_value promise = nullptr;
|
||||
napi_deferred deferred = nullptr;
|
||||
napi_create_promise(env, &deferred, &promise);
|
||||
SetPromise(env, deferred, NapiGetNull(env));
|
||||
SetPromise(env, deferred, ERROR, NapiGetNull(env));
|
||||
return promise;
|
||||
}
|
||||
}
|
||||
@@ -149,7 +153,7 @@ void ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info,
|
||||
if (info.isCallback) {
|
||||
SetCallback(env, info.callback, info.errorCode, result);
|
||||
} else {
|
||||
SetPromise(env, info.deferred, result);
|
||||
SetPromise(env, info.deferred, info.errorCode, result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ ohos_shared_library("time_service") {
|
||||
"time_manager/src/timer_call_back.cpp",
|
||||
"time_manager/src/timer_call_back_proxy.cpp",
|
||||
"time_manager/src/timer_call_back_stub.cpp",
|
||||
"time_manager/src/net_conn_callback_observer.cpp",
|
||||
"timer/src/batch.cpp",
|
||||
"timer/src/timer_handler.cpp",
|
||||
"timer/src/timer_info.cpp",
|
||||
@@ -65,6 +66,7 @@ ohos_shared_library("time_service") {
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/base:base",
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/intent:intent",
|
||||
"//foundation/aafwk/standard/interfaces/innerkits/want:want",
|
||||
"//foundation/communication/netmanager_base/interfaces/innerkits/netconnclient:net_conn_manager_if",
|
||||
"//third_party/jsoncpp:jsoncpp",
|
||||
"//utils/native/base:utils",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H
|
||||
#define NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H
|
||||
|
||||
#include "net_all_capabilities.h"
|
||||
#include "net_conn_callback_stub.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
class NetConnCallbackObserver : public NetManagerStandard::NetConnCallbackStub {
|
||||
public:
|
||||
int32_t NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle) override;
|
||||
|
||||
int32_t NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> &netHandle, const sptr<NetManagerStandard::NetAllCapabilities> &netAllCap) override;
|
||||
|
||||
int32_t NetConnectionPropertiesChange(sptr<NetManagerStandard::NetHandle> &netHandle, const sptr<NetManagerStandard::NetLinkInfo> &info) override;
|
||||
|
||||
int32_t NetLost(sptr<NetManagerStandard::NetHandle> &netHandle) override;
|
||||
|
||||
int32_t NetUnavailable() override;
|
||||
|
||||
int32_t NetBlockStatusChange(sptr<NetManagerStandard::NetHandle> &netHandle, bool blocked) override;
|
||||
};
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
#endif /* NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H */
|
||||
@@ -34,15 +34,15 @@ public:
|
||||
void UpdateStatusOff();
|
||||
void UpdateStatusOn();
|
||||
void UpdateNITZSetTime();
|
||||
void SetSystemTime();
|
||||
void Stop();
|
||||
void Init();
|
||||
private:
|
||||
bool GetAutoTimeInfoFromFile(autoTimeInfo &info);
|
||||
bool SaveAutoTimeInfoToFile(autoTimeInfo &info);
|
||||
void SubscriberNITZTimeChangeCommonEvent();
|
||||
void StartTimer();
|
||||
void SetSystemTime();
|
||||
bool ThreadSetSystemTime();
|
||||
void StartTimer();
|
||||
void RefreshNextTriggerTime();
|
||||
bool CheckStatus();
|
||||
bool IsNITZTimeInvalid();
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
||||
* 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 "net_conn_callback_observer.h"
|
||||
#include "ntp_update_time.h"
|
||||
#include "time_common.h"
|
||||
|
||||
using namespace OHOS::NetManagerStandard;
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
int32_t NetConnCallbackObserver::NetAvailable(sptr<NetHandle> &netHandle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
|
||||
const sptr<NetAllCapabilities> &netAllCap)
|
||||
{
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Observe net capabilities change. start");
|
||||
if (netAllCap->netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Net Capability Internet");
|
||||
DelayedSingleton<NtpUpdateTime>::GetInstance()->SetSystemTime();
|
||||
}
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Observe net capabilities change. end");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
|
||||
const sptr<NetLinkInfo> &info)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t NetConnCallbackObserver::NetLost(sptr<NetHandle> &netHandle)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t NetConnCallbackObserver::NetUnavailable()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace MiscServices
|
||||
} // namespace OHOS
|
||||
@@ -26,24 +26,28 @@
|
||||
#include "time_service.h"
|
||||
#include "nitz_subscriber.h"
|
||||
#include "time_zone_info.h"
|
||||
#include "net_conn_callback_observer.h"
|
||||
#include "net_specifier.h"
|
||||
#include "net_conn_client.h"
|
||||
#include "ntp_update_time.h"
|
||||
|
||||
using namespace std::chrono;
|
||||
using namespace OHOS::NetManagerStandard;
|
||||
|
||||
namespace OHOS {
|
||||
namespace MiscServices {
|
||||
namespace {
|
||||
constexpr uint64_t NANO_TO_MILLISECOND = 1000000;
|
||||
constexpr uint64_t DAY_TO_MILLISECOND = 86400000;
|
||||
constexpr uint64_t MINUTES_TO_SECOND = 20;
|
||||
const std::string AUTOTIME_FILE_PATH = "/data/misc/zoneinfo/autotime.json";
|
||||
const std::string NETWORK_TIME_STATUS_ON = "ON";
|
||||
const std::string NETWORK_TIME_STATUS_OFF = "OFF";
|
||||
const std::string NTP_CN_SERVER = "ntp.aliyun.com";
|
||||
const int64_t INVALID_TIMES = -1;
|
||||
const int MAX_RETRY = 30;
|
||||
|
||||
}
|
||||
|
||||
NtpUpdateTime::NtpUpdateTime() {};
|
||||
NtpUpdateTime::NtpUpdateTime() : nitzUpdateTimeMili_(0) {};
|
||||
NtpUpdateTime::~NtpUpdateTime() {};
|
||||
|
||||
void NtpUpdateTime::Init()
|
||||
@@ -63,19 +67,15 @@ void NtpUpdateTime::Init()
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (autoTimeInfo_.status == NETWORK_TIME_STATUS_ON) {
|
||||
std::thread([this] {
|
||||
for (int i = 0; i < MAX_RETRY; i++) {
|
||||
if (!this->ThreadSetSystemTime()) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "thread set ntp time failed, retry");
|
||||
std::this_thread::sleep_for(seconds(MINUTES_TO_SECOND));
|
||||
} else {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "thread set ntp time success");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}).detach();
|
||||
}
|
||||
// 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);
|
||||
|
||||
int32_t timerType = ITimerManager::TimerType::ELAPSED_REALTIME;
|
||||
auto callback = [this](uint64_t id) {
|
||||
@@ -110,10 +110,7 @@ void NtpUpdateTime::RefreshNetworkTimeByTimer(const uint64_t timerId)
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Network time status off.");
|
||||
return;
|
||||
}
|
||||
if (IsNITZTimeInvalid()) {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "NITZ Time is valid.");
|
||||
return;
|
||||
}
|
||||
|
||||
SetSystemTime();
|
||||
SaveAutoTimeInfoToFile(autoTimeInfo_);
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp update triggertime: %{public}" PRId64 "", nextTriggerTime_);
|
||||
@@ -130,6 +127,10 @@ void NtpUpdateTime::UpdateNITZSetTime()
|
||||
void NtpUpdateTime::SetSystemTime()
|
||||
{
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
|
||||
if (IsNITZTimeInvalid()) {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "NITZ Time is valid.");
|
||||
return;
|
||||
}
|
||||
if (!DelayedSingleton<NtpTrustedTime>::GetInstance()->ForceRefresh(autoTimeInfo_.NTP_SERVER)) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "get ntp time failed.");
|
||||
return;
|
||||
@@ -147,37 +148,11 @@ void NtpUpdateTime::SetSystemTime()
|
||||
auto timeOffsetMs = DelayedSingleton<TimeZoneInfo>::GetInstance()->GetCurrentOffsetMs();
|
||||
currentTime = currentTime + timeOffsetMs;
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp UTC+TIMEZONE tTime: %{public}" PRId64 "", currentTime);
|
||||
TimeService::GetInstance()->SetTime(currentTime);
|
||||
autoTimeInfo_.lastUpdateTime = currentTime;
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp update currentTime: %{public}" PRId64 "", currentTime);
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
|
||||
}
|
||||
|
||||
bool NtpUpdateTime::ThreadSetSystemTime()
|
||||
{
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
|
||||
if (!DelayedSingleton<NtpTrustedTime>::GetInstance()->ForceRefresh(autoTimeInfo_.NTP_SERVER)) {
|
||||
TIME_HILOGE(TIME_MODULE_SERVICE, "get ntp time failed.");
|
||||
return false;
|
||||
}
|
||||
int64_t currentTime = DelayedSingleton<NtpTrustedTime>::GetInstance()->CurrentTimeMillis();
|
||||
if (currentTime == INVALID_TIMES) {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp update time failed");
|
||||
return false;
|
||||
}
|
||||
if (currentTime <= 0) {
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "current time invalid.");
|
||||
return false;
|
||||
}
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp UTC Time: %{public}" PRId64 "", currentTime);
|
||||
auto timeOffsetMs = DelayedSingleton<TimeZoneInfo>::GetInstance()->GetCurrentOffsetMs();
|
||||
currentTime = currentTime + timeOffsetMs;
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp UTC+TIMEZONE tTime: %{public}" PRId64 "", currentTime);
|
||||
TimeService::GetInstance()->SetTime(currentTime);
|
||||
autoTimeInfo_.lastUpdateTime = currentTime;
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Ntp update currentTime: %{public}" PRId64 "", currentTime);
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
|
||||
return true;
|
||||
}
|
||||
|
||||
void NtpUpdateTime::RefreshNextTriggerTime()
|
||||
@@ -221,6 +196,9 @@ bool NtpUpdateTime::CheckStatus()
|
||||
|
||||
bool NtpUpdateTime::IsNITZTimeInvalid()
|
||||
{
|
||||
if (nitzUpdateTimeMili_ == 0 ) {
|
||||
return false;
|
||||
}
|
||||
auto BootTimeNano = steady_clock::now().time_since_epoch().count();
|
||||
auto BootTimeMilli = BootTimeNano / NANO_TO_MILLISECOND;
|
||||
return (BootTimeMilli - nitzUpdateTimeMili_) < DAY_TO_MILLISECOND;
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace {
|
||||
constexpr int INDEX_TWO = 2;
|
||||
constexpr int INDEX_THREE = 3;
|
||||
constexpr int INDEX_FOUR = 4;
|
||||
constexpr int TIME_OUT = 10;
|
||||
constexpr int TIME_OUT = 5;
|
||||
constexpr unsigned char MODE_THREE = 3;
|
||||
constexpr unsigned char VERSION_THREE = 3;
|
||||
constexpr double TEN_TO_MINUS_SIX_POWER = 1.0e-6;
|
||||
|
||||
@@ -179,9 +179,7 @@ void TimerManager::SetHandler(uint64_t id,
|
||||
} else if (intervalDuration > MAX_INTERVAL) {
|
||||
intervalDuration = MAX_INTERVAL;
|
||||
}
|
||||
if (triggerAtTime < 0) {
|
||||
triggerAtTime = 0;
|
||||
}
|
||||
|
||||
auto nowElapsed = steady_clock::now();
|
||||
auto nominalTrigger = ConvertToElapsed(milliseconds(triggerAtTime), type);
|
||||
auto minTrigger = (IsSystemUid(uid)) ? (nowElapsed + ZERO_FUTURITY) : (nowElapsed + MIN_FUTURITY);
|
||||
|
||||
Reference in New Issue
Block a user