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