From d24eba4cc42dabef0a8b573318cf7d52e3aa875f Mon Sep 17 00:00:00 2001 From: liulinna Date: Sat, 26 Mar 2022 01:29:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guduhanyan --- .../js/napi/system_time/src/js_systemtime.cpp | 12 ++-- .../js/napi/system_timer/src/system_timer.cpp | 12 ++-- services/BUILD.gn | 2 + .../include/net_conn_callback_observer.h | 40 +++++++++++ .../time_manager/include/ntp_update_time.h | 4 +- .../src/net_conn_callback_observer.cpp | 63 +++++++++++++++++ services/time_manager/src/ntp_update_time.cpp | 70 +++++++------------ services/time_manager/src/sntp_client.cpp | 2 +- services/timer/src/timer_manager.cpp | 4 +- 9 files changed, 149 insertions(+), 60 deletions(-) create mode 100644 services/time_manager/include/net_conn_callback_observer.h create mode 100644 services/time_manager/src/net_conn_callback_observer.cpp diff --git a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp index 05e1077..d31c0fa 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -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; } } diff --git a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp index 120f314..f2e73b6 100644 --- a/interfaces/kits/js/napi/system_timer/src/system_timer.cpp +++ b/interfaces/kits/js/napi/system_timer/src/system_timer.cpp @@ -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); } } diff --git a/services/BUILD.gn b/services/BUILD.gn index 83ab20a..e5476d1 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -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", ] diff --git a/services/time_manager/include/net_conn_callback_observer.h b/services/time_manager/include/net_conn_callback_observer.h new file mode 100644 index 0000000..00d6b8a --- /dev/null +++ b/services/time_manager/include/net_conn_callback_observer.h @@ -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 &netHandle) override; + + int32_t NetCapabilitiesChange(sptr &netHandle, const sptr &netAllCap) override; + + int32_t NetConnectionPropertiesChange(sptr &netHandle, const sptr &info) override; + + int32_t NetLost(sptr &netHandle) override; + + int32_t NetUnavailable() override; + + int32_t NetBlockStatusChange(sptr &netHandle, bool blocked) override; +}; +} // namespace MiscServices +} // namespace OHOS +#endif /* NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H */ diff --git a/services/time_manager/include/ntp_update_time.h b/services/time_manager/include/ntp_update_time.h index 47f5aa7..4b025fe 100644 --- a/services/time_manager/include/ntp_update_time.h +++ b/services/time_manager/include/ntp_update_time.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(); diff --git a/services/time_manager/src/net_conn_callback_observer.cpp b/services/time_manager/src/net_conn_callback_observer.cpp new file mode 100644 index 0000000..5196cdc --- /dev/null +++ b/services/time_manager/src/net_conn_callback_observer.cpp @@ -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) +{ + return 0; +} + +int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr &netHandle, + const sptr &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::GetInstance()->SetSystemTime(); + } + TIME_HILOGD(TIME_MODULE_SERVICE, "Observe net capabilities change. end"); + return 0; +} + +int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr &netHandle, + const sptr &info) +{ + return 0; +} + +int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) +{ + return 0; +} + +int32_t NetConnCallbackObserver::NetUnavailable() +{ + return 0; +} + +int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr &netHandle, bool blocked) +{ + return 0; +} + +} // namespace MiscServices +} // namespace OHOS diff --git a/services/time_manager/src/ntp_update_time.cpp b/services/time_manager/src/ntp_update_time.cpp index f643c11..ead2e63 100644 --- a/services/time_manager/src/ntp_update_time.cpp +++ b/services/time_manager/src/ntp_update_time.cpp @@ -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 specifier = new NetSpecifier(netSpecifier); + sptr observer = new NetConnCallbackObserver; + DelayedSingleton::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::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::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::GetInstance()->ForceRefresh(autoTimeInfo_.NTP_SERVER)) { - TIME_HILOGE(TIME_MODULE_SERVICE, "get ntp time failed."); - return false; - } - int64_t currentTime = DelayedSingleton::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::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; diff --git a/services/time_manager/src/sntp_client.cpp b/services/time_manager/src/sntp_client.cpp index 9d2ca85..7cca7fa 100644 --- a/services/time_manager/src/sntp_client.cpp +++ b/services/time_manager/src/sntp_client.cpp @@ -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; diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 7bcb98b..871e3cb 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -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);