santigimeno / node-pcsclite

Bindings over pcsclite to access Smart Cards
ISC License
170 stars 125 forks source link

src: migrate pthreads to uv_threads #24

Closed santigimeno closed 9 years ago

santigimeno commented 9 years ago
oliversalzburg commented 9 years ago

This is what I get when trying to build on Windows: https://gist.github.com/oliversalzburg/450e114fbb371b32acd7

So, yeah, it seems like the threads are no longer a problem, now snprintf is :P

santigimeno commented 9 years ago

@oliversalzburg thanks for the report! Can you try again?

oliversalzburg commented 9 years ago

Only 2 errors left:

..\src\pcsclite.cpp(46): error C3861: 'pcsc_stringify_error': identifier not found [d:\temp\node-pcsclite\build\pcsclite.vcxproj]
..\src\pcsclite.cpp(192): error C3861: 'sleep': identifier not found [d:\temp\node-pcsclite\build\pcsclite.vcxproj]
santigimeno commented 9 years ago

@oliversalzburg I think those are fixed now

LudovicRousseau commented 9 years ago

Build and works on Mac OS X.

oliversalzburg commented 9 years ago

Yep, building works on Windows. I can't functionally test anything here at home though :)

oliversalzburg commented 9 years ago

When I run example.js on Windows, I get an infinite loop of this:

New reader detected OMNIKEY CardMan 5x21 0
New reader detected OMNIKEY CardMan 5x21-CL 0
Status( OMNIKEY CardMan 5x21 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
New reader detected OMNIKEY CardMan 5x21 0
New reader detected OMNIKEY CardMan 5x21-CL 0
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
Status( OMNIKEY CardMan 5x21 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
santigimeno commented 9 years ago

Two things:

Thanks!

oliversalzburg commented 9 years ago

I updated my build log here: https://gist.github.com/oliversalzburg/450e114fbb371b32acd7 There are some warnings.

The device I have connected registers as 2 readers, no cards are inserted.

santigimeno commented 9 years ago

The problem could be this:

\?PnP?\Notification and reader count

The documentation states: rgReaderStates - 'To be notified of the arrival of a new smart card reader, set the szReader member of a SCARD_READERSTATE structure to "\\\\?PnP?\\Notification", and set all of the other members of that structure to zero.'

It seems that the high word of the dwCurrentState member of the SCARD_READERSTATE structure must also contain the count of currently attached readers otherwise SCardGetStatusChange returns immediately.

e.g. If one reader is already attached, the value can be set with:

DWORD
currentReaderCount
= 1;
monitorReadersState.dwCurrentState = (currentReaderCount << 16);

Taken from the community additions @ http://msdn.microsoft.com/en-us/library/windows/desktop/aa379773%28v=vs.85%29.aspx

LudovicRousseau commented 9 years ago

http://pcsclite.alioth.debian.org/api/group__API.html#ga33247d5d1257d59e55647c3bb717db24

dwEventState also contains a number of events in the upper 16 bits (dwEventState & 0xFFFF0000). This number of events is incremented for each card insertion or removal in the specified reader.

This can be used to detect a card removal/insertion between two calls to SCardGetStatusChange()

You need to make a 1st call to SCardGetStatusChange() to get the current values in dwEventState. Then copy dwEventState in dwCurrentState to be notified when something changes.

I was not aware that for the \\?PnP?\Notification special reader this number was the number of readers. I will try to add that in pcsc-lite. I opened a feature request at https://alioth.debian.org/tracker/index.php?func=detail&aid=314834&group_id=30105&atid=410088

santigimeno commented 9 years ago

Yes, I was doing that copy when checking status in CardReader but not when checking the \\?PnP?\Notification in PCSCLite. I'll update the PR accordingly. Thanks for the explanation

santigimeno commented 9 years ago

@oliversalzburg can you try again?

oliversalzburg commented 9 years ago

@santigimeno Much better, but still a bit confusing:

$ node examples/example.js
New reader detected OMNIKEY CardMan 5x21 0
New reader detected OMNIKEY CardMan 5x21-CL 0
Status( OMNIKEY CardMan 5x21 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 18, atr: <SlowBuffer > }
card removed
Disconnected
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 65570,
  atr: <SlowBuffer 3b 8f 80 01 80 4f 0c a0 00 00 03 06 03 00 01 00 00 00 00 6a> }
card inserted
Protocol( OMNIKEY CardMan 5x21-CL 0 ): 2
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 65826,
  atr: <SlowBuffer 3b 8f 80 01 80 4f 0c a0 00 00 03 06 03 00 01 00 00 00 00 6a> }
[Error: SCardTransmit error: (0x0000001f)]
Status( OMNIKEY CardMan 5x21-CL 0 ): { state: 131090, atr: <SlowBuffer > }
card removed
Disconnected

I don't understand the first "card removed" events right after I start the application.

I then put one card onto the reader and removed it after a couple of seconds, I don't know if the output is to be expected.

Looks pretty good overall though. Nice.

santigimeno commented 9 years ago

@oliversalzburg Yeah, the example could be improved. In this case it just means that there's no card inserted in the reader. Other than that the output looks correct. I don't know what the 0x0000001f SCardTransmit error means though.

santigimeno commented 9 years ago

@oliversalzburg On a second thought, it seems to me that the transmit(SCardTransmit) call was blocked and didn't returned until you removed the card. I'm experiencing similar issues with OMNIKEY readers myself. Can you confirm this?

oliversalzburg commented 9 years ago

@santigimeno The error is emitted right after connecting the card.

santigimeno commented 9 years ago

Ok. Then it simply looks like the SCardTransmit failed.

santigimeno commented 9 years ago

Already merged in master and published in version 0.4.0