1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <string>
17 #include <map>
18 #include <list>
19 #include <thread>
20 #include <mutex>
21 #include "securec.h"
22 
23 #include "dhcp_client_service_impl.h"
24 #include "dhcp_logger.h"
25 #include "inner_api/include/dhcp_define.h"
26 #include "dhcp_result.h"
27 
28 DEFINE_DHCPLOG_DHCP_LABEL("DhcpResult");
29 
PublishDhcpIpv4Result(struct DhcpIpResult & ipResult)30 bool PublishDhcpIpv4Result(struct DhcpIpResult &ipResult)
31 {
32 #ifndef OHOS_ARCH_LITE
33     OHOS::sptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
34 #else
35     std::shared_ptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
36 #endif
37     if (clientImpl == nullptr) {
38         DHCP_LOGE("PublishDhcpIpv4Result clientImpl is nullptr!");
39         return false;
40     }
41     DHCP_LOGI("PublishDhcpIpv4Result code:%{public}d ifname:%{public}s", ipResult.code, ipResult.ifname.c_str());
42     if (ipResult.code == PUBLISH_CODE_SUCCESS) {
43         if (clientImpl->DhcpIpv4ResultSuccess(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
44             DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultSuccess failed!");
45             return false;
46         }
47     } else if (ipResult.code == PUBLISH_CODE_TIMEOUT) {
48         if (clientImpl->DhcpIpv4ResultTimeOut(ipResult.ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
49             DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultTimeOut failed!");
50             return false;
51         }
52     } else if (ipResult.code == PUBLISH_CODE_EXPIRED) {
53         if (clientImpl->DhcpIpv4ResultExpired(ipResult.ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
54             DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultExpired failed!");
55             return false;
56         }
57     } else if (ipResult.code == PUBLISH_DHCP_OFFER_REPORT) {
58     #ifndef OHOS_ARCH_LITE
59         if (clientImpl->DhcpOfferResultSuccess(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
60             DHCP_LOGE("PublishDhcpIpv4Result DhcpOfferReport failed!");
61             return false;
62         }
63     #endif
64     } else { // PUBLISH_CODE_FAILED
65         if (clientImpl->DhcpIpv4ResultFail(ipResult) != OHOS::DHCP::DHCP_OPT_SUCCESS) {
66             DHCP_LOGE("PublishDhcpIpv4Result DhcpIpv4ResultFail failed!");
67             return false;
68         }
69     }
70     return true;
71 }
72 
DhcpIpv6TimerCallbackEvent(const char * ifname)73 bool DhcpIpv6TimerCallbackEvent(const char *ifname)
74 {
75     DHCP_LOGI("DhcpIpv6TimerCallbackEvent ifname:%{public}s", ifname);
76     if (ifname == nullptr) {
77         DHCP_LOGE("DhcpIpv6TimerCallbackEvent ifname is nullptr!");
78         return false;
79     }
80 #ifndef OHOS_ARCH_LITE
81     OHOS::sptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
82 #else
83     std::shared_ptr<OHOS::DHCP::DhcpClientServiceImpl> clientImpl = OHOS::DHCP::DhcpClientServiceImpl::GetInstance();
84 #endif
85     if ((clientImpl != nullptr) && (clientImpl->DhcpIpv6ResultTimeOut(ifname) != OHOS::DHCP::DHCP_OPT_SUCCESS)) {
86         DHCP_LOGE("DhcpIpv6TimerCallbackEvent DhcpIpv6ResultTimeOut failed!");
87         return false;
88     }
89     return true;
90 }