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