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-2011 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.Exceptions import SmartcardException 
 28  from smartcard.Observer import Observer 
 29  from smartcard.Observer import Observable 
 30   
 31   
32 -class CardConnection(Observable):
33 """Card connection abstract class. 34 35 Known subclasses: smartcard.pcsc.PCSCCardConnection 36 """ 37 T0_protocol = 0x00000001 38 T1_protocol = 0x00000002 39 RAW_protocol = 0x00010000 40 T15_protocol = 0x00000008 41
42 - def __init__(self, reader):
43 """Construct a new card connection. 44 45 readerName: name of the reader in which the smartcard to connect 46 to is located. 47 """ 48 Observable.__init__(self) 49 self.reader = reader 50 self.errorcheckingchain = None 51 self.defaultprotocol = CardConnection.T0_protocol | CardConnection.T1_protocol
52
53 - def __del__(self):
54 """Connect to card.""" 55 pass
56
57 - def addSWExceptionToFilter(self, exClass):
58 """Add a status word exception class to be filtered. 59 60 exClass: the class to filter, e.g. 61 smartcard.sw.SWException.WarningProcessingException 62 63 Filtered exceptions will not be raised when encountered in the 64 error checking chain.""" 65 if None != self.errorcheckingchain: 66 self.errorcheckingchain[0].addFilterException(exClass)
67
68 - def addObserver(self, observer):
69 """Add a CardConnection observer.""" 70 Observable.addObserver(self, observer)
71
72 - def deleteObserver(self, observer):
73 """Remove a CardConnection observer.""" 74 Observable.deleteObserver(self, observer)
75
76 - def connect(self, protocol=None, mode=None, disposition=None):
77 """Connect to card. 78 protocol: a bit mask of the protocols to use, from 79 CardConnection.T0_protocol, CardConnection.T1_protocol, 80 CardConnection.RAW_protocol, CardConnection.T15_protocol 81 82 mode: passed as-is to the PC/SC layer 83 """ 84 Observable.setChanged(self) 85 Observable.notifyObservers(self, CardConnectionEvent('connect'))
86
87 - def disconnect(self):
88 """Disconnect from card.""" 89 Observable.setChanged(self) 90 Observable.notifyObservers(self, CardConnectionEvent('disconnect'))
91
92 - def getATR(self):
93 """Return card ATR""" 94 pass
95
96 - def getProtocol(self):
97 """Return bit mask for the protocol of connection, or None if no 98 protocol set. The return value is a bit mask of 99 CardConnection.T0_protocol, CardConnection.T1_protocol, 100 CardConnection.RAW_protocol, CardConnection.T15_protocol 101 """ 102 return self.defaultprotocol
103
104 - def getReader(self):
105 """Return card connection reader""" 106 return self.reader
107
108 - def setErrorCheckingChain(self, errorcheckingchain):
109 """Add an error checking chain. 110 errorcheckingchain: a smartcard.sw.ErrorCheckingChain object The 111 error checking strategies in errorchecking chain will be tested 112 with each received response APDU, and a 113 smartcard.sw.SWException.SWException will be raised upon 114 error.""" 115 self.errorcheckingchain = errorcheckingchain
116
117 - def setProtocol(self, protocol):
118 """Set protocol for card connection. 119 protocol: a bit mask of CardConnection.T0_protocol, 120 CardConnection.T1_protocol, CardConnection.RAW_protocol, 121 CardConnection.T15_protocol e.g. 122 setProtocol(CardConnection.T1_protocol | 123 CardConnection.T0_protocol) """ 124 self.defaultprotocol = protocol
125
126 - def transmit(self, bytes, protocol=None):
127 """Transmit an apdu. Internally calls doTransmit() class method 128 and notify observers upon command/response APDU events. 129 Subclasses must override the doTransmit() class method. 130 131 bytes: list of bytes to transmit 132 133 protocol: the transmission protocol, from 134 CardConnection.T0_protocol, 135 CardConnection.T1_protocol, or 136 CardConnection.RAW_protocol 137 """ 138 Observable.setChanged(self) 139 Observable.notifyObservers(self, CardConnectionEvent('command', [bytes, protocol])) 140 data, sw1, sw2 = self.doTransmit(bytes, protocol) 141 Observable.setChanged(self) 142 Observable.notifyObservers(self, CardConnectionEvent('response', [data, sw1, sw2])) 143 if None != self.errorcheckingchain: 144 self.errorcheckingchain[0](data, sw1, sw2) 145 return data, sw1, sw2
146
147 - def doTransmit(self, bytes, protocol):
148 """Performs the command APDU transmission. 149 150 Subclasses must override this method for implementing apdu 151 transmission.""" 152 pass
153
154 - def control(self, controlCode, bytes=[]):
155 """Send a control command and buffer. Internally calls doControl() 156 class method and notify observers upon command/response events. 157 Subclasses must override the doControl() class method. 158 159 controlCode: command code 160 161 bytes: list of bytes to transmit 162 """ 163 Observable.setChanged(self) 164 Observable.notifyObservers(self, CardConnectionEvent('command', [controlCode, bytes])) 165 data = self.doControl(controlCode, bytes) 166 Observable.setChanged(self) 167 Observable.notifyObservers(self, CardConnectionEvent('response', data)) 168 if None != self.errorcheckingchain: 169 self.errorcheckingchain[0](data) 170 return data
171
172 - def doControl(self, controlCode, bytes):
173 """Performs the command control. 174 175 Subclasses must override this method for implementing control.""" 176 pass
177