18e745fdaSopenharmony_ci/*
28e745fdaSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
38e745fdaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48e745fdaSopenharmony_ci * you may not use this file except in compliance with the License.
58e745fdaSopenharmony_ci * You may obtain a copy of the License at
68e745fdaSopenharmony_ci *
78e745fdaSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88e745fdaSopenharmony_ci *
98e745fdaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108e745fdaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118e745fdaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128e745fdaSopenharmony_ci * See the License for the specific language governing permissions and
138e745fdaSopenharmony_ci * limitations under the License.
148e745fdaSopenharmony_ci */
158e745fdaSopenharmony_ci
168e745fdaSopenharmony_ci#ifndef ETHERNET_SERVICE_H
178e745fdaSopenharmony_ci#define ETHERNET_SERVICE_H
188e745fdaSopenharmony_ci
198e745fdaSopenharmony_ci#include <cstdint>
208e745fdaSopenharmony_ci#include <iosfwd>
218e745fdaSopenharmony_ci#include <memory>
228e745fdaSopenharmony_ci#include <string>
238e745fdaSopenharmony_ci#include <vector>
248e745fdaSopenharmony_ci
258e745fdaSopenharmony_ci#include "ethernet_management.h"
268e745fdaSopenharmony_ci#include "ethernet_service_common.h"
278e745fdaSopenharmony_ci#include "ethernet_service_stub.h"
288e745fdaSopenharmony_ci#include "event_handler.h"
298e745fdaSopenharmony_ci#include "netsys_controller_callback.h"
308e745fdaSopenharmony_ci#include "refbase.h"
318e745fdaSopenharmony_ci#include "singleton.h"
328e745fdaSopenharmony_ci#include "system_ability.h"
338e745fdaSopenharmony_ci#include "ffrt.h"
348e745fdaSopenharmony_ci
358e745fdaSopenharmony_cinamespace OHOS {
368e745fdaSopenharmony_cinamespace NetManagerStandard {
378e745fdaSopenharmony_ciclass EthernetService : public SystemAbility,
388e745fdaSopenharmony_ci                        public EthernetServiceStub,
398e745fdaSopenharmony_ci                        public std::enable_shared_from_this<EthernetService> {
408e745fdaSopenharmony_ci    DECLARE_DELAYED_SINGLETON(EthernetService)
418e745fdaSopenharmony_ci    DECLARE_SYSTEM_ABILITY(EthernetService)
428e745fdaSopenharmony_ci
438e745fdaSopenharmony_ci    class GlobalInterfaceStateCallback : public NetsysControllerCallback {
448e745fdaSopenharmony_ci    public:
458e745fdaSopenharmony_ci        explicit GlobalInterfaceStateCallback(EthernetService &ethService) : ethernetService_(ethService) {}
468e745fdaSopenharmony_ci        ~GlobalInterfaceStateCallback() = default;
478e745fdaSopenharmony_ci        int32_t OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
488e745fdaSopenharmony_ci                                          int scope) override;
498e745fdaSopenharmony_ci        int32_t OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
508e745fdaSopenharmony_ci                                          int scope) override;
518e745fdaSopenharmony_ci        int32_t OnInterfaceAdded(const std::string &iface) override;
528e745fdaSopenharmony_ci        int32_t OnInterfaceRemoved(const std::string &iface) override;
538e745fdaSopenharmony_ci        int32_t OnInterfaceChanged(const std::string &iface, bool up) override;
548e745fdaSopenharmony_ci        int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
558e745fdaSopenharmony_ci        int32_t OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
568e745fdaSopenharmony_ci                               const std::string &ifName) override;
578e745fdaSopenharmony_ci        int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
588e745fdaSopenharmony_ci        int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
598e745fdaSopenharmony_ci
608e745fdaSopenharmony_ci    private:
618e745fdaSopenharmony_ci        EthernetService &ethernetService_;
628e745fdaSopenharmony_ci    };
638e745fdaSopenharmony_ci
648e745fdaSopenharmony_cipublic:
658e745fdaSopenharmony_ci    void OnStart() override;
668e745fdaSopenharmony_ci    void OnStop() override;
678e745fdaSopenharmony_ci    int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
688e745fdaSopenharmony_ci
698e745fdaSopenharmony_ci    int32_t GetMacAddress(std::vector<MacAddressInfo> &macAddrList) override;
708e745fdaSopenharmony_ci    int32_t SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic) override;
718e745fdaSopenharmony_ci    int32_t GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig) override;
728e745fdaSopenharmony_ci    int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus) override;
738e745fdaSopenharmony_ci    int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces) override;
748e745fdaSopenharmony_ci    int32_t ResetFactory() override;
758e745fdaSopenharmony_ci    int32_t RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
768e745fdaSopenharmony_ci    int32_t UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback) override;
778e745fdaSopenharmony_ci    int32_t SetInterfaceUp(const std::string &iface) override;
788e745fdaSopenharmony_ci    int32_t SetInterfaceDown(const std::string &iface) override;
798e745fdaSopenharmony_ci    int32_t GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &config) override;
808e745fdaSopenharmony_ci    int32_t SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg) override;
818e745fdaSopenharmony_ci
828e745fdaSopenharmony_ciprotected:
838e745fdaSopenharmony_ci    void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
848e745fdaSopenharmony_ci
858e745fdaSopenharmony_ciprivate:
868e745fdaSopenharmony_ci    enum ServiceRunningState {
878e745fdaSopenharmony_ci        STATE_STOPPED = 0,
888e745fdaSopenharmony_ci        STATE_RUNNING,
898e745fdaSopenharmony_ci    };
908e745fdaSopenharmony_ci
918e745fdaSopenharmony_ci    using OnFunctionT = std::function<void(const sptr<InterfaceStateCallback> &callback)>;
928e745fdaSopenharmony_ci
938e745fdaSopenharmony_ci    bool Init();
948e745fdaSopenharmony_ci    void InitManagement();
958e745fdaSopenharmony_ci
968e745fdaSopenharmony_ci    int32_t RegisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
978e745fdaSopenharmony_ci    int32_t UnregisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback);
988e745fdaSopenharmony_ci    void NotifyMonitorIfaceCallbackAsync(OnFunctionT onFunction);
998e745fdaSopenharmony_ci
1008e745fdaSopenharmony_ciprivate:
1018e745fdaSopenharmony_ci    ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
1028e745fdaSopenharmony_ci    bool registerToService_ = false;
1038e745fdaSopenharmony_ci    uint16_t dependentServiceState_ = 0;
1048e745fdaSopenharmony_ci    EthernetManagement& ethManagement_ = EthernetManagement::GetInstance();
1058e745fdaSopenharmony_ci    sptr<EthernetServiceCommon> serviceComm_ = nullptr;
1068e745fdaSopenharmony_ci    sptr<NetsysControllerCallback> interfaceStateCallback_ = nullptr;
1078e745fdaSopenharmony_ci    std::vector<sptr<InterfaceStateCallback>> monitorIfaceCallbacks_;
1088e745fdaSopenharmony_ci    std::shared_ptr<ffrt::queue> ethernetServiceFfrtQueue_ = nullptr;
1098e745fdaSopenharmony_ci};
1108e745fdaSopenharmony_ci} // namespace NetManagerStandard
1118e745fdaSopenharmony_ci} // namespace OHOS
1128e745fdaSopenharmony_ci#endif // ETHERNET_SERVICE_H
113