• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_device_info.h"
26 #include "ethernet_management.h"
27 #include "ethernet_service_common.h"
28 #include "ethernet_service_stub.h"
29 #include "event_handler.h"
30 #include "netsys_controller_callback.h"
31 #include "net_eap_handler.h"
32 #include "refbase.h"
33 #include "singleton.h"
34 #include "system_ability.h"
35 #include "ffrt.h"
36 
37 namespace OHOS {
38 namespace NetManagerStandard {
39 class EthernetService : public SystemAbility,
40                         public EthernetServiceStub,
41                         public std::enable_shared_from_this<EthernetService> {
42     DECLARE_DELAYED_SINGLETON(EthernetService)
DECLARE_SYSTEM_ABILITY(EthernetService)43     DECLARE_SYSTEM_ABILITY(EthernetService)
44 
45     class GlobalInterfaceStateCallback : public NetsysControllerCallback {
46     public:
47         explicit GlobalInterfaceStateCallback(EthernetService &ethService) : ethernetService_(ethService) {}
48         ~GlobalInterfaceStateCallback() = default;
49         int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
50                                           int scope) override;
51         int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
52                                           int scope) override;
53         int32_t OnInterfaceAdded(const std::string &iface) override;
54         int32_t OnInterfaceRemoved(const std::string &iface) override;
55         int32_t OnInterfaceChanged(const std::string &iface, bool up) override;
56         int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
57         int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
58                                const std::string &ifName) override;
59         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
60         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
61 
62     private:
63         EthernetService &ethernetService_;
64     };
65 
66 public:
67     void OnStart() override;
68     void OnStop() override;
69     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
70 
71     int32_t GetMacAddress(std::vector<MacAddressInfo> &macAddrList) override;
72     int32_t SetIfaceConfig(const std::string &iface, const sptr<InterfaceConfiguration> &ic) override;
73     int32_t GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig) override;
74     int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus) override;
75     int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces) override;
76     int32_t ResetFactory() override;
77     int32_t RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
78     int32_t UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
79     int32_t SetInterfaceUp(const std::string &iface) override;
80     int32_t SetInterfaceDown(const std::string &iface) override;
81     int32_t GetInterfaceConfig(const std::string &iface, ConfigurationParcelIpc &cfgIpc) override;
82     int32_t SetInterfaceConfig(const std::string &iface, const ConfigurationParcelIpc &cfgIpc) override;
83     int32_t RegCustomEapHandler(int netType, const std::string &regCmd,
84         const sptr<INetEapPostbackCallback> &callback) override;
85     int32_t ReplyCustomEapData(int result, const sptr<EapData> &eapData) override;
86     int32_t RegisterCustomEapCallback(int netType, const sptr<INetRegisterEapCallback> &callback) override;
87     int32_t UnRegisterCustomEapCallback(int netType, const sptr<INetRegisterEapCallback> &callback) override;
88     int32_t NotifyWpaEapInterceptInfo(int netType, const sptr<EapData> &eapData) override;
89     int32_t GetDeviceInformation(std::vector<EthernetDeviceInfo> &deviceInfoList) override;
90     int32_t StartEthEap(int32_t netId, const EthEapProfile& profile) override;
91     int32_t LogOffEthEap(int32_t netId) override;
92 
93 protected:
94     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
95 
96 private:
97     enum ServiceRunningState {
98         STATE_STOPPED = 0,
99         STATE_RUNNING,
100     };
101 
102     using OnFunctionT = std::function<void(const sptr<InterfaceStateCallback> &callback)>;
103 
104     bool Init();
105     void InitManagement();
106 
107     int32_t RegisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
108     int32_t UnregisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
109     void NotifyMonitorIfaceCallbackAsync(OnFunctionT onFunction);
110 
111 private:
112     ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
113     bool registerToService_ = false;
114     uint16_t dependentServiceState_ = 0;
115     EthernetManagement& ethManagement_ = EthernetManagement::GetInstance();
116     sptr<EthernetServiceCommon> serviceComm_ = nullptr;
117     sptr<NetsysControllerCallback> interfaceStateCallback_ = nullptr;
118     std::vector<sptr<InterfaceStateCallback>> monitorIfaceCallbacks_;
119     std::shared_ptr<ffrt::queue> ethernetServiceFfrtQueue_ = nullptr;
120 };
121 } // namespace NetManagerStandard
122 } // namespace OHOS
123 #endif // ETHERNET_SERVICE_H
124