1 /*
2 * Copyright (c) 2021-2023 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 "net_manager_center.h"
17
18 #include "net_manager_constants.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
GetInstance()22 NetManagerCenter &NetManagerCenter::GetInstance()
23 {
24 static NetManagerCenter gInstance;
25 return gInstance;
26 }
27
GetIfaceNames(NetBearType bearerType,std::list<std::string> & ifaceNames)28 int32_t NetManagerCenter::GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames)
29 {
30 if (connService_ == nullptr) {
31 return NETMANAGER_ERROR;
32 }
33 return connService_->GetIfaceNames(bearerType, ifaceNames);
34 }
35
GetIfaceNameByType(NetBearType bearerType,const std::string & ident,std::string & ifaceName)36 int32_t NetManagerCenter::GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName)
37 {
38 if (connService_ == nullptr) {
39 return NETMANAGER_ERROR;
40 }
41 return connService_->GetIfaceNameByType(bearerType, ident, ifaceName);
42 }
43
RegisterNetSupplier(NetBearType bearerType,const std::string & ident,const std::set<NetCap> & netCaps,uint32_t & supplierId)44 int32_t NetManagerCenter::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
45 const std::set<NetCap> &netCaps, uint32_t &supplierId)
46 {
47 if (connService_ == nullptr) {
48 return NETMANAGER_ERROR;
49 }
50 return connService_->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
51 }
52
UnregisterNetSupplier(uint32_t supplierId)53 int32_t NetManagerCenter::UnregisterNetSupplier(uint32_t supplierId)
54 {
55 if (connService_ == nullptr) {
56 return NETMANAGER_ERROR;
57 }
58 return connService_->UnregisterNetSupplier(supplierId);
59 }
60
UpdateNetLinkInfo(uint32_t supplierId,const sptr<NetLinkInfo> & netLinkInfo)61 int32_t NetManagerCenter::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
62 {
63 if (connService_ == nullptr) {
64 return NETMANAGER_ERROR;
65 }
66 return connService_->UpdateNetLinkInfo(supplierId, netLinkInfo);
67 }
68
UpdateNetSupplierInfo(uint32_t supplierId,const sptr<NetSupplierInfo> & netSupplierInfo)69 int32_t NetManagerCenter::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
70 {
71 if (connService_ == nullptr) {
72 return NETMANAGER_ERROR;
73 }
74 return connService_->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
75 }
76
RegisterNetConnCallback(const sptr<INetConnCallback> & callback)77 int32_t NetManagerCenter::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
78 {
79 if (connService_ == nullptr) {
80 return NETMANAGER_ERROR;
81 }
82 return connService_->RegisterNetConnCallback(callback);
83 }
84
RegisterConnService(const sptr<NetConnBaseService> & service)85 void NetManagerCenter::RegisterConnService(const sptr<NetConnBaseService> &service)
86 {
87 connService_ = service;
88 }
89
GetIfaceStatsDetail(const std::string & iface,uint64_t start,uint64_t end,NetStatsInfo & info)90 int32_t NetManagerCenter::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
91 NetStatsInfo &info)
92 {
93 if (statsService_ == nullptr) {
94 return NETMANAGER_ERROR;
95 }
96 return statsService_->GetIfaceStatsDetail(iface, start, end, info);
97 }
98
ResetStatsFactory()99 int32_t NetManagerCenter::ResetStatsFactory()
100 {
101 if (statsService_ == nullptr) {
102 return NETMANAGER_ERROR;
103 }
104 return statsService_->ResetStatsFactory();
105 }
106
RegisterStatsService(const sptr<NetStatsBaseService> & service)107 void NetManagerCenter::RegisterStatsService(const sptr<NetStatsBaseService> &service)
108 {
109 statsService_ = service;
110 }
111
ResetPolicyFactory()112 int32_t NetManagerCenter::ResetPolicyFactory()
113 {
114 if (policyService_ == nullptr) {
115 return NETMANAGER_ERROR;
116 }
117 return ResetPolicies();
118 }
119
ResetPolicies()120 int32_t NetManagerCenter::ResetPolicies()
121 {
122 if (policyService_ == nullptr) {
123 return NETMANAGER_ERROR;
124 }
125 return policyService_->ResetPolicies();
126 }
127
RegisterPolicyService(const sptr<NetPolicyBaseService> & service)128 void NetManagerCenter::RegisterPolicyService(const sptr<NetPolicyBaseService> &service)
129 {
130 policyService_ = service;
131 }
132
ResetEthernetFactory()133 int32_t NetManagerCenter::ResetEthernetFactory()
134 {
135 if (ethernetService_ == nullptr) {
136 return NETMANAGER_ERROR;
137 }
138 return ethernetService_->ResetEthernetFactory();
139 }
140
RegisterEthernetService(const sptr<NetEthernetBaseService> & service)141 void NetManagerCenter::RegisterEthernetService(const sptr<NetEthernetBaseService> &service)
142 {
143 ethernetService_ = service;
144 }
145
GetAddressesByName(const std::string & hostName,int32_t netId,std::vector<INetAddr> & addrInfo)146 int32_t NetManagerCenter::GetAddressesByName(const std::string &hostName, int32_t netId,
147 std::vector<INetAddr> &addrInfo)
148 {
149 if (dnsService_ == nullptr) {
150 return NETMANAGER_ERROR;
151 }
152 return dnsService_->GetAddressesByName(hostName, netId, addrInfo);
153 }
154
RegisterDnsService(const sptr<DnsBaseService> & service)155 void NetManagerCenter::RegisterDnsService(const sptr<DnsBaseService> &service)
156 {
157 dnsService_ = service;
158 }
159
RestrictBackgroundChanged(bool isRestrictBackground)160 int32_t NetManagerCenter::RestrictBackgroundChanged(bool isRestrictBackground)
161 {
162 if (connService_ == nullptr) {
163 return NETMANAGER_ERROR;
164 }
165 return connService_->RestrictBackgroundChanged(isRestrictBackground);
166 }
167
IsUidNetAccess(uint32_t uid,bool metered)168 bool NetManagerCenter::IsUidNetAccess(uint32_t uid, bool metered)
169 {
170 if (policyService_ == nullptr) {
171 return NETMANAGER_ERROR;
172 }
173 return IsUidNetAllowed(uid, metered);
174 }
175
IsUidNetAllowed(uint32_t uid,bool metered)176 bool NetManagerCenter::IsUidNetAllowed(uint32_t uid, bool metered)
177 {
178 if (policyService_ == nullptr) {
179 return NETMANAGER_ERROR;
180 }
181 return policyService_->IsUidNetAllowed(uid, metered);
182 }
183 } // namespace NetManagerStandard
184 } // namespace OHOS
185