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_MANAGEMENT_H
178e745fdaSopenharmony_ci#define ETHERNET_MANAGEMENT_H
188e745fdaSopenharmony_ci
198e745fdaSopenharmony_ci#include <map>
208e745fdaSopenharmony_ci#include <mutex>
218e745fdaSopenharmony_ci
228e745fdaSopenharmony_ci#include "dev_interface_state.h"
238e745fdaSopenharmony_ci#include "ethernet_configuration.h"
248e745fdaSopenharmony_ci#include "ethernet_dhcp_controller.h"
258e745fdaSopenharmony_ci#include "iservice_registry.h"
268e745fdaSopenharmony_ci#include "mac_address_info.h"
278e745fdaSopenharmony_ci#include "netsys_controller_callback.h"
288e745fdaSopenharmony_ci#include "system_ability_definition.h"
298e745fdaSopenharmony_ci#include "ethernet_lan_management.h"
308e745fdaSopenharmony_ci
318e745fdaSopenharmony_cinamespace OHOS {
328e745fdaSopenharmony_cinamespace NetManagerStandard {
338e745fdaSopenharmony_ciclass EthernetManagement {
348e745fdaSopenharmony_ciprivate:
358e745fdaSopenharmony_ci    class EhternetDhcpNotifyCallback : public EthernetDhcpCallback {
368e745fdaSopenharmony_ci    public:
378e745fdaSopenharmony_ci        EhternetDhcpNotifyCallback(EthernetManagement &ethernetManagement) : ethernetManagement_(ethernetManagement) {}
388e745fdaSopenharmony_ci        int32_t OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult) override;
398e745fdaSopenharmony_ci
408e745fdaSopenharmony_ci    private:
418e745fdaSopenharmony_ci        EthernetManagement &ethernetManagement_;
428e745fdaSopenharmony_ci    };
438e745fdaSopenharmony_ci
448e745fdaSopenharmony_ciprivate:
458e745fdaSopenharmony_ci    class DevInterfaceStateCallback : public NetsysControllerCallback {
468e745fdaSopenharmony_ci    public:
478e745fdaSopenharmony_ci        DevInterfaceStateCallback(EthernetManagement &ethernetManagement) : ethernetManagement_(ethernetManagement) {}
488e745fdaSopenharmony_ci        ~DevInterfaceStateCallback() = default;
498e745fdaSopenharmony_ci        int32_t OnInterfaceAddressUpdated(const std::string &, const std::string &, int, int) override;
508e745fdaSopenharmony_ci        int32_t OnInterfaceAddressRemoved(const std::string &, const std::string &, int, int) 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 &, bool) override;
548e745fdaSopenharmony_ci        int32_t OnInterfaceLinkStateChanged(const std::string &ifName, bool up) override;
558e745fdaSopenharmony_ci        int32_t OnRouteChanged(bool, const std::string &, const std::string &, const std::string &) override;
568e745fdaSopenharmony_ci        int32_t OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult) override;
578e745fdaSopenharmony_ci        int32_t OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface) override;
588e745fdaSopenharmony_ci
598e745fdaSopenharmony_ci    private:
608e745fdaSopenharmony_ci        EthernetManagement &ethernetManagement_;
618e745fdaSopenharmony_ci    };
628e745fdaSopenharmony_ci
638e745fdaSopenharmony_cipublic:
648e745fdaSopenharmony_ci    static EthernetManagement& GetInstance();
658e745fdaSopenharmony_ci    void Init();
668e745fdaSopenharmony_ci    int32_t UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult);
678e745fdaSopenharmony_ci    void UpdateInterfaceState(const std::string &dev, bool up);
688e745fdaSopenharmony_ci    int32_t GetMacAddress(std::vector<MacAddressInfo> &macAddrList);
698e745fdaSopenharmony_ci    int32_t UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg);
708e745fdaSopenharmony_ci    int32_t GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig);
718e745fdaSopenharmony_ci    int32_t IsIfaceActive(const std::string &iface, int32_t &activeStatus);
728e745fdaSopenharmony_ci    int32_t GetAllActiveIfaces(std::vector<std::string> &activeIfaces);
738e745fdaSopenharmony_ci    int32_t ResetFactory();
748e745fdaSopenharmony_ci    void GetDumpInfo(std::string &info);
758e745fdaSopenharmony_ci    void DevInterfaceAdd(const std::string &devName);
768e745fdaSopenharmony_ci    void DevInterfaceRemove(const std::string &devName);
778e745fdaSopenharmony_ci
788e745fdaSopenharmony_ciprivate:
798e745fdaSopenharmony_ci    EthernetManagement();
808e745fdaSopenharmony_ci    ~EthernetManagement();
818e745fdaSopenharmony_ci    EthernetManagement(const EthernetManagement&) = delete;
828e745fdaSopenharmony_ci    EthernetManagement& operator=(const EthernetManagement&) = delete;
838e745fdaSopenharmony_ci    std::string GetMacAddr(const std::string &iface);
848e745fdaSopenharmony_ci    std::string HwAddrToStr(char *hwaddr);
858e745fdaSopenharmony_ci    void StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState);
868e745fdaSopenharmony_ci    void StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState);
878e745fdaSopenharmony_ci    void StartSetDevUpThd();
888e745fdaSopenharmony_ci    bool IsIfaceLinkUp(const std::string &iface);
898e745fdaSopenharmony_ci    bool ModeInputCheck(IPSetMode origin, IPSetMode input);
908e745fdaSopenharmony_ci
918e745fdaSopenharmony_ciprivate:
928e745fdaSopenharmony_ci    std::map<std::string, std::set<NetCap>> devCaps_;
938e745fdaSopenharmony_ci    std::map<std::string, sptr<InterfaceConfiguration>> devCfgs_;
948e745fdaSopenharmony_ci    std::map<std::string, sptr<DevInterfaceState>> devs_;
958e745fdaSopenharmony_ci    std::unique_ptr<EthernetConfiguration> ethConfiguration_ = nullptr;
968e745fdaSopenharmony_ci    std::unique_ptr<EthernetDhcpController> ethDhcpController_ = nullptr;
978e745fdaSopenharmony_ci    sptr<EhternetDhcpNotifyCallback> ethDhcpNotifyCallback_ = nullptr;
988e745fdaSopenharmony_ci    sptr<NetsysControllerCallback> ethDevInterfaceStateCallback_ = nullptr;
998e745fdaSopenharmony_ci    std::map<std::string, sptr<StaticConfiguration>> netLinkConfigs_;
1008e745fdaSopenharmony_ci    std::unique_ptr<EthernetLanManagement> ethLanManageMent_ = nullptr;
1018e745fdaSopenharmony_ci    std::mutex mutex_;
1028e745fdaSopenharmony_ci};
1038e745fdaSopenharmony_ci} // namespace NetManagerStandard
1048e745fdaSopenharmony_ci} // namespace OHOS
1058e745fdaSopenharmony_ci#endif // ETHERNET_MANAGEMENT_H
106