3 # Example nfcpy to hostapd 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.
23 wpas_ctrl = '/var/run/hostapd'
27 if os.path.isdir(wpas_ctrl):
29 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
30 except OSError, error:
31 print "Could not find hostapd: ", error
35 print "No hostapd control interface found"
40 wpas = wpactrl.WPACtrl(ctrl)
42 except wpactrl.error, error:
43 print "Error: ", error
48 def wpas_tag_read(message):
52 print wpas.request("WPS_NFC_TAG_READ " + message.encode("hex"))
55 def wpas_get_config_token():
59 return wpas.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip().decode("hex")
62 def wpas_get_password_token():
66 return wpas.request("WPS_NFC_TOKEN NDEF").rstrip().decode("hex")
69 def wpas_get_handover_req():
73 return wpas.request("NFC_GET_HANDOVER_REQ NDEF WPS").rstrip().decode("hex")
76 def wpas_put_handover_sel(message):
80 print wpas.request("NFC_RX_HANDOVER_SEL " + str(message).encode("hex"))
83 def wps_handover_init(peer):
84 print "Trying to initiate WPS handover"
86 data = wpas_get_handover_req()
88 print "Could not get handover request message from hostapd"
90 print "Handover request from hostapd: " + data.encode("hex")
91 message = nfc.ndef.Message(data)
92 print "Parsed handover request: " + message.pretty()
94 nfc.llcp.activate(peer);
97 client = nfc.handover.HandoverClient()
99 print "Trying handover";
101 print "Connected for handover"
102 except nfc.llcp.ConnectRefused:
103 print "Handover connection refused"
108 print "Sending handover request"
110 if not client.send(message):
111 print "Failed to send handover request"
113 print "Receiving handover response"
114 message = client._recv()
115 print "Handover select received"
116 print message.pretty()
117 wpas_put_handover_sel(message)
122 print "Done with handover"
125 def wps_tag_read(tag):
126 if len(tag.ndef.message):
127 message = nfc.ndef.Message(tag.ndef.message)
128 print "message type " + message.type
130 for record in message:
131 print "record type " + record.type
132 if record.type == "application/vnd.wfa.wsc":
133 print "WPS tag - send to hostapd"
134 wpas_tag_read(tag.ndef.message)
140 while tag.is_present:
144 def wps_write_config_tag(clf):
145 print "Write WPS config token"
146 data = wpas_get_config_token()
148 print "Could not get WPS config token from hostapd"
151 print "Touch an NFC tag"
159 print "Tag found - writing"
160 tag.ndef.message = data
161 print "Done - remove tag"
162 while tag.is_present:
166 def wps_write_password_tag(clf):
167 print "Write WPS password token"
168 data = wpas_get_password_token()
170 print "Could not get WPS password token from hostapd"
173 print "Touch an NFC tag"
181 print "Tag found - writing"
182 tag.ndef.message = data
183 print "Done - remove tag"
184 while tag.is_present:
189 clf = nfc.ContactlessFrontend()
192 if len(sys.argv) > 1 and sys.argv[1] == "write-config":
193 wps_write_config_tag(clf)
196 if len(sys.argv) > 1 and sys.argv[1] == "write-password":
197 wps_write_password_tag(clf)
201 print "Waiting for a tag or peer to be touched"
204 general_bytes = nfc.llcp.startup({})
205 tag = clf.poll(general_bytes)
209 if isinstance(tag, nfc.DEP):
210 wps_handover_init(tag)
218 print "Not an NDEF tag - remove tag"
219 while tag.is_present:
223 except KeyboardInterrupt:
230 if __name__ == '__main__':