1518678f8Sopenharmony_ci/*
2518678f8Sopenharmony_ci * Copyright (C) 2021-2022 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
16518678f8Sopenharmony_ci#include <string>
17518678f8Sopenharmony_ci#include <map>
18518678f8Sopenharmony_ci#include <list>
19518678f8Sopenharmony_ci#include <thread>
20518678f8Sopenharmony_ci#include <mutex>
21518678f8Sopenharmony_ci#include "securec.h"
22518678f8Sopenharmony_ci
23518678f8Sopenharmony_ci#include "dhcp_client_service_impl.h"
24518678f8Sopenharmony_ci#include "dhcp_logger.h"
25518678f8Sopenharmony_ci#include "inner_api/include/dhcp_define.h"
26518678f8Sopenharmony_ci#include "dhcp_result.h"
27518678f8Sopenharmony_ci
28518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpResult");
29518678f8Sopenharmony_ci
30518678f8Sopenharmony_cibool PublishDhcpIpv4Result(struct DhcpIpResult &ipResult)
31518678f8Sopenharmony_ci{
32518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
33518678f8Sopenharmony_ci    OHOS::sptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
34518678f8Sopenharmony_ci#else
35518678f8Sopenharmony_ci    std::shared_ptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
36518678f8Sopenharmony_ci#endif
37518678f8Sopenharmony_ci    if (clientImpl == nullptr) {
38518678f8Sopenharmony_ci        DHCP_LOGE("PublishDhcpIpv4Result clientImpl is nullptr!");
39518678f8Sopenharmony_ci        return false;
40518678f8Sopenharmony_ci    }
41518678f8Sopenharmony_ci    DHCP_LOGI("PublishDhcpIpv4Result code:%{public}d ifname:%{public}s", ipResult.code, ipResult.ifname.c_str());
42518678f8Sopenharmony_ci    if (ipResult.code == PUBLISH_CODE_SUCCESS) {
43518678f8Sopenharmony_ci        if (clientImpl->DhcpIpv4ResultSuccess(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
44518678f8Sopenharmony_ci            DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultSuccess failed!");
45518678f8Sopenharmony_ci            return false;
46518678f8Sopenharmony_ci        }
47518678f8Sopenharmony_ci    } else if (ipResult.code == PUBLISH_CODE_TIMEOUT) {
48518678f8Sopenharmony_ci        if (clientImpl->DhcpIpv4ResultTimeOut(ipResult.ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
49518678f8Sopenharmony_ci            DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultTimeOut failed!");
50518678f8Sopenharmony_ci            return false;
51518678f8Sopenharmony_ci        }
52518678f8Sopenharmony_ci    } else if (ipResult.code == PUBLISH_CODE_EXPIRED) {
53518678f8Sopenharmony_ci        if (clientImpl->DhcpIpv4ResultExpired(ipResult.ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
54518678f8Sopenharmony_ci            DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultExpired failed!");
55518678f8Sopenharmony_ci            return false;
56518678f8Sopenharmony_ci        }
57518678f8Sopenharmony_ci    } else if (ipResult.code == PUBLISH_DHCP_OFFER_REPORT) {
58518678f8Sopenharmony_ci    #ifndef OHOS_ARCH_LITE
59518678f8Sopenharmony_ci        if (clientImpl->DhcpOfferResultSuccess(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
60518678f8Sopenharmony_ci            DHCP_LOGE("PublishDhcpIpv4Result DhcpOfferReport failed!");
61518678f8Sopenharmony_ci            return false;
62518678f8Sopenharmony_ci        }
63518678f8Sopenharmony_ci    #endif
64518678f8Sopenharmony_ci    } else { // PUBLISH_CODE_FAILED
65518678f8Sopenharmony_ci        if (clientImpl->DhcpIpv4ResultFail(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
66518678f8Sopenharmony_ci            DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultFail failed!");
67518678f8Sopenharmony_ci            return false;
68518678f8Sopenharmony_ci        }
69518678f8Sopenharmony_ci    }
70518678f8Sopenharmony_ci    return true;
71518678f8Sopenharmony_ci}
72518678f8Sopenharmony_ci
73518678f8Sopenharmony_cibool DhcpIpv6TimerCallbackEvent(const char *ifname)
74518678f8Sopenharmony_ci{
75518678f8Sopenharmony_ci    DHCP_LOGI("DhcpIpv6TimerCallbackEvent ifname:%{public}s", ifname);
76518678f8Sopenharmony_ci    if (ifname == nullptr) {
77518678f8Sopenharmony_ci        DHCP_LOGE("DhcpIpv6TimerCallbackEvent ifname is nullptr!");
78518678f8Sopenharmony_ci        return false;
79518678f8Sopenharmony_ci    }
80518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
81518678f8Sopenharmony_ci    OHOS::sptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
82518678f8Sopenharmony_ci#else
83518678f8Sopenharmony_ci    std::shared_ptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
84518678f8Sopenharmony_ci#endif
85518678f8Sopenharmony_ci    if ((clientImpl != nullptr) && (clientImpl->DhcpIpv6ResultTimeOut(ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS)) {
86518678f8Sopenharmony_ci        DHCP_LOGE("DhcpIpv6TimerCallbackEvent DhcpIpv6ResultTimeOut failed!");
87518678f8Sopenharmony_ci        return false;
88518678f8Sopenharmony_ci    }
89518678f8Sopenharmony_ci    return true;
90518678f8Sopenharmony_ci}