1 /*
2 * Copyright (C) 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 "ethernet_dhcp_controller.h"
17
18 #include <string>
19
20 #include "dhcp_define.h"
21 #include "dhcp_service.h"
22 #include "ethernet_dhcp_callback.h"
23 #include "netmgr_ext_log_wrapper.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
27 constexpr int32_t DHCP_TIMEOUT = 300;
OnSuccess(int status,const std::string & ifname,OHOS::Wifi::DhcpResult & result)28 void EthernetDhcpController::EthernetDhcpControllerResultNotify::OnSuccess(int status, const std::string &ifname,
29 OHOS::Wifi::DhcpResult &result)
30 {
31 ethDhcpController_.OnDhcpSuccess(ifname, result);
32 }
33
OnFailed(int status,const std::string & ifname,const std::string & reason)34 void EthernetDhcpController::EthernetDhcpControllerResultNotify::OnFailed(int status, const std::string &ifname,
35 const std::string &reason)
36 {
37 return;
38 }
39
OnSerExitNotify(const std::string & ifname)40 void EthernetDhcpController::EthernetDhcpControllerResultNotify::OnSerExitNotify(const std::string &ifname)
41 {
42 return;
43 }
44
RegisterDhcpCallback(sptr<EthernetDhcpCallback> callback)45 void EthernetDhcpController::RegisterDhcpCallback(sptr<EthernetDhcpCallback> callback)
46 {
47 cbObject_ = callback;
48 }
49
StartDhcpClient(const std::string & iface,bool bIpv6)50 void EthernetDhcpController::StartDhcpClient(const std::string &iface, bool bIpv6)
51 {
52 NETMGR_EXT_LOG_D("Start dhcp client iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
53 dhcpService_->StartDhcpClient(iface, bIpv6);
54 if (dhcpService_->GetDhcpResult(iface, dhcpResultNotify_.get(), DHCP_TIMEOUT) != 0) {
55 NETMGR_EXT_LOG_D(" Dhcp connection failed.\n");
56 }
57 }
58
StopDhcpClient(const std::string & iface,bool bIpv6)59 void EthernetDhcpController::StopDhcpClient(const std::string &iface, bool bIpv6)
60 {
61 NETMGR_EXT_LOG_D("Stop dhcp client iface[%{public}s] ipv6[%{public}d]", iface.c_str(), bIpv6);
62 dhcpService_->StopDhcpClient(iface, bIpv6);
63 }
64
OnDhcpSuccess(const std::string & iface,OHOS::Wifi::DhcpResult & result)65 void EthernetDhcpController::OnDhcpSuccess(const std::string &iface, OHOS::Wifi::DhcpResult &result)
66 {
67 if (cbObject_ == nullptr) {
68 NETMGR_EXT_LOG_E("Error OnDhcpSuccess No Cb!");
69 return;
70 }
71 EthernetDhcpCallback::DhcpResult dhcpResult;
72 dhcpResult.iface = iface;
73 dhcpResult.ipAddr = result.strYourCli;
74 dhcpResult.gateWay = result.strRouter1;
75 dhcpResult.subNet = result.strSubnet;
76 dhcpResult.route1 = result.strRouter1;
77 dhcpResult.route2 = result.strRouter2;
78 dhcpResult.dns1 = result.strDns1;
79 dhcpResult.dns2 = result.strDns2;
80 cbObject_->OnDhcpSuccess(dhcpResult);
81 }
82
OnDhcpFailed(int status,const std::string & ifname,const std::string & reason)83 void EthernetDhcpController::OnDhcpFailed(int status, const std::string &ifname, const std::string &reason) {}
84 } // namespace NetManagerStandard
85 } // namespace OHOS
86