3 # Example nfcpy to wpa_supplicant wrapper for WPS NFC operations
4 # Copyright (c) 2012-2013, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
25 wpas_ctrl = '/var/run/wpa_supplicant'
29 if os.path.isdir(wpas_ctrl):
31 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
32 except OSError, error:
33 print "Could not find wpa_supplicant: ", error
37 print "No wpa_supplicant control interface found"
42 wpas = wpactrl.WPACtrl(ctrl)
44 except wpactrl.error, error:
45 print "Error: ", error
50 def wpas_tag_read(message):
54 print wpas.request("WPS_NFC_TAG_READ " + message.encode("hex"))
57 def wpas_get_handover_req():
61 return wpas.request("NFC_GET_HANDOVER_REQ NDEF WPS-CR").rstrip().decode("hex")
64 def wpas_put_handover_sel(message):
68 print wpas.request("NFC_RX_HANDOVER_SEL " + str(message).encode("hex"))
71 def wps_handover_init(peer):
72 print "Trying to initiate WPS handover"
74 data = wpas_get_handover_req()
76 print "Could not get handover request carrier record from wpa_supplicant"
78 print "Handover request carrier record from wpa_supplicant: " + data.encode("hex")
79 record = nfc.ndef.Record()
80 f = StringIO.StringIO(data)
82 record = nfc.ndef.HandoverCarrierRecord(record)
83 print "Parsed handover request carrier record:"
86 message = nfc.ndef.HandoverRequestMessage(version="1.2")
87 message.nonce = random.randint(0, 0xffff)
88 message.add_carrier(record, "active")
90 print "Handover request:"
91 print message.pretty()
93 nfc.llcp.activate(peer);
95 client = nfc.handover.HandoverClient()
97 print "Trying handover";
99 print "Connected for handover"
100 except nfc.llcp.ConnectRefused:
101 print "Handover connection refused"
106 print "Sending handover request"
108 if not client.send(message):
109 print "Failed to send handover request"
111 print "Receiving handover response"
112 message = client._recv()
114 print "No response received"
119 print "Handover select received"
120 print message.pretty()
121 wpas_put_handover_sel(message)
126 print "Done with handover"
129 def wps_tag_read(tag):
130 if len(tag.ndef.message):
131 message = nfc.ndef.Message(tag.ndef.message)
132 print "message type " + message.type
134 for record in message:
135 print "record type " + record.type
136 if record.type == "application/vnd.wfa.wsc":
137 print "WPS tag - send to wpa_supplicant"
138 wpas_tag_read(tag.ndef.message)
144 while tag.is_present:
150 if nfc.llcp.connected():
151 print "LLCP connected"
152 general_bytes = nfc.llcp.startup({})
153 peer = clf.listen(ord(os.urandom(1)) + 200, general_bytes)
154 if isinstance(peer, nfc.DEP):
155 print "listen -> DEP";
156 if peer.general_bytes.startswith("Ffm"):
159 print "mismatch in general_bytes"
160 print peer.general_bytes
162 peer = clf.poll(general_bytes)
163 if isinstance(peer, nfc.DEP):
165 if peer.general_bytes.startswith("Ffm"):
168 print "mismatch in general_bytes"
169 print peer.general_bytes
177 clf = nfc.ContactlessFrontend()
181 print "Waiting for a tag or peer to be touched"
184 if isinstance(tag, nfc.DEP):
185 wps_handover_init(tag)
192 print "Not an NDEF tag - remove tag"
193 while tag.is_present:
196 except KeyboardInterrupt:
203 if __name__ == '__main__':