Package smartcard :: Module ExclusiveConnectCardConnection
[hide private]
[frames] | no frames]

Source Code for Module smartcard.ExclusiveConnectCardConnection

 1  """CardConnectionDecorator that provides exclusive use of a card. 
 2   
 3  __author__ = "http://www.gemalto.com" 
 4   
 5  Copyright 2001-2011 gemalto 
 6  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
 7   
 8  This file is part of pyscard. 
 9   
10  pyscard is free software; you can redistribute it and/or modify 
11  it under the terms of the GNU Lesser General Public License as published by 
12  the Free Software Foundation; either version 2.1 of the License, or 
13  (at your option) any later version. 
14   
15  pyscard is distributed in the hope that it will be useful, 
16  but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  GNU Lesser General Public License for more details. 
19   
20  You should have received a copy of the GNU Lesser General Public License 
21  along with pyscard; if not, write to the Free Software 
22  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
23  """ 
24  from smartcard.CardConnection import CardConnection 
25  from smartcard.CardConnectionDecorator import CardConnectionDecorator 
26  from smartcard.Exceptions import CardConnectionException 
27  from smartcard.scard import SCardConnect, SCardDisconnect, SCARD_LEAVE_CARD, SCARD_SHARE_EXCLUSIVE 
28  from smartcard.scard import SCardGetErrorMessage 
29  from smartcard.pcsc import PCSCCardConnection 
30  import smartcard.pcsc 
31   
32   
33 -class ExclusiveConnectCardConnection(CardConnectionDecorator):
34 '''This decorator uses exclusive access to the card during 35 connection to prevent other processes to connect to this card.''' 36
37 - def __init__(self, cardconnection):
38 CardConnectionDecorator.__init__(self, cardconnection)
39
40 - def connect(self, protocol=None, mode=None, disposition=None):
41 '''Disconnect and reconnect in exclusive mode PCSCCardconnections.''' 42 CardConnectionDecorator.connect(self, protocol, mode, disposition) 43 component = self.component 44 while True: 45 if isinstance(component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 46 pcscprotocol = PCSCCardConnection.translateprotocolmask(protocol) 47 if 0 == pcscprotocol: 48 pcscprotocol = component.getProtocol() 49 50 if None != component.hcard: 51 hresult = SCardDisconnect(component.hcard, SCARD_LEAVE_CARD) 52 if hresult != 0: 53 raise CardConnectionException('Failed to disconnect: ' 54 + SCardGetErrorMessage(hresult)) 55 hresult, component.hcard, dwActiveProtocol = SCardConnect( 56 component.hcontext, str(component.reader), 57 SCARD_SHARE_EXCLUSIVE, pcscprotocol) 58 if hresult != 0: 59 raise CardConnectionException('Failed to connect with SCARD_SHARE_EXCLUSIVE' + SCardGetErrorMessage(hresult)) 60 # print 'reconnected exclusive' 61 break 62 if hasattr(component, 'component'): 63 component = component.component 64 else: 65 break
66