• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ETHERNET_MANAGEMENT_H
17 #define ETHERNET_MANAGEMENT_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "dev_interface_state.h"
23 #include "ethernet_configuration.h"
24 #include "ethernet_dhcp_controller.h"
25 #include "iservice_registry.h"
26 #include "netsys_controller_callback.h"
27 #include "system_ability_definition.h"
28 #include "ethernet_lan_management.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 class EthernetManagement {
33 private:
34     class EhternetDhcpNotifyCallback : public EthernetDhcpCallback {
35     public:
EhternetDhcpNotifyCallback(EthernetManagement & ethernetManagement)36         EhternetDhcpNotifyCallback(EthernetManagement &ethernetManagement) : ethernetManagement_(ethernetManagement) {}
37         int32_t OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult) override;
38 
39     private:
40         EthernetManagement &ethernetManagement_;
41     };
42 
43 private:
44     class DevInterfaceStateCallback : public NetsysControllerCallback {
45     public:
DevInterfaceStateCallback(EthernetManagement & ethernetManagement)46         DevInterfaceStateCallback(EthernetManagement &ethernetManagement) : ethernetManagement_(ethernetManagement) {}
47         ~DevInterfaceStateCallback() = default;
48         int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int) override;
49         int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int) override;
50         int32_t OnInterfaceAdded(const std::string &iface) override;
51         int32_t OnInterfaceRemoved(const std::string &iface) override;
52         int32_t OnInterfaceChanged(const std::string &, bool) override;
53         int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
54         int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) override;
55         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
56         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
57 
58     private:
59         EthernetManagement &ethernetManagement_;
60     };
61 
62 public:
63     static EthernetManagement& GetInstance();
64     void Init();
65     int32_t UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult);
66     void UpdateInterfaceState(const std::string &dev, bool up);
67     int32_t UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg);
68     int32_t GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig);
69     int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus);
70     int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces);
71     int32_t ResetFactory();
72     void GetDumpInfo(std::string &info);
73     void DevInterfaceAdd(const std::string &devName);
74     void DevInterfaceRemove(const std::string &devName);
75 
76 private:
77     EthernetManagement();
78     ~EthernetManagement();
79     EthernetManagement(const EthernetManagement&) = delete;
80     EthernetManagement& operator=(const EthernetManagement&) = delete;
81     void StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState);
82     void StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState);
83     void StartSetDevUpThd();
84     bool IsIfaceLinkUp(const std::string &iface);
85     bool ModeInputCheck(IPSetMode origin, IPSetMode input);
86 
87 private:
88     std::map<std::string, std::set<NetCap>> devCaps_;
89     std::map<std::string, sptr<InterfaceConfiguration>> devCfgs_;
90     std::map<std::string, sptr<DevInterfaceState>> devs_;
91     std::unique_ptr<EthernetConfiguration> ethConfiguration_ = nullptr;
92     std::unique_ptr<EthernetDhcpController> ethDhcpController_ = nullptr;
93     sptr<EhternetDhcpNotifyCallback> ethDhcpNotifyCallback_ = nullptr;
94     sptr<NetsysControllerCallback> ethDevInterfaceStateCallback_ = nullptr;
95     std::map<std::string, sptr<StaticConfiguration>> netLinkConfigs_;
96     std::unique_ptr<EthernetLanManagement> ethLanManageMent_ = nullptr;
97     std::mutex mutex_;
98 };
99 } // namespace NetManagerStandard
100 } // namespace OHOS
101 #endif // ETHERNET_MANAGEMENT_H
102