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