2 # Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 logger = logging.getLogger()
11 from wpasupplicant import WpaSupplicant
12 from test_p2p_grpform import go_neg_pin_authorized
13 from test_p2p_grpform import check_grpform_results
14 from test_p2p_grpform import remove_group
15 from test_nfc_p2p import set_ip_addr_info, check_ip_addr, grpform_events
16 from hwsim import HWSimRadio
20 def test_p2p_device_grpform(dev, apdev):
21 """P2P group formation with driver using cfg80211 P2P Device"""
22 with HWSimRadio(use_p2p_device=True) as (radio, iface):
23 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
24 wpas.interface_add(iface)
25 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
26 r_dev=wpas, r_intent=0)
27 check_grpform_results(i_res, r_res)
28 remove_group(dev[0], wpas)
30 res = wpas.global_request("IFNAME=p2p-dev-" + iface + " STATUS-DRIVER")
31 lines = res.splitlines()
35 [name,value] = l.split('=', 1)
42 raise Exception("wdev_id not found")
44 def test_p2p_device_grpform2(dev, apdev):
45 """P2P group formation with driver using cfg80211 P2P Device (reverse)"""
46 with HWSimRadio(use_p2p_device=True) as (radio, iface):
47 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
48 wpas.interface_add(iface)
49 [i_res, r_res] = go_neg_pin_authorized(i_dev=wpas, i_intent=15,
50 r_dev=dev[0], r_intent=0)
51 check_grpform_results(i_res, r_res)
52 remove_group(wpas, dev[0])
54 def test_p2p_device_group_remove(dev, apdev):
55 """P2P group removal via the P2P ctrl interface with driver using cfg80211 P2P Device"""
56 with HWSimRadio(use_p2p_device=True) as (radio, iface):
57 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
58 wpas.interface_add(iface)
59 [i_res, r_res] = go_neg_pin_authorized(i_dev=dev[0], i_intent=15,
60 r_dev=wpas, r_intent=0)
61 check_grpform_results(i_res, r_res)
62 # Issue the remove request on the interface which will be removed
63 p2p_iface_wpas = WpaSupplicant(ifname=r_res['ifname'])
64 res = p2p_iface_wpas.request("P2P_GROUP_REMOVE *")
66 raise Exception("Failed to remove P2P group")
67 ev = wpas.wait_global_event(["P2P-GROUP-REMOVED"], timeout=10)
69 raise Exception("Group removal event not received")
70 if not wpas.global_ping():
71 raise Exception("Could not ping global ctrl_iface after group removal")
73 def test_p2p_device_concurrent_scan(dev, apdev):
74 """Concurrent P2P and station mode scans with driver using cfg80211 P2P Device"""
75 with HWSimRadio(use_p2p_device=True) as (radio, iface):
76 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
77 wpas.interface_add(iface)
81 ev = wpas.wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=15)
83 raise Exception("Station mode scan did not start")
85 def test_p2p_device_nfc_invite(dev, apdev):
86 """P2P NFC invitiation with driver using cfg80211 P2P Device"""
87 with HWSimRadio(use_p2p_device=True) as (radio, iface):
88 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
89 wpas.interface_add(iface)
91 set_ip_addr_info(dev[0])
92 logger.info("Start autonomous GO")
95 logger.info("Write NFC Tag on the P2P Client")
96 res = wpas.global_request("P2P_LISTEN")
98 raise Exception("Failed to start Listen mode")
99 pw = wpas.global_request("WPS_NFC_TOKEN NDEF").rstrip()
101 raise Exception("Failed to generate password token")
102 res = wpas.global_request("P2P_SET nfc_tag 1").rstrip()
104 raise Exception("Failed to enable NFC Tag for P2P static handover")
105 sel = wpas.global_request("NFC_GET_HANDOVER_SEL NDEF P2P-CR-TAG").rstrip()
107 raise Exception("Failed to generate NFC connection handover select")
109 logger.info("Read NFC Tag on the GO to trigger invitation")
110 res = dev[0].global_request("WPS_NFC_TAG_READ " + sel)
112 raise Exception("Failed to provide NFC tag contents to wpa_supplicant")
114 ev = wpas.wait_global_event(grpform_events, timeout=20)
116 raise Exception("Joining the group timed out")
117 res = wpas.group_form_result(ev)
118 hwsim_utils.test_connectivity_p2p(dev[0], wpas)
121 def test_p2p_device_misuses(dev, apdev):
122 """cfg80211 P2P Device misuses"""
123 hapd = hostapd.add_ap(apdev[0]['ifname'], { "ssid": "open" })
124 with HWSimRadio(use_p2p_device=True) as (radio, iface):
125 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
126 wpas.interface_add(iface)
128 # Add a normal network profile to the P2P Device management only
129 # interface to verify that it does not get used.
130 id = int(wpas.global_request('IFNAME=p2p-dev-%s ADD_NETWORK' % iface).strip())
131 wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d ssid "open"' % (iface, id))
132 wpas.global_request('IFNAME=p2p-dev-%s SET_NETWORK %d key_mgmt NONE' % (iface, id))
133 wpas.global_request('IFNAME=p2p-dev-%s ENABLE_NETWORK %d' % (iface, id))
135 # Scan requests get ignored on p2p-dev
136 wpas.global_request('IFNAME=p2p-dev-%s SCAN' % iface)
138 dev[0].p2p_start_go(freq=2412)
139 addr = dev[0].p2p_interface_addr()
140 wpas.scan_for_bss(addr, freq=2412)
141 wpas.connect("open", key_mgmt="NONE", scan_freq="2412")
142 hwsim_utils.test_connectivity(wpas, hapd)
144 pin = wpas.wps_read_pin()
145 dev[0].p2p_go_authorize_client(pin)
146 res = wpas.p2p_connect_group(dev[0].p2p_dev_addr(), pin, timeout=60,
147 social=True, freq=2412)
148 hwsim_utils.test_connectivity_p2p(dev[0], wpas)
150 # Optimize scan-after-disconnect
151 wpas.group_request("SET_NETWORK 0 scan_freq 2412")
153 dev[0].group_request("DISASSOCIATE " + wpas.p2p_interface_addr())
154 ev = wpas.wait_group_event(["CTRL-EVENT-DISCONNECT"])
156 raise Exception("Did not see disconnect event on P2P group interface")
157 dev[0].remove_group()
159 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
161 raise Exception("Scan not started")
162 ev = wpas.wait_group_event(["CTRL-EVENT-SCAN-RESULTS"], timeout=15)
164 raise Exception("Scan not completed")
166 hwsim_utils.test_connectivity(wpas, hapd)
168 ev = hapd.wait_event([ "AP-STA-DISCONNECTED" ], timeout=0.1)
170 raise Exception("Unexpected disconnection event received from hostapd")
171 ev = wpas.wait_event(["CTRL-EVENT-DISCONNECTED"], timeout=0.1)
173 raise Exception("Unexpected disconnection event received from wpa_supplicant")
175 wpas.request("DISCONNECT")
176 wpas.wait_disconnected()
178 def test_p2p_device_incorrect_command_interface(dev, apdev):
179 """cfg80211 P2P Device and P2P_* command on incorrect interface"""
180 with HWSimRadio(use_p2p_device=True) as (radio, iface):
181 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
182 wpas.interface_add(iface)
185 wpas.request('P2P_FIND type=social')
186 ev = wpas.wait_global_event(["P2P-DEVICE-FOUND"], timeout=10)
188 raise Exception("Peer not found")
189 ev = wpas.wait_event(["P2P-DEVICE-FOUND"], timeout=0.1)
191 raise Exception("Unexpected P2P-DEVICE-FOUND event on station interface")
193 pin = wpas.wps_read_pin()
194 dev[0].p2p_go_neg_auth(wpas.p2p_dev_addr(), pin, "enter", go_intent=14,
196 wpas.request('P2P_STOP_FIND')
197 if "OK" not in wpas.request('P2P_CONNECT ' + dev[0].p2p_dev_addr() + ' ' + pin + ' display go_intent=1'):
198 raise Exception("P2P_CONNECT failed")
200 ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
202 raise Exception("Group formation timed out")
203 wpas.group_form_result(ev)
205 ev = dev[0].wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
207 raise Exception("Group formation timed out(2)")
208 dev[0].group_form_result(ev)
210 dev[0].remove_group()
211 wpas.wait_go_ending_session()
213 def test_p2p_device_incorrect_command_interface2(dev, apdev):
214 """cfg80211 P2P Device and P2P_GROUP_ADD command on incorrect interface"""
215 with HWSimRadio(use_p2p_device=True) as (radio, iface):
216 wpas = WpaSupplicant(global_iface='/tmp/wpas-wlan5')
217 wpas.interface_add(iface)
219 print wpas.request('P2P_GROUP_ADD')
220 ev = wpas.wait_global_event(["P2P-GROUP-STARTED"], timeout=15)
222 raise Exception("Group formation timed out")
223 res = wpas.group_form_result(ev)
224 logger.info("Group results: " + str(res))
226 if not res['ifname'].startswith('p2p-' + iface + '-'):
227 raise Exception("Unexpected group ifname: " + res['ifname'])