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 
)

This function establishes a connection to the reader specified in szReader.

Parameters:
[in] hContext Connection context to the PC/SC Resource Manager.
[in] szReader Reader name to connect to.
[in] dwShareMode Mode 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] dwPreferredProtocols Desired protocol use.
[out] phCard Handle to this connection.
[out] pdwActiveProtocol Established protocol to this connection.
Returns:
Error code.
Return values:
SCARD_S_SUCCESS Successful (SCARD_S_SUCCESS)
SCARD_E_INVALID_HANDLE Invalid hContext handle (SCARD_E_INVALID_HANDLE)
SCARD_E_INVALID_PARAMETER phCard or pdwActiveProtocol is NULL (SCARD_E_INVALID_PARAMETER)
SCARD_E_INVALID_VALUE Invalid sharing mode, requested protocol, or reader name (SCARD_E_INVALID_VALUE)
SCARD_E_NO_SERVICE The server is not runing (SCARD_E_NO_SERVICE)
SCARD_E_NO_SMARTCARD No smart card present (SCARD_E_NO_SMARTCARD)
SCARD_E_NOT_READY Could not allocate the desired port (SCARD_E_NOT_READY)
SCARD_E_PROTO_MISMATCH Requested protocol is unknown (SCARD_E_PROTO_MISMATCH)
SCARD_E_READER_UNAVAILABLE Could not power up the reader or card (SCARD_E_READER_UNAVAILABLE)
SCARD_E_SHARING_VIOLATION Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCARD_E_UNKNOWN_READER szReader is NULL (SCARD_E_UNKNOWN_READER)
SCARD_E_UNSUPPORTED_FEATURE Protocol not supported (SCARD_E_UNSUPPORTED_FEATURE)
SCARD_F_COMM_ERROR An internal communications error has been detected (SCARD_F_COMM_ERROR)
SCARD_F_INTERNAL_ERROR An internal consistency check failed (SCARD_F_INTERNAL_ERROR)
SCARD_W_UNPOWERED_CARD Card is not powered (SCARD_W_UNPOWERED_CARD)
SCARD_W_UNRESPONSIVE_CARD Card 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 809 of file winscard_clnt.c.

References connect_struct::dwActiveProtocol, connect_struct::dwPreferredProtocols, connect_struct::dwShareMode, connect_struct::hCard, connect_struct::hContext, PCSCLITE_LOCK_POLL_RATE, PCSCLITE_READ_TIMEOUT, PHGetAvailableProtocols(), PHGetDefaultProtocol(), PHSetProtocol(), connect_struct::rv, SCARD_CONNECT, SCARD_E_INVALID_HANDLE, SCARD_E_INVALID_PARAMETER, SCARD_E_INVALID_VALUE, SCARD_E_NO_SERVICE, SCARD_E_NO_SMARTCARD, SCARD_E_PROTO_MISMATCH, SCARD_E_SHARING_VIOLATION, SCARD_E_UNKNOWN_READER, SCARD_EXCLUSIVE_CONTEXT, SCARD_F_COMM_ERROR, SCARD_F_INTERNAL_ERROR, SCARD_NO_CONTEXT, 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_SWALLOWED, SCARD_W_UNPOWERED_CARD, SCARD_W_UNRESPONSIVE_CARD, SCardGetContext(), SHMMessageReceive(), SHMMessageSendWithHeader(), SYS_USleep(), and connect_struct::szReader.

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

      PROFILE_START

      /*
       * 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;

      CHECK_SAME_PROCESS

      /*
       * 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;

      strncpy(scConnectStruct.szReader, szReader, MAX_READERNAME);

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

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

      if (rv == -1)
      {
            rv = SCARD_E_NO_SERVICE;
            goto end;
      }

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

      if (rv < 0)
      {
            rv = SCARD_F_COMM_ERROR;
            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)

      return rv;
}


Generated by  Doxygen 1.6.0   Back to index