• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 class EthernetService : public SystemAbility,
37                         public EthernetServiceStub,
38                         public std::enable_shared_from_this<EthernetService> {
39     DECLARE_DELAYED_SINGLETON(EthernetService)
DECLARE_SYSTEM_ABILITY(EthernetService)40     DECLARE_SYSTEM_ABILITY(EthernetService)
41 
42     class GlobalInterfaceStateCallback : public NetsysControllerCallback {
43     public:
44         explicit GlobalInterfaceStateCallback(EthernetService &ethService) : ethernetService_(ethService) {}
45         ~GlobalInterfaceStateCallback() = default;
46         int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
47                                           int scope) override;
48         int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
49                                           int scope) 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 &iface, bool up) override;
53         int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
54         int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
55                                const std::string &ifName) override;
56         int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
57         int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
58 
59     private:
60         EthernetService &ethernetService_;
61     };
62 
63 public:
64     void OnStart() override;
65     void OnStop() override;
66     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
67 
68     int32_t SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic) override;
69     int32_t GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig) override;
70     int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus) override;
71     int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces) override;
72     int32_t ResetFactory() override;
73     int32_t RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
74     int32_t UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
75     int32_t SetInterfaceUp(const std::string &iface) override;
76     int32_t SetInterfaceDown(const std::string &iface) override;
77     int32_t GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &config) override;
78     int32_t SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg) override;
79 
80 protected:
81     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
82 
83 private:
84     enum ServiceRunningState {
85         STATE_STOPPED = 0,
86         STATE_RUNNING,
87     };
88 
89     using OnFunctionT = std::function<void(const sptr<InterfaceStateCallback> &callback)>;
90 
91     bool Init();
92     void InitManagement();
93 
94     int32_t RegisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
95     int32_t UnregisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
96     void NotifyMonitorIfaceCallbackAsync(OnFunctionT onFunction);
97 
98 private:
99     ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
100     bool registerToService_ = false;
101     uint16_t dependentServiceState_ = 0;
102     std::shared_ptr<EthernetManagement> ethManagement_;
103     sptr<EthernetServiceCommon> serviceComm_ = nullptr;
104     sptr<NetsysControllerCallback> interfaceStateCallback_ = nullptr;
105     std::vector<sptr<InterfaceStateCallback>> monitorIfaceCallbacks_;
106     std::shared_ptr<AppExecFwk::EventRunner> policyCallRunner_;
107     std::shared_ptr<AppExecFwk::EventHandler> policyCallHandler_;
108 };
109 } // namespace NetManagerStandard
110 } // namespace OHOS
111 #endif // ETHERNET_SERVICE_H
112