1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#include "net_manager_center.h"
17b1b8bc3fSopenharmony_ci
18b1b8bc3fSopenharmony_ci#include "net_manager_constants.h"
19b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h"
20b1b8bc3fSopenharmony_ci
21b1b8bc3fSopenharmony_cinamespace OHOS {
22b1b8bc3fSopenharmony_cinamespace NetManagerStandard {
23b1b8bc3fSopenharmony_ciNetManagerCenter &NetManagerCenter::GetInstance()
24b1b8bc3fSopenharmony_ci{
25b1b8bc3fSopenharmony_ci    static NetManagerCenter gInstance;
26b1b8bc3fSopenharmony_ci    return gInstance;
27b1b8bc3fSopenharmony_ci}
28b1b8bc3fSopenharmony_ci
29b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames)
30b1b8bc3fSopenharmony_ci{
31b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
32b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
33b1b8bc3fSopenharmony_ci    }
34b1b8bc3fSopenharmony_ci    return connService_->GetIfaceNames(bearerType, ifaceNames);
35b1b8bc3fSopenharmony_ci}
36b1b8bc3fSopenharmony_ci
37b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName)
38b1b8bc3fSopenharmony_ci{
39b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
40b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
41b1b8bc3fSopenharmony_ci    }
42b1b8bc3fSopenharmony_ci    return connService_->GetIfaceNameByType(bearerType, ident, ifaceName);
43b1b8bc3fSopenharmony_ci}
44b1b8bc3fSopenharmony_ci
45b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::RegisterNetSupplier(NetBearType bearerType, const std::string &ident,
46b1b8bc3fSopenharmony_ci                                              const std::set<NetCap> &netCaps, uint32_t &supplierId)
47b1b8bc3fSopenharmony_ci{
48b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
49b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
50b1b8bc3fSopenharmony_ci    }
51b1b8bc3fSopenharmony_ci    return connService_->RegisterNetSupplier(bearerType, ident, netCaps, supplierId);
52b1b8bc3fSopenharmony_ci}
53b1b8bc3fSopenharmony_ci
54b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::UnregisterNetSupplier(uint32_t supplierId)
55b1b8bc3fSopenharmony_ci{
56b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
57b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
58b1b8bc3fSopenharmony_ci    }
59b1b8bc3fSopenharmony_ci    return connService_->UnregisterNetSupplier(supplierId);
60b1b8bc3fSopenharmony_ci}
61b1b8bc3fSopenharmony_ci
62b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo)
63b1b8bc3fSopenharmony_ci{
64b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
65b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
66b1b8bc3fSopenharmony_ci    }
67b1b8bc3fSopenharmony_ci    return connService_->UpdateNetLinkInfo(supplierId, netLinkInfo);
68b1b8bc3fSopenharmony_ci}
69b1b8bc3fSopenharmony_ci
70b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo)
71b1b8bc3fSopenharmony_ci{
72b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
73b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
74b1b8bc3fSopenharmony_ci    }
75b1b8bc3fSopenharmony_ci    return connService_->UpdateNetSupplierInfo(supplierId, netSupplierInfo);
76b1b8bc3fSopenharmony_ci}
77b1b8bc3fSopenharmony_ci
78b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::RegisterNetConnCallback(const sptr<INetConnCallback> &callback)
79b1b8bc3fSopenharmony_ci{
80b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
81b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
82b1b8bc3fSopenharmony_ci    }
83b1b8bc3fSopenharmony_ci    return connService_->RegisterNetConnCallback(callback);
84b1b8bc3fSopenharmony_ci}
85b1b8bc3fSopenharmony_ci
86b1b8bc3fSopenharmony_civoid NetManagerCenter::RegisterConnService(const sptr<NetConnBaseService> &service)
87b1b8bc3fSopenharmony_ci{
88b1b8bc3fSopenharmony_ci    connService_ = service;
89b1b8bc3fSopenharmony_ci}
90b1b8bc3fSopenharmony_ci
91b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::GetIfaceStatsDetail(const std::string &iface, uint64_t start, uint64_t end,
92b1b8bc3fSopenharmony_ci                                              NetStatsInfo &info)
93b1b8bc3fSopenharmony_ci{
94b1b8bc3fSopenharmony_ci    if (statsService_ == nullptr) {
95b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
96b1b8bc3fSopenharmony_ci    }
97b1b8bc3fSopenharmony_ci    return statsService_->GetIfaceStatsDetail(iface, start, end, info);
98b1b8bc3fSopenharmony_ci}
99b1b8bc3fSopenharmony_ci
100b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::ResetStatsFactory()
101b1b8bc3fSopenharmony_ci{
102b1b8bc3fSopenharmony_ci    if (statsService_ == nullptr) {
103b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
104b1b8bc3fSopenharmony_ci    }
105b1b8bc3fSopenharmony_ci    return statsService_->ResetStatsFactory();
106b1b8bc3fSopenharmony_ci}
107b1b8bc3fSopenharmony_ci
108b1b8bc3fSopenharmony_civoid NetManagerCenter::RegisterStatsService(const sptr<NetStatsBaseService> &service)
109b1b8bc3fSopenharmony_ci{
110b1b8bc3fSopenharmony_ci    statsService_ = service;
111b1b8bc3fSopenharmony_ci}
112b1b8bc3fSopenharmony_ci
113b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::ResetPolicyFactory()
114b1b8bc3fSopenharmony_ci{
115b1b8bc3fSopenharmony_ci    if (policyService_ == nullptr) {
116b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
117b1b8bc3fSopenharmony_ci    }
118b1b8bc3fSopenharmony_ci    return ResetPolicies();
119b1b8bc3fSopenharmony_ci}
120b1b8bc3fSopenharmony_ci
121b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::ResetPolicies()
122b1b8bc3fSopenharmony_ci{
123b1b8bc3fSopenharmony_ci    if (policyService_ == nullptr) {
124b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
125b1b8bc3fSopenharmony_ci    }
126b1b8bc3fSopenharmony_ci    return policyService_->ResetPolicies();
127b1b8bc3fSopenharmony_ci}
128b1b8bc3fSopenharmony_ci
129b1b8bc3fSopenharmony_civoid NetManagerCenter::RegisterPolicyService(const sptr<NetPolicyBaseService> &service)
130b1b8bc3fSopenharmony_ci{
131b1b8bc3fSopenharmony_ci    policyService_ = service;
132b1b8bc3fSopenharmony_ci}
133b1b8bc3fSopenharmony_ci
134b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::ResetEthernetFactory()
135b1b8bc3fSopenharmony_ci{
136b1b8bc3fSopenharmony_ci    if (ethernetService_ == nullptr) {
137b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
138b1b8bc3fSopenharmony_ci    }
139b1b8bc3fSopenharmony_ci    return ethernetService_->ResetEthernetFactory();
140b1b8bc3fSopenharmony_ci}
141b1b8bc3fSopenharmony_ci
142b1b8bc3fSopenharmony_civoid NetManagerCenter::RegisterEthernetService(const sptr<NetEthernetBaseService> &service)
143b1b8bc3fSopenharmony_ci{
144b1b8bc3fSopenharmony_ci    ethernetService_ = service;
145b1b8bc3fSopenharmony_ci}
146b1b8bc3fSopenharmony_ci
147b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::GetAddressesByName(const std::string &hostName, int32_t netId,
148b1b8bc3fSopenharmony_ci                                             std::vector<INetAddr> &addrInfo)
149b1b8bc3fSopenharmony_ci{
150b1b8bc3fSopenharmony_ci    if (dnsService_ == nullptr) {
151b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
152b1b8bc3fSopenharmony_ci    }
153b1b8bc3fSopenharmony_ci    return dnsService_->GetAddressesByName(hostName, netId, addrInfo);
154b1b8bc3fSopenharmony_ci}
155b1b8bc3fSopenharmony_ci
156b1b8bc3fSopenharmony_civoid NetManagerCenter::RegisterDnsService(const sptr<DnsBaseService> &service)
157b1b8bc3fSopenharmony_ci{
158b1b8bc3fSopenharmony_ci    dnsService_ = service;
159b1b8bc3fSopenharmony_ci}
160b1b8bc3fSopenharmony_ci
161b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::RestrictBackgroundChanged(bool isRestrictBackground)
162b1b8bc3fSopenharmony_ci{
163b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
164b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
165b1b8bc3fSopenharmony_ci    }
166b1b8bc3fSopenharmony_ci    return connService_->RestrictBackgroundChanged(isRestrictBackground);
167b1b8bc3fSopenharmony_ci}
168b1b8bc3fSopenharmony_ci
169b1b8bc3fSopenharmony_cibool NetManagerCenter::IsUidNetAccess(uint32_t uid, bool metered)
170b1b8bc3fSopenharmony_ci{
171b1b8bc3fSopenharmony_ci    if (policyService_ == nullptr) {
172b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
173b1b8bc3fSopenharmony_ci    }
174b1b8bc3fSopenharmony_ci    return IsUidNetAllowed(uid, metered);
175b1b8bc3fSopenharmony_ci}
176b1b8bc3fSopenharmony_ci
177b1b8bc3fSopenharmony_cibool NetManagerCenter::IsUidNetAllowed(uint32_t uid, bool metered)
178b1b8bc3fSopenharmony_ci{
179b1b8bc3fSopenharmony_ci    if (policyService_ == nullptr) {
180b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
181b1b8bc3fSopenharmony_ci    }
182b1b8bc3fSopenharmony_ci    return policyService_->IsUidNetAllowed(uid, metered);
183b1b8bc3fSopenharmony_ci}
184b1b8bc3fSopenharmony_ci
185b1b8bc3fSopenharmony_ciint32_t NetManagerCenter::RegisterNetFactoryResetCallback(const sptr<INetFactoryResetCallback> &callback)
186b1b8bc3fSopenharmony_ci{
187b1b8bc3fSopenharmony_ci    if (connService_ == nullptr) {
188b1b8bc3fSopenharmony_ci        return NETMANAGER_ERROR;
189b1b8bc3fSopenharmony_ci    }
190b1b8bc3fSopenharmony_ci    NETMGR_LOG_I("NetManagerCenter RegisterNetFactoryResetCallback");
191b1b8bc3fSopenharmony_ci    return connService_->RegisterNetFactoryResetCallback(callback);
192b1b8bc3fSopenharmony_ci}
193b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard
194b1b8bc3fSopenharmony_ci} // namespace OHOS
195