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#include "ethernet_service.h"
178e745fdaSopenharmony_ci
188e745fdaSopenharmony_ci#include <new>
198e745fdaSopenharmony_ci#include <sys/time.h>
208e745fdaSopenharmony_ci
218e745fdaSopenharmony_ci#include "ethernet_management.h"
228e745fdaSopenharmony_ci#include "mac_address_info.h"
238e745fdaSopenharmony_ci#include "interface_configuration.h"
248e745fdaSopenharmony_ci#include "iremote_object.h"
258e745fdaSopenharmony_ci#include "net_ethernet_base_service.h"
268e745fdaSopenharmony_ci#include "net_manager_center.h"
278e745fdaSopenharmony_ci#include "net_manager_constants.h"
288e745fdaSopenharmony_ci#include "netmanager_base_permission.h"
298e745fdaSopenharmony_ci#include "netmgr_ext_log_wrapper.h"
308e745fdaSopenharmony_ci#include "netsys_controller.h"
318e745fdaSopenharmony_ci#include "system_ability_definition.h"
328e745fdaSopenharmony_ci
338e745fdaSopenharmony_cinamespace OHOS {
348e745fdaSopenharmony_cinamespace NetManagerStandard {
358e745fdaSopenharmony_cinamespace {
368e745fdaSopenharmony_ciconstexpr uint16_t DEPENDENT_SERVICE_NET_CONN_MANAGER = 0x0001;
378e745fdaSopenharmony_ciconstexpr uint16_t DEPENDENT_SERVICE_COMMON_EVENT = 0x0002;
388e745fdaSopenharmony_ciconstexpr uint16_t DEPENDENT_SERVICE_All = 0x0003;
398e745fdaSopenharmony_ciconstexpr const char *NET_ACTIVATE_WORK_THREAD = "POLICY_CALLBACK_WORK_THREAD";
408e745fdaSopenharmony_ciconst bool REGISTER_LOCAL_RESULT_ETH =
418e745fdaSopenharmony_ci    SystemAbility::MakeAndRegisterAbility(DelayedSingleton<EthernetService>::GetInstance().get());
428e745fdaSopenharmony_ci} // namespace
438e745fdaSopenharmony_ci
448e745fdaSopenharmony_ciEthernetService::EthernetService() : SystemAbility(COMM_ETHERNET_MANAGER_SYS_ABILITY_ID, true) {}
458e745fdaSopenharmony_ci
468e745fdaSopenharmony_ciEthernetService::~EthernetService() = default;
478e745fdaSopenharmony_ci
488e745fdaSopenharmony_civoid EthernetService::OnStart()
498e745fdaSopenharmony_ci{
508e745fdaSopenharmony_ci    struct timeval tv;
518e745fdaSopenharmony_ci    gettimeofday(&tv, nullptr);
528e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("EthernetService::OnStart begin");
538e745fdaSopenharmony_ci    if (state_ == STATE_RUNNING) {
548e745fdaSopenharmony_ci        NETMGR_EXT_LOG_D("EthernetService the state is already running");
558e745fdaSopenharmony_ci        return;
568e745fdaSopenharmony_ci    }
578e745fdaSopenharmony_ci    if (!Init()) {
588e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService init failed");
598e745fdaSopenharmony_ci        return;
608e745fdaSopenharmony_ci    }
618e745fdaSopenharmony_ci    state_ = STATE_RUNNING;
628e745fdaSopenharmony_ci    gettimeofday(&tv, nullptr);
638e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("EthernetService::OnStart end");
648e745fdaSopenharmony_ci}
658e745fdaSopenharmony_ci
668e745fdaSopenharmony_civoid EthernetService::OnStop()
678e745fdaSopenharmony_ci{
688e745fdaSopenharmony_ci    state_ = STATE_STOPPED;
698e745fdaSopenharmony_ci    registerToService_ = false;
708e745fdaSopenharmony_ci    ethernetServiceFfrtQueue_.reset();
718e745fdaSopenharmony_ci}
728e745fdaSopenharmony_ci
738e745fdaSopenharmony_ciint32_t EthernetService::Dump(int32_t fd, const std::vector<std::u16string> &args)
748e745fdaSopenharmony_ci{
758e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Start Dump, fd: %{public}d", fd);
768e745fdaSopenharmony_ci    std::string result;
778e745fdaSopenharmony_ci    ethManagement_.GetDumpInfo(result);
788e745fdaSopenharmony_ci    int32_t ret = dprintf(fd, "%s\n", result.c_str());
798e745fdaSopenharmony_ci    return ret < 0 ? NETMANAGER_EXT_ERR_LOCAL_PTR_NULL : NETMANAGER_EXT_SUCCESS;
808e745fdaSopenharmony_ci}
818e745fdaSopenharmony_ci
828e745fdaSopenharmony_civoid EthernetService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
838e745fdaSopenharmony_ci{
848e745fdaSopenharmony_ci    switch (systemAbilityId) {
858e745fdaSopenharmony_ci        case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID:
868e745fdaSopenharmony_ci            NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility Conn");
878e745fdaSopenharmony_ci            dependentServiceState_ |= DEPENDENT_SERVICE_NET_CONN_MANAGER;
888e745fdaSopenharmony_ci            break;
898e745fdaSopenharmony_ci        case COMMON_EVENT_SERVICE_ID:
908e745fdaSopenharmony_ci            NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility CES");
918e745fdaSopenharmony_ci            dependentServiceState_ |= DEPENDENT_SERVICE_COMMON_EVENT;
928e745fdaSopenharmony_ci            break;
938e745fdaSopenharmony_ci        default:
948e745fdaSopenharmony_ci            NETMGR_EXT_LOG_D("EthernetService::OnAddSystemAbility unhandled sysabilityId:%{public}d", systemAbilityId);
958e745fdaSopenharmony_ci            break;
968e745fdaSopenharmony_ci    }
978e745fdaSopenharmony_ci    if (dependentServiceState_ == DEPENDENT_SERVICE_All) {
988e745fdaSopenharmony_ci        InitManagement();
998e745fdaSopenharmony_ci    }
1008e745fdaSopenharmony_ci}
1018e745fdaSopenharmony_ci
1028e745fdaSopenharmony_cibool EthernetService::Init()
1038e745fdaSopenharmony_ci{
1048e745fdaSopenharmony_ci    if (!REGISTER_LOCAL_RESULT_ETH) {
1058e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService Register to local sa manager failed");
1068e745fdaSopenharmony_ci        return false;
1078e745fdaSopenharmony_ci    }
1088e745fdaSopenharmony_ci    if (!registerToService_) {
1098e745fdaSopenharmony_ci        if (!Publish(DelayedSingleton<EthernetService>::GetInstance().get())) {
1108e745fdaSopenharmony_ci            NETMGR_EXT_LOG_E("EthernetService Register to sa manager failed");
1118e745fdaSopenharmony_ci            return false;
1128e745fdaSopenharmony_ci        }
1138e745fdaSopenharmony_ci        registerToService_ = true;
1148e745fdaSopenharmony_ci    }
1158e745fdaSopenharmony_ci    AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
1168e745fdaSopenharmony_ci    AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
1178e745fdaSopenharmony_ci    interfaceStateCallback_ = new (std::nothrow) GlobalInterfaceStateCallback(*this);
1188e745fdaSopenharmony_ci    if (interfaceStateCallback_ == nullptr) {
1198e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("allInterfaceStateCallback_ is nullptr");
1208e745fdaSopenharmony_ci        return false;
1218e745fdaSopenharmony_ci    }
1228e745fdaSopenharmony_ci    NetsysController::GetInstance().RegisterCallback(interfaceStateCallback_);
1238e745fdaSopenharmony_ci    serviceComm_ = new (std::nothrow) EthernetServiceCommon();
1248e745fdaSopenharmony_ci    if (serviceComm_ == nullptr) {
1258e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("serviceComm_ is nullptr");
1268e745fdaSopenharmony_ci        return false;
1278e745fdaSopenharmony_ci    }
1288e745fdaSopenharmony_ci    NetManagerCenter::GetInstance().RegisterEthernetService(serviceComm_);
1298e745fdaSopenharmony_ci    ethernetServiceFfrtQueue_ = std::make_shared<ffrt::queue>("EthernetService");
1308e745fdaSopenharmony_ci    return true;
1318e745fdaSopenharmony_ci}
1328e745fdaSopenharmony_ci
1338e745fdaSopenharmony_civoid EthernetService::InitManagement()
1348e745fdaSopenharmony_ci{
1358e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("EthernetService::InitManagement Enter");
1368e745fdaSopenharmony_ci    ethManagement_.Init();
1378e745fdaSopenharmony_ci}
1388e745fdaSopenharmony_ci
1398e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &addr,
1408e745fdaSopenharmony_ci                                                                                 const std::string &ifName, int flags,
1418e745fdaSopenharmony_ci                                                                                 int scope)
1428e745fdaSopenharmony_ci{
1438e745fdaSopenharmony_ci    return 0;
1448e745fdaSopenharmony_ci}
1458e745fdaSopenharmony_ci
1468e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &addr,
1478e745fdaSopenharmony_ci                                                                                 const std::string &ifName, int flags,
1488e745fdaSopenharmony_ci                                                                                 int scope)
1498e745fdaSopenharmony_ci{
1508e745fdaSopenharmony_ci    return 0;
1518e745fdaSopenharmony_ci}
1528e745fdaSopenharmony_ci
1538e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceAdded(const std::string &iface)
1548e745fdaSopenharmony_ci{
1558e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("iface: %{public}s, added", iface.c_str());
1568e745fdaSopenharmony_ci    ethernetService_.NotifyMonitorIfaceCallbackAsync(
1578e745fdaSopenharmony_ci        [=](const sptr<InterfaceStateCallback> &callback) { callback->OnInterfaceAdded(iface); });
1588e745fdaSopenharmony_ci    return 0;
1598e745fdaSopenharmony_ci}
1608e745fdaSopenharmony_ci
1618e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceRemoved(const std::string &iface)
1628e745fdaSopenharmony_ci{
1638e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("iface: %{public}s, removed", iface.c_str());
1648e745fdaSopenharmony_ci    ethernetService_.NotifyMonitorIfaceCallbackAsync(
1658e745fdaSopenharmony_ci        [=](const sptr<InterfaceStateCallback> &callback) { callback->OnInterfaceRemoved(iface); });
1668e745fdaSopenharmony_ci    return 0;
1678e745fdaSopenharmony_ci}
1688e745fdaSopenharmony_ci
1698e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceChanged(const std::string &iface, bool up)
1708e745fdaSopenharmony_ci{
1718e745fdaSopenharmony_ci    return 0;
1728e745fdaSopenharmony_ci}
1738e745fdaSopenharmony_ci
1748e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
1758e745fdaSopenharmony_ci{
1768e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("iface: %{public}s, up: %{public}d", ifName.c_str(), up);
1778e745fdaSopenharmony_ci    ethernetService_.NotifyMonitorIfaceCallbackAsync(
1788e745fdaSopenharmony_ci        [=](const sptr<InterfaceStateCallback> &callback) { callback->OnInterfaceChanged(ifName, up); });
1798e745fdaSopenharmony_ci    return 0;
1808e745fdaSopenharmony_ci}
1818e745fdaSopenharmony_ci
1828e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnRouteChanged(bool updated, const std::string &route,
1838e745fdaSopenharmony_ci                                                                      const std::string &gateway,
1848e745fdaSopenharmony_ci                                                                      const std::string &ifName)
1858e745fdaSopenharmony_ci{
1868e745fdaSopenharmony_ci    return 0;
1878e745fdaSopenharmony_ci}
1888e745fdaSopenharmony_ci
1898e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
1908e745fdaSopenharmony_ci{
1918e745fdaSopenharmony_ci    return 0;
1928e745fdaSopenharmony_ci}
1938e745fdaSopenharmony_ci
1948e745fdaSopenharmony_ciint32_t EthernetService::GlobalInterfaceStateCallback::OnBandwidthReachedLimit(const std::string &limitName,
1958e745fdaSopenharmony_ci                                                                               const std::string &iface)
1968e745fdaSopenharmony_ci{
1978e745fdaSopenharmony_ci    return 0;
1988e745fdaSopenharmony_ci}
1998e745fdaSopenharmony_ci
2008e745fdaSopenharmony_ciint32_t EthernetService::GetMacAddress(std::vector<MacAddressInfo> &macAddrList)
2018e745fdaSopenharmony_ci{
2028e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2038e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2048e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2058e745fdaSopenharmony_ci    }
2068e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_ETHERNET_LOCAL_MAC)) {
2078e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService GetMacAddress no js permission");
2088e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2098e745fdaSopenharmony_ci    }
2108e745fdaSopenharmony_ci
2118e745fdaSopenharmony_ci    return ethManagement_.GetMacAddress(macAddrList);
2128e745fdaSopenharmony_ci}
2138e745fdaSopenharmony_ci
2148e745fdaSopenharmony_ciint32_t EthernetService::SetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ic)
2158e745fdaSopenharmony_ci{
2168e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Set iface: %{public}s config", iface.c_str());
2178e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2188e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2198e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2208e745fdaSopenharmony_ci    }
2218e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
2228e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService SetIfaceConfig no js permission");
2238e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2248e745fdaSopenharmony_ci    }
2258e745fdaSopenharmony_ci
2268e745fdaSopenharmony_ci    return ethManagement_.UpdateDevInterfaceCfg(iface, ic);
2278e745fdaSopenharmony_ci}
2288e745fdaSopenharmony_ci
2298e745fdaSopenharmony_ciint32_t EthernetService::GetIfaceConfig(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
2308e745fdaSopenharmony_ci{
2318e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Get iface: %{public}s config", iface.c_str());
2328e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2338e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2348e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2358e745fdaSopenharmony_ci    }
2368e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
2378e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService GetIfaceConfig no js permission");
2388e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2398e745fdaSopenharmony_ci    }
2408e745fdaSopenharmony_ci
2418e745fdaSopenharmony_ci    return ethManagement_.GetDevInterfaceCfg(iface, ifaceConfig);
2428e745fdaSopenharmony_ci}
2438e745fdaSopenharmony_ci
2448e745fdaSopenharmony_ciint32_t EthernetService::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
2458e745fdaSopenharmony_ci{
2468e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Get iface: %{public}s is active", iface.c_str());
2478e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2488e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2498e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2508e745fdaSopenharmony_ci    }
2518e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
2528e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService IsIfaceActive no js permission");
2538e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2548e745fdaSopenharmony_ci    }
2558e745fdaSopenharmony_ci
2568e745fdaSopenharmony_ci    return ethManagement_.IsIfaceActive(iface, activeStatus);
2578e745fdaSopenharmony_ci}
2588e745fdaSopenharmony_ci
2598e745fdaSopenharmony_ciint32_t EthernetService::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
2608e745fdaSopenharmony_ci{
2618e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2628e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2638e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2648e745fdaSopenharmony_ci    }
2658e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
2668e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService GetAllActiveIfaces no js permission");
2678e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2688e745fdaSopenharmony_ci    }
2698e745fdaSopenharmony_ci
2708e745fdaSopenharmony_ci    return ethManagement_.GetAllActiveIfaces(activeIfaces);
2718e745fdaSopenharmony_ci}
2728e745fdaSopenharmony_ci
2738e745fdaSopenharmony_ciint32_t EthernetService::ResetFactory()
2748e745fdaSopenharmony_ci{
2758e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2768e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2778e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2788e745fdaSopenharmony_ci    }
2798e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
2808e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService GetAllActiveIfaces no js permission");
2818e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
2828e745fdaSopenharmony_ci    }
2838e745fdaSopenharmony_ci
2848e745fdaSopenharmony_ci    return ethManagement_.ResetFactory();
2858e745fdaSopenharmony_ci}
2868e745fdaSopenharmony_ci
2878e745fdaSopenharmony_ciint32_t EthernetService::RegisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
2888e745fdaSopenharmony_ci{
2898e745fdaSopenharmony_ci    if (callback == nullptr) {
2908e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Register interface callback failed");
2918e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
2928e745fdaSopenharmony_ci    }
2938e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
2948e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
2958e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
2968e745fdaSopenharmony_ci    }
2978e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
2988e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("RegisterIfacesStateChanged no permission");
2998e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3008e745fdaSopenharmony_ci    }
3018e745fdaSopenharmony_ci    return RegisterMonitorIfaceCallbackAsync(callback);
3028e745fdaSopenharmony_ci}
3038e745fdaSopenharmony_ci
3048e745fdaSopenharmony_ciint32_t EthernetService::UnregisterIfacesStateChanged(const sptr<InterfaceStateCallback> &callback)
3058e745fdaSopenharmony_ci{
3068e745fdaSopenharmony_ci    if (callback == nullptr) {
3078e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Unregister interface callback failed");
3088e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
3098e745fdaSopenharmony_ci    }
3108e745fdaSopenharmony_ci    if (!NetManagerPermission::IsSystemCaller()) {
3118e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("Caller not have sys permission");
3128e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_NOT_SYSTEM_CALL;
3138e745fdaSopenharmony_ci    }
3148e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
3158e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("RegisterIfacesStateChanged no permission");
3168e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3178e745fdaSopenharmony_ci    }
3188e745fdaSopenharmony_ci    return UnregisterMonitorIfaceCallbackAsync(callback);
3198e745fdaSopenharmony_ci}
3208e745fdaSopenharmony_ci
3218e745fdaSopenharmony_ciint32_t EthernetService::SetInterfaceUp(const std::string &iface)
3228e745fdaSopenharmony_ci{
3238e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Set interface: %{public}s up", iface.c_str());
3248e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
3258e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService SetInterfaceUp no permission");
3268e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3278e745fdaSopenharmony_ci    }
3288e745fdaSopenharmony_ci    return NetsysController::GetInstance().SetInterfaceUp(iface);
3298e745fdaSopenharmony_ci}
3308e745fdaSopenharmony_ci
3318e745fdaSopenharmony_ciint32_t EthernetService::SetInterfaceDown(const std::string &iface)
3328e745fdaSopenharmony_ci{
3338e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Set interface: %{public}s down", iface.c_str());
3348e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
3358e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService SetInterfaceDown no permission");
3368e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3378e745fdaSopenharmony_ci    }
3388e745fdaSopenharmony_ci    return NetsysController::GetInstance().SetInterfaceDown(iface);
3398e745fdaSopenharmony_ci}
3408e745fdaSopenharmony_ci
3418e745fdaSopenharmony_ciint32_t EthernetService::GetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &config)
3428e745fdaSopenharmony_ci{
3438e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Get interface: %{public}s config", iface.c_str());
3448e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
3458e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService GetInterfaceConfig no permission");
3468e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3478e745fdaSopenharmony_ci    }
3488e745fdaSopenharmony_ci    config.ifName = iface;
3498e745fdaSopenharmony_ci    return NetsysController::GetInstance().GetInterfaceConfig(config);
3508e745fdaSopenharmony_ci}
3518e745fdaSopenharmony_ci
3528e745fdaSopenharmony_ciint32_t EthernetService::SetInterfaceConfig(const std::string &iface, OHOS::nmd::InterfaceConfigurationParcel &cfg)
3538e745fdaSopenharmony_ci{
3548e745fdaSopenharmony_ci    NETMGR_EXT_LOG_D("Set interface: %{public}s config", iface.c_str());
3558e745fdaSopenharmony_ci    if (!NetManagerPermission::CheckPermission(Permission::CONNECTIVITY_INTERNAL)) {
3568e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("EthernetService SetInterfaceConfig no permission");
3578e745fdaSopenharmony_ci        return NETMANAGER_EXT_ERR_PERMISSION_DENIED;
3588e745fdaSopenharmony_ci    }
3598e745fdaSopenharmony_ci    cfg.ifName = iface;
3608e745fdaSopenharmony_ci    return NetsysController::GetInstance().SetInterfaceConfig(cfg);
3618e745fdaSopenharmony_ci}
3628e745fdaSopenharmony_ci
3638e745fdaSopenharmony_ciint32_t EthernetService::RegisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback)
3648e745fdaSopenharmony_ci{
3658e745fdaSopenharmony_ci    int32_t ret = NETMANAGER_EXT_ERR_OPERATION_FAILED;
3668e745fdaSopenharmony_ci    if (!ethernetServiceFfrtQueue_) {
3678e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("FFRT Init Fail");
3688e745fdaSopenharmony_ci        return ret;
3698e745fdaSopenharmony_ci    }
3708e745fdaSopenharmony_ci    ffrt::task_handle RegisterMonitorIfaceTask = ethernetServiceFfrtQueue_->submit_h([this, &callback, &ret]() {
3718e745fdaSopenharmony_ci        for (auto iterCb = monitorIfaceCallbacks_.begin(); iterCb != monitorIfaceCallbacks_.end(); iterCb++) {
3728e745fdaSopenharmony_ci            if ((*iterCb)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
3738e745fdaSopenharmony_ci                NETMGR_EXT_LOG_D("Register interface callback failed, callback already exists");
3748e745fdaSopenharmony_ci                ret = NETMANAGER_EXT_ERR_OPERATION_FAILED;
3758e745fdaSopenharmony_ci                return;
3768e745fdaSopenharmony_ci            }
3778e745fdaSopenharmony_ci        }
3788e745fdaSopenharmony_ci        monitorIfaceCallbacks_.push_back(callback);
3798e745fdaSopenharmony_ci        NETMGR_EXT_LOG_D("Register interface callback success");
3808e745fdaSopenharmony_ci        ret = NETMANAGER_EXT_SUCCESS;
3818e745fdaSopenharmony_ci    }, ffrt::task_attr().name("RegisterMonitorIfaceCallbackAsync"));
3828e745fdaSopenharmony_ci    ethernetServiceFfrtQueue_->wait(RegisterMonitorIfaceTask);
3838e745fdaSopenharmony_ci    return ret;
3848e745fdaSopenharmony_ci}
3858e745fdaSopenharmony_ci
3868e745fdaSopenharmony_ciint32_t EthernetService::UnregisterMonitorIfaceCallbackAsync(const sptr<InterfaceStateCallback> &callback)
3878e745fdaSopenharmony_ci{
3888e745fdaSopenharmony_ci    int32_t ret = NETMANAGER_EXT_ERR_OPERATION_FAILED;
3898e745fdaSopenharmony_ci    if (!ethernetServiceFfrtQueue_) {
3908e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("FFRT Init Fail");
3918e745fdaSopenharmony_ci        return ret;
3928e745fdaSopenharmony_ci    }
3938e745fdaSopenharmony_ci    ffrt::task_handle UnregisterMonitorIfaceTask = ethernetServiceFfrtQueue_->submit_h([this, &callback, &ret]() {
3948e745fdaSopenharmony_ci        for (auto iterCb = monitorIfaceCallbacks_.begin(); iterCb != monitorIfaceCallbacks_.end(); iterCb++) {
3958e745fdaSopenharmony_ci            if ((*iterCb)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) {
3968e745fdaSopenharmony_ci                monitorIfaceCallbacks_.erase(iterCb);
3978e745fdaSopenharmony_ci                NETMGR_EXT_LOG_D("Unregister interface callback success.");
3988e745fdaSopenharmony_ci                ret = NETMANAGER_EXT_SUCCESS;
3998e745fdaSopenharmony_ci                return;
4008e745fdaSopenharmony_ci            }
4018e745fdaSopenharmony_ci        }
4028e745fdaSopenharmony_ci            NETMGR_EXT_LOG_E("Unregister interface callback is doesnot exist.");
4038e745fdaSopenharmony_ci            ret = NETMANAGER_EXT_ERR_OPERATION_FAILED;
4048e745fdaSopenharmony_ci    }, ffrt::task_attr().name("UnregisterMonitorIfaceCallbackAsync"));
4058e745fdaSopenharmony_ci    ethernetServiceFfrtQueue_->wait(UnregisterMonitorIfaceTask);
4068e745fdaSopenharmony_ci    return ret;
4078e745fdaSopenharmony_ci}
4088e745fdaSopenharmony_ci
4098e745fdaSopenharmony_civoid EthernetService::NotifyMonitorIfaceCallbackAsync(OnFunctionT onFunction)
4108e745fdaSopenharmony_ci{
4118e745fdaSopenharmony_ci    if (!ethernetServiceFfrtQueue_) {
4128e745fdaSopenharmony_ci        NETMGR_EXT_LOG_E("FFRT Init Fail");
4138e745fdaSopenharmony_ci        return;
4148e745fdaSopenharmony_ci    }
4158e745fdaSopenharmony_ci    ffrt::task_handle NotifyMonitorIfaceTask_ = ethernetServiceFfrtQueue_->submit_h([this, &onFunction]() {
4168e745fdaSopenharmony_ci        std::for_each(monitorIfaceCallbacks_.begin(), monitorIfaceCallbacks_.end(), onFunction);
4178e745fdaSopenharmony_ci    }, ffrt::task_attr().name("NotifyMonitorIfaceCallbackAsync"));
4188e745fdaSopenharmony_ci    ethernetServiceFfrtQueue_->wait(NotifyMonitorIfaceTask_);
4198e745fdaSopenharmony_ci}
4208e745fdaSopenharmony_ci} // namespace NetManagerStandard
4218e745fdaSopenharmony_ci} // namespace OHOS
422