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

Ludovic Rousseau ludovic.rousseau at gmail.com
Mon Sep 15 16:43:28 UTC 2014


2014-09-15 14:18 GMT+02:00 Ignacio Casal <ignacio.casal at nice-software.com>:
> 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



More information about the Pcsclite-muscle mailing list