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-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 SCardBeginTransaction, SCardEndTransaction, SCARD_LEAVE_CARD 
28  from smartcard.scard import SCardGetErrorMessage 
29  from smartcard.pcsc import PCSCCardConnection 
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(component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 47 hresult = SCardBeginTransaction(component.hcard) 48 if 0 != hresult: 49 raise CardConnectionException('Failed to lock with SCardBeginTransaction' + SCardGetErrorMessage(hresult)) 50 else: 51 #print 'locked' 52 pass 53 break 54 if hasattr(component, 'component'): 55 component = component.component 56 else: 57 break
58
59 - def unlock(self):
60 '''Unlock card with SCardEndTransaction.''' 61 component = self.component 62 while True: 63 if isinstance(component, smartcard.pcsc.PCSCCardConnection.PCSCCardConnection): 64 hresult = SCardEndTransaction(component.hcard, SCARD_LEAVE_CARD) 65 if 0 != hresult: 66 raise CardConnectionException('Failed to unlock with SCardEndTransaction' + SCardGetErrorMessage(hresult)) 67 else: 68 #print 'unlocked' 69 pass 70 break 71 if hasattr(component, 'component'): 72 component = component.component 73 else: 74 break
75
76 - def transmit(self, bytes, protocol=None):
77 '''Gain exclusive access to card during APDU transmission for if this 78 decorator decorates a PCSCCardConnection.''' 79 data, sw1, sw2 = CardConnectionDecorator.transmit(self, bytes, protocol) 80 return data, sw1, sw2
81