From 580daf9e6ddecdd0d43936908402fba21faa8a55 Mon Sep 17 00:00:00 2001 From: li_junsong Date: Tue, 10 Dec 2024 16:05:55 +0800 Subject: [PATCH] =?UTF-8?q?cherry=20pick=200e5873e=20from=20https://gitee.?= =?UTF-8?q?com/li=5Fjunsong/time=5Ftime=5Fservice/pulls/855=20=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E5=85=AC=E5=85=B1=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li_junsong --- bundle.json | 1 - services/BUILD.gn | 6 +- ...lback_observer.h => net_conn_subscriber.h} | 51 +++++++++------ services/time/include/ntp_update_time.h | 1 - ...k_observer.cpp => net_conn_subscriber.cpp} | 64 +++++++++---------- services/time/src/nitz_subscriber.cpp | 1 - services/time/src/ntp_update_time.cpp | 45 ------------- services/time/src/power_subscriber.cpp | 1 - services/time_system_ability.cpp | 27 ++++---- services/time_system_ability.h | 1 + 10 files changed, 77 insertions(+), 121 deletions(-) rename services/time/include/{net_conn_callback_observer.h => net_conn_subscriber.h} (39%) rename services/time/src/{net_conn_callback_observer.cpp => net_conn_subscriber.cpp} (35%) diff --git a/bundle.json b/bundle.json index 71f43cfa..727df815 100644 --- a/bundle.json +++ b/bundle.json @@ -32,7 +32,6 @@ "common_event_service", "os_account", "ipc", - "netmanager_base", "ability_base", "ability_runtime", "relational_store", diff --git a/services/BUILD.gn b/services/BUILD.gn index 9c7a4113..e2b0d9e8 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -45,7 +45,7 @@ ohos_shared_library("time_system_ability") { "ipc/stub/time_service_stub.cpp", "ipc/stub/timer_notify_callback_stub.cpp", "time/src/itimer_info.cpp", - "time/src/net_conn_callback_observer.cpp", + "time/src/net_conn_subscriber.cpp", "time/src/nitz_subscriber.cpp", "time/src/ntp_trusted_time.cpp", "time/src/ntp_update_time.cpp", @@ -79,7 +79,6 @@ ohos_shared_library("time_system_ability") { "init:libbeget_proxy", "init:libbegetutil", "ipc:ipc_single", - "netmanager_base:net_conn_manager_if", "os_account:os_account_innerkits", "relational_store:native_dataability", "relational_store:native_rdb", @@ -123,7 +122,7 @@ ohos_static_library("time_system_ability_static") { "ipc/stub/time_service_stub.cpp", "ipc/stub/timer_notify_callback_stub.cpp", "time/src/itimer_info.cpp", - "time/src/net_conn_callback_observer.cpp", + "time/src/net_conn_subscriber.cpp", "time/src/nitz_subscriber.cpp", "time/src/ntp_trusted_time.cpp", "time/src/ntp_update_time.cpp", @@ -156,7 +155,6 @@ ohos_static_library("time_system_ability_static") { "hisysevent:libhisysevent", "init:libbeget_proxy", "ipc:ipc_single", - "netmanager_base:net_conn_manager_if", "os_account:os_account_innerkits", "relational_store:native_dataability", "relational_store:native_rdb", diff --git a/services/time/include/net_conn_callback_observer.h b/services/time/include/net_conn_subscriber.h similarity index 39% rename from services/time/include/net_conn_callback_observer.h rename to services/time/include/net_conn_subscriber.h index 27529dbc..7ea1d7e1 100644 --- a/services/time/include/net_conn_callback_observer.h +++ b/services/time/include/net_conn_subscriber.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2024 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 @@ -13,30 +13,41 @@ * limitations under the License. */ -#ifndef NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H -#define NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H +#ifndef NET_CONN_SUBSCRIBER_H +#define NET_CONN_SUBSCRIBER_H -#include "net_all_capabilities.h" -#include "net_conn_callback_stub.h" +#include +#include +#include +#include + +#include "common_event.h" +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_support.h" namespace OHOS { namespace MiscServices { -class NetConnCallbackObserver : public NetManagerStandard::NetConnCallbackStub { +using namespace OHOS::EventFwk; +class NetConnSubscriber : public CommonEventSubscriber { 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; + explicit NetConnSubscriber(const CommonEventSubscribeInfo &subscriberInfo); + ~NetConnSubscriber() = default; + virtual void OnReceiveEvent(const CommonEventData &data); + +private: + enum NetConnectivityChangeEventType { + UNKNOWN_BROADCAST_EVENT = 1, + CONNECTING, + CONNECTED, + DISCONNECTING, + DISCONNECTED + }; + using netConnectivityChangeFunc = std::function; + + void NetConnStateConnected(const CommonEventData &data); + std::map memberFuncMap_; }; } // namespace MiscServices } // namespace OHOS -#endif /* NETMANAGER_BASE_NET_CONN_CALLBACK_OBSERVER_H */ +#endif // NET_CONN_SUBSCRIBER_H \ No newline at end of file diff --git a/services/time/include/ntp_update_time.h b/services/time/include/ntp_update_time.h index cf4aa221..8d9fc8ce 100644 --- a/services/time/include/ntp_update_time.h +++ b/services/time/include/ntp_update_time.h @@ -39,7 +39,6 @@ public: void UpdateNITZSetTime(); void Stop(); void Init(); - int32_t MonitorNetwork(); bool IsValidNITZTime(); uint64_t GetNITZUpdateTime(); diff --git a/services/time/src/net_conn_callback_observer.cpp b/services/time/src/net_conn_subscriber.cpp similarity index 35% rename from services/time/src/net_conn_callback_observer.cpp rename to services/time/src/net_conn_subscriber.cpp index 04006e01..2441ca46 100644 --- a/services/time/src/net_conn_callback_observer.cpp +++ b/services/time/src/net_conn_subscriber.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2024 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 @@ -13,54 +13,48 @@ * limitations under the License. */ -#include "net_conn_callback_observer.h" - +#include "net_conn_subscriber.h" #include "ntp_update_time.h" #include "time_common.h" - -using namespace OHOS::NetManagerStandard; +#include "time_tick_notify.h" namespace OHOS { namespace MiscServices { -int32_t NetConnCallbackObserver::NetAvailable(sptr &netHandle) +using namespace OHOS::EventFwk; +using namespace OHOS::AAFwk; +NetConnSubscriber::NetConnSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo) + : CommonEventSubscriber(subscriberInfo) { - return E_TIME_OK; + memberFuncMap_ = { + { CONNECTED, [this] (const CommonEventData &data) { NetConnStateConnected(data); } }, + }; } -int32_t NetConnCallbackObserver::NetCapabilitiesChange(sptr &netHandle, - const sptr &netAllCap) +void NetConnSubscriber::OnReceiveEvent(const CommonEventData &data) { - TIME_HILOGD(TIME_MODULE_SERVICE, "Observe net capabilities change. start"); - if (netAllCap->netCaps_.count(NetCap::NET_CAPABILITY_INTERNET)) { - if (NtpUpdateTime::GetInstance().IsValidNITZTime()) { - TIME_HILOGW(TIME_MODULE_SERVICE, "NITZ Time is valid."); - return E_TIME_DEAL_FAILED; + uint32_t code = UNKNOWN_BROADCAST_EVENT; + std::string action = data.GetWant().GetAction(); + TIME_HILOGD(TIME_MODULE_SERVICE, "receive one broadcast:%{public}s", action.c_str()); + if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) { + code = data.GetCode(); + } + auto itFunc = memberFuncMap_.find(code); + if (itFunc != memberFuncMap_.end()) { + auto memberFunc = itFunc->second; + if (memberFunc != nullptr) { + return memberFunc(data); } - TIME_HILOGI(TIME_MODULE_SERVICE, "internet ready"); - NtpUpdateTime::SetSystemTime(); } - return E_TIME_OK; } -int32_t NetConnCallbackObserver::NetConnectionPropertiesChange(sptr &netHandle, - const sptr &info) +void NetConnSubscriber::NetConnStateConnected(const CommonEventData &data) { - return E_TIME_OK; -} - -int32_t NetConnCallbackObserver::NetLost(sptr &netHandle) -{ - return E_TIME_OK; -} - -int32_t NetConnCallbackObserver::NetUnavailable() -{ - return E_TIME_OK; -} - -int32_t NetConnCallbackObserver::NetBlockStatusChange(sptr &netHandle, bool blocked) -{ - return E_TIME_OK; + if (NtpUpdateTime::GetInstance().IsValidNITZTime()) { + TIME_HILOGW(TIME_MODULE_SERVICE, "NITZ Time is valid."); + return; + } + TIME_HILOGI(TIME_MODULE_SERVICE, "Internet ready"); + NtpUpdateTime::SetSystemTime(); } } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/time/src/nitz_subscriber.cpp b/services/time/src/nitz_subscriber.cpp index 05d913a1..84b2a957 100644 --- a/services/time/src/nitz_subscriber.cpp +++ b/services/time/src/nitz_subscriber.cpp @@ -35,7 +35,6 @@ NITZSubscriber::NITZSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &s void NITZSubscriber::OnReceiveEvent(const CommonEventData &data) { uint32_t code = UNKNOWN_BROADCAST_EVENT; - OHOS::EventFwk::Want want = data.GetWant(); std::string action = data.GetWant().GetAction(); TIME_HILOGD(TIME_MODULE_SERVICE, "receive one broadcast:%{public}s", action.c_str()); diff --git a/services/time/src/ntp_update_time.cpp b/services/time/src/ntp_update_time.cpp index 5ab56e8a..5245abdb 100644 --- a/services/time/src/ntp_update_time.cpp +++ b/services/time/src/ntp_update_time.cpp @@ -22,16 +22,12 @@ #include #include "init_param.h" -#include "net_conn_callback_observer.h" -#include "net_conn_client.h" -#include "net_specifier.h" #include "ntp_trusted_time.h" #include "parameters.h" #include "time_common.h" #include "time_system_ability.h" using namespace std::chrono; -using namespace OHOS::NetManagerStandard; namespace OHOS { namespace MiscServices { @@ -46,8 +42,6 @@ const std::string AUTO_TIME_STATUS_ON = "ON"; const std::string AUTO_TIME_STATUS_OFF = "OFF"; constexpr uint64_t TWO_SECONDS = 2000; constexpr uint64_t ONE_HOUR = 3600000; -constexpr int32_t RETRY_TIMES = 3; -constexpr uint32_t RETRY_INTERVAL = 1; const std::string DEFAULT_NTP_SERVER = "1.cn.pool.ntp.org"; } // namespace @@ -92,45 +86,6 @@ void NtpUpdateTime::Init() 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 (std::nothrow) NetSpecifier(netSpecifier); - if (specifier == nullptr) { - TIME_HILOGE(TIME_MODULE_SERVICE, "new operator error.specifier is nullptr"); - return NET_CONN_ERR_INPUT_NULL_PTR; - } - sptr observer = new (std::nothrow) NetConnCallbackObserver(); - if (observer == nullptr) { - TIME_HILOGE(TIME_MODULE_SERVICE, "new operator error.observer is nullptr"); - return NET_CONN_ERR_INPUT_NULL_PTR; - } - int nRet = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, observer, 0); - TIME_HILOGI(TIME_MODULE_SERVICE, "RegisterNetConnCallback retcode= %{public}d", nRet); - - if (nRet == E_TIME_OK) { - return nRet; - } - auto retryRegister = [specifier, observer]() { - for (int i = 0; i < RETRY_TIMES; i++) { - sleep(RETRY_INTERVAL); - int nRet = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, observer, 0); - TIME_HILOGI(TIME_MODULE_SERVICE, "RegisterNetConnCallback retcode= %{public}d", nRet); - if (nRet == E_TIME_OK) { - return; - } - } - }; - std::thread thread(retryRegister); - thread.detach(); - return nRet; -} - void NtpUpdateTime::RefreshNetworkTimeByTimer(uint64_t timerId) { TIME_HILOGI(TIME_MODULE_SERVICE, "The timer is up"); diff --git a/services/time/src/power_subscriber.cpp b/services/time/src/power_subscriber.cpp index 4b293337..3ea20ef2 100644 --- a/services/time/src/power_subscriber.cpp +++ b/services/time/src/power_subscriber.cpp @@ -32,7 +32,6 @@ PowerSubscriber::PowerSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo void PowerSubscriber::OnReceiveEvent(const CommonEventData &data) { uint32_t code = UNKNOWN_BROADCAST_EVENT; - OHOS::EventFwk::Want want = data.GetWant(); std::string action = data.GetWant().GetAction(); TIME_HILOGD(TIME_MODULE_SERVICE, "receive one broadcast:%{public}s", action.c_str()); if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) { diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index 251364df..b2fa5c85 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -43,6 +43,7 @@ #include "common_event_support.h" #include "power_subscriber.h" #include "nitz_subscriber.h" +#include "net_conn_subscriber.h" #include "init_param.h" #include "parameters.h" #include "os_account.h" @@ -226,9 +227,6 @@ void TimeSystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::s case POWER_MANAGER_SERVICE_ID: RegisterPowerStateListener(); break; - case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID: - NtpUpdateTime::GetInstance().MonitorNetwork(); - break; case ABILITY_MGR_SERVICE_ID: AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); RemoveSystemAbilityListener(ABILITY_MGR_SERVICE_ID); @@ -253,11 +251,7 @@ void TimeSystemAbility::RegisterScreenOnSubscriber() CommonEventSubscribeInfo subscriberInfo(matchingSkills); std::shared_ptr subscriberPtr = std::make_shared(subscriberInfo); bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriberPtr); - if (!subscribeResult) { - TIME_HILOGE(TIME_MODULE_SERVICE, "Register COMMON_EVENT_SCREEN_ON failed"); - } else { - TIME_HILOGI(TIME_MODULE_SERVICE, "Register COMMON_EVENT_SCREEN_ON success."); - } + TIME_HILOGI(TIME_MODULE_SERVICE, "Register COMMON_EVENT_SCREEN_ON res:%{public}d", subscribeResult); } void TimeSystemAbility::RegisterNitzTimeSubscriber() @@ -267,11 +261,7 @@ void TimeSystemAbility::RegisterNitzTimeSubscriber() CommonEventSubscribeInfo subscriberNITZInfo(matchingNITZSkills); std::shared_ptr subscriberNITZPtr = std::make_shared(subscriberNITZInfo); bool subscribeNITZResult = CommonEventManager::SubscribeCommonEvent(subscriberNITZPtr); - if (!subscribeNITZResult) { - TIME_HILOGE(TIME_MODULE_SERVICE, "Register COMMON_EVENT_NITZ_TIME_CHANGED failed"); - } else { - TIME_HILOGI(TIME_MODULE_SERVICE, "Register COMMON_EVENT_NITZ_TIME_CHANGED success."); - } + TIME_HILOGI(TIME_MODULE_SERVICE, "Register COMMON_EVENT_NITZ_TIME_CHANGED res:%{public}d", subscribeNITZResult); } void TimeSystemAbility::RegisterPackageRemovedSubscriber() @@ -288,6 +278,16 @@ void TimeSystemAbility::RegisterPackageRemovedSubscriber() TIME_HILOGI(TIME_MODULE_SERVICE, "register res:%{public}d", subscribeResult); } +void TimeSystemAbility::RegisterNetConnSubscriber() +{ + MatchingSkills matchingSkills; + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE); + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + std::shared_ptr subscriberPtr = std::make_shared(subscriberInfo); + bool subscribeResult = CommonEventManager::SubscribeCommonEvent(subscriberPtr); + TIME_HILOGI(TIME_MODULE_SERVICE, "Register COMMON_EVENT_CONNECTIVITY_CHANGE res:%{public}d", subscribeResult); +} + void TimeSystemAbility::RegisterCommonEventSubscriber() { TIME_HILOGD(TIME_MODULE_SERVICE, "RegisterCommonEventSubscriber Started"); @@ -304,6 +304,7 @@ void TimeSystemAbility::RegisterCommonEventSubscriber() RegisterScreenOnSubscriber(); RegisterNitzTimeSubscriber(); RegisterPackageRemovedSubscriber(); + RegisterNetConnSubscriber(); } void TimeSystemAbility::RegisterOsAccountSubscriber() diff --git a/services/time_system_ability.h b/services/time_system_ability.h index 2cd8b293..b8901065 100644 --- a/services/time_system_ability.h +++ b/services/time_system_ability.h @@ -112,6 +112,7 @@ private: void RegisterScreenOnSubscriber(); void RegisterNitzTimeSubscriber(); void RegisterPackageRemovedSubscriber(); + void RegisterNetConnSubscriber(); void RegisterOsAccountSubscriber(); bool IsValidTime(int64_t time); void RecoverTimerInner(std::shared_ptr resultSet, bool autoRestore); -- Gitee