• 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_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 
DeleteGroup(const WifiP2pGroupInfo & group)454 ErrCode WifiP2pProxy::DeleteGroup(const WifiP2pGroupInfo &group)
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     WriteWifiP2pGroupData(data, group);
469     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP), data,
470         reply, option);
471     if (error != ERR_NONE) {
472         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
473             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_DELETE_GROUP), error);
474         return WIFI_OPT_FAILED;
475     }
476     int exception = reply.ReadInt32();
477     if (exception) {
478         return WIFI_OPT_FAILED;
479     }
480     return ErrCode(reply.ReadInt32());
481 }
482 
ReadWifiP2pServiceInfo(MessageParcel & reply,WifiP2pServiceInfo & info) const483 void WifiP2pProxy::ReadWifiP2pServiceInfo(MessageParcel &reply, WifiP2pServiceInfo &info) const
484 {
485     const char *readStr = nullptr;
486     readStr = reply.ReadCString();
487     info.SetServiceName((readStr != nullptr) ? readStr : "");
488     readStr = reply.ReadCString();
489     info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
490     info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(reply.ReadInt32()));
491     std::vector<std::string> queryList;
492 
493     constexpr int MAX_SIZE = 512;
494     int size = reply.ReadInt32();
495     if (size > MAX_SIZE) {
496         WIFI_LOGE("P2p service size error: %{public}d", size);
497         return;
498     }
499     for (int i = 0; i < size; i++) {
500         readStr = reply.ReadCString();
501         std::string str = (readStr != nullptr) ? readStr : "";
502         queryList.push_back(str);
503     }
504     info.SetQueryList(queryList);
505     return;
506 }
507 
WriteWifiP2pServiceInfo(MessageParcel & data,const WifiP2pServiceInfo & info) const508 void WifiP2pProxy::WriteWifiP2pServiceInfo(MessageParcel &data, const WifiP2pServiceInfo &info) const
509 {
510     data.WriteCString(info.GetServiceName().c_str());
511     data.WriteCString(info.GetDeviceAddress().c_str());
512     data.WriteInt32(static_cast<int>(info.GetServicerProtocolType()));
513     std::vector<std::string> queryList = info.GetQueryList();
514     data.WriteInt32(queryList.size());
515     for (std::size_t i = 0; i < queryList.size(); i++) {
516         data.WriteCString(queryList[i].c_str());
517     }
518     return;
519 }
520 
WriteWifiP2pServiceRequest(MessageParcel & data,const WifiP2pDevice & device,const WifiP2pServiceRequest & request) const521 void WifiP2pProxy::WriteWifiP2pServiceRequest(
522     MessageParcel &data, const WifiP2pDevice &device, const WifiP2pServiceRequest &request) const
523 {
524     WriteWifiP2pDeviceData(data, device);
525     data.WriteInt32(static_cast<int>(request.GetProtocolType()));
526     data.WriteInt32(request.GetTransactionId());
527     std::vector<unsigned char> query = request.GetQuery();
528     data.WriteInt32(query.size());
529     for (std::size_t i = 0; i < query.size(); i++) {
530         data.WriteInt8(query[i]);
531     }
532     return;
533 }
534 
WriteWifiP2pDeviceData(MessageParcel & data,const WifiP2pDevice & device) const535 void WifiP2pProxy::WriteWifiP2pDeviceData(MessageParcel &data, const WifiP2pDevice &device) const
536 {
537     data.WriteString(device.GetDeviceName());
538     data.WriteString(device.GetDeviceAddress());
539     data.WriteInt32(device.GetDeviceAddressType());
540     data.WriteString(device.GetPrimaryDeviceType());
541     data.WriteString(device.GetSecondaryDeviceType());
542     data.WriteInt32(static_cast<int>(device.GetP2pDeviceStatus()));
543     data.WriteBool(device.GetWfdInfo().GetWfdEnabled());
544     data.WriteInt32(device.GetWfdInfo().GetDeviceInfo());
545     data.WriteInt32(device.GetWfdInfo().GetCtrlPort());
546     data.WriteInt32(device.GetWfdInfo().GetMaxThroughput());
547     data.WriteInt32(device.GetWpsConfigMethod());
548     data.WriteInt32(device.GetDeviceCapabilitys());
549     data.WriteInt32(device.GetGroupCapabilitys());
550 }
551 
ReadWifiP2pDeviceData(MessageParcel & reply,WifiP2pDevice & device) const552 void WifiP2pProxy::ReadWifiP2pDeviceData(MessageParcel &reply, WifiP2pDevice &device) const
553 {
554     device.SetDeviceName(reply.ReadString());
555     device.SetDeviceAddress(reply.ReadString());
556     device.SetDeviceAddressType(reply.ReadInt32());
557     device.SetPrimaryDeviceType(reply.ReadString());
558     device.SetSecondaryDeviceType(reply.ReadString());
559     device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(reply.ReadInt32()));
560     WifiP2pWfdInfo wfdInfo;
561     wfdInfo.SetWfdEnabled(reply.ReadBool());
562     wfdInfo.SetDeviceInfo(reply.ReadInt32());
563     wfdInfo.SetCtrlPort(reply.ReadInt32());
564     wfdInfo.SetMaxThroughput(reply.ReadInt32());
565     device.SetWfdInfo(wfdInfo);
566     device.SetWpsConfigMethod(reply.ReadInt32());
567     device.SetDeviceCapabilitys(reply.ReadInt32());
568     device.SetGroupCapabilitys(reply.ReadInt32());
569 }
570 
WriteWifiP2pGroupData(MessageParcel & data,const WifiP2pGroupInfo & info) const571 void WifiP2pProxy::WriteWifiP2pGroupData(MessageParcel &data, const WifiP2pGroupInfo &info) const
572 {
573     data.WriteBool(info.IsGroupOwner());
574     WriteWifiP2pDeviceData(data, info.GetOwner());
575     data.WriteString(info.GetPassphrase());
576     data.WriteString(info.GetInterface());
577     data.WriteString(info.GetGroupName());
578     data.WriteInt32(info.GetFrequency());
579     data.WriteBool(info.IsPersistent());
580     data.WriteInt32(static_cast<int>(info.GetP2pGroupStatus()));
581     data.WriteInt32(info.GetNetworkId());
582     data.WriteString(info.GetGoIpAddress());
583     std::vector<WifiP2pDevice> deviceVec;
584     deviceVec = info.GetClientDevices();
585     data.WriteInt32(deviceVec.size());
586     for (auto it = deviceVec.begin(); it != deviceVec.end(); ++it) {
587         WriteWifiP2pDeviceData(data, *it);
588     }
589 }
590 
ReadWifiP2pGroupData(MessageParcel & reply,WifiP2pGroupInfo & info) const591 void WifiP2pProxy::ReadWifiP2pGroupData(MessageParcel &reply, WifiP2pGroupInfo &info) const
592 {
593     info.SetIsGroupOwner(reply.ReadBool());
594     WifiP2pDevice device;
595     ReadWifiP2pDeviceData(reply, device);
596     info.SetOwner(device);
597     info.SetPassphrase(reply.ReadString());
598     info.SetInterface(reply.ReadString());
599     info.SetGroupName(reply.ReadString());
600     info.SetFrequency(reply.ReadInt32());
601     info.SetIsPersistent(reply.ReadBool());
602     info.SetP2pGroupStatus(static_cast<P2pGroupStatus>(reply.ReadInt32()));
603     info.SetNetworkId(reply.ReadInt32());
604     info.SetGoIpAddress(reply.ReadString());
605 
606     constexpr int MAX_SIZE = 512;
607     int size = reply.ReadInt32();
608     if (size > MAX_SIZE) {
609         WIFI_LOGE("Group info device size error: %{public}d", size);
610         return;
611     }
612     for (auto it = 0; it < size; ++it) {
613         WifiP2pDevice cliDev;
614         ReadWifiP2pDeviceData(reply, cliDev);
615         info.AddClientDevice(cliDev);
616     }
617 }
618 
WriteWifiP2pConfigData(MessageParcel & data,const WifiP2pConfig & config) const619 void WifiP2pProxy::WriteWifiP2pConfigData(MessageParcel &data, const WifiP2pConfig &config) const
620 {
621     data.WriteString(config.GetDeviceAddress());
622     data.WriteInt32(config.GetDeviceAddressType());
623     data.WriteString(config.GetPassphrase());
624     data.WriteString(config.GetGroupName());
625     data.WriteInt32(static_cast<int>(config.GetGoBand()));
626     data.WriteInt32(config.GetNetId());
627     data.WriteInt32(config.GetGroupOwnerIntent());
628 }
629 
P2pConnect(const WifiP2pConfig & config)630 ErrCode WifiP2pProxy::P2pConnect(const WifiP2pConfig &config)
631 {
632     if (mRemoteDied) {
633         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
634         return WIFI_OPT_FAILED;
635     }
636     MessageOption option;
637     MessageParcel data;
638     MessageParcel reply;
639     if (!data.WriteInterfaceToken(GetDescriptor())) {
640         WIFI_LOGE("Write interface token error: %{public}s", __func__);
641         return WIFI_OPT_FAILED;
642     }
643     data.WriteInt32(0);
644     WriteWifiP2pConfigData(data, config);
645     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT), data, reply,
646         option);
647     if (error != ERR_NONE) {
648         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
649             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CONNECT), error);
650         return WIFI_OPT_FAILED;
651     }
652 
653     int exception = reply.ReadInt32();
654     if (exception) {
655         return WIFI_OPT_FAILED;
656     }
657     return ErrCode(reply.ReadInt32());
658 }
659 
P2pCancelConnect()660 ErrCode WifiP2pProxy::P2pCancelConnect()
661 {
662     if (mRemoteDied) {
663         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
664         return WIFI_OPT_FAILED;
665     }
666     MessageOption option;
667     MessageParcel data;
668     MessageParcel reply;
669     if (!data.WriteInterfaceToken(GetDescriptor())) {
670         WIFI_LOGE("Write interface token error: %{public}s", __func__);
671         return WIFI_OPT_FAILED;
672     }
673     data.WriteInt32(0);
674     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT), data,
675         reply, option);
676     if (error != ERR_NONE) {
677         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
678             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_CANCEL_CONNECT), error);
679         return WIFI_OPT_FAILED;
680     }
681 
682     int exception = reply.ReadInt32();
683     if (exception) {
684         return WIFI_OPT_FAILED;
685     }
686     return ErrCode(reply.ReadInt32());
687 }
688 
QueryP2pLinkedInfo(WifiP2pLinkedInfo & linkedInfo)689 ErrCode WifiP2pProxy::QueryP2pLinkedInfo(WifiP2pLinkedInfo &linkedInfo)
690 {
691     if (mRemoteDied) {
692         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
693         return WIFI_OPT_FAILED;
694     }
695     MessageOption option;
696     MessageParcel data;
697     MessageParcel reply;
698     if (!data.WriteInterfaceToken(GetDescriptor())) {
699         WIFI_LOGE("Write interface token error: %{public}s", __func__);
700         return WIFI_OPT_FAILED;
701     }
702     data.WriteInt32(0);
703     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO), data, reply,
704         option);
705     if (error != ERR_NONE) {
706         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
707             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_INFO), error);
708         return WIFI_OPT_FAILED;
709     }
710 
711     int exception = reply.ReadInt32();
712     if (exception) {
713         return WIFI_OPT_FAILED;
714     }
715     int ret = reply.ReadInt32();
716     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
717         return ErrCode(ret);
718     }
719     linkedInfo.SetConnectState(static_cast<P2pConnectedState>(reply.ReadInt32()));
720     linkedInfo.SetIsGroupOwner(reply.ReadBool());
721     std::string groupOwnerAddr = reply.ReadString();
722     linkedInfo.SetIsGroupOwnerAddress(groupOwnerAddr);
723 
724     return WIFI_OPT_SUCCESS;
725 }
726 
GetCurrentGroup(WifiP2pGroupInfo & group)727 ErrCode WifiP2pProxy::GetCurrentGroup(WifiP2pGroupInfo &group)
728 {
729     if (mRemoteDied) {
730         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
731         return WIFI_OPT_FAILED;
732     }
733     MessageOption option;
734     MessageParcel data;
735     MessageParcel reply;
736     if (!data.WriteInterfaceToken(GetDescriptor())) {
737         WIFI_LOGE("Write interface token error: %{public}s", __func__);
738         return WIFI_OPT_FAILED;
739     }
740     data.WriteInt32(0);
741     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP), data,
742         reply, option);
743     if (error != ERR_NONE) {
744         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
745             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CURRENT_GROUP), error);
746         return WIFI_OPT_FAILED;
747     }
748 
749     int exception = reply.ReadInt32();
750     if (exception) {
751         return WIFI_OPT_FAILED;
752     }
753     int ret = reply.ReadInt32();
754     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
755         return ErrCode(ret);
756     }
757     ReadWifiP2pGroupData(reply, group);
758     return WIFI_OPT_SUCCESS;
759 }
760 
GetP2pEnableStatus(int & status)761 ErrCode WifiP2pProxy::GetP2pEnableStatus(int &status)
762 {
763     if (mRemoteDied) {
764         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
765         return WIFI_OPT_FAILED;
766     }
767     MessageOption option;
768     MessageParcel data;
769     MessageParcel reply;
770     if (!data.WriteInterfaceToken(GetDescriptor())) {
771         WIFI_LOGE("Write interface token error: %{public}s", __func__);
772         return WIFI_OPT_FAILED;
773     }
774     data.WriteInt32(0);
775     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS), data,
776         reply, option);
777     if (error != ERR_NONE) {
778         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
779             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_ENABLE_STATUS), error);
780         return WIFI_OPT_FAILED;
781     }
782 
783     int exception = reply.ReadInt32();
784     if (exception) {
785         return WIFI_OPT_FAILED;
786     }
787     int ret = reply.ReadInt32();
788     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
789         return ErrCode(ret);
790     }
791     status = reply.ReadInt32();
792     return WIFI_OPT_SUCCESS;
793 }
794 
GetP2pDiscoverStatus(int & status)795 ErrCode WifiP2pProxy::GetP2pDiscoverStatus(int &status)
796 {
797     if (mRemoteDied) {
798         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
799         return WIFI_OPT_FAILED;
800     }
801     MessageOption option;
802     MessageParcel data;
803     MessageParcel reply;
804     if (!data.WriteInterfaceToken(GetDescriptor())) {
805         WIFI_LOGE("Write interface token error: %{public}s", __func__);
806         return WIFI_OPT_FAILED;
807     }
808     data.WriteInt32(0);
809     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS),
810         data, reply, option);
811     if (error != ERR_NONE) {
812         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
813             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_DISCOVER_STATUS), error);
814         return WIFI_OPT_FAILED;
815     }
816 
817     int exception = reply.ReadInt32();
818     if (exception) {
819         return WIFI_OPT_FAILED;
820     }
821     int ret = reply.ReadInt32();
822     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
823         return ErrCode(ret);
824     }
825     status = reply.ReadInt32();
826     return WIFI_OPT_SUCCESS;
827 }
828 
GetP2pConnectedStatus(int & status)829 ErrCode WifiP2pProxy::GetP2pConnectedStatus(int &status)
830 {
831     if (mRemoteDied) {
832         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
833         return WIFI_OPT_FAILED;
834     }
835     MessageOption option;
836     MessageParcel data;
837     MessageParcel reply;
838     if (!data.WriteInterfaceToken(GetDescriptor())) {
839         WIFI_LOGE("Write interface token error: %{public}s", __func__);
840         return WIFI_OPT_FAILED;
841     }
842     data.WriteInt32(0);
843     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS),
844         data, reply, option);
845     if (error != ERR_NONE) {
846         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
847             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_GET_CONNECTED_STATUS), error);
848         return WIFI_OPT_FAILED;
849     }
850 
851     int exception = reply.ReadInt32();
852     if (exception) {
853         return WIFI_OPT_FAILED;
854     }
855     int ret = reply.ReadInt32();
856     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
857         return ErrCode(ret);
858     }
859     status = reply.ReadInt32();
860     return WIFI_OPT_SUCCESS;
861 }
862 
QueryP2pDevices(std::vector<WifiP2pDevice> & devices)863 ErrCode WifiP2pProxy::QueryP2pDevices(std::vector<WifiP2pDevice> &devices)
864 {
865     if (mRemoteDied) {
866         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
867         return WIFI_OPT_FAILED;
868     }
869     MessageOption option;
870     MessageParcel data;
871     MessageParcel reply;
872     if (!data.WriteInterfaceToken(GetDescriptor())) {
873         WIFI_LOGE("Write interface token error: %{public}s", __func__);
874         return WIFI_OPT_FAILED;
875     }
876     data.WriteInt32(0);
877     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES), data,
878         reply, option);
879     if (error != ERR_NONE) {
880         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
881             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_DEVICES), error);
882         return WIFI_OPT_FAILED;
883     }
884 
885     int exception = reply.ReadInt32();
886     if (exception) {
887         return WIFI_OPT_FAILED;
888     }
889     int ret = reply.ReadInt32();
890     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
891         return ErrCode(ret);
892     }
893 
894     constexpr int MAX_SIZE = 512;
895     int size = reply.ReadInt32();
896     if (size > MAX_SIZE) {
897         WIFI_LOGE("Get p2p devices size error: %{public}d", size);
898         return WIFI_OPT_FAILED;
899     }
900     for (int i = 0; i < size; ++i) {
901         WifiP2pDevice config;
902         ReadWifiP2pDeviceData(reply, config);
903         devices.emplace_back(config);
904     }
905     return WIFI_OPT_SUCCESS;
906 }
907 
QueryP2pLocalDevice(WifiP2pDevice & device)908 ErrCode WifiP2pProxy::QueryP2pLocalDevice(WifiP2pDevice &device)
909 {
910     if (mRemoteDied) {
911         WIFI_LOGE("failed to `%{public}s`,remote service is died!", __func__);
912         return WIFI_OPT_FAILED;
913     }
914     MessageOption option;
915     MessageParcel data;
916     MessageParcel reply;
917     if (!data.WriteInterfaceToken(GetDescriptor())) {
918         WIFI_LOGE("Write interface token error: %{public}s", __func__);
919         return WIFI_OPT_FAILED;
920     }
921     data.WriteInt32(0);
922     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE),
923         data, reply, option);
924     if (error != ERR_NONE) {
925         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
926             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_LOCAL_DEVICE), error);
927         return WIFI_OPT_FAILED;
928     }
929 
930     int exception = reply.ReadInt32();
931     if (exception) {
932         return WIFI_OPT_FAILED;
933     }
934     int ret = reply.ReadInt32();
935     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
936         return ErrCode(ret);
937     }
938     ReadWifiP2pDeviceData(reply, device);
939     return WIFI_OPT_SUCCESS;
940 }
941 
QueryP2pGroups(std::vector<WifiP2pGroupInfo> & groups)942 ErrCode WifiP2pProxy::QueryP2pGroups(std::vector<WifiP2pGroupInfo> &groups)
943 {
944     if (mRemoteDied) {
945         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
946         return WIFI_OPT_FAILED;
947     }
948     MessageOption option;
949     MessageParcel data;
950     MessageParcel reply;
951     if (!data.WriteInterfaceToken(GetDescriptor())) {
952         WIFI_LOGE("Write interface token error: %{public}s", __func__);
953         return WIFI_OPT_FAILED;
954     }
955     data.WriteInt32(0);
956     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS), data,
957         reply, option);
958     if (error != ERR_NONE) {
959         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
960             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_GROUPS), error);
961         return WIFI_OPT_FAILED;
962     }
963 
964     int exception = reply.ReadInt32();
965     if (exception) {
966         return WIFI_OPT_FAILED;
967     }
968     int ret = reply.ReadInt32();
969     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
970         return ErrCode(ret);
971     }
972 
973     constexpr int MAX_SIZE = 512;
974     int size = reply.ReadInt32();
975     if (size > MAX_SIZE) {
976         WIFI_LOGE("Get p2p group size error: %{public}d", size);
977         return WIFI_OPT_FAILED;
978     }
979     for (int i = 0; i < size; ++i) {
980         WifiP2pGroupInfo group;
981         ReadWifiP2pGroupData(reply, group);
982         groups.emplace_back(group);
983     }
984     return WIFI_OPT_SUCCESS;
985 }
986 
QueryP2pServices(std::vector<WifiP2pServiceInfo> & services)987 ErrCode WifiP2pProxy::QueryP2pServices(std::vector<WifiP2pServiceInfo> &services)
988 {
989     if (mRemoteDied) {
990         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
991         return WIFI_OPT_FAILED;
992     }
993     MessageOption option;
994     MessageParcel data;
995     MessageParcel reply;
996     if (!data.WriteInterfaceToken(GetDescriptor())) {
997         WIFI_LOGE("Write interface token error: %{public}s", __func__);
998         return WIFI_OPT_FAILED;
999     }
1000     data.WriteInt32(0);
1001     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES), data,
1002         reply, option);
1003     if (error != ERR_NONE) {
1004         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1005             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_QUERY_SERVICES), error);
1006         return WIFI_OPT_FAILED;
1007     }
1008 
1009     int exception = reply.ReadInt32();
1010     if (exception) {
1011         return WIFI_OPT_FAILED;
1012     }
1013     int ret = reply.ReadInt32();
1014     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1015         return ErrCode(ret);
1016     }
1017 
1018     constexpr int MAX_SIZE = 512;
1019     int size = reply.ReadInt32();
1020     if (size > MAX_SIZE) {
1021         WIFI_LOGE("Get p2p service size error: %{public}d", size);
1022         return WIFI_OPT_FAILED;
1023     }
1024     for (int i = 0; i < size; ++i) {
1025         WifiP2pServiceInfo info;
1026         ReadWifiP2pServiceInfo(reply, info);
1027         services.emplace_back(info);
1028     }
1029     return WIFI_OPT_SUCCESS;
1030 }
1031 
SetP2pDeviceName(const std::string & deviceName)1032 ErrCode WifiP2pProxy::SetP2pDeviceName(const std::string &deviceName)
1033 {
1034     if (mRemoteDied) {
1035         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1036         return WIFI_OPT_FAILED;
1037     }
1038     MessageOption option;
1039     MessageParcel data;
1040     MessageParcel 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     data.WriteString(deviceName);
1047     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME), data,
1048         reply, option);
1049     if (error != ERR_NONE) {
1050         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1051             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_DEVICE_NAME), error);
1052         return WIFI_OPT_FAILED;
1053     }
1054 
1055     int exception = reply.ReadInt32();
1056     if (exception) {
1057         return WIFI_OPT_FAILED;
1058     }
1059     int ret = reply.ReadInt32();
1060     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1061         return ErrCode(ret);
1062     }
1063     return WIFI_OPT_SUCCESS;
1064 }
SetP2pWfdInfo(const WifiP2pWfdInfo & wfdInfo)1065 ErrCode WifiP2pProxy::SetP2pWfdInfo(const WifiP2pWfdInfo &wfdInfo)
1066 {
1067     if (mRemoteDied) {
1068         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1069         return WIFI_OPT_FAILED;
1070     }
1071     MessageOption option;
1072     MessageParcel data;
1073     MessageParcel reply;
1074     if (!data.WriteInterfaceToken(GetDescriptor())) {
1075         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1076         return WIFI_OPT_FAILED;
1077     }
1078     data.WriteInt32(0);
1079     data.WriteBool(wfdInfo.GetWfdEnabled());
1080     data.WriteInt32(wfdInfo.GetDeviceInfo());
1081     data.WriteInt32(wfdInfo.GetCtrlPort());
1082     data.WriteInt32(wfdInfo.GetMaxThroughput());
1083     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO), data,
1084         reply, option);
1085     if (error != ERR_NONE) {
1086         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1087             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_SET_WFD_INFO), error);
1088         return WIFI_OPT_FAILED;
1089     }
1090 
1091     int exception = reply.ReadInt32();
1092     if (exception) {
1093         return WIFI_OPT_FAILED;
1094     }
1095     int ret = reply.ReadInt32();
1096     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1097         return ErrCode(ret);
1098     }
1099     return WIFI_OPT_SUCCESS;
1100 }
1101 
1102 
RegisterCallBack(const sptr<IWifiP2pCallback> & callback,const std::vector<std::string> & event)1103 ErrCode WifiP2pProxy::RegisterCallBack(const sptr<IWifiP2pCallback> &callback, const std::vector<std::string> &event)
1104 {
1105     if (mRemoteDied) {
1106         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1107         return WIFI_OPT_FAILED;
1108     }
1109     MessageParcel data;
1110     MessageParcel reply;
1111     MessageOption option(MessageOption::TF_ASYNC);
1112 
1113     if (g_wifiP2pCallbackStub == nullptr) {
1114         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1115         return WIFI_OPT_FAILED;
1116     }
1117     g_wifiP2pCallbackStub->RegisterCallBack(callback);
1118     if (!data.WriteInterfaceToken(GetDescriptor())) {
1119         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1120         return WIFI_OPT_FAILED;
1121     }
1122     data.WriteInt32(0);
1123     if (!data.WriteRemoteObject(g_wifiP2pCallbackStub->AsObject())) {
1124         WIFI_LOGE("RegisterCallBack WriteRemoteObject failed!");
1125         return WIFI_OPT_FAILED;
1126     }
1127     int pid = GetCallingPid();
1128     data.WriteInt32(pid);
1129     int tokenId = GetCallingTokenId();
1130     data.WriteInt32(tokenId);
1131     int eventNum = event.size();
1132     data.WriteInt32(eventNum);
1133     if (eventNum > 0) {
1134         for (auto &eventName : event) {
1135             data.WriteString(eventName);
1136         }
1137     }
1138     WIFI_LOGD("%{public}s, calling uid: %{public}d, pid: %{public}d, tokenId: %{private}d",
1139         __func__, GetCallingUid(), pid, tokenId);
1140     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_REGISTER_CALLBACK), data,
1141         reply, option);
1142     if (error != ERR_NONE) {
1143         WIFI_LOGE("RegisterCallBack failed, error code is %{public}d", error);
1144         return WIFI_OPT_FAILED;
1145     }
1146     int exception = reply.ReadInt32();
1147     if (exception) {
1148         return WIFI_OPT_FAILED;
1149     }
1150     int ret = reply.ReadInt32();
1151     WIFI_LOGD("RegisterCallBack is finished: result=%{public}d", ret);
1152     return ErrCode(ret);
1153 }
1154 
GetSupportedFeatures(long & features)1155 ErrCode WifiP2pProxy::GetSupportedFeatures(long &features)
1156 {
1157     if (mRemoteDied) {
1158         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1159         return WIFI_OPT_FAILED;
1160     }
1161     MessageOption option;
1162     MessageParcel data, reply;
1163     if (!data.WriteInterfaceToken(GetDescriptor())) {
1164         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1165         return WIFI_OPT_FAILED;
1166     }
1167     data.WriteInt32(0);
1168     int error = Remote()->SendRequest(static_cast<uint32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES),
1169         data, reply, option);
1170     if (error != ERR_NONE) {
1171         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1172             static_cast<int32_t>(DevInterfaceCode::WIFI_SVR_CMD_GET_SUPPORTED_FEATURES), error);
1173         return ErrCode(error);
1174     }
1175     int exception = reply.ReadInt32();
1176     if (exception) {
1177         return WIFI_OPT_FAILED;
1178     }
1179     int ret = reply.ReadInt32();
1180     if (ret != WIFI_OPT_SUCCESS) {
1181         return ErrCode(ret);
1182     }
1183 
1184     features = reply.ReadInt64();
1185     return WIFI_OPT_SUCCESS;
1186 }
1187 
Hid2dRequestGcIp(const std::string & gcMac,std::string & ipAddr)1188 ErrCode WifiP2pProxy::Hid2dRequestGcIp(const std::string& gcMac, std::string& ipAddr)
1189 {
1190     if (mRemoteDied) {
1191         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1192         return WIFI_OPT_FAILED;
1193     }
1194     MessageOption option;
1195     MessageParcel data;
1196     MessageParcel reply;
1197     if (!data.WriteInterfaceToken(GetDescriptor())) {
1198         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1199         return WIFI_OPT_FAILED;
1200     }
1201     data.WriteInt32(0);
1202     data.WriteCString(gcMac.c_str());
1203     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP), data,
1204         reply, option);
1205     if (error != ERR_NONE) {
1206         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1207             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_APPLY_IP), error);
1208         return WIFI_OPT_FAILED;
1209     }
1210     int exception = reply.ReadInt32();
1211     if (exception) {
1212         return WIFI_OPT_FAILED;
1213     }
1214     int ret = reply.ReadInt32();
1215     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1216         return ErrCode(ret);
1217     }
1218     const char *ipAddress = reply.ReadCString();
1219     ipAddr = (ipAddress != nullptr) ? ipAddress : "";
1220     return WIFI_OPT_SUCCESS;
1221 }
1222 
Hid2dSharedlinkIncrease()1223 ErrCode WifiP2pProxy::Hid2dSharedlinkIncrease()
1224 {
1225     if (mRemoteDied) {
1226         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1227         return WIFI_OPT_FAILED;
1228     }
1229     MessageOption option;
1230     MessageParcel data;
1231     MessageParcel reply;
1232     if (!data.WriteInterfaceToken(GetDescriptor())) {
1233         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1234         return WIFI_OPT_FAILED;
1235     }
1236     data.WriteInt32(0);
1237     int error = Remote()->SendRequest(
1238         static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE), data, reply, option);
1239     if (error != ERR_NONE) {
1240         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1241             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_INCREASE), error);
1242         return WIFI_OPT_FAILED;
1243     }
1244     int exception = reply.ReadInt32();
1245     if (exception) {
1246         return WIFI_OPT_FAILED;
1247     }
1248     return ErrCode(reply.ReadInt32());
1249 }
1250 
Hid2dSharedlinkDecrease()1251 ErrCode WifiP2pProxy::Hid2dSharedlinkDecrease()
1252 {
1253     if (mRemoteDied) {
1254         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1255         return WIFI_OPT_FAILED;
1256     }
1257     MessageOption option;
1258     MessageParcel data;
1259     MessageParcel reply;
1260     if (!data.WriteInterfaceToken(GetDescriptor())) {
1261         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1262         return WIFI_OPT_FAILED;
1263     }
1264     data.WriteInt32(0);
1265     int error = Remote()->SendRequest(
1266         static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE), data, reply, option);
1267     if (error != ERR_NONE) {
1268         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1269             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_SHARED_LINK_DECREASE), error);
1270         return WIFI_OPT_FAILED;
1271     }
1272     int exception = reply.ReadInt32();
1273     if (exception) {
1274         return WIFI_OPT_FAILED;
1275     }
1276     return ErrCode(reply.ReadInt32());
1277 }
1278 
Hid2dCreateGroup(const int frequency,FreqType type)1279 ErrCode WifiP2pProxy::Hid2dCreateGroup(const int frequency, FreqType type)
1280 {
1281     WIFI_LOGI("Request hid2d create group");
1282 
1283     if (mRemoteDied) {
1284         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1285         return WIFI_OPT_FAILED;
1286     }
1287     MessageOption option;
1288     MessageParcel data;
1289     MessageParcel reply;
1290     if (!data.WriteInterfaceToken(GetDescriptor())) {
1291         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1292         return WIFI_OPT_FAILED;
1293     }
1294     data.WriteInt32(0);
1295     data.WriteInt32(frequency);
1296     data.WriteInt32(static_cast<int>(type));
1297     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP),
1298         data, reply, option);
1299     if (error != ERR_NONE) {
1300         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1301             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CREATE_GROUP), error);
1302         return WIFI_OPT_FAILED;
1303     }
1304     int exception = reply.ReadInt32();
1305     if (exception) {
1306         return WIFI_OPT_FAILED;
1307     }
1308     return ErrCode(reply.ReadInt32());
1309 }
1310 
Hid2dRemoveGcGroup(const std::string & gcIfName)1311 ErrCode WifiP2pProxy::Hid2dRemoveGcGroup(const std::string& gcIfName)
1312 {
1313     WIFI_LOGI("Request hid2d remove group");
1314 
1315     if (mRemoteDied) {
1316         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1317         return WIFI_OPT_FAILED;
1318     }
1319     MessageOption option;
1320     MessageParcel data;
1321     MessageParcel reply;
1322     if (!data.WriteInterfaceToken(GetDescriptor())) {
1323         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1324         return WIFI_OPT_FAILED;
1325     }
1326     data.WriteInt32(0);
1327     data.WriteCString(gcIfName.c_str());
1328     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP),
1329         data, reply, option);
1330     if (error != ERR_NONE) {
1331         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1332             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_REMOVE_GC_GROUP), error);
1333         return WIFI_OPT_FAILED;
1334     }
1335     int exception = reply.ReadInt32();
1336     if (exception) {
1337         return WIFI_OPT_FAILED;
1338     }
1339     return ErrCode(reply.ReadInt32());
1340 }
1341 
Hid2dConnect(const Hid2dConnectConfig & config)1342 ErrCode WifiP2pProxy::Hid2dConnect(const Hid2dConnectConfig& config)
1343 {
1344     WIFI_LOGI("Request hid2d connect");
1345 
1346     if (mRemoteDied) {
1347         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1348         return WIFI_OPT_FAILED;
1349     }
1350     MessageOption option;
1351     MessageParcel data;
1352     MessageParcel reply;
1353     if (!data.WriteInterfaceToken(GetDescriptor())) {
1354         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1355         return WIFI_OPT_FAILED;
1356     }
1357     data.WriteInt32(0);
1358     data.WriteCString(config.GetSsid().c_str());
1359     data.WriteCString(config.GetBssid().c_str());
1360     data.WriteCString(config.GetPreSharedKey().c_str());
1361     data.WriteInt32(config.GetFrequency());
1362     data.WriteInt32(static_cast<int>(config.GetDhcpMode()));
1363     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT), data,
1364         reply, option);
1365     if (error != ERR_NONE) {
1366         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1367             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONNECT), error);
1368         return WIFI_OPT_FAILED;
1369     }
1370     int exception = reply.ReadInt32();
1371     if (exception) {
1372         return WIFI_OPT_FAILED;
1373     }
1374     return ErrCode(reply.ReadInt32());
1375 }
1376 
Hid2dConfigIPAddr(const std::string & ifName,const IpAddrInfo & ipInfo)1377 ErrCode WifiP2pProxy::Hid2dConfigIPAddr(const std::string& ifName, const IpAddrInfo& ipInfo)
1378 {
1379     if (mRemoteDied) {
1380         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1381         return WIFI_OPT_FAILED;
1382     }
1383     MessageOption option;
1384     MessageParcel data;
1385     MessageParcel reply;
1386     if (!data.WriteInterfaceToken(GetDescriptor())) {
1387         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1388         return WIFI_OPT_FAILED;
1389     }
1390     data.WriteInt32(0);
1391     data.WriteCString(ifName.c_str());
1392     data.WriteCString(ipInfo.ip.c_str());
1393     data.WriteCString(ipInfo.gateway.c_str());
1394     data.WriteCString(ipInfo.netmask.c_str());
1395     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP), data,
1396         reply, option);
1397     if (error != ERR_NONE) {
1398         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1399             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_CONFIG_IP), error);
1400         return WIFI_OPT_FAILED;
1401     }
1402     int exception = reply.ReadInt32();
1403     if (exception) {
1404         return WIFI_OPT_FAILED;
1405     }
1406     return ErrCode(reply.ReadInt32());
1407 }
1408 
Hid2dReleaseIPAddr(const std::string & ifName)1409 ErrCode WifiP2pProxy::Hid2dReleaseIPAddr(const std::string& ifName)
1410 {
1411     if (mRemoteDied) {
1412         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1413         return WIFI_OPT_FAILED;
1414     }
1415     MessageOption option;
1416     MessageParcel data;
1417     MessageParcel reply;
1418     if (!data.WriteInterfaceToken(GetDescriptor())) {
1419         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1420         return WIFI_OPT_FAILED;
1421     }
1422     data.WriteInt32(0);
1423     data.WriteCString(ifName.c_str());
1424     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP), data,
1425         reply, option);
1426     if (error != ERR_NONE) {
1427         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1428             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_P2P_HID2D_RELEASE_IP), error);
1429         return WIFI_OPT_FAILED;
1430     }
1431     int exception = reply.ReadInt32();
1432     if (exception) {
1433         return WIFI_OPT_FAILED;
1434     }
1435     return ErrCode(reply.ReadInt32());
1436 }
1437 
Hid2dGetRecommendChannel(const RecommendChannelRequest & request,RecommendChannelResponse & response)1438 ErrCode WifiP2pProxy::Hid2dGetRecommendChannel(const RecommendChannelRequest& request,
1439     RecommendChannelResponse& response)
1440 {
1441     if (mRemoteDied) {
1442         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1443         return WIFI_OPT_FAILED;
1444     }
1445     MessageOption option;
1446     MessageParcel data, 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.WriteCString(request.remoteIfName.c_str());
1453     data.WriteInt32(request.remoteIfMode);
1454     data.WriteCString(request.localIfName.c_str());
1455     data.WriteInt32(request.localIfMode);
1456     data.WriteInt32(request.prefBand);
1457     data.WriteInt32(static_cast<int>(request.prefBandwidth));
1458     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL),
1459         data, reply, option);
1460     if (error != ERR_NONE) {
1461         WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d",
1462             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_P2P_RECOMMENDED_CHANNEL), error);
1463         return ErrCode(error);
1464     }
1465     int exception = reply.ReadInt32();
1466     if (exception) {
1467         return WIFI_OPT_FAILED;
1468     }
1469     int ret = reply.ReadInt32();
1470     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1471         return ErrCode(ret);
1472     }
1473     response.status = RecommendStatus(reply.ReadInt32());
1474     response.index = reply.ReadInt32();
1475     response.centerFreq = reply.ReadInt32();
1476     response.centerFreq1 = reply.ReadInt32();
1477     response.centerFreq2 = reply.ReadInt32();
1478     response.bandwidth = reply.ReadInt32();
1479     return WIFI_OPT_SUCCESS;
1480 }
1481 
Hid2dGetChannelListFor5G(std::vector<int> & vecChannelList)1482 ErrCode WifiP2pProxy::Hid2dGetChannelListFor5G(std::vector<int>& vecChannelList)
1483 {
1484     if (mRemoteDied) {
1485         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1486         return WIFI_OPT_FAILED;
1487     }
1488     MessageOption option;
1489     MessageParcel data;
1490     MessageParcel reply;
1491     if (!data.WriteInterfaceToken(GetDescriptor())) {
1492         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1493         return WIFI_OPT_FAILED;
1494     }
1495     data.WriteInt32(0);
1496     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST), data,
1497         reply, option);
1498     if (error != ERR_NONE) {
1499         WIFI_LOGW("Set Attr(%{public}d) failed",
1500             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_5G_CHANNEL_LIST));
1501         return WIFI_OPT_FAILED;
1502     }
1503     int exception = reply.ReadInt32();
1504     if (exception) {
1505         return WIFI_OPT_FAILED;
1506     }
1507     int ret = reply.ReadInt32();
1508     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1509         return ErrCode(ret);
1510     }
1511 
1512     constexpr int MAX_SIZE = 512;
1513     int listSize = reply.ReadInt32();
1514     if (listSize > MAX_SIZE) {
1515         WIFI_LOGE("Get channel list for 5G size error: %{public}d", listSize);
1516         return WIFI_OPT_FAILED;
1517     }
1518     for (int i = 0; i < listSize; ++i) {
1519         vecChannelList.emplace_back(reply.ReadInt32());
1520     }
1521     return WIFI_OPT_SUCCESS;
1522 }
1523 
Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int * getDatValidLen)1524 ErrCode WifiP2pProxy::Hid2dGetSelfWifiCfgInfo(SelfCfgType cfgType,
1525     char cfgData[CFG_DATA_MAX_BYTES], int* getDatValidLen)
1526 {
1527     if (getDatValidLen == nullptr) {
1528         WIFI_LOGE("getDatValidLen is nullptr!");
1529         return WIFI_OPT_FAILED;
1530     }
1531 
1532     if (mRemoteDied) {
1533         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1534         return WIFI_OPT_FAILED;
1535     }
1536     MessageOption option;
1537     MessageParcel data;
1538     MessageParcel reply;
1539     if (!data.WriteInterfaceToken(GetDescriptor())) {
1540         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1541         return WIFI_OPT_FAILED;
1542     }
1543     data.WriteInt32(0);
1544     data.WriteInt32(static_cast<int>(cfgType));
1545     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG), data,
1546         reply, option);
1547     if (error != ERR_NONE) {
1548         WIFI_LOGW("Set Attr(%{public}d) failed",
1549             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_GET_SELF_WIFI_CFG));
1550         return WIFI_OPT_FAILED;
1551     }
1552     int exception = reply.ReadInt32();
1553     if (exception) {
1554         return WIFI_OPT_FAILED;
1555     }
1556     int ret = reply.ReadInt32();
1557     if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
1558         return WIFI_OPT_FAILED;
1559     }
1560 
1561     *getDatValidLen = reply.ReadInt32();
1562     if (*getDatValidLen > 0) {
1563         const char *dataBuffer = (const char *)reply.ReadBuffer(*getDatValidLen);
1564         if (dataBuffer == nullptr) {
1565             WIFI_LOGE("`%{public}s` inner communication error!", __func__);
1566             return WIFI_OPT_FAILED;
1567         }
1568         if (memcpy_s(cfgData, CFG_DATA_MAX_BYTES, dataBuffer, *getDatValidLen) != EOK) {
1569             WIFI_LOGE("`%{public}s` memcpy_s failed!", __func__);
1570             return WIFI_OPT_FAILED;
1571         }
1572     }
1573     return WIFI_OPT_SUCCESS;
1574 }
1575 
Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,char cfgData[CFG_DATA_MAX_BYTES],int setDataValidLen)1576 ErrCode WifiP2pProxy::Hid2dSetPeerWifiCfgInfo(PeerCfgType cfgType,
1577     char cfgData[CFG_DATA_MAX_BYTES], int setDataValidLen)
1578 {
1579     if (setDataValidLen <= 0) {
1580         WIFI_LOGE("`%{public}s` parameter is error!", __func__);
1581         return WIFI_OPT_INVALID_PARAM;
1582     }
1583     if (mRemoteDied) {
1584         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1585         return WIFI_OPT_FAILED;
1586     }
1587     MessageOption option;
1588     MessageParcel data;
1589     MessageParcel reply;
1590     if (!data.WriteInterfaceToken(GetDescriptor())) {
1591         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1592         return WIFI_OPT_FAILED;
1593     }
1594     data.WriteInt32(0);
1595     data.WriteInt32(static_cast<int>(cfgType));
1596     data.WriteInt32(setDataValidLen);
1597     data.WriteBuffer(cfgData, setDataValidLen);
1598     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG), data,
1599         reply, option);
1600     if (error != ERR_NONE) {
1601         WIFI_LOGW("Set Attr(%{public}d) failed",
1602             static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_PEER_WIFI_CFG));
1603         return WIFI_OPT_FAILED;
1604     }
1605     int exception = reply.ReadInt32();
1606     if (exception) {
1607         return WIFI_OPT_FAILED;
1608     }
1609     return ErrCode(reply.ReadInt32());
1610 }
1611 
Hid2dSetUpperScene(const std::string & ifName,const Hid2dUpperScene & scene)1612 ErrCode WifiP2pProxy::Hid2dSetUpperScene(const std::string& ifName, const Hid2dUpperScene& scene)
1613 {
1614     if (mRemoteDied) {
1615         WIFI_LOGW("failed to `%{public}s`,remote service is died!", __func__);
1616         return WIFI_OPT_FAILED;
1617     }
1618     MessageOption option;
1619     MessageParcel data;
1620     MessageParcel reply;
1621     if (!data.WriteInterfaceToken(GetDescriptor())) {
1622         WIFI_LOGE("Write interface token error: %{public}s", __func__);
1623         return WIFI_OPT_FAILED;
1624     }
1625     data.WriteInt32(0);
1626     data.WriteCString(ifName.c_str());
1627     data.WriteCString(scene.mac.c_str());
1628     data.WriteUint32(scene.scene);
1629     data.WriteInt32(scene.fps);
1630     data.WriteUint32(scene.bw);
1631     int error = Remote()->SendRequest(static_cast<uint32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE), data,
1632         reply, option);
1633     if (error != ERR_NONE) {
1634         WIFI_LOGW("Set Attr(%{public}d) failed", static_cast<int32_t>(P2PInterfaceCode::WIFI_SVR_CMD_SET_UPPER_SCENE));
1635         return WIFI_OPT_FAILED;
1636     }
1637     int exception = reply.ReadInt32();
1638     if (exception) {
1639         return WIFI_OPT_FAILED;
1640     }
1641     return ErrCode(reply.ReadInt32());
1642 }
1643 
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)1644 void WifiP2pProxy::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
1645 {
1646     WIFI_LOGE("Remote service is died!");
1647     mRemoteDied = true;
1648     if (g_wifiP2pCallbackStub == nullptr) {
1649         WIFI_LOGE("g_wifiP2pCallbackStub is nullptr");
1650         return;
1651     }
1652     g_wifiP2pCallbackStub->SetRemoteDied(true);
1653 }
1654 
IsRemoteDied(void)1655 bool WifiP2pProxy::IsRemoteDied(void)
1656 {
1657     if (mRemoteDied) {
1658         WIFI_LOGW("IsRemoteDied! remote is died now!");
1659     }
1660     return mRemoteDied;
1661 }
1662 }  // namespace Wifi
1663 }  // namespace OHOS
1664