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