• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_logger.h"
18 #include "wifi_p2p_callback_stub.h"
19 
20 namespace OHOS {
21 namespace Wifi {
22 DEFINE_WIFILOG_P2P_LABEL("WifiP2pProxy");
23 
24 static sptr<WifiP2pCallbackStub> g_wifiP2pCallbackStub =
25     sptr<WifiP2pCallbackStub>(new (std::nothrow) WifiP2pCallbackStub());
WifiP2pProxy(const sptr<IRemoteObject> & impl)26 WifiP2pProxy::WifiP2pProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IWifiP2p>(impl), mRemoteDied(false)
27 {
28     if (impl) {
29         if ((impl->IsProxyObject()) && (!impl->AddDeathRecipient(this))) {
30             WIFI_LOGD("AddDeathRecipient!");
31         } else {
32             WIFI_LOGW("no recipient!");
33         }
34     }
35 }
36 
~WifiP2pProxy()37 WifiP2pProxy::~WifiP2pProxy()
38 {}
39 
EnableP2p(void)40 ErrCode WifiP2pProxy::EnableP2p(void)
41 {
42     if (mRemoteDied) {
43         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
44         return WIFI_OPT_FAILED;
45     }
46     MessageOption option;
47     MessageParcel data;
48     MessageParcel reply;
49     if (!data.WriteInterfaceToken(GetDescriptor())) {
50         WIFI_LOGE("Write interface token error: %{public}s", __func__);
51         return WIFI_OPT_FAILED;
52     }
53     data.WriteInt32(0);
54     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_ENABLE, data, reply, option);
55     if (error != ERR_NONE) {
56         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_ENABLE, error);
57         return WIFI_OPT_FAILED;
58     }
59     int exception = reply.ReadInt32();
60     if (exception) {
61         return WIFI_OPT_FAILED;
62     }
63     return ErrCode(reply.ReadInt32());
64 }
65 
DisableP2p(void)66 ErrCode WifiP2pProxy::DisableP2p(void)
67 {
68     if (mRemoteDied) {
69         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
70         return WIFI_OPT_FAILED;
71     }
72     MessageOption option;
73     MessageParcel data;
74     MessageParcel reply;
75     if (!data.WriteInterfaceToken(GetDescriptor())) {
76         WIFI_LOGE("Write interface token error: %{public}s", __func__);
77         return WIFI_OPT_FAILED;
78     }
79     data.WriteInt32(0);
80     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DISABLE, data, reply, option);
81     if (error != ERR_NONE) {
82         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DISABLE, error);
83         return WIFI_OPT_FAILED;
84     }
85 
86     int exception = reply.ReadInt32();
87     if (exception) {
88         return WIFI_OPT_FAILED;
89     }
90     return ErrCode(reply.ReadInt32());
91 }
92 
DiscoverDevices(void)93 ErrCode WifiP2pProxy::DiscoverDevices(void)
94 {
95     if (mRemoteDied) {
96         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
97         return WIFI_OPT_FAILED;
98     }
99     MessageOption option;
100     MessageParcel data;
101     MessageParcel reply;
102     if (!data.WriteInterfaceToken(GetDescriptor())) {
103         WIFI_LOGE("Write interface token error: %{public}s", __func__);
104         return WIFI_OPT_FAILED;
105     }
106     data.WriteInt32(0);
107     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DISCOVER_DEVICES, data, reply, option);
108     if (error != ERR_NONE) {
109         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DISCOVER_DEVICES, error);
110         return WIFI_OPT_FAILED;
111     }
112 
113     int exception = reply.ReadInt32();
114     if (exception) {
115         return WIFI_OPT_FAILED;
116     }
117     return ErrCode(reply.ReadInt32());
118 }
119 
StopDiscoverDevices(void)120 ErrCode WifiP2pProxy::StopDiscoverDevices(void)
121 {
122     if (mRemoteDied) {
123         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
124         return WIFI_OPT_FAILED;
125     }
126     MessageOption option;
127     MessageParcel data;
128     MessageParcel reply;
129     if (!data.WriteInterfaceToken(GetDescriptor())) {
130         WIFI_LOGE("Write interface token error: %{public}s", __func__);
131         return WIFI_OPT_FAILED;
132     }
133     data.WriteInt32(0);
134     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES, data, reply, option);
135     if (error != ERR_NONE) {
136         WIFI_LOGE(
137             "Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_STOP_DISCOVER_DEVICES, error);
138         return WIFI_OPT_FAILED;
139     }
140 
141     int exception = reply.ReadInt32();
142     if (exception) {
143         return WIFI_OPT_FAILED;
144     }
145     return ErrCode(reply.ReadInt32());
146 }
147 
DiscoverServices(void)148 ErrCode WifiP2pProxy::DiscoverServices(void)
149 {
150     if (mRemoteDied) {
151         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
152         return WIFI_OPT_FAILED;
153     }
154     MessageOption option;
155     MessageParcel data;
156     MessageParcel reply;
157     if (!data.WriteInterfaceToken(GetDescriptor())) {
158         WIFI_LOGE("Write interface token error: %{public}s", __func__);
159         return WIFI_OPT_FAILED;
160     }
161     data.WriteInt32(0);
162     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DISCOVER_SERVICES, data, reply, option);
163     if (error != ERR_NONE) {
164         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DISCOVER_SERVICES, error);
165         return WIFI_OPT_FAILED;
166     }
167 
168     int exception = reply.ReadInt32();
169     if (exception) {
170         return WIFI_OPT_FAILED;
171     }
172     return ErrCode(reply.ReadInt32());
173 }
174 
StopDiscoverServices(void)175 ErrCode WifiP2pProxy::StopDiscoverServices(void)
176 {
177     if (mRemoteDied) {
178         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
179         return WIFI_OPT_FAILED;
180     }
181     MessageOption option;
182     MessageParcel data;
183     MessageParcel reply;
184     if (!data.WriteInterfaceToken(GetDescriptor())) {
185         WIFI_LOGE("Write interface token error: %{public}s", __func__);
186         return WIFI_OPT_FAILED;
187     }
188     data.WriteInt32(0);
189     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES, data, reply, option);
190     if (error != ERR_NONE) {
191         WIFI_LOGE(
192             "Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_STOP_DISCOVER_SERVICES, error);
193         return WIFI_OPT_FAILED;
194     }
195 
196     int exception = reply.ReadInt32();
197     if (exception) {
198         return WIFI_OPT_FAILED;
199     }
200     return ErrCode(reply.ReadInt32());
201 }
202 
RequestService(const WifiP2pDevice & device,const WifiP2pServiceRequest & request)203 ErrCode WifiP2pProxy::RequestService(const WifiP2pDevice &device, const WifiP2pServiceRequest &request)
204 {
205     if (mRemoteDied) {
206         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
207         return WIFI_OPT_FAILED;
208     }
209     MessageOption option;
210     MessageParcel data;
211     MessageParcel reply;
212     if (!data.WriteInterfaceToken(GetDescriptor())) {
213         WIFI_LOGE("Write interface token error: %{public}s", __func__);
214         return WIFI_OPT_FAILED;
215     }
216     data.WriteInt32(0);
217     WriteWifiP2pServiceRequest(data, device, request);
218     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_REQUEST_SERVICES, data, reply, option);
219     if (error != ERR_NONE) {
220         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_REQUEST_SERVICES, error);
221         return WIFI_OPT_FAILED;
222     }
223 
224     int exception = reply.ReadInt32();
225     if (exception) {
226         return WIFI_OPT_FAILED;
227     }
228     return ErrCode(reply.ReadInt32());
229 }
230 
PutLocalP2pService(const WifiP2pServiceInfo & srvInfo)231 ErrCode WifiP2pProxy::PutLocalP2pService(const WifiP2pServiceInfo &srvInfo)
232 {
233     if (mRemoteDied) {
234         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
235         return WIFI_OPT_FAILED;
236     }
237     MessageOption option;
238     MessageParcel data;
239     MessageParcel reply;
240     if (!data.WriteInterfaceToken(GetDescriptor())) {
241         WIFI_LOGE("Write interface token error: %{public}s", __func__);
242         return WIFI_OPT_FAILED;
243     }
244     data.WriteInt32(0);
245     WriteWifiP2pServiceInfo(data, srvInfo);
246     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES, data, reply, option);
247     if (error != ERR_NONE) {
248         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_PUT_LOCAL_SERVICES, error);
249         return WIFI_OPT_FAILED;
250     }
251 
252     int exception = reply.ReadInt32();
253     if (exception) {
254         return WIFI_OPT_FAILED;
255     }
256     return ErrCode(reply.ReadInt32());
257 }
258 
DeleteLocalP2pService(const WifiP2pServiceInfo & srvInfo)259 ErrCode WifiP2pProxy::DeleteLocalP2pService(const WifiP2pServiceInfo &srvInfo)
260 {
261     if (mRemoteDied) {
262         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
263         return WIFI_OPT_FAILED;
264     }
265     MessageOption option;
266     MessageParcel data;
267     MessageParcel reply;
268     if (!data.WriteInterfaceToken(GetDescriptor())) {
269         WIFI_LOGE("Write interface token error: %{public}s", __func__);
270         return WIFI_OPT_FAILED;
271     }
272     data.WriteInt32(0);
273     WriteWifiP2pServiceInfo(data, srvInfo);
274     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES, data, reply, option);
275     if (error != ERR_NONE) {
276         WIFI_LOGE(
277             "Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DELETE_LOCAL_SERVICES, error);
278         return WIFI_OPT_FAILED;
279     }
280 
281     int exception = reply.ReadInt32();
282     if (exception) {
283         return WIFI_OPT_FAILED;
284     }
285     return ErrCode(reply.ReadInt32());
286 }
287 
StartP2pListen(int period,int interval)288 ErrCode WifiP2pProxy::StartP2pListen(int period, int interval)
289 {
290     if (mRemoteDied) {
291         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
292         return WIFI_OPT_FAILED;
293     }
294     MessageOption option;
295     MessageParcel data;
296     MessageParcel reply;
297     if (!data.WriteInterfaceToken(GetDescriptor())) {
298         WIFI_LOGE("Write interface token error: %{public}s", __func__);
299         return WIFI_OPT_FAILED;
300     }
301     data.WriteInt32(0);
302     data.WriteInt32(period);
303     data.WriteInt32(interval);
304     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_START_LISTEN, data, reply, option);
305     if (error != ERR_NONE) {
306         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_START_LISTEN, error);
307         return WIFI_OPT_FAILED;
308     }
309 
310     int exception = reply.ReadInt32();
311     if (exception) {
312         return WIFI_OPT_FAILED;
313     }
314     return ErrCode(reply.ReadInt32());
315 }
316 
StopP2pListen(void)317 ErrCode WifiP2pProxy::StopP2pListen(void)
318 {
319     if (mRemoteDied) {
320         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
321         return WIFI_OPT_FAILED;
322     }
323     MessageOption option;
324     MessageParcel data;
325     MessageParcel reply;
326     if (!data.WriteInterfaceToken(GetDescriptor())) {
327         WIFI_LOGE("Write interface token error: %{public}s", __func__);
328         return WIFI_OPT_FAILED;
329     }
330     data.WriteInt32(0);
331     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_STOP_LISTEN, data, reply, option);
332     if (error != ERR_NONE) {
333         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_STOP_LISTEN, error);
334         return WIFI_OPT_FAILED;
335     }
336 
337     int exception = reply.ReadInt32();
338     if (exception) {
339         return WIFI_OPT_FAILED;
340     }
341     return ErrCode(reply.ReadInt32());
342 }
343 
FormGroup(const WifiP2pConfig & config)344 ErrCode WifiP2pProxy::FormGroup(const WifiP2pConfig &config)
345 {
346     if (mRemoteDied) {
347         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
348         return WIFI_OPT_FAILED;
349     }
350     MessageOption option;
351     MessageParcel data;
352     MessageParcel reply;
353     if (!data.WriteInterfaceToken(GetDescriptor())) {
354         WIFI_LOGE("Write interface token error: %{public}s", __func__);
355         return WIFI_OPT_FAILED;
356     }
357     data.WriteInt32(0);
358     WriteWifiP2pConfigData(data, config);
359     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_FORM_GROUP, data, reply, option);
360     if (error != ERR_NONE) {
361         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_FORM_GROUP, error);
362         return WIFI_OPT_FAILED;
363     }
364 
365     int exception = reply.ReadInt32();
366     if (exception) {
367         return WIFI_OPT_FAILED;
368     }
369     return ErrCode(reply.ReadInt32());
370 }
371 
RemoveGroup()372 ErrCode WifiP2pProxy::RemoveGroup()
373 {
374     if (mRemoteDied) {
375         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
376         return WIFI_OPT_FAILED;
377     }
378     MessageOption option;
379     MessageParcel data;
380     MessageParcel reply;
381     if (!data.WriteInterfaceToken(GetDescriptor())) {
382         WIFI_LOGE("Write interface token error: %{public}s", __func__);
383         return WIFI_OPT_FAILED;
384     }
385     data.WriteInt32(0);
386     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_REMOVE_GROUP, data, reply, option);
387     if (error != ERR_NONE) {
388         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_REMOVE_GROUP, error);
389         return WIFI_OPT_FAILED;
390     }
391     int exception = reply.ReadInt32();
392     if (exception) {
393         return WIFI_OPT_FAILED;
394     }
395     return ErrCode(reply.ReadInt32());
396 }
397 
DeleteGroup(const WifiP2pGroupInfo & group)398 ErrCode WifiP2pProxy::DeleteGroup(const WifiP2pGroupInfo &group)
399 {
400     if (mRemoteDied) {
401         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
402         return WIFI_OPT_FAILED;
403     }
404     MessageOption option;
405     MessageParcel data;
406     MessageParcel reply;
407     if (!data.WriteInterfaceToken(GetDescriptor())) {
408         WIFI_LOGE("Write interface token error: %{public}s", __func__);
409         return WIFI_OPT_FAILED;
410     }
411     data.WriteInt32(0);
412     WriteWifiP2pGroupData(data, group);
413     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DELETE_GROUP, data, reply, option);
414     if (error != ERR_NONE) {
415         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DELETE_GROUP, error);
416         return WIFI_OPT_FAILED;
417     }
418     int exception = reply.ReadInt32();
419     if (exception) {
420         return WIFI_OPT_FAILED;
421     }
422     return ErrCode(reply.ReadInt32());
423 }
424 
ReadWifiP2pServiceInfo(MessageParcel & reply,WifiP2pServiceInfo & info) const425 void WifiP2pProxy::ReadWifiP2pServiceInfo(MessageParcel &reply, WifiP2pServiceInfo &info) const
426 {
427     const char *readStr = nullptr;
428     readStr = reply.ReadCString();
429     info.SetServiceName((readStr != nullptr) ? readStr : "");
430     readStr = reply.ReadCString();
431     info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
432     info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(reply.ReadInt32()));
433     std::vector<std::string> queryList;
434 
435     constexpr int MAX_SIZE = 512;
436     int size = reply.ReadInt32();
437     if (size > MAX_SIZE) {
438         WIFI_LOGE("P2p service size error: %{public}d", size);
439         return;
440     }
441     for (int i = 0; i < size; i++) {
442         readStr = reply.ReadCString();
443         std::string str = (readStr != nullptr) ? readStr : "";
444         queryList.push_back(str);
445     }
446     info.SetQueryList(queryList);
447     return;
448 }
449 
WriteWifiP2pServiceInfo(MessageParcel & data,const WifiP2pServiceInfo & info) const450 void WifiP2pProxy::WriteWifiP2pServiceInfo(MessageParcel &data, const WifiP2pServiceInfo &info) const
451 {
452     data.WriteCString(info.GetServiceName().c_str());
453     data.WriteCString(info.GetDeviceAddress().c_str());
454     data.WriteInt32(static_cast<int>(info.GetServicerProtocolType()));
455     std::vector<std::string> queryList = info.GetQueryList();
456     data.WriteInt32(queryList.size());
457     for (std::size_t i = 0; i < queryList.size(); i++) {
458         data.WriteCString(queryList[i].c_str());
459     }
460     return;
461 }
462 
WriteWifiP2pServiceRequest(MessageParcel & data,const WifiP2pDevice & device,const WifiP2pServiceRequest & request) const463 void WifiP2pProxy::WriteWifiP2pServiceRequest(
464     MessageParcel &data, const WifiP2pDevice &device, const WifiP2pServiceRequest &request) const
465 {
466     WriteWifiP2pDeviceData(data, device);
467     data.WriteInt32(static_cast<int>(request.GetProtocolType()));
468     data.WriteInt32(request.GetTransactionId());
469     std::vector<unsigned char> query = request.GetQuery();
470     data.WriteInt32(query.size());
471     for (std::size_t i = 0; i < query.size(); i++) {
472         data.WriteInt8(query[i]);
473     }
474     return;
475 }
476 
WriteWifiP2pDeviceData(MessageParcel & data,const WifiP2pDevice & device) const477 void WifiP2pProxy::WriteWifiP2pDeviceData(MessageParcel &data, const WifiP2pDevice &device) const
478 {
479     data.WriteCString(device.GetDeviceName().c_str());
480     data.WriteCString(device.GetDeviceAddress().c_str());
481     data.WriteCString(device.GetPrimaryDeviceType().c_str());
482     data.WriteCString(device.GetSecondaryDeviceType().c_str());
483     data.WriteInt32(static_cast<int>(device.GetP2pDeviceStatus()));
484     data.WriteBool(device.GetWfdInfo().GetWfdEnabled());
485     data.WriteInt32(device.GetWfdInfo().GetDeviceInfo());
486     data.WriteInt32(device.GetWfdInfo().GetCtrlPort());
487     data.WriteInt32(device.GetWfdInfo().GetMaxThroughput());
488     data.WriteInt32(device.GetWpsConfigMethod());
489     data.WriteInt32(device.GetDeviceCapabilitys());
490     data.WriteInt32(device.GetGroupCapabilitys());
491 }
492 
ReadWifiP2pDeviceData(MessageParcel & reply,WifiP2pDevice & device) const493 void WifiP2pProxy::ReadWifiP2pDeviceData(MessageParcel &reply, WifiP2pDevice &device) const
494 {
495     const char *readStr = nullptr;
496     readStr = reply.ReadCString();
497     device.SetDeviceName((readStr != nullptr) ? readStr : "");
498     readStr = reply.ReadCString();
499     device.SetDeviceAddress((readStr != nullptr) ? readStr : "");
500     readStr = reply.ReadCString();
501     device.SetPrimaryDeviceType((readStr != nullptr) ? readStr : "");
502     readStr = reply.ReadCString();
503     device.SetSecondaryDeviceType((readStr != nullptr) ? readStr : "");
504     device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(reply.ReadInt32()));
505     WifiP2pWfdInfo wfdInfo;
506     wfdInfo.SetWfdEnabled(reply.ReadBool());
507     wfdInfo.SetDeviceInfo(reply.ReadInt32());
508     wfdInfo.SetCtrlPort(reply.ReadInt32());
509     wfdInfo.SetMaxThroughput(reply.ReadInt32());
510     device.SetWfdInfo(wfdInfo);
511     device.SetWpsConfigMethod(reply.ReadInt32());
512     device.SetDeviceCapabilitys(reply.ReadInt32());
513     device.SetGroupCapabilitys(reply.ReadInt32());
514 }
515 
WriteWifiP2pGroupData(MessageParcel & data,const WifiP2pGroupInfo & info) const516 void WifiP2pProxy::WriteWifiP2pGroupData(MessageParcel &data, const WifiP2pGroupInfo &info) const
517 {
518     data.WriteBool(info.IsGroupOwner());
519     WriteWifiP2pDeviceData(data, info.GetOwner());
520     data.WriteCString(info.GetPassphrase().c_str());
521     data.WriteCString(info.GetInterface().c_str());
522     data.WriteCString(info.GetGroupName().c_str());
523     data.WriteInt32(info.GetFrequency());
524     data.WriteBool(info.IsPersistent());
525     data.WriteInt32(static_cast<int>(info.GetP2pGroupStatus()));
526     data.WriteInt32(info.GetNetworkId());
527     data.WriteCString(info.GetGoIpAddress().c_str());
528     std::vector<WifiP2pDevice> deviceVec;
529     deviceVec = info.GetClientDevices();
530     data.WriteInt32(deviceVec.size());
531     for (auto it = deviceVec.begin(); it != deviceVec.end(); ++it) {
532         WriteWifiP2pDeviceData(data, *it);
533     }
534 }
535 
ReadWifiP2pGroupData(MessageParcel & reply,WifiP2pGroupInfo & info) const536 void WifiP2pProxy::ReadWifiP2pGroupData(MessageParcel &reply, WifiP2pGroupInfo &info) const
537 {
538     const char *readStr = nullptr;
539     info.SetIsGroupOwner(reply.ReadBool());
540     WifiP2pDevice device;
541     ReadWifiP2pDeviceData(reply, device);
542     info.SetOwner(device);
543     readStr = reply.ReadCString();
544     info.SetPassphrase((readStr != nullptr) ? readStr : "");
545     readStr = reply.ReadCString();
546     info.SetInterface((readStr != nullptr) ? readStr : "");
547     readStr = reply.ReadCString();
548     info.SetGroupName((readStr != nullptr) ? readStr : "");
549     info.SetFrequency(reply.ReadInt32());
550     info.SetIsPersistent(reply.ReadBool());
551     info.SetP2pGroupStatus(static_cast<P2pGroupStatus>(reply.ReadInt32()));
552     info.SetNetworkId(reply.ReadInt32());
553     readStr = reply.ReadCString();
554     info.SetGoIpAddress((readStr != nullptr) ? readStr : "");
555 
556     constexpr int MAX_SIZE = 512;
557     int size = reply.ReadInt32();
558     if (size > MAX_SIZE) {
559         WIFI_LOGE("Group info device size error: %{public}d", size);
560         return;
561     }
562     for (auto it = 0; it < size; ++it) {
563         WifiP2pDevice cliDev;
564         ReadWifiP2pDeviceData(reply, cliDev);
565         info.AddClientDevice(cliDev);
566     }
567 }
568 
WriteWifiP2pConfigData(MessageParcel & data,const WifiP2pConfig & config) const569 void WifiP2pProxy::WriteWifiP2pConfigData(MessageParcel &data, const WifiP2pConfig &config) const
570 {
571     data.WriteCString(config.GetDeviceAddress().c_str());
572     data.WriteCString(config.GetPassphrase().c_str());
573     data.WriteCString(config.GetGroupName().c_str());
574     data.WriteInt32(static_cast<int>(config.GetGoBand()));
575     data.WriteInt32(config.GetNetId());
576     data.WriteInt32(config.GetGroupOwnerIntent());
577 }
578 
P2pConnect(const WifiP2pConfig & config)579 ErrCode WifiP2pProxy::P2pConnect(const WifiP2pConfig &config)
580 {
581     if (mRemoteDied) {
582         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
583         return WIFI_OPT_FAILED;
584     }
585     MessageOption option;
586     MessageParcel data;
587     MessageParcel reply;
588     if (!data.WriteInterfaceToken(GetDescriptor())) {
589         WIFI_LOGE("Write interface token error: %{public}s", __func__);
590         return WIFI_OPT_FAILED;
591     }
592     data.WriteInt32(0);
593     WriteWifiP2pConfigData(data, config);
594     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_CONNECT, data, reply, option);
595     if (error != ERR_NONE) {
596         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_CONNECT, error);
597         return WIFI_OPT_FAILED;
598     }
599 
600     int exception = reply.ReadInt32();
601     if (exception) {
602         return WIFI_OPT_FAILED;
603     }
604     return ErrCode(reply.ReadInt32());
605 }
606 
P2pDisConnect()607 ErrCode WifiP2pProxy::P2pDisConnect()
608 {
609     if (mRemoteDied) {
610         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
611         return WIFI_OPT_FAILED;
612     }
613     MessageOption option;
614     MessageParcel data;
615     MessageParcel reply;
616     if (!data.WriteInterfaceToken(GetDescriptor())) {
617         WIFI_LOGE("Write interface token error: %{public}s", __func__);
618         return WIFI_OPT_FAILED;
619     }
620     data.WriteInt32(0);
621     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_DISCONNECT, data, reply, option);
622     if (error != ERR_NONE) {
623         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_DISCONNECT, error);
624         return WIFI_OPT_FAILED;
625     }
626 
627     int exception = reply.ReadInt32();
628     if (exception) {
629         return WIFI_OPT_FAILED;
630     }
631     return ErrCode(reply.ReadInt32());
632 }
633 
QueryP2pLinkedInfo(WifiP2pLinkedInfo & linkedInfo)634 ErrCode WifiP2pProxy::QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo)
635 {
636     if (mRemoteDied) {
637         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
638         return WIFI_OPT_FAILED;
639     }
640     MessageOption option;
641     MessageParcel data;
642     MessageParcel reply;
643     if (!data.WriteInterfaceToken(GetDescriptor())) {
644         WIFI_LOGE("Write interface token error: %{public}s", __func__);
645         return WIFI_OPT_FAILED;
646     }
647     data.WriteInt32(0);
648     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_QUERY_INFO, data, reply, option);
649     if (error != ERR_NONE) {
650         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_QUERY_INFO, error);
651         return WIFI_OPT_FAILED;
652     }
653 
654     int exception = reply.ReadInt32();
655     if (exception) {
656         return WIFI_OPT_FAILED;
657     }
658     int ret = reply.ReadInt32();
659     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
660         return ErrCode(ret);
661     }
662     linkedInfo.SetConnectState(static_cast<P2pConnectedState>(reply.ReadInt32()));
663     linkedInfo.SetIsGroupOwner(reply.ReadBool());
664     const char *groupOwnerAddr = reply.ReadCString();
665     linkedInfo.SetIsGroupOwnerAddress((groupOwnerAddr != nullptr) ? groupOwnerAddr : "");
666 
667     return WIFI_OPT_SUCCESS;
668 }
669 
GetCurrentGroup(WifiP2pGroupInfo & group)670 ErrCode WifiP2pProxy::GetCurrentGroup(WifiP2pGroupInfo &group)
671 {
672     if (mRemoteDied) {
673         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
674         return WIFI_OPT_FAILED;
675     }
676     MessageOption option;
677     MessageParcel data;
678     MessageParcel reply;
679     if (!data.WriteInterfaceToken(GetDescriptor())) {
680         WIFI_LOGE("Write interface token error: %{public}s", __func__);
681         return WIFI_OPT_FAILED;
682     }
683     data.WriteInt32(0);
684     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP, data, reply, option);
685     if (error != ERR_NONE) {
686         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP, error);
687         return WIFI_OPT_FAILED;
688     }
689 
690     int exception = reply.ReadInt32();
691     if (exception) {
692         return WIFI_OPT_FAILED;
693     }
694     int ret = reply.ReadInt32();
695     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
696         return ErrCode(ret);
697     }
698     ReadWifiP2pGroupData(reply, group);
699     return WIFI_OPT_SUCCESS;
700 }
701 
GetP2pEnableStatus(int & status)702 ErrCode WifiP2pProxy::GetP2pEnableStatus(int &status)
703 {
704     if (mRemoteDied) {
705         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
706         return WIFI_OPT_FAILED;
707     }
708     MessageOption option;
709     MessageParcel data;
710     MessageParcel reply;
711     if (!data.WriteInterfaceToken(GetDescriptor())) {
712         WIFI_LOGE("Write interface token error: %{public}s", __func__);
713         return WIFI_OPT_FAILED;
714     }
715     data.WriteInt32(0);
716     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS, data, reply, option);
717     if (error != ERR_NONE) {
718         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS, error);
719         return WIFI_OPT_FAILED;
720     }
721 
722     int exception = reply.ReadInt32();
723     if (exception) {
724         return WIFI_OPT_FAILED;
725     }
726     int ret = reply.ReadInt32();
727     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
728         return ErrCode(ret);
729     }
730     status = reply.ReadInt32();
731     return WIFI_OPT_SUCCESS;
732 }
733 
GetP2pDiscoverStatus(int & status)734 ErrCode WifiP2pProxy::GetP2pDiscoverStatus(int &status)
735 {
736     if (mRemoteDied) {
737         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
738         return WIFI_OPT_FAILED;
739     }
740     MessageOption option;
741     MessageParcel data;
742     MessageParcel reply;
743     if (!data.WriteInterfaceToken(GetDescriptor())) {
744         WIFI_LOGE("Write interface token error: %{public}s", __func__);
745         return WIFI_OPT_FAILED;
746     }
747     data.WriteInt32(0);
748     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS, data, reply, option);
749     if (error != ERR_NONE) {
750         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS, error);
751         return WIFI_OPT_FAILED;
752     }
753 
754     int exception = reply.ReadInt32();
755     if (exception) {
756         return WIFI_OPT_FAILED;
757     }
758     int ret = reply.ReadInt32();
759     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
760         return ErrCode(ret);
761     }
762     status = reply.ReadInt32();
763     return WIFI_OPT_SUCCESS;
764 }
765 
GetP2pConnectedStatus(int & status)766 ErrCode WifiP2pProxy::GetP2pConnectedStatus(int &status)
767 {
768     if (mRemoteDied) {
769         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
770         return WIFI_OPT_FAILED;
771     }
772     MessageOption option;
773     MessageParcel data;
774     MessageParcel reply;
775     if (!data.WriteInterfaceToken(GetDescriptor())) {
776         WIFI_LOGE("Write interface token error: %{public}s", __func__);
777         return WIFI_OPT_FAILED;
778     }
779     data.WriteInt32(0);
780     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS, data, reply, option);
781     if (error != ERR_NONE) {
782         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS, 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     status = reply.ReadInt32();
795     return WIFI_OPT_SUCCESS;
796 }
797 
QueryP2pDevices(std::vector<WifiP2pDevice> & devices)798 ErrCode WifiP2pProxy::QueryP2pDevices(std::vector<WifiP2pDevice> &devices)
799 {
800     if (mRemoteDied) {
801         WIFI_LOGD("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(WIFI_SVR_CMD_P2P_QUERY_DEVICES, data, reply, option);
813     if (error != ERR_NONE) {
814         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_QUERY_DEVICES, error);
815         return WIFI_OPT_FAILED;
816     }
817 
818     int exception = reply.ReadInt32();
819     if (exception) {
820         return WIFI_OPT_FAILED;
821     }
822     int ret = reply.ReadInt32();
823     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
824         return ErrCode(ret);
825     }
826 
827     constexpr int MAX_SIZE = 512;
828     int size = reply.ReadInt32();
829     if (size > MAX_SIZE) {
830         WIFI_LOGE("Get p2p devices size error: %{public}d", size);
831         return WIFI_OPT_FAILED;
832     }
833     for (int i = 0; i < size; ++i) {
834         WifiP2pDevice config;
835         ReadWifiP2pDeviceData(reply, config);
836         devices.emplace_back(config);
837     }
838     return WIFI_OPT_SUCCESS;
839 }
840 
QueryP2pGroups(std::vector<WifiP2pGroupInfo> & groups)841 ErrCode WifiP2pProxy::QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups)
842 {
843     if (mRemoteDied) {
844         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
845         return WIFI_OPT_FAILED;
846     }
847     MessageOption option;
848     MessageParcel data;
849     MessageParcel reply;
850     if (!data.WriteInterfaceToken(GetDescriptor())) {
851         WIFI_LOGE("Write interface token error: %{public}s", __func__);
852         return WIFI_OPT_FAILED;
853     }
854     data.WriteInt32(0);
855     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_QUERY_GROUPS, data, reply, option);
856     if (error != ERR_NONE) {
857         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_QUERY_GROUPS, error);
858         return WIFI_OPT_FAILED;
859     }
860 
861     int exception = reply.ReadInt32();
862     if (exception) {
863         return WIFI_OPT_FAILED;
864     }
865     int ret = reply.ReadInt32();
866     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
867         return ErrCode(ret);
868     }
869 
870     constexpr int MAX_SIZE = 512;
871     int size = reply.ReadInt32();
872     if (size > MAX_SIZE) {
873         WIFI_LOGE("Get p2p group size error: %{public}d", size);
874         return WIFI_OPT_FAILED;
875     }
876     for (int i = 0; i < size; ++i) {
877         WifiP2pGroupInfo group;
878         ReadWifiP2pGroupData(reply, group);
879         groups.emplace_back(group);
880     }
881     return WIFI_OPT_SUCCESS;
882 }
883 
QueryP2pServices(std::vector<WifiP2pServiceInfo> & services)884 ErrCode WifiP2pProxy::QueryP2pServices(std::vector<WifiP2pServiceInfo> &services)
885 {
886     if (mRemoteDied) {
887         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
888         return WIFI_OPT_FAILED;
889     }
890     MessageOption option;
891     MessageParcel data;
892     MessageParcel reply;
893     if (!data.WriteInterfaceToken(GetDescriptor())) {
894         WIFI_LOGE("Write interface token error: %{public}s", __func__);
895         return WIFI_OPT_FAILED;
896     }
897     data.WriteInt32(0);
898     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_QUERY_SERVICES, data, reply, option);
899     if (error != ERR_NONE) {
900         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_QUERY_SERVICES, error);
901         return WIFI_OPT_FAILED;
902     }
903 
904     int exception = reply.ReadInt32();
905     if (exception) {
906         return WIFI_OPT_FAILED;
907     }
908     int ret = reply.ReadInt32();
909     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
910         return ErrCode(ret);
911     }
912 
913     constexpr int MAX_SIZE = 512;
914     int size = reply.ReadInt32();
915     if (size > MAX_SIZE) {
916         WIFI_LOGE("Get p2p service size error: %{public}d", size);
917         return WIFI_OPT_FAILED;
918     }
919     for (int i = 0; i < size; ++i) {
920         WifiP2pServiceInfo info;
921         ReadWifiP2pServiceInfo(reply, info);
922         services.emplace_back(info);
923     }
924     return WIFI_OPT_SUCCESS;
925 }
926 
SetP2pDeviceName(const std::string & deviceName)927 ErrCode WifiP2pProxy::SetP2pDeviceName(const std::string &deviceName)
928 {
929     if (mRemoteDied) {
930         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
931         return WIFI_OPT_FAILED;
932     }
933     MessageOption option;
934     MessageParcel data;
935     MessageParcel reply;
936     if (!data.WriteInterfaceToken(GetDescriptor())) {
937         WIFI_LOGE("Write interface token error: %{public}s", __func__);
938         return WIFI_OPT_FAILED;
939     }
940     data.WriteInt32(0);
941     data.WriteCString(deviceName.c_str());
942     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_SET_DEVICE_NAME, data, reply, option);
943     if (error != ERR_NONE) {
944         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_SET_DEVICE_NAME, error);
945         return WIFI_OPT_FAILED;
946     }
947 
948     int exception = reply.ReadInt32();
949     if (exception) {
950         return WIFI_OPT_FAILED;
951     }
952     int ret = reply.ReadInt32();
953     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
954         return ErrCode(ret);
955     }
956     return WIFI_OPT_SUCCESS;
957 }
SetP2pWfdInfo(const WifiP2pWfdInfo & wfdInfo)958 ErrCode WifiP2pProxy::SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo)
959 {
960     if (mRemoteDied) {
961         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
962         return WIFI_OPT_FAILED;
963     }
964     MessageOption option;
965     MessageParcel data;
966     MessageParcel reply;
967     if (!data.WriteInterfaceToken(GetDescriptor())) {
968         WIFI_LOGE("Write interface token error: %{public}s", __func__);
969         return WIFI_OPT_FAILED;
970     }
971     data.WriteInt32(0);
972     data.WriteBool(wfdInfo.GetWfdEnabled());
973     data.WriteInt32(wfdInfo.GetDeviceInfo());
974     data.WriteInt32(wfdInfo.GetCtrlPort());
975     data.WriteInt32(wfdInfo.GetMaxThroughput());
976     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_SET_WFD_INFO, data, reply, option);
977     if (error != ERR_NONE) {
978         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_SET_WFD_INFO, error);
979         return WIFI_OPT_FAILED;
980     }
981 
982     int exception = reply.ReadInt32();
983     if (exception) {
984         return WIFI_OPT_FAILED;
985     }
986     int ret = reply.ReadInt32();
987     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
988         return ErrCode(ret);
989     }
990     return WIFI_OPT_SUCCESS;
991 }
992 
993 
RegisterCallBack(const sptr<IWifiP2pCallback> & callback)994 ErrCode WifiP2pProxy::RegisterCallBack(const sptr<IWifiP2pCallback> &callback)
995 {
996     if (mRemoteDied) {
997         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
998         return WIFI_OPT_FAILED;
999     }
1000     MessageParcel data;
1001     MessageParcel reply;
1002     MessageOption option(MessageOption::TF_ASYNC);
1003 
1004     if (g_wifiP2pCallbackStub == nullptr) {
1005         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1006         return WIFI_OPT_FAILED;
1007     }
1008     g_wifiP2pCallbackStub->RegisterCallBack(callback);
1009     if (!data.WriteInterfaceToken(GetDescriptor())) {
1010         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1011         return WIFI_OPT_FAILED;
1012     }
1013     data.WriteInt32(0);
1014     if (!data.WriteRemoteObject(g_wifiP2pCallbackStub->AsObject())) {
1015         WIFI_LOGE("RegisterCallBack WriteRemoteObject failed!");
1016         return WIFI_OPT_FAILED;
1017     }
1018 
1019     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_REGISTER_CALLBACK, data, reply, option);
1020     if (error != ERR_NONE) {
1021         WIFI_LOGE("RegisterCallBack failed, error code is %{public}d", error);
1022         return WIFI_OPT_FAILED;
1023     }
1024     int exception = reply.ReadInt32();
1025     if (exception) {
1026         return WIFI_OPT_FAILED;
1027     }
1028     int ret = reply.ReadInt32();
1029     WIFI_LOGD("RegisterCallBack is finished: result=%{public}d", ret);
1030     return ErrCode(ret);
1031 }
1032 
GetSupportedFeatures(long & features)1033 ErrCode WifiP2pProxy::GetSupportedFeatures(long &features)
1034 {
1035     if (mRemoteDied) {
1036         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1037         return WIFI_OPT_FAILED;
1038     }
1039     MessageOption option;
1040     MessageParcel data, reply;
1041     if (!data.WriteInterfaceToken(GetDescriptor())) {
1042         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1043         return WIFI_OPT_FAILED;
1044     }
1045     data.WriteInt32(0);
1046     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, data, reply, option);
1047     if (error != ERR_NONE) {
1048         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, error);
1049         return ErrCode(error);
1050     }
1051     int exception = reply.ReadInt32();
1052     if (exception) {
1053         return WIFI_OPT_FAILED;
1054     }
1055     int ret = reply.ReadInt32();
1056     if (ret != WIFI_OPT_SUCCESS) {
1057         return ErrCode(ret);
1058     }
1059 
1060     features = reply.ReadInt64();
1061     return WIFI_OPT_SUCCESS;
1062 }
1063 
Hid2dRequestGcIp(const std::string & gcMac,std::string & ipAddr)1064 ErrCode WifiP2pProxy::Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr)
1065 {
1066     if (mRemoteDied) {
1067         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1068         return WIFI_OPT_FAILED;
1069     }
1070     MessageOption option;
1071     MessageParcel data;
1072     MessageParcel reply;
1073     if (!data.WriteInterfaceToken(GetDescriptor())) {
1074         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1075         return WIFI_OPT_FAILED;
1076     }
1077     data.WriteInt32(0);
1078     data.WriteCString(gcMac.c_str());
1079     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_APPLY_IP, data, reply, option);
1080     if (error != ERR_NONE) {
1081         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_P2P_HID2D_APPLY_IP, error);
1082         return WIFI_OPT_FAILED;
1083     }
1084     int exception = reply.ReadInt32();
1085     if (exception) {
1086         return WIFI_OPT_FAILED;
1087     }
1088     int ret = reply.ReadInt32();
1089     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1090         return ErrCode(ret);
1091     }
1092     const char *ipAddress = reply.ReadCString();
1093     ipAddr = (ipAddress != nullptr) ? ipAddress : "";
1094     return WIFI_OPT_SUCCESS;
1095 }
1096 
Hid2dSharedlinkIncrease()1097 ErrCode WifiP2pProxy::Hid2dSharedlinkIncrease()
1098 {
1099     if (mRemoteDied) {
1100         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1101         return WIFI_OPT_FAILED;
1102     }
1103     MessageOption option;
1104     MessageParcel data;
1105     MessageParcel reply;
1106     if (!data.WriteInterfaceToken(GetDescriptor())) {
1107         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1108         return WIFI_OPT_FAILED;
1109     }
1110     data.WriteInt32(0);
1111     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE, data, reply, option);
1112     if (error != ERR_NONE) {
1113         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1114             WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE, error);
1115         return WIFI_OPT_FAILED;
1116     }
1117     int exception = reply.ReadInt32();
1118     if (exception) {
1119         return WIFI_OPT_FAILED;
1120     }
1121     return ErrCode(reply.ReadInt32());
1122 }
1123 
Hid2dSharedlinkDecrease()1124 ErrCode WifiP2pProxy::Hid2dSharedlinkDecrease()
1125 {
1126     if (mRemoteDied) {
1127         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1128         return WIFI_OPT_FAILED;
1129     }
1130     MessageOption option;
1131     MessageParcel data;
1132     MessageParcel reply;
1133     if (!data.WriteInterfaceToken(GetDescriptor())) {
1134         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1135         return WIFI_OPT_FAILED;
1136     }
1137     data.WriteInt32(0);
1138     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE, data, reply, option);
1139     if (error != ERR_NONE) {
1140         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1141             WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE, error);
1142         return WIFI_OPT_FAILED;
1143     }
1144     int exception = reply.ReadInt32();
1145     if (exception) {
1146         return WIFI_OPT_FAILED;
1147     }
1148     return ErrCode(reply.ReadInt32());
1149 }
1150 
Hid2dCreateGroup(const int frequency,FreqType type)1151 ErrCode WifiP2pProxy::Hid2dCreateGroup(const int frequency, FreqType type)
1152 {
1153     WIFI_LOGI("Request hid2d create group");
1154 
1155     if (mRemoteDied) {
1156         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1157         return WIFI_OPT_FAILED;
1158     }
1159     MessageOption option;
1160     MessageParcel data;
1161     MessageParcel reply;
1162     if (!data.WriteInterfaceToken(GetDescriptor())) {
1163         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1164         return WIFI_OPT_FAILED;
1165     }
1166     data.WriteInt32(0);
1167     data.WriteInt32(frequency);
1168     data.WriteInt32(static_cast<int>(type));
1169     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP, data, reply, option);
1170     if (error != ERR_NONE) {
1171         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1172             WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP, error);
1173         return WIFI_OPT_FAILED;
1174     }
1175     int exception = reply.ReadInt32();
1176     if (exception) {
1177         return WIFI_OPT_FAILED;
1178     }
1179     return ErrCode(reply.ReadInt32());
1180 }
1181 
Hid2dRemoveGcGroup(const std::string & gcIfName)1182 ErrCode WifiP2pProxy::Hid2dRemoveGcGroup(const std::string& gcIfName)
1183 {
1184     WIFI_LOGI("Request hid2d remove group");
1185 
1186     if (mRemoteDied) {
1187         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1188         return WIFI_OPT_FAILED;
1189     }
1190     MessageOption option;
1191     MessageParcel data;
1192     MessageParcel reply;
1193     if (!data.WriteInterfaceToken(GetDescriptor())) {
1194         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1195         return WIFI_OPT_FAILED;
1196     }
1197     data.WriteInt32(0);
1198     data.WriteCString(gcIfName.c_str());
1199     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP, data, reply, option);
1200     if (error != ERR_NONE) {
1201         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1202             WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP, error);
1203         return WIFI_OPT_FAILED;
1204     }
1205     int exception = reply.ReadInt32();
1206     if (exception) {
1207         return WIFI_OPT_FAILED;
1208     }
1209     return ErrCode(reply.ReadInt32());
1210 }
1211 
Hid2dConnect(const Hid2dConnectConfig & config)1212 ErrCode WifiP2pProxy::Hid2dConnect(const Hid2dConnectConfig& config)
1213 {
1214     WIFI_LOGI("Request hid2d connect");
1215 
1216     if (mRemoteDied) {
1217         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1218         return WIFI_OPT_FAILED;
1219     }
1220     MessageOption option;
1221     MessageParcel data;
1222     MessageParcel reply;
1223     if (!data.WriteInterfaceToken(GetDescriptor())) {
1224         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1225         return WIFI_OPT_FAILED;
1226     }
1227     data.WriteInt32(0);
1228     data.WriteCString(config.GetSsid().c_str());
1229     data.WriteCString(config.GetBssid().c_str());
1230     data.WriteCString(config.GetPreSharedKey().c_str());
1231     data.WriteInt32(config.GetFrequency());
1232     data.WriteInt32(static_cast<int>(config.GetDhcpMode()));
1233     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_CONNECT, data, reply, option);
1234     if (error != ERR_NONE) {
1235         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1236             WIFI_SVR_CMD_P2P_HID2D_CONNECT, error);
1237         return WIFI_OPT_FAILED;
1238     }
1239     int exception = reply.ReadInt32();
1240     if (exception) {
1241         return WIFI_OPT_FAILED;
1242     }
1243     return ErrCode(reply.ReadInt32());
1244 }
1245 
Hid2dConfigIPAddr(const std::string & ifName,const IpAddrInfo & ipInfo)1246 ErrCode WifiP2pProxy::Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo)
1247 {
1248     if (mRemoteDied) {
1249         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1250         return WIFI_OPT_FAILED;
1251     }
1252     MessageOption option;
1253     MessageParcel data;
1254     MessageParcel reply;
1255     if (!data.WriteInterfaceToken(GetDescriptor())) {
1256         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1257         return WIFI_OPT_FAILED;
1258     }
1259     data.WriteInt32(0);
1260     data.WriteCString(ifName.c_str());
1261     data.WriteCString(ipInfo.ip.c_str());
1262     data.WriteCString(ipInfo.gateway.c_str());
1263     data.WriteCString(ipInfo.netmask.c_str());
1264     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP, data, reply, option);
1265     if (error != ERR_NONE) {
1266         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1267             WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP, error);
1268         return WIFI_OPT_FAILED;
1269     }
1270     int exception = reply.ReadInt32();
1271     if (exception) {
1272         return WIFI_OPT_FAILED;
1273     }
1274     return ErrCode(reply.ReadInt32());
1275 }
1276 
Hid2dReleaseIPAddr(const std::string & ifName)1277 ErrCode WifiP2pProxy::Hid2dReleaseIPAddr(const std::string& ifName)
1278 {
1279     if (mRemoteDied) {
1280         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1281         return WIFI_OPT_FAILED;
1282     }
1283     MessageOption option;
1284     MessageParcel data;
1285     MessageParcel reply;
1286     if (!data.WriteInterfaceToken(GetDescriptor())) {
1287         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1288         return WIFI_OPT_FAILED;
1289     }
1290     data.WriteInt32(0);
1291     data.WriteCString(ifName.c_str());
1292     int error = Remote()->SendRequest(WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP, data, reply, option);
1293     if (error != ERR_NONE) {
1294         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1295             WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP, error);
1296         return WIFI_OPT_FAILED;
1297     }
1298     int exception = reply.ReadInt32();
1299     if (exception) {
1300         return WIFI_OPT_FAILED;
1301     }
1302     return ErrCode(reply.ReadInt32());
1303 }
1304 
Hid2dGetRecommendChannel(const RecommendChannelRequest & request,RecommendChannelResponse & response)1305 ErrCode WifiP2pProxy::Hid2dGetRecommendChannel(const RecommendChannelRequest& request,
1306     RecommendChannelResponse& response)
1307 {
1308     if (mRemoteDied) {
1309         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1310         return WIFI_OPT_FAILED;
1311     }
1312     MessageOption option;
1313     MessageParcel data, reply;
1314     if (!data.WriteInterfaceToken(GetDescriptor())) {
1315         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1316         return WIFI_OPT_FAILED;
1317     }
1318     data.WriteInt32(0);
1319     data.WriteCString(request.remoteIfName.c_str());
1320     data.WriteInt32(request.remoteIfMode);
1321     data.WriteCString(request.localIfName.c_str());
1322     data.WriteInt32(request.localIfMode);
1323     data.WriteInt32(request.prefBand);
1324     data.WriteInt32(static_cast<int>(request.prefBandwidth));
1325     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL, data, reply, option);
1326     if (error != ERR_NONE) {
1327         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1328             WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL, error);
1329         return ErrCode(error);
1330     }
1331     int exception = reply.ReadInt32();
1332     if (exception) {
1333         return WIFI_OPT_FAILED;
1334     }
1335     int ret = reply.ReadInt32();
1336     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1337         return ErrCode(ret);
1338     }
1339     response.status = RecommendStatus(reply.ReadInt32());
1340     response.index = reply.ReadInt32();
1341     response.centerFreq = reply.ReadInt32();
1342     response.centerFreq1 = reply.ReadInt32();
1343     response.centerFreq2 = reply.ReadInt32();
1344     response.bandwidth = reply.ReadInt32();
1345     return WIFI_OPT_SUCCESS;
1346 }
1347 
Hid2dGetChannelListFor5G(std::vector<int> & vecChannelList)1348 ErrCode WifiP2pProxy::Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList)
1349 {
1350     if (mRemoteDied) {
1351         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1352         return WIFI_OPT_FAILED;
1353     }
1354     MessageOption option;
1355     MessageParcel data;
1356     MessageParcel reply;
1357     if (!data.WriteInterfaceToken(GetDescriptor())) {
1358         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1359         return WIFI_OPT_FAILED;
1360     }
1361     data.WriteInt32(0);
1362     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_5G_CHANNEL_LIST, data, reply, option);
1363     if (error != ERR_NONE) {
1364         WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_GET_5G_CHANNEL_LIST);
1365         return WIFI_OPT_FAILED;
1366     }
1367     int exception = reply.ReadInt32();
1368     if (exception) {
1369         return WIFI_OPT_FAILED;
1370     }
1371     int ret = reply.ReadInt32();
1372     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1373         return ErrCode(ret);
1374     }
1375 
1376     constexpr int MAX_SIZE = 512;
1377     int listSize = reply.ReadInt32();
1378     if (listSize > MAX_SIZE) {
1379         WIFI_LOGE("Get channel list for 5G size error: %{public}d", listSize);
1380         return WIFI_OPT_FAILED;
1381     }
1382     for (int i = 0; i < listSize; ++i) {
1383         vecChannelList.emplace_back(reply.ReadInt32());
1384     }
1385     return WIFI_OPT_SUCCESS;
1386 }
1387 
Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int * getDatValidLen)1388 ErrCode WifiP2pProxy::Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,
1389     char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen)
1390 {
1391     if (mRemoteDied) {
1392         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1393         return WIFI_OPT_FAILED;
1394     }
1395     MessageOption option;
1396     MessageParcel data;
1397     MessageParcel reply;
1398     if (!data.WriteInterfaceToken(GetDescriptor())) {
1399         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1400         return WIFI_OPT_FAILED;
1401     }
1402     data.WriteInt32(0);
1403     data.WriteInt32(static_cast<int>(cfgType));
1404     int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SELF_WIFI_CFG, data, reply, option);
1405     if (error != ERR_NONE) {
1406         WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_GET_SELF_WIFI_CFG);
1407         return WIFI_OPT_FAILED;
1408     }
1409     int exception = reply.ReadInt32();
1410     if (exception) {
1411         return WIFI_OPT_FAILED;
1412     }
1413     int ret = reply.ReadInt32();
1414     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1415         return WIFI_OPT_FAILED;
1416     }
1417 
1418     *getDatValidLen = reply.ReadInt32();
1419     if (*getDatValidLen > 0) {
1420         const char *dataBuffer = (const char *)reply.ReadBuffer(*getDatValidLen);
1421         if (dataBuffer == nullptr) {
1422             WIFI_LOGE("`%{public}s` inner communication error!", __func__);
1423             return WIFI_OPT_FAILED;
1424         }
1425         if (memcpy_s(cfgData, CFG_DATA_MAX_BYTES, dataBuffer, *getDatValidLen) != EOK) {
1426             WIFI_LOGD("`%{public}s` memcpy_s failed!", __func__);
1427             return WIFI_OPT_FAILED;
1428         }
1429     }
1430     return WIFI_OPT_SUCCESS;
1431 }
1432 
Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int setDataValidLen)1433 ErrCode WifiP2pProxy::Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,
1434     char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen)
1435 {
1436     if (setDataValidLen <= 0) {
1437         WIFI_LOGE("`%{public}s` parameter is error!", __func__);
1438         return WIFI_OPT_INVALID_PARAM;
1439     }
1440     if (mRemoteDied) {
1441         WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
1442         return WIFI_OPT_FAILED;
1443     }
1444     MessageOption option;
1445     MessageParcel data;
1446     MessageParcel reply;
1447     if (!data.WriteInterfaceToken(GetDescriptor())) {
1448         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1449         return WIFI_OPT_FAILED;
1450     }
1451     data.WriteInt32(0);
1452     data.WriteInt32(static_cast<int>(cfgType));
1453     data.WriteInt32(setDataValidLen);
1454     data.WriteBuffer(cfgData, setDataValidLen);
1455     int error = Remote()->SendRequest(WIFI_SVR_CMD_SET_PEER_WIFI_CFG, data, reply, option);
1456     if (error != ERR_NONE) {
1457         WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_SET_PEER_WIFI_CFG);
1458         return WIFI_OPT_FAILED;
1459     }
1460     int exception = reply.ReadInt32();
1461     if (exception) {
1462         return WIFI_OPT_FAILED;
1463     }
1464     return ErrCode(reply.ReadInt32());
1465 }
1466 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)1467 void WifiP2pProxy::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
1468 {
1469     WIFI_LOGD("Remote service is died!");
1470     mRemoteDied = true;
1471     if (g_wifiP2pCallbackStub == nullptr) {
1472         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1473         return;
1474     }
1475     g_wifiP2pCallbackStub->SetRemoteDied(true);
1476 }
1477 }  // namespace Wifi
1478 }  // namespace OHOS