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

Source Code for Module smartcard.ExclusiveTransmitCardConnection

 1  """Sample CardConnectionDecorator that provides exclusive transmit() 
 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 SCardBeginTransaction, SCardEndTransaction 
28  from smartcard.scard import SCARD_LEAVE_CARD 
29  from smartcard.scard import SCardGetErrorMessage 
30  import smartcard.pcsc 
31   
32   
33 -class ExclusiveTransmitCardConnection(CardConnectionDecorator):
34 '''This decorator uses SCardBeginTransaction/SCardEndTransaction to 35 preserve other processes of threads to access the card during 36 transmit().''' 37
38 - def __init__(self, cardconnection):
39 CardConnectionDecorator.__init__(self, cardconnection)
40
41 - def lock(self):
42 '''Lock card with SCardBeginTransaction.''' 43 44 component = self.component 45 while True: 46 if isinstance( 47 component, 48 smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 49 hresult = SCardBeginTransaction(component.hcard) 50 if 0 != hresult: 51 raise CardConnectionException( 52 'Failed to lock with SCardBeginTransaction: ' +\ 53 SCardGetErrorMessage(hresult)) 54 else: 55 #print('locked') 56 pass 57 break 58 if hasattr(component, 'component'): 59 component = component.component 60 else: 61 break
62
63 - def unlock(self):
64 '''Unlock card with SCardEndTransaction.''' 65 component = self.component 66 while True: 67 if isinstance( 68 component, 69 smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 70 hresult = SCardEndTransaction(component.hcard, 71 SCARD_LEAVE_CARD) 72 if 0 != hresult: 73 raise CardConnectionException( 74 'Failed to unlock with SCardEndTransaction: ' +\ 75 SCardGetErrorMessage(hresult)) 76 else: 77 #print('unlocked') 78 pass 79 break 80 if hasattr(component, 'component'): 81 component = component.component 82 else: 83 break
84
85 - def transmit(self, bytes, protocol=None):
86 '''Gain exclusive access to card during APDU transmission for if this 87 decorator decorates a PCSCCardConnection.''' 88 data, sw1, sw2 = CardConnectionDecorator.transmit( 89 self, bytes, protocol) 90 return data, sw1, sw2
91