llcp on-connect seem to fail on Win7

Asked by imueller on 2016-03-14

Hi,
I wonder if there is a bug in nfcpy on Windows7. I tried to run the below simple program to test a PN533 USB adapter with an Android phone. Everything works on a Raspberry Pi but when run on Windows, on-start is executed but on-connected seems to silently fail, i.a. the function terminates after about 1 sec without the smartphone having been tapped. If I try to rerun the entire program, the following error occurs which can only be avoided by unplugging and plugging the PN533 adapter back in:
clf = nfc.ContactlessFrontend('usb')
  File "C:\dev\python27\lib\site-packages\nfc\clf\__init__.py", line 73, in __init__
    raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
IOError: [Errno 19] No such device

Note, reading mifare cards works repetitively: clf.connect(rdwr={'on-connect': connected})

System configuration: Win7, nfcpy-0.10.2, pyusb-1.0.0a2, python27, PN533 USB adapter (libusb-win32 driver)

import nfc
def started(llc):
    print llc;return True
def connected(llc):
    print llc;return True
clf = nfc.ContactlessFrontend('usb')
clf.connect(llcp={'on-startup':started})
clf.connect(llcp={'on-connect':connected})

Output on Win7:
LLC: Local(MIU=128, LTO=500ms) Remote(MIU=None, LTO=Nonems)

Output on Raspberry Pi:
LLC: Local(MIU=128, LTO=500ms) Remote(MIU=None, LTO=Nonems)
LLC: Local(MIU=128, LTO=500ms) Remote(MIU=128, LTO=1500ms)

Any help would be much appreciated!

Question information

Language:
English Edit question
Status:
Solved
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Solved by:
Stephen Tiedemann
Solved:
2016-03-18
Last query:
2016-03-18
Last reply:
2016-03-16

I can't reproduce this, it works on both Linux and Windows. Though I could only try with latest trunk version where the Python USB library is no longer PyUSB but python-libusb1 (with Microsoft USB driver).

Have you already tried with one of the p2p examples, like beam.py?

imueller (ingo-mueller) said : #2

Hi Stephen, thanks for your reply.
Originally, running beam.py resulted in the same error.

I managed to get everything running after replacing the libusb-win32-bin-1.2.6.0 driver for the PN533 USB device with libusbK version 3.0.7.0. Seems a version clash of libusb API was the problem but... I ran into a new problem when starting on a clean sheet to get a minimal setup installed. The nfcpy version from the trunk together with python-libusb1 does not work for me. The error reads: unable to find USB device. But this is not my concern atm. I am OK with using the 0.10.2 nfcpy branch and pyusb and it works for the abovementioned test program. But beam.py fails again (note, it worked earlier with a messed up Win7 configuration). May I ask for a pointer how to solve this? Note, the smartphone runs Android 4.2.2.

C:\dev\nfcpy-0.10.2\examples>python beam.py send link http://nfcpy.org
[nfc.clf] searching for reader on path usb
[nfc.clf] using SensorID StickID PN533v2.7 at usb:003:003
[nfc.snep.server] snep server bound to port 4 (MIU=1984, RW=15), will accept up to 1048576 byte NDEF messages
[nfc.dep] running as NFC-DEP Initiator 424F active mode MIU=251 RWT=0.038664
[nfc.llcp.llc] LLCP Link established as NFC-DEP Initiator
Local LLCP Settings
  LLCP Version: 1.1
  Link Timeout: 500 ms
  Max Inf Unit: 2175 octet
  Service List: 0000000000010011
Remote LLCP Settings
  LLCP Version: 1.1
  Link Timeout: 1500 ms
  Max Inf Unit: 128 octet
  Service List: 0000000000010011
[nfc.llcp.llc] ProtocolError('unrecoverable NFC-DEP error in attention request',)
[nfc.dep] stop NFC-DEP Initiator 424F active mode MIU=251 RWT=0.038664, packets
sent/rcvd INF 0/0 ATN 0/0 ACK 0/0 NAK 0/0
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\dev\python2711\lib\threading.py", line 801, in __bootstrap_inner self.run()
  File "C:\dev\python2711\lib\threading.py", line 754, in run self.__target(*self.__args, **self.__kwargs)
  File "beam.py", line 100, in run_send_link_action send_message(args, llc, nfc.ndef.Message(sp))
  File "beam.py", line 81, in send_message if not nfc.snep.SnepClient(llc).put(message):
  File "C:\dev\nfcpy-0.10.2\nfc\snep\client.py", line 136, in put self.connect('urn:nfc:sn:snep')
  File "C:\dev\nfcpy-0.10.2\nfc\snep\client.py", line 83, in connect self.socket.connect(service_name)
  File "C:\dev\nfcpy-0.10.2\nfc\llcp\socket.py", line 88, in connect return self.llc.connect(self._tco, address)
  File "C:\dev\nfcpy-0.10.2\nfc\llcp\llc.py", line 536, in connect socket.connect(dest)
  File "C:\dev\nfcpy-0.10.2\nfc\llcp\tco.py", line 420, in connect except IndexError: raise Error(errno.EPIPE)
Error: nfc.llcp.Error: [EPIPE] Broken pipe

Enable debug logs with "-d nfc.clf" and timing info "--reltime", then look at where the ProtocolError happens. Older smartphones have tended to require more time to get the llcp link operational. You should also play with fixing the Initiator/Target mode "--mode initiator|target" to see if there is different behavior.

Regarding trunk and python-libusb1 ahve you followed the procedure at http://nfcpy.org/latest/topics/get-started.html#installation and copied libusb-1.0.dll ?

imueller (ingo-mueller) said : #4

Regarding trunk and python-libusb1. I had copied the libusb-1.0.dll and otherwise followed the procedure on http://nfcpy.readthedocs.org/en/latest/topics/get-started.html. To be honest, I wasn't aware of the page you mentioned.

Perhaps remove the now-outdated duplicate I was using or mark as 0.10x branch only.

Anyway, the problem was a beginner's mistake. I linked the wrong version of nfcpy to the python sys path. After switching to the trunk, things worked as expected.

Thanks for your suggestion re llcp protocol error. I'll look into it.

imueller (ingo-mueller) said : #5

Thanks Stephen Tiedemann, that solved my question.