1 /*
2 * Copyright (C) 2021 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 "cellular_data_controller.h"
17
18 #include "common_event_manager.h"
19 #include "core_manager_inner.h"
20 #include "uri.h"
21
22 #include "cellular_data_constant.h"
23 #include "cellular_data_settings_rdb_helper.h"
24 #include "network_search_callback.h"
25 #include "radio_event.h"
26 #include "telephony_log_wrapper.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 using namespace NetManagerStandard;
31
CellularDataController(std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)32 CellularDataController::CellularDataController(std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
33 : AppExecFwk::EventHandler(runner), slotId_(slotId)
34 {}
35
~CellularDataController()36 CellularDataController::~CellularDataController()
37 {
38 UnRegisterEvents();
39 UnRegisterDataObserver();
40 }
41
Init()42 void CellularDataController::Init()
43 {
44 EventFwk::MatchingSkills matchingSkills;
45 matchingSkills.AddEvent(CALL_STATE_CHANGE_ACTION);
46 EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
47 cellularDataHandler_ = std::make_shared<CellularDataHandler>(GetEventRunner(), subscriberInfo, slotId_);
48 cellularDataRdbObserver_ = std::make_unique<CellularDataRdbObserver>(cellularDataHandler_).release();
49 if (cellularDataHandler_ == nullptr || cellularDataRdbObserver_ == nullptr) {
50 TELEPHONY_LOGE("Slot%{public}d: CellularDataController init failed, cellularDataHandler_ or "
51 "cellularDataRdbObserver_ is null",
52 slotId_);
53 return;
54 }
55 cellularDataHandler_->Init();
56 RegisterDatabaseObserver();
57 }
58
SetCellularDataEnable(bool userDataEnabled)59 bool CellularDataController::SetCellularDataEnable(bool userDataEnabled)
60 {
61 if (cellularDataHandler_ == nullptr) {
62 TELEPHONY_LOGE("Slot%{public}d: SetCellularDataEnable cellularDataHandler_ is null", slotId_);
63 return false;
64 }
65 return cellularDataHandler_->SetCellularDataEnable(userDataEnabled);
66 }
67
IsCellularDataEnabled() const68 bool CellularDataController::IsCellularDataEnabled() const
69 {
70 if (cellularDataHandler_ == nullptr) {
71 TELEPHONY_LOGE("Slot%{public}d: IsCellularDataEnabled cellularDataHandler_ is null", slotId_);
72 return false;
73 }
74 return cellularDataHandler_->IsCellularDataEnabled();
75 }
76
GetCellularDataState() const77 ApnProfileState CellularDataController::GetCellularDataState() const
78 {
79 if (cellularDataHandler_ == nullptr) {
80 TELEPHONY_LOGE("Slot%{public}d: GetCellularDataState cellularDataHandler_ is null", slotId_);
81 return ApnProfileState::PROFILE_STATE_FAILED;
82 }
83 return cellularDataHandler_->GetCellularDataState();
84 }
85
GetCellularDataState(const std::string & apnType) const86 ApnProfileState CellularDataController::GetCellularDataState(const std::string &apnType) const
87 {
88 if (cellularDataHandler_ == nullptr) {
89 TELEPHONY_LOGE("Slot%{public}d: GetCellularDataState cellularDataHandler_ is null", slotId_);
90 return ApnProfileState::PROFILE_STATE_FAILED;
91 }
92 return cellularDataHandler_->GetCellularDataState(apnType);
93 }
94
IsCellularDataRoamingEnabled() const95 bool CellularDataController::IsCellularDataRoamingEnabled() const
96 {
97 if (cellularDataHandler_ == nullptr) {
98 TELEPHONY_LOGE("Slot%{public}d: IsCellularDataRoamingEnabled cellularDataHandler_ is null", slotId_);
99 return false;
100 }
101 return cellularDataHandler_->IsCellularDataRoamingEnabled();
102 }
103
SetCellularDataRoamingEnabled(bool dataRoamingEnabled)104 bool CellularDataController::SetCellularDataRoamingEnabled(bool dataRoamingEnabled)
105 {
106 if (cellularDataHandler_ == nullptr) {
107 TELEPHONY_LOGE("Slot%{public}d: SetCellularDataRoamingEnabled cellularDataHandler is null", slotId_);
108 return false;
109 }
110 return cellularDataHandler_->SetCellularDataRoamingEnabled(dataRoamingEnabled);
111 }
112
ReleaseNet(const NetRequest & request)113 bool CellularDataController::ReleaseNet(const NetRequest &request)
114 {
115 if (cellularDataHandler_ == nullptr) {
116 TELEPHONY_LOGE("Slot%{public}d: ReleaseNet cellularDataHandler_ is null", slotId_);
117 return false;
118 }
119 return cellularDataHandler_->ReleaseNet(request);
120 }
121
RequestNet(const NetRequest & request)122 bool CellularDataController::RequestNet(const NetRequest &request)
123 {
124 if (cellularDataHandler_ == nullptr) {
125 TELEPHONY_LOGE("Slot%{public}d: RequestNet cellularDataHandler_ is null", slotId_);
126 return false;
127 }
128 return cellularDataHandler_->RequestNet(request);
129 }
130
AsynchronousRegister()131 void CellularDataController::AsynchronousRegister()
132 {
133 if (CoreManagerInner::GetInstance().IsInitFinished()) {
134 TELEPHONY_LOGI("Slot%{public}d: core inited", slotId_);
135 Init();
136 RegisterEvents();
137 return;
138 }
139 TELEPHONY_LOGI("Slot%{public}d: AsynchronousRegister", slotId_);
140 SendEvent(CellularDataEventCode::MSG_ASYNCHRONOUS_REGISTER_EVENT_ID, CORE_INIT_DELAY_TIME, Priority::HIGH);
141 }
142
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)143 void CellularDataController::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
144 {
145 if (event == nullptr) {
146 TELEPHONY_LOGE("Slot%{public}d: ProcessEvent event is null.", slotId_);
147 return;
148 }
149 int32_t eventId = event->GetInnerEventId();
150 switch (eventId) {
151 case CellularDataEventCode::MSG_REG_NET_MANAGER: {
152 if (!CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_)) {
153 SendEvent(CellularDataEventCode::MSG_REG_NET_MANAGER, REG_NET_MANAGER_DELAY_TIME, Priority::LOW);
154 }
155 break;
156 }
157 case CellularDataEventCode::MSG_REG_POLICY_CALL_BACK: {
158 if (!CellularDataNetAgent::GetInstance().RegisterPolicyCallback()) {
159 SendEvent(CellularDataEventCode::MSG_REG_POLICY_CALL_BACK, REG_NET_MANAGER_DELAY_TIME, Priority::LOW);
160 }
161 break;
162 }
163 case CellularDataEventCode::MSG_ASYNCHRONOUS_REGISTER_EVENT_ID:
164 AsynchronousRegister();
165 break;
166 default:
167 TELEPHONY_LOGE("Slot%{public}d: ProcessEvent nothing to do", slotId_);
168 break;
169 }
170 }
171
RegisterEvents()172 void CellularDataController::RegisterEvents()
173 {
174 if (cellularDataHandler_ == nullptr) {
175 TELEPHONY_LOGE("Slot%{public}d: core is null or cellularDataHandler is null", slotId_);
176 return;
177 }
178 TELEPHONY_LOGI("Slot%{public}d: RegisterEvents start", slotId_);
179 CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
180 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_STATE_CHANGE, nullptr);
181 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_RECORDS_LOADED, nullptr);
182 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_ACCOUNT_LOADED, nullptr);
183 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_ATTACHED, nullptr);
184 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_DETACHED, nullptr);
185 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_OPEN, nullptr);
186 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_CLOSE, nullptr);
187 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_STATE_CHANGED, nullptr);
188 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_RAT_CHANGED, nullptr);
189 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_CALL_STATUS_INFO, nullptr);
190 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_OPEN, nullptr);
191 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_CLOSE, nullptr);
192 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_STATE_CHANGED, nullptr);
193 coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_FREQUENCY_CHANGED, nullptr);
194 if (!CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_)) {
195 SendEvent(CellularDataEventCode::MSG_REG_NET_MANAGER, REG_NET_MANAGER_DELAY_TIME, Priority::LOW);
196 }
197 if (slotId_ == 0) {
198 sptr<NetworkSearchCallback> networkSearchCallback = std::make_unique<NetworkSearchCallback>().release();
199 if (networkSearchCallback != nullptr) {
200 coreInner.RegisterCellularDataObject(networkSearchCallback);
201 } else {
202 TELEPHONY_LOGE("Slot%{public}d: networkSearchCallback is null", slotId_);
203 }
204 if (!CellularDataNetAgent::GetInstance().RegisterPolicyCallback()) {
205 SendEvent(CellularDataEventCode::MSG_REG_POLICY_CALL_BACK, REG_NET_MANAGER_DELAY_TIME, Priority::LOW);
206 }
207 }
208 }
209
UnRegisterEvents()210 void CellularDataController::UnRegisterEvents()
211 {
212 if (cellularDataHandler_ == nullptr) {
213 TELEPHONY_LOGE("Slot%{public}d: UnRegisterEvents cellularDataHandler_ is null", slotId_);
214 return;
215 }
216 TELEPHONY_LOGI("Slot%{public}d: UnRegisterEvents start", slotId_);
217 CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
218 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_STATE_CHANGE);
219 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_RECORDS_LOADED);
220 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_ACCOUNT_LOADED);
221 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_ATTACHED);
222 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_DETACHED);
223 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_OPEN);
224 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_CLOSE);
225 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_STATE_CHANGED);
226 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_RAT_CHANGED);
227 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_CALL_STATUS_INFO);
228 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_OPEN);
229 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_CLOSE);
230 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_STATE_CHANGED);
231 coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_FREQUENCY_CHANGED);
232 TELEPHONY_LOGI("Slot%{public}d: UnRegisterEvents end", slotId_);
233 }
234
UnRegisterDataObserver()235 void CellularDataController::UnRegisterDataObserver()
236 {
237 std::shared_ptr<CellularDataRdbHelper> cellularDataDbHelper = CellularDataRdbHelper::GetInstance();
238 cellularDataDbHelper->UnRegisterObserver(cellularDataRdbObserver_);
239 }
240
RegisterDatabaseObserver()241 void CellularDataController::RegisterDatabaseObserver()
242 {
243 std::shared_ptr<CellularDataRdbHelper> cellularDataDbHelper = CellularDataRdbHelper::GetInstance();
244 cellularDataDbHelper->RegisterObserver(cellularDataRdbObserver_);
245 }
246
HandleApnChanged()247 bool CellularDataController::HandleApnChanged()
248 {
249 if (cellularDataHandler_ == nullptr) {
250 TELEPHONY_LOGE("Slot%{public}d: ApnChanged cellularDataHandler_ is null", slotId_);
251 return static_cast<int32_t>(DataRespondCode::SET_FAILED);
252 }
253 return cellularDataHandler_->HandleApnChanged();
254 }
255
GetCellularDataFlowType()256 int32_t CellularDataController::GetCellularDataFlowType()
257 {
258 if (cellularDataHandler_ == nullptr) {
259 TELEPHONY_LOGE("Slot%{public}d: cellular data handler is null", slotId_);
260 return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE);
261 }
262 return cellularDataHandler_->GetCellularDataFlowType();
263 }
264
EstablishDataConnection()265 void CellularDataController::EstablishDataConnection()
266 {
267 if (cellularDataHandler_ != nullptr) {
268 cellularDataHandler_->EstablishAllApnsIfConnectable();
269 }
270 }
271
SetPolicyDataOn(bool enable)272 int32_t CellularDataController::SetPolicyDataOn(bool enable)
273 {
274 if (cellularDataHandler_ != nullptr) {
275 cellularDataHandler_->SetPolicyDataOn(enable);
276 }
277 return static_cast<int32_t>(DataRespondCode::SET_SUCCESS);
278 }
279
IsRestrictedMode() const280 bool CellularDataController::IsRestrictedMode() const
281 {
282 if (cellularDataHandler_ != nullptr) {
283 return cellularDataHandler_->IsRestrictedMode();
284 }
285 return false;
286 }
287
GetDisConnectionReason()288 DisConnectionReason CellularDataController::GetDisConnectionReason()
289 {
290 if (cellularDataHandler_ != nullptr) {
291 return cellularDataHandler_->GetDisConnectionReason();
292 }
293 return DisConnectionReason::REASON_NORMAL;
294 }
295
HasInternetCapability(const int32_t cid) const296 bool CellularDataController::HasInternetCapability(const int32_t cid) const
297 {
298 if (cellularDataHandler_ == nullptr) {
299 TELEPHONY_LOGE("Slot%{public}d: cellularDataHandler_ is null", slotId_);
300 return false;
301 }
302 return cellularDataHandler_->HasInternetCapability(cid);
303 }
304
ClearAllConnections(DisConnectionReason reason) const305 bool CellularDataController::ClearAllConnections(DisConnectionReason reason) const
306 {
307 if (cellularDataHandler_ == nullptr) {
308 TELEPHONY_LOGE("Slot%{public}d: cellularDataHandler is null", slotId_);
309 return false;
310 }
311 cellularDataHandler_->ClearAllConnections(reason);
312 return true;
313 }
314 } // namespace Telephony
315 } // namespace OHOS
316