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_server_callback_stub.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "dhcp_logger.h"
18 #include "securec.h"
19
20 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServreCallBackStub");
21
22 namespace OHOS {
23 namespace DHCP {
DhcpServreCallBackStub()24 DhcpServreCallBackStub::DhcpServreCallBackStub() : callback_(nullptr), mRemoteDied(false)
25 {
26 DHCP_LOGI("DhcpServreCallBackStub Enter DhcpServreCallBackStub");
27 }
28
~DhcpServreCallBackStub()29 DhcpServreCallBackStub::~DhcpServreCallBackStub()
30 {
31 DHCP_LOGI("DhcpServreCallBackStub Enter ~DhcpServreCallBackStub");
32 }
33
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int DhcpServreCallBackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
35 MessageOption &option)
36 {
37 DHCP_LOGI("DhcpServreCallBackStub::OnRemoteRequest, code:%{public}u", code);
38 if (data.ReadInterfaceToken() != GetDescriptor()) {
39 DHCP_LOGE("Sta callback stub token verification error: %{public}d", code);
40 return DHCP_E_FAILED;
41 }
42
43 int exception = data.ReadInt32();
44 if (exception) {
45 DHCP_LOGE("DhcpServreCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception);
46 return DHCP_E_FAILED;
47 }
48 int ret = -1;
49 switch (code) {
50 case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_STATUS_CHANGE): {
51 ret = RemoteOnServerStatusChanged(code, data, reply);
52 break;
53 }
54 case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_LEASES_CHANGE): {
55 ret = RemoteOnServerLeasesChanged(code, data, reply);
56 break;
57 }
58 case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SER_EXIT_CHANGE): {
59 ret = RemoteOnServerSerExitChanged(code, data, reply);
60 break;
61 }
62 case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_SUCCESS): {
63 ret = RemoteOnServerSuccess(code, data, reply);
64 break;
65 }
66 default: {
67 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 break;
69 }
70 }
71 DHCP_LOGI("DhcpClientCallBackStub OnRemoteRequest, ret:%{public}d", ret);
72 return ret;
73 }
74
RegisterCallBack(const sptr<IDhcpServerCallBack> & callBack)75 void DhcpServreCallBackStub::RegisterCallBack(const sptr<IDhcpServerCallBack> &callBack)
76 {
77 if (callBack == nullptr) {
78 DHCP_LOGE("DhcpServreCallBackStub:callBack is nullptr!");
79 return;
80 }
81 callback_ = callBack;
82 }
83
IsRemoteDied() const84 bool DhcpServreCallBackStub::IsRemoteDied() const
85 {
86 return mRemoteDied;
87 }
88
SetRemoteDied(bool val)89 void DhcpServreCallBackStub::SetRemoteDied(bool val)
90 {
91 DHCP_LOGI("DhcpServreCallBackStub::SetRemoteDied, state:%{public}d!", val);
92 mRemoteDied = val;
93 }
94
OnServerStatusChanged(int status)95 void DhcpServreCallBackStub::OnServerStatusChanged(int status)
96 {
97 DHCP_LOGI("DhcpServreCallBackStub::OnServerStatusChanged, status:%{public}d", status);
98 if (callback_) {
99 callback_->OnServerStatusChanged(status);
100 }
101 }
102
OnServerLeasesChanged(const std::string & ifname,std::vector<std::string> & leases)103 void DhcpServreCallBackStub::OnServerLeasesChanged(const std::string& ifname, std::vector<std::string>& leases)
104 {
105 DHCP_LOGI("DhcpServreCallBackStub::OnServerLeasesChanged, ifname:%{public}s", ifname.c_str());
106 if (callback_) {
107 callback_->OnServerLeasesChanged(ifname, leases);
108 }
109 }
110
OnServerSuccess(const std::string & ifname,std::vector<DhcpStationInfo> & stationInfos)111 void DhcpServreCallBackStub::OnServerSuccess(const std::string &ifname, std::vector<DhcpStationInfo> &stationInfos)
112 {
113 DHCP_LOGI("DhcpServreCallBackStub::OnServerSuccess, ifname:%{public}s", ifname.c_str());
114 if (callback_) {
115 callback_->OnServerSuccess(ifname.c_str(), stationInfos);
116 }
117 }
118
OnServerSerExitChanged(const std::string & ifname)119 void DhcpServreCallBackStub::OnServerSerExitChanged(const std::string& ifname)
120 {
121 DHCP_LOGI("DhcpServreCallBackStub::OnWifiWpsStateChanged, ifname:%{public}s", ifname.c_str());
122 if (callback_) {
123 callback_->OnServerSerExitChanged(ifname);
124 }
125 }
126
RemoteOnServerStatusChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)127 int DhcpServreCallBackStub::RemoteOnServerStatusChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
128 {
129 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
130 // callback OnServerStatusChanged
131 int state = data.ReadInt32();
132 OnServerStatusChanged(state);
133 reply.WriteInt32(0);
134 return 0;
135 }
136
RemoteOnServerSuccess(uint32_t code,MessageParcel & data,MessageParcel & reply)137 int DhcpServreCallBackStub::RemoteOnServerSuccess(uint32_t code, MessageParcel &data, MessageParcel &reply)
138 {
139 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
140 int size = data.ReadInt32();
141 if (size < 0 || size > MAXIMUM_SIZE) {
142 reply.WriteInt32(0);
143 return DHCP_E_SUCCESS;
144 }
145 std::string ifName = data.ReadString();
146 std::vector<DhcpStationInfo> stationInfos;
147 for (int i = 0; i < size; i++) {
148 std::string deviceName = data.ReadString();
149 std::string macAddress = data.ReadString();
150 std::string ipAddress = data.ReadString();
151 DhcpStationInfo stationInfo;
152 if (memset_s(&stationInfo, sizeof(DhcpStationInfo), 0, sizeof(DhcpStationInfo)) != EOK) {
153 DHCP_LOGE("DhcpStationInfo memset_s failed!");
154 return DHCP_E_FAILED;
155 }
156 if (memcpy_s(stationInfo.ipAddr, sizeof(stationInfo.ipAddr),
157 ipAddress.c_str(), IP_ADDR_STR_LEN) != EOK) {
158 DHCP_LOGE("ipAddr memcpy_s error!");
159 return DHCP_E_FAILED;
160 }
161 if (memcpy_s(stationInfo.macAddr, sizeof(stationInfo.macAddr),
162 macAddress.c_str(), MAC_ADDR_STR_LEN) != EOK) {
163 DHCP_LOGE("macAddr memcpy_s error!");
164 return DHCP_E_FAILED;
165 }
166 if (memcpy_s(stationInfo.deviceName, sizeof(stationInfo.deviceName),
167 deviceName.c_str(), DEVICE_NAME_STR_LEN) != EOK) {
168 DHCP_LOGE("deviceName memcpy_s error!");
169 return DHCP_E_FAILED;
170 }
171 stationInfos.emplace_back(stationInfo);
172 }
173 OnServerSuccess(ifName, stationInfos);
174 reply.WriteInt32(0);
175 return DHCP_E_SUCCESS;
176 }
177
RemoteOnServerLeasesChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)178 int DhcpServreCallBackStub::RemoteOnServerLeasesChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
179 {
180 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
181 reply.WriteInt32(0);
182 return 0;
183 }
184
RemoteOnServerSerExitChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)185 int DhcpServreCallBackStub::RemoteOnServerSerExitChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
186 {
187 DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
188 reply.WriteInt32(0);
189 return 0;
190 }
191
192 } // namespace DHCP
193 } // namespace OHOS
194