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'
28 if os.path.isdir(wpas_ctrl):
30 ifaces = [os.path.join(wpas_ctrl, i) for i in os.listdir(wpas_ctrl)]
31 except OSError, error:
32 print "Could not find hostapd: ", error
36 print "No hostapd control interface found"
41 wpas = wpaspy.Ctrl(ctrl)
48 def wpas_tag_read(message):
52 if "FAIL" in wpas.request("WPS_NFC_TAG_READ " + str(message).encode("hex")):
57 def wpas_get_config_token():
61 return wpas.request("WPS_NFC_CONFIG_TOKEN NDEF").rstrip().decode("hex")
64 def wpas_get_password_token():
68 return wpas.request("WPS_NFC_TOKEN NDEF").rstrip().decode("hex")
71 def wpas_get_handover_sel():
75 return wpas.request("NFC_GET_HANDOVER_SEL NDEF WPS-CR").rstrip().decode("hex")
78 def wpas_report_handover(req, sel):
82 return wpas.request("NFC_REPORT_HANDOVER RESP WPS " +
83 str(req).encode("hex") + " " +
84 str(sel).encode("hex"))
87 class HandoverServer(nfc.handover.HandoverServer):
88 def __init__(self, llc):
89 super(HandoverServer, self).__init__(llc)
90 self.ho_server_processing = False
93 def process_request(self, request):
94 print "HandoverServer - request received"
95 print "Parsed handover request: " + request.pretty()
97 sel = nfc.ndef.HandoverSelectMessage(version="1.2")
99 for carrier in request.carriers:
100 print "Remote carrier type: " + carrier.type
101 if carrier.type == "application/vnd.wfa.wsc":
102 print "WPS carrier type match - add WPS carrier record"
103 data = wpas_get_handover_sel()
105 print "Could not get handover select carrier record from hostapd"
107 print "Handover select carrier record from hostapd:"
108 print data.encode("hex")
109 wpas_report_handover(carrier.record, data)
111 message = nfc.ndef.Message(data);
112 sel.add_carrier(message[0], "active", message[1:])
114 print "Handover select:"
116 print str(sel).encode("hex")
118 print "Sending handover select"
123 def wps_tag_read(tag):
125 if len(tag.ndef.message):
126 for record in tag.ndef.message:
127 print "record type " + record.type
128 if record.type == "application/vnd.wfa.wsc":
129 print "WPS tag - send to hostapd"
130 success = wpas_tag_read(tag.ndef.message)
138 def rdwr_connected_write(tag):
139 print "Tag found - writing"
141 tag.ndef.message = str(write_data)
142 print "Done - remove tag"
146 continue_loop = False
147 global write_wait_remove
148 while write_wait_remove and tag.is_present:
151 def wps_write_config_tag(clf, wait_remove=True):
152 print "Write WPS config token"
153 global write_data, write_wait_remove
154 write_wait_remove = wait_remove
155 write_data = wpas_get_config_token()
156 if write_data == None:
157 print "Could not get WPS config token from hostapd"
160 print "Touch an NFC tag"
161 clf.connect(rdwr={'on-connect': rdwr_connected_write})
164 def wps_write_password_tag(clf, wait_remove=True):
165 print "Write WPS password token"
166 global write_data, write_wait_remove
167 write_wait_remove = wait_remove
168 write_data = wpas_get_password_token()
169 if write_data == None:
170 print "Could not get WPS password token from hostapd"
173 print "Touch an NFC tag"
174 clf.connect(rdwr={'on-connect': rdwr_connected_write})
177 def rdwr_connected(tag):
178 global only_one, no_wait
179 print "Tag connected: " + str(tag)
182 print "NDEF tag: " + tag.type
184 print tag.ndef.message.pretty()
187 success = wps_tag_read(tag)
188 if only_one and success:
190 continue_loop = False
192 print "Not an NDEF tag - remove tag"
197 def llcp_startup(clf, llc):
198 print "Start LLCP server"
200 srv = HandoverServer(llc)
203 def llcp_connected(llc):
204 print "P2P LLCP connected"
205 global wait_connection
206 wait_connection = False
213 clf = nfc.ContactlessFrontend()
215 parser = argparse.ArgumentParser(description='nfcpy to hostapd integration for WPS NFC operations')
216 parser.add_argument('-d', const=logging.DEBUG, default=logging.INFO,
217 action='store_const', dest='loglevel',
218 help='verbose debug output')
219 parser.add_argument('-q', const=logging.WARNING, action='store_const',
220 dest='loglevel', help='be quiet')
221 parser.add_argument('--only-one', '-1', action='store_true',
222 help='run only one operation and exit')
223 parser.add_argument('--no-wait', action='store_true',
224 help='do not wait for tag to be removed before exiting')
225 parser.add_argument('command', choices=['write-config',
228 args = parser.parse_args()
231 only_one = args.only_one
234 no_wait = args.no_wait
236 logging.basicConfig(level=args.loglevel)
239 if not clf.open("usb"):
240 print "Could not open connection with an NFC device"
243 if args.command == "write-config":
244 wps_write_config_tag(clf, wait_remove=not args.no_wait)
247 if args.command == "write-password":
248 wps_write_password_tag(clf, wait_remove=not args.no_wait)
253 print "Waiting for a tag or peer to be touched"
254 wait_connection = True
256 if not clf.connect(rdwr={'on-connect': rdwr_connected},
257 llcp={'on-startup': llcp_startup,
258 'on-connect': llcp_connected}):
261 print "clf.connect failed"
264 if only_one and srv and srv.success:
267 except KeyboardInterrupt:
274 if __name__ == '__main__':