WPS: Use alternating poll/listen for NFC peer discovery with nfcpy

This is needed to find the NFC peer to avoid cases where both devices
could be using the same operation.

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-02-10 16:25:20 +02:00
parent 8140ae969b
commit dc6bda1123
2 changed files with 76 additions and 34 deletions

View file

@ -185,6 +185,34 @@ def wps_write_password_tag(clf):
time.sleep(0.1)
def find_peer(clf):
while True:
if nfc.llcp.connected():
print "LLCP connected"
general_bytes = nfc.llcp.startup({})
peer = clf.listen(ord(os.urandom(1)) + 200, general_bytes)
if isinstance(peer, nfc.DEP):
print "listen -> DEP";
if peer.general_bytes.startswith("Ffm"):
print "Found DEP"
return peer
print "mismatch in general_bytes"
print peer.general_bytes
peer = clf.poll(general_bytes)
if isinstance(peer, nfc.DEP):
print "poll -> DEP";
if peer.general_bytes.startswith("Ffm"):
print "Found DEP"
return peer
print "mismatch in general_bytes"
print peer.general_bytes
if peer:
print "Found tag"
return peer
def main():
clf = nfc.ContactlessFrontend()
@ -200,25 +228,18 @@ def main():
while True:
print "Waiting for a tag or peer to be touched"
while True:
general_bytes = nfc.llcp.startup({})
tag = clf.poll(general_bytes)
if tag == None:
continue
tag = find_peer(clf)
if isinstance(tag, nfc.DEP):
wps_handover_init(tag)
break
continue
if tag.ndef:
wps_tag_read(tag)
break
continue
if tag:
print "Not an NDEF tag - remove tag"
while tag.is_present:
time.sleep(0.1)
break
except KeyboardInterrupt:
raise SystemExit

View file

@ -127,6 +127,34 @@ def wps_tag_read(tag):
time.sleep(0.1)
def find_peer(clf):
while True:
if nfc.llcp.connected():
print "LLCP connected"
general_bytes = nfc.llcp.startup({})
peer = clf.listen(ord(os.urandom(1)) + 200, general_bytes)
if isinstance(peer, nfc.DEP):
print "listen -> DEP";
if peer.general_bytes.startswith("Ffm"):
print "Found DEP"
return peer
print "mismatch in general_bytes"
print peer.general_bytes
peer = clf.poll(general_bytes)
if isinstance(peer, nfc.DEP):
print "poll -> DEP";
if peer.general_bytes.startswith("Ffm"):
print "Found DEP"
return peer
print "mismatch in general_bytes"
print peer.general_bytes
if peer:
print "Found tag"
return peer
def main():
clf = nfc.ContactlessFrontend()
@ -134,25 +162,18 @@ def main():
while True:
print "Waiting for a tag or peer to be touched"
while True:
general_bytes = nfc.llcp.startup({})
tag = clf.poll(general_bytes)
if tag == None:
continue
tag = find_peer(clf)
if isinstance(tag, nfc.DEP):
wps_handover_init(tag)
break
continue
if tag.ndef:
wps_tag_read(tag)
break
continue
if tag:
print "Not an NDEF tag - remove tag"
while tag.is_present:
time.sleep(0.1)
break
except KeyboardInterrupt:
raise SystemExit