• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dhcp_client_callback_proxy.h"
16 #include "dhcp_logger.h"
17 #include "dhcp_manager_service_ipc_interface_code.h"
18 
19 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientCallbackProxy");
20 
21 namespace OHOS {
22 namespace DHCP {
DhcpClientCallbackProxy(const sptr<IRemoteObject> & impl)23 DhcpClientCallbackProxy::DhcpClientCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IDhcpClientCallBack>(impl)
24 {}
25 
~DhcpClientCallbackProxy()26 DhcpClientCallbackProxy::~DhcpClientCallbackProxy()
27 {}
28 
WriteDhcpResult(const DhcpResult & result,MessageParcel & data)29 void DhcpClientCallbackProxy::WriteDhcpResult(const DhcpResult& result, MessageParcel &data)
30 {
31     data.WriteInt32(result.iptype);
32     data.WriteBool(result.isOptSuc);
33     data.WriteInt32(result.uLeaseTime);
34     data.WriteInt32(result.uAddTime);
35     data.WriteInt32(result.uGetTime);
36     data.WriteString(result.strYourCli);
37     data.WriteString(result.strServer);
38     data.WriteString(result.strSubnet);
39     data.WriteString(result.strDns1);
40     data.WriteString(result.strDns2);
41     data.WriteString(result.strRouter1);
42     data.WriteString(result.strRouter2);
43     data.WriteString(result.strVendor);
44     data.WriteString(result.strLinkIpv6Addr);
45     data.WriteString(result.strRandIpv6Addr);
46     data.WriteString(result.strLocalAddr1);
47     data.WriteString(result.strLocalAddr2);
48     data.WriteInt32(result.vectorDnsAddr.size());
49     for (auto &dns : result.vectorDnsAddr) {
50         data.WriteString(dns);
51     }
52 }
53 
OnIpSuccessChanged(int status,const std::string & ifname,DhcpResult & result)54 void DhcpClientCallbackProxy::OnIpSuccessChanged(int status, const std::string& ifname, DhcpResult& result)
55 {
56     DHCP_LOGI("WifiDeviceCallBackProxy::OnIpSuccessChanged");
57     MessageOption option;
58     MessageParcel data;
59     MessageParcel reply;
60     if (!data.WriteInterfaceToken(GetDescriptor())) {
61         DHCP_LOGE("Write interface token error: %{public}s", __func__);
62         return;
63     }
64     sptr<IRemoteObject> remote = Remote();
65     if (remote == nullptr) {
66         DHCP_LOGE("Remote is null");
67         return;
68     }
69     data.WriteInt32(0);
70     data.WriteInt32(status);
71     data.WriteString(ifname);
72     WriteDhcpResult(result, data);
73     int error = remote->SendRequest(
74         static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE), data, reply, option);
75     if (error != ERR_NONE) {
76         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
77             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE), error);
78         return;
79     }
80     int exception = reply.ReadInt32();
81     if (exception) {
82         DHCP_LOGE("notify wifi state change failed!");
83     }
84     DHCP_LOGI("send client request success");
85     return;
86 }
87 
OnIpFailChanged(int status,const std::string & ifname,const std::string & reason)88 void DhcpClientCallbackProxy::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
89 {
90     DHCP_LOGI("DhcpClientCallbackProxy OnIpFailChanged status:%{public}d ifname:%{public}s", status, ifname.c_str());
91     MessageOption option;
92     MessageParcel data;
93     MessageParcel reply;
94     if (!data.WriteInterfaceToken(GetDescriptor())) {
95         DHCP_LOGE("Write interface token error: %{public}s", __func__);
96         return;
97     }
98     sptr<IRemoteObject> remote = Remote();
99     if (remote == nullptr) {
100         DHCP_LOGE("Remote is null");
101         return;
102     }
103     data.WriteInt32(0);
104     data.WriteInt32(status);
105     data.WriteString(ifname);
106     data.WriteString(reason);
107     int error = remote->SendRequest(
108         static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_FAIL_CHANGE), data, reply, option);
109     if (error != ERR_NONE) {
110         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
111             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_FAIL_CHANGE), error);
112         return;
113     }
114     int exception = reply.ReadInt32();
115     if (exception) {
116         DHCP_LOGE("notify wifi state change failed!");
117         return;
118     }
119     DHCP_LOGI("DhcpClientCallbackProxy OnIpFailChanged send client request success");
120     return;
121 }
122 
OnDhcpOfferReport(int status,const std::string & ifname,DhcpResult & result)123 void DhcpClientCallbackProxy::OnDhcpOfferReport(int status, const std::string& ifname, DhcpResult& result)
124 {
125     DHCP_LOGI("WifiDeviceCallBackProxy::OnDhcpOfferReport");
126     MessageOption option;
127     MessageParcel data;
128     MessageParcel reply;
129     if (!data.WriteInterfaceToken(GetDescriptor())) {
130         DHCP_LOGE("Write interface token error: %{public}s", __func__);
131         return;
132     }
133     sptr<IRemoteObject> remote = Remote();
134     if (remote == nullptr) {
135         DHCP_LOGE("Remote is null");
136         return;
137     }
138     data.WriteInt32(0);
139     data.WriteInt32(status);
140     data.WriteString(ifname);
141     WriteDhcpResult(result, data);
142     int error = remote->SendRequest(static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_DHCP_OFFER),
143         data, reply, option);
144     if (error != ERR_NONE) {
145         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
146             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_DHCP_OFFER), error);
147         return;
148     }
149     int exception = reply.ReadInt32();
150     if (exception) {
151         DHCP_LOGE("notify wifi dhcp offer failed!");
152     }
153     return;
154 }
155 }  // namespace DHCP
156 }  // namespace OHOS
157