1518678f8Sopenharmony_ci/* 2518678f8Sopenharmony_ci * Copyright (C) 2021-2023 Huawei Device Co., Ltd. 3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License. 5518678f8Sopenharmony_ci * You may obtain a copy of the License at 6518678f8Sopenharmony_ci * 7518678f8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8518678f8Sopenharmony_ci * 9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and 13518678f8Sopenharmony_ci * limitations under the License. 14518678f8Sopenharmony_ci */ 15518678f8Sopenharmony_ci#include "dhcp_client_impl.h" 16518678f8Sopenharmony_ci#include "i_dhcp_client.h" 17518678f8Sopenharmony_ci#include "dhcp_client_proxy.h" 18518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE 19518678f8Sopenharmony_ci#include "dhcp_sa_manager.h" 20518678f8Sopenharmony_ci#include "iremote_broker.h" 21518678f8Sopenharmony_ci#include "iremote_object.h" 22518678f8Sopenharmony_ci#include "iservice_registry.h" 23518678f8Sopenharmony_ci#endif 24518678f8Sopenharmony_ci#include "dhcp_logger.h" 25518678f8Sopenharmony_ci 26518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpClientImpl"); 27518678f8Sopenharmony_ci 28518678f8Sopenharmony_cinamespace OHOS { 29518678f8Sopenharmony_cinamespace DHCP { 30518678f8Sopenharmony_ci#define RETURN_IF_FAIL(cond) \ 31518678f8Sopenharmony_ci do { \ 32518678f8Sopenharmony_ci if (!(cond)) { \ 33518678f8Sopenharmony_ci DHCP_LOGI("'%{public}s' failed.", #cond); \ 34518678f8Sopenharmony_ci return DHCP_E_FAILED; \ 35518678f8Sopenharmony_ci } \ 36518678f8Sopenharmony_ci } while (0) 37518678f8Sopenharmony_ci 38518678f8Sopenharmony_ciDhcpClientImpl::DhcpClientImpl() : systemAbilityId_(0), client_(nullptr) 39518678f8Sopenharmony_ci{ 40518678f8Sopenharmony_ci DHCP_LOGI("DhcpClientImpl()"); 41518678f8Sopenharmony_ci} 42518678f8Sopenharmony_ci 43518678f8Sopenharmony_ciDhcpClientImpl::~DhcpClientImpl() 44518678f8Sopenharmony_ci{ 45518678f8Sopenharmony_ci DHCP_LOGI("~DhcpClientImpl()"); 46518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE 47518678f8Sopenharmony_ci DhcpClientProxy::ReleaseInstance(); 48518678f8Sopenharmony_ci#endif 49518678f8Sopenharmony_ci} 50518678f8Sopenharmony_ci 51518678f8Sopenharmony_cibool DhcpClientImpl::Init(int systemAbilityId) 52518678f8Sopenharmony_ci{ 53518678f8Sopenharmony_ci DHCP_LOGI("DhcpClientImpl enter Init!"); 54518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE 55518678f8Sopenharmony_ci DhcpClientProxy *clientProxy = DhcpClientProxy::GetInstance(); 56518678f8Sopenharmony_ci if (clientProxy == nullptr) { 57518678f8Sopenharmony_ci DHCP_LOGE("get dhcp client proxy failed."); 58518678f8Sopenharmony_ci return false; 59518678f8Sopenharmony_ci } 60518678f8Sopenharmony_ci if (clientProxy->Init() != DHCP_OPT_SUCCESS) { 61518678f8Sopenharmony_ci DHCP_LOGE("dhcp client proxy init failed."); 62518678f8Sopenharmony_ci DhcpClientProxy::ReleaseInstance(); 63518678f8Sopenharmony_ci return false; 64518678f8Sopenharmony_ci } 65518678f8Sopenharmony_ci client_ = clientProxy; 66518678f8Sopenharmony_ci return true; 67518678f8Sopenharmony_ci#else 68518678f8Sopenharmony_ci systemAbilityId_ = systemAbilityId; 69518678f8Sopenharmony_ci return true; 70518678f8Sopenharmony_ci#endif 71518678f8Sopenharmony_ci} 72518678f8Sopenharmony_ci 73518678f8Sopenharmony_cibool DhcpClientImpl::GetDhcpClientProxy() 74518678f8Sopenharmony_ci{ 75518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE 76518678f8Sopenharmony_ci return (client_ != nullptr); 77518678f8Sopenharmony_ci#else 78518678f8Sopenharmony_ci DhcpSaLoadManager::GetInstance().LoadWifiSa(systemAbilityId_); 79518678f8Sopenharmony_ci if (IsRemoteDied() == false) { 80518678f8Sopenharmony_ci DHCP_LOGE("remote died false, %{public}d", systemAbilityId_); 81518678f8Sopenharmony_ci return true; 82518678f8Sopenharmony_ci } 83518678f8Sopenharmony_ci 84518678f8Sopenharmony_ci sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 85518678f8Sopenharmony_ci if (sa_mgr == nullptr) { 86518678f8Sopenharmony_ci DHCP_LOGE("failed to get SystemAbilityManager"); 87518678f8Sopenharmony_ci return false; 88518678f8Sopenharmony_ci } 89518678f8Sopenharmony_ci 90518678f8Sopenharmony_ci sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_); 91518678f8Sopenharmony_ci if (object == nullptr) { 92518678f8Sopenharmony_ci DHCP_LOGE("failed to get DEVICE_SERVICE"); 93518678f8Sopenharmony_ci return false; 94518678f8Sopenharmony_ci } 95518678f8Sopenharmony_ci 96518678f8Sopenharmony_ci client_ = iface_cast<OHOS::DHCP::IDhcpClient>(object); 97518678f8Sopenharmony_ci if (client_ == nullptr) { 98518678f8Sopenharmony_ci DHCP_LOGI("DhcpClientImpl new DhcpClientProxy"); 99518678f8Sopenharmony_ci client_ = new (std::nothrow)DhcpClientProxy(object); 100518678f8Sopenharmony_ci } 101518678f8Sopenharmony_ci 102518678f8Sopenharmony_ci if (client_ == nullptr) { 103518678f8Sopenharmony_ci DHCP_LOGE("dhcp client init failed. %{public}d", systemAbilityId_); 104518678f8Sopenharmony_ci return false; 105518678f8Sopenharmony_ci } 106518678f8Sopenharmony_ci return true; 107518678f8Sopenharmony_ci#endif 108518678f8Sopenharmony_ci} 109518678f8Sopenharmony_ci 110518678f8Sopenharmony_cibool DhcpClientImpl::IsRemoteDied(void) 111518678f8Sopenharmony_ci{ 112518678f8Sopenharmony_ci return (client_ == nullptr) ? true : client_->IsRemoteDied(); 113518678f8Sopenharmony_ci} 114518678f8Sopenharmony_ci 115518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE 116518678f8Sopenharmony_ciErrCode DhcpClientImpl::RegisterDhcpClientCallBack(const std::string& ifname, 117518678f8Sopenharmony_ci const std::shared_ptr<IDhcpClientCallBack> &callback) 118518678f8Sopenharmony_ci#else 119518678f8Sopenharmony_ciErrCode DhcpClientImpl::RegisterDhcpClientCallBack(const std::string& ifname, 120518678f8Sopenharmony_ci const sptr<IDhcpClientCallBack> &callback) 121518678f8Sopenharmony_ci#endif 122518678f8Sopenharmony_ci{ 123518678f8Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 124518678f8Sopenharmony_ci RETURN_IF_FAIL(GetDhcpClientProxy()); 125518678f8Sopenharmony_ci return client_->RegisterDhcpClientCallBack(ifname, callback); 126518678f8Sopenharmony_ci} 127518678f8Sopenharmony_ci 128518678f8Sopenharmony_ciErrCode DhcpClientImpl::StartDhcpClient(const std::string& ifname, bool bIpv6) 129518678f8Sopenharmony_ci{ 130518678f8Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 131518678f8Sopenharmony_ci RETURN_IF_FAIL(GetDhcpClientProxy()); 132518678f8Sopenharmony_ci return client_->StartDhcpClient(ifname, bIpv6); 133518678f8Sopenharmony_ci} 134518678f8Sopenharmony_ci 135518678f8Sopenharmony_ciErrCode DhcpClientImpl::SetConfiguration(const std::string& ifname, const RouterConfig& config) 136518678f8Sopenharmony_ci{ 137518678f8Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 138518678f8Sopenharmony_ci RETURN_IF_FAIL(GetDhcpClientProxy()); 139518678f8Sopenharmony_ci return client_->SetConfiguration(ifname, config); 140518678f8Sopenharmony_ci} 141518678f8Sopenharmony_ci 142518678f8Sopenharmony_ciErrCode DhcpClientImpl::StopDhcpClient(const std::string& ifname, bool bIpv6) 143518678f8Sopenharmony_ci{ 144518678f8Sopenharmony_ci std::lock_guard<std::mutex> lock(mutex_); 145518678f8Sopenharmony_ci RETURN_IF_FAIL(GetDhcpClientProxy()); 146518678f8Sopenharmony_ci return client_->StopDhcpClient(ifname, bIpv6); 147518678f8Sopenharmony_ci} 148518678f8Sopenharmony_ci} // namespace DHCP 149518678f8Sopenharmony_ci} // namespace OHOS 150