Logo Search packages:      
Sourcecode: pcsc-lite version File versions  Download package

LONG SCardStatus ( SCARDHANDLE  hCard,
LPTSTR  mszReaderNames,
LPDWORD  pcchReaderLen,
LPDWORD  pdwState,
LPDWORD  pdwProtocol,
LPBYTE  pbAtr,
LPDWORD  pcbAtrLen 
)

This function returns the current status of the reader connected to by hCard.

It's friendly name will be stored in szReaderName. pcchReaderLen will be the size of the allocated buffer for szReaderName, while pcbAtrLen will be the size of the allocated buffer for pbAtr. If either of these is too small, the function will return with SCARD_E_INSUFFICIENT_BUFFER and the necessary size in pcchReaderLen and pcbAtrLen. The current state, and protocol will be stored in pdwState and pdwProtocol respectively.

Parameters:
[in] hCard Connection made from SCardConnect.
mszReaderNames [inout] Friendly name of this reader.
pcchReaderLen [inout] Size of the szReaderName multistring.
[out] pdwState Current state of this reader. pdwState is a DWORD possibly OR'd with the following values:
  • SCARD_ABSENT - There is no card in the reader.
  • SCARD_PRESENT - There is a card in the reader, but it has not been moved into position for use.
  • SCARD_SWALLOWED - There is a card in the reader in position for use. The card is not powered.
  • SCARD_POWERED - Power is being provided to the card, but the reader driver is unaware of the mode of the card.
  • SCARD_NEGOTIABLE - The card has been reset and is awaiting PTS negotiation.
  • SCARD_SPECIFIC - The card has been reset and specific communication protocols have been established.
[out] pdwProtocol Current protocol of this reader.
  • SCARD_PROTOCOL_T0 Use the T=0 protocol.
  • SCARD_PROTOCOL_T1 Use the T=1 protocol.
[out] pbAtr Current ATR of a card in this reader.
[out] pcbAtrLen Length of ATR.
Returns:
Error code.
Return values:
SCARD_S_SUCCESS Successful.
SCARD_E_INVALID_HANDLE Invalid hCard handle.
SCARD_E_INSUFFICIENT_BUFFER Not enough allocated memory for szReaderName or for pbAtr.
SCARD_E_READER_UNAVAILABLE The reader has been removed.
Test:
 SCARDCONTEXT hContext;
 SCARDHANDLE hCard;
 DWORD dwActiveProtocol;
 DWORD dwState, dwProtocol, dwAtrLen, dwReaderLen;
 BYTE pbAtr[MAX_ATR_SIZE];
 ...
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardConnect(hContext, "Reader X", SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
 ...
 dwAtrLen = sizeof(pbAtr);
 rv=SCardStatus(hCard, NULL, &dwReaderLen, &dwState, &dwProtocol, pbAtr, &dwAtrLen);

Definition at line 1291 of file winscard_clnt.c.

References rxSharedSegment::data, status_struct::hCard, status_struct::mszReaderNames, status_struct::pbAtr, status_struct::pcbAtrLen, status_struct::pcchReaderLen, _psContextMap::psChannelMap, psContextMap, _psChannelMap::readerName, SCardCheckDaemonAvailability(), SHMClientRead(), SYS_MutexLock(), SYS_MutexUnLock(), and WrapSHMWrite().

{
      DWORD dwReaderLen, dwAtrLen;
      LONG rv;
      int i;
      status_struct scStatusStruct;
      sharedSegmentMsg msgStruct;
      DWORD dwContextIndex, dwChannelIndex;

      /*
       * Check for NULL parameters
       */

      if (pcchReaderLen == NULL || pcbAtrLen == NULL)
            return SCARD_E_INVALID_PARAMETER;

      /* length passed from caller */
      dwReaderLen = *pcchReaderLen;
      dwAtrLen = *pcbAtrLen;

      /* default output values */
      if (pdwState)
            *pdwState = 0;

      if (pdwProtocol)
            *pdwProtocol = 0;

      *pcchReaderLen = 0;
      *pcbAtrLen = 0;

      if (SCardCheckDaemonAvailability() != SCARD_S_SUCCESS)
            return SCARD_E_NO_SERVICE;

      /*
       * Make sure this handle has been opened
       */
      rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);

      if (rv == -1)
            return SCARD_E_INVALID_HANDLE;

      SYS_MutexLock(psContextMap[dwContextIndex].mMutex);   

      for (i = 0; i < PCSCLITE_MAX_READERS_CONTEXTS; i++)
      {
            char *r = psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName;

            /* by default r == NULL */
            if (r && strcmp(r, (readerStates[i])->readerName) == 0)
                  break;
      }

      if (i == PCSCLITE_MAX_READERS_CONTEXTS)
      {
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 
            return SCARD_E_READER_UNAVAILABLE;
      }

      /* initialise the structure */
      memset(&scStatusStruct, 0, sizeof(scStatusStruct));
      scStatusStruct.hCard = hCard;

      /* those sizes need to be initialised */
      scStatusStruct.pcchReaderLen = sizeof(scStatusStruct.mszReaderNames);
      scStatusStruct.pcbAtrLen = sizeof(scStatusStruct.pbAtr);

      rv = WrapSHMWrite(SCARD_STATUS, psContextMap[dwContextIndex].dwClientID,
            sizeof(scStatusStruct),
            PCSCLITE_CLIENT_ATTEMPTS, (void *) &scStatusStruct);

      if (rv == -1)
      {
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 
            return SCARD_E_NO_SERVICE;
      }

      /*
       * Read a message from the server
       */
      rv = SHMClientRead(&msgStruct, psContextMap[dwContextIndex].dwClientID, PCSCLITE_CLIENT_ATTEMPTS);

      memcpy(&scStatusStruct, &msgStruct.data, sizeof(scStatusStruct));

      if (rv == -1)
      {
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 
            return SCARD_F_COMM_ERROR;
      }

      rv = scStatusStruct.rv;
      if (rv != SCARD_S_SUCCESS && rv != SCARD_E_INSUFFICIENT_BUFFER)
      {
            /*
             * An event must have occurred
             */
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 
            return rv;
      }

      /*
       * Now continue with the client side SCardStatus
       */

      *pcchReaderLen = strlen(psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName) + 1;
      *pcbAtrLen = (readerStates[i])->cardAtrLength;

      if (pdwState)
            *pdwState = (readerStates[i])->readerState;

      if (pdwProtocol)
            *pdwProtocol = (readerStates[i])->cardProtocol;

      /* return SCARD_E_INSUFFICIENT_BUFFER only if buffer pointer is non NULL */
      if (mszReaderNames)
      {
            if (*pcchReaderLen > dwReaderLen)
                  rv = SCARD_E_INSUFFICIENT_BUFFER;

            strncpy(mszReaderNames, 
                  psContextMap[dwContextIndex].psChannelMap[dwChannelIndex].readerName, 
                  dwReaderLen);
      }

      if (pbAtr)
      {
            if (*pcbAtrLen > dwAtrLen)
                  rv = SCARD_E_INSUFFICIENT_BUFFER;

            memcpy(pbAtr, (readerStates[i])->cardAtr,
                  min(*pcbAtrLen, dwAtrLen));
      }
      
      SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex); 

      return rv;
}


Generated by  Doxygen 1.6.0   Back to index