• 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 
16 #include "device_state_handler.h"
17 
18 #include "battery_srv_client.h"
19 #include "network_search_manager.h"
20 #include "power_mgr_client.h"
21 #include "telephony_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 namespace {
26 const uint32_t CELL_REQUEST_SHORT_INTERVAL = 2; // This is the minimum interval in seconds for cell requests
27 const uint32_t CELL_REQUEST_LONG_INTERVAL = 10; // This is the maximum interval in seconds for cell requests
28 } // namespace
29 
DeviceStateHandler(const std::weak_ptr<NetworkSearchManager> & networkSearchManager,const std::weak_ptr<ITelRilManager> & telRilManager,int32_t slotId)30 DeviceStateHandler::DeviceStateHandler(
31     const std::weak_ptr<NetworkSearchManager> &networkSearchManager,
32     const std::weak_ptr<ITelRilManager> &telRilManager, int32_t slotId)
33     : networkSearchManager_(networkSearchManager), telRilManager_(telRilManager), slotId_(slotId)
34 {
35     isCharging_ = true;
36     auto &powerMgrClient = PowerMgr::PowerMgrClient::GetInstance();
37     isScreenOn_ = true;
38     auto powerSaveMode = powerMgrClient.GetDeviceMode();
39     isPowerSaveModeOn_ = powerSaveMode == PowerMgr::PowerMode::POWER_SAVE_MODE ||
40         powerSaveMode == PowerMgr::PowerMode::EXTREME_POWER_SAVE_MODE;
41     TELEPHONY_LOGI("DeviceStateHandler isCharging_=%{public}d, isScreenOn_=%{public}d, isPowerSaveModeOn_=%{public}d",
42         isCharging_, isScreenOn_, isPowerSaveModeOn_);
43 }
44 
ProcessWifiState(bool isWifiConnected)45 void DeviceStateHandler::ProcessWifiState(bool isWifiConnected)
46 {
47     isWifiConnected_ = isWifiConnected;
48     ProcessDeviceState();
49 }
50 
ProcessScreenDisplay(bool isScreenOn)51 void DeviceStateHandler::ProcessScreenDisplay(bool isScreenOn)
52 {
53     isScreenOn_ = isScreenOn;
54     if (isScreenOn) {
55         GetRrcConnectionState();
56     }
57     ProcessDeviceState();
58 }
59 
ProcessPowerSaveMode(bool isPowerSaveModeOn)60 void DeviceStateHandler::ProcessPowerSaveMode(bool isPowerSaveModeOn)
61 {
62     isPowerSaveModeOn_ = isPowerSaveModeOn;
63     SetDeviceState(POWER_SAVE_MODE, isPowerSaveModeOn_);
64     ProcessDeviceState();
65 }
66 
ProcessChargingState(bool isCharging)67 void DeviceStateHandler::ProcessChargingState(bool isCharging)
68 {
69     isCharging_ = isCharging;
70     SetDeviceState(CHARGING_STATE, isCharging_);
71     ProcessDeviceState();
72 }
73 
ProcessNetSharingState(bool isNetSharingOn)74 void DeviceStateHandler::ProcessNetSharingState(bool isNetSharingOn)
75 {
76     isNetSharingOn_ = isNetSharingOn;
77     ProcessDeviceState();
78 }
79 
ProcessRadioState()80 void DeviceStateHandler::ProcessRadioState()
81 {
82     SyncSettings();
83 }
84 
ProcessDeviceState()85 void DeviceStateHandler::ProcessDeviceState()
86 {
87     uint32_t newCellRequestMinInterval = GetCellRequestMinInterval();
88     TELEPHONY_LOGI(
89         "ProcessDeviceState isCharging_=%{public}d, isPowerSaveModeOn_=%{public}d, isNetSharingOn_=%{public}d, "
90         "isScreenOn_=%{public}d, isWifiConnected_=%{public}d, newCellRequestMinInterval=%{public}d",
91         isCharging_, isPowerSaveModeOn_, isNetSharingOn_, isScreenOn_, isWifiConnected_, newCellRequestMinInterval);
92     if (cellRequestMinInterval_ != newCellRequestMinInterval) {
93         cellRequestMinInterval_ = newCellRequestMinInterval;
94         SetCellRequestMinInterval(cellRequestMinInterval_);
95     }
96 
97     if (isLowData_ != IsLowPowerConsumption()) {
98         isLowData_ = !isLowData_;
99         SetDeviceState(LOW_DATA_STATE, isLowData_);
100     }
101 
102     int32_t newFilter = NOTIFICATION_FILTER_NONE;
103     if (IsSignalStrengthNotificationExpected()) {
104         newFilter |= NOTIFICATION_FILTER_SIGNAL_STRENGTH;
105     }
106 
107     if (!IsLowPowerConsumption()) {
108         newFilter |= NOTIFICATION_FILTER_NETWORK_STATE;
109         newFilter |= NOTIFICATION_FILTER_DATA_CALL;
110         newFilter |= NOTIFICATION_FILTER_LINK_CAPACITY;
111         newFilter |= NOTIFICATION_FILTER_PHYSICAL_CHANNEL_CONFIG;
112     }
113 
114     SetNotificationFilter(newFilter, false);
115 }
116 
IsSignalStrengthNotificationExpected() const117 bool DeviceStateHandler::IsSignalStrengthNotificationExpected() const
118 {
119     return isCharging_ || isScreenOn_;
120 }
121 
IsLowPowerConsumption() const122 bool DeviceStateHandler::IsLowPowerConsumption() const
123 {
124     return !isCharging_ && !isScreenOn_ && !isNetSharingOn_;
125 }
126 
GetCellRequestMinInterval() const127 uint32_t DeviceStateHandler::GetCellRequestMinInterval() const
128 {
129     if (isScreenOn_ && (!isWifiConnected_ || isCharging_)) {
130         return CELL_REQUEST_SHORT_INTERVAL;
131     } else {
132         return CELL_REQUEST_LONG_INTERVAL;
133     }
134 }
135 
SetCellRequestMinInterval(uint32_t minInterval) const136 void DeviceStateHandler::SetCellRequestMinInterval(uint32_t minInterval) const
137 {
138     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
139     if (nsm == nullptr) {
140         TELEPHONY_LOGE("DeviceStateHandler::SetCellRequestMinInterval nsm is null");
141         return;
142     }
143     auto inner = nsm->FindManagerInner(slotId_);
144     if (inner == nullptr) {
145         TELEPHONY_LOGE("DeviceStateHandler::SetCellRequestMinInterval inner is null");
146         return;
147     }
148     if (inner->networkSearchHandler_ != nullptr) {
149         TELEPHONY_LOGD("DeviceStateHandler::SetCellRequestMinInterval %{public}d", minInterval);
150         inner->networkSearchHandler_->SetCellRequestMinInterval(minInterval);
151     }
152 }
153 
SetNotificationFilter(int32_t newFilter,bool force)154 void DeviceStateHandler::SetNotificationFilter(int32_t newFilter, bool force)
155 {
156     if (!force && newFilter == notificationFilter_) {
157         TELEPHONY_LOGE("DeviceStateHandler::SetNotificationFilter is not necessary");
158         return;
159     }
160     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_NOTIFICATION_FILTER);
161     if (event == nullptr) {
162         TELEPHONY_LOGE("DeviceStateHandler::SetNotificationFilter event is null");
163         return;
164     }
165     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
166     if (telRilManager != nullptr) {
167         TELEPHONY_LOGI("DeviceStateHandler::SetNotificationFilter old filter:%{public}d, new filter:%{public}d,"
168             " slotId_:%{public}d", notificationFilter_, newFilter, slotId_);
169         telRilManager->SetNotificationFilter(slotId_, newFilter, event);
170         notificationFilter_ = newFilter;
171     }
172 }
173 
SetDeviceState(int32_t deviceStateType,bool deviceStateOn)174 void DeviceStateHandler::SetDeviceState(int32_t deviceStateType, bool deviceStateOn)
175 {
176     auto event = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_DEVICE_STATE);
177     if (event == nullptr) {
178         TELEPHONY_LOGE("DeviceStateHandler::SetDeviceState event is null");
179         return;
180     }
181     std::shared_ptr<ITelRilManager> telRilManager = telRilManager_.lock();
182     if (telRilManager != nullptr) {
183         TELEPHONY_LOGD("DeviceStateHandler::SetDeviceState type:%{public}d state:%{public}d, slotId_:%{public}d",
184             deviceStateType, deviceStateOn, slotId_);
185         telRilManager->SetDeviceState(slotId_, deviceStateType, deviceStateOn, event);
186     }
187 }
188 
SyncSettings()189 void DeviceStateHandler::SyncSettings()
190 {
191     TELEPHONY_LOGI("DeviceStateHandler::SyncSettings isCharging_=%{public}d, isLowData_=%{public}d,"
192         " isPowerSaveModeOn_=%{public}d, notificationFilter_=%{public}d",
193         isCharging_, isLowData_, isPowerSaveModeOn_, notificationFilter_);
194     SetDeviceState(CHARGING_STATE, isCharging_);
195     SetDeviceState(LOW_DATA_STATE, isLowData_);
196     SetDeviceState(POWER_SAVE_MODE, isPowerSaveModeOn_);
197     SetNotificationFilter(notificationFilter_, true);
198 }
199 
GetRrcConnectionState() const200 void DeviceStateHandler::GetRrcConnectionState() const
201 {
202     std::shared_ptr<NetworkSearchManager> nsm = networkSearchManager_.lock();
203     if (nsm == nullptr) {
204         TELEPHONY_LOGE("DeviceStateHandler::GetRrcConnectionState nsm is null");
205         return;
206     }
207     int32_t status = 0;
208     nsm->GetRrcConnectionState(slotId_, status);
209 }
210 } // namespace Telephony
211 } // namespace OHOS
212