3 # Example Android logcat to wpa_supplicant wrapper for QR Code scans
4 # Copyright (c) 2017, Qualcomm Atheros, Inc.
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
15 scriptsdir = os.path.dirname(os.path.realpath(sys.modules[__name__].__file__))
16 sys.path.append(os.path.join(scriptsdir, '..', '..', 'wpaspy'))
20 wpas_ctrl = '/var/run/wpa_supplicant'
24 if os.path.isdir(wpas_ctrl):
26 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
27 except OSError as error:
28 print("Could not find wpa_supplicant: ", error)
32 print("No wpa_supplicant control interface found")
37 wpas = wpaspy.Ctrl(ctrl)
39 except Exception as e:
44 for line in iter(sys.stdin.readline, ''):
45 if "ResultHandler: Launching intent: Intent" not in line:
47 if "act=android.intent.action.VIEW" not in line:
50 for val in line.split(' '):
51 if val.startswith('dat='):
52 uri = val.split('=', 1)[1]
56 if not uri.startswith('DPP:'):
58 print("Found DPP bootstrap info URI:")
62 print("Could not connect to wpa_supplicant")
65 res = wpas.request("DPP_QR_CODE " + uri);
69 print("QR Code URI rejected")
71 print("QR Code URI accepted - ID=%d" % id)
72 print(wpas.request("DPP_BOOTSTRAP_INFO %d" % id))
75 def dpp_display(curve):
78 print("Could not connect to wpa_supplicant")
80 res = wpas.request("STATUS")
82 for line in res.splitlines():
83 if line.startswith("address="):
84 addr = line.split('=')[1]
86 cmd = "DPP_BOOTSTRAP_GEN type=qrcode"
89 cmd += " mac=" + addr.replace(':','')
91 cmd += " curve=" + curve
92 res = wpas.request(cmd)
96 print("Failed to generate bootstrap info URI")
98 print("Bootstrap information - ID=%d" % id)
99 print(wpas.request("DPP_BOOTSTRAP_INFO %d" % id))
100 uri = wpas.request("DPP_BOOTSTRAP_GET_URI %d" % id)
103 qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_M,
105 qr.add_data(uri, optimize=5)
106 qr.print_ascii(tty=True)
111 parser = argparse.ArgumentParser(description='Android logcat to wpa_supplicant integration for DPP QR Code operations')
112 parser.add_argument('-d', const=logging.DEBUG, default=logging.INFO,
113 action='store_const', dest='loglevel',
114 help='verbose debug output')
115 parser.add_argument('--curve', '-c',
116 help='set a specific curve (P-256, P-384, P-521, BP-256R1, BP-384R1, BP-512R1) for key generation')
117 parser.add_argument('command', choices=['logcat',
120 args = parser.parse_args()
122 logging.basicConfig(level=args.loglevel)
124 if args.command == "logcat":
126 elif args.command == "display":
127 dpp_display(args.curve)
129 if __name__ == '__main__':