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