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