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_server_impl.h"
16518678f8Sopenharmony_ci#include "dhcp_server_proxy.h"
17518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
18518678f8Sopenharmony_ci#include "dhcp_sa_manager.h"
19518678f8Sopenharmony_ci#include "i_dhcp_server.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("DhcpServerImpl");
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_ciDhcpServerImpl::DhcpServerImpl() : client_(nullptr)
39518678f8Sopenharmony_ci{
40518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerImpl()");
41518678f8Sopenharmony_ci}
42518678f8Sopenharmony_ci
43518678f8Sopenharmony_ciDhcpServerImpl::~DhcpServerImpl()
44518678f8Sopenharmony_ci{
45518678f8Sopenharmony_ci    DHCP_LOGI("~DhcpServerImpl()");
46518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE
47518678f8Sopenharmony_ci    DhcpServerProxy::ReleaseInstance();
48518678f8Sopenharmony_ci#endif
49518678f8Sopenharmony_ci}
50518678f8Sopenharmony_ci
51518678f8Sopenharmony_cibool DhcpServerImpl::Init(int systemAbilityId)
52518678f8Sopenharmony_ci{
53518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerImpl enter Init!");
54518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE
55518678f8Sopenharmony_ci    DhcpServerProxy *serverProxy = DhcpServerProxy::GetInstance();
56518678f8Sopenharmony_ci    if (serverProxy == nullptr) {
57518678f8Sopenharmony_ci        DHCP_LOGE("get dhcp server proxy failed.");
58518678f8Sopenharmony_ci        return false;
59518678f8Sopenharmony_ci    }
60518678f8Sopenharmony_ci    if (clientProxy->Init() != DHCP_OPT_SUCCESS) {
61518678f8Sopenharmony_ci        DHCP_LOGE("dhcp server proxy init failed.");
62518678f8Sopenharmony_ci        DhcpServerProxy::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 DhcpServerImpl::GetDhcpServerProxy()
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    sptr<ISystemAbilityManager> sa_mgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
84518678f8Sopenharmony_ci    if (sa_mgr == nullptr) {
85518678f8Sopenharmony_ci        DHCP_LOGE("failed to get SystemAbilityManager");
86518678f8Sopenharmony_ci        return false;
87518678f8Sopenharmony_ci    }
88518678f8Sopenharmony_ci    sptr<IRemoteObject> object = sa_mgr->GetSystemAbility(systemAbilityId_);
89518678f8Sopenharmony_ci    if (object == nullptr) {
90518678f8Sopenharmony_ci        DHCP_LOGE("failed to get DEVICE_SERVICE");
91518678f8Sopenharmony_ci        return false;
92518678f8Sopenharmony_ci    }
93518678f8Sopenharmony_ci    client_ = iface_cast<OHOS::DHCP::IDhcpServer>(object);
94518678f8Sopenharmony_ci    if (client_ == nullptr) {
95518678f8Sopenharmony_ci        client_ = new (std::nothrow)DhcpServerProxy(object);
96518678f8Sopenharmony_ci    }
97518678f8Sopenharmony_ci
98518678f8Sopenharmony_ci    if (client_ == nullptr) {
99518678f8Sopenharmony_ci        DHCP_LOGE("wifi device init failed. %{public}d", systemAbilityId_);
100518678f8Sopenharmony_ci        return false;
101518678f8Sopenharmony_ci    }
102518678f8Sopenharmony_ci    DHCP_LOGI("DhcpServerImpl GetDhcpClientProxy ok");
103518678f8Sopenharmony_ci    return true;
104518678f8Sopenharmony_ci#endif
105518678f8Sopenharmony_ci}
106518678f8Sopenharmony_ci
107518678f8Sopenharmony_cibool DhcpServerImpl::IsRemoteDied(void)
108518678f8Sopenharmony_ci{
109518678f8Sopenharmony_ci    return (client_ == nullptr) ? true : client_->IsRemoteDied();
110518678f8Sopenharmony_ci}
111518678f8Sopenharmony_ci
112518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE
113518678f8Sopenharmony_ciErrCode DhcpServerImpl::RegisterDhcpServerCallBack(const std::string& ifname,
114518678f8Sopenharmony_ci    const std::shared_ptr<IDhcpServerCallBack> &callback)
115518678f8Sopenharmony_ci#else
116518678f8Sopenharmony_ciErrCode DhcpServerImpl::RegisterDhcpServerCallBack(const std::string& ifname,
117518678f8Sopenharmony_ci    const sptr<IDhcpServerCallBack> &callback)
118518678f8Sopenharmony_ci#endif
119518678f8Sopenharmony_ci{
120518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
121518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
122518678f8Sopenharmony_ci    return client_->RegisterDhcpServerCallBack(ifname, callback);
123518678f8Sopenharmony_ci}
124518678f8Sopenharmony_ci
125518678f8Sopenharmony_ciErrCode DhcpServerImpl::StartDhcpServer(const std::string& ifname)
126518678f8Sopenharmony_ci{
127518678f8Sopenharmony_ci    DHCP_LOGI("%{public}s  %{public}d  start", __func__, __LINE__);
128518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
129518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
130518678f8Sopenharmony_ci    return client_->StartDhcpServer(ifname);
131518678f8Sopenharmony_ci}
132518678f8Sopenharmony_ci
133518678f8Sopenharmony_ciErrCode DhcpServerImpl::StopDhcpServer(const std::string& ifname)
134518678f8Sopenharmony_ci{
135518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
136518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
137518678f8Sopenharmony_ci    return client_->StopDhcpServer(ifname);
138518678f8Sopenharmony_ci}
139518678f8Sopenharmony_ci
140518678f8Sopenharmony_ciErrCode DhcpServerImpl::PutDhcpRange(const std::string& tagName, const DhcpRange& range)
141518678f8Sopenharmony_ci{
142518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
143518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
144518678f8Sopenharmony_ci    return client_->PutDhcpRange(tagName, range);
145518678f8Sopenharmony_ci}
146518678f8Sopenharmony_ci
147518678f8Sopenharmony_ciErrCode DhcpServerImpl::RemoveDhcpRange(const std::string& tagName, const DhcpRange& range)
148518678f8Sopenharmony_ci{
149518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
150518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
151518678f8Sopenharmony_ci    return client_->RemoveDhcpRange(tagName, range);
152518678f8Sopenharmony_ci}
153518678f8Sopenharmony_ci
154518678f8Sopenharmony_ciErrCode DhcpServerImpl::RemoveAllDhcpRange(const std::string& tagName)
155518678f8Sopenharmony_ci{
156518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
157518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
158518678f8Sopenharmony_ci    return client_->RemoveAllDhcpRange(tagName);
159518678f8Sopenharmony_ci}
160518678f8Sopenharmony_ci
161518678f8Sopenharmony_ciErrCode DhcpServerImpl::SetDhcpRange(const std::string& ifname, const DhcpRange& range)
162518678f8Sopenharmony_ci{
163518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
164518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
165518678f8Sopenharmony_ci    return client_->SetDhcpRange(ifname, range);
166518678f8Sopenharmony_ci}
167518678f8Sopenharmony_ci
168518678f8Sopenharmony_ciErrCode DhcpServerImpl::SetDhcpName(const std::string& ifname, const std::string& tagName)
169518678f8Sopenharmony_ci{
170518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
171518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
172518678f8Sopenharmony_ci    return client_->SetDhcpName(ifname, tagName);
173518678f8Sopenharmony_ci}
174518678f8Sopenharmony_ci
175518678f8Sopenharmony_ciErrCode DhcpServerImpl::GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& dhcpClientInfo)
176518678f8Sopenharmony_ci{
177518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
178518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
179518678f8Sopenharmony_ci    return client_->GetDhcpClientInfos(ifname, dhcpClientInfo);
180518678f8Sopenharmony_ci}
181518678f8Sopenharmony_ci
182518678f8Sopenharmony_ciErrCode DhcpServerImpl::UpdateLeasesTime(const std::string& leaseTime)
183518678f8Sopenharmony_ci{
184518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
185518678f8Sopenharmony_ci    RETURN_IF_FAIL(GetDhcpServerProxy());
186518678f8Sopenharmony_ci    return client_->UpdateLeasesTime(leaseTime);
187518678f8Sopenharmony_ci}
188518678f8Sopenharmony_ci}  // namespace DHCP
189518678f8Sopenharmony_ci}  // namespace OHOS
190