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

LONG SCardBeginTransaction ( SCARDHANDLE  hCard  ) 

This function establishes a temporary exclusive access mode for doing a serie of commands in a transaction.

You might want to use this when you are selecting a few files and then writing a large file so you can make sure that another application will not change the current file. If another application has a lock on this reader or this application is in SCARD_SHARE_EXCLUSIVE there will be no action taken.

Parameters:
[in] hCard Connection made from SCardConnect().
Returns:
Error code.
Return values:
SCARD_S_SUCCESS Successful (SCARD_S_SUCCESS)
SCARD_E_INVALID_HANDLE Invalid hCard handle (SCARD_E_INVALID_HANDLE)
SCARD_E_NO_SERVICE The server is not runing (SCARD_E_NO_SERVICE)
SCARD_E_READER_UNAVAILABLE The reader has been removed (SCARD_E_READER_UNAVAILABLE)
SCARD_E_SHARING_VIOLATION Someone else has exclusive rights (SCARD_E_SHARING_VIOLATION)
SCARD_F_COMM_ERROR An internal communications error has been detected (SCARD_F_COMM_ERROR)
 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);
 rv = SCardBeginTransaction(hCard);
 ...
 / * Do some transmit commands * /

Definition at line 1133 of file winscard_clnt.c.

References rxSharedSegment::data, begin_struct::hCard, PCSCLITE_CLIENT_ATTEMPTS, PCSCLITE_MAX_READERS_CONTEXTS, _psContextMap::psChannelMap, psContextMap, _psChannelMap::readerName, begin_struct::rv, SCARD_BEGIN_TRANSACTION, SCARD_E_INVALID_HANDLE, SCARD_E_NO_SERVICE, SCARD_E_READER_UNAVAILABLE, SCARD_E_SHARING_VIOLATION, SCARD_F_COMM_ERROR, SCARD_S_SUCCESS, SCardCheckDaemonAvailability(), SHMClientRead(), and WrapSHMWrite().

{

      LONG rv;
      begin_struct scBeginStruct;
      int i;
      sharedSegmentMsg msgStruct;
      DWORD dwContextIndex, dwChannelIndex;

      PROFILE_START

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

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

      (void)SYS_MutexLock(psContextMap[dwContextIndex].mMutex);

      /* check the handle is still valid */
      rv = SCardGetIndicesFromHandle(hCard, &dwContextIndex, &dwChannelIndex);
      if (rv == -1)
            /* the handle is now invalid
             * -> another thread may have called SCardReleaseContext
             * -> so the mMutex has been unlocked */
            return SCARD_E_INVALID_HANDLE;

      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)
      {
            (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
            return SCARD_E_READER_UNAVAILABLE;
      }

      scBeginStruct.hCard = hCard;
      scBeginStruct.rv = SCARD_S_SUCCESS;

      /*
       * Query the server every so often until the sharing violation ends
       * and then hold the lock for yourself.
       */

      do
      {
            rv = WrapSHMWrite(SCARD_BEGIN_TRANSACTION, psContextMap[dwContextIndex].dwClientID,
                  sizeof(scBeginStruct),
                  PCSCLITE_CLIENT_ATTEMPTS, (void *) &scBeginStruct);

            if (rv == -1)
            {

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

            if (rv == -1)
            {

                  (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);
                  return SCARD_F_COMM_ERROR;
            }

      }
      while (scBeginStruct.rv == SCARD_E_SHARING_VIOLATION);

      (void)SYS_MutexUnLock(psContextMap[dwContextIndex].mMutex);

      PROFILE_END(scBeginStruct.rv);

      return scBeginStruct.rv;
}


Generated by  Doxygen 1.6.0   Back to index