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