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