• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "conditions/network_listener.h"
16 
17 #include "common_event_manager.h"
18 #include "common_event_support.h"
19 #include "matching_skills.h"
20 #include "want.h"
21 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
22 #include "net_supplier_info.h"
23 #include "net_conn_client.h"
24 #endif
25 #include "work_sched_hilog.h"
26 
27 using namespace OHOS::NetManagerStandard;
28 
29 namespace OHOS {
30 namespace WorkScheduler {
31 const int32_t DEFAULT_VALUE = -1;
32 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
33 const int32_t BEARER_CELLULAR = 0;
34 const int32_t BEARER_WIFI = 1;
35 const int32_t BEARER_BLUETOOTH = 2;
36 const int32_t BEARER_ETHERNET = 3;
37 const int32_t BEARER_WIFI_AWARE = 5;
38 #endif
39 
NetworkEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,NetworkListener & listener)40 NetworkEventSubscriber::NetworkEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
41     NetworkListener &listener) : CommonEventSubscriber(subscribeInfo), listener_(listener) {}
42 
OnReceiveEvent(const EventFwk::CommonEventData & data)43 void NetworkEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
44 {
45     const std::string action = data.GetWant().GetAction();
46     WS_HILOGD("OnReceiveEvent get action: %{public}s", action.c_str());
47 
48     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
49         int32_t code = data.GetCode();
50         int32_t netType = data.GetWant().GetIntParam("NetType", DEFAULT_VALUE);
51         WS_HILOGI("Condition changed code:%{public}d, netType:%{public}d", code, netType);
52 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
53         if (code == NetManagerStandard::NetConnState::NET_CONN_STATE_CONNECTED) {
54             if (netType == DEFAULT_VALUE) {
55                 return;
56             }
57             switch (netType) {
58                 case BEARER_CELLULAR:
59                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
60                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_MOBILE, 0, 0, std::string()));
61                     break;
62                 case BEARER_WIFI:
63                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
64                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_WIFI, 0, 0, std::string()));
65                     break;
66                 case BEARER_BLUETOOTH:
67                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
68                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_BLUETOOTH, 0, 0, std::string()));
69                     break;
70                 case BEARER_ETHERNET:
71                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
72                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_ETHERNET, 0, 0, std::string()));
73                     break;
74                 case BEARER_WIFI_AWARE:
75                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
76                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_WIFI_P2P, 0, 0, std::string()));
77                     break;
78                 default:
79                     listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
80                         std::make_shared<DetectorValue>(WorkCondition::NETWORK_TYPE_ANY, 0, 0, std::string()));
81                     break;
82             }
83         } else {
84             if (!IsNetworkOK()) {
85                 listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
86                     std::make_shared<DetectorValue>(WorkCondition::NETWORK_UNKNOWN, 0, 0, std::string()));
87             }
88         }
89 #else
90         listener_.OnConditionChanged(WorkCondition::Type::NETWORK,
91             std::make_shared<DetectorValue>(WorkCondition::NETWORK_UNKNOWN, 0, 0, std::string()));
92 #endif
93     }
94 }
95 
IsNetworkOK()96 bool NetworkEventSubscriber::IsNetworkOK()
97 {
98 #ifdef COMMUNICATION_NETMANAGER_BASE_ENABLE
99     NetHandle netHandle;
100     int ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
101     if (ret != NETMANAGER_SUCCESS) {
102         WS_HILOGE("GetDefaultNet failed %{public}d", ret);
103         return false;
104     }
105     NetAllCapabilities netAllCap;
106     ret = NetConnClient::GetInstance().GetNetCapabilities(netHandle, netAllCap);
107     if (ret != NETMANAGER_SUCCESS) {
108         WS_HILOGE("GetNetCapbilities failed, ret = %{public}d", ret);
109         return false;
110     }
111     return netAllCap.netCaps_.count(NetCap::NET_CAPABILITY_INTERNET);
112 #endif
113     return false;
114 }
115 
CreateNetworkEventSubscriber(NetworkListener & listener)116 std::shared_ptr<EventFwk::CommonEventSubscriber> CreateNetworkEventSubscriber(NetworkListener &listener)
117 {
118     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
119     skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
120     EventFwk::CommonEventSubscribeInfo info(skill);
121     return std::make_shared<NetworkEventSubscriber>(info, listener);
122 }
123 
NetworkListener(std::shared_ptr<WorkQueueManager> workQueueManager)124 NetworkListener::NetworkListener(std::shared_ptr<WorkQueueManager> workQueueManager)
125 {
126     workQueueManager_ = workQueueManager;
127 }
128 
~NetworkListener()129 NetworkListener::~NetworkListener()
130 {
131     this->Stop();
132 }
133 
Start()134 bool NetworkListener::Start()
135 {
136     WS_HILOGI("NetworkListener start");
137     this->commonEventSubscriber = CreateNetworkEventSubscriber(*this);
138     return EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
139 }
140 
Stop()141 bool NetworkListener::Stop()
142 {
143     WS_HILOGI("NetworkListener stop");
144     if (this->commonEventSubscriber != nullptr) {
145         bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
146         if (result) {
147             this->commonEventSubscriber = nullptr;
148         }
149         return result;
150     }
151     return true;
152 }
153 
OnConditionChanged(WorkCondition::Type conditionType,std::shared_ptr<DetectorValue> conditionVal)154 void NetworkListener::OnConditionChanged(WorkCondition::Type conditionType,
155     std::shared_ptr<DetectorValue> conditionVal)
156 {
157     if (workQueueManager_ != nullptr) {
158         workQueueManager_->OnConditionChanged(conditionType, conditionVal);
159     } else {
160         WS_HILOGE("workQueueManager_ is nullptr.");
161     }
162 }
163 } // namespace WorkScheduler
164 } // namespace OHOS