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 
16 #include "wifi_hal_crpc_p2p.h"
17 #include <securec.h>
18 #include "serial.h"
19 #include "wifi_hal_p2p_interface.h"
20 #include "wifi_hal_define.h"
21 
22 #define WIFI_IDL_GET_MAX_BANDS 32
23 #define GROUP_CONFIG_END_POS 9
24 
RpcP2pStart(RpcServer * server,Context * context)25 int RpcP2pStart(RpcServer *server, Context *context)
26 {
27     if (server == NULL || context == NULL) {
28         return HAL_FAILURE;
29     }
30     WifiErrorNo err = P2pStart();
31     WriteBegin(context, 0);
32     WriteInt(context, err);
33     WriteEnd(context);
34     return HAL_SUCCESS;
35 }
36 
RpcP2pStop(RpcServer * server,Context * context)37 int RpcP2pStop(RpcServer *server, Context *context)
38 {
39     if (server == NULL || context == NULL) {
40         return HAL_FAILURE;
41     }
42     WifiErrorNo err = P2pStop();
43     WriteBegin(context, 0);
44     WriteInt(context, err);
45     WriteEnd(context);
46     return HAL_SUCCESS;
47 }
48 
RpcP2pSetRandomMac(RpcServer * server,Context * context)49 int RpcP2pSetRandomMac(RpcServer *server, Context *context)
50 {
51     if (server == NULL || context == NULL) {
52         return HAL_FAILURE;
53     }
54     int enable = 0;
55     if (ReadInt(context, &enable) < 0) {
56         return HAL_FAILURE;
57     }
58     WifiErrorNo err = P2pSetRandomMac(enable);
59     WriteBegin(context, 0);
60     WriteInt(context, err);
61     WriteEnd(context);
62     return HAL_SUCCESS;
63 }
64 
RpcP2pSetDeviceName(RpcServer * server,Context * context)65 int RpcP2pSetDeviceName(RpcServer *server, Context *context)
66 {
67     if (server == NULL || context == NULL) {
68         return HAL_FAILURE;
69     }
70     char name[WIFI_P2P_WPS_NAME_LENGTH] = {0};
71     if (ReadStr(context, name, sizeof(name)) != 0) {
72         return HAL_FAILURE;
73     }
74     WifiErrorNo err = P2pSetDeviceName(name);
75     WriteBegin(context, 0);
76     WriteInt(context, err);
77     WriteEnd(context);
78     return HAL_SUCCESS;
79 }
80 
RpcP2pSetSsidPostfixName(RpcServer * server,Context * context)81 int RpcP2pSetSsidPostfixName(RpcServer *server, Context *context)
82 {
83     if (server == NULL || context == NULL) {
84         return HAL_FAILURE;
85     }
86     char name[WIFI_P2P_WPS_NAME_LENGTH] = {0};
87     if (ReadStr(context, name, sizeof(name)) != 0) {
88         return HAL_FAILURE;
89     }
90     WifiErrorNo err = P2pSetSsidPostfixName(name);
91     WriteBegin(context, 0);
92     WriteInt(context, err);
93     WriteEnd(context);
94     return HAL_SUCCESS;
95 }
96 
RpcP2pSetWpsDeviceType(RpcServer * server,Context * context)97 int RpcP2pSetWpsDeviceType(RpcServer *server, Context *context)
98 {
99     if (server == NULL || context == NULL) {
100         return HAL_FAILURE;
101     }
102     char type[WIFI_P2P_WPS_NAME_LENGTH] = {0};
103     if (ReadStr(context, type, sizeof(type)) != 0) {
104         return HAL_FAILURE;
105     }
106     WifiErrorNo err = P2pSetWpsDeviceType(type);
107     WriteBegin(context, 0);
108     WriteInt(context, err);
109     WriteEnd(context);
110     return HAL_SUCCESS;
111 }
112 
RpcP2pSetWpsSecondaryDeviceType(RpcServer * server,Context * context)113 int RpcP2pSetWpsSecondaryDeviceType(RpcServer *server, Context *context)
114 {
115     if (server == NULL || context == NULL) {
116         return HAL_FAILURE;
117     }
118     char type[WIFI_P2P_WPS_NAME_LENGTH] = {0};
119     if (ReadStr(context, type, sizeof(type)) != 0) {
120         return HAL_FAILURE;
121     }
122     WifiErrorNo err = P2pSetWpsSecondaryDeviceType(type);
123     WriteBegin(context, 0);
124     WriteInt(context, err);
125     WriteEnd(context);
126     return HAL_SUCCESS;
127 }
128 
RpcP2pSetWpsConfigMethods(RpcServer * server,Context * context)129 int RpcP2pSetWpsConfigMethods(RpcServer *server, Context *context)
130 {
131     if (server == NULL || context == NULL) {
132         return HAL_FAILURE;
133     }
134     char methods[WIFI_P2P_WPS_METHODS_LENGTH] = {0};
135     if (ReadStr(context, methods, sizeof(methods)) != 0) {
136         return HAL_FAILURE;
137     }
138     WifiErrorNo err = P2pSetWpsConfigMethods(methods);
139     WriteBegin(context, 0);
140     WriteInt(context, err);
141     WriteEnd(context);
142     return HAL_SUCCESS;
143 }
144 
RpcP2pGetDeviceAddress(RpcServer * server,Context * context)145 int RpcP2pGetDeviceAddress(RpcServer *server, Context *context)
146 {
147     if (server == NULL || context == NULL) {
148         return HAL_FAILURE;
149     }
150     int size = 0;
151     if (ReadInt(context, &size) < 0 || size <= 0) {
152         return HAL_FAILURE;
153     }
154     char *address = (char *)calloc(size, sizeof(char));
155     if (address == NULL) {
156         return HAL_FAILURE;
157     }
158     WifiErrorNo err = P2pGetDeviceAddress(address, size);
159     WriteBegin(context, 0);
160     WriteInt(context, err);
161     if (err == WIFI_HAL_SUCCESS) {
162         WriteStr(context, address);
163     }
164     WriteEnd(context);
165     free(address);
166     address = NULL;
167     return HAL_SUCCESS;
168 }
169 
RpcP2pFlush(RpcServer * server,Context * context)170 int RpcP2pFlush(RpcServer *server, Context *context)
171 {
172     if (server == NULL || context == NULL) {
173         return HAL_FAILURE;
174     }
175     WifiErrorNo err = P2pFlush();
176     WriteBegin(context, 0);
177     WriteInt(context, err);
178     WriteEnd(context);
179     return HAL_SUCCESS;
180 }
181 
RpcP2pFlushService(RpcServer * server,Context * context)182 int RpcP2pFlushService(RpcServer *server, Context *context)
183 {
184     if (server == NULL || context == NULL) {
185         return HAL_FAILURE;
186     }
187     WifiErrorNo err = P2pFlushService();
188     WriteBegin(context, 0);
189     WriteInt(context, err);
190     WriteEnd(context);
191     return HAL_SUCCESS;
192 }
193 
RpcP2pSaveConfig(RpcServer * server,Context * context)194 int RpcP2pSaveConfig(RpcServer *server, Context *context)
195 {
196     if (server == NULL || context == NULL) {
197         return HAL_FAILURE;
198     }
199     WifiErrorNo err = P2pSaveConfig();
200     WriteBegin(context, 0);
201     WriteInt(context, err);
202     WriteEnd(context);
203     return HAL_SUCCESS;
204 }
205 
RpcP2pSetupWpsPbc(RpcServer * server,Context * context)206 int RpcP2pSetupWpsPbc(RpcServer *server, Context *context)
207 {
208     if (server == NULL || context == NULL) {
209         return HAL_FAILURE;
210     }
211     char interface[WIFI_P2P_GROUP_IFNAME_LENGTH] = {0};
212     char bssid[WIFI_BSSID_LENGTH] = {0};
213     if (ReadStr(context, interface, sizeof(interface)) != 0 || ReadStr(context, bssid, sizeof(bssid)) != 0) {
214         return HAL_FAILURE;
215     }
216     WifiErrorNo err = P2pSetupWpsPbc(interface, bssid);
217     WriteBegin(context, 0);
218     WriteInt(context, err);
219     WriteEnd(context);
220     return HAL_SUCCESS;
221 }
222 
RpcP2pSetupWpsPin(RpcServer * server,Context * context)223 int RpcP2pSetupWpsPin(RpcServer *server, Context *context)
224 {
225     if (server == NULL || context == NULL) {
226         return HAL_FAILURE;
227     }
228     char interface[WIFI_P2P_GROUP_IFNAME_LENGTH] = {0};
229     char address[WIFI_BSSID_LENGTH] = {0};
230     char pinCode[WIFI_PIN_CODE_LENGTH + 1] = {0};
231     int resultLen = 0;
232     if (ReadStr(context, interface, sizeof(interface)) != 0 || ReadStr(context, address, sizeof(address)) != 0 ||
233         ReadStr(context, pinCode, sizeof(pinCode)) != 0 || ReadInt(context, &resultLen) < 0) {
234         return HAL_FAILURE;
235     }
236     if (resultLen <= 0) {
237         return HAL_FAILURE;
238     }
239     char *pResult = (char *)calloc(resultLen, sizeof(char));
240     if (pResult == NULL) {
241         return HAL_FAILURE;
242     }
243     WifiErrorNo err = P2pSetupWpsPin(interface, address, pinCode, pResult, resultLen);
244     WriteBegin(context, 0);
245     WriteInt(context, err);
246     if (err == WIFI_HAL_SUCCESS) {
247         WriteStr(context, pResult);
248     }
249     WriteEnd(context);
250     free(pResult);
251     pResult = NULL;
252     return HAL_SUCCESS;
253 }
254 
RpcP2pRemoveNetwork(RpcServer * server,Context * context)255 int RpcP2pRemoveNetwork(RpcServer *server, Context *context)
256 {
257     if (server == NULL || context == NULL) {
258         return HAL_FAILURE;
259     }
260     int networkId = 0;
261     if (ReadInt(context, &networkId) < 0) {
262         return HAL_FAILURE;
263     }
264     WifiErrorNo err = P2pRemoveNetwork(networkId);
265     WriteBegin(context, 0);
266     WriteInt(context, err);
267     WriteEnd(context);
268     return HAL_SUCCESS;
269 }
270 
RpcP2pRemoveClient(RpcServer * server,Context * context)271 int RpcP2pRemoveClient(RpcServer *server, Context *context)
272 {
273     if (server == NULL || context == NULL) {
274         return HAL_FAILURE;
275     }
276     char deviceMac[WIFI_BSSID_LENGTH] = {0};
277     if (ReadStr(context, deviceMac, sizeof(deviceMac)) != 0) {
278         return HAL_FAILURE;
279     }
280     WifiErrorNo err = P2pRemoveGroupClient(deviceMac);
281     WriteBegin(context, 0);
282     WriteInt(context, err);
283     WriteEnd(context);
284     return HAL_SUCCESS;
285 }
286 
RpcP2pListNetworks(RpcServer * server,Context * context)287 int RpcP2pListNetworks(RpcServer *server, Context *context)
288 {
289     if (server == NULL || context == NULL) {
290         return HAL_FAILURE;
291     }
292     P2pNetworkList infoList;
293     if (memset_s(&infoList, sizeof(infoList), 0, sizeof(infoList)) != EOK) {
294         return HAL_FAILURE;
295     }
296     WifiErrorNo err = P2pListNetworks(&infoList);
297     WriteBegin(context, 0);
298     WriteInt(context, err);
299     if (err == WIFI_HAL_SUCCESS) {
300         WriteInt(context, infoList.infoNum);
301         for (int i = 0; i < infoList.infoNum; i++) {
302             WriteInt(context, infoList.infos[i].id);
303             WriteStr(context, infoList.infos[i].ssid);
304             WriteStr(context, infoList.infos[i].bssid);
305             WriteStr(context, infoList.infos[i].flags);
306         }
307     }
308     WriteEnd(context);
309     free(infoList.infos);
310     infoList.infos = NULL;
311     return HAL_SUCCESS;
312 }
313 
RpcP2pSetGroupMaxIdle(RpcServer * server,Context * context)314 int RpcP2pSetGroupMaxIdle(RpcServer *server, Context *context)
315 {
316     if (server == NULL || context == NULL) {
317         return HAL_FAILURE;
318     }
319     char interface[WIFI_P2P_GROUP_IFNAME_LENGTH] = {0};
320     int maxtime = 0;
321     if (ReadStr(context, interface, sizeof(interface)) != 0 || ReadInt(context, &maxtime) < 0) {
322         return HAL_FAILURE;
323     }
324     WifiErrorNo err = P2pSetGroupMaxIdle(interface, maxtime);
325     WriteBegin(context, 0);
326     WriteInt(context, err);
327     WriteEnd(context);
328     return HAL_SUCCESS;
329 }
330 
RpcP2pSetPowerSave(RpcServer * server,Context * context)331 int RpcP2pSetPowerSave(RpcServer *server, Context *context)
332 {
333     if (server == NULL || context == NULL) {
334         return HAL_FAILURE;
335     }
336     char interface[WIFI_P2P_GROUP_IFNAME_LENGTH] = {0};
337     int enable = 0;
338     if (ReadStr(context, interface, sizeof(interface)) != 0 || ReadInt(context, &enable) < 0) {
339         return HAL_FAILURE;
340     }
341     WifiErrorNo err = P2pSetPowerSave(interface, enable);
342     WriteBegin(context, 0);
343     WriteInt(context, err);
344     WriteEnd(context);
345     return HAL_SUCCESS;
346 }
347 
RpcP2pSetWfdEnable(RpcServer * server,Context * context)348 int RpcP2pSetWfdEnable(RpcServer *server, Context *context)
349 {
350     if (server == NULL || context == NULL) {
351         return HAL_FAILURE;
352     }
353     int enable = 0;
354     if (ReadInt(context, &enable) < 0) {
355         return HAL_FAILURE;
356     }
357     WifiErrorNo err = P2pSetWfdEnable(enable);
358     WriteBegin(context, 0);
359     WriteInt(context, err);
360     WriteEnd(context);
361     return HAL_SUCCESS;
362 }
363 
RpcP2pSetWfdDeviceConfig(RpcServer * server,Context * context)364 int RpcP2pSetWfdDeviceConfig(RpcServer *server, Context *context)
365 {
366     if (server == NULL || context == NULL) {
367         return HAL_FAILURE;
368     }
369     char conf[WIFI_P2P_WFD_DEVICE_CONF_LENGTH] = {0};
370     if (ReadStr(context, conf, sizeof(conf)) != 0) {
371         return HAL_FAILURE;
372     }
373     WifiErrorNo err = P2pSetWfdDeviceConfig(conf);
374     WriteBegin(context, 0);
375     WriteInt(context, err);
376     WriteEnd(context);
377     return HAL_SUCCESS;
378 }
379 
RpcP2pStartFind(RpcServer * server,Context * context)380 int RpcP2pStartFind(RpcServer *server, Context *context)
381 {
382     if (server == NULL || context == NULL) {
383         return HAL_FAILURE;
384     }
385     int timeout = 0;
386     if (ReadInt(context, &timeout) < 0) {
387         return HAL_FAILURE;
388     }
389     WifiErrorNo err = P2pStartFind(timeout);
390     WriteBegin(context, 0);
391     WriteInt(context, err);
392     WriteEnd(context);
393     return HAL_SUCCESS;
394 }
395 
RpcP2pStopFind(RpcServer * server,Context * context)396 int RpcP2pStopFind(RpcServer *server, Context *context)
397 {
398     if (server == NULL || context == NULL) {
399         return HAL_FAILURE;
400     }
401     WifiErrorNo err = P2pStopFind();
402     WriteBegin(context, 0);
403     WriteInt(context, err);
404     WriteEnd(context);
405     return HAL_SUCCESS;
406 }
407 
RpcP2pSetExtListen(RpcServer * server,Context * context)408 int RpcP2pSetExtListen(RpcServer *server, Context *context)
409 {
410     if (server == NULL || context == NULL) {
411         return HAL_FAILURE;
412     }
413     int enable = 0;
414     int period = 0;
415     int interval = 0;
416     if (ReadInt(context, &enable) < 0 || ReadInt(context, &period) < 0 || ReadInt(context, &interval) < 0) {
417         return HAL_FAILURE;
418     }
419     WifiErrorNo err = P2pSetExtListen(enable, period, interval);
420     WriteBegin(context, 0);
421     WriteInt(context, err);
422     WriteEnd(context);
423     return HAL_SUCCESS;
424 }
425 
RpcP2pSetListenChannel(RpcServer * server,Context * context)426 int RpcP2pSetListenChannel(RpcServer *server, Context *context)
427 {
428     if (server == NULL || context == NULL) {
429         return HAL_FAILURE;
430     }
431     int channel = 0;
432     int regClass = 0;
433     if (ReadInt(context, &channel) < 0 || ReadInt(context, &regClass) < 0) {
434         return HAL_FAILURE;
435     }
436     WifiErrorNo err = P2pSetListenChannel(channel, regClass);
437     WriteBegin(context, 0);
438     WriteInt(context, err);
439     WriteEnd(context);
440     return HAL_SUCCESS;
441 }
442 
RpcP2pConnect(RpcServer * server,Context * context)443 int RpcP2pConnect(RpcServer *server, Context *context)
444 {
445     if (server == NULL || context == NULL) {
446         return HAL_FAILURE;
447     }
448     P2pConnectInfo info;
449     if (memset_s(&info, sizeof(info), 0, sizeof(info)) != EOK) {
450         return HAL_FAILURE;
451     }
452     if (ReadInt(context, &info.mode) < 0 || ReadInt(context, &info.provdisc) < 0 ||
453         ReadInt(context, &info.goIntent) < 0 || ReadInt(context, &info.persistent) < 0 ||
454         ReadStr(context, info.peerDevAddr, sizeof(info.peerDevAddr)) != 0 ||
455         ReadStr(context, info.pin, sizeof(info.pin)) != 0) {
456         return HAL_FAILURE;
457     }
458     int flag = 0;
459     if (info.provdisc == HAL_WPS_METHOD_DISPLAY && strcmp(info.pin, "pin") == 0) {
460         flag = 1;
461     }
462     WifiErrorNo err = P2pConnect(&info);
463     WriteBegin(context, 0);
464     WriteInt(context, err);
465     if (err == WIFI_HAL_SUCCESS && flag) {
466         WriteStr(context, info.pin);
467     }
468     WriteEnd(context);
469     return HAL_SUCCESS;
470 }
471 
RpcP2pCancelConnect(RpcServer * server,Context * context)472 int RpcP2pCancelConnect(RpcServer *server, Context *context)
473 {
474     if (server == NULL || context == NULL) {
475         return HAL_FAILURE;
476     }
477     WifiErrorNo err = P2pCancelConnect();
478     WriteBegin(context, 0);
479     WriteInt(context, err);
480     WriteEnd(context);
481     return HAL_SUCCESS;
482 }
483 
RpcP2pProvisionDiscovery(RpcServer * server,Context * context)484 int RpcP2pProvisionDiscovery(RpcServer *server, Context *context)
485 {
486     if (server == NULL || context == NULL) {
487         return HAL_FAILURE;
488     }
489     char bssid[WIFI_BSSID_LENGTH] = {0};
490     int mode = 0;
491     if (ReadStr(context, bssid, sizeof(bssid)) != 0 || ReadInt(context, &mode) < 0) {
492         return HAL_FAILURE;
493     }
494     WifiErrorNo err = P2pProvisionDiscovery(bssid, mode);
495     WriteBegin(context, 0);
496     WriteInt(context, err);
497     WriteEnd(context);
498     return HAL_SUCCESS;
499 }
500 
RpcP2pAddGroup(RpcServer * server,Context * context)501 int RpcP2pAddGroup(RpcServer *server, Context *context)
502 {
503     if (server == NULL || context == NULL) {
504         return HAL_FAILURE;
505     }
506     int isPersistent = 0;
507     int networkId = 0;
508     int freq = 0;
509     if (ReadInt(context, &isPersistent) < 0 || ReadInt(context, &networkId) < 0 || ReadInt(context, &freq) < 0) {
510         return HAL_FAILURE;
511     }
512     WifiErrorNo err = P2pAddGroup(isPersistent, networkId, freq);
513     WriteBegin(context, 0);
514     WriteInt(context, err);
515     WriteEnd(context);
516     return HAL_SUCCESS;
517 }
518 
RpcP2pRemoveGroup(RpcServer * server,Context * context)519 int RpcP2pRemoveGroup(RpcServer *server, Context *context)
520 {
521     if (server == NULL || context == NULL) {
522         return HAL_FAILURE;
523     }
524     char interface[WIFI_P2P_GROUP_IFNAME_LENGTH] = {0};
525     if (ReadStr(context, interface, sizeof(interface)) != 0) {
526         return HAL_FAILURE;
527     }
528     WifiErrorNo err = P2pRemoveGroup(interface);
529     WriteBegin(context, 0);
530     WriteInt(context, err);
531     WriteEnd(context);
532     return HAL_SUCCESS;
533 }
534 
RpcP2pInvite(RpcServer * server,Context * context)535 int RpcP2pInvite(RpcServer *server, Context *context)
536 {
537     if (server == NULL || context == NULL) {
538         return HAL_FAILURE;
539     }
540     int persistent = 0;
541     char peerBssid[WIFI_BSSID_LENGTH] = {0};
542     char goBssid[WIFI_BSSID_LENGTH] = {0};
543     char ifName[WIFI_IFACE_NAME_MAXLEN] = {0};
544     if (ReadInt(context, &persistent) < 0 || ReadStr(context, peerBssid, sizeof(peerBssid)) != 0 ||
545         ReadStr(context, goBssid, sizeof(goBssid)) != 0 || ReadStr(context, ifName, sizeof(ifName)) != 0) {
546         return HAL_FAILURE;
547     }
548     WifiErrorNo err = P2pInvite(persistent, peerBssid, goBssid, ifName);
549     WriteBegin(context, 0);
550     WriteInt(context, err);
551     WriteEnd(context);
552     return HAL_SUCCESS;
553 }
554 
RpcP2pReinvoke(RpcServer * server,Context * context)555 int RpcP2pReinvoke(RpcServer *server, Context *context)
556 {
557     if (server == NULL || context == NULL) {
558         return HAL_FAILURE;
559     }
560     int networkId = 0;
561     char bssid[WIFI_BSSID_LENGTH] = {0};
562     if (ReadInt(context, &networkId) < 0 || ReadStr(context, bssid, sizeof(bssid)) != 0) {
563         return HAL_FAILURE;
564     }
565     WifiErrorNo err = P2pReinvoke(networkId, bssid);
566     WriteBegin(context, 0);
567     WriteInt(context, err);
568     WriteEnd(context);
569     return HAL_SUCCESS;
570 }
571 
RpcP2pGetGroupCapability(RpcServer * server,Context * context)572 int RpcP2pGetGroupCapability(RpcServer *server, Context *context)
573 {
574     if (server == NULL || context == NULL) {
575         return HAL_FAILURE;
576     }
577     char bssid[WIFI_BSSID_LENGTH] = {0};
578     if (ReadStr(context, bssid, sizeof(bssid)) != 0) {
579         return HAL_FAILURE;
580     }
581     int capacity = 0;
582     WifiErrorNo err = P2pGetGroupCapability(bssid, &capacity);
583     WriteBegin(context, 0);
584     WriteInt(context, err);
585     if (err == WIFI_HAL_SUCCESS) {
586         WriteInt(context, capacity);
587     }
588     WriteEnd(context);
589     return HAL_SUCCESS;
590 }
591 
RpcP2pAddService(RpcServer * server,Context * context)592 int RpcP2pAddService(RpcServer *server, Context *context)
593 {
594     if (server == NULL || context == NULL) {
595         return HAL_FAILURE;
596     }
597     P2pServiceInfo argv;
598     if (memset_s(&argv, sizeof(argv), 0, sizeof(argv)) != EOK) {
599         return HAL_FAILURE;
600     }
601     if (ReadInt(context, &argv.mode) < 0) {
602         return HAL_FAILURE;
603     }
604     if (!argv.mode) {
605         if (ReadInt(context, &argv.version) < 0 || ReadStr(context, argv.name, sizeof(argv.name)) != 0) {
606             return HAL_FAILURE;
607         }
608     } else {
609         if (ReadStr(context, argv.query, sizeof(argv.query)) != 0 ||
610             ReadStr(context, argv.resp, sizeof(argv.resp)) != 0) {
611             return HAL_FAILURE;
612         }
613     }
614     WifiErrorNo err = P2pAddService(&argv);
615     WriteBegin(context, 0);
616     WriteInt(context, err);
617     WriteEnd(context);
618     return HAL_SUCCESS;
619 }
620 
RpcP2pRemoveService(RpcServer * server,Context * context)621 int RpcP2pRemoveService(RpcServer *server, Context *context)
622 {
623     if (server == NULL || context == NULL) {
624         return HAL_FAILURE;
625     }
626     P2pServiceInfo argv;
627     if (memset_s(&argv, sizeof(argv), 0, sizeof(argv)) != EOK) {
628         return HAL_FAILURE;
629     }
630     if (ReadInt(context, &argv.mode) < 0) {
631         return HAL_FAILURE;
632     }
633     if (!argv.mode) {
634         if (ReadInt(context, &argv.version) < 0 || ReadStr(context, argv.name, sizeof(argv.name)) != 0) {
635             return HAL_FAILURE;
636         }
637     } else {
638         if (ReadStr(context, argv.query, sizeof(argv.query)) != 0) {
639             return HAL_FAILURE;
640         }
641     }
642     WifiErrorNo err = P2pRemoveService(&argv);
643     WriteBegin(context, 0);
644     WriteInt(context, err);
645     WriteEnd(context);
646     return HAL_SUCCESS;
647 }
648 
RpcP2pReqServiceDiscovery(RpcServer * server,Context * context)649 int RpcP2pReqServiceDiscovery(RpcServer *server, Context *context)
650 {
651     if (server == NULL || context == NULL) {
652         return HAL_FAILURE;
653     }
654     char bssid[WIFI_BSSID_LENGTH] = {0};
655     if (ReadStr(context, bssid, sizeof(bssid)) != 0) {
656         return HAL_FAILURE;
657     }
658     char discoverinfo[WIFI_P2P_SERVE_DISCOVER_MSG_LENGTH] = {0};
659     char *pDiscoverInfo = NULL;
660     int len = ReadStr(context, discoverinfo, sizeof(discoverinfo));
661     if (len < 0) {
662         return HAL_FAILURE;
663     } else if (len > 0) {
664         pDiscoverInfo = (char *)calloc(len + 1, sizeof(char));
665         if (pDiscoverInfo == NULL) {
666             return HAL_FAILURE;
667         }
668         if (ReadStr(context, pDiscoverInfo, len + 1) != 0) {
669             free(pDiscoverInfo);
670             pDiscoverInfo = NULL;
671             return HAL_FAILURE;
672         }
673     }
674     int retSize = 0;
675     if (ReadInt(context, &retSize) < 0 || retSize <= 0) {
676         free(pDiscoverInfo);
677         pDiscoverInfo = NULL;
678         return HAL_FAILURE;
679     }
680     char *pRetBuf = (char *)calloc(retSize, sizeof(char));
681     if (pRetBuf == NULL) {
682         free(pDiscoverInfo); /* free(NULL) is ok, so here no need to judge pDiscoverInfo != NULL */
683         pDiscoverInfo = NULL;
684         return HAL_FAILURE;
685     }
686     WifiErrorNo err =
687         P2pReqServiceDiscovery(bssid, ((pDiscoverInfo == NULL) ? discoverinfo : pDiscoverInfo), pRetBuf, retSize);
688     WriteBegin(context, 0);
689     WriteInt(context, err);
690     if (err == WIFI_HAL_SUCCESS) {
691         WriteStr(context, pRetBuf);
692     }
693     WriteEnd(context);
694     free(pRetBuf);
695     pRetBuf = NULL;
696     free(pDiscoverInfo);
697     pDiscoverInfo = NULL;
698     return HAL_SUCCESS;
699 }
700 
RpcP2pCancelServiceDiscovery(RpcServer * server,Context * context)701 int RpcP2pCancelServiceDiscovery(RpcServer *server, Context *context)
702 {
703     if (server == NULL || context == NULL) {
704         return HAL_FAILURE;
705     }
706     char id[WIFI_P2P_SERVER_DISCOVERY_SEQUENCE_LENGTH] = {0};
707     if (ReadStr(context, id, sizeof(id)) != 0) {
708         return HAL_FAILURE;
709     }
710     WifiErrorNo err = P2pCancelServiceDiscovery(id);
711     WriteBegin(context, 0);
712     WriteInt(context, err);
713     WriteEnd(context);
714     return HAL_SUCCESS;
715 }
716 
RpcP2pSetMiracastType(RpcServer * server,Context * context)717 int RpcP2pSetMiracastType(RpcServer *server, Context *context)
718 {
719     if (server == NULL || context == NULL) {
720         return HAL_FAILURE;
721     }
722     int type = 0;
723     if (ReadInt(context, &type) < 0) {
724         return HAL_FAILURE;
725     }
726     WifiErrorNo err = P2pSetMiracastType(type);
727     WriteBegin(context, 0);
728     WriteInt(context, err);
729     WriteEnd(context);
730     return HAL_SUCCESS;
731 }
732 
RpcP2pRespServerDiscovery(RpcServer * server,Context * context)733 int RpcP2pRespServerDiscovery(RpcServer *server, Context *context)
734 {
735     if (server == NULL || context == NULL) {
736         return HAL_FAILURE;
737     }
738     P2pServDiscReqInfo info;
739     if (memset_s(&info, sizeof(info), 0, sizeof(info)) != EOK) {
740         return HAL_FAILURE;
741     }
742     if (ReadInt(context, &info.freq) < 0 || ReadInt(context, &info.dialogToken) < 0 ||
743         ReadStr(context, info.mac, sizeof(info.mac)) != 0) {
744         return HAL_FAILURE;
745     }
746     int tlvsLen = ReadStr(context, NULL, 0);
747     if (tlvsLen <= 0) {
748         return HAL_FAILURE;
749     }
750     info.tlvs = (char *)calloc(tlvsLen + 1, sizeof(char));
751     if (info.tlvs == NULL) {
752         return HAL_FAILURE;
753     }
754     if (ReadStr(context, info.tlvs, tlvsLen + 1) != 0) {
755         free(info.tlvs);
756         info.tlvs = NULL;
757         return HAL_FAILURE;
758     }
759     WifiErrorNo err = P2pRespServerDiscovery(&info);
760     WriteBegin(context, 0);
761     WriteInt(context, err);
762     WriteEnd(context);
763     free(info.tlvs);
764     info.tlvs = NULL;
765     return HAL_SUCCESS;
766 }
767 
RpcP2pSetServDiscExternal(RpcServer * server,Context * context)768 int RpcP2pSetServDiscExternal(RpcServer *server, Context *context)
769 {
770     if (server == NULL || context == NULL) {
771         return HAL_FAILURE;
772     }
773     int mode = 0;
774     if (ReadInt(context, &mode) < 0) {
775         return HAL_FAILURE;
776     }
777     WifiErrorNo err = P2pSetServDiscExternal(mode);
778     WriteBegin(context, 0);
779     WriteInt(context, err);
780     WriteEnd(context);
781     return HAL_SUCCESS;
782 }
783 
RpcP2pSetPersistentReconnect(RpcServer * server,Context * context)784 int RpcP2pSetPersistentReconnect(RpcServer *server, Context *context)
785 {
786     if (server == NULL || context == NULL) {
787         return HAL_FAILURE;
788     }
789     int mode = 0;
790     if (ReadInt(context, &mode) < 0) {
791         return HAL_FAILURE;
792     }
793     WifiErrorNo err = P2pSetPersistentReconnect(mode);
794     WriteBegin(context, 0);
795     WriteInt(context, err);
796     WriteEnd(context);
797     return HAL_SUCCESS;
798 }
799 
RpcP2pGetPeer(RpcServer * server,Context * context)800 int RpcP2pGetPeer(RpcServer *server, Context *context)
801 {
802     if (server == NULL || context == NULL) {
803         return HAL_FAILURE;
804     }
805     char bssid[WIFI_BSSID_LENGTH] = {0};
806     P2pDeviceInfo peerInfo;
807     if (memset_s(&peerInfo, sizeof(peerInfo), 0, sizeof(peerInfo)) != EOK ||
808         ReadStr(context, bssid, sizeof(bssid)) != 0) {
809         return HAL_FAILURE;
810     }
811     WifiErrorNo err = P2pGetPeer(bssid, &peerInfo);
812     WriteBegin(context, 0);
813     WriteInt(context, err);
814     if (err == WIFI_HAL_SUCCESS) {
815         WriteStr(context, peerInfo.p2pDeviceAddress);
816         WriteStr(context, peerInfo.deviceName);
817         WriteStr(context, peerInfo.primaryDeviceType);
818         WriteInt(context, peerInfo.configMethods);
819         WriteInt(context, peerInfo.deviceCapabilities);
820         WriteInt(context, peerInfo.groupCapabilities);
821         WriteStr(context, peerInfo.operSsid);
822     }
823     WriteEnd(context);
824     return HAL_SUCCESS;
825 }
826 
RpcP2pGetChba0Freq(RpcServer * server,Context * context)827 int RpcP2pGetChba0Freq(RpcServer *server, Context *context)
828 {
829     if (server == NULL || context == NULL) {
830         return HAL_FAILURE;
831     }
832     int chba0Freq = 0;
833     WifiErrorNo err = P2pGetChba0Freq(&chba0Freq);
834     WriteBegin(context, 0);
835     WriteInt(context, err);
836     if (err == WIFI_HAL_SUCCESS) {
837         WriteInt(context, chba0Freq);
838     }
839     WriteEnd(context);
840     return HAL_SUCCESS;
841 }
842 
RpcP2pGetFrequencies(RpcServer * server,Context * context)843 int RpcP2pGetFrequencies(RpcServer *server, Context *context)
844 {
845     if (server == NULL || context == NULL) {
846         return HAL_FAILURE;
847     }
848     int band = 0;
849     int maxSize = 0;
850     if (ReadInt(context, &band) < 0 || ReadInt(context, &maxSize) < 0 || maxSize <= 0
851         || maxSize > WIFI_IDL_GET_MAX_BANDS) {
852         return HAL_FAILURE;
853     }
854     int *frequencies = (int *)calloc(maxSize, sizeof(int));
855     if (frequencies == NULL) {
856         return HAL_FAILURE;
857     }
858     WifiErrorNo err = P2pGetFrequencies(band, frequencies, &maxSize);
859     WriteBegin(context, 0);
860     WriteInt(context, err);
861     if (err == WIFI_HAL_SUCCESS) {
862         WriteInt(context, maxSize);
863         for (int i = 0; i < maxSize; ++i) {
864             WriteInt(context, frequencies[i]);
865         }
866     }
867     WriteEnd(context);
868     free(frequencies);
869     frequencies = NULL;
870     return HAL_SUCCESS;
871 }
872 
RpcP2pSetGroupConfig(RpcServer * server,Context * context)873 int RpcP2pSetGroupConfig(RpcServer *server, Context *context)
874 {
875     if (server == NULL || context == NULL) {
876         return HAL_FAILURE;
877     }
878     int networkId = 0;
879     int size = 0;
880     if (ReadInt(context, &networkId) < 0 || ReadInt(context, &size) < 0 || size <= 0 || size > GROUP_CONFIG_END_POS) {
881         return HAL_FAILURE;
882     }
883     P2pGroupConfig *confs = (P2pGroupConfig *)calloc(size, sizeof(P2pGroupConfig));
884     if (confs == NULL) {
885         return HAL_FAILURE;
886     }
887     int flag = 0;
888     for (int i = 0; i < size; ++i) {
889         if (ReadInt(context, (int *)&(confs[i].cfgParam)) < 0 ||
890             ReadStr(context, confs[i].cfgValue, sizeof(confs[i].cfgValue)) != 0) {
891             flag = 1;
892             break;
893         }
894     }
895     WifiErrorNo err = WIFI_HAL_FAILED;
896     if (flag == 0) {
897         err = P2pSetGroupConfig(networkId, confs, size);
898     }
899     WriteBegin(context, 0);
900     WriteInt(context, err);
901     WriteEnd(context);
902     free(confs);
903     confs = NULL;
904     return HAL_SUCCESS;
905 }
906 
RpcP2pGetGroupConfig(RpcServer * server,Context * context)907 int RpcP2pGetGroupConfig(RpcServer *server, Context *context)
908 {
909     if (server == NULL || context == NULL) {
910         return HAL_FAILURE;
911     }
912     int networkId = 0;
913     int size = 0;
914     if (ReadInt(context, &networkId) < 0 || ReadInt(context, &size) < 0 || size <= 0 || size > GROUP_CONFIG_END_POS) {
915         return HAL_FAILURE;
916     }
917     P2pGroupConfig *confs = (P2pGroupConfig *)calloc(size, sizeof(P2pGroupConfig));
918     if (confs == NULL) {
919         return HAL_FAILURE;
920     }
921     int flag = 0;
922     for (int i = 0; i < size; ++i) {
923         if (ReadInt(context, (int *)&(confs[i].cfgParam)) < 0) {
924             flag = 1;
925             break;
926         }
927     }
928     WifiErrorNo err = WIFI_HAL_FAILED;
929     if (flag == 0) {
930         err = P2pGetGroupConfig(networkId, confs, size);
931     }
932     WriteBegin(context, 0);
933     WriteInt(context, err);
934     if (err == WIFI_HAL_SUCCESS) {
935         for (int i = 0; i < size; i++) {
936             WriteStr(context, confs[i].cfgValue);
937         }
938     }
939     WriteEnd(context);
940     free(confs);
941     confs = NULL;
942     return HAL_SUCCESS;
943 }
944 
RpcP2pAddNetwork(RpcServer * server,Context * context)945 int RpcP2pAddNetwork(RpcServer *server, Context *context)
946 {
947     if (server == NULL || context == NULL) {
948         return HAL_FAILURE;
949     }
950     int networkId = 0;
951     WifiErrorNo err = P2pAddNetwork(&networkId);
952     WriteBegin(context, 0);
953     WriteInt(context, err);
954     if (err == WIFI_HAL_SUCCESS) {
955         WriteInt(context, networkId);
956     }
957     WriteEnd(context);
958     return HAL_SUCCESS;
959 }
960 
RpcP2pHid2dConnect(RpcServer * server,Context * context)961 int RpcP2pHid2dConnect(RpcServer *server, Context *context)
962 {
963     if (server == NULL || context == NULL) {
964         return HAL_FAILURE;
965     }
966 
967     Hid2dConnectInfo info;
968     if (memset_s(&info, sizeof(info), 0, sizeof(info)) != EOK) {
969         return HAL_FAILURE;
970     }
971     if (ReadStr(context, info.ssid, sizeof(info.ssid)) != 0 ||
972         ReadStr(context, info.bssid, sizeof(info.bssid)) != 0 ||
973         ReadStr(context, info.passphrase, sizeof(info.passphrase)) != 0 ||
974         ReadInt(context, &info.frequency) < 0 ||
975         ReadInt(context, &info.isLegacyGo) < 0) {
976         return HAL_FAILURE;
977     }
978     WifiErrorNo err = P2pHid2dConnect(&info);
979     WriteBegin(context, 0);
980     WriteInt(context, err);
981     WriteEnd(context);
982     return HAL_SUCCESS;
983 }
984