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