• 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 
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 
UnregisterAllNetSupplier()103 void CellularDataNetAgent::UnregisterAllNetSupplier()
104 {
105     for (const NetSupplier &netSupplier : netSuppliers_) {
106         int32_t result = NetConnClient::GetInstance().UnregisterNetSupplier(netSupplier.supplierId);
107         TELEPHONY_LOGI("Unregister network result:%{public}d", result);
108     }
109     netSuppliers_.clear();
110 }
111 
RegisterPolicyCallback()112 bool CellularDataNetAgent::RegisterPolicyCallback()
113 {
114     std::shared_ptr<NetManagerStandard::NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
115     if (netPolicy == nullptr) {
116         TELEPHONY_LOGE("Net Policy Client is null");
117         return false;
118     }
119     int32_t registerResult = netPolicy->RegisterNetPolicyCallback(tacticsCallBack_);
120     if (registerResult == NETMANAGER_SUCCESS) {
121         TELEPHONY_LOGI("Register NetPolicy Callback successful");
122         return true;
123     }
124     return false;
125 }
126 
UnregisterPolicyCallback()127 void CellularDataNetAgent::UnregisterPolicyCallback()
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;
133     }
134     int32_t registerResult = netPolicy->UnregisterNetPolicyCallback(tacticsCallBack_);
135     TELEPHONY_LOGI("Unregister NetPolicy Callback is :%{public}d", registerResult);
136 }
137 
UpdateNetSupplierInfo(int32_t supplierId,sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)138 void CellularDataNetAgent::UpdateNetSupplierInfo(
139     int32_t supplierId, sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
140 {
141     int32_t result = NetConnClient::GetInstance().UpdateNetSupplierInfo(supplierId, netSupplierInfo);
142     TELEPHONY_LOGI("Update network result:%{public}d", result);
143 }
144 
UpdateNetLinkInfo(int32_t supplierId,sptr<NetManagerStandard::NetLinkInfo> & netLinkInfo)145 void CellularDataNetAgent::UpdateNetLinkInfo(int32_t supplierId, sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo)
146 {
147     int32_t result = NetConnClient::GetInstance().UpdateNetLinkInfo(supplierId, netLinkInfo);
148     TELEPHONY_LOGI("result:%{public}d", result);
149 }
150 
AddNetSupplier(const NetSupplier & netSupplier)151 void CellularDataNetAgent::AddNetSupplier(const NetSupplier &netSupplier)
152 {
153     netSuppliers_.push_back(netSupplier);
154 }
155 
ClearNetSupplier()156 void CellularDataNetAgent::ClearNetSupplier()
157 {
158     netSuppliers_.clear();
159 }
160 
GetSupplierId(const int32_t slotId,uint64_t capability) const161 int32_t CellularDataNetAgent::GetSupplierId(const int32_t slotId, uint64_t capability) const
162 {
163     for (const NetSupplier &netSupplier : netSuppliers_) {
164         if (netSupplier.slotId == slotId && netSupplier.capability == capability) {
165             TELEPHONY_LOGI(
166                 "find supplierId %{public}d capability:%{public}" PRIu64 "", netSupplier.supplierId, capability);
167             return netSupplier.supplierId;
168         }
169     }
170     return 0;
171 }
172 
RegisterSlotType(int32_t supplierId,int32_t radioTech)173 void CellularDataNetAgent::RegisterSlotType(int32_t supplierId, int32_t radioTech)
174 {
175     int32_t result = NetConnClient::GetInstance().RegisterSlotType(supplierId, radioTech);
176     TELEPHONY_LOGI("result:%{public}d", result);
177 }
178 } // namespace Telephony
179 } // namespace OHOS
180