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_proxy.h"
16 #include "define.h"
17 #include "wifi_manager_service_ipc_interface_code.h"
18 #include "wifi_logger.h"
19 #include "wifi_p2p_callback_stub.h"
20 #include "wifi_common_util.h"
21 
22 namespace OHOS {
23 namespace Wifi {
24 DEFINE_WIFILOG_P2P_LABEL("WifiP2pProxy");
25 
26 static sptr<WifiP2pCallbackStub> g_wifiP2pCallbackStub =
27     sptr<WifiP2pCallbackStub>(new (std::nothrow) WifiP2pCallbackStub());
28 
WifiP2pProxy(const sptr<IRemoteObject> & impl)29 WifiP2pProxy::WifiP2pProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IWifiP2p>(impl),
30     mRemoteDied(false), remote_(nullptr), deathRecipient_(nullptr)
31 {
32     std::lock_guard<std::mutex> lock(mutex_);
33     if (impl) {
34         if (!impl->IsProxyObject()) {
35             WIFI_LOGW("not proxy object!");
36             return;
37         }
38         deathRecipient_ = new (std::nothrow) WifiDeathRecipient(*this);
39         if (deathRecipient_ == nullptr) {
40             WIFI_LOGW("deathRecipient_ is nullptr!");
41         }
42         if (!impl->AddDeathRecipient(deathRecipient_)) {
43             WIFI_LOGW("AddDeathRecipient failed!");
44             return;
45         }
46         remote_ = impl;
47         WIFI_LOGI("AddDeathRecipient success! deathRecipient_");
48     }
49 }
50 
~WifiP2pProxy()51 WifiP2pProxy::~WifiP2pProxy()
52 {
53     WIFI_LOGI("enter ~WifiP2pProxy!");
54     RemoveDeathRecipient();
55 }
56 
RemoveDeathRecipient(void)57 void WifiP2pProxy::RemoveDeathRecipient(void)
58 {
59     WIFI_LOGI("enter RemoveDeathRecipient, deathRecipient_!");
60     std::lock_guard<std::mutex> lock(mutex_);
61     if (remote_ == nullptr) {
62         WIFI_LOGI("remote_ is nullptr!");
63         return;
64     }
65     if (deathRecipient_ == nullptr) {
66         WIFI_LOGI("deathRecipient_ is nullptr!");
67         return;
68     }
69     remote_->RemoveDeathRecipient(deathRecipient_);
70     remote_ = nullptr;
71 }
72 
EnableP2p(void)73 ErrCode WifiP2pProxy::EnableP2p(void)
74 {
75     if (mRemoteDied) {
76         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
77         return WIFI_OPT_FAILED;
78     }
79     MessageOption option;
80     MessageParcel data;
81     MessageParcel reply;
82     if (!data.WriteInterfaceToken(GetDescriptor())) {
83         WIFI_LOGE("Write interface token error: %{public}s", __func__);
84         return WIFI_OPT_FAILED;
85     }
86     data.WriteInt32(0);
87     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_ENABLE), data, reply,
88         option);
89     if (error != ERR_NONE) {
90         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
91             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_ENABLE), error);
92         return WIFI_OPT_FAILED;
93     }
94     int exception = reply.ReadInt32();
95     if (exception) {
96         return WIFI_OPT_FAILED;
97     }
98     return ErrCode(reply.ReadInt32());
99 }
100 
DisableP2p(void)101 ErrCode WifiP2pProxy::DisableP2p(void)
102 {
103     if (mRemoteDied) {
104         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
105         return WIFI_OPT_FAILED;
106     }
107     MessageOption option;
108     MessageParcel data;
109     MessageParcel reply;
110     if (!data.WriteInterfaceToken(GetDescriptor())) {
111         WIFI_LOGE("Write interface token error: %{public}s", __func__);
112         return WIFI_OPT_FAILED;
113     }
114     data.WriteInt32(0);
115     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE), data, reply,
116         option);
117     if (error != ERR_NONE) {
118         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
119             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE), error);
120         return WIFI_OPT_FAILED;
121     }
122 
123     int exception = reply.ReadInt32();
124     if (exception) {
125         return WIFI_OPT_FAILED;
126     }
127     return ErrCode(reply.ReadInt32());
128 }
129 
DiscoverDevices(void)130 ErrCode WifiP2pProxy::DiscoverDevices(void)
131 {
132     if (mRemoteDied) {
133         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
134         return WIFI_OPT_FAILED;
135     }
136     MessageOption option;
137     MessageParcel data;
138     MessageParcel reply;
139     if (!data.WriteInterfaceToken(GetDescriptor())) {
140         WIFI_LOGE("Write interface token error: %{public}s", __func__);
141         return WIFI_OPT_FAILED;
142     }
143     data.WriteInt32(0);
144     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_DEVICES), data,
145         reply, option);
146     if (error != ERR_NONE) {
147         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
148             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_DEVICES), error);
149         return WIFI_OPT_FAILED;
150     }
151 
152     int exception = reply.ReadInt32();
153     if (exception) {
154         return WIFI_OPT_FAILED;
155     }
156     return ErrCode(reply.ReadInt32());
157 }
158 
StopDiscoverDevices(void)159 ErrCode WifiP2pProxy::StopDiscoverDevices(void)
160 {
161     if (mRemoteDied) {
162         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
163         return WIFI_OPT_FAILED;
164     }
165     MessageOption option;
166     MessageParcel data;
167     MessageParcel reply;
168     if (!data.WriteInterfaceToken(GetDescriptor())) {
169         WIFI_LOGE("Write interface token error: %{public}s", __func__);
170         return WIFI_OPT_FAILED;
171     }
172     data.WriteInt32(0);
173     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES),
174         data, reply, option);
175     if (error != ERR_NONE) {
176         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
177             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES), error);
178         return WIFI_OPT_FAILED;
179     }
180 
181     int exception = reply.ReadInt32();
182     if (exception) {
183         return WIFI_OPT_FAILED;
184     }
185     return ErrCode(reply.ReadInt32());
186 }
187 
DiscoverServices(void)188 ErrCode WifiP2pProxy::DiscoverServices(void)
189 {
190     if (mRemoteDied) {
191         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
192         return WIFI_OPT_FAILED;
193     }
194     MessageOption option;
195     MessageParcel data;
196     MessageParcel reply;
197     if (!data.WriteInterfaceToken(GetDescriptor())) {
198         WIFI_LOGE("Write interface token error: %{public}s", __func__);
199         return WIFI_OPT_FAILED;
200     }
201     data.WriteInt32(0);
202     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_SERVICES), data,
203         reply, option);
204     if (error != ERR_NONE) {
205         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
206             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_SERVICES), error);
207         return WIFI_OPT_FAILED;
208     }
209 
210     int exception = reply.ReadInt32();
211     if (exception) {
212         return WIFI_OPT_FAILED;
213     }
214     return ErrCode(reply.ReadInt32());
215 }
216 
StopDiscoverServices(void)217 ErrCode WifiP2pProxy::StopDiscoverServices(void)
218 {
219     if (mRemoteDied) {
220         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
221         return WIFI_OPT_FAILED;
222     }
223     MessageOption option;
224     MessageParcel data;
225     MessageParcel reply;
226     if (!data.WriteInterfaceToken(GetDescriptor())) {
227         WIFI_LOGE("Write interface token error: %{public}s", __func__);
228         return WIFI_OPT_FAILED;
229     }
230     data.WriteInt32(0);
231     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES),
232         data, reply, option);
233     if (error != ERR_NONE) {
234         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
235             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES), error);
236         return WIFI_OPT_FAILED;
237     }
238 
239     int exception = reply.ReadInt32();
240     if (exception) {
241         return WIFI_OPT_FAILED;
242     }
243     return ErrCode(reply.ReadInt32());
244 }
245 
RequestService(const WifiP2pDevice & device,const WifiP2pServiceRequest & request)246 ErrCode WifiP2pProxy::RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request)
247 {
248     if (mRemoteDied) {
249         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
250         return WIFI_OPT_FAILED;
251     }
252     MessageOption option;
253     MessageParcel data;
254     MessageParcel reply;
255     if (!data.WriteInterfaceToken(GetDescriptor())) {
256         WIFI_LOGE("Write interface token error: %{public}s", __func__);
257         return WIFI_OPT_FAILED;
258     }
259     data.WriteInt32(0);
260     WriteWifiP2pServiceRequest(data, device, request);
261     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REQUEST_SERVICES), data,
262         reply, option);
263     if (error != ERR_NONE) {
264         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
265             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REQUEST_SERVICES), error);
266         return WIFI_OPT_FAILED;
267     }
268 
269     int exception = reply.ReadInt32();
270     if (exception) {
271         return WIFI_OPT_FAILED;
272     }
273     return ErrCode(reply.ReadInt32());
274 }
275 
PutLocalP2pService(const WifiP2pServiceInfo & srvInfo)276 ErrCode WifiP2pProxy::PutLocalP2pService(const WifiP2pServiceInfo &srvInfo)
277 {
278     if (mRemoteDied) {
279         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
280         return WIFI_OPT_FAILED;
281     }
282     MessageOption option;
283     MessageParcel data;
284     MessageParcel reply;
285     if (!data.WriteInterfaceToken(GetDescriptor())) {
286         WIFI_LOGE("Write interface token error: %{public}s", __func__);
287         return WIFI_OPT_FAILED;
288     }
289     data.WriteInt32(0);
290     WriteWifiP2pServiceInfo(data, srvInfo);
291     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES),
292         data, reply, option);
293     if (error != ERR_NONE) {
294         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
295             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES), error);
296         return WIFI_OPT_FAILED;
297     }
298 
299     int exception = reply.ReadInt32();
300     if (exception) {
301         return WIFI_OPT_FAILED;
302     }
303     return ErrCode(reply.ReadInt32());
304 }
305 
DeleteLocalP2pService(const WifiP2pServiceInfo & srvInfo)306 ErrCode WifiP2pProxy::DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo)
307 {
308     if (mRemoteDied) {
309         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
310         return WIFI_OPT_FAILED;
311     }
312     MessageOption option;
313     MessageParcel data;
314     MessageParcel reply;
315     if (!data.WriteInterfaceToken(GetDescriptor())) {
316         WIFI_LOGE("Write interface token error: %{public}s", __func__);
317         return WIFI_OPT_FAILED;
318     }
319     data.WriteInt32(0);
320     WriteWifiP2pServiceInfo(data, srvInfo);
321     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES),
322         data, reply, option);
323     if (error != ERR_NONE) {
324         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
325             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES), error);
326         return WIFI_OPT_FAILED;
327     }
328 
329     int exception = reply.ReadInt32();
330     if (exception) {
331         return WIFI_OPT_FAILED;
332     }
333     return ErrCode(reply.ReadInt32());
334 }
335 
StartP2pListen(int period,int interval)336 ErrCode WifiP2pProxy::StartP2pListen(int period, int interval)
337 {
338     if (mRemoteDied) {
339         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
340         return WIFI_OPT_FAILED;
341     }
342     MessageOption option;
343     MessageParcel data;
344     MessageParcel reply;
345     if (!data.WriteInterfaceToken(GetDescriptor())) {
346         WIFI_LOGE("Write interface token error: %{public}s", __func__);
347         return WIFI_OPT_FAILED;
348     }
349     data.WriteInt32(0);
350     data.WriteInt32(period);
351     data.WriteInt32(interval);
352     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_START_LISTEN), data,
353         reply, option);
354     if (error != ERR_NONE) {
355         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
356             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_START_LISTEN), error);
357         return WIFI_OPT_FAILED;
358     }
359 
360     int exception = reply.ReadInt32();
361     if (exception) {
362         return WIFI_OPT_FAILED;
363     }
364     return ErrCode(reply.ReadInt32());
365 }
366 
StopP2pListen(void)367 ErrCode WifiP2pProxy::StopP2pListen(void)
368 {
369     if (mRemoteDied) {
370         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
371         return WIFI_OPT_FAILED;
372     }
373     MessageOption option;
374     MessageParcel data;
375     MessageParcel reply;
376     if (!data.WriteInterfaceToken(GetDescriptor())) {
377         WIFI_LOGE("Write interface token error: %{public}s", __func__);
378         return WIFI_OPT_FAILED;
379     }
380     data.WriteInt32(0);
381     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_LISTEN), data,
382         reply, option);
383     if (error != ERR_NONE) {
384         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
385             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_STOP_LISTEN), error);
386         return WIFI_OPT_FAILED;
387     }
388 
389     int exception = reply.ReadInt32();
390     if (exception) {
391         return WIFI_OPT_FAILED;
392     }
393     return ErrCode(reply.ReadInt32());
394 }
395 
CreateGroup(const WifiP2pConfig & config)396 ErrCode WifiP2pProxy::CreateGroup(const WifiP2pConfig &config)
397 {
398     if (mRemoteDied) {
399         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
400         return WIFI_OPT_FAILED;
401     }
402     MessageOption option;
403     MessageParcel data;
404     MessageParcel reply;
405     if (!data.WriteInterfaceToken(GetDescriptor())) {
406         WIFI_LOGE("Write interface token error: %{public}s", __func__);
407         return WIFI_OPT_FAILED;
408     }
409     data.WriteInt32(0);
410     WriteWifiP2pConfigData(data, config);
411     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CREATE_GROUP), data,
412         reply, option);
413     if (error != ERR_NONE) {
414         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
415             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CREATE_GROUP), error);
416         return WIFI_OPT_FAILED;
417     }
418 
419     int exception = reply.ReadInt32();
420     if (exception) {
421         return WIFI_OPT_FAILED;
422     }
423     return ErrCode(reply.ReadInt32());
424 }
425 
RemoveGroup()426 ErrCode WifiP2pProxy::RemoveGroup()
427 {
428     if (mRemoteDied) {
429         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
430         return WIFI_OPT_FAILED;
431     }
432     MessageOption option;
433     MessageParcel data;
434     MessageParcel reply;
435     if (!data.WriteInterfaceToken(GetDescriptor())) {
436         WIFI_LOGE("Write interface token error: %{public}s", __func__);
437         return WIFI_OPT_FAILED;
438     }
439     data.WriteInt32(0);
440     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP), data,
441         reply, option);
442     if (error != ERR_NONE) {
443         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
444             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP), error);
445         return WIFI_OPT_FAILED;
446     }
447     int exception = reply.ReadInt32();
448     if (exception) {
449         return WIFI_OPT_FAILED;
450     }
451     return ErrCode(reply.ReadInt32());
452 }
453 
RemoveGroupClient(const GcInfo & info)454 ErrCode WifiP2pProxy::RemoveGroupClient(const GcInfo &info)
455 {
456     if (mRemoteDied) {
457         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
458         return WIFI_OPT_FAILED;
459     }
460     MessageOption option;
461     MessageParcel data;
462     MessageParcel reply;
463     if (!data.WriteInterfaceToken(GetDescriptor())) {
464         WIFI_LOGE("Write interface token error: %{public}s", __func__);
465         return WIFI_OPT_FAILED;
466     }
467     data.WriteInt32(0);
468     data.WriteString(info.ip);
469     data.WriteString(info.mac);
470     data.WriteString(info.host);
471 
472     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP_CLIENT),
473         data, reply, option);
474     if (error != ERR_NONE) {
475         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
476             P2PInterfaceCode::WIFI_SVR_CMD_P2P_REMOVE_GROUP_CLIENT, error);
477         return WIFI_OPT_FAILED;
478     }
479     int exception = reply.ReadInt32();
480     if (exception) {
481         return WIFI_OPT_FAILED;
482     }
483     return ErrCode(reply.ReadInt32());
484 }
485 
DeleteGroup(const WifiP2pGroupInfo & group)486 ErrCode WifiP2pProxy::DeleteGroup(const WifiP2pGroupInfo &group)
487 {
488     if (mRemoteDied) {
489         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
490         return WIFI_OPT_FAILED;
491     }
492     MessageOption option;
493     MessageParcel data;
494     MessageParcel reply;
495     if (!data.WriteInterfaceToken(GetDescriptor())) {
496         WIFI_LOGE("Write interface token error: %{public}s", __func__);
497         return WIFI_OPT_FAILED;
498     }
499     data.WriteInt32(0);
500     WriteWifiP2pGroupData(data, group);
501     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP), data,
502         reply, option);
503     if (error != ERR_NONE) {
504         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
505             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP), error);
506         return WIFI_OPT_FAILED;
507     }
508     int exception = reply.ReadInt32();
509     if (exception) {
510         return WIFI_OPT_FAILED;
511     }
512     return ErrCode(reply.ReadInt32());
513 }
514 
ReadWifiP2pServiceInfo(MessageParcel & reply,WifiP2pServiceInfo & info) const515 void WifiP2pProxy::ReadWifiP2pServiceInfo(MessageParcel &reply, WifiP2pServiceInfo &info) const
516 {
517     const char *readStr = nullptr;
518     readStr = reply.ReadCString();
519     info.SetServiceName((readStr != nullptr) ? readStr : "");
520     readStr = reply.ReadCString();
521     info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
522     info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(reply.ReadInt32()));
523     std::vector<std::string> queryList;
524 
525     constexpr int MAX_SIZE = 512;
526     int size = reply.ReadInt32();
527     if (size > MAX_SIZE) {
528         WIFI_LOGE("P2p service size error: %{public}d", size);
529         return;
530     }
531     for (int i = 0; i < size; i++) {
532         readStr = reply.ReadCString();
533         std::string str = (readStr != nullptr) ? readStr : "";
534         queryList.push_back(str);
535     }
536     info.SetQueryList(queryList);
537     return;
538 }
539 
WriteWifiP2pServiceInfo(MessageParcel & data,const WifiP2pServiceInfo & info) const540 void WifiP2pProxy::WriteWifiP2pServiceInfo(MessageParcel &data, const WifiP2pServiceInfo &info) const
541 {
542     data.WriteCString(info.GetServiceName().c_str());
543     data.WriteCString(info.GetDeviceAddress().c_str());
544     data.WriteInt32(static_cast<int>(info.GetServicerProtocolType()));
545     std::vector<std::string> queryList = info.GetQueryList();
546     data.WriteInt32(queryList.size());
547     for (std::size_t i = 0; i < queryList.size(); i++) {
548         data.WriteCString(queryList[i].c_str());
549     }
550     return;
551 }
552 
WriteWifiP2pServiceRequest(MessageParcel & data,const WifiP2pDevice & device,const WifiP2pServiceRequest & request) const553 void WifiP2pProxy::WriteWifiP2pServiceRequest(
554     MessageParcel &data, const WifiP2pDevice &device, const WifiP2pServiceRequest &request) const
555 {
556     WriteWifiP2pDeviceData(data, device);
557     data.WriteInt32(static_cast<int>(request.GetProtocolType()));
558     data.WriteInt32(request.GetTransactionId());
559     std::vector<unsigned char> query = request.GetQuery();
560     data.WriteInt32(query.size());
561     for (std::size_t i = 0; i < query.size(); i++) {
562         data.WriteInt8(query[i]);
563     }
564     return;
565 }
566 
WriteWifiP2pDeviceData(MessageParcel & data,const WifiP2pDevice & device) const567 void WifiP2pProxy::WriteWifiP2pDeviceData(MessageParcel &data, const WifiP2pDevice &device) const
568 {
569     data.WriteString(device.GetDeviceName());
570     data.WriteString(device.GetDeviceAddress());
571     data.WriteInt32(device.GetDeviceAddressType());
572     data.WriteString(device.GetPrimaryDeviceType());
573     data.WriteString(device.GetSecondaryDeviceType());
574     data.WriteInt32(static_cast<int>(device.GetP2pDeviceStatus()));
575     data.WriteBool(device.GetWfdInfo().GetWfdEnabled());
576     data.WriteInt32(device.GetWfdInfo().GetDeviceInfo());
577     data.WriteInt32(device.GetWfdInfo().GetCtrlPort());
578     data.WriteInt32(device.GetWfdInfo().GetMaxThroughput());
579     data.WriteInt32(device.GetWpsConfigMethod());
580     data.WriteInt32(device.GetDeviceCapabilitys());
581     data.WriteInt32(device.GetGroupCapabilitys());
582 }
583 
ReadWifiP2pDeviceData(MessageParcel & reply,WifiP2pDevice & device) const584 void WifiP2pProxy::ReadWifiP2pDeviceData(MessageParcel &reply, WifiP2pDevice &device) const
585 {
586     device.SetDeviceName(reply.ReadString());
587     device.SetDeviceAddress(reply.ReadString());
588     device.SetRandomDeviceAddress(reply.ReadString());
589     device.SetDeviceAddressType(reply.ReadInt32());
590     device.SetPrimaryDeviceType(reply.ReadString());
591     device.SetSecondaryDeviceType(reply.ReadString());
592     device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(reply.ReadInt32()));
593     WifiP2pWfdInfo wfdInfo;
594     wfdInfo.SetWfdEnabled(reply.ReadBool());
595     wfdInfo.SetDeviceInfo(reply.ReadInt32());
596     wfdInfo.SetCtrlPort(reply.ReadInt32());
597     wfdInfo.SetMaxThroughput(reply.ReadInt32());
598     device.SetWfdInfo(wfdInfo);
599     device.SetWpsConfigMethod(reply.ReadInt32());
600     device.SetDeviceCapabilitys(reply.ReadInt32());
601     device.SetGroupCapabilitys(reply.ReadInt32());
602 }
603 
WriteWifiP2pGroupData(MessageParcel & data,const WifiP2pGroupInfo & info) const604 void WifiP2pProxy::WriteWifiP2pGroupData(MessageParcel &data, const WifiP2pGroupInfo &info) const
605 {
606     data.WriteBool(info.IsGroupOwner());
607     WriteWifiP2pDeviceData(data, info.GetOwner());
608     data.WriteString(info.GetPassphrase());
609     data.WriteString(info.GetInterface());
610     data.WriteString(info.GetGroupName());
611     data.WriteInt32(info.GetFrequency());
612     data.WriteBool(info.IsPersistent());
613     data.WriteInt32(static_cast<int>(info.GetP2pGroupStatus()));
614     data.WriteInt32(info.GetNetworkId());
615     data.WriteString(info.GetGoIpAddress());
616     std::vector<WifiP2pDevice> deviceVec;
617     deviceVec = info.GetClientDevices();
618     data.WriteInt32(deviceVec.size());
619     for (auto it = deviceVec.begin(); it != deviceVec.end(); ++it) {
620         WriteWifiP2pDeviceData(data, *it);
621     }
622 }
623 
ReadWifiP2pGroupData(MessageParcel & reply,WifiP2pGroupInfo & info) const624 void WifiP2pProxy::ReadWifiP2pGroupData(MessageParcel &reply, WifiP2pGroupInfo &info) const
625 {
626     info.SetIsGroupOwner(reply.ReadBool());
627     WifiP2pDevice device;
628     ReadWifiP2pDeviceData(reply, device);
629     info.SetOwner(device);
630     info.SetPassphrase(reply.ReadString());
631     info.SetInterface(reply.ReadString());
632     info.SetGroupName(reply.ReadString());
633     info.SetFrequency(reply.ReadInt32());
634     info.SetIsPersistent(reply.ReadBool());
635     info.SetP2pGroupStatus(static_cast<P2pGroupStatus>(reply.ReadInt32()));
636     info.SetNetworkId(reply.ReadInt32());
637     info.SetGoIpAddress(reply.ReadString());
638 
639     constexpr int MAX_SIZE = 512;
640     int size = reply.ReadInt32();
641     if (size > MAX_SIZE) {
642         WIFI_LOGE("Group info device size error: %{public}d", size);
643         return;
644     }
645     for (auto it = 0; it < size; ++it) {
646         WifiP2pDevice cliDev;
647         ReadWifiP2pDeviceData(reply, cliDev);
648         info.AddClientDevice(cliDev);
649     }
650 }
651 
WriteWifiP2pConfigData(MessageParcel & data,const WifiP2pConfig & config) const652 void WifiP2pProxy::WriteWifiP2pConfigData(MessageParcel &data, const WifiP2pConfig &config) const
653 {
654     data.WriteString(config.GetDeviceAddress());
655     data.WriteInt32(config.GetDeviceAddressType());
656     data.WriteString(config.GetPassphrase());
657     data.WriteString(config.GetGroupName());
658     data.WriteInt32(static_cast<int>(config.GetGoBand()));
659     data.WriteInt32(config.GetNetId());
660     data.WriteInt32(config.GetGroupOwnerIntent());
661 }
662 
P2pConnect(const WifiP2pConfig & config)663 ErrCode WifiP2pProxy::P2pConnect(const WifiP2pConfig &config)
664 {
665     if (mRemoteDied) {
666         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
667         return WIFI_OPT_FAILED;
668     }
669     MessageOption option;
670     MessageParcel data;
671     MessageParcel reply;
672     if (!data.WriteInterfaceToken(GetDescriptor())) {
673         WIFI_LOGE("Write interface token error: %{public}s", __func__);
674         return WIFI_OPT_FAILED;
675     }
676     data.WriteInt32(0);
677     WriteWifiP2pConfigData(data, config);
678     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT), data, reply,
679         option);
680     if (error != ERR_NONE) {
681         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
682             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT), error);
683         return WIFI_OPT_FAILED;
684     }
685 
686     int exception = reply.ReadInt32();
687     if (exception) {
688         return WIFI_OPT_FAILED;
689     }
690     return ErrCode(reply.ReadInt32());
691 }
692 
P2pCancelConnect()693 ErrCode WifiP2pProxy::P2pCancelConnect()
694 {
695     if (mRemoteDied) {
696         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
697         return WIFI_OPT_FAILED;
698     }
699     MessageOption option;
700     MessageParcel data;
701     MessageParcel reply;
702     if (!data.WriteInterfaceToken(GetDescriptor())) {
703         WIFI_LOGE("Write interface token error: %{public}s", __func__);
704         return WIFI_OPT_FAILED;
705     }
706     data.WriteInt32(0);
707     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT), data,
708         reply, option);
709     if (error != ERR_NONE) {
710         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
711             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT), error);
712         return WIFI_OPT_FAILED;
713     }
714 
715     int exception = reply.ReadInt32();
716     if (exception) {
717         return WIFI_OPT_FAILED;
718     }
719     return ErrCode(reply.ReadInt32());
720 }
721 
QueryP2pLinkedInfo(WifiP2pLinkedInfo & linkedInfo)722 ErrCode WifiP2pProxy::QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo)
723 {
724     if (mRemoteDied) {
725         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
726         return WIFI_OPT_FAILED;
727     }
728     MessageOption option;
729     MessageParcel data;
730     MessageParcel reply;
731     if (!data.WriteInterfaceToken(GetDescriptor())) {
732         WIFI_LOGE("Write interface token error: %{public}s", __func__);
733         return WIFI_OPT_FAILED;
734     }
735     data.WriteInt32(0);
736     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO), data, reply,
737         option);
738     if (error != ERR_NONE) {
739         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
740             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO), error);
741         return WIFI_OPT_FAILED;
742     }
743 
744     int exception = reply.ReadInt32();
745     if (exception) {
746         return WIFI_OPT_FAILED;
747     }
748     int ret = reply.ReadInt32();
749     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
750         return ErrCode(ret);
751     }
752     linkedInfo.SetConnectState(static_cast<P2pConnectedState>(reply.ReadInt32()));
753     linkedInfo.SetIsGroupOwner(reply.ReadBool());
754     std::string groupOwnerAddr = reply.ReadString();
755     linkedInfo.SetIsGroupOwnerAddress(groupOwnerAddr);
756 
757     int size = reply.ReadInt32();
758     for (int i = 0; i < size; i++) {
759         linkedInfo.AddClientInfoList(reply.ReadString(), reply.ReadString(), reply.ReadString());
760     }
761     return WIFI_OPT_SUCCESS;
762 }
763 
GetCurrentGroup(WifiP2pGroupInfo & group)764 ErrCode WifiP2pProxy::GetCurrentGroup(WifiP2pGroupInfo &group)
765 {
766     if (mRemoteDied) {
767         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
768         return WIFI_OPT_FAILED;
769     }
770     MessageOption option;
771     MessageParcel data;
772     MessageParcel reply;
773     if (!data.WriteInterfaceToken(GetDescriptor())) {
774         WIFI_LOGE("Write interface token error: %{public}s", __func__);
775         return WIFI_OPT_FAILED;
776     }
777     data.WriteInt32(0);
778     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP), data,
779         reply, option);
780     if (error != ERR_NONE) {
781         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
782             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP), error);
783         return WIFI_OPT_FAILED;
784     }
785 
786     int exception = reply.ReadInt32();
787     if (exception) {
788         return WIFI_OPT_FAILED;
789     }
790     int ret = reply.ReadInt32();
791     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
792         return ErrCode(ret);
793     }
794     ReadWifiP2pGroupData(reply, group);
795     return WIFI_OPT_SUCCESS;
796 }
797 
GetP2pEnableStatus(int & status)798 ErrCode WifiP2pProxy::GetP2pEnableStatus(int &status)
799 {
800     if (mRemoteDied) {
801         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
802         return WIFI_OPT_FAILED;
803     }
804     MessageOption option;
805     MessageParcel data;
806     MessageParcel reply;
807     if (!data.WriteInterfaceToken(GetDescriptor())) {
808         WIFI_LOGE("Write interface token error: %{public}s", __func__);
809         return WIFI_OPT_FAILED;
810     }
811     data.WriteInt32(0);
812     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS), data,
813         reply, option);
814     if (error != ERR_NONE) {
815         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
816             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS), error);
817         return WIFI_OPT_FAILED;
818     }
819 
820     int exception = reply.ReadInt32();
821     if (exception) {
822         return WIFI_OPT_FAILED;
823     }
824     int ret = reply.ReadInt32();
825     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
826         return ErrCode(ret);
827     }
828     status = reply.ReadInt32();
829     return WIFI_OPT_SUCCESS;
830 }
831 
GetP2pDiscoverStatus(int & status)832 ErrCode WifiP2pProxy::GetP2pDiscoverStatus(int &status)
833 {
834     if (mRemoteDied) {
835         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
836         return WIFI_OPT_FAILED;
837     }
838     MessageOption option;
839     MessageParcel data;
840     MessageParcel reply;
841     if (!data.WriteInterfaceToken(GetDescriptor())) {
842         WIFI_LOGE("Write interface token error: %{public}s", __func__);
843         return WIFI_OPT_FAILED;
844     }
845     data.WriteInt32(0);
846     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS),
847         data, reply, option);
848     if (error != ERR_NONE) {
849         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
850             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS), error);
851         return WIFI_OPT_FAILED;
852     }
853 
854     int exception = reply.ReadInt32();
855     if (exception) {
856         return WIFI_OPT_FAILED;
857     }
858     int ret = reply.ReadInt32();
859     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
860         return ErrCode(ret);
861     }
862     status = reply.ReadInt32();
863     return WIFI_OPT_SUCCESS;
864 }
865 
GetP2pConnectedStatus(int & status)866 ErrCode WifiP2pProxy::GetP2pConnectedStatus(int &status)
867 {
868     if (mRemoteDied) {
869         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
870         return WIFI_OPT_FAILED;
871     }
872     MessageOption option;
873     MessageParcel data;
874     MessageParcel reply;
875     if (!data.WriteInterfaceToken(GetDescriptor())) {
876         WIFI_LOGE("Write interface token error: %{public}s", __func__);
877         return WIFI_OPT_FAILED;
878     }
879     data.WriteInt32(0);
880     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS),
881         data, reply, option);
882     if (error != ERR_NONE) {
883         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
884             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS), error);
885         return WIFI_OPT_FAILED;
886     }
887 
888     int exception = reply.ReadInt32();
889     if (exception) {
890         return WIFI_OPT_FAILED;
891     }
892     int ret = reply.ReadInt32();
893     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
894         return ErrCode(ret);
895     }
896     status = reply.ReadInt32();
897     return WIFI_OPT_SUCCESS;
898 }
899 
QueryP2pDevices(std::vector<WifiP2pDevice> & devices)900 ErrCode WifiP2pProxy::QueryP2pDevices(std::vector<WifiP2pDevice> &devices)
901 {
902     if (mRemoteDied) {
903         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
904         return WIFI_OPT_FAILED;
905     }
906     MessageOption option;
907     MessageParcel data;
908     MessageParcel reply;
909     if (!data.WriteInterfaceToken(GetDescriptor())) {
910         WIFI_LOGE("Write interface token error: %{public}s", __func__);
911         return WIFI_OPT_FAILED;
912     }
913     data.WriteInt32(0);
914     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES), data,
915         reply, option);
916     if (error != ERR_NONE) {
917         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
918             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES), error);
919         return WIFI_OPT_FAILED;
920     }
921 
922     int exception = reply.ReadInt32();
923     if (exception) {
924         return WIFI_OPT_FAILED;
925     }
926     int ret = reply.ReadInt32();
927     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
928         return ErrCode(ret);
929     }
930 
931     constexpr int MAX_SIZE = 512;
932     int size = reply.ReadInt32();
933     if (size > MAX_SIZE) {
934         WIFI_LOGE("Get p2p devices size error: %{public}d", size);
935         return WIFI_OPT_FAILED;
936     }
937     for (int i = 0; i < size; ++i) {
938         WifiP2pDevice config;
939         ReadWifiP2pDeviceData(reply, config);
940         devices.emplace_back(config);
941     }
942     return WIFI_OPT_SUCCESS;
943 }
944 
QueryP2pLocalDevice(WifiP2pDevice & device)945 ErrCode WifiP2pProxy::QueryP2pLocalDevice(WifiP2pDevice &device)
946 {
947     if (mRemoteDied) {
948         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
949         return WIFI_OPT_FAILED;
950     }
951     MessageOption option;
952     MessageParcel data;
953     MessageParcel reply;
954     if (!data.WriteInterfaceToken(GetDescriptor())) {
955         WIFI_LOGE("Write interface token error: %{public}s", __func__);
956         return WIFI_OPT_FAILED;
957     }
958     data.WriteInt32(0);
959     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE),
960         data, reply, option);
961     if (error != ERR_NONE) {
962         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
963             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE), error);
964         return WIFI_OPT_FAILED;
965     }
966 
967     int exception = reply.ReadInt32();
968     if (exception) {
969         return WIFI_OPT_FAILED;
970     }
971     int ret = reply.ReadInt32();
972     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
973         return ErrCode(ret);
974     }
975     ReadWifiP2pDeviceData(reply, device);
976     return WIFI_OPT_SUCCESS;
977 }
978 
QueryP2pGroups(std::vector<WifiP2pGroupInfo> & groups)979 ErrCode WifiP2pProxy::QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups)
980 {
981     if (mRemoteDied) {
982         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
983         return WIFI_OPT_FAILED;
984     }
985     MessageOption option;
986     MessageParcel data;
987     MessageParcel reply;
988     if (!data.WriteInterfaceToken(GetDescriptor())) {
989         WIFI_LOGE("Write interface token error: %{public}s", __func__);
990         return WIFI_OPT_FAILED;
991     }
992     data.WriteInt32(0);
993     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS), data,
994         reply, option);
995     if (error != ERR_NONE) {
996         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
997             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS), error);
998         return WIFI_OPT_FAILED;
999     }
1000 
1001     int exception = reply.ReadInt32();
1002     if (exception) {
1003         return WIFI_OPT_FAILED;
1004     }
1005     int ret = reply.ReadInt32();
1006     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1007         return ErrCode(ret);
1008     }
1009 
1010     constexpr int MAX_SIZE = 512;
1011     int size = reply.ReadInt32();
1012     if (size > MAX_SIZE) {
1013         WIFI_LOGE("Get p2p group size error: %{public}d", size);
1014         return WIFI_OPT_FAILED;
1015     }
1016     for (int i = 0; i < size; ++i) {
1017         WifiP2pGroupInfo group;
1018         ReadWifiP2pGroupData(reply, group);
1019         groups.emplace_back(group);
1020     }
1021     return WIFI_OPT_SUCCESS;
1022 }
1023 
QueryP2pServices(std::vector<WifiP2pServiceInfo> & services)1024 ErrCode WifiP2pProxy::QueryP2pServices(std::vector<WifiP2pServiceInfo> &services)
1025 {
1026     if (mRemoteDied) {
1027         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1028         return WIFI_OPT_FAILED;
1029     }
1030     MessageOption option;
1031     MessageParcel data;
1032     MessageParcel reply;
1033     if (!data.WriteInterfaceToken(GetDescriptor())) {
1034         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1035         return WIFI_OPT_FAILED;
1036     }
1037     data.WriteInt32(0);
1038     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES), data,
1039         reply, option);
1040     if (error != ERR_NONE) {
1041         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1042             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES), error);
1043         return WIFI_OPT_FAILED;
1044     }
1045 
1046     int exception = reply.ReadInt32();
1047     if (exception) {
1048         return WIFI_OPT_FAILED;
1049     }
1050     int ret = reply.ReadInt32();
1051     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1052         return ErrCode(ret);
1053     }
1054 
1055     constexpr int MAX_SIZE = 512;
1056     int size = reply.ReadInt32();
1057     if (size > MAX_SIZE) {
1058         WIFI_LOGE("Get p2p service size error: %{public}d", size);
1059         return WIFI_OPT_FAILED;
1060     }
1061     for (int i = 0; i < size; ++i) {
1062         WifiP2pServiceInfo info;
1063         ReadWifiP2pServiceInfo(reply, info);
1064         services.emplace_back(info);
1065     }
1066     return WIFI_OPT_SUCCESS;
1067 }
1068 
SetP2pDeviceName(const std::string & deviceName)1069 ErrCode WifiP2pProxy::SetP2pDeviceName(const std::string &deviceName)
1070 {
1071     if (mRemoteDied) {
1072         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1073         return WIFI_OPT_FAILED;
1074     }
1075     MessageOption option;
1076     MessageParcel data;
1077     MessageParcel reply;
1078     if (!data.WriteInterfaceToken(GetDescriptor())) {
1079         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1080         return WIFI_OPT_FAILED;
1081     }
1082     data.WriteInt32(0);
1083     data.WriteString(deviceName);
1084     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME), data,
1085         reply, option);
1086     if (error != ERR_NONE) {
1087         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1088             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME), error);
1089         return WIFI_OPT_FAILED;
1090     }
1091 
1092     int exception = reply.ReadInt32();
1093     if (exception) {
1094         return WIFI_OPT_FAILED;
1095     }
1096     int ret = reply.ReadInt32();
1097     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1098         return ErrCode(ret);
1099     }
1100     return WIFI_OPT_SUCCESS;
1101 }
SetP2pWfdInfo(const WifiP2pWfdInfo & wfdInfo)1102 ErrCode WifiP2pProxy::SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo)
1103 {
1104     if (mRemoteDied) {
1105         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1106         return WIFI_OPT_FAILED;
1107     }
1108     MessageOption option;
1109     MessageParcel data;
1110     MessageParcel reply;
1111     if (!data.WriteInterfaceToken(GetDescriptor())) {
1112         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1113         return WIFI_OPT_FAILED;
1114     }
1115     data.WriteInt32(0);
1116     data.WriteBool(wfdInfo.GetWfdEnabled());
1117     data.WriteInt32(wfdInfo.GetDeviceInfo());
1118     data.WriteInt32(wfdInfo.GetCtrlPort());
1119     data.WriteInt32(wfdInfo.GetMaxThroughput());
1120     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO), data,
1121         reply, option);
1122     if (error != ERR_NONE) {
1123         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1124             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO), error);
1125         return WIFI_OPT_FAILED;
1126     }
1127 
1128     int exception = reply.ReadInt32();
1129     if (exception) {
1130         return WIFI_OPT_FAILED;
1131     }
1132     int ret = reply.ReadInt32();
1133     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1134         return ErrCode(ret);
1135     }
1136     return WIFI_OPT_SUCCESS;
1137 }
1138 
1139 
RegisterCallBack(const sptr<IWifiP2pCallback> & callback,const std::vector<std::string> & event)1140 ErrCode WifiP2pProxy::RegisterCallBack(const sptr<IWifiP2pCallback> &callback, const std::vector<std::string> &event)
1141 {
1142     if (mRemoteDied) {
1143         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1144         return WIFI_OPT_FAILED;
1145     }
1146     MessageParcel data;
1147     MessageParcel reply;
1148     MessageOption option(MessageOption::TF_ASYNC);
1149 
1150     if (g_wifiP2pCallbackStub == nullptr) {
1151         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1152         return WIFI_OPT_FAILED;
1153     }
1154     g_wifiP2pCallbackStub->RegisterCallBack(callback);
1155     if (!data.WriteInterfaceToken(GetDescriptor())) {
1156         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1157         return WIFI_OPT_FAILED;
1158     }
1159     data.WriteInt32(0);
1160     if (!data.WriteRemoteObject(g_wifiP2pCallbackStub->AsObject())) {
1161         WIFI_LOGE("RegisterCallBack WriteRemoteObject failed!");
1162         return WIFI_OPT_FAILED;
1163     }
1164     int pid = GetCallingPid();
1165     data.WriteInt32(pid);
1166     int tokenId = GetCallingTokenId();
1167     data.WriteInt32(tokenId);
1168     int eventNum = static_cast<int>(event.size());
1169     data.WriteInt32(eventNum);
1170     if (eventNum > 0) {
1171         for (auto &eventName : event) {
1172             data.WriteString(eventName);
1173         }
1174     }
1175     WIFI_LOGD("%{public}s, calling uid: %{public}d, pid: %{public}d, tokenId: %{private}d",
1176         __func__, GetCallingUid(), pid, tokenId);
1177     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REGISTER_CALLBACK), data,
1178         reply, option);
1179     if (error != ERR_NONE) {
1180         WIFI_LOGE("RegisterCallBack failed, error code is %{public}d", error);
1181         return WIFI_OPT_FAILED;
1182     }
1183     int exception = reply.ReadInt32();
1184     if (exception) {
1185         return WIFI_OPT_FAILED;
1186     }
1187     int ret = reply.ReadInt32();
1188     WIFI_LOGD("RegisterCallBack is finished: result=%{public}d", ret);
1189     return ErrCode(ret);
1190 }
1191 
GetSupportedFeatures(long & features)1192 ErrCode WifiP2pProxy::GetSupportedFeatures(long &features)
1193 {
1194     if (mRemoteDied) {
1195         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1196         return WIFI_OPT_FAILED;
1197     }
1198     MessageOption option;
1199     MessageParcel data;
1200     MessageParcel reply;
1201     if (!data.WriteInterfaceToken(GetDescriptor())) {
1202         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1203         return WIFI_OPT_FAILED;
1204     }
1205     data.WriteInt32(0);
1206     int error = Remote()->SendRequest(static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES),
1207         data, reply, option);
1208     if (error != ERR_NONE) {
1209         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1210             static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES), error);
1211         return ErrCode(error);
1212     }
1213     int exception = reply.ReadInt32();
1214     if (exception) {
1215         return WIFI_OPT_FAILED;
1216     }
1217     int ret = reply.ReadInt32();
1218     if (ret != WIFI_OPT_SUCCESS) {
1219         return ErrCode(ret);
1220     }
1221 
1222     features = reply.ReadInt64();
1223     return WIFI_OPT_SUCCESS;
1224 }
1225 
Hid2dRequestGcIp(const std::string & gcMac,std::string & ipAddr)1226 ErrCode WifiP2pProxy::Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr)
1227 {
1228     if (mRemoteDied) {
1229         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1230         return WIFI_OPT_FAILED;
1231     }
1232     MessageOption option;
1233     MessageParcel data;
1234     MessageParcel reply;
1235     if (!data.WriteInterfaceToken(GetDescriptor())) {
1236         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1237         return WIFI_OPT_FAILED;
1238     }
1239     data.WriteInt32(0);
1240     data.WriteCString(gcMac.c_str());
1241     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP), data,
1242         reply, option);
1243     if (error != ERR_NONE) {
1244         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1245             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP), error);
1246         return WIFI_OPT_FAILED;
1247     }
1248     int exception = reply.ReadInt32();
1249     if (exception) {
1250         return WIFI_OPT_FAILED;
1251     }
1252     int ret = reply.ReadInt32();
1253     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1254         return ErrCode(ret);
1255     }
1256     const char *ipAddress = reply.ReadCString();
1257     ipAddr = (ipAddress != nullptr) ? ipAddress : "";
1258     return WIFI_OPT_SUCCESS;
1259 }
1260 
Hid2dSharedlinkIncrease()1261 ErrCode WifiP2pProxy::Hid2dSharedlinkIncrease()
1262 {
1263     if (mRemoteDied) {
1264         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1265         return WIFI_OPT_FAILED;
1266     }
1267     MessageOption option;
1268     MessageParcel data;
1269     MessageParcel reply;
1270     if (!data.WriteInterfaceToken(GetDescriptor())) {
1271         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1272         return WIFI_OPT_FAILED;
1273     }
1274     data.WriteInt32(0);
1275     int error = Remote()->SendRequest(
1276         static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE), data, reply, option);
1277     if (error != ERR_NONE) {
1278         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1279             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE), error);
1280         return WIFI_OPT_FAILED;
1281     }
1282     int exception = reply.ReadInt32();
1283     if (exception) {
1284         return WIFI_OPT_FAILED;
1285     }
1286     return ErrCode(reply.ReadInt32());
1287 }
1288 
Hid2dSharedlinkDecrease()1289 ErrCode WifiP2pProxy::Hid2dSharedlinkDecrease()
1290 {
1291     if (mRemoteDied) {
1292         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1293         return WIFI_OPT_FAILED;
1294     }
1295     MessageOption option;
1296     MessageParcel data;
1297     MessageParcel reply;
1298     if (!data.WriteInterfaceToken(GetDescriptor())) {
1299         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1300         return WIFI_OPT_FAILED;
1301     }
1302     data.WriteInt32(0);
1303     int error = Remote()->SendRequest(
1304         static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE), data, reply, option);
1305     if (error != ERR_NONE) {
1306         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1307             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE), error);
1308         return WIFI_OPT_FAILED;
1309     }
1310     int exception = reply.ReadInt32();
1311     if (exception) {
1312         return WIFI_OPT_FAILED;
1313     }
1314     return ErrCode(reply.ReadInt32());
1315 }
1316 
Hid2dCreateGroup(const int frequency,FreqType type)1317 ErrCode WifiP2pProxy::Hid2dCreateGroup(const int frequency, FreqType type)
1318 {
1319     WIFI_LOGI("Request hid2d create group");
1320 
1321     if (mRemoteDied) {
1322         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1323         return WIFI_OPT_FAILED;
1324     }
1325     MessageOption option;
1326     MessageParcel data;
1327     MessageParcel reply;
1328     if (!data.WriteInterfaceToken(GetDescriptor())) {
1329         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1330         return WIFI_OPT_FAILED;
1331     }
1332     data.WriteInt32(0);
1333     data.WriteInt32(frequency);
1334     data.WriteInt32(static_cast<int>(type));
1335     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP),
1336         data, reply, option);
1337     if (error != ERR_NONE) {
1338         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1339             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP), error);
1340         return WIFI_OPT_FAILED;
1341     }
1342     int exception = reply.ReadInt32();
1343     if (exception) {
1344         return WIFI_OPT_FAILED;
1345     }
1346     return ErrCode(reply.ReadInt32());
1347 }
1348 
Hid2dRemoveGcGroup(const std::string & gcIfName)1349 ErrCode WifiP2pProxy::Hid2dRemoveGcGroup(const std::string& gcIfName)
1350 {
1351     WIFI_LOGI("Request hid2d remove group");
1352 
1353     if (mRemoteDied) {
1354         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1355         return WIFI_OPT_FAILED;
1356     }
1357     MessageOption option;
1358     MessageParcel data;
1359     MessageParcel reply;
1360     if (!data.WriteInterfaceToken(GetDescriptor())) {
1361         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1362         return WIFI_OPT_FAILED;
1363     }
1364     data.WriteInt32(0);
1365     data.WriteCString(gcIfName.c_str());
1366     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP),
1367         data, reply, option);
1368     if (error != ERR_NONE) {
1369         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1370             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP), error);
1371         return WIFI_OPT_FAILED;
1372     }
1373     int exception = reply.ReadInt32();
1374     if (exception) {
1375         return WIFI_OPT_FAILED;
1376     }
1377     return ErrCode(reply.ReadInt32());
1378 }
1379 
Hid2dConnect(const Hid2dConnectConfig & config)1380 ErrCode WifiP2pProxy::Hid2dConnect(const Hid2dConnectConfig& config)
1381 {
1382     WIFI_LOGI("Request hid2d connect");
1383 
1384     if (mRemoteDied) {
1385         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1386         return WIFI_OPT_FAILED;
1387     }
1388     MessageOption option;
1389     MessageParcel data;
1390     MessageParcel reply;
1391     if (!data.WriteInterfaceToken(GetDescriptor())) {
1392         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1393         return WIFI_OPT_FAILED;
1394     }
1395     data.WriteInt32(0);
1396     data.WriteCString(config.GetSsid().c_str());
1397     data.WriteCString(config.GetBssid().c_str());
1398     data.WriteCString(config.GetPreSharedKey().c_str());
1399     data.WriteInt32(config.GetFrequency());
1400     data.WriteInt32(static_cast<int>(config.GetDhcpMode()));
1401     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT), data,
1402         reply, option);
1403     if (error != ERR_NONE) {
1404         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1405             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT), error);
1406         return WIFI_OPT_FAILED;
1407     }
1408     int exception = reply.ReadInt32();
1409     if (exception) {
1410         return WIFI_OPT_FAILED;
1411     }
1412     return ErrCode(reply.ReadInt32());
1413 }
1414 
Hid2dConfigIPAddr(const std::string & ifName,const IpAddrInfo & ipInfo)1415 ErrCode WifiP2pProxy::Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo)
1416 {
1417     if (mRemoteDied) {
1418         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1419         return WIFI_OPT_FAILED;
1420     }
1421     MessageOption option;
1422     MessageParcel data;
1423     MessageParcel reply;
1424     if (!data.WriteInterfaceToken(GetDescriptor())) {
1425         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1426         return WIFI_OPT_FAILED;
1427     }
1428     data.WriteInt32(0);
1429     data.WriteCString(ifName.c_str());
1430     data.WriteCString(ipInfo.ip.c_str());
1431     data.WriteCString(ipInfo.gateway.c_str());
1432     data.WriteCString(ipInfo.netmask.c_str());
1433     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP), data,
1434         reply, option);
1435     if (error != ERR_NONE) {
1436         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1437             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP), error);
1438         return WIFI_OPT_FAILED;
1439     }
1440     int exception = reply.ReadInt32();
1441     if (exception) {
1442         return WIFI_OPT_FAILED;
1443     }
1444     return ErrCode(reply.ReadInt32());
1445 }
1446 
Hid2dReleaseIPAddr(const std::string & ifName)1447 ErrCode WifiP2pProxy::Hid2dReleaseIPAddr(const std::string& ifName)
1448 {
1449     if (mRemoteDied) {
1450         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1451         return WIFI_OPT_FAILED;
1452     }
1453     MessageOption option;
1454     MessageParcel data;
1455     MessageParcel reply;
1456     if (!data.WriteInterfaceToken(GetDescriptor())) {
1457         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1458         return WIFI_OPT_FAILED;
1459     }
1460     data.WriteInt32(0);
1461     data.WriteCString(ifName.c_str());
1462     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP), data,
1463         reply, option);
1464     if (error != ERR_NONE) {
1465         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1466             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP), error);
1467         return WIFI_OPT_FAILED;
1468     }
1469     int exception = reply.ReadInt32();
1470     if (exception) {
1471         return WIFI_OPT_FAILED;
1472     }
1473     return ErrCode(reply.ReadInt32());
1474 }
1475 
Hid2dGetRecommendChannel(const RecommendChannelRequest & request,RecommendChannelResponse & response)1476 ErrCode WifiP2pProxy::Hid2dGetRecommendChannel(const RecommendChannelRequest& request,
1477     RecommendChannelResponse& response)
1478 {
1479     if (mRemoteDied) {
1480         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1481         return WIFI_OPT_FAILED;
1482     }
1483     MessageOption option;
1484     MessageParcel data;
1485     MessageParcel reply;
1486     if (!data.WriteInterfaceToken(GetDescriptor())) {
1487         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1488         return WIFI_OPT_FAILED;
1489     }
1490     data.WriteInt32(0);
1491     data.WriteCString(request.remoteIfName.c_str());
1492     data.WriteInt32(request.remoteIfMode);
1493     data.WriteCString(request.localIfName.c_str());
1494     data.WriteInt32(request.localIfMode);
1495     data.WriteInt32(request.prefBand);
1496     data.WriteInt32(static_cast<int>(request.prefBandwidth));
1497     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL),
1498         data, reply, option);
1499     if (error != ERR_NONE) {
1500         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1501             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL), error);
1502         return ErrCode(error);
1503     }
1504     int exception = reply.ReadInt32();
1505     if (exception) {
1506         return WIFI_OPT_FAILED;
1507     }
1508     int ret = reply.ReadInt32();
1509     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1510         return ErrCode(ret);
1511     }
1512     response.status = RecommendStatus(reply.ReadInt32());
1513     response.index = reply.ReadInt32();
1514     response.centerFreq = reply.ReadInt32();
1515     response.centerFreq1 = reply.ReadInt32();
1516     response.centerFreq2 = reply.ReadInt32();
1517     response.bandwidth = reply.ReadInt32();
1518     return WIFI_OPT_SUCCESS;
1519 }
1520 
Hid2dGetChannelListFor5G(std::vector<int> & vecChannelList)1521 ErrCode WifiP2pProxy::Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList)
1522 {
1523     if (mRemoteDied) {
1524         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1525         return WIFI_OPT_FAILED;
1526     }
1527     MessageOption option;
1528     MessageParcel data;
1529     MessageParcel reply;
1530     if (!data.WriteInterfaceToken(GetDescriptor())) {
1531         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1532         return WIFI_OPT_FAILED;
1533     }
1534     data.WriteInt32(0);
1535     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST), data,
1536         reply, option);
1537     if (error != ERR_NONE) {
1538         WIFI_LOGW("Set Attr(%{public}d) failed",
1539             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST));
1540         return WIFI_OPT_FAILED;
1541     }
1542     int exception = reply.ReadInt32();
1543     if (exception) {
1544         return WIFI_OPT_FAILED;
1545     }
1546     int ret = reply.ReadInt32();
1547     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1548         return ErrCode(ret);
1549     }
1550 
1551     constexpr int MAX_SIZE = 512;
1552     int listSize = reply.ReadInt32();
1553     if (listSize > MAX_SIZE) {
1554         WIFI_LOGE("Get channel list for 5G size error: %{public}d", listSize);
1555         return WIFI_OPT_FAILED;
1556     }
1557     for (int i = 0; i < listSize; ++i) {
1558         vecChannelList.emplace_back(reply.ReadInt32());
1559     }
1560     return WIFI_OPT_SUCCESS;
1561 }
1562 
Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int * getDatValidLen)1563 ErrCode WifiP2pProxy::Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,
1564     char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen)
1565 {
1566     if (getDatValidLen == nullptr) {
1567         WIFI_LOGE("getDatValidLen is nullptr!");
1568         return WIFI_OPT_FAILED;
1569     }
1570 
1571     if (mRemoteDied) {
1572         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1573         return WIFI_OPT_FAILED;
1574     }
1575     MessageOption option;
1576     MessageParcel data;
1577     MessageParcel reply;
1578     if (!data.WriteInterfaceToken(GetDescriptor())) {
1579         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1580         return WIFI_OPT_FAILED;
1581     }
1582     data.WriteInt32(0);
1583     data.WriteInt32(static_cast<int>(cfgType));
1584     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG), data,
1585         reply, option);
1586     if (error != ERR_NONE) {
1587         WIFI_LOGW("Set Attr(%{public}d) failed",
1588             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG));
1589         return WIFI_OPT_FAILED;
1590     }
1591     int exception = reply.ReadInt32();
1592     if (exception) {
1593         return WIFI_OPT_FAILED;
1594     }
1595     int ret = reply.ReadInt32();
1596     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1597         return ErrCode(ret);
1598     }
1599 
1600     *getDatValidLen = reply.ReadInt32();
1601     if (*getDatValidLen > 0) {
1602         const char *dataBuffer = (const char *)reply.ReadBuffer(*getDatValidLen);
1603         if (dataBuffer == nullptr) {
1604             WIFI_LOGE("`%{public}s` inner communication error!", __func__);
1605             return WIFI_OPT_FAILED;
1606         }
1607         if (memcpy_s(cfgData, CFG_DATA_MAX_BYTES, dataBuffer, *getDatValidLen) != EOK) {
1608             WIFI_LOGE("`%{public}s` memcpy_s failed!", __func__);
1609             return WIFI_OPT_FAILED;
1610         }
1611     }
1612     return WIFI_OPT_SUCCESS;
1613 }
1614 
Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int setDataValidLen)1615 ErrCode WifiP2pProxy::Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,
1616     char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen)
1617 {
1618     if (setDataValidLen <= 0) {
1619         WIFI_LOGE("`%{public}s` parameter is error!", __func__);
1620         return WIFI_OPT_INVALID_PARAM;
1621     }
1622     if (mRemoteDied) {
1623         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1624         return WIFI_OPT_FAILED;
1625     }
1626     MessageOption option;
1627     MessageParcel data;
1628     MessageParcel reply;
1629     if (!data.WriteInterfaceToken(GetDescriptor())) {
1630         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1631         return WIFI_OPT_FAILED;
1632     }
1633     data.WriteInt32(0);
1634     data.WriteInt32(static_cast<int>(cfgType));
1635     data.WriteInt32(setDataValidLen);
1636     data.WriteBuffer(cfgData, setDataValidLen);
1637     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG), data,
1638         reply, option);
1639     if (error != ERR_NONE) {
1640         WIFI_LOGW("Set Attr(%{public}d) failed",
1641             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG));
1642         return WIFI_OPT_FAILED;
1643     }
1644     int exception = reply.ReadInt32();
1645     if (exception) {
1646         return WIFI_OPT_FAILED;
1647     }
1648     int ret = reply.ReadInt32();
1649     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1650         return ErrCode(ret);
1651     }
1652     return WIFI_OPT_SUCCESS;
1653 }
1654 
Hid2dSetUpperScene(const std::string & ifName,const Hid2dUpperScene & scene)1655 ErrCode WifiP2pProxy::Hid2dSetUpperScene(const std::string& ifName, const Hid2dUpperScene& scene)
1656 {
1657     if (mRemoteDied) {
1658         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1659         return WIFI_OPT_FAILED;
1660     }
1661     MessageOption option;
1662     MessageParcel data;
1663     MessageParcel reply;
1664     if (!data.WriteInterfaceToken(GetDescriptor())) {
1665         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1666         return WIFI_OPT_FAILED;
1667     }
1668     data.WriteInt32(0);
1669     data.WriteCString(ifName.c_str());
1670     data.WriteCString(scene.mac.c_str());
1671     data.WriteUint32(scene.scene);
1672     data.WriteInt32(scene.fps);
1673     data.WriteUint32(scene.bw);
1674     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE), data,
1675         reply, option);
1676     if (error != ERR_NONE) {
1677         WIFI_LOGW("Set Attr(%{public}d) failed", static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE));
1678         return WIFI_OPT_FAILED;
1679     }
1680     int exception = reply.ReadInt32();
1681     if (exception) {
1682         return WIFI_OPT_FAILED;
1683     }
1684     return ErrCode(reply.ReadInt32());
1685 }
1686 
DiscoverPeers(int32_t channelid)1687 ErrCode WifiP2pProxy::DiscoverPeers(int32_t channelid)
1688 {
1689     if (mRemoteDied) {
1690         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1691         return WIFI_OPT_FAILED;
1692     }
1693     MessageOption option;
1694     MessageParcel data;
1695     MessageParcel reply;
1696     if (!data.WriteInterfaceToken(GetDescriptor())) {
1697         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1698         return WIFI_OPT_FAILED;
1699     }
1700     data.WriteInt32(0);
1701     data.WriteInt32(channelid);
1702     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_PEERS),
1703         data, reply, option);
1704     if (error != ERR_NONE) {
1705         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1706             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISCOVER_PEERS), error);
1707         return WIFI_OPT_FAILED;
1708     }
1709     int exception = reply.ReadInt32();
1710     if (exception) {
1711         return WIFI_OPT_FAILED;
1712     }
1713     return ErrCode(reply.ReadInt32());
1714 }
1715 
DisableRandomMac(int setmode)1716 ErrCode WifiP2pProxy::DisableRandomMac(int setmode)
1717 {
1718     if (mRemoteDied) {
1719         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1720         return WIFI_OPT_FAILED;
1721     }
1722     MessageOption option;
1723     MessageParcel data;
1724     MessageParcel reply;
1725     if (!data.WriteInterfaceToken(GetDescriptor())) {
1726         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1727         return WIFI_OPT_FAILED;
1728     }
1729     data.WriteInt32(0);
1730     data.WriteInt32(setmode);
1731     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE_RANDOM_MAC),
1732         data, reply, option);
1733     if (error != ERR_NONE) {
1734         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1735             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DISABLE_RANDOM_MAC), error);
1736         return WIFI_OPT_FAILED;
1737     }
1738     int exception = reply.ReadInt32();
1739     if (exception) {
1740         return WIFI_OPT_FAILED;
1741     }
1742     return ErrCode(reply.ReadInt32());
1743 }
1744 
CheckCanUseP2p()1745 ErrCode WifiP2pProxy::CheckCanUseP2p()
1746 {
1747     if (mRemoteDied) {
1748         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1749         return WIFI_OPT_FAILED;
1750     }
1751     MessageOption option;
1752     MessageParcel data;
1753     MessageParcel reply;
1754     if (!data.WriteInterfaceToken(GetDescriptor())) {
1755         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1756         return WIFI_OPT_FAILED;
1757     }
1758     data.WriteInt32(0);
1759     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CHECK_CAN_USE_P2P),
1760         data, reply, option);
1761     if (error != ERR_NONE) {
1762         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1763             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CHECK_CAN_USE_P2P), error);
1764         return WIFI_OPT_FAILED;
1765     }
1766     int exception = reply.ReadInt32();
1767     if (exception) {
1768         return WIFI_OPT_FAILED;
1769     }
1770     return ErrCode(reply.ReadInt32());
1771 }
1772 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)1773 void WifiP2pProxy::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
1774 {
1775     WIFI_LOGE("Remote service is died!");
1776     mRemoteDied = true;
1777     if (g_wifiP2pCallbackStub == nullptr) {
1778         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1779         return;
1780     }
1781     g_wifiP2pCallbackStub->SetRemoteDied(true);
1782 }
1783 
IsRemoteDied(void)1784 bool WifiP2pProxy::IsRemoteDied(void)
1785 {
1786     if (mRemoteDied) {
1787         WIFI_LOGW("IsRemoteDied! remote is died now!");
1788     }
1789     return mRemoteDied;
1790 }
1791 
Hid2dIsWideBandwidthSupported(bool & isSupport)1792 ErrCode WifiP2pProxy::Hid2dIsWideBandwidthSupported(bool &isSupport)
1793 {
1794     if (mRemoteDied) {
1795         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1796         return WIFI_OPT_FAILED;
1797     }
1798     MessageOption option;
1799     MessageParcel data;
1800     MessageParcel reply;
1801     if (!data.WriteInterfaceToken(GetDescriptor())) {
1802         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1803         return WIFI_OPT_FAILED;
1804     }
1805     data.WriteInt32(0);
1806     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_WIDE_SUPPORTED),
1807         data, reply, option);
1808     if (error != ERR_NONE) {
1809         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1810             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_WIDE_SUPPORTED), error);
1811         return WIFI_OPT_FAILED;
1812     }
1813     int exception = reply.ReadInt32();
1814     if (exception) {
1815         return WIFI_OPT_FAILED;
1816     }
1817     int ret = reply.ReadInt32();
1818     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1819         return ErrCode(ret);
1820     }
1821     isSupport = (reply.ReadInt32() == 1);
1822     return WIFI_OPT_SUCCESS;
1823 }
1824 }  // namespace Wifi
1825 }  // namespace OHOS
1826