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
21namespace OHOS {
22namespace NetManagerStandard {
23NetManagerCenter &NetManagerCenter::GetInstance()
24{
25    static NetManagerCenter gInstance;
26    return gInstance;
27}
28
29int32_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
37int32_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
45int32_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
54int32_t NetManagerCenter::UnregisterNetSupplier(uint32_t supplierId)
55{
56    if (connService_ == nullptr) {
57        return NETMANAGER_ERROR;
58    }
59    return connService_->UnregisterNetSupplier(supplierId);
60}
61
62int32_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
70int32_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
78int32_t NetManagerCenter::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
79{
80    if (connService_ == nullptr) {
81        return NETMANAGER_ERROR;
82    }
83    return connService_->RegisterNetConnCallback(callback);
84}
85
86void NetManagerCenter::RegisterConnService(const sptr<NetConnBaseService> &service)
87{
88    connService_ = service;
89}
90
91int32_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
100int32_t NetManagerCenter::ResetStatsFactory()
101{
102    if (statsService_ == nullptr) {
103        return NETMANAGER_ERROR;
104    }
105    return statsService_->ResetStatsFactory();
106}
107
108void NetManagerCenter::RegisterStatsService(const sptr<NetStatsBaseService> &service)
109{
110    statsService_ = service;
111}
112
113int32_t NetManagerCenter::ResetPolicyFactory()
114{
115    if (policyService_ == nullptr) {
116        return NETMANAGER_ERROR;
117    }
118    return ResetPolicies();
119}
120
121int32_t NetManagerCenter::ResetPolicies()
122{
123    if (policyService_ == nullptr) {
124        return NETMANAGER_ERROR;
125    }
126    return policyService_->ResetPolicies();
127}
128
129void NetManagerCenter::RegisterPolicyService(const sptr<NetPolicyBaseService> &service)
130{
131    policyService_ = service;
132}
133
134int32_t NetManagerCenter::ResetEthernetFactory()
135{
136    if (ethernetService_ == nullptr) {
137        return NETMANAGER_ERROR;
138    }
139    return ethernetService_->ResetEthernetFactory();
140}
141
142void NetManagerCenter::RegisterEthernetService(const sptr<NetEthernetBaseService> &service)
143{
144    ethernetService_ = service;
145}
146
147int32_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
156void NetManagerCenter::RegisterDnsService(const sptr<DnsBaseService> &service)
157{
158    dnsService_ = service;
159}
160
161int32_t NetManagerCenter::RestrictBackgroundChanged(bool isRestrictBackground)
162{
163    if (connService_ == nullptr) {
164        return NETMANAGER_ERROR;
165    }
166    return connService_->RestrictBackgroundChanged(isRestrictBackground);
167}
168
169bool NetManagerCenter::IsUidNetAccess(uint32_t uid, bool metered)
170{
171    if (policyService_ == nullptr) {
172        return NETMANAGER_ERROR;
173    }
174    return IsUidNetAllowed(uid, metered);
175}
176
177bool 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
185int32_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