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 auto& netManager = NetConnClient::GetInstance();
48 if (netSupplier.capability > NetCap::NET_CAPABILITY_INTERNAL_DEFAULT) {
49 TELEPHONY_LOGE("capabilities(%{public}" PRIu64 ") not support", netSupplier.capability);
50 continue;
51 }
52 int32_t simId = CoreManagerInner::GetInstance().GetSimId(netSupplier.slotId);
53 if (simId <= INVALID_SIM_ID) {
54 return false;
55 }
56 std::set<NetCap> netCap { static_cast<NetCap>(netSupplier.capability) };
57 uint32_t supplierId = 0;
58 int32_t result = netManager.RegisterNetSupplier(
59 NetBearType::BEARER_CELLULAR, std::string(IDENT_PREFIX) + std::to_string(simId), netCap, supplierId);
60 TELEPHONY_LOGI(
61 "Slot%{public}d Register network supplierId: %{public}d,result:%{public}d", slotId, supplierId, result);
62 if (result == NETMANAGER_SUCCESS) {
63 flag = true;
64 netSupplier.supplierId = supplierId;
65 int32_t regCallback = netManager.RegisterNetSupplierCallback(netSupplier.supplierId, callBack_);
66 TELEPHONY_LOGI("Register supplier callback(%{public}d)", regCallback);
67 }
68 }
69 return flag;
70 }
71
UnregisterNetSupplier(const int32_t slotId)72 void CellularDataNetAgent::UnregisterNetSupplier(const int32_t slotId)
73 {
74 for (const NetSupplier &netSupplier : netSuppliers_) {
75 if (netSupplier.slotId != slotId) {
76 continue;
77 }
78 auto& netManager = NetConnClient::GetInstance();
79 int32_t result = netManager.UnregisterNetSupplier(netSupplier.supplierId);
80 TELEPHONY_LOGI("Slot%{public}d unregister network result:%{public}d", slotId, result);
81 }
82 }
83
UnregisterAllNetSupplier()84 void CellularDataNetAgent::UnregisterAllNetSupplier()
85 {
86 for (const NetSupplier &netSupplier : netSuppliers_) {
87 int32_t result = NetConnClient::GetInstance().UnregisterNetSupplier(netSupplier.supplierId);
88 TELEPHONY_LOGI("Unregister network result:%{public}d", result);
89 }
90 netSuppliers_.clear();
91 }
92
RegisterPolicyCallback()93 bool CellularDataNetAgent::RegisterPolicyCallback()
94 {
95 std::shared_ptr<NetManagerStandard::NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
96 if (netPolicy == nullptr) {
97 TELEPHONY_LOGE("Net Policy Client is null");
98 return false;
99 }
100 int32_t registerResult = netPolicy->RegisterNetPolicyCallback(tacticsCallBack_);
101 if (registerResult == NETMANAGER_SUCCESS) {
102 TELEPHONY_LOGI("Register NetPolicy Callback successful");
103 return true;
104 }
105 return false;
106 }
107
UnregisterPolicyCallback()108 void CellularDataNetAgent::UnregisterPolicyCallback()
109 {
110 std::shared_ptr<NetManagerStandard::NetPolicyClient> netPolicy = DelayedSingleton<NetPolicyClient>::GetInstance();
111 if (netPolicy == nullptr) {
112 TELEPHONY_LOGE("Net Policy Client is null");
113 return;
114 }
115 int32_t registerResult = netPolicy->UnregisterNetPolicyCallback(tacticsCallBack_);
116 TELEPHONY_LOGI("Unregister NetPolicy Callback is :%{public}d", registerResult);
117 }
118
UpdateNetSupplierInfo(int32_t supplierId,sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)119 void CellularDataNetAgent::UpdateNetSupplierInfo(
120 int32_t supplierId, sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
121 {
122 int32_t result = NetConnClient::GetInstance().UpdateNetSupplierInfo(supplierId, netSupplierInfo);
123 TELEPHONY_LOGI("Update network result:%{public}d", result);
124 }
125
UpdateNetLinkInfo(int32_t supplierId,sptr<NetManagerStandard::NetLinkInfo> & netLinkInfo)126 void CellularDataNetAgent::UpdateNetLinkInfo(int32_t supplierId, sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo)
127 {
128 int32_t result = NetConnClient::GetInstance().UpdateNetLinkInfo(supplierId, netLinkInfo);
129 TELEPHONY_LOGI("result:%{public}d", result);
130 }
131
AddNetSupplier(const NetSupplier & netSupplier)132 void CellularDataNetAgent::AddNetSupplier(const NetSupplier &netSupplier)
133 {
134 netSuppliers_.push_back(netSupplier);
135 }
136
ClearNetSupplier()137 void CellularDataNetAgent::ClearNetSupplier()
138 {
139 netSuppliers_.clear();
140 }
141
GetSupplierId(const int32_t slotId,uint64_t capability) const142 int32_t CellularDataNetAgent::GetSupplierId(const int32_t slotId, uint64_t capability) const
143 {
144 for (const NetSupplier &netSupplier : netSuppliers_) {
145 if (netSupplier.slotId == slotId && netSupplier.capability == capability) {
146 TELEPHONY_LOGI(
147 "find supplierId %{public}d capability:%{public}" PRIu64 "", netSupplier.supplierId, capability);
148 return netSupplier.supplierId;
149 }
150 }
151 return 0;
152 }
153 } // namespace Telephony
154 } // namespace OHOS
155