• 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 "string_ex.h"
19 #include "system_ability_definition.h"
20 
21 #include "net_specifier.h"
22 
23 #include "cellular_data_error.h"
24 #include "core_manager_inner.h"
25 #include "telephony_log_wrapper.h"
26 #include "telephony_permission.h"
27 
28 #include "cellular_data_net_agent.h"
29 #include "cellular_data_dump_helper.h"
30 #include "data_connection_monitor.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 using namespace NetManagerStandard;
35 
36 bool g_registerResult =
37     SystemAbility::MakeAndRegisterAbility(&DelayedRefSingleton<CellularDataService>::GetInstance());
CellularDataService()38 CellularDataService::CellularDataService()
39     : SystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID, true), registerToService_(false),
40     state_(ServiceRunningState::STATE_NOT_START)
41 {}
42 
43 CellularDataService::~CellularDataService() = default;
44 
OnStart()45 void CellularDataService::OnStart()
46 {
47     bindTime_ =
48         std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
49             .count();
50     if (state_ == ServiceRunningState::STATE_RUNNING) {
51         TELEPHONY_LOGE("CellularDataService has already started.");
52         return;
53     }
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_ = AppExecFwk::EventRunner::Create("CellularDataService");
104     if (eventLoop_ == nullptr) {
105         TELEPHONY_LOGE("failed to create EventRunner");
106         return false;
107     }
108     InitModule();
109     eventLoop_->Run();
110     if (!registerToService_) {
111         bool ret = Publish(DelayedRefSingleton<CellularDataService>::GetInstance().AsObject());
112         if (!ret) {
113             TELEPHONY_LOGE("CellularDataService::Init Publish failed!");
114             return false;
115         }
116         registerToService_ = true;
117     }
118     for (const std::pair<const int32_t, std::shared_ptr<CellularDataController>> &it : cellularDataControllers_) {
119         if (it.second != nullptr) {
120             it.second->AsynchronousRegister();
121         } else {
122             TELEPHONY_LOGE("CellularDataController is null");
123         }
124     }
125     return true;
126 }
127 
IsCellularDataEnabled()128 int32_t CellularDataService::IsCellularDataEnabled()
129 {
130     if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
131         return TELEPHONY_ERR_PERMISSION_ERR;
132     }
133     int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
134     if (!CheckParamValid(slotId)) {
135         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
136         return CELLULAR_DATA_INVALID_PARAM;
137     }
138     bool result = cellularDataControllers_[slotId]->IsCellularDataEnabled();
139     return result ? static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_ENABLED) :
140         static_cast<int32_t>(DataSwitchCode::CELLULAR_DATA_DISABLED);
141 }
142 
EnableCellularData(bool enable)143 int32_t CellularDataService::EnableCellularData(bool enable)
144 {
145     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
146         return TELEPHONY_ERR_PERMISSION_ERR;
147     }
148     bool result = false;
149     for (const std::pair<const int32_t, std::shared_ptr<CellularDataController>> &it : cellularDataControllers_) {
150         if (it.second != nullptr) {
151             bool itemResult = it.second->SetCellularDataEnable(enable);
152             if (!result) {
153                 result = itemResult;
154             }
155         } else {
156             TELEPHONY_LOGE("CellularDataController is null");
157         }
158     }
159     return result ? static_cast<int32_t>(DataRespondCode::SET_SUCCESS) :
160         static_cast<int32_t>(DataRespondCode::SET_FAILED);
161 }
162 
GetCellularDataState()163 int32_t CellularDataService::GetCellularDataState()
164 {
165     int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
166     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
167         cellularDataControllers_.find(slotId);
168     if (item == cellularDataControllers_.end() || item->second == nullptr) {
169         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
170         return CELLULAR_DATA_INVALID_PARAM;
171     }
172     int32_t dataState = CellularDataStateAdapter(item->second->GetCellularDataState());
173     DisConnectionReason reason = item->second->GetDisConnectionReason();
174     if (reason == DisConnectionReason::REASON_GSM_AND_CALLING_ONLY && item->second->IsRestrictedMode()) {
175         dataState = static_cast<int32_t>(DataConnectionStatus::DATA_STATE_SUSPENDED);
176     }
177     return dataState;
178 }
179 
IsCellularDataRoamingEnabled(const int32_t slotId)180 int32_t CellularDataService::IsCellularDataRoamingEnabled(const int32_t slotId)
181 {
182     if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
183         return TELEPHONY_ERR_PERMISSION_ERR;
184     }
185     if (!CheckParamValid(slotId)) {
186         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
187         return CELLULAR_DATA_INVALID_PARAM;
188     }
189     bool result = cellularDataControllers_[slotId]->IsCellularDataRoamingEnabled();
190     return result ? static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_ENABLED) :
191         static_cast<int32_t>(RoamingSwitchCode::CELLULAR_DATA_ROAMING_DISABLED);
192 }
193 
EnableCellularDataRoaming(const int32_t slotId,bool enable)194 int32_t CellularDataService::EnableCellularDataRoaming(const int32_t slotId, bool enable)
195 {
196     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
197         return TELEPHONY_ERR_PERMISSION_ERR;
198     }
199     if (!CheckParamValid(slotId)) {
200         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
201         return CELLULAR_DATA_INVALID_PARAM;
202     }
203     bool result = cellularDataControllers_[slotId]->SetCellularDataRoamingEnabled(enable);
204     return result ? static_cast<int32_t>(DataRespondCode::SET_SUCCESS) :
205         static_cast<int32_t>(DataRespondCode::SET_FAILED);
206 }
207 
InitModule()208 void CellularDataService::InitModule()
209 {
210     CellularDataNetAgent &netAgent = CellularDataNetAgent::GetInstance();
211     netAgent.ClearNetSupplier();
212     cellularDataControllers_.clear();
213     std::vector<uint64_t> netCapabilities;
214     netCapabilities.push_back(NetCap::NET_CAPABILITY_INTERNET);
215     netCapabilities.push_back(NetCap::NET_CAPABILITY_MMS);
216     int32_t simNum = CoreManagerInner::GetInstance().GetMaxSimCount();
217     for (int32_t i = 0; i < simNum; ++i) {
218         std::shared_ptr<CellularDataController> cellularDataController =
219         std::make_shared<CellularDataController>(eventLoop_, i);
220         if (cellularDataController == nullptr) {
221             TELEPHONY_LOGE("CellularDataService init module failed cellularDataController is null.");
222             continue;
223         }
224         cellularDataControllers_.insert(
225             std::pair<int32_t, std::shared_ptr<CellularDataController>>(i, cellularDataController));
226         for (uint64_t capability: netCapabilities) {
227             NetSupplier netSupplier = { 0 };
228             netSupplier.supplierId = 0;
229             netSupplier.slotId = i;
230             netSupplier.capability = capability;
231             netAgent.AddNetSupplier(netSupplier);
232         }
233     }
234 }
235 
CheckParamValid(const int32_t slotId)236 bool CellularDataService::CheckParamValid(const int32_t slotId)
237 {
238     if (slotId < 0) {
239         return false;
240     }
241     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
242         cellularDataControllers_.find(slotId);
243     if (item == cellularDataControllers_.end()) {
244         return false;
245     }
246     if (item->second == nullptr) {
247         return false;
248     }
249     return true;
250 }
251 
ReleaseNet(const NetRequest & request)252 int32_t CellularDataService::ReleaseNet(const NetRequest &request)
253 {
254     int32_t slotId = std::stoi(request.ident.substr(IDENT_PREFIX.length()));
255     if (!CheckParamValid(slotId)) {
256         return CELLULAR_DATA_INVALID_PARAM;
257     }
258     bool result = cellularDataControllers_[slotId]->ReleaseNet(request);
259     return static_cast<int32_t>(result ? RequestNetCode::REQUEST_SUCCESS : RequestNetCode::REQUEST_FAILED);
260 }
261 
RequestNet(const NetRequest & request)262 int32_t CellularDataService::RequestNet(const NetRequest &request)
263 {
264     int32_t slotId = std::stoi(request.ident.substr(IDENT_PREFIX.length()));
265     if (!CheckParamValid(slotId)) {
266         return CELLULAR_DATA_INVALID_PARAM;
267     }
268     bool result = cellularDataControllers_[slotId]->RequestNet(request);
269     return static_cast<int32_t>(result ? RequestNetCode::REQUEST_SUCCESS : RequestNetCode::REQUEST_FAILED);
270 }
271 
DispatchEvent(const int32_t slotId,const AppExecFwk::InnerEvent::Pointer & event)272 void CellularDataService::DispatchEvent(const int32_t slotId, const AppExecFwk::InnerEvent::Pointer &event)
273 {
274     if (!CheckParamValid(slotId)) {
275         TELEPHONY_LOGI("dispatch event slotId invalid");
276         return;
277     }
278     cellularDataControllers_[slotId]->ProcessEvent(event);
279 }
280 
UnRegisterAllNetSpecifier()281 void CellularDataService::UnRegisterAllNetSpecifier()
282 {
283     CellularDataNetAgent::GetInstance().UnregisterNetSupplier();
284     CellularDataNetAgent::GetInstance().UnregisterPolicyCallback();
285 }
286 
HandleApnChanged(const int32_t slotId)287 int32_t CellularDataService::HandleApnChanged(const int32_t slotId)
288 {
289     if (!CheckParamValid(slotId)) {
290         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
291         return CELLULAR_DATA_INVALID_PARAM;
292     }
293     bool result = cellularDataControllers_[slotId]->HandleApnChanged();
294     return result ? static_cast<int32_t>(DataRespondCode::SET_SUCCESS) :
295         static_cast<int32_t>(DataRespondCode::SET_FAILED);
296 }
297 
GetDefaultCellularDataSlotId()298 int32_t CellularDataService::GetDefaultCellularDataSlotId()
299 {
300     if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
301         return TELEPHONY_ERR_PERMISSION_ERR;
302     }
303     return CoreManagerInner::GetInstance().GetDefaultCellularDataSlotId();
304 }
305 
SetDefaultCellularDataSlotId(const int32_t slotId)306 int32_t CellularDataService::SetDefaultCellularDataSlotId(const int32_t slotId)
307 {
308     if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
309         return TELEPHONY_ERR_PERMISSION_ERR;
310     }
311     if (!CheckParamValid(slotId)) {
312         return CELLULAR_DATA_INVALID_PARAM;
313     }
314     int32_t formerSlotId = GetDefaultCellularDataSlotId();
315     if (formerSlotId < 0) {
316         TELEPHONY_LOGI("No old card slot id.");
317     }
318     bool result = CoreManagerInner::GetInstance().SetDefaultCellularDataSlotId(slotId);
319     if (!result) {
320         TELEPHONY_LOGE("set slot id fail");
321         return static_cast<int32_t>(DataRespondCode::SET_FAILED);
322     }
323     if (formerSlotId >= 0 && formerSlotId != slotId) {
324         std::map<int32_t, std::shared_ptr<CellularDataController>>::iterator itController
325             = cellularDataControllers_.find(formerSlotId);
326         if (itController != cellularDataControllers_.end() && (itController->second != nullptr)) {
327             itController->second->ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
328         } else {
329             TELEPHONY_LOGI("Not find old slot[%{public}d] object", formerSlotId);
330         }
331     }
332     if (IsCellularDataEnabled()) {
333         cellularDataControllers_[slotId]->EstablishDataConnection();
334     }
335     return static_cast<int32_t>(DataRespondCode::SET_SUCCESS);
336 }
337 
GetCellularDataFlowType()338 int32_t CellularDataService::GetCellularDataFlowType()
339 {
340     int32_t slotId = CellularDataService::GetDefaultCellularDataSlotId();
341     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
342         cellularDataControllers_.find(slotId);
343     if (item == cellularDataControllers_.end() || item->second == nullptr) {
344         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
345         return CELLULAR_DATA_INVALID_PARAM;
346     }
347     DisConnectionReason reason = item->second->GetDisConnectionReason();
348     if (reason == DisConnectionReason::REASON_GSM_AND_CALLING_ONLY && item->second->IsRestrictedMode()) {
349         return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_DORMANT);
350     }
351     int32_t result = item->second->GetCellularDataFlowType();
352     return result;
353 }
354 
GetBindTime()355 std::string CellularDataService::GetBindTime()
356 {
357     std::ostringstream oss;
358     oss << bindTime_;
359     TELEPHONY_LOGI("bindTime :=  %{public}s", oss.str().c_str());
360     return oss.str();
361 }
362 
GetEndTime()363 std::string CellularDataService::GetEndTime()
364 {
365     std::ostringstream oss;
366     oss << endTime_;
367     TELEPHONY_LOGI("endTime :=  %{public}s", oss.str().c_str());
368     return oss.str();
369 }
370 
GetCellularDataSlotIdDump()371 std::string CellularDataService::GetCellularDataSlotIdDump()
372 {
373     std::ostringstream oss;
374     oss << "slotId:" << GetDefaultCellularDataSlotId();
375     return oss.str();
376 }
377 
GetStateMachineCurrentStatusDump()378 std::string CellularDataService::GetStateMachineCurrentStatusDump()
379 {
380     std::ostringstream oss;
381     int32_t slotId = GetDefaultCellularDataSlotId();
382     if (!CheckParamValid(slotId)) {
383         oss << "default slotId: " << slotId;
384         return oss.str();
385     }
386     ApnProfileState statusDefault = cellularDataControllers_[slotId]->GetCellularDataState(DATA_CONTEXT_ROLE_DEFAULT);
387     ApnProfileState statusIms = cellularDataControllers_[slotId]->GetCellularDataState(DATA_CONTEXT_ROLE_IMS);
388     oss << "Default connect state: " << static_cast<int32_t>(statusDefault);
389     oss << "Ims connect state:  " << static_cast<int32_t>(statusIms);
390     return oss.str();
391 }
392 
GetFlowDataInfoDump()393 std::string CellularDataService::GetFlowDataInfoDump()
394 {
395     std::ostringstream oss;
396     int32_t slotId = GetDefaultCellularDataSlotId();
397     if (!CheckParamValid(slotId)) {
398         oss << "default slotId: " << slotId;
399         return oss.str();
400     }
401     int32_t dataFlowInfo = cellularDataControllers_[slotId]->GetCellularDataFlowType();
402     oss << "data flow info: " << dataFlowInfo;
403     return oss.str();
404 }
405 
StrategySwitch(int32_t slotId,bool enable)406 int32_t CellularDataService::StrategySwitch(int32_t slotId, bool enable)
407 {
408     if (!CheckParamValid(slotId)) {
409         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
410         return CELLULAR_DATA_INVALID_PARAM;
411     }
412     int32_t result = cellularDataControllers_[slotId]->SetPolicyDataOn(enable);
413     return result;
414 }
415 
HasInternetCapability(const int32_t slotId,const int32_t cid)416 int32_t CellularDataService::HasInternetCapability(const int32_t slotId, const int32_t cid)
417 {
418     if (!CheckParamValid(slotId)) {
419         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
420         return CELLULAR_DATA_INVALID_PARAM;
421     }
422     bool result = cellularDataControllers_[slotId]->HasInternetCapability(cid);
423     return result ? static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS) :
424         static_cast<int32_t>(RequestNetCode::REQUEST_FAILED);
425 }
426 
ClearCellularDataConnections(const int32_t slotId)427 int32_t CellularDataService::ClearCellularDataConnections(const int32_t slotId)
428 {
429     return ClearAllConnections(slotId, DisConnectionReason::REASON_CLEAR_CONNECTION);
430 }
431 
ClearAllConnections(const int32_t slotId,DisConnectionReason reason)432 int32_t CellularDataService::ClearAllConnections(const int32_t slotId, DisConnectionReason reason)
433 {
434     std::map<int32_t, std::shared_ptr<CellularDataController>>::const_iterator item =
435         cellularDataControllers_.find(slotId);
436     if (item == cellularDataControllers_.end() || item->second == nullptr) {
437         TELEPHONY_LOGE("cellularDataControllers_[%{public}d] is null", slotId);
438         return CELLULAR_DATA_INVALID_PARAM;
439     }
440     bool result = item->second->ClearAllConnections(reason);
441     return result ? static_cast<int32_t>(RequestNetCode::REQUEST_SUCCESS) :
442        static_cast<int32_t>(RequestNetCode::REQUEST_FAILED);
443 }
444 } // namespace Telephony
445 } // namespace OHOS
446