1# P2P+NFC tests
2# Copyright (c) 2013, Qualcomm Atheros, Inc.
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7from remotehost import remote_compatible
8import time
9import logging
10logger = logging.getLogger(__name__)
11
12import hwsim_utils
13from utils import alloc_fail
14
15grpform_events = ["P2P-GROUP-STARTED",
16                  "P2P-GO-NEG-FAILURE",
17                  "P2P-GROUP-FORMATION-FAILURE",
18                  "WPS-PIN-NEEDED",
19                  "WPS-M2D",
20                  "WPS-FAIL"]
21
22def set_ip_addr_info(dev):
23    dev.global_request("SET ip_addr_go 192.168.42.1")
24    dev.global_request("SET ip_addr_mask 255.255.255.0")
25    dev.global_request("SET ip_addr_start 192.168.42.100")
26    dev.global_request("SET ip_addr_end 192.168.42.199")
27
28def check_ip_addr(res):
29    if 'ip_addr' not in res:
30        raise Exception("Did not receive IP address from GO")
31    if '192.168.42.' not in res['ip_addr']:
32        raise Exception("Unexpected IP address received from GO")
33    if 'ip_mask' not in res:
34        raise Exception("Did not receive IP address mask from GO")
35    if '255.255.255.' not in res['ip_mask']:
36        raise Exception("Unexpected IP address mask received from GO")
37    if 'go_ip_addr' not in res:
38        raise Exception("Did not receive GO IP address from GO")
39    if '192.168.42.' not in res['go_ip_addr']:
40        raise Exception("Unexpected GO IP address received from GO")
41
42def test_nfc_p2p_go_neg(dev):
43    """NFC connection handover to form a new P2P group (initiator becomes GO)"""
44    try:
45        _test_nfc_p2p_go_neg(dev)
46    finally:
47        dev[0].global_request("SET p2p_go_intent 7")
48
49def _test_nfc_p2p_go_neg(dev):
50    set_ip_addr_info(dev[0])
51    ip = dev[0].p2pdev_request("GET ip_addr_go")
52    if ip != "192.168.42.1":
53        raise Exception("Unexpected ip_addr_go returned: " + ip)
54    dev[0].global_request("SET p2p_go_intent 10")
55    logger.info("Perform NFC connection handover")
56    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
57    if "FAIL" in req:
58        raise Exception("Failed to generate NFC connection handover request")
59    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
60    if "FAIL" in sel:
61        raise Exception("Failed to generate NFC connection handover select")
62    dev[0].dump_monitor()
63    dev[1].dump_monitor()
64    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
65    if "FAIL" in res:
66        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
67    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
68    if "FAIL" in res:
69        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
70
71    ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
72                                   "P2P-GO-NEG-FAILURE",
73                                   "P2P-GROUP-FORMATION-FAILURE",
74                                   "WPS-PIN-NEEDED"], timeout=15)
75    if ev is None:
76        raise Exception("Group formation timed out")
77    res0 = dev[0].group_form_result(ev)
78
79    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
80                                   "P2P-GO-NEG-FAILURE",
81                                   "P2P-GROUP-FORMATION-FAILURE",
82                                   "WPS-PIN-NEEDED"], timeout=1)
83    if ev is None:
84        raise Exception("Group formation timed out")
85    res1 = dev[1].group_form_result(ev)
86    logger.info("Group formed")
87
88    if res1['role'] != 'client' or res0['role'] != 'GO':
89        raise Exception("Unexpected roles negotiated")
90    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
91    check_ip_addr(res1)
92
93def test_nfc_p2p_go_neg_ip_pool_oom(dev):
94    """NFC connection handover to form a new P2P group and IP pool OOM"""
95    try:
96        _test_nfc_p2p_go_neg_ip_pool_oom(dev)
97    finally:
98        dev[0].global_request("SET p2p_go_intent 7")
99
100def _test_nfc_p2p_go_neg_ip_pool_oom(dev):
101    set_ip_addr_info(dev[0])
102    ip = dev[0].p2pdev_request("GET ip_addr_go")
103    if ip != "192.168.42.1":
104        raise Exception("Unexpected ip_addr_go returned: " + ip)
105    dev[0].global_request("SET p2p_go_intent 10")
106    logger.info("Perform NFC connection handover")
107    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
108    if "FAIL" in req:
109        raise Exception("Failed to generate NFC connection handover request")
110    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
111    if "FAIL" in sel:
112        raise Exception("Failed to generate NFC connection handover select")
113    dev[0].dump_monitor()
114    dev[1].dump_monitor()
115
116    with alloc_fail(dev[0], 1, "bitfield_alloc;wpa_init"):
117        res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
118        if "FAIL" in res:
119            raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
120        res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
121        if "FAIL" in res:
122            raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
123
124        ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
125                                       "P2P-GO-NEG-FAILURE",
126                                       "P2P-GROUP-FORMATION-FAILURE",
127                                       "WPS-PIN-NEEDED"], timeout=15)
128        if ev is None:
129            raise Exception("Group formation timed out")
130        res0 = dev[0].group_form_result(ev)
131
132    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
133                                   "P2P-GO-NEG-FAILURE",
134                                   "P2P-GROUP-FORMATION-FAILURE",
135                                   "WPS-PIN-NEEDED"], timeout=1)
136    if ev is None:
137        raise Exception("Group formation timed out")
138    res1 = dev[1].group_form_result(ev)
139    logger.info("Group formed")
140
141    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
142    if 'ip_addr' in res1:
143        raise Exception("Unexpectedly received IP address from GO")
144
145def test_nfc_p2p_go_neg_reverse(dev):
146    """NFC connection handover to form a new P2P group (responder becomes GO)"""
147    try:
148        _test_nfc_p2p_go_neg_reverse(dev)
149    finally:
150        dev[0].global_request("SET p2p_go_intent 7")
151
152def _test_nfc_p2p_go_neg_reverse(dev):
153    set_ip_addr_info(dev[1])
154    dev[0].global_request("SET p2p_go_intent 3")
155    logger.info("Perform NFC connection handover")
156    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
157    if "FAIL" in req:
158        raise Exception("Failed to generate NFC connection handover request")
159    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
160    if "FAIL" in sel:
161        raise Exception("Failed to generate NFC connection handover select")
162    dev[0].dump_monitor()
163    dev[1].dump_monitor()
164    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
165    if "FAIL" in res:
166        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
167    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
168    if "FAIL" in res:
169        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
170
171    ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
172                                   "P2P-GO-NEG-FAILURE",
173                                   "P2P-GROUP-FORMATION-FAILURE",
174                                   "WPS-PIN-NEEDED"], timeout=15)
175    if ev is None:
176        raise Exception("Group formation timed out")
177    res0 = dev[0].group_form_result(ev)
178
179    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
180                                   "P2P-GO-NEG-FAILURE",
181                                   "P2P-GROUP-FORMATION-FAILURE",
182                                   "WPS-PIN-NEEDED"], timeout=1)
183    if ev is None:
184        raise Exception("Group formation timed out")
185    res1 = dev[1].group_form_result(ev)
186    logger.info("Group formed")
187
188    if res0['role'] != 'client' or res1['role'] != 'GO':
189        raise Exception("Unexpected roles negotiated")
190    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
191    check_ip_addr(res0)
192
193def test_nfc_p2p_initiator_go(dev):
194    """NFC connection handover with initiator already GO"""
195    set_ip_addr_info(dev[0])
196    logger.info("Start autonomous GO")
197    dev[0].p2p_start_go()
198    logger.info("Perform NFC connection handover")
199    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
200    if "FAIL" in req:
201        raise Exception("Failed to generate NFC connection handover request")
202    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
203    if "FAIL" in sel:
204        raise Exception("Failed to generate NFC connection handover select")
205    dev[0].dump_monitor()
206    dev[1].dump_monitor()
207    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
208    if "FAIL" in res:
209        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
210    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
211    if "FAIL" in res:
212        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
213
214    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
215    if ev is None:
216        raise Exception("Connection to the group timed out")
217    res1 = dev[1].group_form_result(ev)
218    if res1['result'] != 'success':
219        raise Exception("Unexpected connection failure")
220    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
221    check_ip_addr(res1)
222
223def test_nfc_p2p_responder_go(dev):
224    """NFC connection handover with responder already GO"""
225    set_ip_addr_info(dev[1])
226    logger.info("Start autonomous GO")
227    dev[1].p2p_start_go()
228    logger.info("Perform NFC connection handover")
229    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
230    if "FAIL" in req:
231        raise Exception("Failed to generate NFC connection handover request")
232    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
233    if "FAIL" in sel:
234        raise Exception("Failed to generate NFC connection handover select")
235    dev[0].dump_monitor()
236    dev[1].dump_monitor()
237    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
238    if "FAIL" in res:
239        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
240    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
241    if "FAIL" in res:
242        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
243
244    ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
245    if ev is None:
246        raise Exception("Connection to the group timed out")
247    res0 = dev[0].group_form_result(ev)
248    if res0['result'] != 'success':
249        raise Exception("Unexpected connection failure")
250    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
251    check_ip_addr(res0)
252
253def test_nfc_p2p_both_go(dev):
254    """NFC connection handover with both devices already GOs"""
255    set_ip_addr_info(dev[0])
256    set_ip_addr_info(dev[1])
257    logger.info("Start autonomous GOs")
258    dev[0].p2p_start_go()
259    dev[1].p2p_start_go()
260    logger.info("Perform NFC connection handover")
261    req = dev[0].request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
262    if "FAIL" in req:
263        raise Exception("Failed to generate NFC connection handover request")
264    sel = dev[1].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
265    if "FAIL" in sel:
266        raise Exception("Failed to generate NFC connection handover select")
267    dev[0].dump_monitor()
268    dev[1].dump_monitor()
269    res = dev[1].request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
270    if "FAIL" in res:
271        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
272    res = dev[0].request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
273    if "FAIL" in res:
274        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
275
276    ev = dev[0].wait_event(["P2P-NFC-BOTH-GO"], timeout=15)
277    if ev is None:
278        raise Exception("Time out waiting for P2P-NFC-BOTH-GO (dev0)")
279    ev = dev[1].wait_event(["P2P-NFC-BOTH-GO"], timeout=1)
280    if ev is None:
281        raise Exception("Time out waiting for P2P-NFC-BOTH-GO (dev1)")
282    dev[0].remove_group()
283    dev[1].remove_group()
284
285def test_nfc_p2p_client(dev):
286    """NFC connection handover when one device is P2P client"""
287    logger.info("Start autonomous GOs")
288    go_res = dev[0].p2p_start_go()
289    logger.info("Connect one device as a P2P client")
290    pin = dev[1].wps_read_pin()
291    dev[0].p2p_go_authorize_client(pin)
292    dev[1].p2p_connect_group(dev[0].p2p_dev_addr(), pin,
293                             freq=int(go_res['freq']), timeout=60)
294    logger.info("Client connected")
295    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
296
297    logger.info("NFC connection handover between P2P client and P2P device")
298    req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
299    if "FAIL" in req:
300        raise Exception("Failed to generate NFC connection handover request")
301    sel = dev[2].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
302    if "FAIL" in sel:
303        raise Exception("Failed to generate NFC connection handover select")
304    dev[1].dump_monitor()
305    dev[2].dump_monitor()
306    res = dev[2].request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
307    if "FAIL" in res:
308        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
309    res = dev[1].request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
310    if "FAIL" in res:
311        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
312
313    ev = dev[1].wait_event(["P2P-NFC-WHILE-CLIENT"], timeout=15)
314    if ev is None:
315        raise Exception("Time out waiting for P2P-NFC-WHILE-CLIENT")
316    ev = dev[2].wait_event(["P2P-NFC-PEER-CLIENT"], timeout=1)
317    if ev is None:
318        raise Exception("Time out waiting for P2P-NFC-PEER-CLIENT")
319
320    logger.info("Connect to group based on upper layer trigger")
321    pin = dev[2].wps_read_pin()
322    dev[0].p2p_go_authorize_client(pin)
323    dev[2].p2p_connect_group(dev[0].p2p_dev_addr(), pin,
324                             freq=int(go_res['freq']), timeout=60)
325    logger.info("Client connected")
326    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
327    hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
328    dev[2].remove_group()
329    dev[1].remove_group()
330    dev[0].remove_group()
331
332def test_nfc_p2p_static_handover_tagdev_client(dev):
333    """NFC static handover to form a new P2P group (NFC Tag device becomes P2P Client)"""
334    try:
335        _test_nfc_p2p_static_handover_tagdev_client(dev)
336    finally:
337        dev[0].global_request("SET p2p_go_intent 7")
338
339def _test_nfc_p2p_static_handover_tagdev_client(dev):
340    set_ip_addr_info(dev[0])
341
342    logger.info("Perform NFC connection handover")
343
344    res = dev[1].global_request("SET p2p_listen_reg_class 81")
345    res2 = dev[1].global_request("SET p2p_listen_channel 6")
346    if "FAIL" in res or "FAIL" in res2:
347        raise Exception("Could not set Listen channel")
348    pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
349    if "FAIL" in pw:
350        raise Exception("Failed to generate password token")
351    res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
352    if "FAIL" in res:
353        raise Exception("Failed to enable NFC Tag for P2P static handover")
354    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
355    if "FAIL" in sel:
356        raise Exception("Failed to generate NFC connection handover select")
357    res = dev[1].global_request("P2P_LISTEN")
358    if "FAIL" in res:
359        raise Exception("Failed to start Listen mode")
360    dev[1].dump_monitor()
361
362    dev[0].dump_monitor()
363    dev[0].global_request("SET p2p_go_intent 10")
364    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
365    if "FAIL" in res:
366        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
367
368    ev = dev[0].wait_global_event(grpform_events, timeout=15)
369    if ev is None:
370        raise Exception("Group formation timed out")
371    res0 = dev[0].group_form_result(ev)
372
373    ev = dev[1].wait_global_event(grpform_events, timeout=1)
374    if ev is None:
375        raise Exception("Group formation timed out")
376    res1 = dev[1].group_form_result(ev)
377    logger.info("Group formed")
378
379    if res1['role'] != 'client' or res0['role'] != 'GO':
380        raise Exception("Unexpected roles negotiated")
381    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
382    check_ip_addr(res1)
383
384def test_nfc_p2p_static_handover_tagdev_client_group_iface(dev):
385    """NFC static handover to form a new P2P group (NFC Tag device becomes P2P Client with group iface)"""
386    try:
387        _test_nfc_p2p_static_handover_tagdev_client_group_iface(dev)
388    finally:
389        dev[0].global_request("SET p2p_go_intent 7")
390
391def _test_nfc_p2p_static_handover_tagdev_client_group_iface(dev):
392    set_ip_addr_info(dev[0])
393
394    logger.info("Perform NFC connection handover")
395
396    res = dev[1].global_request("SET p2p_listen_reg_class 81")
397    res2 = dev[1].global_request("SET p2p_listen_channel 6")
398    if "FAIL" in res or "FAIL" in res2:
399        raise Exception("Could not set Listen channel")
400    pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
401    if "FAIL" in pw:
402        raise Exception("Failed to generate password token")
403    dev[1].global_request("SET p2p_no_group_iface 0")
404    res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
405    if "FAIL" in res:
406        raise Exception("Failed to enable NFC Tag for P2P static handover")
407    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
408    if "FAIL" in sel:
409        raise Exception("Failed to generate NFC connection handover select")
410    res = dev[1].global_request("P2P_LISTEN")
411    if "FAIL" in res:
412        raise Exception("Failed to start Listen mode")
413    dev[1].dump_monitor()
414
415    dev[0].dump_monitor()
416    dev[0].global_request("SET p2p_go_intent 10")
417    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
418    if "FAIL" in res:
419        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
420
421    ev = dev[0].wait_global_event(grpform_events, timeout=15)
422    if ev is None:
423        raise Exception("Group formation timed out")
424    res0 = dev[0].group_form_result(ev)
425
426    ev = dev[1].wait_global_event(grpform_events, timeout=1)
427    if ev is None:
428        raise Exception("Group formation timed out")
429    res1 = dev[1].group_form_result(ev)
430    logger.info("Group formed")
431
432    if res1['role'] != 'client' or res0['role'] != 'GO':
433        raise Exception("Unexpected roles negotiated")
434    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
435    check_ip_addr(res1)
436
437def test_nfc_p2p_static_handover_tagdev_go(dev):
438    """NFC static handover to form a new P2P group (NFC Tag device becomes GO)"""
439    try:
440        _test_nfc_p2p_static_handover_tagdev_go(dev)
441    finally:
442        dev[0].global_request("SET p2p_go_intent 7")
443
444def _test_nfc_p2p_static_handover_tagdev_go(dev):
445    set_ip_addr_info(dev[1])
446
447    logger.info("Perform NFC connection handover")
448
449    res = dev[1].global_request("SET p2p_listen_reg_class 81")
450    res2 = dev[1].global_request("SET p2p_listen_channel 6")
451    if "FAIL" in res or "FAIL" in res2:
452        raise Exception("Could not set Listen channel")
453    pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
454    if "FAIL" in pw:
455        raise Exception("Failed to generate password token")
456    res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
457    if "FAIL" in res:
458        raise Exception("Failed to enable NFC Tag for P2P static handover")
459    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
460    if "FAIL" in sel:
461        raise Exception("Failed to generate NFC connection handover select")
462    res = dev[1].global_request("P2P_LISTEN")
463    if "FAIL" in res:
464        raise Exception("Failed to start Listen mode")
465    dev[1].dump_monitor()
466
467    dev[0].dump_monitor()
468    dev[0].global_request("SET p2p_go_intent 3")
469    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
470    if "FAIL" in res:
471        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
472
473    ev = dev[0].wait_global_event(grpform_events, timeout=15)
474    if ev is None:
475        raise Exception("Group formation timed out")
476    res0 = dev[0].group_form_result(ev)
477
478    ev = dev[1].wait_global_event(grpform_events, timeout=1)
479    if ev is None:
480        raise Exception("Group formation timed out")
481    res1 = dev[1].group_form_result(ev)
482    logger.info("Group formed")
483
484    if res0['role'] != 'client' or res1['role'] != 'GO':
485        raise Exception("Unexpected roles negotiated")
486    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
487    check_ip_addr(res0)
488
489def test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev):
490    """NFC static handover to form a new P2P group on forced channel (NFC Tag device becomes GO)"""
491    try:
492        _test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev)
493    finally:
494        dev[0].global_request("SET p2p_go_intent 7")
495
496def _test_nfc_p2p_static_handover_tagdev_go_forced_freq(dev):
497    set_ip_addr_info(dev[1])
498
499    logger.info("Perform NFC connection handover")
500
501    res = dev[1].global_request("SET p2p_listen_reg_class 81")
502    res2 = dev[1].global_request("SET p2p_listen_channel 6")
503    if "FAIL" in res or "FAIL" in res2:
504        raise Exception("Could not set Listen channel")
505    pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
506    if "FAIL" in pw:
507        raise Exception("Failed to generate password token")
508    res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
509    if "FAIL" in res:
510        raise Exception("Failed to enable NFC Tag for P2P static handover")
511    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
512    if "FAIL" in sel:
513        raise Exception("Failed to generate NFC connection handover select")
514    res = dev[1].global_request("P2P_LISTEN")
515    if "FAIL" in res:
516        raise Exception("Failed to start Listen mode")
517    dev[1].dump_monitor()
518
519    dev[0].dump_monitor()
520    dev[0].global_request("SET p2p_go_intent 3")
521    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel + " freq=2442")
522    if "FAIL" in res:
523        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
524
525    ev = dev[0].wait_global_event(grpform_events, timeout=15)
526    if ev is None:
527        raise Exception("Group formation timed out")
528    res0 = dev[0].group_form_result(ev)
529
530    ev = dev[1].wait_global_event(grpform_events, timeout=1)
531    if ev is None:
532        raise Exception("Group formation timed out")
533    res1 = dev[1].group_form_result(ev)
534    logger.info("Group formed")
535
536    if res0['role'] != 'client' or res1['role'] != 'GO':
537        raise Exception("Unexpected roles negotiated")
538    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
539    check_ip_addr(res0)
540
541def test_nfc_p2p_static_handover_join_tagdev_go(dev):
542    """NFC static handover to join a P2P group (NFC Tag device is the GO)"""
543
544    logger.info("Start autonomous GO")
545    set_ip_addr_info(dev[0])
546    dev[0].p2p_start_go()
547
548    logger.info("Write NFC Tag on the GO")
549    pw = dev[0].request("WPS_NFC_TOKEN NDEF").rstrip()
550    if "FAIL" in pw:
551        raise Exception("Failed to generate password token")
552    res = dev[0].request("P2P_SET nfc_tag 1").rstrip()
553    if "FAIL" in res:
554        raise Exception("Failed to enable NFC Tag for P2P static handover")
555    sel = dev[0].request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
556    if "FAIL" in sel:
557        raise Exception("Failed to generate NFC connection handover select")
558
559    logger.info("Read NFC Tag on a P2P Device to join a group")
560    res = dev[1].request("WPS_NFC_TAG_READ " + sel)
561    if "FAIL" in res:
562        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
563
564    ev = dev[1].wait_event(grpform_events, timeout=30)
565    if ev is None:
566        raise Exception("Joining the group timed out")
567    res = dev[1].group_form_result(ev)
568    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
569    check_ip_addr(res)
570
571    logger.info("Read NFC Tag on another P2P Device to join a group")
572    res = dev[2].request("WPS_NFC_TAG_READ " + sel)
573    if "FAIL" in res:
574        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
575
576    ev = dev[2].wait_event(grpform_events, timeout=30)
577    if ev is None:
578        raise Exception("Joining the group timed out")
579    res = dev[2].group_form_result(ev)
580    hwsim_utils.test_connectivity_p2p(dev[0], dev[2])
581    check_ip_addr(res)
582
583def test_nfc_p2p_static_handover_join_tagdev_client(dev):
584    """NFC static handover to join a P2P group (NFC Tag device is the P2P Client)"""
585    set_ip_addr_info(dev[0])
586    logger.info("Start autonomous GO")
587    dev[0].p2p_start_go()
588
589    dev[1].global_request("SET ignore_old_scan_res 1")
590    dev[2].global_request("SET ignore_old_scan_res 1")
591
592    logger.info("Write NFC Tag on the P2P Client")
593    res = dev[1].global_request("P2P_LISTEN")
594    if "FAIL" in res:
595        raise Exception("Failed to start Listen mode")
596    pw = dev[1].global_request("WPS_NFC_TOKEN NDEF").rstrip()
597    if "FAIL" in pw:
598        raise Exception("Failed to generate password token")
599    res = dev[1].global_request("P2P_SET nfc_tag 1").rstrip()
600    if "FAIL" in res:
601        raise Exception("Failed to enable NFC Tag for P2P static handover")
602    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
603    if "FAIL" in sel:
604        raise Exception("Failed to generate NFC connection handover select")
605
606    logger.info("Read NFC Tag on the GO to trigger invitation")
607    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
608    if "FAIL" in res:
609        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
610
611    ev = dev[1].wait_global_event(grpform_events, timeout=30)
612    if ev is None:
613        raise Exception("Joining the group timed out")
614    res = dev[1].group_form_result(ev)
615    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
616    check_ip_addr(res)
617
618    logger.info("Write NFC Tag on another P2P Client")
619    res = dev[2].global_request("P2P_LISTEN")
620    if "FAIL" in res:
621        raise Exception("Failed to start Listen mode")
622    pw = dev[2].global_request("WPS_NFC_TOKEN NDEF").rstrip()
623    if "FAIL" in pw:
624        raise Exception("Failed to generate password token")
625    res = dev[2].global_request("P2P_SET nfc_tag 1").rstrip()
626    if "FAIL" in res:
627        raise Exception("Failed to enable NFC Tag for P2P static handover")
628    sel = dev[2].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
629    if "FAIL" in sel:
630        raise Exception("Failed to generate NFC connection handover select")
631
632    logger.info("Read NFC Tag on the GO to trigger invitation")
633    res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
634    if "FAIL" in res:
635        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
636
637    ev = dev[2].wait_global_event(grpform_events, timeout=30)
638    if ev is None:
639        raise Exception("Joining the group timed out")
640    res = dev[2].group_form_result(ev)
641    hwsim_utils.test_connectivity_p2p(dev[0], dev[2])
642    check_ip_addr(res)
643
644def test_nfc_p2p_go_legacy_config_token(dev):
645    """NFC config token from P2P GO to legacy WPS STA"""
646    logger.info("Start autonomous GOs")
647    dev[0].p2p_start_go()
648    logger.info("Connect legacy WPS STA with configuration token")
649    conf = dev[0].group_request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip()
650    if "FAIL" in conf:
651        raise Exception("Failed to generate configuration token")
652    dev[1].dump_monitor()
653    res = dev[1].request("WPS_NFC_TAG_READ " + conf)
654    if "FAIL" in res:
655        raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
656    dev[1].wait_connected(timeout=15, error="Joining the group timed out")
657    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
658    dev[1].request("DISCONNECT")
659    dev[0].remove_group()
660
661def test_nfc_p2p_go_legacy_handover(dev):
662    """NFC token from legacy WPS STA to P2P GO"""
663    logger.info("Start autonomous GOs")
664    dev[0].p2p_start_go()
665    logger.info("Connect legacy WPS STA with connection handover")
666    req = dev[1].request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip()
667    if "FAIL" in req:
668        raise Exception("Failed to generate NFC connection handover request")
669    sel = dev[0].group_request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip()
670    if "FAIL" in sel:
671        raise Exception("Failed to generate NFC connection handover select")
672    res = dev[0].group_request("NFC_REPORT_HANDOVER RESP WPS " + req + " " + sel)
673    if "FAIL" in res:
674        raise Exception("Failed to report NFC connection handover to wpa_supplicant (GO)")
675    dev[1].dump_monitor()
676    res = dev[1].request("NFC_REPORT_HANDOVER INIT WPS " + req + " " + sel)
677    if "FAIL" in res:
678        raise Exception("Failed to report NFC connection handover to wpa_supplicant (legacy STA)")
679    dev[1].wait_connected(timeout=15, error="Joining the group timed out")
680    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
681    dev[1].request("DISCONNECT")
682    dev[0].remove_group()
683
684def test_nfc_p2p_ip_addr_assignment(dev):
685    """NFC connection handover and legacy station IP address assignment"""
686    try:
687        _test_nfc_p2p_ip_addr_assignment(dev)
688    finally:
689        dev[0].global_request("SET p2p_go_intent 7")
690
691def _test_nfc_p2p_ip_addr_assignment(dev):
692    set_ip_addr_info(dev[1])
693    dev[0].global_request("SET p2p_go_intent 3")
694    logger.info("Perform NFC connection handover")
695    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
696    if "FAIL" in req:
697        raise Exception("Failed to generate NFC connection handover request")
698    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
699    if "FAIL" in sel:
700        raise Exception("Failed to generate NFC connection handover select")
701    dev[0].dump_monitor()
702    dev[1].dump_monitor()
703    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
704    if "FAIL" in res:
705        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
706    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
707    if "FAIL" in res:
708        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
709
710    ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
711                                   "P2P-GO-NEG-FAILURE",
712                                   "P2P-GROUP-FORMATION-FAILURE",
713                                   "WPS-PIN-NEEDED"], timeout=15)
714    if ev is None:
715        raise Exception("Group formation timed out")
716    res0 = dev[0].group_form_result(ev)
717
718    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
719                                   "P2P-GO-NEG-FAILURE",
720                                   "P2P-GROUP-FORMATION-FAILURE",
721                                   "WPS-PIN-NEEDED"], timeout=1)
722    if ev is None:
723        raise Exception("Group formation timed out")
724    res1 = dev[1].group_form_result(ev)
725    logger.info("Group formed")
726
727    if res0['role'] != 'client' or res1['role'] != 'GO':
728        raise Exception("Unexpected roles negotiated")
729    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
730    check_ip_addr(res0)
731
732    logger.info("Connect legacy P2P client that does not use new IP address assignment")
733    res = dev[2].global_request("P2P_SET disable_ip_addr_req 1")
734    if "FAIL" in res:
735        raise Exception("Failed to disable IP address assignment request")
736    pin = dev[2].wps_read_pin()
737    dev[1].p2p_go_authorize_client(pin)
738    res = dev[2].p2p_connect_group(dev[1].p2p_dev_addr(), pin, timeout=60)
739    logger.info("Client connected")
740    res = dev[2].global_request("P2P_SET disable_ip_addr_req 0")
741    hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
742    if 'ip_addr' in res:
743        raise Exception("Unexpected IP address assignment")
744
745def test_nfc_p2p_ip_addr_assignment2(dev):
746    """NFC connection handover and IP address assignment for two clients"""
747    try:
748        _test_nfc_p2p_ip_addr_assignment2(dev)
749    finally:
750        dev[0].global_request("SET p2p_go_intent 7")
751
752def _test_nfc_p2p_ip_addr_assignment2(dev):
753    set_ip_addr_info(dev[1])
754    dev[0].global_request("SET p2p_go_intent 3")
755    logger.info("Perform NFC connection handover")
756    req = dev[0].global_request("NFC_GET_HANDOVER_REQ NDEF P2P-CR").rstrip()
757    if "FAIL" in req:
758        raise Exception("Failed to generate NFC connection handover request")
759    sel = dev[1].global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR").rstrip()
760    if "FAIL" in sel:
761        raise Exception("Failed to generate NFC connection handover select")
762    dev[0].dump_monitor()
763    dev[1].dump_monitor()
764    res = dev[1].global_request("NFC_REPORT_HANDOVER RESP P2P " + req + " " + sel)
765    if "FAIL" in res:
766        raise Exception("Failed to report NFC connection handover to wpa_supplicant(resp)")
767    res = dev[0].global_request("NFC_REPORT_HANDOVER INIT P2P " + req + " " + sel)
768    if "FAIL" in res:
769        raise Exception("Failed to report NFC connection handover to wpa_supplicant(init)")
770
771    ev = dev[0].wait_global_event(["P2P-GROUP-STARTED",
772                                   "P2P-GO-NEG-FAILURE",
773                                   "P2P-GROUP-FORMATION-FAILURE",
774                                   "WPS-PIN-NEEDED"], timeout=15)
775    if ev is None:
776        raise Exception("Group formation timed out")
777    res0 = dev[0].group_form_result(ev)
778
779    ev = dev[1].wait_global_event(["P2P-GROUP-STARTED",
780                                   "P2P-GO-NEG-FAILURE",
781                                   "P2P-GROUP-FORMATION-FAILURE",
782                                   "WPS-PIN-NEEDED"], timeout=1)
783    if ev is None:
784        raise Exception("Group formation timed out")
785    res1 = dev[1].group_form_result(ev)
786    logger.info("Group formed")
787
788    if res0['role'] != 'client' or res1['role'] != 'GO':
789        raise Exception("Unexpected roles negotiated")
790    hwsim_utils.test_connectivity_p2p(dev[0], dev[1])
791    check_ip_addr(res0)
792    logger.info("Client 1 IP address: " + res0['ip_addr'])
793
794    logger.info("Connect a P2P client")
795    pin = dev[2].wps_read_pin()
796    dev[1].p2p_go_authorize_client(pin)
797    res = dev[2].p2p_connect_group(dev[1].p2p_dev_addr(), pin, timeout=60)
798    logger.info("Client connected")
799    hwsim_utils.test_connectivity_p2p(dev[1], dev[2])
800    check_ip_addr(res)
801    logger.info("Client 2 IP address: " + res['ip_addr'])
802    if res['ip_addr'] == res0['ip_addr']:
803        raise Exception("Same IP address assigned to both clients")
804
805@remote_compatible
806def test_nfc_p2p_tag_enable_disable(dev):
807    """NFC tag enable/disable for P2P"""
808    if "FAIL" in dev[0].request("WPS_NFC_TOKEN NDEF").rstrip():
809        raise Exception("Failed to generate password token")
810    if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
811        raise Exception("Failed to enable NFC Tag for P2P static handover")
812    if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
813        raise Exception("Failed to disable NFC Tag for P2P static handover")
814
815    dev[0].request("SET p2p_no_group_iface 0")
816    if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
817        raise Exception("Failed to enable NFC Tag for P2P static handover")
818    if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
819        raise Exception("Failed to disable NFC Tag for P2P static handover")
820    if "OK" not in dev[0].request("P2P_SET nfc_tag 1"):
821        raise Exception("Failed to enable NFC Tag for P2P static handover")
822    if "OK" not in dev[0].request("P2P_SET nfc_tag 0"):
823        raise Exception("Failed to disable NFC Tag for P2P static handover")
824
825@remote_compatible
826def test_nfc_p2p_static_handover_invalid(dev):
827    """NFC static handover with invalid contents"""
828    logger.info("Unknown OOB GO Neg channel")
829    sel = "D217A36170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120002E02020025000D1D000200000001001108000000000000000000101100084465766963652042130600585804ff0B00"
830    if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
831        raise Exception("Invalid tag contents accepted (1)")
832
833    logger.info("No OOB GO Neg channel attribute")
834    sel = "D2179A6170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120002502020025000D1D000200000001001108000000000000000000101100084465766963652042"
835    if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
836        raise Exception("Invalid tag contents accepted (2)")
837
838    logger.info("No Device Info attribute")
839    sel = "D217836170706C69636174696F6E2F766E642E7766612E7032700071102100012010230001201024000120102C0036C3B2ADB8D26F53CE1CB7F000BEEDA762922FF5307E87CCE484EF4B5DAD440D0A4752579767610AD1293F7A76A66B09A7C9D58A66994E103C000103104200012010470010572CF82FC95756539B16B5CFB298ABF11049000600372A000120000E0202002500130600585804510B00"
840    if "FAIL" not in dev[0].global_request("WPS_NFC_TAG_READ " + sel):
841        raise Exception("Invalid tag contents accepted (3)")
842