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 29 namespace OHOS { 30 namespace NetManagerStandard { 31 class EthernetManagement : public std::enable_shared_from_this<EthernetManagement> { 32 class EhternetDhcpNotifyCallback : public EthernetDhcpCallback { 33 public: EhternetDhcpNotifyCallback(EthernetManagement & ethernetManagement)34 EhternetDhcpNotifyCallback(EthernetManagement ðernetManagement) : ethernetManagement_(ethernetManagement) {} 35 int32_t OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult) override; 36 37 private: 38 EthernetManagement ðernetManagement_; 39 }; 40 41 private: 42 class DevInterfaceStateCallback : public NetsysControllerCallback { 43 public: DevInterfaceStateCallback(EthernetManagement & ethernetManagement)44 DevInterfaceStateCallback(EthernetManagement ðernetManagement) : ethernetManagement_(ethernetManagement) {} 45 ~DevInterfaceStateCallback() = default; 46 int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int) override; 47 int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int) override; 48 int32_t OnInterfaceAdded(const std::string &iface) override; 49 int32_t OnInterfaceRemoved(const std::string &iface) override; 50 int32_t OnInterfaceChanged(const std::string &, bool) override; 51 int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override; 52 int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) override; 53 int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override; 54 int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override; 55 56 private: 57 EthernetManagement ðernetManagement_; 58 }; 59 60 public: 61 EthernetManagement(); 62 ~EthernetManagement(); 63 void Init(); 64 int32_t UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult); 65 void UpdateInterfaceState(const std::string &dev, bool up); 66 int32_t UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg); 67 int32_t GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig); 68 int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus); 69 int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces); 70 int32_t ResetFactory(); 71 void GetDumpInfo(std::string &info); 72 void DevInterfaceAdd(const std::string &devName); 73 void DevInterfaceRemove(const std::string &devName); 74 75 private: 76 void StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState); 77 void StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState); 78 void StartSetDevUpThd(); 79 bool IsIfaceLinkUp(const std::string &iface); 80 81 private: 82 std::map<std::string, std::set<NetCap>> devCaps_; 83 std::map<std::string, sptr<InterfaceConfiguration>> devCfgs_; 84 std::map<std::string, sptr<DevInterfaceState>> devs_; 85 std::unique_ptr<EthernetConfiguration> ethConfiguration_ = nullptr; 86 std::unique_ptr<EthernetDhcpController> ethDhcpController_ = nullptr; 87 sptr<EhternetDhcpNotifyCallback> ethDhcpNotifyCallback_ = nullptr; 88 sptr<NetsysControllerCallback> ethDevInterfaceStateCallback_ = nullptr; 89 std::map<std::string, sptr<StaticConfiguration>> netLinkConfigs_; 90 std::mutex mutex_; 91 }; 92 } // namespace NetManagerStandard 93 } // namespace OHOS 94 #endif // ETHERNET_MANAGEMENT_H 95