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