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