• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service.h"
17 
18 #include "cellular_data_dump_helper.h"
19 #include "cellular_data_error.h"
20 #include "cellular_data_hisysevent.h"
21 #include "cellular_data_net_agent.h"
22 #include "core_manager_inner.h"
23 #include "data_connection_monitor.h"
24 #include "net_specifier.h"
25 #include "runner_pool.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28 #include "telephony_common_utils.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_permission.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 using namespace NetManagerStandard;
35 
36 bool g_registerResult = SystemAbility::MakeAndRegisterAbility(&DelayedRefSingleton<CellularDataService>::GetInstance());
CellularDataService()37 CellularDataService::CellularDataService()
38     : SystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID, true), registerToService_(false),
39       state_(ServiceRunningState::STATE_NOT_START)
40 {}
41 
42 CellularDataService::~CellularDataService() = default;
43 
OnStart()44 void CellularDataService::OnStart()
45 {
46     beginTime_ =
47         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
48             .count();
49     if (state_ == ServiceRunningState::STATE_RUNNING) {
50         TELEPHONY_LOGE("CellularDataService has already started.");
51         return;
52     }
53     RunnerPool::GetInstance().Init();
54     if (!Init()) {
55         TELEPHONY_LOGE("failed to init CellularDataService");
56         return;
57     }
58     state_ = ServiceRunningState::STATE_RUNNING;
59     endTime_ =
60         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
61             .count();
62     TELEPHONY_LOGI("CellularDataService::OnStart start service success.");
63 }
64 
OnStop()65 void CellularDataService::OnStop()
66 {
67     TELEPHONY_LOGI("CellularDataService::OnStop ready to stop service.");
68     if (eventLoop_ != nullptr) {
69         eventLoop_.reset();
70     }
71     state_ = ServiceRunningState::STATE_NOT_START;
72     registerToService_ = false;
73     UnRegisterAllNetSpecifier();
74 }
75 
Dump(std::int32_t fd,const std::vector<std::u16string> & args)76 int32_t CellularDataService::Dump(std::int32_t fd, const std::vector<std::u16string> &args)
77 {
78     std::vector<std::string> argsInStr;
79     std::string result;
80     CellularDataDumpHelper dumpHelper;
81     if (fd < 0) {
82         TELEPHONY_LOGE("Dump fd invalid");
83         return TELEPHONY_ERR_FAIL;
84     }
85     for (const std::u16string &arg : args) {
86         TELEPHONY_LOGI("Dump args: %s", Str16ToStr8(arg).c_str());
87         argsInStr.emplace_back(Str16ToStr8(arg));
88     }
89     if (dumpHelper.Dump(argsInStr, result)) {
90         int32_t ret = dprintf(fd, "%s", result.c_str());
91         if (ret < 0) {
92             TELEPHONY_LOGE("dprintf to dump fd failed");
93             return TELEPHONY_ERR_FAIL;
94         }
95         return TELEPHONY_SUCCESS;
96     }
97     TELEPHONY_LOGW("dumpHelper failed");
98     return TELEPHONY_ERR_FAIL;
99 }
100 
Init()101 bool CellularDataService::Init()
102 {
103     eventLoop_ = RunnerPool::GetInstance().GetCommonRunner();
104     if (eventLoop_ == nullptr) {
105         TELEPHONY_LOGE("failed to create EventRunner");
106         return false;
107     }
108     InitModule();
109     if (!registerToService_) {
110         bool ret = Publish(DelayedRefSingleton<CellularDataService>::GetInstance().AsObject());
111         if (!ret) {
112             TELEPHONY_LOGE("CellularDataService::Init Publish failed!");
113             return false;
114         }
115         registerToService_ = true;
116     }
117     for (const std::pair<const int32_t, std::shared_ptr<CellularDataController>> &it : cellularDataControllers_) {
118         if (it.second != nullptr) {
119             it.second->AsynchronousRegister();
120         } else {
121             TELEPHONY_LOGE("CellularDataController is null");
122         }
123     }
124     return true;
125 }
126 
IsCellularDataEnabled(bool & dataEnabled)127 int32_t CellularDataService::IsCellularDataEnabled(bool &dataEnabled)
128 {
129     if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
130         return TELEPHONY_ERR_PERMISSION_ERR;
131     }
132     if (cellularDataControllers_[DEFAULT_SIM_SLOT_ID] == nullptr) {
133         TELEPHONY_LOGE("cellularDataControllers_[0] is null");
134         return TELEPHONY_ERR_LOCAL_PTR_NULL;
135     }
136     return cellularDataControllers_[DEFAULT_SIM_SLOT_ID]->IsCellularDataEnabled(dataEnabled);
137 }
138 
EnableCellularData(bool enable)139 int32_t CellularDataService::EnableCellularData(bool enable)
140 {
141     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
142         TELEPHONY_LOGE("Non-system applications use system APIs!");
143         return TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
144     }
145     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
146         int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
147         CellularDataHiSysEvent::WriteDataActivateFaultEvent(
148             slotId, enable, CellularDataErrorCode::DATA_ERROR_PERMISSION_ERROR, Permission::SET_TELEPHONY_STATE);
149         return TELEPHONY_ERR_PERMISSION_ERR;
150     }
151     if (cellularDataControllers_[DEFAULT_SIM_SLOT_ID] == nullptr) {
152         TELEPHONY_LOGE("cellularDataControllers_[0] is null");
153         return TELEPHONY_ERR_LOCAL_PTR_NULL;
154     }
155     return cellularDataControllers_[DEFAULT_SIM_SLOT_ID]->SetCellularDataEnable(enable);
156 }
157 
GetCellularDataState()158 int32_t CellularDataService::GetCellularDataState()
159 {
160     int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
161     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
162         cellularDataControllers_.find(slotId);
163     if (item == cellularDataControllers_.end() || item->second == nullptr) {
164         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
165         return CELLULAR_DATA_INVALID_PARAM;
166     }
167     int32_t dataState = CellularDataStateAdapter(item->second->GetCellularDataState());
168     DisConnectionReason reason = item->second->GetDisConnectionReason();
169     if (reason == DisConnectionReason::REASON_GSM_AND_CALLING_ONLY && item->second->IsRestrictedMode()) {
170         dataState = static_cast<int32_t>(DataConnectionStatus::DATA_STATE_SUSPENDED);
171     }
172     return dataState;
173 }
174 
IsCellularDataRoamingEnabled(const int32_t slotId,bool & dataRoamingEnabled)175 int32_t CellularDataService::IsCellularDataRoamingEnabled(const int32_t slotId, bool &dataRoamingEnabled)
176 {
177     if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
178         return TELEPHONY_ERR_PERMISSION_ERR;
179     }
180     if (!CheckParamValid(slotId)) {
181         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
182         return CELLULAR_DATA_INVALID_PARAM;
183     }
184     return cellularDataControllers_[slotId]->IsCellularDataRoamingEnabled(dataRoamingEnabled);
185 }
186 
EnableCellularDataRoaming(const int32_t slotId,bool enable)187 int32_t CellularDataService::EnableCellularDataRoaming(const int32_t slotId, bool enable)
188 {
189     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
190         TELEPHONY_LOGE("Non-system applications use system APIs!");
191         return TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
192     }
193     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
194         return TELEPHONY_ERR_PERMISSION_ERR;
195     }
196     if (!CheckParamValid(slotId)) {
197         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
198         return TELEPHONY_ERR_SLOTID_INVALID;
199     }
200     int32_t result = cellularDataControllers_[slotId]->SetCellularDataRoamingEnabled(enable);
201     if (result == TELEPHONY_ERR_SUCCESS) {
202         CellularDataHiSysEvent::WriteRoamingConnectStateBehaviorEvent(enable);
203     }
204     return result;
205 }
206 
InitModule()207 void CellularDataService::InitModule()
208 {
209     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
210     netAgent.ClearNetSupplier();
211     cellularDataControllers_.clear();
212     std::vector<uint64_t> netCapabilities;
213     netCapabilities.push_back(NetCap::NET_CAPABILITY_INTERNET);
214     netCapabilities.push_back(NetCap::NET_CAPABILITY_MMS);
215     int32_t simNum = CoreManagerInner::GetInstance().GetMaxSimCount();
216     for (int32_t i = 0; i < simNum; ++i) {
217         std::shared_ptr<CellularDataController> cellularDataController =
218             std::make_shared<CellularDataController>(eventLoop_, i);
219         if (cellularDataController == nullptr) {
220             TELEPHONY_LOGE("CellularDataService init module failed cellularDataController is null.");
221             continue;
222         }
223         cellularDataControllers_.insert(
224             std::pair<int32_t, std::shared_ptr<CellularDataController>>(i, cellularDataController));
225         for (uint64_t capability : netCapabilities) {
226             NetSupplier netSupplier = { 0 };
227             netSupplier.supplierId = 0;
228             netSupplier.slotId = i;
229             netSupplier.capability = capability;
230             netAgent.AddNetSupplier(netSupplier);
231         }
232     }
233 }
234 
CheckParamValid(const int32_t slotId)235 bool CellularDataService::CheckParamValid(const int32_t slotId)
236 {
237     if (slotId < 0) {
238         return false;
239     }
240     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
241         cellularDataControllers_.find(slotId);
242     if (item == cellularDataControllers_.end()) {
243         return false;
244     }
245     if (item->second == nullptr) {
246         return false;
247     }
248     return true;
249 }
250 
ReleaseNet(const NetRequest & request)251 int32_t CellularDataService::ReleaseNet(const NetRequest &request)
252 {
253     std::string requestIdent = request.ident.substr(strlen(IDENT_PREFIX));
254     if (!IsValidDecValue(requestIdent)) {
255         return CELLULAR_DATA_INVALID_PARAM;
256     }
257     int32_t simId = std::stoi(requestIdent);
258     int32_t slotId = CoreManagerInner::GetInstance().GetSlotId(simId);
259     if (!CheckParamValid(slotId)) {
260         return CELLULAR_DATA_INVALID_PARAM;
261     }
262     bool result = cellularDataControllers_[slotId]->ReleaseNet(request);
263     return static_cast<int32_t>(result ? RequestNetCode::REQUEST_SUCCESS : RequestNetCode::REQUEST_FAILED);
264 }
265 
RequestNet(const NetRequest & request)266 int32_t CellularDataService::RequestNet(const NetRequest &request)
267 {
268     std::string requestIdent = request.ident.substr(strlen(IDENT_PREFIX));
269     if (!IsValidDecValue(requestIdent)) {
270         return CELLULAR_DATA_INVALID_PARAM;
271     }
272     int32_t simId = std::stoi(requestIdent);
273     int32_t slotId = CoreManagerInner::GetInstance().GetSlotId(simId);
274     if (!CheckParamValid(slotId)) {
275         return CELLULAR_DATA_INVALID_PARAM;
276     }
277     bool result = cellularDataControllers_[slotId]->RequestNet(request);
278     return static_cast<int32_t>(result ? RequestNetCode::REQUEST_SUCCESS : RequestNetCode::REQUEST_FAILED);
279 }
280 
DispatchEvent(const int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)281 void CellularDataService::DispatchEvent(const int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
282 {
283     if (!CheckParamValid(slotId)) {
284         TELEPHONY_LOGI("dispatch event slotId invalid");
285         return;
286     }
287     cellularDataControllers_[slotId]->ProcessEvent(event);
288 }
289 
UnRegisterAllNetSpecifier()290 void CellularDataService::UnRegisterAllNetSpecifier()
291 {
292     CellularDataNetAgent::GetInstance().UnregisterAllNetSupplier();
293     CellularDataNetAgent::GetInstance().UnregisterPolicyCallback();
294 }
295 
HandleApnChanged(const int32_t slotId)296 int32_t CellularDataService::HandleApnChanged(const int32_t slotId)
297 {
298     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
299         TELEPHONY_LOGE("permission denied!");
300         return TELEPHONY_ERR_PERMISSION_ERR;
301     }
302     if (!CheckParamValid(slotId)) {
303         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
304         return CELLULAR_DATA_INVALID_PARAM;
305     }
306     bool result = cellularDataControllers_[slotId]->HandleApnChanged();
307     return result ? static_cast<int32_t>(DataRespondCode::SET_SUCCESS)
308                   : static_cast<int32_t>(DataRespondCode::SET_FAILED);
309 }
310 
GetDefaultCellularDataSlotId()311 int32_t CellularDataService::GetDefaultCellularDataSlotId()
312 {
313     return CoreManagerInner::GetInstance().GetDefaultCellularDataSlotId();
314 }
315 
GetDefaultCellularDataSimId(int32_t & simId)316 int32_t CellularDataService::GetDefaultCellularDataSimId(int32_t &simId)
317 {
318     return CoreManagerInner::GetInstance().GetDefaultCellularDataSimId(simId);
319 }
320 
SetDefaultCellularDataSlotId(const int32_t slotId)321 int32_t CellularDataService::SetDefaultCellularDataSlotId(const int32_t slotId)
322 {
323     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
324         TELEPHONY_LOGE("Non-system applications use system APIs!");
325         return TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
326     }
327     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
328         return TELEPHONY_ERR_PERMISSION_ERR;
329     }
330     int32_t formerSlotId = GetDefaultCellularDataSlotId();
331     if (formerSlotId < 0) {
332         TELEPHONY_LOGI("No old card slot id.");
333     }
334     int32_t result = CoreManagerInner::GetInstance().SetDefaultCellularDataSlotId(slotId);
335     if (result != TELEPHONY_ERR_SUCCESS) {
336         TELEPHONY_LOGE("set slot id fail");
337         return result;
338     }
339     return TELEPHONY_ERR_SUCCESS;
340 }
341 
GetCellularDataFlowType()342 int32_t CellularDataService::GetCellularDataFlowType()
343 {
344     int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
345     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
346         cellularDataControllers_.find(slotId);
347     if (item == cellularDataControllers_.end() || item->second == nullptr) {
348         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
349         return CELLULAR_DATA_INVALID_PARAM;
350     }
351     DisConnectionReason reason = item->second->GetDisConnectionReason();
352     if (reason == DisConnectionReason::REASON_GSM_AND_CALLING_ONLY && item->second->IsRestrictedMode()) {
353         return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT);
354     }
355     int32_t result = item->second->GetCellularDataFlowType();
356     return result;
357 }
358 
GetBeginTime()359 std::string CellularDataService::GetBeginTime()
360 {
361     std::ostringstream oss;
362     oss << beginTime_;
363     TELEPHONY_LOGI("bindTime :=  %{public}s", oss.str().c_str());
364     return oss.str();
365 }
366 
GetEndTime()367 std::string CellularDataService::GetEndTime()
368 {
369     std::ostringstream oss;
370     oss << endTime_;
371     TELEPHONY_LOGI("endTime :=  %{public}s", oss.str().c_str());
372     return oss.str();
373 }
374 
GetCellularDataSlotIdDump()375 std::string CellularDataService::GetCellularDataSlotIdDump()
376 {
377     std::ostringstream oss;
378     oss << "slotId:" << GetDefaultCellularDataSlotId();
379     return oss.str();
380 }
381 
GetStateMachineCurrentStatusDump()382 std::string CellularDataService::GetStateMachineCurrentStatusDump()
383 {
384     std::ostringstream oss;
385     int32_t slotId = GetDefaultCellularDataSlotId();
386     if (!CheckParamValid(slotId)) {
387         oss << "default slotId: " << slotId;
388         return oss.str();
389     }
390     ApnProfileState statusDefault = cellularDataControllers_[slotId]->GetCellularDataState(DATA_CONTEXT_ROLE_DEFAULT);
391     ApnProfileState statusIms = cellularDataControllers_[slotId]->GetCellularDataState(DATA_CONTEXT_ROLE_IMS);
392     oss << "Default connect state: " << static_cast<int32_t>(statusDefault);
393     oss << "Ims connect state:  " << static_cast<int32_t>(statusIms);
394     return oss.str();
395 }
396 
GetFlowDataInfoDump()397 std::string CellularDataService::GetFlowDataInfoDump()
398 {
399     std::ostringstream oss;
400     int32_t slotId = GetDefaultCellularDataSlotId();
401     if (!CheckParamValid(slotId)) {
402         oss << "default slotId: " << slotId;
403         return oss.str();
404     }
405     int32_t dataFlowInfo = cellularDataControllers_[slotId]->GetCellularDataFlowType();
406     oss << "data flow info: " << dataFlowInfo;
407     return oss.str();
408 }
409 
StrategySwitch(int32_t slotId,bool enable)410 int32_t CellularDataService::StrategySwitch(int32_t slotId, bool enable)
411 {
412     if (!CheckParamValid(slotId)) {
413         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
414         return CELLULAR_DATA_INVALID_PARAM;
415     }
416     int32_t result = cellularDataControllers_[slotId]->SetPolicyDataOn(enable);
417     if (result == static_cast<int32_t>(DataRespondCode::SET_SUCCESS) && enable) {
418         CellularDataHiSysEvent::WriteDataDeactiveBehaviorEvent(slotId, DataDisconnectCause::HIGN_PRIORITY_NETWORK);
419     }
420     return result;
421 }
422 
HasInternetCapability(const int32_t slotId,const int32_t cid)423 int32_t CellularDataService::HasInternetCapability(const int32_t slotId, const int32_t cid)
424 {
425     if (!CheckParamValid(slotId)) {
426         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
427         return CELLULAR_DATA_INVALID_PARAM;
428     }
429     bool result = cellularDataControllers_[slotId]->HasInternetCapability(cid);
430     return result ? static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS)
431                   : static_cast<int32_t>(RequestNetCode::REQUEST_FAILED);
432 }
433 
ClearCellularDataConnections(const int32_t slotId)434 int32_t CellularDataService::ClearCellularDataConnections(const int32_t slotId)
435 {
436     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
437         TELEPHONY_LOGE("Permission denied!");
438         return TELEPHONY_ERR_PERMISSION_ERR;
439     }
440     return ClearAllConnections(slotId, DisConnectionReason::REASON_CLEAR_CONNECTION);
441 }
442 
ClearAllConnections(const int32_t slotId,DisConnectionReason reason)443 int32_t CellularDataService::ClearAllConnections(const int32_t slotId, DisConnectionReason reason)
444 {
445     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
446         cellularDataControllers_.find(slotId);
447     if (item == cellularDataControllers_.end() || item->second == nullptr) {
448         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
449         return CELLULAR_DATA_INVALID_PARAM;
450     }
451     bool result = item->second->ClearAllConnections(reason);
452     return result ? static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS)
453                   : static_cast<int32_t>(RequestNetCode::REQUEST_FAILED);
454 }
455 
GetServiceRunningState()456 int32_t CellularDataService::GetServiceRunningState()
457 {
458     return static_cast<int32_t>(state_);
459 }
460 
GetSpendTime()461 int64_t CellularDataService::GetSpendTime()
462 {
463     return endTime_ - beginTime_;
464 }
465 
RegisterSimAccountCallback(const sptr<SimAccountCallback> & callback)466 int32_t CellularDataService::RegisterSimAccountCallback(const sptr<SimAccountCallback> &callback)
467 {
468     return CoreManagerInner::GetInstance().RegisterSimAccountCallback(GetBundleName(), callback);
469 }
470 
UnregisterSimAccountCallback()471 int32_t CellularDataService::UnregisterSimAccountCallback()
472 {
473     return CoreManagerInner::GetInstance().UnregisterSimAccountCallback(GetBundleName());
474 }
475 } // namespace Telephony
476 } // namespace OHOS
477