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_net_agent.h"
17
18 #include <cinttypes>
19
20 #include "core_manager_inner.h"
21 #include "net_conn_client.h"
22 #include "net_policy_client.h"
23 #include "telephony_log_wrapper.h"
24
25 namespace OHOS {
26 namespace Telephony {
27 using namespace NetManagerStandard;
28
CellularDataNetAgent()29 CellularDataNetAgent::CellularDataNetAgent()
30 {
31 callBack_ = std::make_unique<NetManagerCallBack>().release();
32 tacticsCallBack_ = std::make_unique<NetManagerTacticsCallBack>().release();
33 if (callBack_ == nullptr || tacticsCallBack_ == nullptr) {
34 TELEPHONY_LOGE("Callback or tacticsCallBack init failed");
35 }
36 }
37
38 CellularDataNetAgent::~CellularDataNetAgent() = default;
39
RegisterNetSupplier(const int32_t slotId)40 bool CellularDataNetAgent::RegisterNetSupplier(const int32_t slotId)
41 {
42 bool flag = false;
43 for (NetSupplier &netSupplier : netSuppliers_) {
44 if (netSupplier.slotId != slotId) {
45 continue;
46 }
47 std::shared_ptr<NetManagerStandard::NetConnClient> netManager = DelayedSingleton<NetConnClient>::GetInstance();
48 if (netManager == nullptr) {
49 TELEPHONY_LOGE("NetConnClient is null");
50 return false;
51 }
52 if (netSupplier.capability > NetCap::NET_CAPABILITY_INTERNAL_DEFAULT) {
53 TELEPHONY_LOGE("capabilities(%{public}" PRIu64 ") not support", netSupplier.capability);
54 continue;
55 }
56 int32_t simId = CoreManagerInner::GetInstance().GetSimId(netSupplier.slotId);
57 if (simId <= INVALID_SIM_ID) {
58 return false;
59 }
60 std::set<NetCap> netCap { static_cast<NetCap>(netSupplier.capability) };
61 uint32_t supplierId = 0;
62 int32_t result = netManager->RegisterNetSupplier(
63 NetBearType::BEARER_CELLULAR, std::string(IDENT_PREFIX) + std::to_string(simId), netCap, supplierId);
64 if (result == NETMANAGER_SUCCESS) {
65 TELEPHONY_LOGI("Register network successful, supplierId[%{public}d]", supplierId);
66 flag = true;
67 netSupplier.supplierId = supplierId;
68 int32_t regCallback = netManager->RegisterNetSupplierCallback(netSupplier.supplierId, callBack_);
69 TELEPHONY_LOGI("Register supplier callback(%{public}d)", regCallback);
70 }
71 }
72 return flag;
73 }
74
UnregisterNetSupplier()75 void CellularDataNetAgent::UnregisterNetSupplier()
76 {
77 for (const NetSupplier &netSupplier : netSuppliers_) {
78 std::shared_ptr<NetManagerStandard::NetConnClient> netManager = DelayedSingleton<NetConnClient>::GetInstance();
79 if (netManager == nullptr) {
80 TELEPHONY_LOGE("NetConnClient is null");
81 return;
82 }
83 int32_t result = netManager->UnregisterNetSupplier(netSupplier.supplierId);
84 TELEPHONY_LOGI("Unregister network result:%{public}d", result);
85 }
86 netSuppliers_.clear();
87 }
88
RegisterPolicyCallback()89 bool CellularDataNetAgent::RegisterPolicyCallback()
90 {
91 std::shared_ptr<NetManagerStandard::NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
92 if (netPolicy == nullptr) {
93 TELEPHONY_LOGE("Net Policy Client is null");
94 return false;
95 }
96 int32_t registerResult = netPolicy->RegisterNetPolicyCallback(tacticsCallBack_);
97 if (registerResult == NETMANAGER_SUCCESS) {
98 TELEPHONY_LOGI("Register NetPolicy Callback successful");
99 return true;
100 }
101 return false;
102 }
103
UnregisterPolicyCallback()104 void CellularDataNetAgent::UnregisterPolicyCallback()
105 {
106 std::shared_ptr<NetManagerStandard::NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
107 if (netPolicy == nullptr) {
108 TELEPHONY_LOGE("Net Policy Client is null");
109 return;
110 }
111 int32_t registerResult = netPolicy->UnregisterNetPolicyCallback(tacticsCallBack_);
112 TELEPHONY_LOGI("Register NetPolicy Callback is :%{public}d", registerResult);
113 }
114
UpdateNetSupplierInfo(int32_t supplierId,sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)115 void CellularDataNetAgent::UpdateNetSupplierInfo(
116 int32_t supplierId, sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
117 {
118 std::shared_ptr<NetManagerStandard::NetConnClient> netManager = DelayedSingleton<NetConnClient>::GetInstance();
119 if (netManager == nullptr) {
120 TELEPHONY_LOGE("NetConnClient is null");
121 return;
122 }
123 int32_t result = netManager->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
124 TELEPHONY_LOGI("Update network result:%{public}d", result);
125 }
126
UpdateNetLinkInfo(int32_t supplierId,sptr<NetManagerStandard::NetLinkInfo> & netLinkInfo)127 void CellularDataNetAgent::UpdateNetLinkInfo(int32_t supplierId, sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo)
128 {
129 std::shared_ptr<NetManagerStandard::NetConnClient> netManager = DelayedSingleton<NetConnClient>::GetInstance();
130 if (netManager == nullptr) {
131 TELEPHONY_LOGE("NetConnClient is null");
132 return;
133 }
134 int32_t result = netManager->UpdateNetLinkInfo(supplierId, netLinkInfo);
135 TELEPHONY_LOGI("result:%{public}d", result);
136 }
137
AddNetSupplier(const NetSupplier & netSupplier)138 void CellularDataNetAgent::AddNetSupplier(const NetSupplier &netSupplier)
139 {
140 netSuppliers_.push_back(netSupplier);
141 }
142
ClearNetSupplier()143 void CellularDataNetAgent::ClearNetSupplier()
144 {
145 netSuppliers_.clear();
146 }
147
GetSupplierId(const int32_t slotId,uint64_t capability) const148 int32_t CellularDataNetAgent::GetSupplierId(const int32_t slotId, uint64_t capability) const
149 {
150 for (const NetSupplier &netSupplier : netSuppliers_) {
151 if (netSupplier.slotId == slotId && netSupplier.capability == capability) {
152 TELEPHONY_LOGI(
153 "find supplierId %{public}d capability:%{public}" PRIu64 "", netSupplier.supplierId, capability);
154 return netSupplier.supplierId;
155 }
156 }
157 return 0;
158 }
159 } // namespace Telephony
160 } // namespace OHOS
161