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
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
52
54 """Connect to card."""
55 pass
56
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
71
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
91
93 """Return card ATR"""
94 pass
95
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
105 """Return card connection reader"""
106 return self.reader
107
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
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
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=[]):
171
173 """Performs the command control.
174
175 Subclasses must override this method for implementing control."""
176 pass
177