From 848f2c701127248f93c57bbfe97953239585aaa2 Mon Sep 17 00:00:00 2001 From: chenming Date: Wed, 9 Mar 2022 20:11:51 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E9=80=82=E9=85=8D=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=8F=98=E5=8C=96=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenming --- bundle.json | 3 +- services/BUILD.gn | 1 + .../src/conditions/network_listener.cpp | 54 ++++++++++++++++--- services/native/src/work_status.cpp | 8 +++ utils/dump/BUILD.gn | 1 + utils/dump/src/event_publisher.cpp | 25 ++++++--- 6 files changed, 78 insertions(+), 14 deletions(-) diff --git a/bundle.json b/bundle.json index af78dc0..be9fac3 100644 --- a/bundle.json +++ b/bundle.json @@ -36,7 +36,8 @@ "ces_standard", "ability_runtime", "eventhandler", - "thermal_manager" + "thermal_manager", + "netmanager_base" ], "third_party": [ "googletest" ] }, diff --git a/services/BUILD.gn b/services/BUILD.gn index a227a98..4d273c1 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -69,6 +69,7 @@ ohos_shared_library("workschedservice") { "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "netmanager_base:net_conn_manager_if", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", "thermal_manager:thermalsrv_client", diff --git a/services/native/src/conditions/network_listener.cpp b/services/native/src/conditions/network_listener.cpp index 401a070..3a3503d 100644 --- a/services/native/src/conditions/network_listener.cpp +++ b/services/native/src/conditions/network_listener.cpp @@ -18,9 +18,17 @@ #include "common_event_support.h" #include "matching_skills.h" #include "want.h" +#include "net_supplier_info.h" namespace OHOS { namespace WorkScheduler { +const int DEFAULT_VALUE = -1; +const int BEARER_CELLULAR = 0; +const int BEARER_WIFI = 1; +const int BEARER_BLUETOOTH = 2; +const int BEARER_ETHERNET = 3; +const int BEARER_WIFI_AWARE = 5; + NetworkEventSubscriber::NetworkEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, NetworkListener &listener) : CommonEventSubscriber(subscribeInfo), listener_(listener) {} @@ -29,19 +37,51 @@ void NetworkEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &dat const std::string action = data.GetWant().GetAction(); WS_HILOGI("OnReceiveEvent get action: %{public}s", action.c_str()); - if (action == EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE) { - WS_HILOGI("Condition changed: WIFI_CONN_STATE"); - listener_.OnConditionChanged(WorkCondition::Type::NETWORK, - std::make_shared(WorkCondition::NETWORK_TYPE_WIFI, 0, 0, std::string())); - } else { - WS_HILOGI("OnReceiveEvent action is invalid"); + if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) { + int code = data.GetCode(); + int netType = data.GetWant().GetIntParam("NetType", DEFAULT_VALUE); + WS_HILOGI("Condition changed code:%{public}d, netType:%{public}d", code, netType); + if (code == NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED) { + if (netType == DEFAULT_VALUE) { + return; + } + switch (netType) { + case BEARER_CELLULAR: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_MOBILE, 0, 0, std::string())); + break; + case BEARER_WIFI: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_WIFI, 0, 0, std::string())); + break; + case BEARER_BLUETOOTH: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_BLUETOOTH, 0, 0, std::string())); + break; + case BEARER_ETHERNET: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_ETHERNET, 0, 0, std::string())); + break; + case BEARER_WIFI_AWARE: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_WIFI_P2P, 0, 0, std::string())); + break; + default: + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_TYPE_ANY, 0, 0, std::string())); + break; + } + } else { + listener_.OnConditionChanged(WorkCondition::Type::NETWORK, + std::make_shared(WorkCondition::NETWORK_UNKNOWN, 0, 0, std::string())); + } } } std::shared_ptr CreateNetworkEventSubscriber(NetworkListener &listener) { EventFwk::MatchingSkills skill = EventFwk::MatchingSkills(); - skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE); + skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE); EventFwk::CommonEventSubscribeInfo info(skill); return std::make_shared(info, listener); } diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index 045a89f..5404835 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -141,6 +141,14 @@ bool WorkStatus::IsReady() } switch (it.first) { case WorkCondition::Type::NETWORK: + if (conditionMap_.at(it.first)->enumVal == WorkCondition::Network::NETWORK_UNKNOWN) { + return false; + } + if (workConditionMap->at(it.first)->enumVal != WorkCondition::Network::NETWORK_TYPE_ANY && + workConditionMap->at(it.first)->enumVal != conditionMap_.at(it.first)->enumVal) { + return false; + } + break; case WorkCondition::Type::BATTERY_STATUS: case WorkCondition::Type::STORAGE: { if (workConditionMap->at(it.first)->enumVal != conditionMap_.at(it.first)->enumVal) { diff --git a/utils/dump/BUILD.gn b/utils/dump/BUILD.gn index daba3d0..8a8ea83 100644 --- a/utils/dump/BUILD.gn +++ b/utils/dump/BUILD.gn @@ -44,6 +44,7 @@ ohos_executable("workscheduler") { "ces_standard:cesfwk_innerkits", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", + "netmanager_base:net_conn_manager_if", "samgr_standard:samgr_proxy", "utils_base:utils", ] diff --git a/utils/dump/src/event_publisher.cpp b/utils/dump/src/event_publisher.cpp index b7d61ec..31764cb 100644 --- a/utils/dump/src/event_publisher.cpp +++ b/utils/dump/src/event_publisher.cpp @@ -22,6 +22,7 @@ #include "common_event_manager.h" #include "common_event_support.h" #include "want.h" +#include "net_supplier_info.h" #include "work_sched_common.h" @@ -32,6 +33,7 @@ namespace { static const int DETAIL_PARAM = 2; static const std::string NETWORK = "network"; static const std::string EV_NETWORK_TYPE_WIFI = "wifi"; + static const std::string EV_NETWORK_TYPE_DISCONNECT = "disconnect"; static const std::string CHARGING = "charging"; static const std::string EV_CHARGING_TYPE_USB = "usb"; static const std::string EV_CHARGING_TYPE_AC = "ac"; @@ -47,7 +49,8 @@ namespace { "usage: workscheduler dump -E []\n" "options list:\n" " help help menu\n" - " network wifi (TMP)publish COMMON_EVENT_WIFI_CONN_STATE event\n" + " network wifi (TMP)publish COMMON_EVENT_CONNECTIVITY_CHANGE event\n" + " network disconnect (TMP)publish COMMON_EVENT_CONNECTIVITY_CHANGE event\n" " charging usb publish usb charging event\n" " charging ac publish ac charging event\n" " charging wireless publish wireless charging event\n" @@ -83,16 +86,26 @@ void EventPublisher::PublishNetworkEvent(const std::vector &dumpOpt { EventFwk::Want want; if (dumpOption[DETAIL_PARAM] == EV_NETWORK_TYPE_WIFI) { - want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE); + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE); + want.SetParam("NetType", 1); dumpInfo.push_back(std::string("publishing COMMON_EVENT_WIFI_CONN_STATE")); + EventFwk::CommonEventData data; + data.SetWant(want); + data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED); + bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data); + dumpInfo.push_back(std::string("publish result: " + std::to_string(isSuccess))); + } else if(dumpOption[DETAIL_PARAM] == EV_NETWORK_TYPE_DISCONNECT) { + want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE); + dumpInfo.push_back(std::string("publishing COMMON_EVENT_WIFI_CONN_STATE")); + EventFwk::CommonEventData data; + data.SetWant(want); + data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_DISCONNECTED); + bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data); + dumpInfo.push_back(std::string("publish result: " + std::to_string(isSuccess))); } else { dumpInfo.push_back(std::string("dump need right param.")); return; } - EventFwk::CommonEventData data; - data.SetWant(want); - bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data); - dumpInfo.push_back(std::string("publish result: " + std::to_string(isSuccess))); } void EventPublisher::PublishChargingEvent(const std::vector &dumpOption, -- Gitee From 8920f1ee51f7c7ee875be6d029e6b28a599ded4b Mon Sep 17 00:00:00 2001 From: chenming Date: Wed, 9 Mar 2022 20:19:56 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=8A=B6=E6=80=81=E5=8F=98=E5=8C=96?= =?UTF-8?q?=E6=9B=B4=E6=96=B0bugfix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenming --- services/native/src/work_status.cpp | 9 ++++++++- utils/dump/src/event_publisher.cpp | 3 +-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index 5404835..0c85d01 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -150,8 +150,15 @@ bool WorkStatus::IsReady() } break; case WorkCondition::Type::BATTERY_STATUS: + int batteryReq = workConditionMap->at(it.first)->enumVal; + if (batteryReq != WorkCondition::BatteryStatus::BATTERY_STATUS_LOW_OR_OKAY && + batteryReq != conditionMap_.at(it.first)->enumVal) { + return false; + } + break; case WorkCondition::Type::STORAGE: { - if (workConditionMap->at(it.first)->enumVal != conditionMap_.at(it.first)->enumVal) { + if (workConditionMap->at(it.first)->enumVal != WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY && + workConditionMap->at(it.first)->enumVal != conditionMap_.at(it.first)->enumVal) { return false; } break; diff --git a/utils/dump/src/event_publisher.cpp b/utils/dump/src/event_publisher.cpp index 31764cb..9159802 100644 --- a/utils/dump/src/event_publisher.cpp +++ b/utils/dump/src/event_publisher.cpp @@ -21,9 +21,8 @@ #include "battery_srv_client.h" #include "common_event_manager.h" #include "common_event_support.h" -#include "want.h" #include "net_supplier_info.h" - +#include "want.h" #include "work_sched_common.h" namespace OHOS { -- Gitee From dfc5c4e00fffe7123884199844623e748edb09a8 Mon Sep 17 00:00:00 2001 From: chenming Date: Wed, 9 Mar 2022 20:26:04 +0800 Subject: [PATCH 3/4] =?UTF-8?q?bugfix=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenming --- services/native/src/work_status.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index 0c85d01..fc98e64 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -140,7 +140,7 @@ bool WorkStatus::IsReady() return false; } switch (it.first) { - case WorkCondition::Type::NETWORK: + case WorkCondition::Type::NETWORK: { if (conditionMap_.at(it.first)->enumVal == WorkCondition::Network::NETWORK_UNKNOWN) { return false; } @@ -149,13 +149,15 @@ bool WorkStatus::IsReady() return false; } break; - case WorkCondition::Type::BATTERY_STATUS: + } + case WorkCondition::Type::BATTERY_STATUS: { int batteryReq = workConditionMap->at(it.first)->enumVal; if (batteryReq != WorkCondition::BatteryStatus::BATTERY_STATUS_LOW_OR_OKAY && batteryReq != conditionMap_.at(it.first)->enumVal) { return false; } break; + } case WorkCondition::Type::STORAGE: { if (workConditionMap->at(it.first)->enumVal != WorkCondition::Storage::STORAGE_LEVEL_LOW_OR_OKAY && workConditionMap->at(it.first)->enumVal != conditionMap_.at(it.first)->enumVal) { -- Gitee From 56e2d59aa95e67d8ea1e5ce8dbb06e48bc2bee0b Mon Sep 17 00:00:00 2001 From: chenming Date: Thu, 10 Mar 2022 09:46:30 +0800 Subject: [PATCH 4/4] =?UTF-8?q?codestyle=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenming --- utils/dump/src/event_publisher.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/dump/src/event_publisher.cpp b/utils/dump/src/event_publisher.cpp index 9159802..1e1069b 100644 --- a/utils/dump/src/event_publisher.cpp +++ b/utils/dump/src/event_publisher.cpp @@ -93,7 +93,7 @@ void EventPublisher::PublishNetworkEvent(const std::vector &dumpOpt data.SetCode(NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED); bool isSuccess = EventFwk::CommonEventManager::PublishCommonEvent(data); dumpInfo.push_back(std::string("publish result: " + std::to_string(isSuccess))); - } else if(dumpOption[DETAIL_PARAM] == EV_NETWORK_TYPE_DISCONNECT) { + } else if (dumpOption[DETAIL_PARAM] == EV_NETWORK_TYPE_DISCONNECT) { want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE); dumpInfo.push_back(std::string("publishing COMMON_EVENT_WIFI_CONN_STATE")); EventFwk::CommonEventData data; -- Gitee