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 friendly name of the reader specified in szReader. The first connection will power up and perform a reset on the card.

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.
[in] dwPreferredProtocols Desired protocol use.
  • SCARD_PROTOCOL_T0 - Use the T=0 protocol.
  • SCARD_PROTOCOL_T1 - Use the T=1 protocol.
  • SCARD_PROTOCOL_RAW - Use with memory type cards. dwPreferredProtocols is a bit mask of acceptable protocols for the connection. You can use (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if you do not have a preferred protocol.
[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_NOT_READY Could not allocate the desired port (SCARD_E_NOT_READY)
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_INTERNAL_ERROR An internal consistency check failed (SCARD_F_INTERNAL_ERROR)
Test:
 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 720 of file winscard_clnt.c.

References rxSharedSegment::data, connect_struct::dwPreferredProtocols, connect_struct::dwShareMode, connect_struct::hContext, PCSCLITE_CLIENT_ATTEMPTS, connect_struct::pdwActiveProtocol, connect_struct::phCard, psContextMap, connect_struct::rv, SCARD_CONNECT, SCARD_E_INVALID_HANDLE, SCARD_E_INVALID_PARAMETER, SCARD_E_INVALID_VALUE, SCARD_E_NO_SERVICE, SCARD_E_UNKNOWN_READER, SCARD_F_COMM_ERROR, SCARD_S_SUCCESS, SCardCheckDaemonAvailability(), SCardGetContextIndice(), SHMClientRead(), connect_struct::szReader, and WrapSHMWrite().

{
      LONG rv;
      connect_struct scConnectStruct;
      sharedSegmentMsg msgStruct;
      LONG dwContextIndex;

      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;

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

      /*
       * Make sure this context has been opened
       */
      dwContextIndex = SCardGetContextIndice(hContext);
      if (dwContextIndex == -1)
            return SCARD_E_INVALID_HANDLE;

      SYS_MutexLock(psContextMap[dwContextIndex].mMutex);

      /* check the context is still opened */
      dwContextIndex = SCardGetContextIndice(hContext);
      if (dwContextIndex == -1)
            /* 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.phCard = 0;
      scConnectStruct.pdwActiveProtocol = 0;
      scConnectStruct.rv = SCARD_S_SUCCESS;

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

      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(&scConnectStruct, &msgStruct.data, sizeof(scConnectStruct));

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

      *phCard = scConnectStruct.phCard;
      *pdwActiveProtocol = scConnectStruct.pdwActiveProtocol;

      if (scConnectStruct.rv == SCARD_S_SUCCESS)
      {
            /*
             * Keep track of the handle locally
             */
            rv = SCardAddHandle(*phCard, dwContextIndex, szReader);
            SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);

            PROFILE_END(rv)

            return rv;
      }

      SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);

      PROFILE_END(scConnectStruct.rv)

      return scConnectStruct.rv;
}


Generated by  Doxygen 1.6.0   Back to index