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
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
51
53 """Connect to card."""
54 pass
55
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
70
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
94
96 """Return card ATR"""
97 pass
98
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
108 """Return card connection reader"""
109 return self.reader
110
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
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
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
188 """Performs the command control.
189
190 Subclasses must override this method for implementing control."""
191 pass
192
207
209 """Performs the command get attrib.
210
211 Subclasses must override this method for implementing get attrib."""
212 pass
213
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