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

LONG SCardConnect ( SCARDCONTEXT  hContext,
LPCSTR  szReader,
DWORD  dwShareMode,
DWORD  dwPreferredProtocols,
LPSCARDHANDLE  phCard,
LPDWORD  pdwActiveProtocol 
)

Establishes a connection to the reader specified in * szReader.

Parameters:
[in]hContextConnection context to the PC/SC Resource Manager.
[in]szReaderReader name to connect to.
[in]dwShareModeMode of connection type: exclusive or shared.
  • SCARD_SHARE_SHARED - This application will allow others to share the reader.
  • SCARD_SHARE_EXCLUSIVE - This application will NOT allow others to share the reader.
  • SCARD_SHARE_DIRECT - Direct control of the reader, even without a card. SCARD_SHARE_DIRECT can be used before using SCardControl() to send control commands to the reader even if a card is not present in the reader. Contrary to Windows winscard behavior, the reader is accessed in shared mode and not exclusive mode.
[in]dwPreferredProtocolsDesired protocol use.
[out]phCardHandle to this connection.
[out]pdwActiveProtocolEstablished protocol to this connection.
Returns:
Error code.
Return values:
SCARD_S_SUCCESSSuccessful (SCARD_S_SUCCESS)
SCARD_E_INVALID_HANDLEInvalid hContext handle (SCARD_E_INVALID_HANDLE)
SCARD_E_INVALID_PARAMETERphCard or pdwActiveProtocol is NULL (SCARD_E_INVALID_PARAMETER)
SCARD_E_INVALID_VALUEInvalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)
SCARD_E_NO_SERVICEThe server is not running (SCARD_E_NO_SERVICE)
SCARD_E_NO_SMARTCARDNo smart card present (SCARD_E_NO_SMARTCARD)
SCARD_E_NOT_READYCould not allocate the desired port (SCARD_E_NOT_READY)
SCARD_E_PROTO_MISMATCHRequested protocol is unknown (SCARD_E_PROTO_MISMATCH)
SCARD_E_READER_UNAVAILABLECould not power up the reader or card (SCARD_E_READER_UNAVAILABLE)
SCARD_E_SHARING_VIOLATIONSomeone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCARD_E_UNKNOWN_READERszReader is NULL (SCARD_E_UNKNOWN_READER)
SCARD_E_UNSUPPORTED_FEATUREProtocol not supported (SCARD_E_UNSUPPORTED_FEATURE)
SCARD_F_COMM_ERRORAn internal communications error has been detected (SCARD_F_COMM_ERROR)
SCARD_F_INTERNAL_ERRORAn internal consistency check failed (SCARD_F_INTERNAL_ERROR)
SCARD_W_UNPOWERED_CARDCard is not powered (SCARD_W_UNPOWERED_CARD)
SCARD_W_UNRESPONSIVE_CARDCard is mute (SCARD_W_UNRESPONSIVE_CARD)
 SCARDCONTEXT hContext;
 SCARDHANDLE hCard;
 DWORD dwActiveProtocol;
 LONG rv;
 ...
 rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
 rv = SCardConnect(hContext, "Reader X", SCARD_SHARE_SHARED,
          SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);

Definition at line 771 of file winscard_clnt.c.

References ATRDecodeAtr(), pubReaderStatesList::cardAtr, pubReaderStatesList::cardAtrLength, pubReaderStatesList::cardProtocol, ReaderContext::contexts, ReaderContext::hLockId, IFD_POWER_UP, IFD_SUCCESS, IFDPowerICC(), MessageReceive(), MessageSendWithHeader(), ReaderContext::mMutex, PCSCLITE_LOCK_POLL_RATE, PCSCLITE_SHARING_EXCLUSIVE_CONTEXT, PCSCLITE_SHARING_NO_CONTEXT, PHSetProtocol(), POWER_STATE_INUSE, POWER_STATE_UNPOWERED, ReaderContext::powerState, ReaderContext::powerState_lock, pubReaderStatesList::readerSharing, pubReaderStatesList::readerState, ReaderContext::readerState, SCARD_CONNECT, SCARD_E_INVALID_HANDLE, SCARD_E_INVALID_PARAMETER, SCARD_E_INVALID_VALUE, SCARD_E_NO_SMARTCARD, SCARD_E_PROTO_MISMATCH, SCARD_E_SHARING_VIOLATION, SCARD_E_UNKNOWN_READER, SCARD_F_INTERNAL_ERROR, SCARD_NEGOTIABLE, SCARD_POWERED, SCARD_PRESENT, SCARD_PROTOCOL_ANY_OLD, SCARD_PROTOCOL_RAW, SCARD_PROTOCOL_T0, SCARD_PROTOCOL_T1, SCARD_PROTOCOL_UNDEFINED, SCARD_S_SUCCESS, SCARD_SHARE_DIRECT, SCARD_SHARE_EXCLUSIVE, SCARD_SHARE_SHARED, SCARD_W_UNPOWERED_CARD, SCARD_W_UNRESPONSIVE_CARD, SCardGetContext(), and SYS_USleep().

{
      LONG rv;
      struct connect_struct scConnectStruct;
      SCONTEXTMAP * currentContextMap;

      PROFILE_START
      API_TRACE_IN("%ld %s %ld %ld", hContext, szReader, dwShareMode, dwPreferredProtocols)

      /*
       * Check for NULL parameters
       */
      if (phCard == NULL || pdwActiveProtocol == NULL)
            return SCARD_E_INVALID_PARAMETER;
      else
            *phCard = 0;

      if (szReader == NULL)
            return SCARD_E_UNKNOWN_READER;

      /*
       * Check for uninitialized strings
       */
      if (strlen(szReader) > MAX_READERNAME)
            return SCARD_E_INVALID_VALUE;

      /*
       * Make sure this context has been opened
       */
      currentContextMap = SCardGetContext(hContext);
      if (NULL == currentContextMap)
            return SCARD_E_INVALID_HANDLE;

      (void)pthread_mutex_lock(currentContextMap->mMutex);

      /* check the context is still opened */
      currentContextMap = SCardGetContext(hContext);
      if (NULL == currentContextMap)
            /* the context is now invalid
             * -> another thread may have called SCardReleaseContext
             * -> so the mMutex has been unlocked */
            return SCARD_E_INVALID_HANDLE;

      strlcpy(scConnectStruct.szReader, szReader, sizeof scConnectStruct.szReader);

      scConnectStruct.hContext = hContext;
      scConnectStruct.dwShareMode = dwShareMode;
      scConnectStruct.dwPreferredProtocols = dwPreferredProtocols;
      scConnectStruct.hCard = 0;
      scConnectStruct.dwActiveProtocol = 0;
      scConnectStruct.rv = SCARD_S_SUCCESS;

      rv = MessageSendWithHeader(SCARD_CONNECT, currentContextMap->dwClientID,
            sizeof(scConnectStruct), (void *) &scConnectStruct);

      if (rv != SCARD_S_SUCCESS)
            goto end;

      /*
       * Read a message from the server
       */
      rv = MessageReceive(&scConnectStruct, sizeof(scConnectStruct),
            currentContextMap->dwClientID);

      if (rv != SCARD_S_SUCCESS)
            goto end;

      *phCard = scConnectStruct.hCard;
      *pdwActiveProtocol = scConnectStruct.dwActiveProtocol;

      if (scConnectStruct.rv == SCARD_S_SUCCESS)
      {
            /*
             * Keep track of the handle locally
             */
            rv = SCardAddHandle(*phCard, currentContextMap, szReader);
      }
      else
            rv = scConnectStruct.rv;

end:
      (void)pthread_mutex_unlock(currentContextMap->mMutex);

      PROFILE_END(rv)
      API_TRACE_OUT("%d", *pdwActiveProtocol)

      return rv;
}

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index