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 "wifi_p2p_stub.h"
16 #include "wifi_logger.h"
17 #include "wifi_errcode.h"
18 #include "wifi_internal_event_dispatcher.h"
19 #include "wifi_p2p_callback_proxy.h"
20 #include "wifi_p2p_death_recipient.h"
21 #include "wifi_common_def.h"
22 #include "wifi_manager_service_ipc_interface_code.h"
23
24 DEFINE_WIFILOG_P2P_LABEL("WifiP2pStub");
25
26 namespace OHOS {
27 namespace Wifi {
WifiP2pStub()28 WifiP2pStub::WifiP2pStub() : mSingleCallback(false)
29 {
30 InitHandleMap();
31 deathRecipient_ = nullptr;
32 }
33
~WifiP2pStub()34 WifiP2pStub::~WifiP2pStub()
35 {
36 deathRecipient_ = nullptr;
37 }
38
InitHandleMapEx()39 void WifiP2pStub::InitHandleMapEx()
40 {
41 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REGISTER_CALLBACK)] = [this](uint32_t code,
42 MessageParcel &data, MessageParcel &reply,
43 MessageOption &option) { OnRegisterCallBack(code, data, reply, option); };
44 handleFuncMap[static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES)] = [this](uint32_t code,
45 MessageParcel &data, MessageParcel &reply,
46 MessageOption &option) { OnGetSupportedFeatures(code, data, reply, option); };
47 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME)] = [this](uint32_t code,
48 MessageParcel &data, MessageParcel &reply,
49 MessageOption &option) { OnSetP2pDeviceName(code, data, reply, option); };
50 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO)] = [this](uint32_t code,
51 MessageParcel &data, MessageParcel &reply,
52 MessageOption &option) { OnSetP2pWfdInfo(code, data, reply, option); };
53 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP)] = [this](uint32_t code,
54 MessageParcel &data, MessageParcel &reply,
55 MessageOption &option) { OnHid2dRequestGcIp(code, data, reply, option); };
56 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE)] =
57 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
58 OnHid2dSharedlinkIncrease(code, data, reply, option);
59 };
60 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE)] =
61 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
62 OnHid2dSharedlinkDecrease(code, data, reply, option);
63 };
64 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP)] = [this](uint32_t code,
65 MessageParcel &data, MessageParcel &reply,
66 MessageOption &option) { OnHid2dCreateGroup(code, data, reply, option); };
67 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP)] =
68 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
69 OnHid2dRemoveGcGroup(code, data, reply, option);
70 };
71 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT)] = [this](uint32_t code,
72 MessageParcel &data, MessageParcel &reply,
73 MessageOption &option) { OnHid2dConnect(code, data, reply, option); };
74 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP)] = [this](uint32_t code,
75 MessageParcel &data, MessageParcel &reply,
76 MessageOption &option) { OnHid2dConfigIPAddr(code, data, reply, option); };
77 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP)] = [this](uint32_t code,
78 MessageParcel &data, MessageParcel &reply,
79 MessageOption &option) { OnHid2dReleaseIPAddr(code, data, reply, option); };
80 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL)] =
81 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
82 OnHid2dGetRecommendChannel(code, data, reply, option);
83 };
84 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST)] = [this](uint32_t code,
85 MessageParcel &data, MessageParcel &reply,
86 MessageOption &option) { OnHid2dGetChannelListFor5G(code, data, reply, option); };
87 InitHandleMapExPart3();
88 }
89
InitHandleMapExPart3()90 void WifiP2pStub::InitHandleMapExPart3()
91 {
92 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG)] = [this](uint32_t code,
93 MessageParcel &data, MessageParcel &reply,
94 MessageOption &option) { OnHid2dGetSelfWifiCfgInfo(code, data, reply, option); };
95 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG)] = [this](uint32_t code,
96 MessageParcel &data, MessageParcel &reply,
97 MessageOption &option) { OnHid2dSetPeerWifiCfgInfo(code, data, reply, option); };
98 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE)] = [this](uint32_t code,
99 MessageParcel &data, MessageParcel &reply,
100 MessageOption &option) { OnQueryP2pLocalDevice(code, data, reply, option); };
101 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE)] = [this](uint32_t code,
102 MessageParcel &data, MessageParcel &reply,
103 MessageOption &option) { OnHid2dSetUpperScene(code, data, reply, option); };
104 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_PEERS)] = [this](uint32_t code,
105 MessageParcel &data, MessageParcel &reply,
106 MessageOption &option) { OnDiscoverPeers(code, data, reply, option); };
107 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE_RANDOM_MAC)] = [this](uint32_t code,
108 MessageParcel &data, MessageParcel &reply,
109 MessageOption &option) { OnDisableRandomMac(code, data, reply, option); };
110 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CHECK_CAN_USE_P2P)] = [this](uint32_t code,
111 MessageParcel &data, MessageParcel &reply,
112 MessageOption &option) { OnCheckCanUseP2p(code, data, reply, option); };
113 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_WIDE_SUPPORTED)] =
114 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
115 OnHid2dIsWideBandwidthSupported(code, data, reply, option);
116 };
117 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_MIRACAST_SINK_CONFIG)] =
118 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
119 OnSetMiracastSinkConfig(code, data, reply, option);
120 };
121 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_SUPPORT_CHANN_FOR_BAND)] =
122 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
123 OnGetSupportChanForBand(code, data, reply, option);
124 };
125 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_P2P_HIGH_PERF_MODE)] =
126 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
127 OnSetP2pHighPerf(code, data, reply, option);
128 };
129 }
130
InitHandleMap()131 void WifiP2pStub::InitHandleMap()
132 {
133 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_ENABLE)] = [this](uint32_t code,
134 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnEnableP2p(code, data, reply, option); };
135 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE)] = [this](uint32_t code,
136 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnDisableP2p(code, data, reply, option); };
137 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_DEVICES)] = [this](uint32_t code,
138 MessageParcel &data, MessageParcel &reply,
139 MessageOption &option) { OnDiscoverDevices(code, data, reply, option); };
140 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES)] =
141 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
142 OnStopDiscoverDevices(code, data, reply, option);
143 };
144 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_SERVICES)] = [this](uint32_t code,
145 MessageParcel &data, MessageParcel &reply,
146 MessageOption &option) { OnDiscoverServices(code, data, reply, option); };
147 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES)] =
148 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
149 OnStopDiscoverServices(code, data, reply, option);
150 };
151 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REQUEST_SERVICES)] = [this](uint32_t code,
152 MessageParcel &data, MessageParcel &reply,
153 MessageOption &option) { OnRequestService(code, data, reply, option); };
154 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES)] = [this](uint32_t code,
155 MessageParcel &data, MessageParcel &reply,
156 MessageOption &option) { OnPutLocalP2pService(code, data, reply, option); };
157 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES)] =
158 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
159 OnDeleteLocalP2pService(code, data, reply, option);
160 };
161 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_START_LISTEN)] = [this](uint32_t code,
162 MessageParcel &data, MessageParcel &reply,
163 MessageOption &option) { OnStartP2pListen(code, data, reply, option); };
164 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_LISTEN)] = [this](uint32_t code,
165 MessageParcel &data, MessageParcel &reply,
166 MessageOption &option) { OnStopP2pListen(code, data, reply, option); };
167 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CREATE_GROUP)] = [this](uint32_t code,
168 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnCreateGroup(code, data, reply, option); };
169 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP)] = [this](uint32_t code,
170 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnRemoveGroup(code, data, reply, option); };
171 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP_CLIENT)] = [this](uint32_t code,
172 MessageParcel &data, MessageParcel &reply,
173 MessageOption &option) { OnRemoveGroupClient(code, data, reply, option); };
174 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP)] = [this](uint32_t code,
175 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnDeleteGroup(code, data, reply, option); };
176 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT)] = [this](uint32_t code,
177 MessageParcel &data, MessageParcel &reply, MessageOption &option) { OnP2pConnect(code, data, reply, option); };
178 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT)] = [this](uint32_t code,
179 MessageParcel &data, MessageParcel &reply,
180 MessageOption &option) { OnP2pCancelConnect(code, data, reply, option); };
181 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO)] = [this](uint32_t code,
182 MessageParcel &data, MessageParcel &reply,
183 MessageOption &option) { OnQueryP2pLinkedInfo(code, data, reply, option); };
184 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP)] = [this](uint32_t code,
185 MessageParcel &data, MessageParcel &reply,
186 MessageOption &option) { OnGetCurrentGroup(code, data, reply, option); };
187 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS)] = [this](uint32_t code,
188 MessageParcel &data, MessageParcel &reply,
189 MessageOption &option) { OnGetP2pEnableStatus(code, data, reply, option); };
190 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS)] = [this](uint32_t code,
191 MessageParcel &data, MessageParcel &reply,
192 MessageOption &option) { OnGetP2pDiscoverStatus(code, data, reply, option); };
193 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS)] =
194 [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
195 OnGetP2pConnectedStatus(code, data, reply, option);
196 };
197 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES)] = [this](uint32_t code,
198 MessageParcel &data, MessageParcel &reply,
199 MessageOption &option) { OnQueryP2pDevices(code, data, reply, option); };
200 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS)] = [this](uint32_t code,
201 MessageParcel &data, MessageParcel &reply,
202 MessageOption &option) { OnQueryP2pGroups(code, data, reply, option); };
203 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES)] = [this](uint32_t code,
204 MessageParcel &data, MessageParcel &reply,
205 MessageOption &option) { OnQueryP2pServices(code, data, reply, option); };
206 InitHandleMapEx();
207 return;
208 }
209
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)210 int WifiP2pStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
211 {
212 if (data.ReadInterfaceToken() != GetDescriptor()) {
213 WIFI_LOGE("P2p stub token verification error: %{public}d", code);
214 return WIFI_OPT_FAILED;
215 }
216
217 HandleFuncMap::iterator iter = handleFuncMap.find(code);
218 if (iter == handleFuncMap.end()) {
219 WIFI_LOGD("not find function to deal, code %{public}u", code);
220 reply.WriteInt32(0);
221 reply.WriteInt32(WIFI_OPT_NOT_SUPPORTED);
222 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
223 } else {
224 int exception = data.ReadInt32();
225 if (exception) {
226 return WIFI_OPT_FAILED;
227 }
228 (iter->second)(code, data, reply, option);
229 }
230 return 0;
231 }
232
OnEnableP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)233 void WifiP2pStub::OnEnableP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
234 {
235 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
236 ErrCode ret = EnableP2p();
237 reply.WriteInt32(0);
238 reply.WriteInt32(ret);
239 return;
240 }
241
OnDisableP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)242 void WifiP2pStub::OnDisableP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
243 {
244 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
245 ErrCode ret = DisableP2p();
246 reply.WriteInt32(0);
247 reply.WriteInt32(ret);
248 return;
249 }
250
OnDiscoverDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)251 void WifiP2pStub::OnDiscoverDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
252 {
253 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
254 ErrCode ret = DiscoverDevices();
255 reply.WriteInt32(0);
256 reply.WriteInt32(ret);
257 return;
258 }
259
OnStopDiscoverDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)260 void WifiP2pStub::OnStopDiscoverDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
261 {
262 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
263 ErrCode ret = StopDiscoverDevices();
264 reply.WriteInt32(0);
265 reply.WriteInt32(ret);
266 return;
267 }
268
OnDiscoverServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)269 void WifiP2pStub::OnDiscoverServices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
270 {
271 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
272 ErrCode ret = DiscoverServices();
273 reply.WriteInt32(0);
274 reply.WriteInt32(ret);
275 return;
276 }
277
OnStopDiscoverServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)278 void WifiP2pStub::OnStopDiscoverServices(
279 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
280 {
281 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
282 ErrCode ret = StopDiscoverServices();
283 reply.WriteInt32(0);
284 reply.WriteInt32(ret);
285 return;
286 }
287
OnRequestService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)288 void WifiP2pStub::OnRequestService(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
289 {
290 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
291 WifiP2pDevice device;
292 WifiP2pServiceRequest request;
293 if (!ReadWifiP2pServiceRequest(data, device, request)) {
294 reply.WriteInt32(0);
295 reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
296 return;
297 }
298 ErrCode ret = RequestService(device, request);
299 reply.WriteInt32(0);
300 reply.WriteInt32(ret);
301 return;
302 }
303
OnPutLocalP2pService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)304 void WifiP2pStub::OnPutLocalP2pService(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
305 {
306 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
307 WifiP2pServiceInfo config;
308 if (!ReadWifiP2pServiceInfo(data, config)) {
309 reply.WriteInt32(0);
310 reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
311 return;
312 }
313 ErrCode ret = PutLocalP2pService(config);
314 reply.WriteInt32(0);
315 reply.WriteInt32(ret);
316 return;
317 }
318
OnDeleteLocalP2pService(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)319 void WifiP2pStub::OnDeleteLocalP2pService(
320 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
321 {
322 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
323 WifiP2pServiceInfo config;
324 if (!ReadWifiP2pServiceInfo(data, config)) {
325 reply.WriteInt32(0);
326 reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
327 return;
328 }
329 ErrCode ret = DeleteLocalP2pService(config);
330 reply.WriteInt32(0);
331 reply.WriteInt32(ret);
332 return;
333 }
334
OnStartP2pListen(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)335 void WifiP2pStub::OnStartP2pListen(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
336 {
337 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
338 int period = data.ReadInt32();
339 int interval = data.ReadInt32();
340 ErrCode ret = StartP2pListen(period, interval);
341 reply.WriteInt32(0);
342 reply.WriteInt32(ret);
343 return;
344 }
345
OnStopP2pListen(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)346 void WifiP2pStub::OnStopP2pListen(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
347 {
348 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
349 ErrCode ret = StopP2pListen();
350 reply.WriteInt32(0);
351 reply.WriteInt32(ret);
352 return;
353 }
354
OnCreateGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)355 void WifiP2pStub::OnCreateGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
356 {
357 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
358 WifiP2pConfig config;
359 ReadWifiP2pConfigData(data, config);
360 ErrCode ret = CreateGroup(config);
361 reply.WriteInt32(0);
362 reply.WriteInt32(ret);
363 return;
364 }
365
OnRemoveGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)366 void WifiP2pStub::OnRemoveGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
367 {
368 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
369 ErrCode ret = RemoveGroup();
370 reply.WriteInt32(0);
371 reply.WriteInt32(ret);
372 return;
373 }
374
OnRemoveGroupClient(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)375 void WifiP2pStub::OnRemoveGroupClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
376 {
377 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
378 GcInfo info;
379 info.ip = data.ReadString();
380 info.mac = data.ReadString();
381 info.host = data.ReadString();
382 ErrCode ret = RemoveGroupClient(info);
383 reply.WriteInt32(0);
384 reply.WriteInt32(ret);
385 return;
386 }
387
OnDeleteGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)388 void WifiP2pStub::OnDeleteGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
389 {
390 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
391 WifiP2pGroupInfo config;
392 if (!ReadWifiP2pGroupData(data, config)) {
393 reply.WriteInt32(0);
394 reply.WriteInt32(WIFI_OPT_INVALID_PARAM);
395 return;
396 }
397 ErrCode ret = DeleteGroup(config);
398 reply.WriteInt32(0);
399 reply.WriteInt32(ret);
400 return;
401 }
402
OnP2pConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)403 void WifiP2pStub::OnP2pConnect(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
404 {
405 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
406 WifiP2pConfig config;
407 ReadWifiP2pConfigData(data, config);
408
409 ErrCode ret = P2pConnect(config);
410 reply.WriteInt32(0);
411 reply.WriteInt32(ret);
412 return;
413 }
414
OnP2pCancelConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)415 void WifiP2pStub::OnP2pCancelConnect(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
416 {
417 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
418 ErrCode ret = P2pCancelConnect();
419 reply.WriteInt32(0);
420 reply.WriteInt32(ret);
421 return;
422 }
423
OnQueryP2pLinkedInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)424 void WifiP2pStub::OnQueryP2pLinkedInfo(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
425 {
426 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
427 WifiP2pLinkedInfo config;
428 ErrCode ret = QueryP2pLinkedInfo(config);
429
430 reply.WriteInt32(0);
431 reply.WriteInt32(ret);
432 if (ret == WIFI_OPT_SUCCESS) {
433 reply.WriteInt32(static_cast<int>(config.GetConnectState()));
434 reply.WriteBool(config.IsGroupOwner());
435 reply.WriteString(config.GetGroupOwnerAddress());
436 std::vector<GcInfo> gcInfos = config.GetClientInfoList();
437 int size = static_cast<int>(gcInfos.size());
438 reply.WriteInt32(size);
439 for (int i = 0; i < size; i++) {
440 reply.WriteString(gcInfos[i].mac);
441 reply.WriteString(gcInfos[i].ip);
442 reply.WriteString(gcInfos[i].host);
443 }
444 }
445 return;
446 }
447
OnGetCurrentGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)448 void WifiP2pStub::OnGetCurrentGroup(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
449 {
450 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
451 WifiP2pGroupInfo config;
452 ErrCode ret = GetCurrentGroup(config);
453
454 reply.WriteInt32(0);
455 reply.WriteInt32(ret);
456 if (ret == WIFI_OPT_SUCCESS) {
457 WriteWifiP2pGroupData(reply, config, false);
458 }
459 return;
460 }
461
OnGetP2pEnableStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)462 void WifiP2pStub::OnGetP2pEnableStatus(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
463 {
464 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
465 int status = 0;
466 ErrCode ret = GetP2pEnableStatus(status);
467 reply.WriteInt32(0);
468 reply.WriteInt32(ret);
469 if (ret == WIFI_OPT_SUCCESS) {
470 reply.WriteInt32(status);
471 }
472 return;
473 }
474
OnGetP2pDiscoverStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)475 void WifiP2pStub::OnGetP2pDiscoverStatus(
476 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
477 {
478 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
479 int status = 0;
480 ErrCode ret = GetP2pDiscoverStatus(status);
481 reply.WriteInt32(0);
482 reply.WriteInt32(ret);
483 if (ret == WIFI_OPT_SUCCESS) {
484 reply.WriteInt32(status);
485 }
486 return;
487 }
488
OnGetP2pConnectedStatus(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)489 void WifiP2pStub::OnGetP2pConnectedStatus(
490 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
491 {
492 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
493 int status = 0;
494 ErrCode ret = GetP2pConnectedStatus(status);
495 reply.WriteInt32(0);
496 reply.WriteInt32(ret);
497 if (ret == WIFI_OPT_SUCCESS) {
498 reply.WriteInt32(status);
499 }
500 return;
501 }
502
OnQueryP2pDevices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)503 void WifiP2pStub::OnQueryP2pDevices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
504 {
505 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
506 std::vector<WifiP2pDevice> devices;
507 ErrCode ret = QueryP2pDevices(devices);
508
509 reply.WriteInt32(0);
510 reply.WriteInt32(ret);
511 if (ret == WIFI_OPT_SUCCESS) {
512 int size = static_cast<int>(devices.size());
513 reply.WriteInt32(size);
514 for (int i = 0; i < size; ++i) {
515 WriteWifiP2pDeviceData(reply, devices[i]);
516 }
517 }
518 return;
519 }
520
OnQueryP2pLocalDevice(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)521 void WifiP2pStub::OnQueryP2pLocalDevice(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
522 {
523 WIFI_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
524 WifiP2pDevice device;
525 ErrCode ret = QueryP2pLocalDevice(device);
526 reply.WriteInt32(0);
527 reply.WriteInt32(ret);
528 if (ret == WIFI_OPT_SUCCESS) {
529 WriteWifiP2pDeviceData(reply, device);
530 }
531 return;
532 }
533
OnQueryP2pGroups(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)534 void WifiP2pStub::OnQueryP2pGroups(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
535 {
536 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
537 std::vector<WifiP2pGroupInfo> groups;
538 ErrCode ret = QueryP2pGroups(groups);
539
540 reply.WriteInt32(0);
541 reply.WriteInt32(ret);
542
543 if (ret == WIFI_OPT_SUCCESS) {
544 int size = static_cast<int>(groups.size());
545 reply.WriteInt32(size);
546 for (int i = 0; i < size; ++i) {
547 WriteWifiP2pGroupData(reply, groups[i], true);
548 }
549 }
550 return;
551 }
552
OnQueryP2pServices(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)553 void WifiP2pStub::OnQueryP2pServices(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
554 {
555 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
556 std::vector<WifiP2pServiceInfo> services;
557 ErrCode ret = QueryP2pServices(services);
558 reply.WriteInt32(0);
559 reply.WriteInt32(ret);
560
561 if (ret == WIFI_OPT_SUCCESS) {
562 int size = static_cast<int>(services.size());
563 reply.WriteInt32(size);
564 for (int i = 0; i < size; ++i) {
565 WriteWifiP2pServiceInfo(reply, services[i]);
566 }
567 }
568 return;
569 }
570
OnDiscoverPeers(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)571 void WifiP2pStub::OnDiscoverPeers(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
572 {
573 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
574 int channelid = data.ReadInt32();
575 ErrCode ret = DiscoverPeers(channelid);
576 reply.WriteInt32(0);
577 reply.WriteInt32(ret);
578 return;
579 }
580
OnDisableRandomMac(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)581 void WifiP2pStub::OnDisableRandomMac(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
582 {
583 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
584 int setmode = data.ReadInt32();
585 ErrCode ret = DisableRandomMac(setmode);
586 reply.WriteInt32(0);
587 reply.WriteInt32(ret);
588 return;
589 }
590
OnCheckCanUseP2p(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)591 void WifiP2pStub::OnCheckCanUseP2p(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
592 {
593 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
594 ErrCode ret = CheckCanUseP2p();
595 reply.WriteInt32(0);
596 reply.WriteInt32(ret);
597 return;
598 }
599
ReadWifiP2pServiceInfo(MessageParcel & data,WifiP2pServiceInfo & info)600 bool WifiP2pStub::ReadWifiP2pServiceInfo(MessageParcel &data, WifiP2pServiceInfo &info)
601 {
602 const char *readStr = nullptr;
603 constexpr int MAX_QUERY_SIZE = 256;
604 readStr = data.ReadCString();
605 info.SetServiceName((readStr != nullptr) ? readStr : "");
606 readStr = data.ReadCString();
607 info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
608 info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(data.ReadInt32()));
609 std::vector<std::string> queryList;
610 int size = data.ReadInt32();
611 if (size > MAX_QUERY_SIZE) {
612 return false;
613 }
614 for (int i = 0; i < size; i++) {
615 readStr = data.ReadCString();
616 std::string str = (readStr != nullptr) ? readStr : "";
617 queryList.push_back(str);
618 }
619 info.SetQueryList(queryList);
620 return true;
621 }
622
ReadWifiP2pServiceRequest(MessageParcel & data,WifiP2pDevice & device,WifiP2pServiceRequest & request)623 bool WifiP2pStub::ReadWifiP2pServiceRequest(MessageParcel &data, WifiP2pDevice &device, WifiP2pServiceRequest &request)
624 {
625 constexpr int MAX_QUERY_SIZE = 256;
626 ReadWifiP2pDeviceData(data, device);
627 request.SetProtocolType(static_cast<P2pServicerProtocolType>(data.ReadInt32()));
628 request.SetTransactionId(data.ReadInt32());
629 int size = data.ReadInt32();
630 if (size > MAX_QUERY_SIZE) {
631 return false;
632 }
633 std::vector<unsigned char> query;
634 for (int i = 0; i < size; i++) {
635 unsigned char chr = data.ReadInt8();
636 query.push_back(chr);
637 }
638 request.SetQuery(query);
639 return true;
640 }
641
WriteWifiP2pServiceInfo(MessageParcel & reply,const WifiP2pServiceInfo & info)642 void WifiP2pStub::WriteWifiP2pServiceInfo(MessageParcel &reply, const WifiP2pServiceInfo &info)
643 {
644 reply.WriteCString(info.GetServiceName().c_str());
645 reply.WriteCString(info.GetDeviceAddress().c_str());
646 reply.WriteInt32(static_cast<int>(info.GetServicerProtocolType()));
647 reply.WriteInt32(info.GetQueryList().size());
648 std::vector<std::string> queryList = info.GetQueryList();
649 for (auto it = queryList.begin(); it != queryList.end(); it++) {
650 reply.WriteCString((*it).c_str());
651 }
652 return;
653 }
654
ReadWifiP2pDeviceData(MessageParcel & data,WifiP2pDevice & device)655 void WifiP2pStub::ReadWifiP2pDeviceData(MessageParcel &data, WifiP2pDevice &device)
656 {
657 device.SetDeviceName(data.ReadString());
658 device.SetDeviceAddress(data.ReadString());
659 device.SetDeviceAddressType(data.ReadInt32());
660 device.SetPrimaryDeviceType(data.ReadString());
661 device.SetSecondaryDeviceType(data.ReadString());
662 device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(data.ReadInt32()));
663 WifiP2pWfdInfo wfdInfo;
664 wfdInfo.SetWfdEnabled(data.ReadBool());
665 wfdInfo.SetDeviceInfo(data.ReadInt32());
666 wfdInfo.SetCtrlPort(data.ReadInt32());
667 wfdInfo.SetMaxThroughput(data.ReadInt32());
668 device.SetWfdInfo(wfdInfo);
669 device.SetWpsConfigMethod(data.ReadInt32());
670 device.SetDeviceCapabilitys(data.ReadInt32());
671 device.SetGroupCapabilitys(data.ReadInt32());
672 }
673
WriteWifiP2pDeviceData(MessageParcel & reply,const WifiP2pDevice & device)674 void WifiP2pStub::WriteWifiP2pDeviceData(MessageParcel &reply, const WifiP2pDevice &device)
675 {
676 reply.WriteString(device.GetDeviceName());
677 reply.WriteString(device.GetDeviceAddress());
678 reply.WriteString(device.GetRandomDeviceAddress());
679 reply.WriteInt32(device.GetDeviceAddressType());
680 reply.WriteString(device.GetPrimaryDeviceType());
681 reply.WriteString(device.GetSecondaryDeviceType());
682 reply.WriteInt32(static_cast<int>(device.GetP2pDeviceStatus()));
683 reply.WriteBool(device.GetWfdInfo().GetWfdEnabled());
684 reply.WriteInt32(device.GetWfdInfo().GetDeviceInfo());
685 reply.WriteInt32(device.GetWfdInfo().GetCtrlPort());
686 reply.WriteInt32(device.GetWfdInfo().GetMaxThroughput());
687 reply.WriteInt32(device.GetWpsConfigMethod());
688 reply.WriteInt32(device.GetDeviceCapabilitys());
689 reply.WriteInt32(device.GetGroupCapabilitys());
690 reply.WriteString(device.GetGroupAddress());
691 }
692
ReadWifiP2pGroupData(MessageParcel & data,WifiP2pGroupInfo & info)693 bool WifiP2pStub::ReadWifiP2pGroupData(MessageParcel &data, WifiP2pGroupInfo &info)
694 {
695 constexpr int MAX_DEV_SIZE = 256;
696 info.SetIsGroupOwner(data.ReadBool());
697 WifiP2pDevice device;
698 ReadWifiP2pDeviceData(data, device);
699 info.SetOwner(device);
700 info.SetPassphrase(data.ReadString());
701 info.SetInterface(data.ReadString());
702 info.SetGroupName(data.ReadString());
703 info.SetFrequency(data.ReadInt32());
704 info.SetIsPersistent(data.ReadBool());
705 info.SetP2pGroupStatus(static_cast<P2pGroupStatus>(data.ReadInt32()));
706 info.SetNetworkId(data.ReadInt32());
707 info.SetGoIpAddress(data.ReadString());
708 int size = data.ReadInt32();
709 if (size > MAX_DEV_SIZE) {
710 return false;
711 }
712 for (auto it = 0; it < size; ++it) {
713 WifiP2pDevice cliDev;
714 ReadWifiP2pDeviceData(data, cliDev);
715 info.AddClientDevice(cliDev);
716 }
717 return true;
718 }
719
WriteWifiP2pGroupData(MessageParcel & reply,const WifiP2pGroupInfo & info,bool isPersistent)720 void WifiP2pStub::WriteWifiP2pGroupData(MessageParcel &reply, const WifiP2pGroupInfo &info, bool isPersistent)
721 {
722 reply.WriteBool(info.IsGroupOwner());
723 WriteWifiP2pDeviceData(reply, info.GetOwner());
724 reply.WriteString(info.GetPassphrase());
725 reply.WriteString(info.GetInterface());
726 reply.WriteString(info.GetGroupName());
727 reply.WriteInt32(info.GetFrequency());
728 reply.WriteBool(info.IsPersistent());
729 reply.WriteInt32(static_cast<int>(info.GetP2pGroupStatus()));
730 reply.WriteInt32(info.GetNetworkId());
731 reply.WriteString(info.GetGoIpAddress());
732 reply.WriteString(info.GetGcIpAddress());
733 std::vector<WifiP2pDevice> deviceVec;
734 if (isPersistent) {
735 deviceVec = info.GetPersistentDevices();
736 } else {
737 deviceVec = info.GetClientDevices();
738 }
739 reply.WriteInt32(deviceVec.size());
740 for (auto it = deviceVec.begin(); it != deviceVec.end(); ++it) {
741 WriteWifiP2pDeviceData(reply, *it);
742 }
743 }
744
ReadWifiP2pConfigData(MessageParcel & data,WifiP2pConfig & config)745 void WifiP2pStub::ReadWifiP2pConfigData(MessageParcel &data, WifiP2pConfig &config)
746 {
747 config.SetDeviceAddress(data.ReadString());
748 config.SetDeviceAddressType(data.ReadInt32());
749 config.SetPassphrase(data.ReadString());
750 config.SetGroupName(data.ReadString());
751 config.SetGoBand(static_cast<GroupOwnerBand>(data.ReadInt32()));
752 config.SetNetId(data.ReadInt32());
753 config.SetGroupOwnerIntent(data.ReadInt32());
754 config.SetFreq(data.ReadInt32());
755 }
756
OnRegisterCallBack(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)757 void WifiP2pStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
758 {
759 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
760 ErrCode ret = WIFI_OPT_FAILED;
761 do {
762 sptr<IRemoteObject> remote = data.ReadRemoteObject();
763 if (remote == nullptr) {
764 WIFI_LOGE("Failed to readRemoteObject!");
765 break;
766 }
767
768 sptr<IWifiP2pCallback> callback_ = iface_cast<IWifiP2pCallback>(remote);
769 if (callback_ == nullptr) {
770 callback_ = sptr<WifiP2pCallbackProxy>::MakeSptr(remote);
771 WIFI_LOGI("create new `WifiP2pCallbackProxy`!");
772 }
773
774 int pid = data.ReadInt32();
775 int tokenId = data.ReadInt32();
776 int eventNum = data.ReadInt32();
777 std::vector<std::string> event;
778 if (eventNum > 0 && eventNum <= MAX_READ_EVENT_SIZE) {
779 for (int i = 0; i < eventNum; ++i) {
780 event.emplace_back(data.ReadString());
781 }
782 }
783 WIFI_LOGD("%{public}s, get pid: %{public}d, tokenId: %{private}d", __func__, pid, tokenId);
784
785 if (mSingleCallback) {
786 ret = RegisterCallBack(callback_, event);
787 } else {
788 std::unique_lock<std::mutex> lock(deathRecipientMutex);
789 if (deathRecipient_ == nullptr) {
790 deathRecipient_ = sptr<WifiP2pDeathRecipient>::MakeSptr();
791 }
792 // Add death recipient to remote object if this is the first time to register callback.
793 if ((remote->IsProxyObject()) &&
794 !WifiInternalEventDispatcher::GetInstance().HasP2pRemote(remote)) {
795 remote->AddDeathRecipient(deathRecipient_);
796 }
797
798 if (callback_ != nullptr) {
799 for (const auto &eventName : event) {
800 ret = WifiInternalEventDispatcher::GetInstance().AddP2pCallback(remote, callback_, pid,
801 eventName, tokenId);
802 }
803 }
804 }
805 MonitorCfgChange();
806 } while (0);
807
808 reply.WriteInt32(0);
809 reply.WriteInt32(ret);
810 return;
811 }
812
OnGetSupportedFeatures(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)813 void WifiP2pStub::OnGetSupportedFeatures(
814 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
815 {
816 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
817 long features = 0;
818 int ret = GetSupportedFeatures(features);
819 reply.WriteInt32(0);
820 reply.WriteInt32(ret);
821
822 if (ret == WIFI_OPT_SUCCESS) {
823 reply.WriteInt64(features);
824 }
825
826 return;
827 }
828
OnSetP2pDeviceName(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)829 void WifiP2pStub::OnSetP2pDeviceName(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
830 {
831 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
832 ErrCode ret = WIFI_OPT_FAILED;
833 std::string deviceName = data.ReadString();
834 ret = SetP2pDeviceName(deviceName);
835 reply.WriteInt32(0);
836 reply.WriteInt32(ret);
837
838 return;
839 }
840
OnSetP2pWfdInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)841 void WifiP2pStub::OnSetP2pWfdInfo(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
842 {
843 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
844 WifiP2pWfdInfo wfdInfo;
845 wfdInfo.SetWfdEnabled(data.ReadBool());
846 wfdInfo.SetDeviceInfo(data.ReadInt32());
847 wfdInfo.SetCtrlPort(data.ReadInt32());
848 wfdInfo.SetMaxThroughput(data.ReadInt32());
849
850 int ret = SetP2pWfdInfo(wfdInfo);
851 reply.WriteInt32(0);
852 reply.WriteInt32(ret);
853 return;
854 }
855
OnHid2dRequestGcIp(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)856 void WifiP2pStub::OnHid2dRequestGcIp(uint32_t code, MessageParcel &data,
857 MessageParcel &reply, MessageOption &option)
858 {
859 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
860
861 ErrCode ret = WIFI_OPT_FAILED;
862 const char *readStr = data.ReadCString();
863 std::string ipAddr;
864 if (readStr == nullptr) {
865 ret = WIFI_OPT_INVALID_PARAM;
866 } else {
867 std::string gcMac = readStr;
868 ret = Hid2dRequestGcIp(gcMac, ipAddr);
869 }
870 reply.WriteInt32(0);
871 reply.WriteInt32(ret);
872 if (ret == WIFI_OPT_SUCCESS) {
873 reply.WriteCString(ipAddr.c_str());
874 }
875 }
876
OnHid2dSharedlinkIncrease(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)877 void WifiP2pStub::OnHid2dSharedlinkIncrease(uint32_t code, MessageParcel &data,
878 MessageParcel &reply, MessageOption &option)
879 {
880 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
881
882 int ret = Hid2dSharedlinkIncrease();
883 reply.WriteInt32(0);
884 reply.WriteInt32(ret);
885 }
886
OnHid2dSharedlinkDecrease(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)887 void WifiP2pStub::OnHid2dSharedlinkDecrease(uint32_t code, MessageParcel &data,
888 MessageParcel &reply, MessageOption &option)
889 {
890 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
891 int ret = Hid2dSharedlinkDecrease();
892 reply.WriteInt32(0);
893 reply.WriteInt32(ret);
894 }
895
OnHid2dCreateGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)896 void WifiP2pStub::OnHid2dCreateGroup(uint32_t code, MessageParcel &data,
897 MessageParcel &reply, MessageOption &option)
898 {
899 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
900
901 int frequency = data.ReadInt32();
902 int type = data.ReadInt32();
903 int ret = Hid2dCreateGroup(frequency, FreqType(type));
904 reply.WriteInt32(0);
905 reply.WriteInt32(ret);
906 }
907
OnHid2dRemoveGcGroup(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)908 void WifiP2pStub::OnHid2dRemoveGcGroup(uint32_t code, MessageParcel &data,
909 MessageParcel &reply, MessageOption &option)
910 {
911 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
912
913 ErrCode ret = WIFI_OPT_FAILED;
914 const char *readStr = data.ReadCString();
915 if (readStr == nullptr) {
916 ret = WIFI_OPT_INVALID_PARAM;
917 } else {
918 std::string gcIfName = readStr;
919 ret = Hid2dRemoveGcGroup(gcIfName);
920 }
921 reply.WriteInt32(0);
922 reply.WriteInt32(ret);
923 }
924
OnHid2dConnect(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)925 void WifiP2pStub::OnHid2dConnect(uint32_t code, MessageParcel &data,
926 MessageParcel &reply, MessageOption &option)
927 {
928 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
929
930 ErrCode ret = WIFI_OPT_FAILED;
931 Hid2dConnectConfig config;
932 const char *ssidRead = data.ReadCString();
933 const char *bssidRead = data.ReadCString();
934 const char *preSharedKeyRead = data.ReadCString();
935 if (ssidRead == nullptr || bssidRead == nullptr || preSharedKeyRead == nullptr) {
936 ret = WIFI_OPT_INVALID_PARAM;
937 } else {
938 config.SetSsid(ssidRead);
939 config.SetBssid(bssidRead);
940 config.SetPreSharedKey(preSharedKeyRead);
941 config.SetFrequency(data.ReadInt32());
942 config.SetDhcpMode(DhcpMode(data.ReadInt32()));
943
944 ret = Hid2dConnect(config);
945 }
946
947 reply.WriteInt32(0);
948 reply.WriteInt32(ret);
949 }
950
OnHid2dConfigIPAddr(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)951 void WifiP2pStub::OnHid2dConfigIPAddr(uint32_t code, MessageParcel &data,
952 MessageParcel &reply, MessageOption &option)
953 {
954 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
955
956 ErrCode ret = WIFI_OPT_FAILED;
957 IpAddrInfo ipInfo;
958 const char *ifNameRead = data.ReadCString();
959 const char *ipRead = data.ReadCString();
960 const char *gatewayRead = data.ReadCString();
961 const char *netmaskRead = data.ReadCString();
962 if (ifNameRead == nullptr || ipRead == nullptr || gatewayRead == nullptr || netmaskRead == nullptr) {
963 ret = WIFI_OPT_INVALID_PARAM;
964 } else {
965 std::string ifName = ifNameRead;
966 ipInfo.ip = ipRead;
967 ipInfo.gateway = gatewayRead;
968 ipInfo.netmask = netmaskRead;
969 ret = Hid2dConfigIPAddr(ifName, ipInfo);
970 }
971 reply.WriteInt32(0);
972 reply.WriteInt32(ret);
973 }
974
OnHid2dReleaseIPAddr(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)975 void WifiP2pStub::OnHid2dReleaseIPAddr(uint32_t code, MessageParcel &data,
976 MessageParcel &reply, MessageOption &option)
977 {
978 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
979
980 ErrCode ret = WIFI_OPT_FAILED;
981 const char *ifNameRead = data.ReadCString();
982 if (ifNameRead == nullptr) {
983 ret = WIFI_OPT_INVALID_PARAM;
984 } else {
985 std::string ifName = ifNameRead;
986 ret = Hid2dReleaseIPAddr(ifName);
987 }
988 reply.WriteInt32(0);
989 reply.WriteInt32(ret);
990 }
991
OnHid2dGetRecommendChannel(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)992 void WifiP2pStub::OnHid2dGetRecommendChannel(
993 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
994 {
995 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
996 const char *readStr = nullptr;
997 RecommendChannelRequest request;
998 RecommendChannelResponse response;
999 readStr = data.ReadCString();
1000 request.remoteIfName = (readStr != nullptr) ? readStr : "";
1001 request.remoteIfMode = data.ReadInt32();
1002 readStr = data.ReadCString();
1003 request.localIfName = (readStr != nullptr) ? readStr : "";
1004 request.localIfMode = data.ReadInt32();
1005 request.prefBand = data.ReadInt32();
1006 request.prefBandwidth = PreferBandwidth(data.ReadInt32());
1007 ErrCode ret = Hid2dGetRecommendChannel(request, response);
1008 reply.WriteInt32(0);
1009 reply.WriteInt32(ret);
1010 if (ret == WIFI_OPT_SUCCESS) {
1011 reply.WriteInt32(static_cast<int>(response.status));
1012 reply.WriteInt32(response.index);
1013 reply.WriteInt32(response.centerFreq);
1014 reply.WriteInt32(response.centerFreq1);
1015 reply.WriteInt32(response.centerFreq2);
1016 reply.WriteInt32(response.bandwidth);
1017 }
1018 }
1019
OnHid2dGetChannelListFor5G(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1020 void WifiP2pStub::OnHid2dGetChannelListFor5G(
1021 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1022 {
1023 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1024
1025 std::vector<int> vecChannelList;
1026 ErrCode ret = Hid2dGetChannelListFor5G(vecChannelList);
1027 reply.WriteInt32(0);
1028 reply.WriteInt32(ret);
1029 if (ret == WIFI_OPT_SUCCESS) {
1030 reply.WriteInt32((int)vecChannelList.size());
1031 for (auto& channel : vecChannelList) {
1032 reply.WriteInt32(channel);
1033 }
1034 }
1035 }
1036
OnHid2dGetSelfWifiCfgInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1037 void WifiP2pStub::OnHid2dGetSelfWifiCfgInfo(
1038 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1039 {
1040 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1041
1042 int cfgType = data.ReadInt32();
1043 int len = 0;
1044 char cfgData[CFG_DATA_MAX_BYTES];
1045 if (memset_s(cfgData, CFG_DATA_MAX_BYTES, 0, CFG_DATA_MAX_BYTES) != EOK) {
1046 WIFI_LOGE("`%{public}s` memset_s failed!", __func__);
1047 }
1048 ErrCode ret = Hid2dGetSelfWifiCfgInfo(SelfCfgType(cfgType), cfgData, &len);
1049 reply.WriteInt32(0);
1050 reply.WriteInt32(ret);
1051 reply.WriteInt32(len);
1052 if (ret == WIFI_OPT_SUCCESS && len > 0) {
1053 reply.WriteBuffer(cfgData, len);
1054 }
1055 }
1056
OnHid2dSetPeerWifiCfgInfo(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1057 void WifiP2pStub::OnHid2dSetPeerWifiCfgInfo(
1058 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1059 {
1060 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1061
1062 char cfgData[CFG_DATA_MAX_BYTES];
1063 if (memset_s(cfgData, CFG_DATA_MAX_BYTES, 0, CFG_DATA_MAX_BYTES) != EOK) {
1064 WIFI_LOGE("`%{public}s` memset_s failed!", __func__);
1065 }
1066 int cfgType = data.ReadInt32();
1067 int len = data.ReadInt32();
1068 const char *dataBuffer = (const char *)data.ReadBuffer(len);
1069 if (memcpy_s(cfgData, CFG_DATA_MAX_BYTES, dataBuffer, len) != EOK) {
1070 WIFI_LOGE("`%{public}s` memcpy_s failed!", __func__);
1071 reply.WriteInt32(0);
1072 reply.WriteInt32(WIFI_OPT_FAILED);
1073 return;
1074 }
1075 ErrCode ret = Hid2dSetPeerWifiCfgInfo(PeerCfgType(cfgType), cfgData, len);
1076 reply.WriteInt32(0);
1077 reply.WriteInt32(ret);
1078 }
1079
OnHid2dSetUpperScene(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1080 void WifiP2pStub::OnHid2dSetUpperScene(
1081 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1082 {
1083 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1084
1085 ErrCode ret = WIFI_OPT_FAILED;
1086 Hid2dUpperScene scene;
1087 const char *ifNameRead = data.ReadCString();
1088 const char *macRead = data.ReadCString();
1089 if (ifNameRead == nullptr || macRead == nullptr) {
1090 ret = WIFI_OPT_INVALID_PARAM;
1091 } else {
1092 std::string ifName = ifNameRead;
1093 scene.mac = macRead;
1094 scene.scene = data.ReadUint32();
1095 scene.fps = data.ReadInt32();
1096 scene.bw = data.ReadUint32();
1097 struct timespec times = {0, 0};
1098 clock_gettime(CLOCK_MONOTONIC, ×);
1099 if (scene.scene == 0) {
1100 scene.setTime = 0;
1101 } else {
1102 scene.setTime =
1103 static_cast<int64_t>(times.tv_sec) * SECOND_TO_MILLI_SECOND + times.tv_nsec / SECOND_TO_MICRO_SECOND;
1104 }
1105 ret = Hid2dSetUpperScene(ifName, scene);
1106 }
1107 reply.WriteInt32(0);
1108 reply.WriteInt32(ret);
1109 }
1110
IsSingleCallback() const1111 bool WifiP2pStub::IsSingleCallback() const
1112 {
1113 return mSingleCallback;
1114 }
1115
SetSingleCallback(const bool isSingleCallback)1116 void WifiP2pStub::SetSingleCallback(const bool isSingleCallback)
1117 {
1118 mSingleCallback = true;
1119 }
1120
OnHid2dIsWideBandwidthSupported(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1121 void WifiP2pStub::OnHid2dIsWideBandwidthSupported(
1122 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1123 {
1124 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1125 bool isSupport = false;
1126 ErrCode ret = Hid2dIsWideBandwidthSupported(isSupport);
1127 reply.WriteInt32(0);
1128 reply.WriteInt32(ret);
1129 if (ret == WIFI_OPT_SUCCESS) {
1130 reply.WriteInt32(isSupport ? 1 : 0);
1131 }
1132 }
1133
OnSetMiracastSinkConfig(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1134 void WifiP2pStub::OnSetMiracastSinkConfig(
1135 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1136 {
1137 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1138 std::string config = data.ReadString();
1139 ErrCode ret = SetMiracastSinkConfig(config);
1140 reply.WriteInt32(0);
1141 reply.WriteInt32(ret);
1142 }
1143
OnGetSupportChanForBand(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1144 void WifiP2pStub::OnGetSupportChanForBand(
1145 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1146 {
1147 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
1148 std::vector<int> channels;
1149 int band = data.ReadInt32();
1150 ErrCode ret = GetSupportedChanForBand(channels, band);
1151 reply.WriteInt32(0);
1152 reply.WriteInt32(ret);
1153 if (ret == WIFI_OPT_SUCCESS) {
1154 reply.WriteInt32(static_cast<int>(channels.size()));
1155 for (auto& channel : channels) {
1156 reply.WriteInt32(channel);
1157 }
1158 }
1159 }
1160
OnSetP2pHighPerf(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)1161 void WifiP2pStub::OnSetP2pHighPerf(
1162 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
1163 {
1164 bool isEnable = data.ReadBool();
1165 WIFI_LOGD("run %{public}s code %{public}u, isEnable = %{public}d", __func__, code, isEnable);
1166
1167 ErrCode ret = SetP2pHighPerf(isEnable);
1168 reply.WriteInt32(0);
1169 reply.WriteInt32(ret);
1170 }
1171 } // namespace Wifi
1172 } // namespace OHOS
1173