[Pcsclite-muscle] Portable way to detect when a smart card is inserted

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Pcsclite-muscle] Portable way to detect when a smart card is inserted

Ignacio Casal
Hey guys,

do you know what is the proper way to detect when a smart card is inserted?

in some places I've seen something like this:

rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

while this worked for me on linux with pcsclite it does not seem to work for me on windows, while doing this next does:
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

if (rv == SCARD_S_SUCCESS) {
  rgReaderStates[0].dwCurrentState = rgReaderStates[0].dwEventState;
}

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

As you can see I need to do the call twice, first to get the current state and second to actually block to wait for the smart card to be inserted. Is this second way the recommended way also on pcsclite? If yes is the first version shown here in this mail some kind of shortcut for the 2 way call?
Is there a better way to check when a smart card is inserted?

Thanks in advance.

--
Ignacio Casal Quinteiro

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle
Reply | Threaded
Open this post in threaded view
|

Re: [Pcsclite-muscle] Portable way to detect when a smart card is inserted

helpcrypto helpcrypto
Hello ignacio


As you said, you have to call SCardGetStatusChange twice:
The first call get the current state of the readers, and has a timeout of 0.
The second call, with a timeout of INFINITE wait until any difference is detected.

Have a look at http://pcsclite.alioth.debian.org/api/group__API.html#ga33247d5d1257d59e55647c3bb717db24

There are many tricky things you could do to improve the speed/trustworthy of the detection, so start with something basic before looking for performance.






On Mon, Sep 15, 2014 at 2:18 PM, Ignacio Casal <[hidden email]> wrote:
Hey guys,

do you know what is the proper way to detect when a smart card is inserted?

in some places I've seen something like this:

rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

while this worked for me on linux with pcsclite it does not seem to work for me on windows, while doing this next does:
rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

if (rv == SCARD_S_SUCCESS) {
  rgReaderStates[0].dwCurrentState = rgReaderStates[0].dwEventState;
}

rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

As you can see I need to do the call twice, first to get the current state and second to actually block to wait for the smart card to be inserted. Is this second way the recommended way also on pcsclite? If yes is the first version shown here in this mail some kind of shortcut for the 2 way call?
Is there a better way to check when a smart card is inserted?

Thanks in advance.

--
Ignacio Casal Quinteiro

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle


_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle
Reply | Threaded
Open this post in threaded view
|

Re: [Pcsclite-muscle] Portable way to detect when a smart card is inserted

Bruno Jesus
In reply to this post by Ignacio Casal
On Mon, Sep 15, 2014 at 9:18 AM, Ignacio Casal
<[hidden email]> wrote:
>
> Hey guys,
>
> do you know what is the proper way to detect when a smart card is inserted?
> ...
>
> As you can see I need to do the call twice, first to get the current state and second to actually block to wait for the smart card to be inserted. Is this second way the recommended way also on pcsclite? If yes is the first version shown here in this mail some kind of shortcut for the 2 way call?
> Is there a better way to check when a smart card is inserted?

The easiest and less elegant way is to use only SCardConnect in a loop
(with a delay like 200ms) until it returns OK. It may not look
beautiful but works anywhere.

Some may argue that in a shared environment with multiple smart card
software running this would not work because one of the programs could
lock the card. In this case SCardConnect will return
SCARD_E_SHARING_VIOLATION so you still know there is a card.

> Thanks in advance.
>
> --
> Ignacio Casal Quinteiro


Best regards,
Bruno

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle
Reply | Threaded
Open this post in threaded view
|

Re: [Pcsclite-muscle] Portable way to detect when a smart card is inserted

Ludovic Rousseau
In reply to this post by Ignacio Casal
2014-09-15 14:18 GMT+02:00 Ignacio Casal <[hidden email]>:
> Hey guys,

Hello,

> do you know what is the proper way to detect when a smart card is inserted?
>
> in some places I've seen something like this:
>
> rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
> rgReaderStates[0].dwCurrentState = SCARD_STATE_EMPTY;
>
> rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);

This is done like that in PCSC/src/testpcsc.c

> while this worked for me on linux with pcsclite it does not seem to work for
> me on windows, while doing this next does:
> rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
> rgReaderStates[0].dwCurrentState = SCARD_STATE_UNAWARE;
>
> rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
>
> if (rv == SCARD_S_SUCCESS) {
>   rgReaderStates[0].dwCurrentState = rgReaderStates[0].dwEventState;
> }
>
> rv = SCardGetStatusChange(hContext, INFINITE, rgReaderStates, 1);
>
> As you can see I need to do the call twice, first to get the current state
> and second to actually block to wait for the smart card to be inserted. Is
> this second way the recommended way also on pcsclite? If yes is the first
> version shown here in this mail some kind of shortcut for the 2 way call?
> Is there a better way to check when a smart card is inserted?

The use of SCARD_STATE_EMPTY looks like a pcsc-lite feature.
This feature is present in pcsc-lite code at least since 2002 (the
oldest version of PCSC/src/winscard_clnt.c in the subversion
repository). I do not remember why this feature is present.

The safe way is to use the double call as you described but with a
timeout of 0 for the first call as corrected by "helpcrypto".

Please, please do NOT use a loop of proposed by Bruno Jesus.
I invested a lot of time in removing all polling loops in pcsc-lite
and the CCID driver. So please do not add a polling loop in your
application when you can do without one.

Thanks

--
 Dr. Ludovic Rousseau

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle
Reply | Threaded
Open this post in threaded view
|

Re: [Pcsclite-muscle] Portable way to detect when a smart card is inserted

Bruno Jesus
On Mon, Sep 15, 2014 at 1:43 PM, Ludovic Rousseau
<[hidden email]> wrote:
> Please, please do NOT use a loop of proposed by Bruno Jesus.
> I invested a lot of time in removing all polling loops in pcsc-lite
> and the CCID driver. So please do not add a polling loop in your
> application when you can do without one.

As I stated that is just the lazy way of doing it and certainly not a
good practice =)

> Thanks
>
> --
>  Dr. Ludovic Rousseau


Best regards,
Bruno

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle
Reply | Threaded
Open this post in threaded view
|

Re: [Pcsclite-muscle] Portable way to detect when a smart card is inserted

Ignacio Casal
Thanks you all for the answers!

On Mon, Sep 15, 2014 at 6:50 PM, Bruno Jesus <[hidden email]> wrote:
On Mon, Sep 15, 2014 at 1:43 PM, Ludovic Rousseau
<[hidden email]> wrote:
> Please, please do NOT use a loop of proposed by Bruno Jesus.
> I invested a lot of time in removing all polling loops in pcsc-lite
> and the CCID driver. So please do not add a polling loop in your
> application when you can do without one.

As I stated that is just the lazy way of doing it and certainly not a
good practice =)

> Thanks
>
> --
>  Dr. Ludovic Rousseau


Best regards,
Bruno

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle



--
Ignacio Casal Quinteiro

_______________________________________________
Pcsclite-muscle mailing list
[hidden email]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pcsclite-muscle