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_manager_service_ipc_interface_code.h"
17 #include "ipc_skeleton.h"
18 #include "rpc_errno.h"
19 #include "dhcp_logger.h"
20 #include "dhcp_sdk_define.h"
21 
22 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientCallbackProxyLite");
23 namespace OHOS {
24 namespace DHCP {
25 #ifndef OHOS_EUPDATER
DhcpClientCallbackProxy(SvcIdentity * sid)26 DhcpClientCallbackProxy::DhcpClientCallbackProxy(SvcIdentity *sid) : sid_(*sid)
27 {}
28 #endif
29 
~DhcpClientCallbackProxy()30 DhcpClientCallbackProxy::~DhcpClientCallbackProxy()
31 {
32 #ifndef OHOS_EUPDATER
33     ReleaseSvc(sid_);
34 #endif
35 }
36 
OnIpSuccessChanged(int status,const std::string & ifname,DhcpResult & result)37 void DhcpClientCallbackProxy::OnIpSuccessChanged(int status, const std::string& ifname, DhcpResult& result)
38 {
39     DHCP_LOGI("DhcpClientCallbackProxy OnIpSuccessChanged status:%{public}d ifname:%{public}s", status, ifname.c_str());
40     IpcIo data;
41     uint8_t buff[DEFAULT_IPC_SIZE];
42     IpcIoInit(&data, buff, DEFAULT_IPC_SIZE, 0);
43     if (!WriteInterfaceToken(&data, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
44         DHCP_LOGE("Write interface token error: %{public}s", __func__);
45         return;
46     }
47     (void)WriteInt32(&data, 0);
48     (void)WriteInt32(&data, status);
49     (void)WriteString(&data, ifname.c_str());
50     (void)WriteInt32(&data, result.iptype);
51     (void)WriteBool(&data, result.isOptSuc);
52     (void)WriteInt32(&data, result.uLeaseTime);
53     (void)WriteInt32(&data, result.uAddTime);
54     (void)WriteInt32(&data, result.uGetTime);
55     (void)WriteString(&data, result.strYourCli.c_str());
56     (void)WriteString(&data, result.strServer.c_str());
57     (void)WriteString(&data, result.strSubnet.c_str());
58     (void)WriteString(&data, result.strDns1.c_str());
59     (void)WriteString(&data, result.strDns2.c_str());
60     (void)WriteString(&data, result.strRouter1.c_str());
61     (void)WriteString(&data, result.strRouter2.c_str());
62     (void)WriteString(&data, result.strVendor.c_str());
63     (void)WriteString(&data, result.strLinkIpv6Addr.c_str());
64     (void)WriteString(&data, result.strRandIpv6Addr.c_str());
65     (void)WriteString(&data, result.strLocalAddr1.c_str());
66     (void)WriteString(&data, result.strLocalAddr2.c_str());
67     for (auto dnsAddr : result.vectorDnsAddr) {
68         (void)WriteString(&data, dnsAddr.c_str());
69     }
70 
71     IpcIo reply;
72     MessageOption option;
73     MessageOptionInit(&option);
74     option.flags = TF_OP_ASYNC;
75 #ifndef OHOS_EUPDATER
76     int ret = SendRequest(sid_, static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE), &data, &reply,
77         option, nullptr);
78     if (ret != ERR_NONE) {
79         DHCP_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
80             static_cast<int32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE), ret);
81     }
82 #endif
83     DHCP_LOGI("OnIpSuccessChanged request success");
84     return;
85 }
86 
OnIpFailChanged(int status,const std::string & ifname,const std::string & reason)87 void DhcpClientCallbackProxy::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
88 {
89     DHCP_LOGI("DhcpClientCallbackProxy OnIpFailChanged status:%{public}d ifname:%{public}s", status, ifname.c_str());
90     IpcIo data;
91     uint8_t buff[DEFAULT_IPC_SIZE];
92     IpcIoInit(&data, buff, DEFAULT_IPC_SIZE, 0);
93     if (!WriteInterfaceToken(&data, DECLARE_INTERFACE_DESCRIPTOR_L1, DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH)) {
94         DHCP_LOGE("Write interface token error: %{public}s", __func__);
95         return;
96     }
97     (void)WriteInt32(&data, 0);
98     (void)WriteInt32(&data, status);
99     (void)WriteString(&data, ifname.c_str());
100     (void)WriteString(&data, reason.c_str());
101 
102     IpcIo reply;
103     MessageOption option;
104     MessageOptionInit(&option);
105     option.flags = TF_OP_ASYNC;
106 #ifndef OHOS_EUPDATER
107     int ret = SendRequest(sid_, static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_FAIL_CHANGE), &data, &reply,
108         option, nullptr);
109     if (ret != 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), ret);
112     }
113 #endif
114     DHCP_LOGI("DhcpClientCallbackProxy OnIpFailChanged send client request success");
115     return;
116 }
117 }  // namespace DHCP
118 }  // namespace OHOS
119