Package smartcard :: Package pcsc :: Module PCSCReader
[hide private]
[frames] | no frames]

Source Code for Module smartcard.pcsc.PCSCReader

  1  """PCSCReader: concrete reader class for PCSC Readers 
  2   
  3  __author__ = "gemalto 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   
 25  from smartcard.CardConnectionDecorator import CardConnectionDecorator 
 26  from smartcard.reader.Reader import Reader 
 27  from smartcard.pcsc.PCSCContext import PCSCContext 
 28  from smartcard.pcsc.PCSCCardConnection import PCSCCardConnection 
 29  from smartcard.Exceptions import * 
 30  from smartcard.pcsc.PCSCExceptions import * 
 31  from smartcard.scard import * 
 32   
 33   
34 -def __PCSCreaders__(hcontext, groups=[]):
35 """Returns the list of PCSC smartcard readers in PCSC group. 36 37 If group is not specified, returns the list of all PCSC smartcard readers. 38 """ 39 40 # in case we have a string instead of a list 41 if isinstance(groups, type("")): 42 groups = [groups] 43 hresult, readers = SCardListReaders(hcontext, groups) 44 if hresult != 0: 45 if hresult == SCARD_E_NO_READERS_AVAILABLE: 46 readers = [] 47 else: 48 raise ListReadersException(hresult) 49 50 return readers
51 52
53 -class PCSCReader(Reader):
54 """PCSC reader class.""" 55
56 - def __init__(self, readername):
57 """Constructs a new PCSC reader.""" 58 Reader.__init__(self, readername)
59
60 - def addtoreadergroup(self, groupname):
61 """Add reader to a reader group.""" 62 63 hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 64 if 0 != hresult: 65 raise EstablishContextException(hresult) 66 try: 67 hresult = SCardIntroduceReader(hcontext, self.name, self.name) 68 if 0 != hresult and SCARD_E_DUPLICATE_READER != hresult: 69 raise IntroduceReaderException(hresult, self.name) 70 hresult = SCardAddReaderToGroup(hcontext, self.name, groupname) 71 if 0 != hresult: 72 raise AddReaderToGroupException(hresult, self.name, groupname) 73 finally: 74 hresult = SCardReleaseContext(hcontext) 75 if 0 != hresult: 76 raise ReleaseContextException(hresult)
77
78 - def removefromreadergroup(self, groupname):
79 """Remove a reader from a reader group""" 80 81 hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) 82 if 0 != hresult: 83 raise EstablishContextException(hresult) 84 try: 85 hresult = SCardRemoveReaderFromGroup(hcontext, self.name, 86 groupname) 87 if 0 != hresult: 88 raise RemoveReaderFromGroupException(hresult, self.name, 89 groupname) 90 finally: 91 hresult = SCardReleaseContext(hcontext) 92 if 0 != hresult: 93 raise ReleaseContextException(hresult)
94
95 - def createConnection(self):
96 """Return a card connection thru PCSC reader.""" 97 return CardConnectionDecorator(PCSCCardConnection(self.name))
98
99 - class Factory:
100
101 - def create(readername):
102 return PCSCReader(readername)
103 create = staticmethod(create)
104
105 - def readers(groups=[]):
106 creaders = [] 107 hcontext = PCSCContext().getContext() 108 109 for reader in __PCSCreaders__(hcontext, groups): 110 creaders.append(PCSCReader.Factory.create(reader)) 111 return creaders
112 readers = staticmethod(readers)
113 114 if __name__ == '__main__': 115 from smartcard.util import * 116 SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02] 117 DF_TELECOM = [0x7F, 0x10] 118 119 creaders = PCSCReader.readers() 120 for reader in creaders: 121 try: 122 print reader.name 123 connection = reader.createConnection() 124 connection.connect() 125 print toHexString(connection.getATR()) 126 data, sw1, sw2 = connection.transmit(SELECT + DF_TELECOM) 127 print "%X %X" % (sw1, sw2) 128 except NoCardException, x: 129 print 'no card in reader' 130