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-2012 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 __future__ import print_function 
25  from smartcard.CardConnectionDecorator import CardConnectionDecorator 
26  from smartcard.Exceptions import CardConnectionException 
27  from smartcard.scard import SCardConnect, SCardDisconnect 
28  from smartcard.scard import SCARD_LEAVE_CARD, SCARD_SHARE_EXCLUSIVE 
29  from smartcard.scard import SCardGetErrorMessage 
30  from smartcard.pcsc import PCSCCardConnection 
31  import smartcard.pcsc 
32   
33   
34 -class ExclusiveConnectCardConnection(CardConnectionDecorator):
35 '''This decorator uses exclusive access to the card during 36 connection to prevent other processes to connect to this card.''' 37
38 - def __init__(self, cardconnection):
39 CardConnectionDecorator.__init__(self, cardconnection)
40
41 - def connect(self, protocol=None, mode=None, disposition=None):
42 '''Disconnect and reconnect in exclusive mode PCSCCardconnections.''' 43 CardConnectionDecorator.connect(self, protocol, mode, disposition) 44 component = self.component 45 while True: 46 if isinstance( 47 component, 48 smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 49 pcscprotocol = PCSCCardConnection.translateprotocolmask( 50 protocol) 51 if 0 == pcscprotocol: 52 pcscprotocol = component.getProtocol() 53 54 if None != component.hcard: 55 hresult = SCardDisconnect(component.hcard, 56 SCARD_LEAVE_CARD) 57 if hresult != 0: 58 raise CardConnectionException('Failed to disconnect: ' 59 + SCardGetErrorMessage(hresult)) 60 hresult, component.hcard, dwActiveProtocol = SCardConnect( 61 component.hcontext, str(component.reader), 62 SCARD_SHARE_EXCLUSIVE, pcscprotocol) 63 if hresult != 0: 64 raise CardConnectionException( 65 'Failed to connect with SCARD_SHARE_EXCLUSIVE' +\ 66 SCardGetErrorMessage(hresult)) 67 # print('reconnected exclusive') 68 break 69 if hasattr(component, 'component'): 70 component = component.component 71 else: 72 break
73