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        ev = wpas.wait_global_event(["WPS-SUCCESS"], timeout=10)
355        if ev is None:
356            raise Exception("WPS did not succeed (GO)")
357        dev[0].dump_monitor()
358        ev = wpas.wait_global_event(["P2P-GROUP-FORMATION-FAILURE"], timeout=20)
359        if ev is None:
360            raise Exception("Group formation timeout not seen on GO")
361        ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=5)
362        if ev is None:
363            raise Exception("Group removal not seen on GO")
364        wpas.p2p_cancel_ext_listen()
365        time.sleep(0.1)
366        ifaces = wpas.global_request("INTERFACES")
367        logger.info("Remaining interfaces: " + ifaces)
368        del wpas
369        if "p2p-" + iface + "-" in ifaces:
370            raise Exception("Group interface still present after failure")
371
372def test_p2p_device_autogo(dev, apdev):
373    """P2P autogo using cfg80211 P2P Device"""
374    with HWSimRadio(use_p2p_device=True) as (radio, iface):
375        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
376        wpas.interface_add(iface)
377
378        res = wpas.p2p_start_go()
379        if not res['ifname'].startswith('p2p-' + iface):
380            raise Exception("Unexpected group ifname: " + res['ifname'])
381        bssid = wpas.get_group_status_field('bssid')
382
383        dev[0].scan_for_bss(bssid, res['freq'])
384        connect_cli(wpas, dev[0], freq=res['freq'])
385        terminate_group(wpas, dev[0])
386
387def test_p2p_device_autogo_no_group_iface(dev, apdev):
388    """P2P autogo using cfg80211 P2P Device (no separate group interface)"""
389    with HWSimRadio(use_p2p_device=True) as (radio, iface):
390        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
391        wpas.interface_add(iface)
392        wpas.global_request("SET p2p_no_group_iface 1")
393
394        res = wpas.p2p_start_go()
395        if res['ifname'] != iface:
396            raise Exception("Unexpected group ifname: " + res['ifname'])
397        bssid = wpas.get_group_status_field('bssid')
398
399        dev[0].scan_for_bss(bssid, res['freq'])
400        connect_cli(wpas, dev[0], freq=res['freq'])
401        terminate_group(wpas, dev[0])
402
403def test_p2p_device_join(dev, apdev):
404    """P2P join-group using cfg80211 P2P Device"""
405    with HWSimRadio(use_p2p_device=True) as (radio, iface):
406        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
407        wpas.interface_add(iface)
408
409        res = dev[0].p2p_start_go()
410        bssid = dev[0].get_group_status_field('bssid')
411
412        wpas.scan_for_bss(bssid, res['freq'])
413        res2 = connect_cli(dev[0], wpas, freq=res['freq'])
414        if not res2['ifname'].startswith('p2p-' + iface):
415            raise Exception("Unexpected group ifname: " + res2['ifname'])
416
417        terminate_group(dev[0], wpas)
418
419def test_p2p_device_join_no_group_iface(dev, apdev):
420    """P2P join-group using cfg80211 P2P Device (no separate group interface)"""
421    with HWSimRadio(use_p2p_device=True) as (radio, iface):
422        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
423        wpas.interface_add(iface)
424        wpas.global_request("SET p2p_no_group_iface 1")
425
426        res = dev[0].p2p_start_go()
427        bssid = dev[0].get_group_status_field('bssid')
428
429        wpas.scan_for_bss(bssid, res['freq'])
430        res2 = connect_cli(dev[0], wpas, freq=res['freq'])
431        if res2['ifname'] != iface:
432            raise Exception("Unexpected group ifname: " + res2['ifname'])
433
434        terminate_group(dev[0], wpas)
435
436def test_p2p_device_join_no_group_iface_cancel(dev, apdev):
437    """P2P cancel join-group using cfg80211 P2P Device (no separate group interface)"""
438    with HWSimRadio(use_p2p_device=True) as (radio, iface):
439        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
440        wpas.interface_add(iface)
441        wpas.global_request("SET p2p_no_group_iface 1")
442
443        res = dev[0].p2p_start_go()
444        bssid = dev[0].get_group_status_field('bssid')
445
446        wpas.scan_for_bss(bssid, res['freq'])
447        pin = wpas.wps_read_pin()
448        dev[0].p2p_go_authorize_client(pin)
449        cmd = "P2P_CONNECT %s %s join freq=%s" % (dev[0].p2p_dev_addr(), pin,
450                                                  res['freq'])
451        if "OK" not in wpas.request(cmd):
452            raise Exception("P2P_CONNECT(join) failed")
453        ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=1)
454        if "OK" not in wpas.request("P2P_CANCEL"):
455            raise Exception("P2P_CANCEL failed")
456
457        dev[0].remove_group()
458
459def test_p2p_device_persistent_group(dev):
460    """P2P persistent group formation and re-invocation with cfg80211 P2P Device"""
461    with HWSimRadio(use_p2p_device=True) as (radio, iface):
462        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
463        wpas.interface_add(iface)
464        wpas.global_request("SET p2p_no_group_iface 0")
465
466        form(dev[0], wpas)
467        invite_from_cli(dev[0], wpas)
468        invite_from_go(dev[0], wpas)
469
470def test_p2p_device_persistent_group_no_group_iface(dev):
471    """P2P persistent group formation and re-invocation with cfg80211 P2P Device (no separate group interface)"""
472    with HWSimRadio(use_p2p_device=True) as (radio, iface):
473        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
474        wpas.interface_add(iface)
475        wpas.global_request("SET p2p_no_group_iface 1")
476
477        form(dev[0], wpas)
478        invite_from_cli(dev[0], wpas)
479        invite_from_go(dev[0], wpas)
480
481def test_p2p_device_persistent_group2(dev):
482    """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device"""
483    with HWSimRadio(use_p2p_device=True) as (radio, iface):
484        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
485        wpas.interface_add(iface)
486        wpas.global_request("SET p2p_no_group_iface 0")
487
488        form(wpas, dev[0])
489        invite_from_cli(wpas, dev[0])
490        invite_from_go(wpas, dev[0])
491
492def test_p2p_device_persistent_group2_no_group_iface(dev):
493    """P2P persistent group formation and re-invocation (reverse) with cfg80211 P2P Device (no separate group interface)"""
494    with HWSimRadio(use_p2p_device=True) as (radio, iface):
495        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
496        wpas.interface_add(iface)
497        wpas.global_request("SET p2p_no_group_iface 1")
498
499        form(wpas, dev[0])
500        invite_from_cli(wpas, dev[0])
501        invite_from_go(wpas, dev[0])
502
503def p2p_device_group_conf(dev1, dev2):
504    dev1.global_request("SET p2p_group_idle 12")
505    dev1.global_request("SET p2p_go_freq_change_policy 2")
506    dev1.global_request("SET p2p_go_ctwindow 7")
507
508    [i_res, r_res] = go_neg_pin_authorized(i_dev=dev1, i_intent=15,
509                                           r_dev=dev2, r_intent=0)
510    check_grpform_results(i_res, r_res)
511
512    if (dev1.group_request("GET p2p_group_idle") != "12" or
513        dev1.group_request("GET p2p_go_freq_change_policy") != "2" or
514        dev1.group_request("GET p2p_go_ctwindow") != "7"):
515        raise Exception("Unexpected configuration value")
516
517    remove_group(dev1, dev2)
518    dev1.global_request("P2P_FLUSH")
519    dev2.global_request("P2P_FLUSH")
520
521def test_p2p_device_conf(dev, apdev):
522    """P2P configuration with cfg80211 P2P Device"""
523    with HWSimRadio(use_p2p_device=True) as (radio, iface):
524        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
525        wpas.interface_add(iface)
526        wpas.global_request("SET p2p_no_group_iface 1")
527        p2p_device_group_conf(wpas, dev[0])
528        wpas.global_request("SET p2p_no_group_iface 0")
529        p2p_device_group_conf(wpas, dev[0])
530
531def test_p2p_device_autogo_chan_switch(dev):
532    """P2P autonomous GO switching channels with cfg80211 P2P Device"""
533    with HWSimRadio(use_p2p_device=True) as (radio, iface):
534        wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
535        wpas.interface_add(iface)
536        wpas.global_request("SET p2p_no_group_iface 1")
537        autogo(wpas, freq=2417)
538        connect_cli(wpas, dev[1])
539        res = wpas.group_request("CHAN_SWITCH 5 2422")
540        if "FAIL" in res:
541            # for now, skip test since mac80211_hwsim support is not yet widely
542            # deployed
543            raise HwsimSkip("Assume mac80211_hwsim did not support channel switching")
544        ev = wpas.wait_group_event(["AP-CSA-FINISHED"], timeout=10)
545        if ev is None:
546            raise Exception("CSA finished event timed out")
547        if "freq=2422" not in ev:
548            raise Exception("Unexpected cahnnel in CSA finished event")
549        wpas.dump_monitor()
550        dev[1].dump_monitor()
551        time.sleep(0.1)
552        hwsim_utils.test_connectivity_p2p(wpas, dev[1])
553