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

Source Code for Module smartcard.CardConnection

  1  """The CardConnection abstract class manages connections with a card and 
  2  apdu transmission. 
  3   
  4  __author__ = "http://www.gemalto.com" 
  5   
  6  Copyright 2001-2012 gemalto 
  7  Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com 
  8   
  9  This file is part of pyscard. 
 10   
 11  pyscard is free software; you can redistribute it and/or modify 
 12  it under the terms of the GNU Lesser General Public License as published by 
 13  the Free Software Foundation; either version 2.1 of the License, or 
 14  (at your option) any later version. 
 15   
 16  pyscard is distributed in the hope that it will be useful, 
 17  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 19  GNU Lesser General Public License for more details. 
 20   
 21  You should have received a copy of the GNU Lesser General Public License 
 22  along with pyscard; if not, write to the Free Software 
 23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 24  """ 
 25   
 26  from smartcard.CardConnectionEvent import CardConnectionEvent 
 27  from smartcard.Observer import Observable 
 28   
 29   
30 -class CardConnection(Observable):
31 """Card connection abstract class. 32 33 Known subclasses: smartcard.pcsc.PCSCCardConnection 34 """ 35 T0_protocol = 0x00000001 36 T1_protocol = 0x00000002 37 RAW_protocol = 0x00010000 38 T15_protocol = 0x00000008 39
40 - def __init__(self, reader):
41 """Construct a new card connection. 42 43 @param reader: name of the reader in which the smartcard to connect 44 to is located. 45 """ 46 Observable.__init__(self) 47 self.reader = reader 48 self.errorcheckingchain = None 49 self.defaultprotocol = CardConnection.T0_protocol |\ 50 CardConnection.T1_protocol
51
52 - def __del__(self):
53 """Connect to card.""" 54 pass
55
56 - def addSWExceptionToFilter(self, exClass):
57 """Add a status word exception class to be filtered. 58 59 @param exClass: the class to filter, e.g. 60 L{smartcard.sw.SWException.WarningProcessingException} 61 62 Filtered exceptions will not be raised when encountered in the 63 error checking chain.""" 64 if None != self.errorcheckingchain: 65 self.errorcheckingchain[0].addFilterException(exClass)
66
67 - def addObserver(self, observer):
68 """Add a CardConnection observer.""" 69 Observable.addObserver(self, observer)
70
71 - def deleteObserver(self, observer):
72 """Remove a CardConnection observer.""" 73 Observable.deleteObserver(self, observer)
74
75 - def connect(self, protocol=None, mode=None, disposition=None):
76 """Connect to card. 77 @param protocol: a bit mask of the protocols to use, from 78 L{CardConnection.T0_protocol}, L{CardConnection.T1_protocol}, 79 L{CardConnection.RAW_protocol}, L{CardConnection.T15_protocol} 80 81 @param mode: SCARD_SHARE_SHARED (default), SCARD_SHARE_EXCLUSIVE or 82 SCARD_SHARE_DIRECT 83 84 @param disposition: SCARD_LEAVE_CARD (default), SCARD_RESET_CARD, 85 SCARD_UNPOWER_CARD or SCARD_EJECT_CARD 86 """ 87 Observable.setChanged(self) 88 Observable.notifyObservers(self, CardConnectionEvent('connect'))
89
90 - def disconnect(self):
91 """Disconnect from card.""" 92 Observable.setChanged(self) 93 Observable.notifyObservers(self, CardConnectionEvent('disconnect'))
94
95 - def getATR(self):
96 """Return card ATR""" 97 pass
98
99 - def getProtocol(self):
100 """Return bit mask for the protocol of connection, or None if no 101 protocol set. The return value is a bit mask of 102 CardConnection.T0_protocol, CardConnection.T1_protocol, 103 CardConnection.RAW_protocol, CardConnection.T15_protocol 104 """ 105 return self.defaultprotocol
106
107 - def getReader(self):
108 """Return card connection reader""" 109 return self.reader
110
111 - def setErrorCheckingChain(self, errorcheckingchain):
112 """Add an error checking chain. 113 @param errorcheckingchain: a smartcard.sw.ErrorCheckingChain object The 114 error checking strategies in errorchecking chain will be tested 115 with each received response APDU, and a 116 smartcard.sw.SWException.SWException will be raised upon 117 error.""" 118 self.errorcheckingchain = errorcheckingchain
119
120 - def setProtocol(self, protocol):
121 """Set protocol for card connection. 122 @param protocol: a bit mask of CardConnection.T0_protocol, 123 CardConnection.T1_protocol, CardConnection.RAW_protocol, 124 CardConnection.T15_protocol e.g. 125 setProtocol(CardConnection.T1_protocol | 126 CardConnection.T0_protocol) """ 127 self.defaultprotocol = protocol
128
129 - def transmit(self, bytes, protocol=None):
130 """Transmit an apdu. Internally calls doTransmit() class method 131 and notify observers upon command/response APDU events. 132 Subclasses must override the doTransmit() class method. 133 134 @param bytes: list of bytes to transmit 135 136 @param protocol: the transmission protocol, from 137 CardConnection.T0_protocol, 138 CardConnection.T1_protocol, or 139 CardConnection.RAW_protocol 140 """ 141 Observable.setChanged(self) 142 Observable.notifyObservers(self, 143 CardConnectionEvent( 144 'command', 145 [bytes, protocol])) 146 data, sw1, sw2 = self.doTransmit(bytes, protocol) 147 Observable.setChanged(self) 148 Observable.notifyObservers(self, 149 CardConnectionEvent( 150 'response', 151 [data, sw1, sw2])) 152 if None != self.errorcheckingchain: 153 self.errorcheckingchain[0](data, sw1, sw2) 154 return data, sw1, sw2
155
156 - def doTransmit(self, bytes, protocol):
157 """Performs the command APDU transmission. 158 159 Subclasses must override this method for implementing apdu 160 transmission.""" 161 pass
162
163 - def control(self, controlCode, bytes=[]):
164 """Send a control command and buffer. Internally calls doControl() 165 class method and notify observers upon command/response events. 166 Subclasses must override the doControl() class method. 167 168 @param controlCode: command code 169 170 @param bytes: list of bytes to transmit 171 """ 172 Observable.setChanged(self) 173 Observable.notifyObservers(self, 174 CardConnectionEvent( 175 'command', 176 [controlCode, bytes])) 177 data = self.doControl(controlCode, bytes) 178 Observable.setChanged(self) 179 Observable.notifyObservers(self, 180 CardConnectionEvent( 181 'response', 182 data)) 183 if None != self.errorcheckingchain: 184 self.errorcheckingchain[0](data) 185 return data
186
187 - def doControl(self, controlCode, bytes):
188 """Performs the command control. 189 190 Subclasses must override this method for implementing control.""" 191 pass
192
193 - def getAttrib(self, attribId):
194 """return the requested attribute 195 196 @param attribId: attribute id like SCARD_ATTR_VENDOR_NAME 197 """ 198 Observable.setChanged(self) 199 Observable.notifyObservers(self, 200 CardConnectionEvent( 201 'attrib', 202 [attribId])) 203 data = self.doGetAttrib(attribId) 204 if None != self.errorcheckingchain: 205 self.errorcheckingchain[0](data) 206 return data
207
208 - def doGetAttrib(self, attribId):
209 """Performs the command get attrib. 210 211 Subclasses must override this method for implementing get attrib.""" 212 pass
213
214 - def __enter__(self):
215 """Enter the runtime context. 216 """ 217 return self
218
219 - def __exit__(self, type, value, traceback):
220 """Exit the runtime context trying to disconnect. 221 """ 222 self.disconnect()
223