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