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