Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # |
| 3 | # Example nfcpy to wpa_supplicant wrapper for DPP NFC operations |
| 4 | # Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi> |
| 5 | # Copyright (c) 2019-2020, The Linux Foundation |
| 6 | # |
| 7 | # This software may be distributed under the terms of the BSD license. |
| 8 | # See README for more details. |
| 9 | |
| 10 | import os |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 11 | import struct |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 12 | import sys |
| 13 | import time |
| 14 | import threading |
| 15 | import argparse |
| 16 | |
| 17 | import nfc |
| 18 | import ndef |
| 19 | |
| 20 | import logging |
| 21 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 22 | scriptsdir = os.path.dirname(os.path.realpath(sys.modules[__name__].__file__)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 23 | sys.path.append(os.path.join(scriptsdir, '..', '..', 'wpaspy')) |
| 24 | import wpaspy |
| 25 | |
| 26 | wpas_ctrl = '/var/run/wpa_supplicant' |
| 27 | ifname = None |
| 28 | init_on_touch = False |
| 29 | in_raw_mode = False |
| 30 | prev_tcgetattr = 0 |
| 31 | no_input = False |
| 32 | srv = None |
| 33 | continue_loop = True |
| 34 | terminate_now = False |
| 35 | summary_file = None |
| 36 | success_file = None |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 37 | my_crn_ready = False |
| 38 | my_crn = None |
| 39 | peer_crn = None |
| 40 | hs_sent = False |
| 41 | mutex = threading.Lock() |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 42 | |
| 43 | def summary(txt): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 44 | with mutex: |
| 45 | print(txt) |
| 46 | if summary_file: |
| 47 | with open(summary_file, 'a') as f: |
| 48 | f.write(txt + "\n") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 49 | |
| 50 | def success_report(txt): |
| 51 | summary(txt) |
| 52 | if success_file: |
| 53 | with open(success_file, 'a') as f: |
| 54 | f.write(txt + "\n") |
| 55 | |
| 56 | def wpas_connect(): |
| 57 | ifaces = [] |
| 58 | if os.path.isdir(wpas_ctrl): |
| 59 | try: |
| 60 | ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)] |
| 61 | except OSError as error: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 62 | summary("Could not find wpa_supplicant: %s", str(error)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 63 | return None |
| 64 | |
| 65 | if len(ifaces) < 1: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 66 | summary("No wpa_supplicant control interface found") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 67 | return None |
| 68 | |
| 69 | for ctrl in ifaces: |
| 70 | if ifname: |
| 71 | if ifname not in ctrl: |
| 72 | continue |
| 73 | try: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 74 | summary("Trying to use control interface " + ctrl) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 75 | wpas = wpaspy.Ctrl(ctrl) |
| 76 | return wpas |
| 77 | except Exception as e: |
| 78 | pass |
| 79 | return None |
| 80 | |
| 81 | def dpp_nfc_uri_process(uri): |
| 82 | wpas = wpas_connect() |
| 83 | if wpas is None: |
| 84 | return False |
| 85 | peer_id = wpas.request("DPP_NFC_URI " + uri) |
| 86 | if "FAIL" in peer_id: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 87 | summary("Could not parse DPP URI from NFC URI record") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 88 | return False |
| 89 | peer_id = int(peer_id) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 90 | summary("peer_id=%d for URI from NFC Tag: %s" % (peer_id, uri)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 91 | cmd = "DPP_AUTH_INIT peer=%d" % peer_id |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 92 | global enrollee_only, configurator_only, config_params |
| 93 | if enrollee_only: |
| 94 | cmd += " role=enrollee" |
| 95 | elif configurator_only: |
| 96 | cmd += " role=configurator" |
| 97 | if config_params: |
| 98 | cmd += " " + config_params |
| 99 | summary("Initiate DPP authentication: " + cmd) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 100 | res = wpas.request(cmd) |
| 101 | if "OK" not in res: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 102 | summary("Failed to initiate DPP Authentication") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 103 | return False |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 104 | summary("DPP Authentication initiated") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 105 | return True |
| 106 | |
| 107 | def dpp_hs_tag_read(record): |
| 108 | wpas = wpas_connect() |
| 109 | if wpas is None: |
| 110 | return False |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 111 | summary(record) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 112 | if len(record.data) < 5: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 113 | summary("Too short DPP HS") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 114 | return False |
| 115 | if record.data[0] != 0: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 116 | summary("Unexpected URI Identifier Code") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 117 | return False |
| 118 | uribuf = record.data[1:] |
| 119 | try: |
| 120 | uri = uribuf.decode() |
| 121 | except: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 122 | summary("Invalid URI payload") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 123 | return False |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 124 | summary("URI: " + uri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 125 | if not uri.startswith("DPP:"): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 126 | summary("Not a DPP URI") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 127 | return False |
| 128 | return dpp_nfc_uri_process(uri) |
| 129 | |
| 130 | def get_status(wpas, extra=None): |
| 131 | if extra: |
| 132 | extra = "-" + extra |
| 133 | else: |
| 134 | extra = "" |
| 135 | res = wpas.request("STATUS" + extra) |
| 136 | lines = res.splitlines() |
| 137 | vals = dict() |
| 138 | for l in lines: |
| 139 | try: |
| 140 | [name, value] = l.split('=', 1) |
| 141 | except ValueError: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 142 | summary("Ignore unexpected status line: %s" % l) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 143 | continue |
| 144 | vals[name] = value |
| 145 | return vals |
| 146 | |
| 147 | def get_status_field(wpas, field, extra=None): |
| 148 | vals = get_status(wpas, extra) |
| 149 | if field in vals: |
| 150 | return vals[field] |
| 151 | return None |
| 152 | |
| 153 | def own_addr(wpas): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 154 | addr = get_status_field(wpas, "address") |
| 155 | if addr is None: |
| 156 | addr = get_status_field(wpas, "bssid[0]") |
| 157 | return addr |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 158 | |
| 159 | def dpp_bootstrap_gen(wpas, type="qrcode", chan=None, mac=None, info=None, |
| 160 | curve=None, key=None): |
| 161 | cmd = "DPP_BOOTSTRAP_GEN type=" + type |
| 162 | if chan: |
| 163 | cmd += " chan=" + chan |
| 164 | if mac: |
| 165 | if mac is True: |
| 166 | mac = own_addr(wpas) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 167 | if mac is None: |
| 168 | summary("Could not determine local MAC address for bootstrap info") |
| 169 | else: |
| 170 | cmd += " mac=" + mac.replace(':', '') |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 171 | if info: |
| 172 | cmd += " info=" + info |
| 173 | if curve: |
| 174 | cmd += " curve=" + curve |
| 175 | if key: |
| 176 | cmd += " key=" + key |
| 177 | res = wpas.request(cmd) |
| 178 | if "FAIL" in res: |
| 179 | raise Exception("Failed to generate bootstrapping info") |
| 180 | return int(res) |
| 181 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 182 | def wpas_get_nfc_uri(start_listen=True, pick_channel=False): |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 183 | wpas = wpas_connect() |
| 184 | if wpas is None: |
| 185 | return None |
| 186 | global own_id, chanlist |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 187 | chan = chanlist |
| 188 | if chan is None and get_status_field(wpas, "bssid[0]"): |
| 189 | freq = get_status_field(wpas, "freq") |
| 190 | if freq: |
| 191 | freq = int(freq) |
| 192 | if freq >= 2412 and freq <= 2462: |
| 193 | chan = "81/%d" % ((freq - 2407) / 5) |
| 194 | summary("Use current AP operating channel (%d MHz) as the URI channel list (%s)" % (freq, chan)) |
| 195 | if chan is None and pick_channel: |
| 196 | chan = "81/6" |
| 197 | summary("Use channel 2437 MHz since no other preference provided") |
| 198 | own_id = dpp_bootstrap_gen(wpas, type="nfc-uri", chan=chan, mac=True) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 199 | res = wpas.request("DPP_BOOTSTRAP_GET_URI %d" % own_id).rstrip() |
| 200 | if "FAIL" in res: |
| 201 | return None |
| 202 | if start_listen: |
| 203 | wpas.request("DPP_LISTEN 2412 netrole=configurator") |
| 204 | return res |
| 205 | |
| 206 | def wpas_report_handover_req(uri): |
| 207 | wpas = wpas_connect() |
| 208 | if wpas is None: |
| 209 | return None |
| 210 | global own_id |
| 211 | cmd = "DPP_NFC_HANDOVER_REQ own=%d uri=%s" % (own_id, uri) |
| 212 | return wpas.request(cmd) |
| 213 | |
| 214 | def wpas_report_handover_sel(uri): |
| 215 | wpas = wpas_connect() |
| 216 | if wpas is None: |
| 217 | return None |
| 218 | global own_id |
| 219 | cmd = "DPP_NFC_HANDOVER_SEL own=%d uri=%s" % (own_id, uri) |
| 220 | return wpas.request(cmd) |
| 221 | |
| 222 | def dpp_handover_client(llc): |
| 223 | uri = wpas_get_nfc_uri(start_listen=False) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 224 | if uri is None: |
| 225 | summary("Cannot start handover client - no bootstrap URI available") |
| 226 | return |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 227 | uri = ndef.UriRecord(uri) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 228 | summary("NFC URI record for DPP: " + str(uri)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 229 | carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 230 | crn = os.urandom(2) |
| 231 | hr = ndef.HandoverRequestRecord(version="1.4", crn=crn) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 232 | hr.add_alternative_carrier('active', carrier.name) |
| 233 | message = [hr, carrier] |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 234 | summary("NFC Handover Request message for DPP: " + str(message)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 235 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 236 | global peer_crn |
| 237 | if peer_crn is not None: |
| 238 | summary("NFC handover request from peer was already received - do not send own") |
| 239 | return |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 240 | client = nfc.handover.HandoverClient(llc) |
| 241 | try: |
| 242 | summary("Trying to initiate NFC connection handover") |
| 243 | client.connect() |
| 244 | summary("Connected for handover") |
| 245 | except nfc.llcp.ConnectRefused: |
| 246 | summary("Handover connection refused") |
| 247 | client.close() |
| 248 | return |
| 249 | except Exception as e: |
| 250 | summary("Other exception: " + str(e)) |
| 251 | client.close() |
| 252 | return |
| 253 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 254 | if peer_crn is not None: |
| 255 | summary("NFC handover request from peer was already received - do not send own") |
| 256 | client.close() |
| 257 | return |
| 258 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 259 | summary("Sending handover request") |
| 260 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 261 | global my_crn, my_crn_ready, hs_sent |
| 262 | my_crn_ready = True |
| 263 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 264 | if not client.send_records(message): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 265 | my_crn_ready = False |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 266 | summary("Failed to send handover request") |
| 267 | client.close() |
| 268 | return |
| 269 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 270 | my_crn, = struct.unpack('>H', crn) |
| 271 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 272 | summary("Receiving handover response") |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 273 | try: |
| 274 | message = client.recv_records(timeout=3.0) |
| 275 | except Exception as e: |
| 276 | # This is fine if we are the handover selector |
| 277 | if hs_sent: |
| 278 | summary("Client receive failed as expected since I'm the handover server: %s" % str(e)) |
| 279 | else: |
| 280 | summary("Client receive failed: %s" % str(e)) |
| 281 | message = None |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 282 | if message is None: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 283 | if hs_sent: |
| 284 | summary("No response received as expected since I'm the handover server") |
| 285 | else: |
| 286 | summary("No response received") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 287 | client.close() |
| 288 | return |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 289 | summary("Received message: " + str(message)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 290 | if len(message) < 1 or \ |
| 291 | not isinstance(message[0], ndef.HandoverSelectRecord): |
| 292 | summary("Response was not Hs - received: " + message.type) |
| 293 | client.close() |
| 294 | return |
| 295 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 296 | summary("Received handover select message") |
| 297 | summary("alternative carriers: " + str(message[0].alternative_carriers)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 298 | |
| 299 | dpp_found = False |
| 300 | for carrier in message: |
| 301 | if isinstance(carrier, ndef.HandoverSelectRecord): |
| 302 | continue |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 303 | summary("Remote carrier type: " + carrier.type) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 304 | if carrier.type == "application/vnd.wfa.dpp": |
| 305 | if len(carrier.data) == 0 or carrier.data[0] != 0: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 306 | summary("URI Identifier Code 'None' not seen") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 307 | continue |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 308 | summary("DPP carrier type match - send to wpa_supplicant") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 309 | dpp_found = True |
| 310 | uri = carrier.data[1:].decode("utf-8") |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 311 | summary("DPP URI: " + uri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 312 | res = wpas_report_handover_sel(uri) |
| 313 | if res is None or "FAIL" in res: |
| 314 | summary("DPP handover report rejected") |
| 315 | break |
| 316 | |
| 317 | success_report("DPP handover reported successfully (initiator)") |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 318 | summary("peer_id=" + res) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 319 | peer_id = int(res) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 320 | wpas = wpas_connect() |
| 321 | if wpas is None: |
| 322 | break |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 323 | |
| 324 | global enrollee_only |
| 325 | global config_params |
| 326 | if enrollee_only: |
| 327 | extra = " role=enrollee" |
| 328 | elif config_params: |
| 329 | extra = " role=configurator " + config_params |
| 330 | else: |
| 331 | # TODO: Single Configurator instance |
| 332 | res = wpas.request("DPP_CONFIGURATOR_ADD") |
| 333 | if "FAIL" in res: |
| 334 | summary("Failed to initiate Configurator") |
| 335 | break |
| 336 | conf_id = int(res) |
| 337 | extra = " conf=sta-dpp configurator=%d" % conf_id |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 338 | global own_id |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 339 | summary("Initiate DPP authentication") |
| 340 | cmd = "DPP_AUTH_INIT peer=%d own=%d" % (peer_id, own_id) |
| 341 | cmd += extra |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 342 | res = wpas.request(cmd) |
| 343 | if "FAIL" in res: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 344 | summary("Failed to initiate DPP authentication") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 345 | break |
| 346 | |
| 347 | if not dpp_found: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 348 | summary("DPP carrier not seen in response - allow peer to initiate a new handover with different parameters") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 349 | client.close() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 350 | summary("Returning from dpp_handover_client") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 351 | return |
| 352 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 353 | summary("Remove peer") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 354 | client.close() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 355 | summary("Done with handover") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 356 | global only_one |
| 357 | if only_one: |
| 358 | print("only_one -> stop loop") |
| 359 | global continue_loop |
| 360 | continue_loop = False |
| 361 | |
| 362 | global no_wait |
| 363 | if no_wait: |
| 364 | print("Trying to exit..") |
| 365 | global terminate_now |
| 366 | terminate_now = True |
| 367 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 368 | summary("Returning from dpp_handover_client") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 369 | |
| 370 | class HandoverServer(nfc.handover.HandoverServer): |
| 371 | def __init__(self, llc): |
| 372 | super(HandoverServer, self).__init__(llc) |
| 373 | self.sent_carrier = None |
| 374 | self.ho_server_processing = False |
| 375 | self.success = False |
| 376 | self.try_own = False |
| 377 | |
| 378 | def process_handover_request_message(self, records): |
| 379 | self.ho_server_processing = True |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 380 | global in_raw_mode |
| 381 | was_in_raw_mode = in_raw_mode |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 382 | clear_raw_mode() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 383 | if was_in_raw_mode: |
| 384 | print("\n") |
| 385 | summary("HandoverServer - request received: " + str(records)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 386 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 387 | global my_crn, peer_crn, my_crn_ready |
| 388 | |
| 389 | for carrier in records: |
| 390 | if not isinstance(carrier, ndef.HandoverRequestRecord): |
| 391 | continue |
| 392 | if carrier.collision_resolution_number: |
| 393 | peer_crn = carrier.collision_resolution_number |
| 394 | summary("peer_crn: %d" % peer_crn) |
| 395 | |
| 396 | if my_crn is None and my_crn_ready: |
| 397 | summary("Still trying to send own handover request - wait a moment to see if that succeeds before checking crn values") |
| 398 | for i in range(10): |
| 399 | if my_crn is not None: |
| 400 | break |
| 401 | time.sleep(0.01) |
| 402 | if my_crn is not None: |
| 403 | summary("my_crn: %d" % my_crn) |
| 404 | |
| 405 | if my_crn is not None and peer_crn is not None: |
| 406 | if my_crn == peer_crn: |
| 407 | summary("Same crn used - automatic collision resolution failed") |
| 408 | # TODO: Should generate a new Handover Request message |
| 409 | return '' |
| 410 | if ((my_crn & 1) == (peer_crn & 1) and my_crn > peer_crn) or \ |
| 411 | ((my_crn & 1) != (peer_crn & 1) and my_crn < peer_crn): |
| 412 | summary("I'm the Handover Selector Device") |
| 413 | pass |
| 414 | else: |
| 415 | summary("Peer is the Handover Selector device") |
| 416 | summary("Ignore the received request.") |
| 417 | return '' |
| 418 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 419 | hs = ndef.HandoverSelectRecord('1.4') |
| 420 | sel = [hs] |
| 421 | |
| 422 | found = False |
| 423 | |
| 424 | for carrier in records: |
| 425 | if isinstance(carrier, ndef.HandoverRequestRecord): |
| 426 | continue |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 427 | summary("Remote carrier type: " + carrier.type) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 428 | if carrier.type == "application/vnd.wfa.dpp": |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 429 | summary("DPP carrier type match - add DPP carrier record") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 430 | if len(carrier.data) == 0 or carrier.data[0] != 0: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 431 | summary("URI Identifier Code 'None' not seen") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 432 | continue |
| 433 | uri = carrier.data[1:].decode("utf-8") |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 434 | summary("Received DPP URI: " + uri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 435 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 436 | data = wpas_get_nfc_uri(start_listen=False, pick_channel=True) |
| 437 | summary("Own URI (pre-processing): %s" % data) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 438 | |
| 439 | res = wpas_report_handover_req(uri) |
| 440 | if res is None or "FAIL" in res: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 441 | summary("DPP handover request processing failed") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 442 | continue |
| 443 | |
| 444 | found = True |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 445 | |
| 446 | wpas = wpas_connect() |
| 447 | if wpas is None: |
| 448 | continue |
| 449 | global own_id |
| 450 | data = wpas.request("DPP_BOOTSTRAP_GET_URI %d" % own_id).rstrip() |
| 451 | if "FAIL" in data: |
| 452 | continue |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 453 | summary("Own URI (post-processing): %s" % data) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 454 | uri = ndef.UriRecord(data) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 455 | summary("Own bootstrapping NFC URI record: " + str(uri)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 456 | |
| 457 | info = wpas.request("DPP_BOOTSTRAP_INFO %d" % own_id) |
| 458 | freq = None |
| 459 | for line in info.splitlines(): |
| 460 | if line.startswith("use_freq="): |
| 461 | freq = int(line.split('=')[1]) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 462 | if freq is None or freq == 0: |
| 463 | summary("No channel negotiated over NFC - use channel 6") |
| 464 | freq = 2437 |
| 465 | else: |
| 466 | summary("Negotiated channel: %d MHz" % freq) |
| 467 | if get_status_field(wpas, "bssid[0]"): |
| 468 | summary("Own AP freq: %s MHz" % str(get_status_field(wpas, "freq"))) |
| 469 | if get_status_field(wpas, "beacon_set", extra="DRIVER") is None: |
| 470 | summary("Enable beaconing to have radio ready for RX") |
| 471 | wpas.request("DISABLE") |
| 472 | wpas.request("SET start_disabled 0") |
| 473 | wpas.request("ENABLE") |
| 474 | cmd = "DPP_LISTEN %d" % freq |
| 475 | global enrollee_only |
| 476 | global configurator_only |
| 477 | if enrollee_only: |
| 478 | cmd += " role=enrollee" |
| 479 | elif configurator_only: |
| 480 | cmd += " role=configurator" |
| 481 | summary(cmd) |
| 482 | res = wpas.request(cmd) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 483 | if "OK" not in res: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 484 | summary("Failed to start DPP listen") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 485 | break |
| 486 | |
| 487 | carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 488 | summary("Own DPP carrier record: " + str(carrier)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 489 | hs.add_alternative_carrier('active', carrier.name) |
| 490 | sel = [hs, carrier] |
| 491 | break |
| 492 | |
| 493 | summary("Sending handover select: " + str(sel)) |
| 494 | if found: |
| 495 | self.success = True |
| 496 | else: |
| 497 | self.try_own = True |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 498 | global hs_sent |
| 499 | hs_sent = True |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 500 | return sel |
| 501 | |
| 502 | def clear_raw_mode(): |
| 503 | import sys, tty, termios |
| 504 | global prev_tcgetattr, in_raw_mode |
| 505 | if not in_raw_mode: |
| 506 | return |
| 507 | fd = sys.stdin.fileno() |
| 508 | termios.tcsetattr(fd, termios.TCSADRAIN, prev_tcgetattr) |
| 509 | in_raw_mode = False |
| 510 | |
| 511 | def getch(): |
| 512 | import sys, tty, termios, select |
| 513 | global prev_tcgetattr, in_raw_mode |
| 514 | fd = sys.stdin.fileno() |
| 515 | prev_tcgetattr = termios.tcgetattr(fd) |
| 516 | ch = None |
| 517 | try: |
| 518 | tty.setraw(fd) |
| 519 | in_raw_mode = True |
| 520 | [i, o, e] = select.select([fd], [], [], 0.05) |
| 521 | if i: |
| 522 | ch = sys.stdin.read(1) |
| 523 | finally: |
| 524 | termios.tcsetattr(fd, termios.TCSADRAIN, prev_tcgetattr) |
| 525 | in_raw_mode = False |
| 526 | return ch |
| 527 | |
| 528 | def dpp_tag_read(tag): |
| 529 | success = False |
| 530 | for record in tag.ndef.records: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 531 | summary(record) |
| 532 | summary("record type " + record.type) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 533 | if record.type == "application/vnd.wfa.dpp": |
| 534 | summary("DPP HS tag - send to wpa_supplicant") |
| 535 | success = dpp_hs_tag_read(record) |
| 536 | break |
| 537 | if isinstance(record, ndef.UriRecord): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 538 | summary("URI record: uri=" + record.uri) |
| 539 | summary("URI record: iri=" + record.iri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 540 | if record.iri.startswith("DPP:"): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 541 | summary("DPP URI") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 542 | if not dpp_nfc_uri_process(record.iri): |
| 543 | break |
| 544 | success = True |
| 545 | else: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 546 | summary("Ignore unknown URI") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 547 | break |
| 548 | |
| 549 | if success: |
| 550 | success_report("Tag read succeeded") |
| 551 | |
| 552 | return success |
| 553 | |
| 554 | def rdwr_connected_write_tag(tag): |
| 555 | summary("Tag found - writing - " + str(tag)) |
| 556 | if not tag.ndef.is_writeable: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 557 | summary("Not a writable tag") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 558 | return |
| 559 | global dpp_tag_data |
| 560 | if tag.ndef.capacity < len(dpp_tag_data): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 561 | summary("Not enough room for the message") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 562 | return |
| 563 | tag.ndef.records = dpp_tag_data |
| 564 | success_report("Tag write succeeded") |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 565 | summary("Done - remove tag") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 566 | global only_one |
| 567 | if only_one: |
| 568 | global continue_loop |
| 569 | continue_loop = False |
| 570 | global dpp_sel_wait_remove |
| 571 | return dpp_sel_wait_remove |
| 572 | |
| 573 | def write_nfc_uri(clf, wait_remove=True): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 574 | summary("Write NFC URI record") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 575 | data = wpas_get_nfc_uri() |
| 576 | if data is None: |
| 577 | summary("Could not get NFC URI from wpa_supplicant") |
| 578 | return |
| 579 | |
| 580 | global dpp_sel_wait_remove |
| 581 | dpp_sel_wait_remove = wait_remove |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 582 | summary("URI: %s" % data) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 583 | uri = ndef.UriRecord(data) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 584 | summary(uri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 585 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 586 | summary("Touch an NFC tag") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 587 | global dpp_tag_data |
| 588 | dpp_tag_data = [uri] |
| 589 | clf.connect(rdwr={'on-connect': rdwr_connected_write_tag}) |
| 590 | |
| 591 | def write_nfc_hs(clf, wait_remove=True): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 592 | summary("Write NFC Handover Select record on a tag") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 593 | data = wpas_get_nfc_uri() |
| 594 | if data is None: |
| 595 | summary("Could not get NFC URI from wpa_supplicant") |
| 596 | return |
| 597 | |
| 598 | global dpp_sel_wait_remove |
| 599 | dpp_sel_wait_remove = wait_remove |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 600 | summary("URI: %s" % data) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 601 | uri = ndef.UriRecord(data) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 602 | summary(uri) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 603 | carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data) |
| 604 | hs = ndef.HandoverSelectRecord('1.4') |
| 605 | hs.add_alternative_carrier('active', carrier.name) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 606 | summary(hs) |
| 607 | summary(carrier) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 608 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 609 | summary("Touch an NFC tag") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 610 | global dpp_tag_data |
| 611 | dpp_tag_data = [hs, carrier] |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 612 | summary(dpp_tag_data) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 613 | clf.connect(rdwr={'on-connect': rdwr_connected_write_tag}) |
| 614 | |
| 615 | def rdwr_connected(tag): |
| 616 | global only_one, no_wait |
| 617 | summary("Tag connected: " + str(tag)) |
| 618 | |
| 619 | if tag.ndef: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 620 | summary("NDEF tag: " + tag.type) |
| 621 | summary(tag.ndef.records) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 622 | success = dpp_tag_read(tag) |
| 623 | if only_one and success: |
| 624 | global continue_loop |
| 625 | continue_loop = False |
| 626 | else: |
| 627 | summary("Not an NDEF tag - remove tag") |
| 628 | return True |
| 629 | |
| 630 | return not no_wait |
| 631 | |
| 632 | def llcp_worker(llc): |
| 633 | global init_on_touch |
| 634 | if init_on_touch: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 635 | summary("Starting handover client") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 636 | dpp_handover_client(llc) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 637 | summary("Exiting llcp_worker thread (init_in_touch)") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 638 | return |
| 639 | |
| 640 | global no_input |
| 641 | if no_input: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 642 | summary("Wait for handover to complete") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 643 | else: |
| 644 | print("Wait for handover to complete - press 'i' to initiate") |
| 645 | global srv |
| 646 | global wait_connection |
| 647 | while not wait_connection and srv.sent_carrier is None: |
| 648 | if srv.try_own: |
| 649 | srv.try_own = False |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 650 | summary("Try to initiate another handover with own parameters") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 651 | dpp_handover_client(llc) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 652 | summary("Exiting llcp_worker thread (retry with own parameters)") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 653 | return |
| 654 | if srv.ho_server_processing: |
| 655 | time.sleep(0.025) |
| 656 | elif no_input: |
| 657 | time.sleep(0.5) |
| 658 | else: |
| 659 | res = getch() |
| 660 | if res != 'i': |
| 661 | continue |
| 662 | clear_raw_mode() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 663 | summary("Starting handover client") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 664 | dpp_handover_client(llc) |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 665 | summary("Exiting llcp_worker thread (manual init)") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 666 | return |
| 667 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 668 | global in_raw_mode |
| 669 | was_in_raw_mode = in_raw_mode |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 670 | clear_raw_mode() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 671 | if was_in_raw_mode: |
| 672 | print("\r") |
| 673 | summary("Exiting llcp_worker thread") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 674 | |
| 675 | def llcp_startup(llc): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 676 | summary("Start LLCP server") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 677 | global srv |
| 678 | srv = HandoverServer(llc) |
| 679 | return llc |
| 680 | |
| 681 | def llcp_connected(llc): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 682 | summary("P2P LLCP connected") |
| 683 | global wait_connection, my_crn, peer_crn, my_crn_ready, hs_sent |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 684 | wait_connection = False |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 685 | my_crn_ready = False |
| 686 | my_crn = None |
| 687 | peer_crn = None |
| 688 | hs_sent = False |
| 689 | global srv |
| 690 | srv.start() |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 691 | if init_on_touch or not no_input: |
| 692 | threading.Thread(target=llcp_worker, args=(llc,)).start() |
| 693 | return True |
| 694 | |
| 695 | def llcp_release(llc): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 696 | summary("LLCP release") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 697 | return True |
| 698 | |
| 699 | def terminate_loop(): |
| 700 | global terminate_now |
| 701 | return terminate_now |
| 702 | |
| 703 | def main(): |
| 704 | clf = nfc.ContactlessFrontend() |
| 705 | |
| 706 | parser = argparse.ArgumentParser(description='nfcpy to wpa_supplicant integration for DPP NFC operations') |
| 707 | parser.add_argument('-d', const=logging.DEBUG, default=logging.INFO, |
| 708 | action='store_const', dest='loglevel', |
| 709 | help='verbose debug output') |
| 710 | parser.add_argument('-q', const=logging.WARNING, action='store_const', |
| 711 | dest='loglevel', help='be quiet') |
| 712 | parser.add_argument('--only-one', '-1', action='store_true', |
| 713 | help='run only one operation and exit') |
| 714 | parser.add_argument('--init-on-touch', '-I', action='store_true', |
| 715 | help='initiate handover on touch') |
| 716 | parser.add_argument('--no-wait', action='store_true', |
| 717 | help='do not wait for tag to be removed before exiting') |
| 718 | parser.add_argument('--ifname', '-i', |
| 719 | help='network interface name') |
| 720 | parser.add_argument('--no-input', '-a', action='store_true', |
| 721 | help='do not use stdout input to initiate handover') |
| 722 | parser.add_argument('--tag-read-only', '-t', action='store_true', |
| 723 | help='tag read only (do not allow connection handover)') |
| 724 | parser.add_argument('--handover-only', action='store_true', |
| 725 | help='connection handover only (do not allow tag read)') |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 726 | parser.add_argument('--enrollee', action='store_true', |
| 727 | help='run as Enrollee-only') |
| 728 | parser.add_argument('--configurator', action='store_true', |
| 729 | help='run as Configurator-only') |
| 730 | parser.add_argument('--config-params', default='', |
| 731 | help='configurator parameters') |
| 732 | parser.add_argument('--ctrl', default='/var/run/wpa_supplicant', |
| 733 | help='wpa_supplicant/hostapd control interface') |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 734 | parser.add_argument('--summary', |
| 735 | help='summary file for writing status updates') |
| 736 | parser.add_argument('--success', |
| 737 | help='success file for writing success update') |
| 738 | parser.add_argument('--device', default='usb', help='NFC device to open') |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 739 | parser.add_argument('--chan', default=None, help='channel list') |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 740 | parser.add_argument('command', choices=['write-nfc-uri', |
| 741 | 'write-nfc-hs'], |
| 742 | nargs='?') |
| 743 | args = parser.parse_args() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 744 | summary(args) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 745 | |
| 746 | global only_one |
| 747 | only_one = args.only_one |
| 748 | |
| 749 | global no_wait |
| 750 | no_wait = args.no_wait |
| 751 | |
| 752 | global chanlist |
| 753 | chanlist = args.chan |
| 754 | |
| 755 | logging.basicConfig(level=args.loglevel) |
| 756 | |
| 757 | global init_on_touch |
| 758 | init_on_touch = args.init_on_touch |
| 759 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 760 | global enrollee_only |
| 761 | enrollee_only = args.enrollee |
| 762 | |
| 763 | global configurator_only |
| 764 | configurator_only = args.configurator |
| 765 | |
| 766 | global config_params |
| 767 | config_params = args.config_params |
| 768 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 769 | if args.ifname: |
| 770 | global ifname |
| 771 | ifname = args.ifname |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 772 | summary("Selected ifname " + ifname) |
| 773 | |
| 774 | if args.ctrl: |
| 775 | global wpas_ctrl |
| 776 | wpas_ctrl = args.ctrl |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 777 | |
| 778 | if args.summary: |
| 779 | global summary_file |
| 780 | summary_file = args.summary |
| 781 | |
| 782 | if args.success: |
| 783 | global success_file |
| 784 | success_file = args.success |
| 785 | |
| 786 | if args.no_input: |
| 787 | global no_input |
| 788 | no_input = True |
| 789 | |
| 790 | clf = nfc.ContactlessFrontend() |
| 791 | global wait_connection |
| 792 | |
| 793 | try: |
| 794 | if not clf.open(args.device): |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 795 | summary("Could not open connection with an NFC device") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 796 | raise SystemExit |
| 797 | |
| 798 | if args.command == "write-nfc-uri": |
| 799 | write_nfc_uri(clf, wait_remove=not args.no_wait) |
| 800 | raise SystemExit |
| 801 | |
| 802 | if args.command == "write-nfc-hs": |
| 803 | write_nfc_hs(clf, wait_remove=not args.no_wait) |
| 804 | raise SystemExit |
| 805 | |
| 806 | global continue_loop |
| 807 | while continue_loop: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 808 | global in_raw_mode |
| 809 | was_in_raw_mode = in_raw_mode |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 810 | clear_raw_mode() |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 811 | if was_in_raw_mode: |
| 812 | print("\r") |
| 813 | summary("Waiting for a tag or peer to be touched") |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 814 | wait_connection = True |
| 815 | try: |
| 816 | if args.tag_read_only: |
| 817 | if not clf.connect(rdwr={'on-connect': rdwr_connected}): |
| 818 | break |
| 819 | elif args.handover_only: |
| 820 | if not clf.connect(llcp={'on-startup': llcp_startup, |
| 821 | 'on-connect': llcp_connected, |
| 822 | 'on-release': llcp_release}, |
| 823 | terminate=terminate_loop): |
| 824 | break |
| 825 | else: |
| 826 | if not clf.connect(rdwr={'on-connect': rdwr_connected}, |
| 827 | llcp={'on-startup': llcp_startup, |
| 828 | 'on-connect': llcp_connected, |
| 829 | 'on-release': llcp_release}, |
| 830 | terminate=terminate_loop): |
| 831 | break |
| 832 | except Exception as e: |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame^] | 833 | summary("clf.connect failed: " + str(e)) |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 834 | break |
| 835 | |
| 836 | global srv |
| 837 | if only_one and srv and srv.success: |
| 838 | raise SystemExit |
| 839 | |
| 840 | except KeyboardInterrupt: |
| 841 | raise SystemExit |
| 842 | finally: |
| 843 | clf.close() |
| 844 | |
| 845 | raise SystemExit |
| 846 | |
| 847 | if __name__ == '__main__': |
| 848 | main() |