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_SERVICE_H 17 #define ETHERNET_SERVICE_H 18 19 #include <cstdint> 20 #include <iosfwd> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "ethernet_management.h" 26 #include "ethernet_service_common.h" 27 #include "ethernet_service_stub.h" 28 #include "event_handler.h" 29 #include "netsys_controller_callback.h" 30 #include "refbase.h" 31 #include "singleton.h" 32 #include "system_ability.h" 33 #include "ffrt.h" 34 35 namespace OHOS { 36 namespace NetManagerStandard { 37 class EthernetService : public SystemAbility, 38 public EthernetServiceStub, 39 public std::enable_shared_from_this<EthernetService> { 40 DECLARE_DELAYED_SINGLETON(EthernetService) DECLARE_SYSTEM_ABILITY(EthernetService)41 DECLARE_SYSTEM_ABILITY(EthernetService) 42 43 class GlobalInterfaceStateCallback : public NetsysControllerCallback { 44 public: 45 explicit GlobalInterfaceStateCallback(EthernetService ðService) : ethernetService_(ethService) {} 46 ~GlobalInterfaceStateCallback() = default; 47 int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags, 48 int scope) override; 49 int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags, 50 int scope) 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 &iface, bool up) override; 54 int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override; 55 int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway, 56 const std::string &ifName) 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 EthernetService ðernetService_; 62 }; 63 64 public: 65 void OnStart() override; 66 void OnStop() override; 67 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 68 69 int32_t GetMacAddress(std::vector<MacAddressInfo> &macAddrList) override; 70 int32_t SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic) override; 71 int32_t GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig) override; 72 int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus) override; 73 int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces) override; 74 int32_t ResetFactory() override; 75 int32_t RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override; 76 int32_t UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override; 77 int32_t SetInterfaceUp(const std::string &iface) override; 78 int32_t SetInterfaceDown(const std::string &iface) override; 79 int32_t GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &config) override; 80 int32_t SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg) override; 81 82 protected: 83 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 84 85 private: 86 enum ServiceRunningState { 87 STATE_STOPPED = 0, 88 STATE_RUNNING, 89 }; 90 91 using OnFunctionT = std::function<void(const sptr<InterfaceStateCallback> &callback)>; 92 93 bool Init(); 94 void InitManagement(); 95 96 int32_t RegisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback); 97 int32_t UnregisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback); 98 void NotifyMonitorIfaceCallbackAsync(OnFunctionT onFunction); 99 100 private: 101 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 102 bool registerToService_ = false; 103 uint16_t dependentServiceState_ = 0; 104 EthernetManagement& ethManagement_ = EthernetManagement::GetInstance(); 105 sptr<EthernetServiceCommon> serviceComm_ = nullptr; 106 sptr<NetsysControllerCallback> interfaceStateCallback_ = nullptr; 107 std::vector<sptr<InterfaceStateCallback>> monitorIfaceCallbacks_; 108 std::shared_ptr<ffrt::queue> ethernetServiceFfrtQueue_ = nullptr; 109 }; 110 } // namespace NetManagerStandard 111 } // namespace OHOS 112 #endif // ETHERNET_SERVICE_H 113