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