-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathp2p.c
3141 lines (2700 loc) · 80.3 KB
/
p2p.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sigma Control API DUT (station/AP)
* Copyright (c) 2010-2011, Atheros Communications, Inc.
* Copyright (c) 2011-2017, Qualcomm Atheros, Inc.
* Copyright (c) 2018-2019, The Linux Foundation
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <sys/stat.h>
#include "wpa_ctrl.h"
#include "wpa_helpers.h"
#include "miracast.h"
int run_system(struct sigma_dut *dut, const char *cmd)
{
int res;
sigma_dut_print(dut, DUT_MSG_DEBUG, "Running '%s'", cmd);
res = system(cmd);
if (res < 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Failed to execute command '%s'", cmd);
}
return res;
}
int run_system_wrapper(struct sigma_dut *dut, const char *cmd, ...)
{
va_list ap;
char *buf;
int bytes_required;
int res;
va_start(ap, cmd);
bytes_required = vsnprintf(NULL, 0, cmd, ap);
bytes_required += 1;
va_end(ap);
buf = malloc(bytes_required);
if (!buf) {
printf("ERROR!! No memory\n");
return -1;
}
va_start(ap, cmd);
vsnprintf(buf, bytes_required, cmd, ap);
va_end(ap);
res = run_system(dut, buf);
free(buf);
return res;
}
int run_iwpriv(struct sigma_dut *dut, const char *ifname, const char *cmd, ...)
{
va_list ap;
char *buf;
int bytes_required;
int res;
size_t prefix_len;
if (!ifname)
return -1;
prefix_len = strlen(dut->priv_cmd) + 1 + strlen(ifname) + 1;
va_start(ap, cmd);
bytes_required = vsnprintf(NULL, 0, cmd, ap);
bytes_required += 1;
va_end(ap);
buf = malloc(prefix_len + bytes_required);
if (!buf) {
printf("ERROR!! No memory\n");
return -1;
}
snprintf(buf, prefix_len + bytes_required, "%s %s ",
dut->priv_cmd, ifname);
va_start(ap, cmd);
vsnprintf(buf + prefix_len, bytes_required, cmd, ap);
va_end(ap);
res = run_system(dut, buf);
free(buf);
return res;
}
static int get_60g_freq(int chan)
{
int freq = 0;
switch(chan) {
case 1:
freq = 58320;
break;
case 2:
freq = 60480;
break;
case 3:
freq = 62640;
break;
case 4:
/* freq = 64800; Not supported in Sparrow 2.0 */
break;
default:
break;
}
return freq;
}
#define GO_IP_ADDR "192.168.43.1"
#define START_IP_RANGE "192.168.43.10"
#define END_IP_RANGE "192.168.43.100"
#define FLUSH_IP_ADDR "0.0.0.0"
void start_dhcp(struct sigma_dut *dut, const char *group_ifname, int go)
{
#ifdef __linux__
char buf[200];
if (go) {
snprintf(buf, sizeof(buf), "ifconfig %s %s", group_ifname,
GO_IP_ADDR);
run_system(dut, buf);
#ifdef ANDROID
snprintf(buf, sizeof(buf),
"/system/bin/dnsmasq -x /data/dnsmasq.pid --no-resolv --no-poll --dhcp-range=%s,%s,1h",
START_IP_RANGE, END_IP_RANGE);
#else /* ANDROID */
run_system(dut, "killall dnsmasq");
snprintf(buf, sizeof(buf),
"dnsmasq --no-resolv --no-poll --port=5353 --dhcp-range=%s,%s,1h",
START_IP_RANGE, END_IP_RANGE);
#endif /* ANDROID */
} else {
#ifdef ANDROID
if (access("/system/bin/dhcpcd", F_OK) != -1) {
snprintf(buf, sizeof(buf), "/system/bin/dhcpcd -KL %s",
group_ifname);
} else if (access("/system/bin/dhcptool", F_OK) != -1) {
snprintf(buf, sizeof(buf), "/system/bin/dhcptool %s",
group_ifname);
} else if (access("/vendor/bin/dhcpcd", F_OK) != -1) {
snprintf(buf, sizeof(buf), "/vendor/bin/dhcpcd %s",
group_ifname);
} else if (access("/vendor/bin/dhcptool", F_OK) != -1) {
snprintf(buf, sizeof(buf), "/vendor/bin/dhcptool %s",
group_ifname);
} else {
sigma_dut_print(dut, DUT_MSG_ERROR,
"DHCP client program missing");
return;
}
#else /* ANDROID */
snprintf(buf, sizeof(buf),
"dhclient -nw -pf /var/run/dhclient-%s.pid %s",
group_ifname, group_ifname);
#endif /* ANDROID */
}
run_system(dut, buf);
#endif /* __linux__ */
}
void stop_dhcp(struct sigma_dut *dut, const char *group_ifname, int go)
{
#ifdef __linux__
char path[128];
char buf[200];
struct stat s;
if (go) {
snprintf(path, sizeof(path), "/data/dnsmasq.pid");
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Kill previous DHCP server: %s", buf);
} else {
#ifdef ANDROID
if (access("/system/bin/dhcpcd", F_OK) != -1) {
snprintf(path, sizeof(path),
"/data/misc/dhcp/dhcpcd-%s.pid", group_ifname);
} else {
/*
* dhcptool terminates as soon as IP is
* assigned/registered using ioctls, no need to kill it
* explicitly.
*/
sigma_dut_print(dut, DUT_MSG_ERROR,
"No active DHCP client program");
return;
}
snprintf(path, sizeof(path), "/data/misc/dhcp/dhcpcd-%s.pid",
group_ifname);
#else /* ANDROID */
snprintf(path, sizeof(path), "/var/run/dhclient-%s.pid",
group_ifname);
#endif /* ANDROID */
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Kill previous DHCP client: %s", buf);
}
if (stat(path, &s) == 0) {
snprintf(buf, sizeof(buf), "kill `cat %s`", path);
run_system(dut, buf);
unlink(path);
sleep(1);
}
snprintf(buf, sizeof(buf), "ip address flush dev %s", group_ifname);
run_system(dut, buf);
snprintf(buf, sizeof(buf), "ifconfig %s %s",
group_ifname, FLUSH_IP_ADDR);
sigma_dut_print(dut, DUT_MSG_DEBUG, "Clear IP address: %s", buf);
run_system(dut, buf);
#endif /* __linux__ */
}
static int stop_event_rx = 0;
#ifdef __linux__
void stop_event_thread()
{
stop_event_rx = 1;
printf("sigma_dut dhcp terminating\n");
}
#endif /* __linux__ */
static void * wpa_event_recv(void *ptr)
{
struct sigma_dut *dut = ptr;
struct wpa_ctrl *ctrl;
char buf[4096];
char *pos, *gtype, *p2p_group_ifname = NULL;
int fd, ret, i;
int go = 0;
fd_set rfd;
struct timeval tv;
size_t len;
const char *events[] = {
"P2P-GROUP-STARTED",
"P2P-GROUP-REMOVED",
NULL
};
ctrl = open_wpa_mon(dut->p2p_ifname);
if (!ctrl) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open wpa_supplicant monitor connection");
return NULL;
}
for (i = 0; events[i]; i++) {
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Waiting for wpa_cli event: %s", events[i]);
}
fd = wpa_ctrl_get_fd(ctrl);
if (fd < 0) {
wpa_ctrl_detach(ctrl);
wpa_ctrl_close(ctrl);
return NULL;
}
while (!stop_event_rx) {
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select(fd + 1, &rfd, NULL, NULL, &tv);
if (ret == 0)
continue;
if (ret < 0) {
sigma_dut_print(dut, DUT_MSG_INFO, "select: %s",
strerror(errno));
usleep(100000);
continue;
}
len = sizeof(buf);
if (wpa_ctrl_recv(ctrl, buf, &len) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failure while waiting for events");
continue;
}
ret = 0;
pos = strchr(buf, '>');
if (pos) {
for (i = 0; events[i]; i++) {
if (strncmp(pos + 1, events[i],
strlen(events[i])) == 0) {
ret = 1;
break; /* Event found */
}
}
}
if (!ret)
continue;
if (strstr(buf, "P2P-GROUP-")) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "Group event '%s'",
buf);
p2p_group_ifname = strchr(buf, ' ');
if (!p2p_group_ifname)
continue;
p2p_group_ifname++;
pos = strchr(p2p_group_ifname, ' ');
if (!pos)
continue;
*pos++ = '\0';
gtype = pos;
pos = strchr(gtype, ' ');
if (!pos)
continue;
*pos++ = '\0';
go = strcmp(gtype, "GO") == 0;
}
if (strstr(buf, "P2P-GROUP-STARTED")) {
start_dhcp(dut, p2p_group_ifname, go);
} else if (strstr(buf, "P2P-GROUP-REMOVED")) {
stop_dhcp(dut, p2p_group_ifname, go);
go = 0;
}
}
/* terminate DHCP server, if runnin! */
if (go)
stop_dhcp(dut, p2p_group_ifname, go);
wpa_ctrl_detach(ctrl);
wpa_ctrl_close(ctrl);
pthread_exit(0);
return NULL;
}
void p2p_create_event_thread(struct sigma_dut *dut)
{
static pthread_t event_thread;
/* create event thread */
pthread_create(&event_thread, NULL, &wpa_event_recv, (void *) dut);
}
static int p2p_group_add(struct sigma_dut *dut, const char *ifname,
int go, const char *grpid, const char *ssid,
const char *peer_mac)
{
struct wfa_cs_p2p_group *grp;
if (go)
dut->go = 1;
else
dut->p2p_client = 1;
grp = malloc(sizeof(*grp));
if (grp == NULL)
return -1;
memset(grp, 0, sizeof(*grp));
strlcpy(grp->ifname, ifname, IFNAMSIZ);
grp->go = go;
strlcpy(grp->grpid, grpid, P2P_GRP_ID_LEN);
strlcpy(grp->ssid, ssid, sizeof(grp->ssid));
if (peer_mac)
strlcpy(grp->peer_mac, peer_mac, sizeof(grp->peer_mac));
grp->next = dut->groups;
dut->groups = grp;
return 0;
}
static int p2p_group_remove(struct sigma_dut *dut, const char *grpid)
{
struct wfa_cs_p2p_group *grp, *prev;
prev = NULL;
grp = dut->groups;
while (grp) {
if (strcmp(grpid, grp->grpid) == 0) {
if (prev)
prev->next = grp->next;
else
dut->groups = grp->next;
free(grp);
return 0;
}
prev = grp;
grp = grp->next;
}
return -1;
}
static struct wfa_cs_p2p_group * p2p_group_get(struct sigma_dut *dut,
const char *grpid)
{
struct wfa_cs_p2p_group *grp;
char buf[1000], buf2[4096], *ifname, *pos;
char go_dev_addr[50];
char ssid[33];
for (grp = dut->groups; grp; grp = grp->next) {
if (strcmp(grpid, grp->grpid) == 0)
return grp;
}
/*
* No group found based on group id. As a workaround for GO Negotiation
* responder case where we do not store group id, try to find an active
* group that matches with the requested group id.
*/
pos = strchr(grpid, ' ');
if (pos == NULL)
return NULL;
if (pos - grpid >= (int) sizeof(go_dev_addr))
return NULL;
memcpy(go_dev_addr, grpid, pos - grpid);
go_dev_addr[pos - grpid] = '\0';
strlcpy(ssid, pos + 1, sizeof(ssid));
ssid[sizeof(ssid) - 1] = '\0';
printf("Trying to find suitable interface for group: go_dev_addr='%s' "
"grpid='%s'\n", go_dev_addr, grpid);
if (wpa_command_resp(get_main_ifname(dut), "INTERFACES",
buf, sizeof(buf)) < 0)
return NULL;
ifname = buf;
while (ifname && *ifname) {
int add = 0;
int go = 0;
pos = strchr(ifname, '\n');
if (pos)
*pos++ = '\0';
printf("Considering interface '%s' for group\n", ifname);
if (wpa_command_resp(ifname, "STATUS", buf2, sizeof(buf2)) ==
0) {
if (strstr(buf2, ssid)) {
printf("Selected interface '%s' based on "
"STATUS\n", ifname);
add = 1;
}
if (strstr(buf2, "P2P GO"))
go = 1;
}
if (wpa_command_resp(ifname, "LIST_NETWORKS", buf2,
sizeof(buf2)) == 0) {
char *line, *end;
line = buf2;
while (line && *line) {
end = strchr(line, ' ');
if (end)
*end++ = '\0';
if (strstr(line, ssid) &&
strstr(line, "[CURRENT]")) {
printf("Selected interface '%s' "
"based on LIST_NETWORKS\n",
ifname);
add = 1;
break;
}
line = end;
}
}
if (add) {
p2p_group_add(dut, ifname, go, grpid, ssid, NULL);
return dut->groups;
}
ifname = pos;
}
return NULL;
}
const char * get_p2p_group_ifname(struct sigma_dut *dut, const char *ifname)
{
char *iface, *pos;
static char buf[1000];
/* Try to find a suitable group interface */
if (wpa_command_resp(get_main_ifname(dut), "INTERFACES",
buf, sizeof(buf)) < 0)
return ifname;
iface = buf;
while (iface && *iface) {
pos = strchr(iface, '\n');
if (pos)
*pos++ = '\0';
if (memcmp(iface, "p2p-", 4) == 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Considering interface '%s' for IP address",
iface);
return iface;
}
iface = pos;
}
return ifname;
}
static const char * get_group_ifname(struct sigma_dut *dut, const char *ifname)
{
static char buf[1000];
char *iface, *pos;
char state[100];
if (dut->groups) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "%s: Use group interface "
"%s instead of main interface %s",
__func__, dut->groups->ifname, ifname);
return dut->groups->ifname;
}
/* Try to find a suitable group interface */
if (wpa_command_resp(get_main_ifname(dut), "INTERFACES",
buf, sizeof(buf)) < 0)
return ifname;
iface = buf;
while (iface && *iface) {
pos = strchr(iface, '\n');
if (pos)
*pos++ = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG, "Considering interface "
"'%s' for IP address", iface);
if (get_wpa_status(iface, "wpa_state", state, sizeof(state)) ==
0 && strcmp(state, "COMPLETED") == 0)
return iface;
iface = pos;
}
return ifname;
}
static int p2p_peer_known(const char *ifname, const char *peer, int full)
{
char buf[4096];
snprintf(buf, sizeof(buf), "P2P_PEER %s", peer);
if (wpa_command_resp(ifname, buf, buf, sizeof(buf)) < 0)
return 0;
if (strncasecmp(buf, peer, strlen(peer)) != 0)
return 0;
if (!full)
return 1;
return strstr(buf, "[PROBE_REQ_ONLY]") == NULL ? 1 : 0;
}
int p2p_discover_peer(struct sigma_dut *dut, const char *ifname,
const char *peer, int full)
{
unsigned int count;
if (p2p_peer_known(ifname, peer, full))
return 0;
printf("Peer not yet discovered - start discovery\n");
if (wpa_command(ifname, "P2P_FIND type=progressive") < 0) {
printf("Failed to start discovery\n");
return -1;
}
count = 0;
while (count < dut->default_timeout) {
count++;
sleep(1);
if (p2p_peer_known(ifname, peer, full)) {
printf("Peer discovered - return to previous state\n");
switch (dut->p2p_mode) {
case P2P_IDLE:
wpa_command(ifname, "P2P_STOP_FIND");
break;
case P2P_DISCOVER:
/* Already running discovery */
break;
case P2P_LISTEN:
wpa_command(ifname, "P2P_LISTEN");
break;
case P2P_DISABLE:
printf("Invalid state - P2P was disabled?!\n");
break;
}
return 0;
}
}
printf("Peer discovery timed out - peer not discovered\n");
wpa_command(ifname, "P2P_STOP_FIND");
return -1;
}
static void add_dummy_services(const char *intf)
{
wpa_command(intf, "P2P_SERVICE_ADD bonjour 0b5f6166706f766572746370c00c000c01 074578616d706c65c027");
wpa_command(intf, "P2P_SERVICE_ADD bonjour 076578616d706c650b5f6166706f766572746370c00c001001 00");
wpa_command(intf, "P2P_SERVICE_ADD bonjour 045f697070c00c000c01 094d795072696e746572c027");
wpa_command(intf, "P2P_SERVICE_ADD bonjour 096d797072696e746572045f697070c00c001001 09747874766572733d311a70646c3d6170706c69636174696f6e2f706f7374736372797074");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:1122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:5566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:6859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:1859dede-8574-59ab-9332-123456789012::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:1566d33e-9774-09ab-4822-333456785632::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:2122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:1566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:1859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:2859dede-8574-59ab-9332-123456789012::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:2566d33e-9774-09ab-4822-333456785632::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:3122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:2566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:2859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:3859dede-8574-59ab-9332-123456789012::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:3566d33e-9774-09ab-4822-333456785632::upnp:rootdevice");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:4122de4e-8574-59ab-9322-333456789044::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:3566d33e-9774-09ab-4822-333456785632::urn:schemas-upnp-org:service:ContentDirectory:2");
wpa_command(intf, "P2P_SERVICE_ADD upnp 10 uuid:3859dede-8574-59ab-9332-123456789012::urn:schemas-upnp-org:device:InternetGatewayDevice:1");
}
void disconnect_station(struct sigma_dut *dut)
{
wpa_command(get_station_ifname(dut), "DISCONNECT");
remove_wpa_networks(get_station_ifname(dut));
dut->infra_ssid[0] = '\0';
#ifdef __linux__
{
char path[128];
char buf[200];
struct stat s;
snprintf(path, sizeof(path), "/var/run/dhclient-%s.pid",
get_station_ifname(dut));
if (stat(path, &s) == 0) {
snprintf(buf, sizeof(buf),
"kill `cat %s`", path);
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Kill previous DHCP client: %s", buf);
run_system(dut, buf);
unlink(path);
}
snprintf(buf, sizeof(buf),
"ifconfig %s 0.0.0.0", get_station_ifname(dut));
sigma_dut_print(dut, DUT_MSG_DEBUG,
"Clear infrastructure station IP address: %s",
buf);
run_system(dut, buf);
}
#endif /* __linux__ */
}
static enum sigma_cmd_result
cmd_sta_get_p2p_dev_address(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *intf = get_param(cmd, "interface");
char buf[100], resp[200];
start_sta_mode(dut);
if (get_wpa_status(intf, "p2p_device_address", buf, sizeof(buf)) < 0) {
send_resp(dut, conn, SIGMA_ERROR, NULL);
return 0;
}
snprintf(resp, sizeof(resp), "DevID,%s", buf);
send_resp(dut, conn, SIGMA_COMPLETE, resp);
return 0;
}
static enum sigma_cmd_result cmd_sta_set_p2p(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *intf = get_p2p_ifname(dut, get_param(cmd, "Interface"));
char buf[256];
const char *val;
const char *noa_dur, *noa_int, *noa_count;
const char *ext_listen_int, *ext_listen_period;
val = get_param(cmd, "LISTEN_CHN");
if (val) {
dut->listen_chn = atoi(val);
if (dut->listen_chn == 2) {
/* social channel 2 on 60 GHz band */
snprintf(buf, sizeof(buf),
"P2P_SET listen_channel 2 180");
} else {
/* social channels 1/6/11 on 2.4 GHz band */
snprintf(buf, sizeof(buf), "P2P_SET listen_channel %d",
dut->listen_chn);
}
if (wpa_command(intf, buf) < 0)
return -2;
}
ext_listen_int = get_param(cmd, "Ext_Listen_Time_Interval");
ext_listen_period = get_param(cmd, "Ext_Listen_Time_Period");
if (ext_listen_int || ext_listen_period) {
if (!ext_listen_int || !ext_listen_period) {
sigma_dut_print(dut, DUT_MSG_INFO, "Only one "
"ext_listen_time parameter included; "
"both are needed");
return -1;
}
snprintf(buf, sizeof(buf), "P2P_EXT_LISTEN %d %d",
atoi(ext_listen_period),
atoi(ext_listen_int));
if (wpa_command(intf, buf) < 0)
return -2;
}
val = get_param(cmd, "P2P_MODE");
if (val) {
if (strcasecmp(val, "Listen") == 0) {
wpa_command(intf, "P2P_SET disabled 0");
if (wpa_command(intf, "P2P_LISTEN") < 0)
return -2;
dut->p2p_mode = P2P_LISTEN;
} else if (strcasecmp(val, "Discover") == 0) {
wpa_command(intf, "P2P_SET disabled 0");
if (wpa_command(intf, "P2P_FIND") < 0)
return -2;
dut->p2p_mode = P2P_DISCOVER;
} else if (strcasecmp(val, "Idle") == 0) {
wpa_command(intf, "P2P_SET disabled 0");
if (wpa_command(intf, "P2P_STOP_FIND") < 0)
return -2;
dut->p2p_mode = P2P_IDLE;
} else if (strcasecmp(val, "Disable") == 0) {
if (wpa_command(intf, "P2P_SET disabled 1") < 0)
return -2;
dut->p2p_mode = P2P_DISABLE;
} else
return -1;
}
val = get_param(cmd, "PERSISTENT");
if (val) {
dut->persistent = atoi(val);
}
val = get_param(cmd, "INTRA_BSS");
if (val) {
int intra_bss = atoi(val);
/* TODO: add support for this */
if (!intra_bss) {
sigma_dut_print(dut, DUT_MSG_INFO, "Disabling of "
"intra-BSS bridging not supported");
return -1;
}
dut->intra_bss = intra_bss;
}
/* NoA is not applicable for 60 GHz */
if (dut->program != PROGRAM_60GHZ) {
noa_dur = get_param(cmd, "NoA_duration");
noa_int = get_param(cmd, "NoA_Interval");
noa_count = get_param(cmd, "NoA_Count");
if (noa_dur)
dut->noa_duration = atoi(noa_dur);
if (noa_int)
dut->noa_interval = atoi(noa_int);
if (noa_count)
dut->noa_count = atoi(noa_count);
if (noa_dur || noa_int || noa_count) {
int start = 0;
const char *ifname;
snprintf(buf, sizeof(buf),
"DRIVER P2P_SET_NOA %d %d %d %d",
dut->noa_count, start,
dut->noa_duration, dut->noa_interval);
ifname = get_group_ifname(dut, intf);
sigma_dut_print(dut, DUT_MSG_INFO,
"Set GO NoA for interface %s", ifname);
if (wpa_command(ifname, buf) == 0)
goto noa_done;
if (dut->noa_count == 0 && dut->noa_duration == 0)
start = 0;
else if (dut->noa_duration > 102) /* likely non-periodic
* NoA */
start = 50;
else
start = 102 - dut->noa_duration;
snprintf(buf, sizeof(buf), "P2P_SET noa %d,%d,%d",
dut->noa_count, start,
dut->noa_duration);
if (wpa_command(ifname, buf) == 0)
goto noa_done;
snprintf(buf, sizeof(buf), "P2P_SET_NOA %d %d %d %d",
dut->noa_count, start, dut->noa_duration,
dut->noa_interval);
if (wcn_driver_cmd(intf, buf) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Use of NoA as GO not supported");
return 0;
}
}
}
noa_done:
val = get_param(cmd, "Concurrency");
if (val) {
/* TODO */
}
val = get_param(cmd, "P2PInvitation");
if (val) {
/* TODO */
}
val = get_param(cmd, "BCN_INT");
if (val) {
/* TODO */
}
val = get_param(cmd, "Discoverability");
if (val) {
snprintf(buf, sizeof(buf), "P2P_SET discoverability %d",
atoi(val));
if (wpa_command(intf, buf) < 0)
return -2;
}
val = get_param(cmd, "GenNewDeviceAddr");
if (val && atoi(val) == 1) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Generate new P2P Device Address");
if (wpa_command(intf, "NEW_RANDOM_MAC_ADDRESS") < 0)
return ERROR_SEND_STATUS;
}
val = get_param(cmd, "PASN");
if (val && strcasecmp(val, "Enable") == 0) {
/* Support Wi-Fi Direct R2 capabilities */
sigma_dut_print(dut, DUT_MSG_INFO, "PASN Enable");
val = get_param(cmd, "PASN_Unsupported_DH_Group");
if (val) {
if (atoi(val) == 20)
dut->pasn_type &= ~0xc;
else if (atoi(val) == 19)
dut->pasn_type &= ~0x3;
}
snprintf(buf, sizeof(buf), "P2P_SET pasn_type %d",
dut->pasn_type);
if (wpa_command(intf, buf) < 0)
return ERROR_SEND_STATUS;
}
val = get_param(cmd, "Service_Discovery");
if (val) {
int sd = atoi(val);
if (sd) {
wpa_command(intf, "P2P_SERVICE_FLUSH");
if (sd == 2)
wpa_command(intf, "P2P_SET force_long_sd 1");
/*
* Set up some dummy service to create a large SD
* response that requires fragmentation.
*/
add_dummy_services(intf);
} else {
wpa_command(intf, "P2P_SERVICE_FLUSH");
}
}
val = get_param(cmd, "CrossConnection");
if (val) {
if (atoi(val)) {
if (wpa_command(intf, "P2P_SET cross_connect 1") < 0)
return -2;
} else {
if (wpa_command(intf, "P2P_SET cross_connect 0") < 0)
return -2;
}
}
val = get_param(cmd, "P2PManaged");
if (val) {
if (atoi(val)) {
send_resp(dut, conn, SIGMA_INVALID, "ErrorCode,"
"P2P Managed functionality not supported");
return 0;
}
}
val = get_param(cmd, "GO_APSD");
if (val) {
if (atoi(val)) {
if (wpa_command(intf, "P2P_SET go_apsd 1") < 0)
return -2;
} else {
if (wpa_command(intf, "P2P_SET go_apsd 0") < 0)
return -2;
}
}
val = get_param(cmd, "UnsyncServDsc");
if (val) {
if (strcasecmp(val, "On") == 0) {
/* Support USD functionality */
dut->usd_enabled = true;
sigma_dut_print(dut, DUT_MSG_INFO,
"Unsynchronized Service Discovery ON");
} else {
dut->usd_enabled = false;
sigma_dut_print(dut, DUT_MSG_INFO,
"Unsynchronized Service Discovery OFF");
}
}
val = get_param(cmd, "WFDR2Capabilities");
if (val) {
if (strcasecmp(val, "On") == 0) {
/* Support Wi-Fi Direct R2 capabilities */
dut->p2p_r2_capable = true;
sigma_dut_print(dut, DUT_MSG_INFO,
"Wi-Fi Direct R2 Capabilities ON");
snprintf(buf, sizeof(buf),
"P2P_SET chan_switch_req_enable 1");
if (wpa_command(intf, buf) < 0)
return ERROR_SEND_STATUS;
wpa_command(intf, "P2P_SET reginfo 2");
} else {
dut->p2p_r2_capable = false;
sigma_dut_print(dut, DUT_MSG_INFO,
"Wi-Fi Direct R2 Capabilities OFF");
}
}
return 1;
}
static enum sigma_cmd_result
cmd_sta_start_autonomous_go(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *intf = get_p2p_ifname(dut, get_param(cmd, "Interface"));
const char *oper_chn = get_param(cmd, "OPER_CHN");
const char *ssid_param = get_param(cmd, "SSID");
#ifdef MIRACAST
const char *rtsp = get_param(cmd, "RTSP");
#endif /* MIRACAST */
int freq, chan, res;
char buf[256], grpid[100], resp[200];
struct wpa_ctrl *ctrl;
char *ifname, *gtype, *pos, *ssid, bssid[20];
char *go_dev_addr;
if (oper_chn == NULL)
return -1;
chan = atoi(oper_chn);
if (dut->program == PROGRAM_60GHZ) {
freq = get_60g_freq(chan);
if (freq == 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Invalid channel: %d", chan);
return -1;
}
} else if (chan >= 1 && chan <= 13)
freq = 2407 + chan * 5;
else if (chan == 14)
freq = 2484;
else if (chan >= 36 && chan <= 165)
freq = 5000 + chan * 5;