1 # P2P device discovery test cases
2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
7 from remotehost
import remote_compatible
9 logger
= logging
.getLogger()
14 from wpasupplicant
import WpaSupplicant
15 from p2p_utils
import *
16 from hwsim
import HWSimRadio
17 from tshark
import run_tshark
18 from test_gas
import start_ap
19 from test_cfg80211
import nl80211_remain_on_channel
20 from test_p2p_channel
import set_country
23 def test_discovery(dev
):
24 """P2P device discovery and provision discovery"""
25 addr0
= dev
[0].p2p_dev_addr()
26 addr1
= dev
[1].p2p_dev_addr()
27 logger
.info("Start device discovery")
28 dev
[0].p2p_find(social
=True, delay
=1)
29 if not dev
[1].discover_peer(addr0
):
30 raise Exception("Device discovery timed out")
31 if not dev
[0].discover_peer(addr1
):
32 raise Exception("Device discovery timed out")
34 logger
.info("Test provision discovery for display")
35 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " display")
36 ev1
= dev
[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout
=15)
38 raise Exception("Provision discovery timed out (display/dev1)")
40 raise Exception("Dev0 not in provision discovery event")
41 ev0
= dev
[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
42 "P2P-PROV-DISC-FAILURE"], timeout
=15)
44 raise Exception("Provision discovery timed out (display/dev0)")
45 if "P2P-PROV-DISC-FAILURE" in ev0
:
46 raise Exception("Provision discovery failed (display/dev0)")
48 raise Exception("Dev1 not in provision discovery event")
50 logger
.info("Test provision discovery for keypad")
51 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " keypad")
52 ev1
= dev
[1].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout
=15)
54 raise Exception("Provision discovery timed out (keypad/dev1)")
56 raise Exception("Dev0 not in provision discovery event")
57 ev0
= dev
[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN",
58 "P2P-PROV-DISC-FAILURE"],
61 raise Exception("Provision discovery timed out (keypad/dev0)")
62 if "P2P-PROV-DISC-FAILURE" in ev0
:
63 raise Exception("Provision discovery failed (keypad/dev0)")
65 raise Exception("Dev1 not in provision discovery event")
67 logger
.info("Test provision discovery for push button")
68 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " pbc")
69 ev1
= dev
[1].wait_global_event(["P2P-PROV-DISC-PBC-REQ"], timeout
=15)
71 raise Exception("Provision discovery timed out (pbc/dev1)")
73 raise Exception("Dev0 not in provision discovery event")
74 ev0
= dev
[0].wait_global_event(["P2P-PROV-DISC-PBC-RESP",
75 "P2P-PROV-DISC-FAILURE"],
78 raise Exception("Provision discovery timed out (pbc/dev0)")
79 if "P2P-PROV-DISC-FAILURE" in ev0
:
80 raise Exception("Provision discovery failed (pbc/dev0)")
82 raise Exception("Dev1 not in provision discovery event")
87 if "FAIL" not in dev
[0].p2p_find(dev_id
="foo"):
88 raise Exception("P2P_FIND with invalid dev_id accepted")
89 if "FAIL" not in dev
[0].p2p_find(dev_type
="foo"):
90 raise Exception("P2P_FIND with invalid dev_type accepted")
91 if "FAIL" not in dev
[0].p2p_find(dev_type
="1-foo-2"):
92 raise Exception("P2P_FIND with invalid dev_type accepted")
93 if "FAIL" not in dev
[0].p2p_find(dev_type
="1-11223344"):
94 raise Exception("P2P_FIND with invalid dev_type accepted")
96 if "FAIL" not in dev
[0].global_request("P2P_PROV_DISC foo pbc"):
97 raise Exception("Invalid P2P_PROV_DISC accepted")
98 if "FAIL" not in dev
[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55"):
99 raise Exception("Invalid P2P_PROV_DISC accepted")
100 if "FAIL" not in dev
[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55 pbc join"):
101 raise Exception("Invalid P2P_PROV_DISC accepted")
102 if "FAIL" not in dev
[0].global_request("P2P_PROV_DISC 00:11:22:33:44:55 foo"):
103 raise Exception("Invalid P2P_PROV_DISC accepted")
106 def test_discovery_pd_retries(dev
):
107 """P2P device discovery and provision discovery retries"""
108 addr0
= dev
[0].p2p_dev_addr()
109 addr1
= dev
[1].p2p_dev_addr()
111 if not dev
[0].discover_peer(addr1
):
112 raise Exception("Device discovery timed out")
113 dev
[1].p2p_stop_find()
114 dev
[0].p2p_stop_find()
115 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " display")
116 ev
= dev
[0].wait_global_event(["P2P-PROV-DISC-FAILURE"], timeout
=60)
118 raise Exception("No PD failure reported")
120 def test_discovery_group_client(dev
):
121 """P2P device discovery for a client in a group"""
122 logger
.info("Start autonomous GO " + dev
[0].ifname
)
123 res
= dev
[0].p2p_start_go(freq
="2422")
124 logger
.debug("res: " + str(res
))
125 logger
.info("Connect a client to the GO")
126 pin
= dev
[1].wps_read_pin()
127 dev
[0].p2p_go_authorize_client(pin
)
128 dev
[1].p2p_connect_group(dev
[0].p2p_dev_addr(), pin
, freq
=int(res
['freq']),
130 logger
.info("Client connected")
131 hwsim_utils
.test_connectivity_p2p(dev
[0], dev
[1])
132 logger
.info("Try to discover a P2P client in a group")
133 if not dev
[2].discover_peer(dev
[1].p2p_dev_addr(), social
=False, timeout
=10):
134 stop_p2p_find_and_wait(dev
[2])
135 if not dev
[2].discover_peer(dev
[1].p2p_dev_addr(), social
=False, timeout
=10):
136 stop_p2p_find_and_wait(dev
[2])
137 if not dev
[2].discover_peer(dev
[1].p2p_dev_addr(), social
=False, timeout
=10):
138 raise Exception("Could not discover group client")
140 # This is not really perfect, but something to get a bit more testing
141 # coverage.. For proper discoverability mechanism validation, the P2P
142 # client would need to go to sleep to avoid acknowledging the GO Negotiation
143 # Request frame. Offchannel Listen mode operation on the P2P Client with
144 # mac80211_hwsim is apparently not enough to avoid the acknowledgement on
145 # the operating channel, so need to disconnect from the group which removes
146 # the GO-to-P2P Client part of the discoverability exchange in practice.
148 pin
= dev
[2].wps_read_pin()
149 # make group client non-responsive on operating channel
150 dev
[1].dump_monitor()
151 dev
[1].group_request("DISCONNECT")
152 ev
= dev
[1].wait_group_event(["CTRL-EVENT-DISCONNECTED"], timeout
=10)
154 raise Exception("Timeout on waiting disconnection")
155 dev
[2].request("P2P_CONNECT {} {} display".format(dev
[1].p2p_dev_addr(),
157 ev
= dev
[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout
=2)
159 raise Exception("Unexpected frame RX on P2P client")
160 # make group client available on operating channe
161 dev
[1].group_request("REASSOCIATE")
162 ev
= dev
[1].wait_global_event(["CTRL-EVENT-CONNECTED",
163 "P2P-GO-NEG-REQUEST"], timeout
=10)
165 raise Exception("Timeout on reconnection to group")
166 if "P2P-GO-NEG-REQUEST" not in ev
:
167 ev
= dev
[1].wait_global_event(["P2P-GO-NEG-REQUEST"], timeout
=10)
169 raise Exception("Timeout on waiting for GO Negotiation Request")
171 def stop_p2p_find_and_wait(dev
):
172 dev
.request("P2P_STOP_FIND")
174 res
= dev
.get_driver_status_field("scan_state")
175 if "SCAN_STARTED" not in res
and "SCAN_REQUESTED" not in res
:
177 logger
.debug("Waiting for final P2P_FIND scan to complete")
180 def test_discovery_ctrl_char_in_devname(dev
):
181 """P2P device discovery and control character in Device Name"""
183 _test_discovery_ctrl_char_in_devname(dev
)
185 dev
[1].global_request("SET device_name Device B")
187 def _test_discovery_ctrl_char_in_devname(dev
):
188 dev
[1].global_request("SET device_name Device\tB")
189 addr0
= dev
[0].p2p_dev_addr()
190 addr1
= dev
[1].p2p_dev_addr()
191 res
= dev
[0].p2p_start_go(freq
=2422)
192 bssid
= dev
[0].p2p_interface_addr()
193 pin
= dev
[1].wps_read_pin()
194 dev
[0].p2p_go_authorize_client(pin
)
195 dev
[1].scan_for_bss(bssid
, freq
=2422)
196 dev
[1].p2p_connect_group(addr0
, pin
, timeout
=60, freq
=2422)
197 if not dev
[2].discover_peer(addr1
, social
=False, freq
=2422, timeout
=5):
198 stop_p2p_find_and_wait(dev
[2])
199 if not dev
[2].discover_peer(addr1
, social
=False, freq
=2422, timeout
=5):
200 stop_p2p_find_and_wait(dev
[2])
201 if not dev
[2].discover_peer(addr1
, social
=False, freq
=2422,
203 raise Exception("Could not discover group client")
204 devname
= dev
[2].get_peer(addr1
)['device_name']
205 dev
[2].p2p_stop_find()
206 if devname
!= "Device_B":
207 raise Exception("Unexpected device_name from group client: " + devname
)
209 terminate_group(dev
[0], dev
[1])
210 dev
[2].request("P2P_FLUSH")
213 if not dev
[2].discover_peer(addr1
, social
=True, timeout
=10):
214 raise Exception("Could not discover peer")
215 devname
= dev
[2].get_peer(addr1
)['device_name']
216 dev
[2].p2p_stop_find()
217 if devname
!= "Device_B":
218 raise Exception("Unexpected device_name from peer: " + devname
)
221 def test_discovery_dev_type(dev
):
222 """P2P device discovery with Device Type filter"""
223 dev
[1].request("SET sec_device_type 1-0050F204-2")
225 dev
[0].p2p_find(social
=True, dev_type
="5-0050F204-1")
226 ev
= dev
[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=1)
228 raise Exception("Unexpected P2P device found")
229 dev
[0].p2p_find(social
=True, dev_type
="1-0050F204-2")
230 ev
= dev
[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=2)
232 raise Exception("P2P device not found")
233 peer
= dev
[0].get_peer(dev
[1].p2p_dev_addr())
234 if "1-0050F204-2" not in peer
['sec_dev_type']:
235 raise Exception("sec_device_type not reported properly")
237 def test_discovery_dev_type_go(dev
):
238 """P2P device discovery with Device Type filter on GO"""
239 addr1
= dev
[1].p2p_dev_addr()
240 dev
[1].request("SET sec_device_type 1-0050F204-2")
241 res
= dev
[0].p2p_start_go(freq
="2412")
242 pin
= dev
[1].wps_read_pin()
243 dev
[0].p2p_go_authorize_client(pin
)
244 dev
[1].p2p_connect_group(dev
[0].p2p_dev_addr(), pin
, timeout
=60)
246 dev
[2].p2p_find(social
=True, dev_type
="5-0050F204-1")
247 ev
= dev
[2].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=1)
249 raise Exception("Unexpected P2P device found")
250 dev
[2].p2p_find(social
=True, dev_type
="1-0050F204-2")
251 ev
= dev
[2].wait_global_event(['P2P-DEVICE-FOUND ' + addr1
], timeout
=2)
253 raise Exception("P2P device not found")
255 def test_discovery_dev_id(dev
):
256 """P2P device discovery with Device ID filter"""
257 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
258 wpas
.interface_add("wlan5")
259 wpas
.request("P2P_LISTEN 1")
260 status
= wpas
.global_request("STATUS")
261 if "p2p_state=LISTEN_ONLY" not in status
:
262 raise Exception("Unexpected status: " + status
)
263 addr1
= dev
[1].p2p_dev_addr()
265 dev
[0].p2p_find(social
=True, dev_id
="02:03:04:05:06:07")
266 ev
= dev
[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=1)
268 raise Exception("Unexpected P2P device found")
269 dev
[0].p2p_find(social
=True, dev_id
=addr1
)
270 ev
= dev
[0].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=5)
272 raise Exception("P2P device not found")
274 raise Exception("Unexpected P2P peer found")
275 status
= wpas
.global_request("STATUS")
276 for i
in range(0, 2):
277 if "p2p_state=IDLE" in status
:
280 status
= wpas
.global_request("STATUS")
281 if "p2p_state=IDLE" not in status
:
282 raise Exception("Unexpected status: " + status
)
284 def test_discovery_dev_id_go(dev
):
285 """P2P device discovery with Device ID filter on GO"""
286 addr1
= dev
[1].p2p_dev_addr()
287 res
= dev
[0].p2p_start_go(freq
="2412")
288 pin
= dev
[1].wps_read_pin()
289 dev
[0].p2p_go_authorize_client(pin
)
290 dev
[1].p2p_connect_group(dev
[0].p2p_dev_addr(), pin
, timeout
=60)
292 dev
[2].p2p_find(social
=True, dev_id
="02:03:04:05:06:07")
293 ev
= dev
[2].wait_global_event(['P2P-DEVICE-FOUND'], timeout
=1)
295 raise Exception("Unexpected P2P device found")
296 dev
[2].p2p_find(social
=True, dev_id
=addr1
)
297 ev
= dev
[2].wait_global_event(['P2P-DEVICE-FOUND ' + addr1
], timeout
=2)
299 raise Exception("P2P device not found")
301 def test_discovery_social_plus_one(dev
):
302 """P2P device discovery with social-plus-one"""
303 logger
.info("Start autonomous GO " + dev
[0].ifname
)
304 dev
[1].p2p_find(social
=True)
305 dev
[0].p2p_find(progressive
=True)
306 logger
.info("Wait for initial progressive find phases")
307 dev
[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
308 dev
[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
309 go
= dev
[2].p2p_dev_addr()
310 dev
[2].p2p_start_go(freq
="2422")
311 logger
.info("Verify whether the GO on non-social channel can be found")
312 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
314 raise Exception("Peer not found")
316 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
318 raise Exception("Peer not found")
319 ev
= dev
[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
321 raise Exception("Peer not found")
322 dev
[0].p2p_stop_find()
323 dev
[1].p2p_stop_find()
324 if not dev
[0].peer_known(go
):
325 raise Exception("GO not found in progressive scan")
326 if dev
[1].peer_known(go
):
327 raise Exception("GO found in social-only scan")
329 def _test_discovery_and_interface_disabled(dev
, delay
=1):
331 if "OK" not in dev
[0].p2p_find():
332 raise Exception("Failed to start P2P find")
333 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-STARTED"])
335 raise Exception("Scan did not start")
336 dev
[0].request("DRIVER_EVENT INTERFACE_DISABLED")
339 # verify that P2P_FIND is rejected
340 if "FAIL" not in dev
[0].p2p_find():
341 raise Exception("New P2P_FIND request was accepted unexpectedly")
343 dev
[0].request("DRIVER_EVENT INTERFACE_ENABLED")
345 dev
[0].scan(freq
="2412")
346 if "OK" not in dev
[0].p2p_find():
347 raise Exception("Failed to start P2P find")
348 dev
[0].dump_monitor()
350 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
352 raise Exception("Peer not found")
354 dev
[0].request("DRIVER_EVENT INTERFACE_ENABLED")
356 def test_discovery_and_interface_disabled(dev
):
357 """P2P device discovery with interface getting disabled"""
358 _test_discovery_and_interface_disabled(dev
, delay
=1)
359 _test_discovery_and_interface_disabled(dev
, delay
=5)
361 def test_discovery_auto(dev
):
362 """P2P device discovery and provision discovery with auto GO/dev selection"""
363 dev
[0].flush_scan_cache()
364 addr0
= dev
[0].p2p_dev_addr()
365 addr1
= dev
[1].p2p_dev_addr()
366 addr2
= dev
[2].p2p_dev_addr()
367 dev
[2].p2p_start_go(freq
="2412")
368 logger
.info("Start device discovery")
370 if not dev
[1].discover_peer(addr0
):
371 raise Exception("Device discovery timed out")
373 if not dev
[0].discover_peer(addr1
):
374 raise Exception("Device discovery timed out")
375 if not dev
[0].discover_peer(addr2
):
376 raise Exception("Device discovery timed out")
378 logger
.info("Test provision discovery for display (device)")
379 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " display auto")
380 ev1
= dev
[1].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout
=15)
382 raise Exception("Provision discovery timed out (display/dev1)")
384 raise Exception("Dev0 not in provision discovery event")
386 raise Exception("Unexpected group parameter from non-GO")
387 ev0
= dev
[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
388 "P2P-PROV-DISC-FAILURE"], timeout
=15)
390 raise Exception("Provision discovery timed out (display/dev0)")
391 if "P2P-PROV-DISC-FAILURE" in ev0
:
392 raise Exception("Provision discovery failed (display/dev0)")
394 raise Exception("Dev1 not in provision discovery event")
395 if "peer_go=0" not in ev0
:
396 raise Exception("peer_go incorrect in PD response from non-GO")
398 logger
.info("Test provision discovery for display (GO)")
399 dev
[0].global_request("P2P_PROV_DISC " + addr2
+ " display auto")
400 ev2
= dev
[2].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout
=15)
402 raise Exception("Provision discovery timed out (display/dev2)")
404 raise Exception("Dev0 not in provision discovery event")
405 if " group=" not in ev2
:
406 raise Exception("Group parameter missing from GO")
407 ev0
= dev
[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN",
408 "P2P-PROV-DISC-FAILURE"], timeout
=15)
410 raise Exception("Provision discovery timed out (display/dev0)")
411 if "P2P-PROV-DISC-FAILURE" in ev0
:
412 raise Exception("Provision discovery failed (display/dev0)")
414 raise Exception("Dev1 not in provision discovery event")
415 if "peer_go=1" not in ev0
:
416 raise Exception("peer_go incorrect in PD response from GO")
418 def test_discovery_stop(dev
):
419 """P2P device discovery and p2p_stop_find"""
420 addr0
= dev
[0].p2p_dev_addr()
421 addr1
= dev
[1].p2p_dev_addr()
425 dev
[0].p2p_find(social
=False)
426 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout
=0.5)
428 logger
.info("No CTRL-EVENT-SCAN-STARTED event")
429 dev
[0].p2p_stop_find()
430 ev
= dev
[0].wait_global_event(["P2P-FIND-STOPPED"], timeout
=1)
432 raise Exception("P2P_STOP not reported")
433 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=5)
435 raise Exception("Peer found unexpectedly: " + ev
)
437 dev
[0].p2p_find(social
=False)
438 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout
=0.5)
440 logger
.info("No CTRL-EVENT-SCAN-STARTED event")
441 dev
[0].global_request("P2P_FLUSH")
442 ev
= dev
[0].wait_global_event(["P2P-FIND-STOPPED"], timeout
=1)
444 raise Exception("P2P_STOP not reported")
445 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=5)
447 raise Exception("Peer found unexpectedly: " + ev
)
449 def test_discovery_restart(dev
):
450 """P2P device discovery and p2p_find restart"""
451 autogo(dev
[1], freq
=2457)
452 dev
[0].p2p_find(social
=True)
453 dev
[0].p2p_stop_find()
454 dev
[0].p2p_find(social
=False)
455 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=7)
457 dev
[0].p2p_find(social
=False)
458 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=7)
460 raise Exception("Peer not found")
462 def test_discovery_restart_progressive(dev
):
463 """P2P device discovery and p2p_find type=progressive restart"""
465 set_country("US", dev
[1])
466 autogo(dev
[1], freq
=5805)
467 dev
[0].p2p_find(social
=True)
468 dev
[0].p2p_stop_find()
469 dev
[0].p2p_find(progressive
=True)
470 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=20)
471 dev
[1].remove_group()
473 raise Exception("Peer not found")
476 dev
[1].flush_scan_cache()
478 def test_p2p_peer_command(dev
):
479 """P2P_PEER command"""
480 addr0
= dev
[0].p2p_dev_addr()
481 addr1
= dev
[1].p2p_dev_addr()
482 addr2
= dev
[2].p2p_dev_addr()
485 if not dev
[0].discover_peer(addr1
):
486 raise Exception("Device discovery timed out")
487 if not dev
[0].discover_peer(addr2
):
488 raise Exception("Device discovery timed out")
489 dev
[0].p2p_stop_find()
490 dev
[1].p2p_stop_find()
491 dev
[2].p2p_stop_find()
493 res0
= dev
[0].request("P2P_PEER FIRST")
494 peer
= res0
.splitlines()[0]
495 if peer
not in [addr1
, addr2
]:
496 raise Exception("Unexpected P2P_PEER FIRST address")
497 res1
= dev
[0].request("P2P_PEER NEXT-" + peer
)
498 peer2
= res1
.splitlines()[0]
499 if peer2
not in [addr1
, addr2
] or peer
== peer2
:
500 raise Exception("Unexpected P2P_PEER NEXT address")
502 if "FAIL" not in dev
[0].request("P2P_PEER NEXT-foo"):
503 raise Exception("Invalid P2P_PEER command accepted")
504 if "FAIL" not in dev
[0].request("P2P_PEER foo"):
505 raise Exception("Invalid P2P_PEER command accepted")
506 if "FAIL" not in dev
[0].request("P2P_PEER 00:11:22:33:44:55"):
507 raise Exception("P2P_PEER command for unknown peer accepted")
509 def test_p2p_listen_and_offchannel_tx(dev
):
510 """P2P_LISTEN behavior with offchannel TX"""
511 addr0
= dev
[0].p2p_dev_addr()
512 addr1
= dev
[1].p2p_dev_addr()
513 addr2
= dev
[2].p2p_dev_addr()
516 if not dev
[0].discover_peer(addr1
):
517 raise Exception("Device discovery timed out")
520 dev
[0].global_request("P2P_PROV_DISC " + addr1
+ " display")
521 ev
= dev
[0].wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout
=15)
523 raise Exception("No PD result reported")
524 dev
[1].p2p_stop_find()
526 if not dev
[2].discover_peer(addr0
):
527 raise Exception("Device discovery timed out after PD exchange")
528 dev
[2].p2p_stop_find()
529 dev
[0].p2p_stop_find()
532 def test_p2p_listen_and_scan(dev
):
533 """P2P_LISTEN and scan"""
535 if "OK" not in dev
[0].request("SCAN freq=2412"):
536 raise Exception("Failed to request a scan")
537 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 3)
539 raise Exception("Unexpected scan results")
540 dev
[0].p2p_stop_find()
541 ev
= dev
[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
543 raise Exception("Scan timed out")
545 def test_p2p_config_methods(dev
):
546 """P2P and WPS config method update"""
547 addr0
= dev
[0].p2p_dev_addr()
548 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
549 wpas
.interface_add("wlan5")
550 addr1
= wpas
.p2p_dev_addr()
552 if "OK" not in wpas
.request("SET config_methods keypad virtual_push_button"):
553 raise Exception("Failed to set config_methods")
556 if not dev
[0].discover_peer(addr1
):
557 raise Exception("Device discovery timed out")
558 dev
[0].p2p_stop_find()
559 peer
= dev
[0].get_peer(addr1
)
560 if peer
['config_methods'] != '0x180':
561 raise Exception("Unexpected peer config methods(1): " + peer
['config_methods'])
562 dev
[0].global_request("P2P_FLUSH")
564 if "OK" not in wpas
.request("SET config_methods virtual_display"):
565 raise Exception("Failed to set config_methods")
567 if not dev
[0].discover_peer(addr1
):
568 raise Exception("Device discovery timed out")
569 dev
[0].p2p_stop_find()
570 peer
= dev
[0].get_peer(addr1
)
571 if peer
['config_methods'] != '0x8':
572 raise Exception("Unexpected peer config methods(2): " + peer
['config_methods'])
577 def test_discovery_after_gas(dev
, apdev
):
578 """P2P device discovery after GAS/ANQP exchange"""
579 hapd
= start_ap(apdev
[0])
580 hapd
.set("gas_frag_limit", "50")
581 dev
[0].scan_for_bss(apdev
[0]['bssid'], freq
="2412", force_scan
=True)
582 dev
[0].request("FETCH_ANQP")
583 ev
= dev
[0].wait_event(["ANQP-QUERY-DONE"], timeout
=10)
585 raise Exception("No ANQP-QUERY-DONE event")
586 dev
[0].dump_monitor()
588 start
= os
.times()[4]
590 dev
[1].p2p_find(social
=True)
591 ev
= dev
[1].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=5)
593 raise Exception("Peer not discovered")
595 dev
[0].dump_monitor()
596 dev
[0].p2p_stop_find()
597 dev
[1].p2p_stop_find()
598 logger
.info("Device discovery after fragmented GAS took %f seconds" % (end
- start
))
599 if end
- start
> 1.3:
600 raise Exception("Device discovery took unexpectedly long time")
603 def test_discovery_listen_find(dev
):
604 """P2P_LISTEN immediately followed by P2P_FIND"""
605 # Request an external remain-on-channel operation to delay start of the ROC
606 # for the following p2p_listen() enough to get p2p_find() processed before
607 # the ROC started event shows up. This is done to test a code path where the
608 # p2p_find() needs to clear the wait for the pending listen operation
609 # (p2p->pending_listen_freq).
610 ifindex
= int(dev
[0].get_driver_status_field("ifindex"))
611 nl80211_remain_on_channel(dev
[0], ifindex
, 2417, 200)
613 addr0
= dev
[0].p2p_dev_addr()
615 dev
[0].p2p_find(social
=True)
618 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=1.2)
619 if not dev
[1].discover_peer(addr0
):
620 raise Exception("Device discovery timed out")
622 raise Exception("Did not find peer quickly enough after stopped P2P_LISTEN")
624 def test_discovery_long_listen(dev
):
625 """Long P2P_LISTEN and offchannel TX"""
626 addr0
= dev
[0].p2p_dev_addr()
629 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
630 wpas
.interface_add("wlan5")
631 addr
= wpas
.p2p_dev_addr()
632 if not wpas
.discover_peer(addr0
):
633 raise Exception("Device discovery timed out")
634 peer
= wpas
.get_peer(addr0
)
635 chan
= '1' if peer
['listen_freq'] == '2462' else '11'
637 wpas
.request("P2P_SET listen_channel " + chan
)
638 wpas
.request("P2P_LISTEN 10")
639 if not dev
[0].discover_peer(addr
):
640 raise Exception("Device discovery timed out (2)")
643 wpas
.global_request("P2P_PROV_DISC " + addr0
+ " display")
644 ev
= dev
[0].wait_global_event(["P2P-PROV-DISC-SHOW-PIN"], timeout
=15)
646 raise Exception("Provision discovery timed out")
647 dev
[0].p2p_stop_find()
649 # Verify that the long listen period is still continuing after off-channel
650 # TX of Provision Discovery frames.
651 if not dev
[1].discover_peer(addr
):
652 raise Exception("Device discovery timed out (3)")
654 dev
[1].p2p_stop_find()
657 def pd_test(dev
, addr
):
658 if not dev
.discover_peer(addr
, freq
=2412):
659 raise Exception("Device discovery timed out")
660 dev
.global_request("P2P_PROV_DISC " + addr
+ " display")
661 ev0
= dev
.wait_global_event(["P2P-PROV-DISC-ENTER-PIN"], timeout
=15)
663 raise Exception("Provision discovery timed out (display)")
666 def run_discovery_while_go(wpas
, dev
, params
):
667 wpas
.request("P2P_SET listen_channel 1")
668 wpas
.p2p_start_go(freq
="2412")
669 addr
= wpas
.p2p_dev_addr()
670 pin
= dev
[0].wps_read_pin()
671 wpas
.p2p_go_authorize_client(pin
)
672 dev
[1].p2p_connect_group(addr
, pin
, freq
=2412, timeout
=30)
674 pd_test(dev
[0], addr
)
676 pd_test(dev
[2], addr
)
679 terminate_group(wpas
, dev
[1])
681 out
= run_tshark(os
.path
.join(params
['logdir'], "hwsim0.pcapng"),
682 "wifi_p2p.public_action.subtype == 8", ["wlan.da"])
683 da
= out
.splitlines()
684 logger
.info("PD Response DAs: " + str(da
))
686 raise Exception("Unexpected DA count for PD Response")
688 def test_discovery_while_go(dev
, apdev
, params
):
689 """P2P provision discovery from GO"""
690 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
691 wpas
.interface_add("wlan5")
692 run_discovery_while_go(wpas
, dev
, params
)
694 def test_discovery_while_go_p2p_dev(dev
, apdev
, params
):
695 """P2P provision discovery from GO (using P2P Device interface)"""
696 with
HWSimRadio(use_p2p_device
=True) as (radio
, iface
):
697 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
698 wpas
.interface_add(iface
)
699 run_discovery_while_go(wpas
, dev
, params
)
701 def run_discovery_while_cli(wpas
, dev
, params
):
702 wpas
.request("P2P_SET listen_channel 1")
703 dev
[1].p2p_start_go(freq
="2412")
704 addr
= wpas
.p2p_dev_addr()
705 pin
= wpas
.wps_read_pin()
706 dev
[1].p2p_go_authorize_client(pin
)
707 wpas
.p2p_connect_group(dev
[1].p2p_dev_addr(), pin
, freq
=2412, timeout
=30)
709 pd_test(dev
[0], addr
)
711 pd_test(dev
[2], addr
)
714 terminate_group(dev
[1], wpas
)
716 out
= run_tshark(os
.path
.join(params
['logdir'], "hwsim0.pcapng"),
717 "wifi_p2p.public_action.subtype == 8", ["wlan.da"])
718 da
= out
.splitlines()
719 logger
.info("PD Response DAs: " + str(da
))
721 raise Exception("Unexpected DA count for PD Response")
723 def test_discovery_while_cli(dev
, apdev
, params
):
724 """P2P provision discovery from CLI"""
725 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
726 wpas
.interface_add("wlan5")
727 run_discovery_while_cli(wpas
, dev
, params
)
729 def test_discovery_while_cli_p2p_dev(dev
, apdev
, params
):
730 """P2P provision discovery from CLI (using P2P Device interface)"""
731 with
HWSimRadio(use_p2p_device
=True) as (radio
, iface
):
732 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
733 wpas
.interface_add(iface
)
734 run_discovery_while_cli(wpas
, dev
, params
)
736 def test_discovery_device_name_change(dev
):
737 """P2P device discovery and peer changing device name"""
738 wpas
= WpaSupplicant(global_iface
='/tmp/wpas-wlan5')
739 wpas
.interface_add("wlan5")
740 wpas
.set("device_name", "test-a")
742 dev
[0].p2p_find(social
=True)
743 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
745 raise Exception("Peer not found")
746 if "new=1" not in ev
:
747 raise Exception("Incorrect new event: " + ev
)
748 if "name='test-a'" not in ev
:
749 raise Exception("Unexpected device name(1): " + ev
)
751 # Verify that new P2P-DEVICE-FOUND event is indicated when the peer changes
753 wpas
.set("device_name", "test-b")
754 ev
= dev
[0].wait_global_event(["P2P-DEVICE-FOUND"], timeout
=15)
756 raise Exception("Peer update not seen")
757 if "new=0" not in ev
:
758 raise Exception("Incorrect update event: " + ev
)
759 if "name='test-b'" not in ev
:
760 raise Exception("Unexpected device name(2): " + ev
)
762 dev
[0].p2p_stop_find()