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