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 : public std::enable_shared_from_this<EthernetManagement> { 33 private: 34 class EhternetDhcpNotifyCallback : public EthernetDhcpCallback { 35 public: EhternetDhcpNotifyCallback(EthernetManagement & ethernetManagement)36 EhternetDhcpNotifyCallback(EthernetManagement ðernetManagement) : ethernetManagement_(ethernetManagement) {} 37 int32_t OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult) override; 38 39 private: 40 EthernetManagement ðernetManagement_; 41 }; 42 43 private: 44 class DevInterfaceStateCallback : public NetsysControllerCallback { 45 public: DevInterfaceStateCallback(EthernetManagement & ethernetManagement)46 DevInterfaceStateCallback(EthernetManagement ðernetManagement) : 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 ðernetManagement_; 60 }; 61 62 public: 63 EthernetManagement(); 64 ~EthernetManagement(); 65 void Init(); 66 int32_t UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult); 67 void UpdateInterfaceState(const std::string &dev, bool up); 68 int32_t UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg); 69 int32_t GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig); 70 int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus); 71 int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces); 72 int32_t ResetFactory(); 73 void GetDumpInfo(std::string &info); 74 void DevInterfaceAdd(const std::string &devName); 75 void DevInterfaceRemove(const std::string &devName); 76 77 private: 78 void StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState); 79 void StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState); 80 void StartSetDevUpThd(); 81 bool IsIfaceLinkUp(const std::string &iface); 82 bool ModeInputCheck(IPSetMode origin, IPSetMode input); 83 84 private: 85 std::map<std::string, std::set<NetCap>> devCaps_; 86 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs_; 87 std::map<std::string, sptr<DevInterfaceState>> devs_; 88 std::unique_ptr<EthernetConfiguration> ethConfiguration_ = nullptr; 89 std::unique_ptr<EthernetDhcpController> ethDhcpController_ = nullptr; 90 sptr<EhternetDhcpNotifyCallback> ethDhcpNotifyCallback_ = nullptr; 91 sptr<NetsysControllerCallback> ethDevInterfaceStateCallback_ = nullptr; 92 std::map<std::string, sptr<StaticConfiguration>> netLinkConfigs_; 93 std::unique_ptr<EthernetLanManagement> ethLanManageMent_ = nullptr; 94 std::mutex mutex_; 95 }; 96 } // namespace NetManagerStandard 97 } // namespace OHOS 98 #endif // ETHERNET_MANAGEMENT_H 99