1#! /usr/bin/env python
2"""
3Sample script that monitors card connection events.
4
5__author__ = "http://www.gemalto.com"
6
7Copyright 2001-2012 gemalto
8Author: Jean-Daniel Aussel, mailto:jean-daniel.aussel@gemalto.com
9
10This file is part of pyscard.
11
12pyscard is free software; you can redistribute it and/or modify
13it under the terms of the GNU Lesser General Public License as published by
14the Free Software Foundation; either version 2.1 of the License, or
15(at your option) any later version.
16
17pyscard is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20GNU Lesser General Public License for more details.
21
22You should have received a copy of the GNU Lesser General Public License
23along with pyscard; if not, write to the Free Software
24Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
25"""
26from __future__ import print_function
27from smartcard.CardType import AnyCardType
28from smartcard.CardRequest import CardRequest
29from smartcard.CardConnectionObserver import ConsoleCardConnectionObserver
30
31# define the apdus used in this script
32GET_RESPONSE = [0XA0, 0XC0, 00, 00]
33SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02]
34DF_TELECOM = [0x7F, 0x10]
35
36
37# request any card type
38cardtype = AnyCardType()
39cardrequest = CardRequest(timeout=1.5, cardType=cardtype)
40cardservice = cardrequest.waitforcard()
41
42
43# attach the console tracer
44observer = ConsoleCardConnectionObserver()
45cardservice.connection.addObserver(observer)
46
47
48# connect to the card and perform a few transmits
49cardservice.connection.connect()
50
51apdu = SELECT + DF_TELECOM
52response, sw1, sw2 = cardservice.connection.transmit(apdu)
53
54if sw1 == 0x9F:
55    apdu = GET_RESPONSE + [sw2]
56    response, sw1, sw2 = cardservice.connection.transmit(apdu)
57
58
59import sys
60if 'win32' == sys.platform:
61    print('press Enter to continue')
62    sys.stdin.read(1)
63