1# cfg80211 P2P Device
2# Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7import logging
8logger = logging.getLogger()
9import time
10
11from wpasupplicant import WpaSupplicant
12from p2p_utils import *
13from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
14from hwsim import HWSimRadio
15from utils import HwsimSkip
16import hostapd
17import hwsim_utils
18
19def test_p2p_device_grpform(dev, apdev):
20    """P2P group formation with driver using cfg80211 P2P Device"""
21    with HWSimRadio(use_p2p_device=True) as (radio, iface):
22        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
23        wpas.interface_add(iface)
24        [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
25                                               r_dev=wpas, r_intent=0)
26        check_grpform_results(i_res, r_res)
27        wpas.dump_monitor()
28        remove_group(dev[0], wpas)
29        wpas.dump_monitor()
30        if not r_res['ifname'].startswith('p2p-' + iface):
31            raise Exception("Unexpected group ifname: " + r_res['ifname'])
32
33        res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
34        lines = res.splitlines()
35        found = False
36        for l in lines:
37            try:
38                [name, value] = l.split('=', 1)
39                if name == "wdev_id":
40                    found = True
41                    break
42            except ValueError:
43                pass
44        if not found:
45            raise Exception("wdev_id not found")
46
47def test_p2p_device_grpform2(dev, apdev):
48    """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
49    with HWSimRadio(use_p2p_device=True) as (radio, iface):
50        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
51        wpas.interface_add(iface)
52        [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
53                                               r_dev=dev[0], r_intent=0)
54        check_grpform_results(i_res, r_res)
55        wpas.dump_monitor()
56        remove_group(wpas, dev[0])
57        wpas.dump_monitor()
58        if not i_res['ifname'].startswith('p2p-' + iface):
59            raise Exception("Unexpected group ifname: " + i_res['ifname'])
60
61def test_p2p_device_grpform_no_group_iface(dev, apdev):
62    """P2P group formation with driver using cfg80211 P2P Device but no separate group interface"""
63    with HWSimRadio(use_p2p_device=True) as (radio, iface):
64        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
65        wpas.interface_add(iface)
66        wpas.global_request("SET p2p_no_group_iface 1")
67        [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
68                                               r_dev=wpas, r_intent=0)
69        check_grpform_results(i_res, r_res)
70        wpas.dump_monitor()
71        remove_group(dev[0], wpas)
72        wpas.dump_monitor()
73        if r_res['ifname'] != iface:
74            raise Exception("Unexpected group ifname: " + r_res['ifname'])
75
76def test_p2p_device_grpform_no_group_iface2(dev, apdev):
77    """P2P group formation with driver using cfg80211 P2P Device but no separate group interface (reverse)"""
78    with HWSimRadio(use_p2p_device=True) as (radio, iface):
79        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
80        wpas.interface_add(iface)
81        wpas.global_request("SET p2p_no_group_iface 1")
82        [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
83                                               r_dev=dev[0], r_intent=0)
84        check_grpform_results(i_res, r_res)
85        wpas.dump_monitor()
86        remove_group(dev[0], wpas)
87        wpas.dump_monitor()
88        if i_res['ifname'] != iface:
89            raise Exception("Unexpected group ifname: " + i_res['ifname'])
90
91def test_p2p_device_group_remove(dev, apdev):
92    """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
93    with HWSimRadio(use_p2p_device=True) as (radio, iface):
94        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
95        wpas.interface_add(iface)
96        [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
97                                               r_dev=wpas, r_intent=0)
98        check_grpform_results(i_res, r_res)
99        # Issue the remove request on the interface which will be removed
100        p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
101        res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
102        if "OK" not in res:
103            raise Exception("Failed to remove P2P group")
104        ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
105        if ev is None:
106            raise Exception("Group removal event not received")
107        if not wpas.global_ping():
108            raise Exception("Could not ping global ctrl_iface after group removal")
109
110def test_p2p_device_concurrent_scan(dev, apdev):
111    """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
112    with HWSimRadio(use_p2p_device=True) as (radio, iface):
113        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
114        wpas.interface_add(iface)
115        wpas.p2p_find()
116        time.sleep(0.1)
117        wpas.request("SCAN")
118        ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
119        if ev is None:
120            raise Exception("Station mode scan did not start")
121
122def test_p2p_device_nfc_invite(dev, apdev):
123    """P2P NFC invitation with driver using cfg80211 P2P Device"""
124    run_p2p_device_nfc_invite(dev, apdev, 0)
125
126def test_p2p_device_nfc_invite_no_group_iface(dev, apdev):
127    """P2P NFC invitation with driver using cfg80211 P2P Device (no separate group interface)"""
128    run_p2p_device_nfc_invite(dev, apdev, 1)
129
130def run_p2p_device_nfc_invite(dev, apdev, no_group_iface):
131    with HWSimRadio(use_p2p_device=True) as (radio, iface):
132        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
133        wpas.interface_add(iface)
134        wpas.global_request("SET p2p_no_group_iface %d" % no_group_iface)
135
136        set_ip_addr_info(dev[0])
137        logger.info("Start autonomous GO")
138        dev[0].p2p_start_go()
139
140        logger.info("Write NFC Tag on the P2P Client")
141        res = wpas.global_request("P2P_LISTEN")
142        if "FAIL" in res:
143            raise Exception("Failed to start Listen mode")
144        wpas.dump_monitor()
145        pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
146        if "FAIL" in pw:
147            raise Exception("Failed to generate password token")
148        res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
149        if "FAIL" in res:
150            raise Exception("Failed to enable NFC Tag for P2P static handover")
151        sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
152        if "FAIL" in sel:
153            raise Exception("Failed to generate NFC connection handover select")
154        wpas.dump_monitor()
155
156        logger.info("Read NFC Tag on the GO to trigger invitation")
157        res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
158        if "FAIL" in res:
159            raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
160
161        ev = wpas.wait_global_event(grpform_events, timeout=20)
162        if ev is None:
163            raise Exception("Joining the group timed out")
164        res = wpas.group_form_result(ev)
165        wpas.dump_monitor()
166        hwsim_utils.test_connectivity_p2p(dev[0], wpas)
167        check_ip_addr(res)
168        wpas.dump_monitor()
169
170def test_p2p_device_misuses(dev, apdev):
171    """cfg80211 P2P Device misuses"""
172    hapd = hostapd.add_ap(apdev[0], {"ssid": "open"})
173    with HWSimRadio(use_p2p_device=True) as (radio, iface):
174        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
175        wpas.interface_add(iface)
176
177        # Add a normal network profile to the P2P Device management only
178        # interface to verify that it does not get used.
179        id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
180        wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
181        wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
182        wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
183
184        # Scan requests get ignored on p2p-dev
185        wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
186
187        dev[0].p2p_start_go(freq=2412)
188        addr = dev[0].p2p_interface_addr()
189        wpas.scan_for_bss(addr, freq=2412)
190        wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
191        hwsim_utils.test_connectivity(wpas, hapd)
192
193        pin = wpas.wps_read_pin()
194        dev[0].p2p_go_authorize_client(pin)
195        res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
196                                     social=True, freq=2412)
197        hwsim_utils.test_connectivity_p2p(dev[0], wpas)
198
199        # Optimize scan-after-disconnect
200        wpas.group_request("SET_NETWORK 0 scan_freq 2412")
201
202        dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
203        ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
204        if ev is None:
205            raise Exception("Did not see disconnect event on P2P group interface")
206        dev[0].remove_group()
207
208        ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
209        if ev is None:
210            raise Exception("Scan not started")
211        ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
212        if ev is None:
213            raise Exception("Scan not completed")
214        time.sleep(1)
215        hwsim_utils.test_connectivity(wpas, hapd)
216
217        ev = hapd.wait_event(["AP-STA-DISCONNECTED"], timeout=0.1)
218        if ev is not None:
219            raise Exception("Unexpected disconnection event received from hostapd")
220        ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
221        if ev is not None:
222            raise Exception("Unexpected disconnection event received from wpa_supplicant")
223
224        wpas.request("DISCONNECT")
225        wpas.wait_disconnected()
226
227def test_p2p_device_incorrect_command_interface(dev, apdev):
228    """cfg80211 P2P Device and P2P_* command on incorrect interface"""
229    with HWSimRadio(use_p2p_device=True) as (radio, iface):
230        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
231        wpas.interface_add(iface)
232
233        dev[0].p2p_listen()
234        wpas.request('P2P_FIND type=social')
235        ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
236        if ev is None:
237            raise Exception("Peer not found")
238        ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
239        if ev is not None:
240            raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
241        wpas.dump_monitor()
242
243        pin = wpas.wps_read_pin()
244        dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
245                               freq=2412)
246        wpas.request('P2P_STOP_FIND')
247        wpas.dump_monitor()
248        if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
249            raise Exception("P2P_CONNECT failed")
250
251        ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
252        if ev is None:
253            raise Exception("Group formation timed out")
254        wpas.group_form_result(ev)
255        wpas.dump_monitor()
256
257        ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
258        if ev is None:
259            raise Exception("Group formation timed out(2)")
260        dev[0].group_form_result(ev)
261
262        dev[0].remove_group()
263        wpas.wait_go_ending_session()
264        wpas.dump_monitor()
265
266def test_p2p_device_incorrect_command_interface2(dev, apdev):
267    """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
268    with HWSimRadio(use_p2p_device=True) as (radio, iface):
269        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
270        wpas.interface_add(iface)
271
272        if "OK" not in wpas.request('P2P_GROUP_ADD'):
273            raise Exception("P2P_GROUP_ADD failed")
274        ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
275        if ev is None:
276            raise Exception("Group formation timed out")
277        res = wpas.group_form_result(ev)
278        wpas.dump_monitor()
279        logger.info("Group results: " + str(res))
280        wpas.remove_group()
281        if not res['ifname'].startswith('p2p-' + iface + '-'):
282            raise Exception("Unexpected group ifname: " + res['ifname'])
283        wpas.dump_monitor()
284
285def test_p2p_device_grpform_timeout_client(dev, apdev):
286    """P2P group formation timeout on client with cfg80211 P2P Device"""
287    with HWSimRadio(use_p2p_device=True) as (radio, iface):
288        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
289        wpas.interface_add(iface)
290        addr0 = dev[0].p2p_dev_addr()
291        addr5 = wpas.p2p_dev_addr()
292        wpas.p2p_listen()
293        dev[0].discover_peer(addr5)
294        dev[0].p2p_listen()
295        wpas.discover_peer(addr0)
296        wpas.p2p_ext_listen(100, 150)
297        dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=15 auth")
298        wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=0")
299        ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
300        if ev is None:
301            raise Exception("GO Negotiation did not succeed")
302        ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
303        if ev is None:
304            raise Exception("WPS did not succeed (GO)")
305        if "OK" not in dev[0].global_request("P2P_CANCEL"):
306            wpas.global_request("P2P_CANCEL")
307            del wpas
308            raise HwsimSkip("Did not manage to cancel group formation")
309        dev[0].dump_monitor()
310        ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
311        if ev is None:
312            raise Exception("WPS did not succeed (Client)")
313        dev[0].dump_monitor()
314        ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
315        if ev is None:
316            raise Exception("Group formation timeout not seen on client")
317        ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
318        if ev is None:
319            raise Exception("Group removal not seen on client")
320        wpas.p2p_cancel_ext_listen()
321        time.sleep(0.1)
322        ifaces = wpas.global_request("INTERFACES")
323        logger.info("Remaining interfaces: " + ifaces)
324        del wpas
325        if "p2p-" + iface + "-" in ifaces:
326            raise Exception("Group interface still present after failure")
327
328def test_p2p_device_grpform_timeout_go(dev, apdev):
329    """P2P group formation timeout on GO with cfg80211 P2P Device"""
330    with HWSimRadio(use_p2p_device=True) as (radio, iface):
331        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
332        wpas.interface_add(iface)
333        addr0 = dev[0].p2p_dev_addr()
334        addr5 = wpas.p2p_dev_addr()
335        wpas.p2p_listen()
336        dev[0].discover_peer(addr5)
337        dev[0].p2p_listen()
338        wpas.discover_peer(addr0)
339        wpas.p2p_ext_listen(100, 150)
340        dev[0].global_request("P2P_CONNECT " + addr5 + " 12345670 enter go_intent=0 auth")
341        wpas.global_request("P2P_CONNECT " + addr0 + " 12345670 display go_intent=15")
342        ev = dev[0].wait_global_event(["P2P-GO-NEG-SUCCESS"], timeout=5)
343        if ev is None:
344            raise Exception("GO Negotiation did not succeed")
345        ev = dev[0].wait_global_event(["WPS-SUCCESS"], timeout=10)
346        if ev is None:
347            raise Exception("WPS did not succeed (Client)")
348        if "OK" not in dev[0].global_request("P2P_CANCEL"):
349            if "OK" not in dev[0].global_request("P2P_GROUP_REMOVE *"):
350                wpas.global_request("P2P_CANCEL")
351                del wpas
352                raise HwsimSkip("Did not manage to cancel group formation")
353        dev[0].dump_monitor()
354        # There is a race condition on WPS-SUCCESS being reported on the GO
355        # since the P2P_CANCEL command above might actually stop the WPS
356        # exchange before the WSC_Done is sent to the GO and processed there.
357        # It would be possible for the GO to receive a disconnection event
358        # first.
359        ev = wpas.wait_global_event(["WPS-SUCCESS",
360                                     "P2P-GROUP-FORMATION-FAILURE"],
361                                    timeout=30)
362        if ev is None:
363            raise Exception("WPS did not succeed (GO)")
364        dev[0].dump_monitor()
365        if "P2P-GROUP-FORMATION-FAILURE" not in ev:
366            ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"],
367                                        timeout=20)
368            if ev is None:
369                raise Exception("Group formation timeout not seen on GO")
370        ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
371        if ev is None:
372            raise Exception("Group removal not seen on GO")
373        wpas.p2p_cancel_ext_listen()
374        time.sleep(0.1)
375        ifaces = wpas.global_request("INTERFACES")
376        logger.info("Remaining interfaces: " + ifaces)
377        del wpas
378        if "p2p-" + iface + "-" in ifaces:
379            raise Exception("Group interface still present after failure")
380
381def test_p2p_device_autogo(dev, apdev):
382    """P2P autogo using cfg80211 P2P Device"""
383    with HWSimRadio(use_p2p_device=True) as (radio, iface):
384        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
385        wpas.interface_add(iface)
386
387        res = wpas.p2p_start_go()
388        if not res['ifname'].startswith('p2p-' + iface):
389            raise Exception("Unexpected group ifname: " + res['ifname'])
390        bssid = wpas.get_group_status_field('bssid')
391
392        dev[0].scan_for_bss(bssid, res['freq'])
393        connect_cli(wpas, dev[0], freq=res['freq'])
394        terminate_group(wpas, dev[0])
395
396def test_p2p_device_autogo_no_group_iface(dev, apdev):
397    """P2P autogo using cfg80211 P2P Device (no separate group interface)"""
398    with HWSimRadio(use_p2p_device=True) as (radio, iface):
399        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
400        wpas.interface_add(iface)
401        wpas.global_request("SET p2p_no_group_iface 1")
402
403        res = wpas.p2p_start_go()
404        if res['ifname'] != iface:
405            raise Exception("Unexpected group ifname: " + res['ifname'])
406        bssid = wpas.get_group_status_field('bssid')
407
408        dev[0].scan_for_bss(bssid, res['freq'])
409        connect_cli(wpas, dev[0], freq=res['freq'])
410        terminate_group(wpas, dev[0])
411
412def test_p2p_device_join(dev, apdev):
413    """P2P join-group using cfg80211 P2P Device"""
414    with HWSimRadio(use_p2p_device=True) as (radio, iface):
415        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
416        wpas.interface_add(iface)
417
418        res = dev[0].p2p_start_go()
419        bssid = dev[0].get_group_status_field('bssid')
420
421        wpas.scan_for_bss(bssid, res['freq'])
422        res2 = connect_cli(dev[0], wpas, freq=res['freq'])
423        if not res2['ifname'].startswith('p2p-' + iface):
424            raise Exception("Unexpected group ifname: " + res2['ifname'])
425
426        terminate_group(dev[0], wpas)
427
428def test_p2p_device_join_no_group_iface(dev, apdev):
429    """P2P join-group using cfg80211 P2P Device (no separate group interface)"""
430    with HWSimRadio(use_p2p_device=True) as (radio, iface):
431        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
432        wpas.interface_add(iface)
433        wpas.global_request("SET p2p_no_group_iface 1")
434
435        res = dev[0].p2p_start_go()
436        bssid = dev[0].get_group_status_field('bssid')
437
438        wpas.scan_for_bss(bssid, res['freq'])
439        res2 = connect_cli(dev[0], wpas, freq=res['freq'])
440        if res2['ifname'] != iface:
441            raise Exception("Unexpected group ifname: " + res2['ifname'])
442
443        terminate_group(dev[0], wpas)
444
445def test_p2p_device_join_no_group_iface_cancel(dev, apdev):
446    """P2P cancel join-group using cfg80211 P2P Device (no separate group interface)"""
447    with HWSimRadio(use_p2p_device=True) as (radio, iface):
448        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
449        wpas.interface_add(iface)
450        wpas.global_request("SET p2p_no_group_iface 1")
451
452        res = dev[0].p2p_start_go()
453        bssid = dev[0].get_group_status_field('bssid')
454
455        wpas.scan_for_bss(bssid, res['freq'])
456        pin = wpas.wps_read_pin()
457        dev[0].p2p_go_authorize_client(pin)
458        cmd = "P2P_CONNECT %s %s join freq=%s" % (dev[0].p2p_dev_addr(), pin,
459                                                  res['freq'])
460        if "OK" not in wpas.request(cmd):
461            raise Exception("P2P_CONNECT(join) failed")
462        ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=1)
463        if "OK" not in wpas.request("P2P_CANCEL"):
464            raise Exception("P2P_CANCEL failed")
465
466        dev[0].remove_group()
467
468def test_p2p_device_persistent_group(dev):
469    """P2P persistent group formation and re-invocation with cfg80211 P2P Device"""
470    with HWSimRadio(use_p2p_device=True) as (radio, iface):
471        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
472        wpas.interface_add(iface)
473        wpas.global_request("SET p2p_no_group_iface 0")
474
475        form(dev[0], wpas)
476        invite_from_cli(dev[0], wpas)
477        invite_from_go(dev[0], wpas)
478
479def test_p2p_device_persistent_group_no_group_iface(dev):
480    """P2P persistent group formation and re-invocation with cfg80211 P2P Device (no separate group interface)"""
481    with HWSimRadio(use_p2p_device=True) as (radio, iface):
482        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
483        wpas.interface_add(iface)
484        wpas.global_request("SET p2p_no_group_iface 1")
485
486        form(dev[0], wpas)
487        invite_from_cli(dev[0], wpas)
488        invite_from_go(dev[0], wpas)
489
490def test_p2p_device_persistent_group2(dev):
491    """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device"""
492    with HWSimRadio(use_p2p_device=True) as (radio, iface):
493        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
494        wpas.interface_add(iface)
495        wpas.global_request("SET p2p_no_group_iface 0")
496
497        form(wpas, dev[0])
498        invite_from_cli(wpas, dev[0])
499        invite_from_go(wpas, dev[0])
500
501def test_p2p_device_persistent_group2_no_group_iface(dev):
502    """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device (no separate group interface)"""
503    with HWSimRadio(use_p2p_device=True) as (radio, iface):
504        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
505        wpas.interface_add(iface)
506        wpas.global_request("SET p2p_no_group_iface 1")
507
508        form(wpas, dev[0])
509        invite_from_cli(wpas, dev[0])
510        invite_from_go(wpas, dev[0])
511
512def p2p_device_group_conf(dev1, dev2):
513    dev1.global_request("SET p2p_group_idle 12")
514    dev1.global_request("SET p2p_go_freq_change_policy 2")
515    dev1.global_request("SET p2p_go_ctwindow 7")
516
517    [i_res, r_res] = go_neg_pin_authorized(i_dev=dev1, i_intent=15,
518                                           r_dev=dev2, r_intent=0)
519    check_grpform_results(i_res, r_res)
520
521    if (dev1.group_request("GET p2p_group_idle") != "12" or
522        dev1.group_request("GET p2p_go_freq_change_policy") != "2" or
523        dev1.group_request("GET p2p_go_ctwindow") != "7"):
524        raise Exception("Unexpected configuration value")
525
526    remove_group(dev1, dev2)
527    dev1.global_request("P2P_FLUSH")
528    dev2.global_request("P2P_FLUSH")
529
530def test_p2p_device_conf(dev, apdev):
531    """P2P configuration with cfg80211 P2P Device"""
532    with HWSimRadio(use_p2p_device=True) as (radio, iface):
533        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
534        wpas.interface_add(iface)
535        wpas.global_request("SET p2p_no_group_iface 1")
536        p2p_device_group_conf(wpas, dev[0])
537        wpas.global_request("SET p2p_no_group_iface 0")
538        p2p_device_group_conf(wpas, dev[0])
539
540def test_p2p_device_autogo_chan_switch(dev):
541    """P2P autonomous GO switching channels with cfg80211 P2P Device"""
542    with HWSimRadio(use_p2p_device=True) as (radio, iface):
543        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
544        wpas.interface_add(iface)
545        wpas.global_request("SET p2p_no_group_iface 1")
546        autogo(wpas, freq=2417)
547        connect_cli(wpas, dev[1])
548        res = wpas.group_request("CHAN_SWITCH 5 2422 ht")
549        if "FAIL" in res:
550            # for now, skip test since mac80211_hwsim support is not yet widely
551            # deployed
552            raise HwsimSkip("Assume mac80211_hwsim did not support channel switching")
553        ev = wpas.wait_group_event(["AP-CSA-FINISHED"], timeout=10)
554        if ev is None:
555            raise Exception("CSA finished event timed out")
556        if "freq=2422" not in ev:
557            raise Exception("Unexpected channel in CSA finished event")
558        ev = dev[1].wait_event(["CTRL-EVENT-CHANNEL-SWITCH"], timeout=2)
559        if ev is None:
560            raise Exception("Channel switch not reported on P2P Client")
561        if "freq=2422" not in ev:
562            raise Exception("Unexpected channel in CS event")
563        wpas.dump_monitor()
564        dev[1].dump_monitor()
565        time.sleep(0.1)
566        hwsim_utils.test_connectivity_p2p(wpas, dev[1])
567
568def test_p2p_device_persistent_group_go_bssid(dev):
569    """P2P persistent group re-invocation (go_bssid) with cfg80211 P2P Device"""
570    with HWSimRadio(use_p2p_device=True) as (radio, iface):
571        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
572        wpas.interface_add(iface)
573        wpas.global_request("SET p2p_no_group_iface 0")
574
575        addr0 = dev[0].p2p_dev_addr()
576        addr1 = wpas.p2p_dev_addr()
577        [i_res, r_res] = go_neg_pin_authorized_persistent(i_dev=dev[0],
578                                                          i_intent=15,
579                                                          r_dev=wpas,
580                                                          r_intent=0)
581        bssid = wpas.get_group_status_field("bssid")
582        wpas.remove_group()
583        wpas.dump_monitor()
584
585        wpas.p2p_listen()
586        if not dev[0].discover_peer(addr1, social=True):
587            raise Exception("Peer " + peer + " not found")
588        dev[0].global_request("P2P_INVITE group=" + dev[0].group_ifname + " peer=" + addr1)
589        ev = wpas.wait_global_event(["P2P-INVITATION-RECEIVED"], timeout=10)
590        if ev is None:
591            raise Exception("Timeout on invitation")
592        if "sa=" + addr0 + " persistent=" not in ev:
593            raise Exception("Unexpected invitation event")
594        [event, addr, persistent] = ev.split(' ', 2)
595
596        wpas.p2p_stop_find()
597        time.sleep(1)
598        wpas.dump_monitor()
599        wpas.flush_scan_cache()
600
601        cmd = "P2P_GROUP_ADD " + persistent + " go_bssid=" + bssid
602        if "OK" not in wpas.global_request(cmd):
603            raise Exception("Could not re-start persistent group")
604        ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=30)
605        if ev is None:
606            raise Exception("Timeout on group restart")
607