1 /*
2 * Copyright (C) 2021-2023 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_stub.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "dhcp_logger.h"
18
19 DEFINE_DHCPLOG_DHCP_LABEL("DhcpClientCallBackStub");
20 namespace OHOS {
21 namespace Wifi {
DhcpClientCallBackStub()22 DhcpClientCallBackStub::DhcpClientCallBackStub() : callback_(nullptr), mRemoteDied(false)
23 {
24 DHCP_LOGI("DhcpClientCallBackStub Enter DhcpClientCallBackStub");
25 }
26
~DhcpClientCallBackStub()27 DhcpClientCallBackStub::~DhcpClientCallBackStub()
28 {
29 DHCP_LOGI("DhcpClientCallBackStub Enter ~DhcpClientCallBackStub");
30 }
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int DhcpClientCallBackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
33 MessageOption &option)
34 {
35 DHCP_LOGI("DhcpClientCallBackStub::OnRemoteRequest, code:%{public}d", code);
36 if (data.ReadInterfaceToken() != GetDescriptor()) {
37 DHCP_LOGE("Sta callback stub token verification error: %{public}d", code);
38 return DHCP_E_FAILED;
39 }
40 int exception = data.ReadInt32();
41 if (exception) {
42 DHCP_LOGE("DhcpClientCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception);
43 return DHCP_E_FAILED;
44 }
45 int ret = -1;
46 switch (code) {
47 case static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_SUCCESS_CHANGE): {
48 ret = RemoteOnIpSuccessChanged(code, data, reply);
49 break;
50 }
51 case static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_CBK_CMD_IP_FAIL_CHANGE): {
52 ret = RemoteOnIpFailChanged(code, data, reply);
53 break;
54 }
55 default: {
56 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57 break;
58 }
59 }
60 DHCP_LOGI("DhcpClientCallBackStub OnRemoteRequest, ret:%{public}d", ret);
61 return ret;
62 }
63
RegisterCallBack(const sptr<IDhcpClientCallBack> & callBack)64 void DhcpClientCallBackStub::RegisterCallBack(const sptr<IDhcpClientCallBack> &callBack)
65 {
66 if (callBack == nullptr) {
67 DHCP_LOGE("DhcpClientCallBackStub:callBack is nullptr!");
68 return;
69 }
70 callback_ = callBack;
71 }
72
IsRemoteDied() const73 bool DhcpClientCallBackStub::IsRemoteDied() const
74 {
75 return mRemoteDied;
76 }
77
SetRemoteDied(bool val)78 void DhcpClientCallBackStub::SetRemoteDied(bool val)
79 {
80 DHCP_LOGI("DhcpClientCallBackStub::SetRemoteDied, state:%{public}d!", val);
81 mRemoteDied = val;
82 }
83
OnIpSuccessChanged(int status,const std::string & ifname,DhcpResult & result)84 void DhcpClientCallBackStub::OnIpSuccessChanged(int status, const std::string& ifname, DhcpResult& result)
85 {
86 DHCP_LOGI("DhcpClientCallBackStub::OnIpSuccessChanged, status:%{public}d!", status);
87 if (callback_) {
88 callback_->OnIpSuccessChanged(status, ifname, result);
89 }
90 }
91
OnIpFailChanged(int status,const std::string & ifname,const std::string & reason)92 void DhcpClientCallBackStub::OnIpFailChanged(int status, const std::string& ifname, const std::string& reason)
93 {
94 DHCP_LOGI("DhcpClientCallBackStub::OnIpFailChanged, status:%{public}d!", status);
95 if (callback_) {
96 callback_->OnIpFailChanged(status, ifname, reason);
97 }
98 }
99
100
RemoteOnIpSuccessChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)101 int DhcpClientCallBackStub::RemoteOnIpSuccessChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
102 {
103 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
104 int state = data.ReadInt32();
105 std::string ifname = data.ReadString();
106 DhcpResult result;
107 result.iptype = data.ReadInt32();
108 result.isOptSuc = data.ReadBool();
109 result.uLeaseTime = data.ReadInt32();
110 result.uAddTime = data.ReadInt32();
111 result.uGetTime = data.ReadInt32();
112 result.strYourCli = data.ReadString();
113 result.strServer = data.ReadString();
114 result.strSubnet = data.ReadString();
115 result.strDns1 = data.ReadString();
116 result.strDns2 = data.ReadString();
117 result.strRouter1 = data.ReadString();
118 result.strRouter2 = data.ReadString();
119 result.strVendor = data.ReadString();
120 OnIpSuccessChanged(state, ifname, result);
121 reply.WriteInt32(0);
122 return 0;
123 }
124
RemoteOnIpFailChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)125 int DhcpClientCallBackStub::RemoteOnIpFailChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
126 {
127 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
128 int state = data.ReadInt32();
129 std::string ifname = data.ReadString();
130 std::string reason = data.ReadString();
131 OnIpFailChanged(state, ifname, reason);
132 reply.WriteInt32(0);
133 return 0;
134 }
135 } // namespace Wifi
136 } // namespace OHOS