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
16 #include "device_state_observer.h"
17
18 #ifdef ABILITY_BATTERY_SUPPORT
19 #include "battery_srv_client.h"
20 #endif
21 #include "iservice_registry.h"
22 #ifdef ABILITY_NETMANAGER_EXT_SUPPORT
23 #include "networkshare_client.h"
24 #include "networkshare_constants.h"
25 #endif
26 #ifdef ABILITY_POWER_SUPPORT
27 #include "power_mgr_client.h"
28 #include "power_mode_info.h"
29 #endif
30 #include "system_ability_definition.h"
31 #include "telephony_log_wrapper.h"
32
33 namespace OHOS {
34 namespace Telephony {
35 #ifdef ABILITY_POWER_SUPPORT
36 using PowerMode = OHOS::PowerMgr::PowerMode;
37 #endif
38 namespace {
39 const std::string NET_TYPE = "NetType";
40 }
41
StartEventSubscriber(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)42 void DeviceStateObserver::StartEventSubscriber(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler)
43 {
44 MatchingSkills matchingSkills;
45 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
46 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
47 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
48 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
49 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_CHARGING);
50 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_DISCHARGING);
51 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SHUTDOWN);
52 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
53 subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
54 subscriber_ = std::make_shared<DeviceStateEventSubscriber>(subscriberInfo);
55 subscriber_->SetEventHandler(deviceStateHandler);
56 subscriber_->InitEventMap();
57 #ifdef ABILITY_NETMANAGER_EXT_SUPPORT
58 std::unique_lock<ffrt::mutex> lck(callbackMutex_);
59 sharingEventCallback_ = new (std::nothrow) SharingEventCallback(deviceStateHandler);
60
61 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62 statusChangeListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(subscriber_, sharingEventCallback_);
63 if (samgrProxy == nullptr || statusChangeListener_ == nullptr) {
64 TELEPHONY_LOGE("StartEventSubscriber samgrProxy or statusChangeListener_ is nullptr");
65 return;
66 }
67 int32_t commonEventResult = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusChangeListener_);
68 int32_t powerManagerResult = samgrProxy->SubscribeSystemAbility(POWER_MANAGER_SERVICE_ID, statusChangeListener_);
69 int32_t powerManagerBattResult =
70 samgrProxy->SubscribeSystemAbility(POWER_MANAGER_BATT_SERVICE_ID, statusChangeListener_);
71 int32_t netManagerResult =
72 samgrProxy->SubscribeSystemAbility(COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID, statusChangeListener_);
73 TELEPHONY_LOGI(
74 "SubscribeSystemAbility COMMON_EVENT_SERVICE_ID(result:%{public}d) POWER_MANAGER_SERVICE_ID(result:%{public}d) "
75 "POWER_MANAGER_BATT_SERVICE_ID(result:%{public}d) COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID(result:%{public}d)",
76 commonEventResult, powerManagerResult, powerManagerBattResult, netManagerResult);
77 #endif
78 }
79
StopEventSubscriber()80 void DeviceStateObserver::StopEventSubscriber()
81 {
82 if (subscriber_ != nullptr) {
83 bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
84 subscriber_ = nullptr;
85 TELEPHONY_LOGI("DeviceStateObserver::StopEventSubscriber subscribeResult = %{public}d", subscribeResult);
86 }
87
88 #ifdef ABILITY_NETMANAGER_EXT_SUPPORT
89 std::unique_lock<ffrt::mutex> lck(callbackMutex_);
90 if (sharingEventCallback_ == nullptr) {
91 TELEPHONY_LOGE("DeviceStateObserver::StopEventSubscriber sharingEventCallback_ is nullptr");
92 return;
93 }
94 auto networkShareClient = DelayedSingleton<NetManagerStandard::NetworkShareClient>::GetInstance();
95 if (networkShareClient == nullptr) {
96 TELEPHONY_LOGE("DeviceStateObserver::StopEventSubscriber networkShareClient is nullptr");
97 return;
98 }
99 networkShareClient->UnregisterSharingEvent(sharingEventCallback_);
100 sharingEventCallback_ = nullptr;
101 #endif
102 }
103
OnReceiveEvent(const CommonEventData & data)104 void DeviceStateEventSubscriber::OnReceiveEvent(const CommonEventData &data)
105 {
106 if (deviceStateHandler_ == nullptr) {
107 TELEPHONY_LOGE("DeviceStateEventSubscriber::OnReceiveEvent: networkSearchHandler_ is nullptr");
108 return;
109 }
110 std::string action = data.GetWant().GetAction();
111 TELEPHONY_LOGI("DeviceStateEventSubscriber::OnReceiveEvent: action = %{public}s", action.c_str());
112 switch (GetDeviceStateEventIntValue(action)) {
113 case COMMON_EVENT_CONNECTIVITY_CHANGE:
114 ProcessWifiState(data);
115 break;
116 case COMMON_EVENT_SCREEN_ON:
117 deviceStateHandler_->ProcessScreenDisplay(true);
118 break;
119 case COMMON_EVENT_SCREEN_OFF:
120 deviceStateHandler_->ProcessScreenDisplay(false);
121 break;
122 case COMMON_EVENT_POWER_SAVE_MODE_CHANGED:
123 ProcessPowerSaveMode(data);
124 break;
125 case COMMON_EVENT_CHARGING:
126 deviceStateHandler_->ProcessChargingState(true);
127 break;
128 case COMMON_EVENT_DISCHARGING:
129 deviceStateHandler_->ProcessChargingState(false);
130 break;
131 case COMMON_EVENT_SHUTDOWN:
132 deviceStateHandler_->ProcessShutDown();
133 break;
134 default:
135 TELEPHONY_LOGE("DeviceStateEventSubscriber::OnReceiveEvent: invalid event");
136 break;
137 }
138 }
139
ProcessWifiState(const CommonEventData & data)140 void DeviceStateEventSubscriber::ProcessWifiState(const CommonEventData &data)
141 {
142 if (deviceStateHandler_ == nullptr) {
143 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessWifiState networkSearchHandler_ is nullptr");
144 return;
145 }
146 if (data.GetWant().GetIntParam(NET_TYPE, NetBearType::BEARER_DEFAULT) == NetBearType::BEARER_WIFI) {
147 bool isWifiConnected = data.GetCode() == NetConnState::NET_CONN_STATE_CONNECTED;
148 deviceStateHandler_->ProcessWifiState(isWifiConnected);
149 TELEPHONY_LOGI("DeviceStateEventSubscriber wifi %{public}s", isWifiConnected ? "connected" : "no connected");
150 }
151 }
152
ProcessPowerSaveMode(const CommonEventData & data)153 void DeviceStateEventSubscriber::ProcessPowerSaveMode(const CommonEventData &data)
154 {
155 #ifdef ABILITY_POWER_SUPPORT
156 if (deviceStateHandler_ == nullptr) {
157 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessPowerSaveMode networkSearchHandler_ is nullptr");
158 return;
159 }
160 PowerMode powerModeCode = static_cast<PowerMode>(data.GetCode());
161 switch (powerModeCode) {
162 case PowerMode::POWER_SAVE_MODE:
163 case PowerMode::EXTREME_POWER_SAVE_MODE:
164 deviceStateHandler_->ProcessPowerSaveMode(true);
165 break;
166 case PowerMode::PERFORMANCE_MODE:
167 case PowerMode::NORMAL_MODE:
168 deviceStateHandler_->ProcessPowerSaveMode(false);
169 break;
170 default:
171 TELEPHONY_LOGE("DeviceStateEventSubscriber::ProcessPowerSaveMode invalid event");
172 break;
173 }
174 TELEPHONY_LOGI("ProcessPowerSaveMode powerModeCode %{public}d", static_cast<int32_t>(powerModeCode));
175 #endif
176 }
177
SetEventHandler(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)178 void DeviceStateEventSubscriber::SetEventHandler(const std::shared_ptr<DeviceStateHandler> &deviceStateHandler)
179 {
180 deviceStateHandler_ = deviceStateHandler;
181 }
182
GetEventHandler()183 std::shared_ptr<DeviceStateHandler> DeviceStateEventSubscriber::GetEventHandler()
184 {
185 return deviceStateHandler_;
186 }
187
GetDeviceStateEventIntValue(std::string & event) const188 DeviceStateEventIntValue DeviceStateEventSubscriber::GetDeviceStateEventIntValue(std::string &event) const
189 {
190 auto iter = deviceStateEventMapIntValues_.find(event);
191 if (iter == deviceStateEventMapIntValues_.end()) {
192 return COMMON_EVENT_UNKNOWN;
193 }
194 return iter->second;
195 }
196
InitEventMap()197 void DeviceStateEventSubscriber::InitEventMap()
198 {
199 deviceStateEventMapIntValues_ = {
200 {CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE, COMMON_EVENT_CONNECTIVITY_CHANGE},
201 {CommonEventSupport::COMMON_EVENT_SCREEN_ON, COMMON_EVENT_SCREEN_ON},
202 {CommonEventSupport::COMMON_EVENT_SCREEN_OFF, COMMON_EVENT_SCREEN_OFF},
203 {CommonEventSupport::COMMON_EVENT_POWER_SAVE_MODE_CHANGED, COMMON_EVENT_POWER_SAVE_MODE_CHANGED},
204 {CommonEventSupport::COMMON_EVENT_CHARGING, COMMON_EVENT_CHARGING},
205 {CommonEventSupport::COMMON_EVENT_DISCHARGING, COMMON_EVENT_DISCHARGING},
206 {CommonEventSupport::COMMON_EVENT_SHUTDOWN, COMMON_EVENT_SHUTDOWN},
207 };
208 }
209
210 #ifdef ABILITY_NETMANAGER_EXT_SUPPORT
SystemAbilityStatusChangeListener(std::shared_ptr<DeviceStateEventSubscriber> & sub,sptr<NetManagerStandard::ISharingEventCallback> & callback)211 DeviceStateObserver::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
212 std::shared_ptr<DeviceStateEventSubscriber> &sub, sptr<NetManagerStandard::ISharingEventCallback> &callback)
213 : sub_(sub), callback_(callback)
214 {}
215
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)216 void DeviceStateObserver::SystemAbilityStatusChangeListener::OnAddSystemAbility(
217 int32_t systemAbilityId, const std::string& deviceId)
218 {
219 TELEPHONY_LOGI("systemAbilityId is %{public}d", systemAbilityId);
220 if (sub_ == nullptr || sub_->GetEventHandler() == nullptr) {
221 TELEPHONY_LOGE("sub_ is nullptr or eventHandler is nullptr");
222 return;
223 }
224 switch (systemAbilityId) {
225 case POWER_MANAGER_SERVICE_ID: {
226 #ifdef ABILITY_POWER_SUPPORT
227 auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
228 sub_->GetEventHandler()->ProcessScreenDisplay(powerMgrClient.IsScreenOn());
229 auto powerSaveMode = powerMgrClient.GetDeviceMode();
230 sub_->GetEventHandler()->ProcessPowerSaveMode(
231 powerSaveMode == PowerMode::POWER_SAVE_MODE || powerSaveMode == PowerMode::EXTREME_POWER_SAVE_MODE);
232 #endif
233 break;
234 }
235 case POWER_MANAGER_BATT_SERVICE_ID: {
236 #ifdef ABILITY_BATTERY_SUPPORT
237 auto &batterySrvClient = PowerMgr::BatterySrvClient::GetInstance();
238 sub_->GetEventHandler()->ProcessChargingState(
239 batterySrvClient.GetChargingStatus() == PowerMgr::BatteryChargeState::CHARGE_STATE_ENABLE);
240 #endif
241 break;
242 }
243 case COMMON_EVENT_SERVICE_ID: {
244 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(sub_);
245 TELEPHONY_LOGI("DeviceStateObserver::OnAddSystemAbility subscribeResult = %{public}d", subscribeResult);
246 break;
247 }
248 case COMM_NET_TETHERING_MANAGER_SYS_ABILITY_ID: {
249 auto networkShareClient = DelayedSingleton<NetManagerStandard::NetworkShareClient>::GetInstance();
250 if (networkShareClient == nullptr) {
251 TELEPHONY_LOGE("DeviceStateObserver OnAddSystemAbility networkShareClient is nullptr");
252 return;
253 }
254 int32_t isSharing = 0;
255 networkShareClient->IsSharing(isSharing);
256 sub_->GetEventHandler()->ProcessNetSharingState(isSharing == NetManagerStandard::NETWORKSHARE_IS_SHARING);
257 networkShareClient->RegisterSharingEvent(callback_);
258 break;
259 }
260 default:
261 TELEPHONY_LOGE("systemAbilityId is invalid");
262 break;
263 }
264 }
265
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)266 void DeviceStateObserver::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
267 int32_t systemAbilityId, const std::string& deviceId)
268 {
269 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
270 TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
271 return;
272 }
273 if (sub_ == nullptr) {
274 TELEPHONY_LOGE("DeviceStateObserver::OnRemoveSystemAbility sub_ is nullptr");
275 return;
276 }
277 bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(sub_);
278 TELEPHONY_LOGI("DeviceStateObserver::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
279 }
280
SharingEventCallback(const std::shared_ptr<DeviceStateHandler> & deviceStateHandler)281 SharingEventCallback::SharingEventCallback(
282 const std::shared_ptr<DeviceStateHandler> &deviceStateHandler) : handler_(deviceStateHandler)
283 {}
284
OnSharingStateChanged(const bool & isRunning)285 void SharingEventCallback::OnSharingStateChanged(const bool &isRunning)
286 {
287 if (handler_ == nullptr) {
288 TELEPHONY_LOGE("OnSharingStateChanged handler_ is nullptr");
289 return;
290 }
291 TELEPHONY_LOGI("DeviceStateObserver::OnSharingStateChanged: isSharing = %{public}d", isRunning);
292 handler_->ProcessNetSharingState(isRunning);
293 }
294 #endif
295 } // namespace Telephony
296 } // namespace OHOS
297